Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -95,24 +95,45 @@ public static implicit operator AnalyzerOptionsProvider(CSharpAnalyzerOptionsPro

internal static class CSharpAnalyzerOptionsProviders
{
public static CSharpAnalyzerOptionsProvider GetCSharpAnalyzerOptions(this AnalyzerOptions options, SyntaxTree syntaxTree)
extension(AnalyzerOptions options)
{
public CSharpAnalyzerOptionsProvider GetCSharpAnalyzerOptions(SyntaxTree syntaxTree)
=> new(options.AnalyzerConfigOptionsProvider.GetOptions(syntaxTree).GetOptionsReader());
}

public static CSharpAnalyzerOptionsProvider GetCSharpAnalyzerOptions(this SemanticModelAnalysisContext context)
extension(SemanticModelAnalysisContext context)
{
public CSharpAnalyzerOptionsProvider GetCSharpAnalyzerOptions()
=> GetCSharpAnalyzerOptions(context.Options, context.SemanticModel.SyntaxTree);
}

public static CSharpAnalyzerOptionsProvider GetCSharpAnalyzerOptions(this SyntaxNodeAnalysisContext context)
extension(SyntaxNodeAnalysisContext context)
{
public CSharpAnalyzerOptionsProvider GetCSharpAnalyzerOptions()
=> GetCSharpAnalyzerOptions(context.Options, context.Node.SyntaxTree);
}

public static CSharpAnalyzerOptionsProvider GetCSharpAnalyzerOptions(this SyntaxTreeAnalysisContext context)
extension(SyntaxTreeAnalysisContext context)
{
public CSharpAnalyzerOptionsProvider GetCSharpAnalyzerOptions()
=> GetCSharpAnalyzerOptions(context.Options, context.Tree);
}

public static CSharpAnalyzerOptionsProvider GetCSharpAnalyzerOptions(this CodeBlockAnalysisContext context)
extension(CodeBlockAnalysisContext context)
{
public CSharpAnalyzerOptionsProvider GetCSharpAnalyzerOptions()
=> GetCSharpAnalyzerOptions(context.Options, context.SemanticModel.SyntaxTree);
}

public static CSharpAnalyzerOptionsProvider GetCSharpAnalyzerOptions(this OperationAnalysisContext context)
extension(OperationAnalysisContext context)
{
public CSharpAnalyzerOptionsProvider GetCSharpAnalyzerOptions()
=> GetCSharpAnalyzerOptions(context.Options, context.Operation.Syntax.SyntaxTree);
}

public static CSharpAnalyzerOptionsProvider GetCSharpAnalyzerOptions(this SymbolStartAnalysisContext context, SyntaxTree syntaxTree)
extension(SymbolStartAnalysisContext context)
{
public CSharpAnalyzerOptionsProvider GetCSharpAnalyzerOptions(SyntaxTree syntaxTree)
=> GetCSharpAnalyzerOptions(context.Options, syntaxTree);
}
}
77 changes: 40 additions & 37 deletions src/Analyzers/CSharp/Tests/UseVarTestExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,83 +22,86 @@ internal static class UseVarTestExtensions
private static readonly CodeStyleOption2<bool> offWithError = new(false, NotificationOption2.Error);
private static readonly CodeStyleOption2<bool> onWithError = new(true, NotificationOption2.Error);

public static OptionsCollection PreferExplicitTypeWithError(this AbstractCodeActionOrUserDiagnosticTest_NoEditor<TestHostDocument, TestHostProject, TestHostSolution, TestWorkspace> test)
extension(AbstractCodeActionOrUserDiagnosticTest_NoEditor<TestHostDocument, TestHostProject, TestHostSolution, TestWorkspace> test)
{
public OptionsCollection PreferExplicitTypeWithError()
=> new(test.GetLanguage())
{
{ CSharpCodeStyleOptions.VarElsewhere, offWithError },
{ CSharpCodeStyleOptions.VarWhenTypeIsApparent, offWithError },
{ CSharpCodeStyleOptions.VarForBuiltInTypes, offWithError },
};

public static OptionsCollection PreferImplicitTypeWithError(this AbstractCodeActionOrUserDiagnosticTest_NoEditor<TestHostDocument, TestHostProject, TestHostSolution, TestWorkspace> test)
=> new(test.GetLanguage())
{
public OptionsCollection PreferImplicitTypeWithError()
=> new(test.GetLanguage())
{
{ CSharpCodeStyleOptions.VarElsewhere, onWithError },
{ CSharpCodeStyleOptions.VarWhenTypeIsApparent, onWithError },
{ CSharpCodeStyleOptions.VarForBuiltInTypes, onWithError },
};
};

public static OptionsCollection PreferExplicitTypeWithWarning(this AbstractCodeActionOrUserDiagnosticTest_NoEditor<TestHostDocument, TestHostProject, TestHostSolution, TestWorkspace> test)
=> new(test.GetLanguage())
{
public OptionsCollection PreferExplicitTypeWithWarning()
=> new(test.GetLanguage())
{
{ CSharpCodeStyleOptions.VarElsewhere, offWithWarning },
{ CSharpCodeStyleOptions.VarWhenTypeIsApparent, offWithWarning },
{ CSharpCodeStyleOptions.VarForBuiltInTypes, offWithWarning },
};
};

public static OptionsCollection PreferImplicitTypeWithWarning(this AbstractCodeActionOrUserDiagnosticTest_NoEditor<TestHostDocument, TestHostProject, TestHostSolution, TestWorkspace> test)
=> new(test.GetLanguage())
{
public OptionsCollection PreferImplicitTypeWithWarning()
=> new(test.GetLanguage())
{
{ CSharpCodeStyleOptions.VarElsewhere, onWithWarning },
{ CSharpCodeStyleOptions.VarWhenTypeIsApparent, onWithWarning },
{ CSharpCodeStyleOptions.VarForBuiltInTypes, onWithWarning },
};
};

public static OptionsCollection PreferExplicitTypeWithInfo(this AbstractCodeActionOrUserDiagnosticTest_NoEditor<TestHostDocument, TestHostProject, TestHostSolution, TestWorkspace> test)
=> new(test.GetLanguage())
{
public OptionsCollection PreferExplicitTypeWithInfo()
=> new(test.GetLanguage())
{
{ CSharpCodeStyleOptions.VarElsewhere, offWithInfo },
{ CSharpCodeStyleOptions.VarWhenTypeIsApparent, offWithInfo },
{ CSharpCodeStyleOptions.VarForBuiltInTypes, offWithInfo },
};
};

public static OptionsCollection PreferImplicitTypeWithInfo(this AbstractCodeActionOrUserDiagnosticTest_NoEditor<TestHostDocument, TestHostProject, TestHostSolution, TestWorkspace> test)
=> new(test.GetLanguage())
{
public OptionsCollection PreferImplicitTypeWithInfo()
=> new(test.GetLanguage())
{
{ CSharpCodeStyleOptions.VarElsewhere, onWithInfo },
{ CSharpCodeStyleOptions.VarWhenTypeIsApparent, onWithInfo },
{ CSharpCodeStyleOptions.VarForBuiltInTypes, onWithInfo },
};
};

public static OptionsCollection PreferExplicitTypeWithSilent(this AbstractCodeActionOrUserDiagnosticTest_NoEditor<TestHostDocument, TestHostProject, TestHostSolution, TestWorkspace> test)
=> new(test.GetLanguage())
{
public OptionsCollection PreferExplicitTypeWithSilent()
=> new(test.GetLanguage())
{
{ CSharpCodeStyleOptions.VarElsewhere, offWithSilent },
{ CSharpCodeStyleOptions.VarWhenTypeIsApparent, offWithSilent },
{ CSharpCodeStyleOptions.VarForBuiltInTypes, offWithSilent },
};
};

public static OptionsCollection PreferImplicitTypeWithSilent(this AbstractCodeActionOrUserDiagnosticTest_NoEditor<TestHostDocument, TestHostProject, TestHostSolution, TestWorkspace> test)
=> new(test.GetLanguage())
{
public OptionsCollection PreferImplicitTypeWithSilent()
=> new(test.GetLanguage())
{
{ CSharpCodeStyleOptions.VarElsewhere, onWithSilent },
{ CSharpCodeStyleOptions.VarWhenTypeIsApparent, onWithSilent },
{ CSharpCodeStyleOptions.VarForBuiltInTypes, onWithSilent },
};
};

public static OptionsCollection PreferExplicitTypeWithNone(this AbstractCodeActionOrUserDiagnosticTest_NoEditor<TestHostDocument, TestHostProject, TestHostSolution, TestWorkspace> test)
=> new(test.GetLanguage())
{
public OptionsCollection PreferExplicitTypeWithNone()
=> new(test.GetLanguage())
{
{ CSharpCodeStyleOptions.VarElsewhere, offWithNone },
{ CSharpCodeStyleOptions.VarWhenTypeIsApparent, offWithNone },
{ CSharpCodeStyleOptions.VarForBuiltInTypes, offWithNone },
};
};

public static OptionsCollection PreferImplicitTypeWithNone(this AbstractCodeActionOrUserDiagnosticTest_NoEditor<TestHostDocument, TestHostProject, TestHostSolution, TestWorkspace> test)
=> new(test.GetLanguage())
{
public OptionsCollection PreferImplicitTypeWithNone()
=> new(test.GetLanguage())
{
{ CSharpCodeStyleOptions.VarElsewhere, onWithNone },
{ CSharpCodeStyleOptions.VarWhenTypeIsApparent, onWithNone },
{ CSharpCodeStyleOptions.VarForBuiltInTypes, onWithNone },
};
};
}
}
30 changes: 24 additions & 6 deletions src/Analyzers/Core/Analyzers/AnalyzerOptionsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,39 @@ internal IOptionsReader GetAnalyzerConfigOptions()

internal static partial class AnalyzerOptionsProviders
{
public static AnalyzerOptionsProvider GetAnalyzerOptions(this AnalyzerOptions analyzerOptions, SyntaxTree syntaxTree)
extension(AnalyzerOptions analyzerOptions)
{
public AnalyzerOptionsProvider GetAnalyzerOptions(SyntaxTree syntaxTree)
=> new(analyzerOptions.AnalyzerConfigOptionsProvider.GetOptions(syntaxTree).GetOptionsReader(), syntaxTree.Options.Language);
}

public static AnalyzerOptionsProvider GetAnalyzerOptions(this SemanticModelAnalysisContext context)
extension(SemanticModelAnalysisContext context)
{
public AnalyzerOptionsProvider GetAnalyzerOptions()
=> GetAnalyzerOptions(context.Options, context.SemanticModel.SyntaxTree);
}

public static AnalyzerOptionsProvider GetAnalyzerOptions(this SyntaxNodeAnalysisContext context)
extension(SyntaxNodeAnalysisContext context)
{
public AnalyzerOptionsProvider GetAnalyzerOptions()
=> GetAnalyzerOptions(context.Options, context.Node.SyntaxTree);
}

public static AnalyzerOptionsProvider GetAnalyzerOptions(this SyntaxTreeAnalysisContext context)
extension(SyntaxTreeAnalysisContext context)
{
public AnalyzerOptionsProvider GetAnalyzerOptions()
=> GetAnalyzerOptions(context.Options, context.Tree);
}

public static AnalyzerOptionsProvider GetAnalyzerOptions(this OperationAnalysisContext context)
extension(OperationAnalysisContext context)
{
public AnalyzerOptionsProvider GetAnalyzerOptions()
=> GetAnalyzerOptions(context.Options, context.Operation.Syntax.SyntaxTree);
}

public static AnalyzerOptionsProvider GetAnalyzerOptions(this CodeBlockAnalysisContext context)
extension(CodeBlockAnalysisContext context)
{
public AnalyzerOptionsProvider GetAnalyzerOptions()
=> GetAnalyzerOptions(context.Options, context.CodeBlock.SyntaxTree);
}
}
5 changes: 4 additions & 1 deletion src/Analyzers/Core/Analyzers/EnforceOnBuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ internal enum EnforceOnBuild

internal static class EnforceOnBuildExtensions
{
public static string ToCustomTag(this EnforceOnBuild enforceOnBuild)
extension(EnforceOnBuild enforceOnBuild)
{
public string ToCustomTag()
=> $"{nameof(EnforceOnBuild)}_{enforceOnBuild}";
}
}
13 changes: 8 additions & 5 deletions src/Analyzers/Core/CodeFixes/AnalyzerOptionsProviders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ namespace Microsoft.CodeAnalysis.Diagnostics;

internal static partial class AnalyzerOptionsProviders
{
public static async ValueTask<AnalyzerOptionsProvider> GetAnalyzerOptionsProviderAsync(this Document document, CancellationToken cancellationToken)
extension(Document document)
{
var syntaxTree = await document.GetRequiredSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);
var analyzerOptions = document.Project.AnalyzerOptions;
var configOptions = analyzerOptions.AnalyzerConfigOptionsProvider.GetOptions(syntaxTree).GetOptionsReader();
public async ValueTask<AnalyzerOptionsProvider> GetAnalyzerOptionsProviderAsync(CancellationToken cancellationToken)
{
var syntaxTree = await document.GetRequiredSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);
var analyzerOptions = document.Project.AnalyzerOptions;
var configOptions = analyzerOptions.AnalyzerConfigOptionsProvider.GetOptions(syntaxTree).GetOptionsReader();

return new AnalyzerOptionsProvider(configOptions, document.Project.Language);
return new AnalyzerOptionsProvider(configOptions, document.Project.Language);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,22 @@ internal static class ImplementTypeOptionsStorage

internal static class ImplementTypeOptionsProviders
{
public static ImplementTypeOptions GetImplementTypeOptions(this IOptionsReader reader, string language)
extension(IOptionsReader reader)
{
public ImplementTypeOptions GetImplementTypeOptions(string language)
=> new()
{
InsertionBehavior = reader.GetOption(ImplementTypeOptionsStorage.InsertionBehavior, language),
PropertyGenerationBehavior = reader.GetOption(ImplementTypeOptionsStorage.PropertyGenerationBehavior, language)
};
}

public static async ValueTask<ImplementTypeOptions> GetImplementTypeOptionsAsync(this Document document, CancellationToken cancellationToken)
extension(Document document)
{
var configOptions = await document.GetHostAnalyzerConfigOptionsAsync(cancellationToken).ConfigureAwait(false);
return configOptions.GetImplementTypeOptions(document.Project.Language);
public async ValueTask<ImplementTypeOptions> GetImplementTypeOptionsAsync(CancellationToken cancellationToken)
{
var configOptions = await document.GetHostAnalyzerConfigOptionsAsync(cancellationToken).ConfigureAwait(false);
return configOptions.GetImplementTypeOptions(document.Project.Language);
}
}
}
41 changes: 22 additions & 19 deletions src/Analyzers/Core/CodeFixes/Naming/NamingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,31 @@ namespace Microsoft.CodeAnalysis.Shared.Extensions;

internal static class NamingExtensions
{
public static async Task<NamingRule> GetApplicableNamingRuleAsync(
this Document document, SymbolKind symbolKind, Accessibility accessibility, CancellationToken cancellationToken)
extension(Document document)
{
var rules = await document.GetNamingRulesAsync(cancellationToken).ConfigureAwait(false);
foreach (var rule in rules)
public async Task<NamingRule> GetApplicableNamingRuleAsync(
SymbolKind symbolKind, Accessibility accessibility, CancellationToken cancellationToken)
{
if (rule.SymbolSpecification.AppliesTo(symbolKind, accessibility))
return rule;
}
var rules = await document.GetNamingRulesAsync(cancellationToken).ConfigureAwait(false);
foreach (var rule in rules)
{
if (rule.SymbolSpecification.AppliesTo(symbolKind, accessibility))
return rule;
}

throw ExceptionUtilities.Unreachable();
}
throw ExceptionUtilities.Unreachable();
}

/// <summary>
/// Gets the set of naming rules the user has set for this document. Will include a set of default naming rules
/// that match if the user hasn't specified any for a particular symbol type. The are added at the end so they
/// will only be used if the user hasn't specified a preference.
/// </summary>
public static async Task<ImmutableArray<NamingRule>> GetNamingRulesAsync(
this Document document, CancellationToken cancellationToken)
{
var options = await document.GetNamingStylePreferencesAsync(cancellationToken).ConfigureAwait(false);
return options.Rules.NamingRules.AddRange(FallbackNamingRules.Default);
/// <summary>
/// Gets the set of naming rules the user has set for this document. Will include a set of default naming rules
/// that match if the user hasn't specified any for a particular symbol type. The are added at the end so they
/// will only be used if the user hasn't specified a preference.
/// </summary>
public async Task<ImmutableArray<NamingRule>> GetNamingRulesAsync(
CancellationToken cancellationToken)
{
var options = await document.GetNamingStylePreferencesAsync(cancellationToken).ConfigureAwait(false);
return options.Rules.NamingRules.AddRange(FallbackNamingRules.Default);
}
}
}
Loading
Loading