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

Convert ImplementTypeOptions to editorconfig options #74376

Merged
merged 5 commits into from
Jul 16, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -8007,10 +8007,13 @@ void M() { }
public int Prop => throw new System.NotImplementedException();
}
""",
CodeActionOptions = (CodeActionOptions.Default with
Options =
{
ImplementTypeOptions = new() { InsertionBehavior = ImplementTypeInsertionBehavior.AtTheEnd }
}).CreateProvider()
new OptionsCollection(LanguageNames.CSharp)
{
{ ImplementTypeOptionsStorage.InsertionBehavior, ImplementTypeInsertionBehavior.AtTheEnd }
}
}
}.RunAsync();
}

Expand Down Expand Up @@ -8189,10 +8192,13 @@ class Class : IInterface
public int WriteOnlyProp { set => throw new System.NotImplementedException(); }
}
""",
CodeActionOptions = (CodeActionOptions.Default with
Options =
{
ImplementTypeOptions = new() { PropertyGenerationBehavior = ImplementTypePropertyGenerationBehavior.PreferAutoProperties }
}).CreateProvider()
new OptionsCollection(LanguageNames.CSharp)
{
{ ImplementTypeOptionsStorage.PropertyGenerationBehavior, ImplementTypePropertyGenerationBehavior.PreferAutoProperties }
}
}
}.RunAsync();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.ImplementAbstractClass
MyBase.New(editorOperationsFactoryService, globalOptions)
End Sub

Protected Overrides Function TryGetNewDocument(
Protected Overrides Async Function TryGetNewDocumentAsync(
document As Document,
options As ImplementTypeOptions,
typeSyntax As TypeSyntax,
cancellationToken As CancellationToken
) As Document
) As Task(Of Document)

If typeSyntax.Parent.Kind <> SyntaxKind.InheritsStatement Then
Return Nothing
Expand All @@ -47,14 +46,19 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.ImplementAbstractClass
Return Nothing
End If

Dim updatedDocument = ImplementAbstractClassData.TryImplementAbstractClassAsync(
document, classBlock, classBlock.ClassStatement.Identifier, options, cancellationToken).WaitAndGetResult(cancellationToken)
If updatedDocument IsNot Nothing AndAlso
updatedDocument.GetTextChangesAsync(document, cancellationToken).WaitAndGetResult(cancellationToken).Count = 0 Then
Dim updatedDocument = Await ImplementAbstractClassData.TryImplementAbstractClassAsync(
document, classBlock, classBlock.ClassStatement.Identifier, cancellationToken).ConfigureAwait(False)

If updatedDocument Is Nothing Then
Return Nothing
End If

Return updatedDocument
Dim changes = Await updatedDocument.GetTextChangesAsync(document, cancellationToken).ConfigureAwait(False)
If changes.Any() Then
Return updatedDocument
End If

Return Nothing
End Function
End Class
End Namespace
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,31 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.ImplementInterface
MyBase.New(editorOperationsFactoryService, globalOptions)
End Sub

Protected Overrides Function TryGetNewDocument(
Protected Overrides Async Function TryGetNewDocumentAsync(
document As Document,
options As ImplementTypeOptions,
typeSyntax As TypeSyntax,
cancellationToken As CancellationToken
) As Document
) As Task(Of Document)

If typeSyntax.Parent.Kind <> SyntaxKind.ImplementsStatement Then
Return Nothing
End If

Dim service = document.GetLanguageService(Of IImplementInterfaceService)()
Dim updatedDocument = service.ImplementInterfaceAsync(
Dim options = Await document.GetImplementTypeOptionsAsync(cancellationToken).ConfigureAwait(False)

Dim updatedDocument = Await service.ImplementInterfaceAsync(
document,
options,
typeSyntax.Parent,
cancellationToken).WaitAndGetResult(cancellationToken)
If updatedDocument.GetTextChangesAsync(document, cancellationToken).WaitAndGetResult(cancellationToken).Count = 0 Then
Return Nothing
cancellationToken).ConfigureAwait(False)

Dim changes = Await updatedDocument.GetTextChangesAsync(document, cancellationToken).ConfigureAwait(False)
If changes.Any() Then
Return updatedDocument
End If

Return updatedDocument
Return Nothing
End Function
End Class
End Namespace
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.Utilities.CommandHandlers
_globalOptions = globalOptions
End Sub

Protected MustOverride Overloads Function TryGetNewDocument(
Protected MustOverride Overloads Function TryGetNewDocumentAsync(
document As Document,
options As ImplementTypeOptions,
typeSyntax As TypeSyntax,
cancellationToken As CancellationToken) As Document
cancellationToken As CancellationToken) As Task(Of Document)

Private Function ExecuteCommand(args As ReturnKeyCommandArgs, context As CommandExecutionContext) As Boolean Implements ICommandHandler(Of ReturnKeyCommandArgs).ExecuteCommand
Dim caretPointOpt = args.TextView.GetCaretPoint(args.SubjectBuffer)
Expand Down Expand Up @@ -165,8 +164,7 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.Utilities.CommandHandlers
Return False
End If

Dim newDocument = TryGetNewDocument(document, _globalOptions.GetImplementTypeOptions(document.Project.Language), identifier, cancellationToken)

Dim newDocument = TryGetNewDocumentAsync(document, identifier, cancellationToken).WaitAndGetResult(cancellationToken)
If newDocument Is Nothing Then
Return False
End If
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Diagnostics;
using Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions;
using Microsoft.CodeAnalysis.ImplementType;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;
Expand Down Expand Up @@ -1713,7 +1712,7 @@ void Goo() { }

public override int Prop => throw new System.NotImplementedException();
}
""", globalOptions: options);
""", options: options);
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/17274")]
Expand Down Expand Up @@ -1910,7 +1909,7 @@ class C : AbstractClass
public override int ReadWriteProp { get; set; }
public override int WriteOnlyProp { set => throw new System.NotImplementedException(); }
}
""", parameters: new TestParameters(globalOptions: options));
""", parameters: new TestParameters(options: options));
}

[Theory, CombinatorialData]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.ImplementType;
using Microsoft.CodeAnalysis.Shared.Extensions;

namespace Microsoft.CodeAnalysis.ImplementAbstractClass;
Expand Down Expand Up @@ -39,7 +40,7 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
return;

var data = await ImplementAbstractClassData.TryGetDataAsync(
document, classNode, GetClassIdentifier(classNode), context.Options.GetOptions(document.Project.Services).ImplementTypeOptions, cancellationToken).ConfigureAwait(false);
document, classNode, GetClassIdentifier(classNode), cancellationToken).ConfigureAwait(false);
if (data == null)
return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal sealed class ImplementAbstractClassData(
public readonly INamedTypeSymbol AbstractClassType = abstractClassType;

public static async Task<ImplementAbstractClassData?> TryGetDataAsync(
Document document, SyntaxNode classNode, SyntaxToken classIdentifier, ImplementTypeOptions options, CancellationToken cancellationToken)
Document document, SyntaxNode classNode, SyntaxToken classIdentifier, CancellationToken cancellationToken)
{
var semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);
if (semanticModel.GetDeclaredSymbol(classNode, cancellationToken) is not INamedTypeSymbol classType)
Expand All @@ -62,15 +62,17 @@ internal sealed class ImplementAbstractClassData(
if (unimplementedMembers.IsEmpty)
return null;

var options = await document.GetImplementTypeOptionsAsync(cancellationToken).ConfigureAwait(false);

return new ImplementAbstractClassData(
document, options, classNode, classIdentifier,
classType, abstractClassType, unimplementedMembers);
}

public static async Task<Document?> TryImplementAbstractClassAsync(
Document document, SyntaxNode classNode, SyntaxToken classIdentifier, ImplementTypeOptions options, CancellationToken cancellationToken)
Document document, SyntaxNode classNode, SyntaxToken classIdentifier, CancellationToken cancellationToken)
{
var data = await TryGetDataAsync(document, classNode, classIdentifier, options, cancellationToken).ConfigureAwait(false);
var data = await TryGetDataAsync(document, classNode, classIdentifier, cancellationToken).ConfigureAwait(false);
if (data == null)
return null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.ImplementType;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Shared.Extensions;

Expand Down Expand Up @@ -37,12 +38,13 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
if (!token.Span.IntersectsWith(span))
return;

var options = await document.GetImplementTypeOptionsAsync(cancellationToken).ConfigureAwait(false);

foreach (var type in token.Parent.GetAncestorsOrThis<TTypeSyntax>())
{
if (this.IsTypeInInterfaceBaseList(type))
{
var service = document.GetRequiredLanguageService<IImplementInterfaceService>();
var options = context.Options.GetOptions(document.Project.Services).ImplementTypeOptions;

var info = await service.AnalyzeAsync(
document, type, cancellationToken).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// 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 System;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Options;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.ImplementType;

[DataContract]
internal readonly record struct ImplementTypeOptions()
{
[DataMember]
public ImplementTypeInsertionBehavior InsertionBehavior { get; init; } = ImplementTypeInsertionBehavior.WithOtherMembersOfTheSameKind;

[DataMember]
public ImplementTypePropertyGenerationBehavior PropertyGenerationBehavior { get; init; } = ImplementTypePropertyGenerationBehavior.PreferThrowingProperties;

public static readonly ImplementTypeOptions Default = new();
}

internal static class ImplementTypeOptionsStorage
{
public static readonly PerLanguageOption2<ImplementTypeInsertionBehavior> InsertionBehavior = new(
"dotnet_member_insertion_location",
defaultValue: ImplementTypeOptions.Default.InsertionBehavior,
group: MemberDisplayOptionsStorage.TypeMemberGroup,
isEditorConfigOption: true,
serializer: EditorConfigValueSerializer.CreateSerializerForEnum(
entries:
[
("at_the_end", ImplementTypeInsertionBehavior.AtTheEnd),
("with_other_members_of_the_same_kind", ImplementTypeInsertionBehavior.WithOtherMembersOfTheSameKind),
],
alternativeEntries:
[
("AtTheEnd", ImplementTypeInsertionBehavior.AtTheEnd),
("WithOtherMembersOfTheSameKind", ImplementTypeInsertionBehavior.WithOtherMembersOfTheSameKind),
]));

public static readonly PerLanguageOption2<ImplementTypePropertyGenerationBehavior> PropertyGenerationBehavior = new(
"dotnet_property_generation_behavior",
defaultValue: ImplementTypeOptions.Default.PropertyGenerationBehavior,
group: MemberDisplayOptionsStorage.TypeMemberGroup,
isEditorConfigOption: true,
serializer: EditorConfigValueSerializer.CreateSerializerForEnum(
entries:
[
("prefer_throwing_properties", ImplementTypePropertyGenerationBehavior.PreferThrowingProperties),
("prefer_auto_properties", ImplementTypePropertyGenerationBehavior.PreferAutoProperties),
],
alternativeEntries:
[
("PreferThrowingProperties", ImplementTypePropertyGenerationBehavior.PreferThrowingProperties),
("PreferAutoProperties", ImplementTypePropertyGenerationBehavior.PreferAutoProperties),
]));

/// <summary>
/// Options that we expect the user to set in editorconfig.
/// </summary>
public static readonly ImmutableArray<IOption2> EditorConfigOptions = [InsertionBehavior, PropertyGenerationBehavior];
}

internal static class ImplementTypeOptionsProviders
{
public static ImplementTypeOptions GetImplementTypeOptions(this IOptionsReader reader, 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)
{
var configOptions = await document.GetAnalyzerConfigOptionsAsync(cancellationToken).ConfigureAwait(false);
return configOptions.GetImplementTypeOptions(document.Project.Language);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<InternalsVisibleTo Include="Microsoft.CodeAnalysis.ExternalAccess.UnitTesting" />
<InternalsVisibleTo Include="Microsoft.CodeAnalysis.ExternalAccess.VisualDiagnostics" />
<InternalsVisibleTo Include="Microsoft.CodeAnalysis.ExternalAccess.Xaml" />
<InternalsVisibleTo Include="Microsoft.CodeAnalysis.ExternalAccess.EditorConfigGenerator"/>
<InternalsVisibleTo Include="Microsoft.CodeAnalysis.ExternalAccess.EditorConfigGenerator" />
<InternalsVisibleTo Include="Microsoft.CodeAnalysis.Features.DiagnosticsTests.Utilities" />
<InternalsVisibleTo Include="Microsoft.CodeAnalysis.Features.Test.Utilities" />
<InternalsVisibleTo Include="Microsoft.CodeAnalysis.Features.UnitTests" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
using System.Composition;
using System.Linq;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.Completion;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Features.EmbeddedLanguages.Json.LanguageServices;
using Microsoft.CodeAnalysis.Features.EmbeddedLanguages.RegularExpressions.LanguageServices;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.ImplementType;
using Microsoft.CodeAnalysis.ValidateFormatString;

namespace Microsoft.CodeAnalysis.Options;
Expand Down Expand Up @@ -43,7 +42,11 @@ internal sealed class EditorConfigOptionsEnumerator(
yield return ("unsupported", RegexOptionsStorage.UnsupportedOptions);
}

yield return (FeaturesResources.NET_Code_Actions, MemberDisplayOptionsStorage.EditorConfigOptions);
yield return (FeaturesResources.NET_Code_Actions,
[
.. ImplementTypeOptionsStorage.EditorConfigOptions,
.. MemberDisplayOptionsStorage.EditorConfigOptions
]);

yield return (WorkspacesResources.dot_NET_Coding_Conventions,
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ internal readonly record struct MemberDisplayOptions()
/// </summary>
internal static class MemberDisplayOptionsStorage
{
public static readonly OptionGroup CodeActionsGroup = new(name: "code_actions", description: FeaturesResources.NET_Code_Actions, priority: 3);
public static readonly OptionGroup TypeMemberGroup = new(name: "type_members", description: FeaturesResources.Type_members, priority: 3, parent: CodeActionsGroup);
public static readonly OptionGroup TypeMemberGroup = new(name: "type_members", description: FeaturesResources.Type_members, priority: 3, parent: null);

public static readonly PerLanguageOption2<bool> HideAdvancedMembers = new(
"dotnet_hide_advanced_members",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,15 +411,14 @@ internal Task TestInRegularAndScriptAsync(
CodeActionPriority? priority = null,
CompilationOptions compilationOptions = null,
OptionsCollectionAlias options = null,
OptionsCollectionAlias globalOptions = null,
object fixProviderData = null,
ParseOptions parseOptions = null,
string title = null,
TestHost testHost = TestHost.OutOfProcess)
{
return TestInRegularAndScript1Async(
initialMarkup, expectedMarkup,
new TestParameters(parseOptions, compilationOptions, options, globalOptions, fixProviderData, index, priority, title: title, testHost: testHost));
new TestParameters(parseOptions, compilationOptions, options, globalOptions: null, fixProviderData, index, priority, title: title, testHost: testHost));
}

internal Task TestInRegularAndScript1Async(
Expand Down
Loading
Loading