Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Options and CodeStyle APIs to shared layer #42323

Merged
59 commits merged into from
Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
6692e76
Move common Options and CodeStyle related public and internal types i…
mavasani Mar 10, 2020
cd860f7
Add API overloads for new shared layer options types to options servi…
mavasani Mar 10, 2020
a2c78bd
Make similar changes to FormattingOptions and C# FormattingOptions - …
mavasani Mar 10, 2020
762c09c
Some more follow-up changes in CodeStyle related files
mavasani Mar 10, 2020
2848a31
Updating formatting rules and engine to use the shared layer formatti…
mavasani Mar 10, 2020
70b4089
VB CodeStyle related changes. We still haven't moved VB CodeStyle opt…
mavasani Mar 10, 2020
3ba9025
Move bunch of options in Workspaces and Features layers to use the ne…
mavasani Mar 10, 2020
7077431
Move the base CodeStyle and naming analyzer to the new options relate…
mavasani Mar 10, 2020
30b4e99
Some cleanup
mavasani Mar 10, 2020
e96fd8f
VB formatting engine and rules moved to new formatting options API in…
mavasani Mar 10, 2020
e182acd
Move all the shared layer analyzers to use the new options and code s…
mavasani Mar 10, 2020
2143656
Move all the IDE (Features layer) analyzer to the new options and cod…
mavasani Mar 10, 2020
a9da22c
Add BannedSymbols.txt for Features layer to prevent accidental use of…
mavasani Mar 10, 2020
b9cf84d
Changes to CodeStyle layer to delete linked files and trim public APIs
mavasani Mar 10, 2020
00b3593
Move EditorFeatures layer to new options types
mavasani Mar 10, 2020
6e1e5dd
Move VS layer and some of the options it depends on to the new intern…
mavasani Mar 10, 2020
74b90a8
Move part of EditorConfigFileGenerator that can serialize naming opti…
mavasani Mar 10, 2020
f1b4348
Split the CodeStyle test utilities project into separate projects for…
mavasani Mar 10, 2020
cbead33
Fix some build and test failures
mavasani Mar 10, 2020
e4c7e8e
Cleanup the conditional directives in analyzer/fixer tests in shared …
mavasani Mar 10, 2020
7e3c47e
Tons of test changes to move them to the new Options APIs.
mavasani Mar 10, 2020
c765e1f
Commit for all project file, resx moves and xlf file changes.
mavasani Mar 10, 2020
11d03e1
Fix nullable warning
mavasani Mar 11, 2020
80a215d
Merge remote-tracking branch 'upstream/master' into OptionsRefactor
mavasani Mar 11, 2020
d6bc6f6
Merge remote-tracking branch 'upstream/master' into OptionsRefactor
mavasani Mar 16, 2020
193ac53
Use OptionDefinition to ensure options are equtable - this is needed …
mavasani Mar 17, 2020
8b547f4
Merge remote-tracking branch 'upstream/master' into OptionsRefactor
mavasani Mar 17, 2020
b704f66
Fix merge conflict
mavasani Mar 17, 2020
c45db4c
Override equality operators
mavasani Mar 17, 2020
fb10c5e
Retain the exception checks from public APIs - these are guarded by u…
mavasani Mar 17, 2020
04056c0
Switch to explicit conversion operators between option types
mavasani Mar 18, 2020
de4d5d9
Merge remote-tracking branch 'upstream/master' into OptionsRefactor
mavasani Mar 18, 2020
7301aa3
Fix merge conflicts
mavasani Mar 18, 2020
0ff090f
Add inheritdoc documentation comments for public Options API. I will …
mavasani Mar 18, 2020
f47f6d1
Fix test failures caused by prior commit
mavasani Mar 18, 2020
50287de
Switch back to implicit operators for conversions to and from CodeSty…
mavasani Mar 18, 2020
4985a18
Address feedback
mavasani Mar 19, 2020
0066c5c
Merge remote-tracking branch 'upstream/master' into OptionsRefactor
mavasani Mar 19, 2020
f52f4fe
Revert unintentional commit
mavasani Mar 19, 2020
736ba89
Address more feedback
mavasani Mar 19, 2020
0f22449
Merge remote-tracking branch 'upstream/master' into OptionsRefactor
mavasani Mar 19, 2020
9793b0b
Fix build break from the refactoring in the prior commit
mavasani Mar 19, 2020
6192d8e
Revert changes to PythiaOptions in ExternalAccessLayer
mavasani Mar 19, 2020
759dc07
Fix RS0038 (use null instead of default) in test code
mavasani Mar 19, 2020
e8464de
Merge remote-tracking branch 'upstream/master' into OptionsRefactor
mavasani Mar 19, 2020
8961e3b
Revert API changes to options used by internal partners through IVT
mavasani Mar 19, 2020
d02be0c
Revert changes to EditorComponentOnOffOptions - these are actually no…
mavasani Mar 20, 2020
02f40aa
Add back cref to PreferBracesPreference.WhenMultiline
mavasani Mar 20, 2020
a56b5a7
Enable nullable reference types for operators
sharwell Mar 20, 2020
717c508
Unwrap CodeStyleOption<T> without allocating
sharwell Mar 20, 2020
17ce912
Improve accuracy of TestHostAnalyzers
sharwell Mar 20, 2020
c533f82
Use explicit operators, since they are no longer used implicitly
sharwell Mar 20, 2020
6878054
Ensure that we use "CodeStyleOption" as the serialization element nam…
mavasani Mar 20, 2020
f139867
Address feedback
mavasani Mar 20, 2020
6aa9798
Add matrix of tests for Get/Set option APIs on OptionSet and OptionSe…
mavasani Mar 20, 2020
81ff9eb
Address feedback
mavasani Mar 20, 2020
17fbad5
Merge remote-tracking branch 'upstream/master' into OptionsRefactor
mavasani Mar 20, 2020
4c01f51
Merge remote-tracking branch 'upstream/master' into OptionsRefactor
mavasani Mar 23, 2020
950b3b6
Fix conflicts
mavasani Mar 23, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
using System.Diagnostics;
using System.Linq;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Options;
using Roslyn.Utilities;

#if CODE_STYLE
using Microsoft.CodeAnalysis.Internal.Options;
using OptionSet = Microsoft.CodeAnalysis.Diagnostics.AnalyzerConfigOptions;
#else
using Microsoft.CodeAnalysis.Options;
#endif

namespace Microsoft.CodeAnalysis.CodeStyle
Expand Down
25 changes: 13 additions & 12 deletions src/Analyzers/Core/Analyzers/Helpers/AnalyzerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@

#nullable enable

using Microsoft.CodeAnalysis.Options;

#if CODE_STYLE
using Microsoft.CodeAnalysis.Internal.Options;
using TOption = Microsoft.CodeAnalysis.Options.IOption2;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A rare case where #if CODE_STYLE in a helper methods file is helping avoid duplicate code, which needs to operate on core fields of IOption and IOptions.

#else
using System.Threading;
using Microsoft.CodeAnalysis.Options;
using TOption = Microsoft.CodeAnalysis.Options.IOption;
#endif

namespace Microsoft.CodeAnalysis.Diagnostics
{
internal static partial class AnalyzerHelper
{
public static T GetOption<T>(this SemanticModelAnalysisContext context, Option<T> option)
public static T GetOption<T>(this SemanticModelAnalysisContext context, Option2<T> option)
{
var analyzerOptions = context.Options;
var syntaxTree = context.SemanticModel.SyntaxTree;
Expand All @@ -24,7 +25,7 @@ public static T GetOption<T>(this SemanticModelAnalysisContext context, Option<T
return GetOption(analyzerOptions, option, syntaxTree, cancellationToken);
}

public static T GetOption<T>(this SyntaxNodeAnalysisContext context, Option<T> option)
public static T GetOption<T>(this SyntaxNodeAnalysisContext context, Option2<T> option)
{
var analyzerOptions = context.Options;
var syntaxTree = context.Node.SyntaxTree;
Expand All @@ -33,7 +34,7 @@ public static T GetOption<T>(this SyntaxNodeAnalysisContext context, Option<T> o
return GetOption(analyzerOptions, option, syntaxTree, cancellationToken);
}

public static T GetOption<T>(this SyntaxTreeAnalysisContext context, Option<T> option)
public static T GetOption<T>(this SyntaxTreeAnalysisContext context, Option2<T> option)
{
var analyzerOptions = context.Options;
var syntaxTree = context.Tree;
Expand All @@ -42,7 +43,7 @@ public static T GetOption<T>(this SyntaxTreeAnalysisContext context, Option<T> o
return GetOption(analyzerOptions, option, syntaxTree, cancellationToken);
}

public static T GetOption<T>(this OperationAnalysisContext context, Option<T> option)
public static T GetOption<T>(this OperationAnalysisContext context, Option2<T> option)
{
var analyzerOptions = context.Options;
var syntaxTree = context.Operation.Syntax.SyntaxTree;
Expand All @@ -51,7 +52,7 @@ public static T GetOption<T>(this OperationAnalysisContext context, Option<T> op
return GetOption(analyzerOptions, option, syntaxTree, cancellationToken);
}

public static T GetOption<T>(this SemanticModelAnalysisContext context, PerLanguageOption<T> option, string? language)
public static T GetOption<T>(this SemanticModelAnalysisContext context, PerLanguageOption2<T> option, string? language)
{
var analyzerOptions = context.Options;
var syntaxTree = context.SemanticModel.SyntaxTree;
Expand All @@ -60,7 +61,7 @@ public static T GetOption<T>(this SemanticModelAnalysisContext context, PerLangu
return GetOption(analyzerOptions, option, language, syntaxTree, cancellationToken);
}

public static T GetOption<T>(this SyntaxNodeAnalysisContext context, PerLanguageOption<T> option, string? language)
public static T GetOption<T>(this SyntaxNodeAnalysisContext context, PerLanguageOption2<T> option, string? language)
{
var analyzerOptions = context.Options;
var syntaxTree = context.Node.SyntaxTree;
Expand All @@ -69,7 +70,7 @@ public static T GetOption<T>(this SyntaxNodeAnalysisContext context, PerLanguage
return GetOption(analyzerOptions, option, language, syntaxTree, cancellationToken);
}

public static T GetOption<T>(this SyntaxTreeAnalysisContext context, PerLanguageOption<T> option, string? language)
public static T GetOption<T>(this SyntaxTreeAnalysisContext context, PerLanguageOption2<T> option, string? language)
{
var analyzerOptions = context.Options;
var syntaxTree = context.Tree;
Expand All @@ -78,7 +79,7 @@ public static T GetOption<T>(this SyntaxTreeAnalysisContext context, PerLanguage
return GetOption(analyzerOptions, option, language, syntaxTree, cancellationToken);
}

public static T GetOption<T>(this OperationAnalysisContext context, PerLanguageOption<T> option, string? language)
public static T GetOption<T>(this OperationAnalysisContext context, PerLanguageOption2<T> option, string? language)
{
var analyzerOptions = context.Options;
var syntaxTree = context.Operation.Syntax.SyntaxTree;
Expand All @@ -87,7 +88,7 @@ public static T GetOption<T>(this OperationAnalysisContext context, PerLanguageO
return GetOption(analyzerOptions, option, language, syntaxTree, cancellationToken);
}

public static bool TryGetEditorConfigOption<T>(this AnalyzerOptions analyzerOptions, IOption option, SyntaxTree syntaxTree, out T value)
public static bool TryGetEditorConfigOption<T>(this AnalyzerOptions analyzerOptions, TOption option, SyntaxTree syntaxTree, out T value)
{
var configOptions = analyzerOptions.AnalyzerConfigOptionsProvider.GetOptions(syntaxTree);
return configOptions.TryGetEditorConfigOption(option, out value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;

#if CODE_STYLE
using Microsoft.CodeAnalysis.Internal.Options;
#else
using Microsoft.CodeAnalysis.Options;
#endif

namespace Microsoft.CodeAnalysis.Diagnostics
{
Expand All @@ -23,10 +18,10 @@ namespace Microsoft.CodeAnalysis.Diagnostics
/// </summary>
internal static class IDEDiagnosticIdToOptionMappingHelper
{
private static readonly ConcurrentDictionary<string, ImmutableHashSet<IOption>> s_diagnosticIdToOptionMap = new ConcurrentDictionary<string, ImmutableHashSet<IOption>>();
private static readonly ConcurrentDictionary<string, ConcurrentDictionary<string, ImmutableHashSet<IOption>>> s_diagnosticIdToLanguageSpecificOptionsMap = new ConcurrentDictionary<string, ConcurrentDictionary<string, ImmutableHashSet<IOption>>>();
private static readonly ConcurrentDictionary<string, ImmutableHashSet<IOption2>> s_diagnosticIdToOptionMap = new ConcurrentDictionary<string, ImmutableHashSet<IOption2>>();
private static readonly ConcurrentDictionary<string, ConcurrentDictionary<string, ImmutableHashSet<IOption2>>> s_diagnosticIdToLanguageSpecificOptionsMap = new ConcurrentDictionary<string, ConcurrentDictionary<string, ImmutableHashSet<IOption2>>>();

public static bool TryGetMappedOptions(string diagnosticId, string language, out ImmutableHashSet<IOption> options)
public static bool TryGetMappedOptions(string diagnosticId, string language, out ImmutableHashSet<IOption2> options)
=> s_diagnosticIdToOptionMap.TryGetValue(diagnosticId, out options) ||
(s_diagnosticIdToLanguageSpecificOptionsMap.TryGetValue(language, out var map) &&
map.TryGetValue(diagnosticId, out options));
Expand All @@ -36,7 +31,7 @@ public static void AddOptionMapping(string diagnosticId, ImmutableHashSet<IPerLa
diagnosticId = diagnosticId ?? throw new ArgumentNullException(nameof(diagnosticId));
perLanguageOptions = perLanguageOptions ?? throw new ArgumentNullException(nameof(perLanguageOptions));

var options = perLanguageOptions.Cast<IOption>().ToImmutableHashSet();
var options = perLanguageOptions.Cast<IOption2>().ToImmutableHashSet();
AddOptionMapping(s_diagnosticIdToOptionMap, diagnosticId, options);
}
public static void AddOptionMapping(string diagnosticId, ImmutableHashSet<ILanguageSpecificOption> languageSpecificOptions, string language)
Expand All @@ -45,12 +40,12 @@ public static void AddOptionMapping(string diagnosticId, ImmutableHashSet<ILangu
languageSpecificOptions = languageSpecificOptions ?? throw new ArgumentNullException(nameof(languageSpecificOptions));
language = language ?? throw new ArgumentNullException(nameof(language));

var map = s_diagnosticIdToLanguageSpecificOptionsMap.GetOrAdd(language, _ => new ConcurrentDictionary<string, ImmutableHashSet<IOption>>());
var options = languageSpecificOptions.Cast<IOption>().ToImmutableHashSet();
var map = s_diagnosticIdToLanguageSpecificOptionsMap.GetOrAdd(language, _ => new ConcurrentDictionary<string, ImmutableHashSet<IOption2>>());
var options = languageSpecificOptions.Cast<IOption2>().ToImmutableHashSet();
AddOptionMapping(map, diagnosticId, options);
}

private static void AddOptionMapping(ConcurrentDictionary<string, ImmutableHashSet<IOption>> map, string diagnosticId, ImmutableHashSet<IOption> options)
private static void AddOptionMapping(ConcurrentDictionary<string, ImmutableHashSet<IOption2>> map, string diagnosticId, ImmutableHashSet<IOption2> options)
{
// Verify that the option is either being added for the first time, or the existing option is already the same.
// Latter can happen in tests as we re-instantiate the analyzer for every test, which attempts to add the mapping every time.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,11 @@
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.NamingStyles;
using Microsoft.CodeAnalysis.Simplification;
using Roslyn.Utilities;

#if CODE_STYLE
using Microsoft.CodeAnalysis.Internal.Options;
#else
using Microsoft.CodeAnalysis.Options;
#endif

namespace Microsoft.CodeAnalysis.Diagnostics.Analyzers.NamingStyles
{
internal abstract class NamingStyleDiagnosticAnalyzerBase<TLanguageKindEnum>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ namespace Microsoft.CodeAnalysis.CSharp.Completion
internal static class CSharpCompletionOptions
{
[Obsolete("This option is superceded by CompletionOptions.EnterKeyBehavior")]
public static readonly Option<bool> AddNewLineOnEnterAfterFullyTypedWord = new Option<bool>(nameof(CSharpCompletionOptions), nameof(AddNewLineOnEnterAfterFullyTypedWord), defaultValue: false);
public static readonly Option2<bool> AddNewLineOnEnterAfterFullyTypedWord = new Option2<bool>(nameof(CSharpCompletionOptions), nameof(AddNewLineOnEnterAfterFullyTypedWord), defaultValue: false);

[Obsolete("This option is superceded by CompletionOptions.SnippetsBehavior")]
public static readonly Option<bool> IncludeSnippets = new Option<bool>(nameof(CSharpCompletionOptions), nameof(IncludeSnippets), defaultValue: true);
public static readonly Option2<bool> IncludeSnippets = new Option2<bool>(nameof(CSharpCompletionOptions), nameof(IncludeSnippets), defaultValue: true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System;
using System.Threading;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Options;

namespace Microsoft.CodeAnalysis.CodeStyle
{
Expand Down Expand Up @@ -51,7 +50,7 @@ public sealed override DiagnosticAnalyzerCategory GetAnalyzerCategory()
/// To that end, we don't let the subclass have direct access to the real <see
/// cref="Diagnostics.AnalysisContext"/>. Instead, we pass this type to the subclass for it
/// register with. We then check if the registration should proceed given the <see
/// cref="CodeStyleOption{T}"/>
/// cref="CodeStyleOption2{T}"/>
/// and the current <see cref="SyntaxTree"/> being processed. If not, we don't do the
/// actual registration.
/// </summary>
Expand All @@ -73,29 +72,29 @@ public void RegisterCompilationStartAction(Action<Compilation, AnalysisContext>
c => analyze(c.Compilation, _this));
}

public void RegisterCodeBlockAction(Action<CodeBlockAnalysisContext, CodeStyleOption<TOptionKind>> analyze)
public void RegisterCodeBlockAction(Action<CodeBlockAnalysisContext, CodeStyleOption2<TOptionKind>> analyze)
{
var provider = _codeStyleProvider;
_context.RegisterCodeBlockAction(
c => AnalyzeIfEnabled(provider, c, analyze, c.Options, c.SemanticModel.SyntaxTree, c.CancellationToken));
}

public void RegisterSemanticModelAction(Action<SemanticModelAnalysisContext, CodeStyleOption<TOptionKind>> analyze)
public void RegisterSemanticModelAction(Action<SemanticModelAnalysisContext, CodeStyleOption2<TOptionKind>> analyze)
{
var provider = _codeStyleProvider;
_context.RegisterSemanticModelAction(
c => AnalyzeIfEnabled(provider, c, analyze, c.Options, c.SemanticModel.SyntaxTree, c.CancellationToken));
}

public void RegisterSyntaxTreeAction(Action<SyntaxTreeAnalysisContext, CodeStyleOption<TOptionKind>> analyze)
public void RegisterSyntaxTreeAction(Action<SyntaxTreeAnalysisContext, CodeStyleOption2<TOptionKind>> analyze)
{
var provider = _codeStyleProvider;
_context.RegisterSyntaxTreeAction(
c => AnalyzeIfEnabled(provider, c, analyze, c.Options, c.Tree, c.CancellationToken));
}

public void RegisterOperationAction(
Action<OperationAnalysisContext, CodeStyleOption<TOptionKind>> analyze,
Action<OperationAnalysisContext, CodeStyleOption2<TOptionKind>> analyze,
params OperationKind[] operationKinds)
{
var provider = _codeStyleProvider;
Expand All @@ -105,7 +104,7 @@ public void RegisterOperationAction(
}

public void RegisterSyntaxNodeAction<TSyntaxKind>(
Action<SyntaxNodeAnalysisContext, CodeStyleOption<TOptionKind>> analyze,
Action<SyntaxNodeAnalysisContext, CodeStyleOption2<TOptionKind>> analyze,
params TSyntaxKind[] syntaxKinds) where TSyntaxKind : struct
{
var provider = _codeStyleProvider;
Expand All @@ -115,7 +114,7 @@ public void RegisterSyntaxNodeAction<TSyntaxKind>(
}

private static void AnalyzeIfEnabled<TContext>(
TCodeStyleProvider provider, TContext context, Action<TContext, CodeStyleOption<TOptionKind>> analyze,
TCodeStyleProvider provider, TContext context, Action<TContext, CodeStyleOption2<TOptionKind>> analyze,
AnalyzerOptions options, SyntaxTree syntaxTree, CancellationToken cancellationToken)
{
var optionValue = options.GetOption(provider._option, syntaxTree, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ internal abstract partial class AbstractCodeStyleProvider<
TOptionKind, TCodeStyleProvider>
where TCodeStyleProvider : AbstractCodeStyleProvider<TOptionKind, TCodeStyleProvider>, new()
{
private readonly Option<CodeStyleOption<TOptionKind>> _option;
private readonly Option2<CodeStyleOption2<TOptionKind>> _option;
private readonly string _language;
private readonly string _descriptorId;
private readonly LocalizableString _title;
private readonly LocalizableString _message;

protected AbstractCodeStyleProvider(
Option<CodeStyleOption<TOptionKind>> option,
Option2<CodeStyleOption2<TOptionKind>> option,
string language,
string descriptorId,
LocalizableString title,
Expand All @@ -58,7 +58,7 @@ protected AbstractCodeStyleProvider(
/// handle ReportDiagnostic.Default and will map that back to the appropriate value in that
/// case.
/// </summary>
protected static ReportDiagnostic GetOptionSeverity(CodeStyleOption<TOptionKind> optionValue)
protected static ReportDiagnostic GetOptionSeverity(CodeStyleOption2<TOptionKind> optionValue)
{
var severity = optionValue.Notification.Severity;
return severity == ReportDiagnostic.Default
Expand Down
Loading