Skip to content

Commit

Permalink
editorconfig codestyle options
Browse files Browse the repository at this point in the history
  • Loading branch information
jmarolf committed Nov 5, 2016
1 parent 56c2896 commit b887870
Show file tree
Hide file tree
Showing 21 changed files with 256 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ protected override void AnalyzeNode(SyntaxNodeAnalysisContext context)
}

Diagnostic diagnostic;
var options = context.Options;
var cancellationToken = context.CancellationToken;
Func<SyntaxNode, bool> descendIntoChildren = n =>
{
if (!IsRegularCandidate(n) ||
!TrySimplifyTypeNameExpression(context.SemanticModel, n, context.Options, out diagnostic, context.CancellationToken))
!TrySimplifyTypeNameExpression(context.SemanticModel, n, options, out diagnostic, cancellationToken))
{
return true;
}
Expand All @@ -62,16 +64,16 @@ protected override void AnalyzeNode(SyntaxNodeAnalysisContext context)
// find regular node first - search from top to down. once found one, don't get into its children
foreach (var candidate in context.Node.DescendantNodesAndSelf(descendIntoChildren))
{
context.CancellationToken.ThrowIfCancellationRequested();
cancellationToken.ThrowIfCancellationRequested();
}

// now search structure trivia
foreach (var candidate in context.Node.DescendantNodesAndSelf(descendIntoChildren: n => !IsCrefCandidate(n), descendIntoTrivia: true))
{
context.CancellationToken.ThrowIfCancellationRequested();
cancellationToken.ThrowIfCancellationRequested();

if (IsCrefCandidate(candidate) &&
TrySimplifyTypeNameExpression(context.SemanticModel, candidate, context.Options, out diagnostic, context.CancellationToken))
TrySimplifyTypeNameExpression(context.SemanticModel, candidate, options, out diagnostic, cancellationToken))
{
context.ReportDiagnostic(diagnostic);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,16 @@ private void HandleVariableDeclaration(SyntaxNodeAnalysisContext context)
State state = null;
var shouldAnalyze = false;
var declarationStatement = context.Node;
var optionSet = context.Options.GetOptionSet();
var semanticModel = context.SemanticModel;
var options = context.Options;
var syntaxTree = context.Node.SyntaxTree;
var cancellationToken = context.CancellationToken;
var optionSet = options.GetDocumentOptionSetAsync(syntaxTree, cancellationToken).GetAwaiter().GetResult();
if (optionSet == null)
{
return;
}

var semanticModel = context.SemanticModel;

if (declarationStatement.IsKind(SyntaxKind.VariableDeclaration))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,15 @@ private void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext context)
// out-vars are not supported prior to C# 7.0.
return;
}

var optionSet = context.Options.GetOptionSet();
var options = context.Options;
var syntaxTree = context.Node.SyntaxTree;
var cancellationToken = context.CancellationToken;
var optionSet = options.GetDocumentOptionSetAsync(syntaxTree, cancellationToken).GetAwaiter().GetResult();
if (optionSet == null)
{
return;
}

var option = optionSet.GetOption(CodeStyleOptions.PreferInlinedVariableDeclaration, argumentNode.Language);
if (!option.Value)
{
Expand Down Expand Up @@ -99,7 +106,6 @@ private void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext context)
}

var semanticModel = context.SemanticModel;
var cancellationToken = context.CancellationToken;
var outSymbol = semanticModel.GetSymbolInfo(argumentExpression, cancellationToken).Symbol;
if (outSymbol?.Kind != SymbolKind.Local)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,16 @@ public override void Initialize(AnalysisContext context)

private void SyntaxNodeAction(SyntaxNodeAnalysisContext syntaxContext)
{
var options = syntaxContext.Options.GetOptionSet();
var styleOption = options.GetOption(CSharpCodeStyleOptions.PreferConditionalDelegateCall);
var options = syntaxContext.Options;
var syntaxTree = syntaxContext.Node.SyntaxTree;
var cancellationToken = syntaxContext.CancellationToken;
var optionSet = options.GetDocumentOptionSetAsync(syntaxTree, cancellationToken).GetAwaiter().GetResult();
if (optionSet == null)
{
return;
}

var styleOption = optionSet.GetOption(CSharpCodeStyleOptions.PreferConditionalDelegateCall);
if (!styleOption.Value)
{
// Bail immediately if the user has disabled this feature.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,16 @@ public override void Initialize(AnalysisContext context)

private void AnalyzeSyntax(SyntaxNodeAnalysisContext context)
{
var diagnostic = AnalyzeSyntax(context.Options.GetOptionSet(), (TDeclaration)context.Node);
var options = context.Options;
var syntaxTree = context.Node.SyntaxTree;
var cancellationToken = context.CancellationToken;
var optionSet = options.GetDocumentOptionSetAsync(syntaxTree, cancellationToken).GetAwaiter().GetResult();
if (optionSet == null)
{
return;
}

var diagnostic = AnalyzeSyntax(optionSet, (TDeclaration)context.Node);
if (diagnostic != null)
{
context.ReportDiagnostic(diagnostic);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,16 @@ public override void Initialize(AnalysisContext context)

private void SyntaxNodeAction(SyntaxNodeAnalysisContext syntaxContext)
{
var options = syntaxContext.Options.GetOptionSet();
var styleOption = options.GetOption(CSharpCodeStyleOptions.PreferPatternMatchingOverAsWithNullCheck);
var options = syntaxContext.Options;
var syntaxTree = syntaxContext.Node.SyntaxTree;
var cancellationToken = syntaxContext.CancellationToken;
var optionSet = options.GetDocumentOptionSetAsync(syntaxTree, cancellationToken).GetAwaiter().GetResult();
if (optionSet == null)
{
return;
}

var styleOption = optionSet.GetOption(CSharpCodeStyleOptions.PreferPatternMatchingOverAsWithNullCheck);
if (!styleOption.Value)
{
// Bail immediately if the user has disabled this feature.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,16 @@ public override void Initialize(AnalysisContext context)

private void SyntaxNodeAction(SyntaxNodeAnalysisContext syntaxContext)
{
var options = syntaxContext.Options.GetOptionSet();
var styleOption = options.GetOption(CSharpCodeStyleOptions.PreferPatternMatchingOverIsWithCastCheck);
var options = syntaxContext.Options;
var syntaxTree = syntaxContext.Node.SyntaxTree;
var cancellationToken = syntaxContext.CancellationToken;
var optionSet = options.GetDocumentOptionSetAsync(syntaxTree, cancellationToken).GetAwaiter().GetResult();
if (optionSet == null)
{
return;
}

var styleOption = optionSet.GetOption(CSharpCodeStyleOptions.PreferPatternMatchingOverIsWithCastCheck);
if (!styleOption.Value)
{
// Bail immediately if the user has disabled this feature.
Expand Down
25 changes: 23 additions & 2 deletions src/Features/Core/Portable/Diagnostics/AnalyzerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using Microsoft.CodeAnalysis.Options;
using Roslyn.Utilities;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.CodeAnalysis.Diagnostics
{
Expand Down Expand Up @@ -118,9 +120,28 @@ public static string GetAnalyzerAssemblyName(this DiagnosticAnalyzer analyzer)
return typeInfo.Assembly.GetName().Name;
}

public static OptionSet GetOptionSet(this AnalyzerOptions analyzerOptions)
public static async Task<OptionSet> GetDocumentOptionSetAsync(this AnalyzerOptions analyzerOptions, SyntaxTree syntaxTree, CancellationToken cancellationToken)
{
return (analyzerOptions as WorkspaceAnalyzerOptions)?.Workspace.Options;
var workspace = (analyzerOptions as WorkspaceAnalyzerOptions)?.Workspace;
if (workspace == null)
{
return null;
}

var documentId = workspace.CurrentSolution.GetDocumentId(syntaxTree);
if (documentId == null)
{
return workspace.Options;
}

var document = workspace.CurrentSolution.GetDocument(documentId);
if (document == null)
{
return workspace.Options;
}

var documentOptionSet = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);
return documentOptionSet ?? workspace.Options;
}

internal static void OnAnalyzerException_NoTelemetryLogging(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,15 @@ public override void Initialize(AnalysisContext context)

protected void AnalyzeNode(SyntaxNodeAnalysisContext context)
{
var optionSet = context.Options.GetOptionSet();
var syntaxTree = context.Node.SyntaxTree;
var cancellationToken = context.CancellationToken;
var optionSet = context.Options.GetDocumentOptionSetAsync(syntaxTree, cancellationToken).GetAwaiter().GetResult();
if (optionSet == null)
{
return;
}

var semanticModel = context.SemanticModel;
var cancellationToken = context.CancellationToken;
var language = semanticModel.Language;

// if the user never prefers this style, do not analyze at all.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,14 @@ public bool OpenFileOnly(Workspace workspace)
protected bool TrySimplifyTypeNameExpression(SemanticModel model, SyntaxNode node, AnalyzerOptions analyzerOptions, out Diagnostic diagnostic, CancellationToken cancellationToken)
{
diagnostic = default(Diagnostic);

var optionSet = analyzerOptions.GetOptionSet();
string diagnosticId;

TextSpan issueSpan;
if (!CanSimplifyTypeNameExpressionCore(model, node, optionSet, out issueSpan, out diagnosticId, cancellationToken))
var syntaxTree = node.SyntaxTree;
var optionSet = analyzerOptions.GetDocumentOptionSetAsync(syntaxTree, cancellationToken).GetAwaiter().GetResult();
if (optionSet == null)
{
return false;
}

if (!CanSimplifyTypeNameExpressionCore(model, node, optionSet, out var issueSpan, out string diagnosticId, cancellationToken))
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ private void AnalyzeOperation(OperationAnalysisContext context)
return;
}

// get the option
var optionSet = context.Options.GetOptionSet();
var syntaxTree = context.Operation.Syntax.SyntaxTree;
var cancellationToken = context.CancellationToken;
var optionSet = context.Options.GetDocumentOptionSetAsync(syntaxTree, cancellationToken).GetAwaiter().GetResult();
if (optionSet == null)
{
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ private void AnalyzeSyntax(SyntaxNodeAnalysisContext context)
{
var conditionalExpression = (TConditionalExpressionSyntax)context.Node;

var optionSet = context.Options.GetOptionSet();
var syntaxTree = context.Node.SyntaxTree;
var cancellationTokan = context.CancellationToken;
var optionSet = context.Options.GetDocumentOptionSetAsync(syntaxTree, cancellationTokan).GetAwaiter().GetResult();
if (optionSet == null)
{
return;
}

var option = optionSet.GetOption(CodeStyleOptions.PreferCoalesceExpression, conditionalExpression.Language);
if (!option.Value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,14 @@ private void AnalyzeSyntax(SyntaxNodeAnalysisContext context)
{
var conditionalExpression = (TConditionalExpressionSyntax)context.Node;

var optionSet = context.Options.GetOptionSet();
var syntaxTree = context.Node.SyntaxTree;
var cancellationToken = context.CancellationToken;
var optionSet = context.Options.GetDocumentOptionSetAsync(syntaxTree, cancellationToken).GetAwaiter().GetResult();
if (optionSet == null)
{
return;
}

var option = optionSet.GetOption(CodeStyleOptions.PreferCoalesceExpression, conditionalExpression.Language);
if (!option.Value)
{
Expand Down Expand Up @@ -114,7 +121,6 @@ private void AnalyzeSyntax(SyntaxNodeAnalysisContext context)
return;
}

var cancellationToken = context.CancellationToken;
var type = semanticModel.GetTypeInfo(conditionExpression, cancellationToken);

if (!nullableType.Equals(type.Type?.OriginalDefinition))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,16 @@ private void AnalyzeNode(SyntaxNodeAnalysisContext context, INamedTypeSymbol ien
return;
}

var cancellationToken = context.CancellationToken;
var objectCreationExpression = (TObjectCreationExpressionSyntax)context.Node;
var language = objectCreationExpression.Language;
var syntaxTree = objectCreationExpression.SyntaxTree;
var cancellationToken = context.CancellationToken;
var optionSet = context.Options.GetDocumentOptionSetAsync(syntaxTree, cancellationToken).GetAwaiter().GetResult();
if (optionSet == null)
{
return;
}

var optionSet = context.Options.GetOptionSet();
var option = optionSet.GetOption(CodeStyleOptions.PreferCollectionInitializer, language);
if (!option.Value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@ private void AnalyzeSyntax(SyntaxNodeAnalysisContext context)
return;
}

var optionSet = context.Options.GetOptionSet();
var syntaxTree = conditionalExpression.SyntaxTree;
var cancellationToken = context.CancellationToken;
var optionSet = context.Options.GetDocumentOptionSetAsync(syntaxTree, cancellationToken).GetAwaiter().GetResult();
if (optionSet == null)
{
return;
}

var option = optionSet.GetOption(CodeStyleOptions.PreferNullPropagation, conditionalExpression.Language);
if (!option.Value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,16 @@ private void AnalyzeNode(SyntaxNodeAnalysisContext context)
return;
}

var syntaxTree = context.Node.SyntaxTree;
var cancellationToken = context.CancellationToken;
var optionSet = context.Options.GetDocumentOptionSetAsync(syntaxTree, cancellationToken).GetAwaiter().GetResult();
if (optionSet == null)
{
return;
}

var objectCreationExpression = (TObjectCreationExpressionSyntax)context.Node;
var language = objectCreationExpression.Language;

var optionSet = context.Options.GetOptionSet();
var option = optionSet.GetOption(CodeStyleOptions.PreferObjectInitializer, language);
if (!option.Value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,13 @@ private void AnalyzeOperation(OperationAnalysisContext context)

var throwOperation = (IThrowStatement)context.Operation;
var throwStatement = throwOperation.Syntax;

var optionSet = context.Options.GetOptionSet();
var options = context.Options;
var optionSet = options.GetDocumentOptionSetAsync(syntaxTree, cancellationToken).GetAwaiter().GetResult();
if (optionSet == null)
{
return;
}

var option = optionSet.GetOption(CodeStyleOptions.PreferThrowExpression, throwStatement.Language);
if (!option.Value)
{
Expand Down
Loading

0 comments on commit b887870

Please sign in to comment.