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

Add external access APIs for O# #59469

Merged
merged 11 commits into from
Feb 23, 2022
Merged
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<!-- Versions used by several individual references below -->
<RoslynDiagnosticsNugetPackageVersion>3.3.3-beta1.21105.3</RoslynDiagnosticsNugetPackageVersion>
<MicrosoftCodeAnalysisNetAnalyzersVersion>6.0.0-rc1.21366.2</MicrosoftCodeAnalysisNetAnalyzersVersion>
<MicrosoftCodeAnalysisTestingVersion>1.1.1-beta1.22081.4</MicrosoftCodeAnalysisTestingVersion>
<MicrosoftCodeAnalysisTestingVersion>1.1.2-beta1.22122.4</MicrosoftCodeAnalysisTestingVersion>
<MicrosoftVisualStudioExtensibilityTestingVersion>0.1.127-beta</MicrosoftVisualStudioExtensibilityTestingVersion>
<!-- CodeStyleAnalyzerVersion should we updated together with version of dotnet-format in dotnet-tools.json -->
<CodeStyleAnalyzerVersion>4.0.1</CodeStyleAnalyzerVersion>
Expand Down
4 changes: 2 additions & 2 deletions global.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"xcopy-msbuild": "16.10.0-preview2"
},
"msbuild-sdks": {
"Microsoft.DotNet.Arcade.Sdk": "7.0.0-beta.22117.2",
"Microsoft.DotNet.Helix.Sdk": "7.0.0-beta.22117.2"
"Microsoft.DotNet.Arcade.Sdk": "7.0.0-beta.22080.1",
"Microsoft.DotNet.Helix.Sdk": "7.0.0-beta.22080.1"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected override void InitializeWorker(AnalysisContext context)

private void AnalyzeSemanticModel(SemanticModelAnalysisContext context)
{
var fadeCode = context.GetIdeOptions().FadeOutUnreachableCode;
var fadeCode = context.Options.GetIdeOptions().FadeOutUnreachableCode;
var semanticModel = context.SemanticModel;
var cancellationToken = context.CancellationToken;

Expand Down
52 changes: 25 additions & 27 deletions src/Analyzers/Core/Analyzers/Helpers/AnalyzerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,44 @@
namespace Microsoft.CodeAnalysis.Diagnostics
{
internal readonly record struct IdeAnalyzerOptions(
bool FadeOutUnusedImports,
bool FadeOutUnreachableCode,
bool ReportInvalidPlaceholdersInStringDotFormatCalls,
bool ReportInvalidRegexPatterns)
bool FadeOutUnusedImports = true,
bool FadeOutUnreachableCode = true,
bool ReportInvalidPlaceholdersInStringDotFormatCalls = true,
bool ReportInvalidRegexPatterns = true)
{
public IdeAnalyzerOptions()
: this(FadeOutUnusedImports: true)
{
}

public static readonly IdeAnalyzerOptions Default = new();

public static readonly IdeAnalyzerOptions CodeStyleDefault = new(
FadeOutUnusedImports: false,
FadeOutUnreachableCode: false,
ReportInvalidPlaceholdersInStringDotFormatCalls: true,
ReportInvalidRegexPatterns: true);
}

internal static partial class AnalyzerHelper
{
public static IdeAnalyzerOptions GetIdeOptions(this SyntaxTreeAnalysisContext context)
#if CODE_STYLE
=> IdeAnalyzerOptions.CodeStyleDefault;
#else
=> (context.Options is WorkspaceAnalyzerOptions workspaceOptions) ? workspaceOptions.GetIdeOptions(context.Tree.Options.Language) : IdeAnalyzerOptions.CodeStyleDefault;
#endif

public static IdeAnalyzerOptions GetIdeOptions(this OperationAnalysisContext context)
#if CODE_STYLE
=> IdeAnalyzerOptions.CodeStyleDefault;
#else
=> (context.Options is WorkspaceAnalyzerOptions workspaceOptions) ? workspaceOptions.GetIdeOptions(context.Operation.Language) : IdeAnalyzerOptions.CodeStyleDefault;
#endif
#if !CODE_STYLE
public static IdeAnalyzerOptions FromProject(Project project)
=> From(project.Solution.Options, project.Language);

public static IdeAnalyzerOptions GetIdeOptions(this SyntaxNodeAnalysisContext context)
#if CODE_STYLE
=> IdeAnalyzerOptions.CodeStyleDefault;
#else
=> (context.Options is WorkspaceAnalyzerOptions workspaceOptions) ? workspaceOptions.GetIdeOptions(context.Node.Language) : IdeAnalyzerOptions.CodeStyleDefault;
public static IdeAnalyzerOptions From(OptionSet options, string language)
=> new(
FadeOutUnusedImports: options.GetOption(Microsoft.CodeAnalysis.Fading.FadingOptions.Metadata.FadeOutUnusedImports, language),
FadeOutUnreachableCode: options.GetOption(Microsoft.CodeAnalysis.Fading.FadingOptions.Metadata.FadeOutUnreachableCode, language),
ReportInvalidPlaceholdersInStringDotFormatCalls: options.GetOption(Microsoft.CodeAnalysis.ValidateFormatString.ValidateFormatStringOption.ReportInvalidPlaceholdersInStringDotFormatCalls, language),
ReportInvalidRegexPatterns: options.GetOption(Microsoft.CodeAnalysis.Features.EmbeddedLanguages.RegularExpressions.LanguageServices.RegularExpressionsOptions.ReportInvalidRegexPatterns, language));
#endif
}

public static IdeAnalyzerOptions GetIdeOptions(this SemanticModelAnalysisContext context)
internal static partial class AnalyzerHelper
{
public static IdeAnalyzerOptions GetIdeOptions(this AnalyzerOptions options)
#if CODE_STYLE
=> IdeAnalyzerOptions.CodeStyleDefault;
#else
=> (context.Options is WorkspaceAnalyzerOptions workspaceOptions) ? workspaceOptions.GetIdeOptions(context.SemanticModel.Language) : IdeAnalyzerOptions.CodeStyleDefault;
=> (options is WorkspaceAnalyzerOptions workspaceOptions) ? workspaceOptions.IdeOptions : IdeAnalyzerOptions.CodeStyleDefault;
#endif

public static T GetOption<T>(this SemanticModelAnalysisContext context, Option2<T> option)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private void AnalyzeSemanticModel(SemanticModelAnalysisContext context)
// for us appropriately.
unnecessaryImports = MergeImports(unnecessaryImports);

var fadeOut = context.GetIdeOptions().FadeOutUnusedImports;
var fadeOut = context.Options.GetIdeOptions().FadeOutUnusedImports;

DiagnosticDescriptor descriptor;
if (GeneratedCodeUtilities.IsGeneratedCode(tree, IsRegularCommentOrDocComment, cancellationToken))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private void AnalyzeNode(SyntaxNodeAnalysisContext context, INamedTypeSymbol for
return;
}

if (!context.GetIdeOptions().ReportInvalidPlaceholdersInStringDotFormatCalls)
if (!context.Options.GetIdeOptions().ReportInvalidPlaceholdersInStringDotFormatCalls)
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Options;

namespace Microsoft.CodeAnalysis.ValidateFormatString
Expand All @@ -12,7 +13,7 @@ internal class ValidateFormatStringOption
new(
nameof(ValidateFormatStringOption),
nameof(ReportInvalidPlaceholdersInStringDotFormatCalls),
defaultValue: true,
IdeAnalyzerOptions.Default.ReportInvalidPlaceholdersInStringDotFormatCalls,
storageLocation: new RoamingProfileStorageLocation("TextEditor.%LANGUAGE%.Specific.WarnOnInvalidStringDotFormatCalls"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#nullable disable

using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp;
Expand Down Expand Up @@ -1468,6 +1469,11 @@ class T : A
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsImplementAbstractClass)]
public async Task TestWithGroupingOff1()
{
var options = CodeActionOptions.Default with
{
ImplementTypeOptions = new ImplementTypeOptions(InsertionBehavior: ImplementTypeInsertionBehavior.AtTheEnd)
};

await TestInRegularAndScriptAsync(
@"abstract class Base
{
Expand All @@ -1488,7 +1494,7 @@ class Derived : Base
void Goo() { }

public override int Prop => throw new System.NotImplementedException();
}", options: Option(ImplementTypeOptions.Metadata.InsertionBehavior, ImplementTypeInsertionBehavior.AtTheEnd));
}", codeActionOptions: options);
}

[WorkItem(17274, "https://github.com/dotnet/roslyn/issues/17274")]
Expand Down Expand Up @@ -1641,6 +1647,11 @@ public override void M2<T>(T? i = null)
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsImplementAbstractClass)]
public async Task TestAutoProperties()
{
var options = CodeActionOptions.Default with
{
ImplementTypeOptions = new ImplementTypeOptions(PropertyGenerationBehavior: ImplementTypePropertyGenerationBehavior.PreferAutoProperties)
};

await TestInRegularAndScript1Async(
@"abstract class AbstractClass
{
Expand All @@ -1664,9 +1675,7 @@ class C : AbstractClass
public override int ReadOnlyProp { get; }
public override int ReadWriteProp { get; set; }
public override int WriteOnlyProp { set => throw new System.NotImplementedException(); }
}", parameters: new TestParameters(options: Option(
ImplementTypeOptions.Metadata.PropertyGenerationBehavior,
ImplementTypePropertyGenerationBehavior.PreferAutoProperties)));
}", parameters: new TestParameters(codeActionOptions: options));
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsImplementAbstractClass)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions;
Expand Down Expand Up @@ -7286,10 +7287,10 @@ void M() { }

public int Prop => throw new System.NotImplementedException();
}",
Options =
CodeActionOptions = CodeActionOptions.Default with
{
{ ImplementTypeOptions.Metadata.InsertionBehavior, ImplementTypeInsertionBehavior.AtTheEnd },
},
ImplementTypeOptions = new ImplementTypeOptions(InsertionBehavior: ImplementTypeInsertionBehavior.AtTheEnd)
}
}.RunAsync();
}

Expand Down Expand Up @@ -7461,10 +7462,10 @@ class Class : IInterface
public int ReadWriteProp { get; set; }
public int WriteOnlyProp { set => throw new System.NotImplementedException(); }
}",
Options =
CodeActionOptions = CodeActionOptions.Default with
{
{ ImplementTypeOptions.Metadata.PropertyGenerationBehavior, ImplementTypePropertyGenerationBehavior.PreferAutoProperties },
},
ImplementTypeOptions = new ImplementTypeOptions(PropertyGenerationBehavior: ImplementTypePropertyGenerationBehavior.PreferAutoProperties)
}
}.RunAsync();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.CodeRefactorings;
using Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions;
using Microsoft.CodeAnalysis.ImplementType;
using Microsoft.CodeAnalysis.SymbolSearch;

namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Wrapping
Expand All @@ -19,7 +20,9 @@ protected sealed override ImmutableArray<CodeAction> MassageActions(ImmutableArr
=> FlattenActions(actions);

private protected static CodeActionOptions GetIndentionColumn(int column)
=> new(SymbolSearchOptions.Default, WrappingColumn: column);
=> new(SymbolSearchOptions.Default,
ImplementTypeOptions.Default,
WrappingColumn: column);

protected Task TestAllWrappingCasesAsync(
string input,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.CodeAnalysis.Options;

namespace Microsoft.CodeAnalysis.ImplementType
{
internal static class ImplementTypeOptionsStorage
{
public static ImplementTypeOptions GetImplementTypeOptions(this IGlobalOptionService globalOptions, string language)
=> new(
InsertionBehavior: globalOptions.GetOption(InsertionBehavior, language),
PropertyGenerationBehavior: globalOptions.GetOption(PropertyGenerationBehavior, language));

private const string FeatureName = "ImplementTypeOptions";

public static readonly PerLanguageOption2<ImplementTypeInsertionBehavior> InsertionBehavior =
new(FeatureName,
"InsertionBehavior",
defaultValue: ImplementTypeOptions.Default.InsertionBehavior,
storageLocation: new RoamingProfileStorageLocation("TextEditor.%LANGUAGE%.ImplementTypeOptions.InsertionBehavior"));

public static readonly PerLanguageOption2<ImplementTypePropertyGenerationBehavior> PropertyGenerationBehavior =
new(FeatureName,
"PropertyGenerationBehavior",
defaultValue: ImplementTypeOptions.Default.PropertyGenerationBehavior,
storageLocation: new RoamingProfileStorageLocation("TextEditor.%LANGUAGE%.ImplementTypeOptions.PropertyGenerationBehavior"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System.Collections.Immutable;
using Microsoft.CodeAnalysis.Completion;
using Microsoft.CodeAnalysis.ImplementType;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.SymbolSearch;

Expand All @@ -20,6 +21,7 @@ internal static CodeActionOptions GetBlockingCodeActionOptions(this IGlobalOptio
private static CodeActionOptions GetCodeActionOptions(this IGlobalOptionService globalOptions, string language, bool isBlocking)
=> new(
SearchOptions: globalOptions.GetSymbolSearchOptions(language),
ImplementTypeOptions: globalOptions.GetImplementTypeOptions(language),
HideAdvancedMembers: globalOptions.GetOption(CompletionOptionsStorage.HideAdvancedMembers, language),
IsBlocking: isBlocking);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,14 +376,15 @@ internal Task TestInRegularAndScriptAsync(
CodeActionPriority? priority = null,
CompilationOptions compilationOptions = null,
OptionsCollection options = null,
CodeActionOptions? codeActionOptions = null,
object fixProviderData = null,
ParseOptions parseOptions = null,
string title = null,
TestHost testHost = TestHost.InProcess)
{
return TestInRegularAndScript1Async(
initialMarkup, expectedMarkup, index,
new TestParameters(parseOptions, compilationOptions, options, CodeActionOptions.Default, fixProviderData, index, priority, title: title, testHost: testHost));
new TestParameters(parseOptions, compilationOptions, options, codeActionOptions ?? CodeActionOptions.Default, fixProviderData, index, priority, title: title, testHost: testHost));
}

internal Task TestInRegularAndScript1Async(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
using Microsoft.CodeAnalysis.Test.Utilities;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.Text;
using System.Collections.Generic;
using Microsoft.CodeAnalysis.Shared.Utilities;

#if !CODE_STYLE
using Roslyn.Utilities;
Expand Down Expand Up @@ -73,6 +75,13 @@ public Test()
/// <inheritdoc cref="SharedVerifierState.Options"/>
internal OptionsCollection Options => _sharedState.Options;

#if !CODE_STYLE
internal CodeActionOptions CodeActionOptions
{
get => _sharedState.CodeActionOptions;
set => _sharedState.CodeActionOptions = value;
}
#endif
/// <inheritdoc cref="SharedVerifierState.EditorConfig"/>
public string? EditorConfig
{
Expand All @@ -97,7 +106,31 @@ protected override async Task RunImplAsync(CancellationToken cancellationToken =

#if !CODE_STYLE
protected override AnalyzerOptions GetAnalyzerOptions(Project project)
=> new WorkspaceAnalyzerOptions(base.GetAnalyzerOptions(project), project.Solution);
=> new WorkspaceAnalyzerOptions(base.GetAnalyzerOptions(project), project);

protected override CodeFixContext CreateCodeFixContext(Document document, TextSpan span, ImmutableArray<Diagnostic> diagnostics, Action<CodeAction, ImmutableArray<Diagnostic>> registerCodeFix, CancellationToken cancellationToken)
=> new(document, span, diagnostics, registerCodeFix, _sharedState.CodeActionOptions, cancellationToken);

protected override FixAllContext CreateFixAllContext(
Document? document,
Project project,
CodeFixProvider codeFixProvider,
FixAllScope scope,
string? codeActionEquivalenceKey,
IEnumerable<string> diagnosticIds,
FixAllContext.DiagnosticProvider fixAllDiagnosticProvider,
CancellationToken cancellationToken)
=> new(new FixAllState(
fixAllProvider: null,
document,
project,
codeFixProvider,
scope,
codeActionEquivalenceKey,
diagnosticIds,
fixAllDiagnosticProvider,
_ => _sharedState.CodeActionOptions),
new ProgressTracker(), cancellationToken);
#endif

protected override Diagnostic? TrySelectDiagnosticToFix(ImmutableArray<Diagnostic> fixableDiagnostics)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected override ImmutableArray<CodeAction> FilterCodeActions(ImmutableArray<C

#if !CODE_STYLE
protected override AnalyzerOptions GetAnalyzerOptions(Project project)
=> new WorkspaceAnalyzerOptions(base.GetAnalyzerOptions(project), project.Solution);
=> new WorkspaceAnalyzerOptions(base.GetAnalyzerOptions(project), project);

/// <summary>
/// The <see cref="TestHost"/> we want this test to run in. Defaults to <see cref="TestHost.InProcess"/> if unspecified.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.CodeAnalysis.Testing.Verifiers;

#if !CODE_STYLE
using Microsoft.CodeAnalysis.CodeActions;
using Roslyn.Utilities;
#endif

Expand Down Expand Up @@ -45,6 +46,9 @@ public SharedVerifierState(AnalyzerTest<XUnitVerifier> test, string defaultFileE
/// </summary>
internal OptionsCollection Options { get; }

#if !CODE_STYLE
internal CodeActionOptions CodeActionOptions { get; set; }
#endif
internal void Apply()
{
var (analyzerConfigSource, remainingOptions) = CodeFixVerifierHelper.ConvertOptionsToAnalyzerConfig(_defaultFileExt, EditorConfig, Options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected override async Task RunImplAsync(CancellationToken cancellationToken =

#if !CODE_STYLE
protected override AnalyzerOptions GetAnalyzerOptions(Project project)
=> new WorkspaceAnalyzerOptions(base.GetAnalyzerOptions(project), project.Solution);
=> new WorkspaceAnalyzerOptions(base.GetAnalyzerOptions(project), project);
#endif

protected override Diagnostic? TrySelectDiagnosticToFix(ImmutableArray<Diagnostic> fixableDiagnostics)
Expand Down
Loading