提交 59fe532a 编写于 作者: C Cyrus Najmabadi

Simplify and inline

上级 c607bbfd
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.CodeStyle.TypeStyle;
......@@ -16,62 +14,57 @@ internal partial class CSharpTypeStyleHelper
{
protected struct State
{
private readonly Dictionary<UseVarPreference, ReportDiagnostic> _styleToSeverityMap;
public readonly UseVarPreference TypeStylePreference;
public UseVarPreference TypeStylePreference { get; private set; }
public bool IsInIntrinsicTypeContext { get; private set; }
public bool IsTypeApparentInContext { get; private set; }
public bool IsInVariableDeclarationContext { get; }
private readonly ReportDiagnostic _forBuiltInTypes;
private readonly ReportDiagnostic _whenTypeIsApparent;
private readonly ReportDiagnostic _elsewhere;
private State(bool isVariableDeclarationContext)
public readonly bool IsInIntrinsicTypeContext;
public readonly bool IsTypeApparentInContext;
public State(
SyntaxNode declaration, SemanticModel semanticModel,
OptionSet optionSet, CancellationToken cancellationToken)
{
TypeStylePreference = default;
IsInIntrinsicTypeContext = default;
IsTypeApparentInContext = default;
this.IsInVariableDeclarationContext = isVariableDeclarationContext;
_styleToSeverityMap = new Dictionary<UseVarPreference, ReportDiagnostic>();
}
var styleForIntrinsicTypes = optionSet.GetOption(CSharpCodeStyleOptions.VarForBuiltInTypes);
var styleForApparent = optionSet.GetOption(CSharpCodeStyleOptions.VarWhenTypeIsApparent);
var styleForElsewhere = optionSet.GetOption(CSharpCodeStyleOptions.VarElsewhere);
public static State Generate(
SyntaxNode declaration, SemanticModel semanticModel,
OptionSet optionSet, CancellationToken cancellationToken)
{
var isVariableDeclarationContext = declaration.IsKind(SyntaxKind.VariableDeclaration);
var state = new State(isVariableDeclarationContext);
state.Initialize(declaration, semanticModel, optionSet, cancellationToken);
return state;
}
_forBuiltInTypes = styleForIntrinsicTypes.Notification.Severity;
_whenTypeIsApparent = styleForApparent.Notification.Severity;
_elsewhere = styleForElsewhere.Notification.Severity;
public ReportDiagnostic GetDiagnosticSeverityPreference()
{
if (IsInIntrinsicTypeContext)
{
return _styleToSeverityMap[UseVarPreference.ForBuiltInTypes];
}
else if (IsTypeApparentInContext)
{
return _styleToSeverityMap[UseVarPreference.WhenTypeIsApparent];
}
else
{
return _styleToSeverityMap[UseVarPreference.Elsewhere];
}
}
var stylePreferences = UseVarPreference.None;
private void Initialize(SyntaxNode declaration, SemanticModel semanticModel, OptionSet optionSet, CancellationToken cancellationToken)
{
this.TypeStylePreference = GetCurrentTypeStylePreferences(optionSet);
if (styleForIntrinsicTypes.Value)
stylePreferences |= UseVarPreference.ForBuiltInTypes;
if (styleForApparent.Value)
stylePreferences |= UseVarPreference.WhenTypeIsApparent;
if (styleForElsewhere.Value)
stylePreferences |= UseVarPreference.Elsewhere;
this.TypeStylePreference = stylePreferences;
IsTypeApparentInContext =
IsInVariableDeclarationContext
&& IsTypeApparentInDeclaration((VariableDeclarationSyntax)declaration, semanticModel, TypeStylePreference, cancellationToken);
declaration.IsKind(SyntaxKind.VariableDeclaration) &&
IsTypeApparentInDeclaration((VariableDeclarationSyntax)declaration, semanticModel, TypeStylePreference, cancellationToken);
IsInIntrinsicTypeContext =
IsPredefinedTypeInDeclaration(declaration, semanticModel)
|| IsInferredPredefinedType(declaration, semanticModel);
IsPredefinedTypeInDeclaration(declaration, semanticModel) ||
IsInferredPredefinedType(declaration, semanticModel);
}
public ReportDiagnostic GetDiagnosticSeverityPreference()
=> IsInIntrinsicTypeContext ? _forBuiltInTypes :
IsTypeApparentInContext ? _whenTypeIsApparent : _elsewhere;
/// <summary>
/// Returns true if type information could be gleaned by simply looking at the given statement.
/// This typically means that the type name occurs in right hand side of an assignment.
......@@ -159,36 +152,6 @@ private TypeSyntax GetTypeSyntaxFromDeclaration(SyntaxNode declarationStatement)
DeclarationExpressionSyntax declExpr => declExpr.Type,
_ => null,
};
private UseVarPreference GetCurrentTypeStylePreferences(OptionSet optionSet)
{
var stylePreferences = UseVarPreference.None;
var styleForIntrinsicTypes = optionSet.GetOption(CSharpCodeStyleOptions.VarForBuiltInTypes);
var styleForApparent = optionSet.GetOption(CSharpCodeStyleOptions.VarWhenTypeIsApparent);
var styleForElsewhere = optionSet.GetOption(CSharpCodeStyleOptions.VarElsewhere);
_styleToSeverityMap.Add(UseVarPreference.ForBuiltInTypes, styleForIntrinsicTypes.Notification.Severity);
_styleToSeverityMap.Add(UseVarPreference.WhenTypeIsApparent, styleForApparent.Notification.Severity);
_styleToSeverityMap.Add(UseVarPreference.Elsewhere, styleForElsewhere.Notification.Severity);
if (styleForIntrinsicTypes.Value)
{
stylePreferences |= UseVarPreference.ForBuiltInTypes;
}
if (styleForApparent.Value)
{
stylePreferences |= UseVarPreference.WhenTypeIsApparent;
}
if (styleForElsewhere.Value)
{
stylePreferences |= UseVarPreference.Elsewhere;
}
return stylePreferences;
}
}
}
}
......@@ -50,8 +50,7 @@ public bool CanConvert()
internal abstract partial class CSharpTypeStyleHelper
{
protected abstract bool IsStylePreferred(
SemanticModel semanticModel, OptionSet optionSet, State state, CancellationToken cancellationToken);
protected abstract bool IsStylePreferred(in State state);
public virtual TypeStyleResult AnalyzeTypeName(
TypeSyntax typeName, SemanticModel semanticModel,
......@@ -65,9 +64,9 @@ internal abstract partial class CSharpTypeStyleHelper
return default;
}
var state = State.Generate(
var state = new State(
declaration, semanticModel, optionSet, cancellationToken);
var isStylePreferred = this.IsStylePreferred(semanticModel, optionSet, state, cancellationToken);
var isStylePreferred = this.IsStylePreferred(in state);
var severity = state.GetDiagnosticSeverityPreference();
return new TypeStyleResult(
......
......@@ -18,9 +18,7 @@ private CSharpUseExplicitTypeHelper()
{
}
protected override bool IsStylePreferred(
SemanticModel semanticModel, OptionSet optionSet,
State state, CancellationToken cancellationToken)
protected override bool IsStylePreferred(in State state)
{
var stylePreferences = state.TypeStylePreference;
......
......@@ -71,9 +71,7 @@ protected override bool ShouldAnalyzeForEachStatement(ForEachStatementSyntax for
return base.ShouldAnalyzeForEachStatement(forEachStatement, semanticModel, cancellationToken);
}
protected override bool IsStylePreferred(
SemanticModel semanticModel, OptionSet optionSet,
State state, CancellationToken cancellationToken)
protected override bool IsStylePreferred(in State state)
{
var stylePreferences = state.TypeStylePreference;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册