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

Remove usages of #if CODE_STYLE where possible. #74958

Merged
merged 26 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from 23 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 @@ -238,13 +238,7 @@ private bool TryInitializeDelegatedConstructor(CancellationToken cancellationTok
}

private TLanguageService GetRequiredLanguageService<TLanguageService>(string language) where TLanguageService : ILanguageService
{
#if CODE_STYLE
return _document.Project.Solution.Workspace.Services.GetExtendedLanguageServices(language).GetRequiredService<TLanguageService>();
#else
return _document.Project.Solution.Services.GetRequiredLanguageService<TLanguageService>(language);
#endif
}
=> _document.Project.Solution.Workspace.Services.GetExtendedLanguageServices(language).GetRequiredService<TLanguageService>();
Copy link
Member Author

Choose a reason for hiding this comment

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

the call to GetExtendedLanguageServices already defers to GetRequiredLanguageService if not in the code-style layer. so we can simplify all of this.


private bool ClashesWithExistingConstructor()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ private partial class GenerateEnumMemberCodeAction(Document document, State stat

protected override async Task<Document> GetChangedDocumentAsync(CancellationToken cancellationToken)
{
#if CODE_STYLE
var languageServices = _document.Project.Solution.Workspace.Services.GetExtendedLanguageServices(_state.TypeToGenerateIn.Language);
#else
var languageServices = _document.Project.Solution.Services.GetLanguageServices(_state.TypeToGenerateIn.Language);
#endif
var codeGenerator = languageServices.GetService<ICodeGenerationService>();
var semanticFacts = languageServices.GetService<ISemanticFactsService>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,7 @@ public async ValueTask<IMethodSymbol> GenerateMethodAsync(
methodKind: State.MethodKind);

// Ensure no conflicts between type parameter names and parameter names.
#if CODE_STYLE
var languageServiceProvider = Document.Project.Solution.Workspace.Services.GetExtendedLanguageServices(State.TypeToGenerateIn.Language);
#else
var languageServiceProvider = Document.Project.Solution.Services.GetLanguageServices(State.TypeToGenerateIn.Language);
#endif

var syntaxFacts = languageServiceProvider.GetService<ISyntaxFactsService>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,7 @@ protected async Task<bool> TryFinishInitializingStateAsync(TService service, Sem
.GetMembers(IdentifierToken.ValueText)
.OfType<IMethodSymbol>();

#if CODE_STYLE
var destinationProvider = document.Project.Solution.Workspace.Services.GetExtendedLanguageServices(TypeToGenerateIn.Language);
#else
var destinationProvider = document.Project.Solution.Services.GetLanguageServices(TypeToGenerateIn.Language);
#endif

var syntaxFacts = destinationProvider.GetService<ISyntaxFactsService>();
var syntaxFactory = destinationProvider.GetService<SyntaxGenerator>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ protected async ValueTask<ImmutableArray<CodeAction>> GetActionsAsync(Document d
if (canGenerateAbstractly)
result.Add(new GenerateParameterizedMemberCodeAction((TService)this, document, state, isAbstract: true, generateProperty: false));

#if CODE_STYLE
var semanticFacts = document.Project.Solution.Workspace.Services.GetExtendedLanguageServices(state.TypeToGenerateIn.Language).GetService<ISemanticFactsService>();
#else
var semanticFacts = document.Project.Solution.Services.GetLanguageServices(state.TypeToGenerateIn.Language).GetService<ISemanticFactsService>();
#endif

if (semanticFacts.SupportsParameterizedProperties &&
state.InvocationExpressionOpt != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ protected static async Task<SyntaxNode> ExtractMethodAsync(

var options = new ExtractMethodGenerationOptions()
{
CodeGenerationOptions = CodeGenerationOptions.GetDefault(document.Project.Services),
CodeCleanupOptions = CodeCleanupOptions.GetDefault(document.Project.Services),
CodeGenerationOptions = CodeGenerationOptionsProviders.GetDefault(document.Project.Services),
CodeCleanupOptions = await document.GetCodeCleanupOptionsAsync(CancellationToken.None),
};

var semanticDocument = await SemanticDocument.CreateAsync(document, CancellationToken.None);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
Imports System.Collections.Immutable
Imports System.Threading
Imports Microsoft.CodeAnalysis
Imports Microsoft.CodeAnalysis.CodeActions
Imports Microsoft.CodeAnalysis.CodeCleanup
Imports Microsoft.CodeAnalysis.Editor.UnitTests.Extensions
Imports Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces
Imports Microsoft.CodeAnalysis.Formatting
Imports Microsoft.CodeAnalysis.Simplification
Imports Microsoft.CodeAnalysis.Text
Expand All @@ -30,7 +28,7 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.Expansion

Dim root = Await document.GetSyntaxRootAsync()

Dim cleanupOptions = CodeCleanupOptions.GetDefault(document.Project.Services)
Dim cleanupOptions = Await document.GetCodeCleanupOptionsAsync(CancellationToken.None)

Dim spans As ImmutableArray(Of TextSpan) = Nothing
If hostDocument.AnnotatedSpans.TryGetValue("Expand", spans) Then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal async Task TestAsync(string testCode, string expected, OptionsCollectio

var cleanupOptions =
options?.GetCodeCleanupOptions(languageServices, allowImportsInHiddenRegions: false) ??
CodeCleanupOptions.GetDefault(languageServices);
await document.GetCodeCleanupOptionsAsync(CancellationToken.None);

var formattingService = document.GetRequiredLanguageService<INewDocumentFormattingService>();
var formattedDocument = await formattingService.FormatNewDocumentAsync(document, hintDocument: null, cleanupOptions, CancellationToken.None);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UnitTests.CaseCorrecting
Dim document = workspace.CurrentSolution.GetDocument(hostDocument.Id)
Dim span = (Await document.GetSyntaxRootAsync()).FullSpan

Dim options = CodeCleanupOptions.GetDefault(document.Project.Services)
Dim options = Await document.GetCodeCleanupOptionsAsync(CancellationToken.None)

Dim service = document.GetLanguageService(Of ICodeCleanerService)
Dim newDocument = Await service.CleanupAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UnitTests.ExtractMethod

' extract method
Dim extractGenerationOptions = VBOptionsFactory.CreateExtractMethodGenerationOptions(
CodeGenerationOptions.GetDefault(document.Project.Services),
CodeCleanupOptions.GetDefault(document.Project.Services))
Await document.GetCodeGenerationOptionsAsync(CancellationToken.None),
Await document.GetCodeCleanupOptionsAsync(CancellationToken.None))

Dim extractor = New VisualBasicMethodExtractor(selectedCode, extractGenerationOptions)
Dim result = extractor.ExtractMethod(status, CancellationToken.None)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ private async Task<IEnumerable<TextChange>> ProcessNodeAsync(Document document,
var newRoot = await ReplaceNodeAsync(simpleName, containerName, originalSymbol.IsType, cancellationToken).ConfigureAwait(false);
var newDocument = document.WithSyntaxRoot(newRoot);
var cleanedDocument = await CodeAction.CleanupDocumentAsync(
newDocument, CodeCleanupOptions.GetDefault(document.Project.Services), cancellationToken).ConfigureAwait(false);
newDocument, await document.GetCodeCleanupOptionsAsync(cancellationToken).ConfigureAwait(false), cancellationToken).ConfigureAwait(false);

return await cleanedDocument.GetTextChangesAsync(document, cancellationToken).ConfigureAwait(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ internal readonly record struct ExtractMethodGenerationOptions
public static ExtractMethodGenerationOptions GetDefault(LanguageServices languageServices)
=> new()
{
CodeGenerationOptions = CodeGenerationOptions.GetDefault(languageServices),
CodeCleanupOptions = CodeCleanupOptions.GetDefault(languageServices),
CodeGenerationOptions = CodeGenerationOptionsProviders.GetDefault(languageServices),
CodeCleanupOptions = CodeCleanupOptionsProviders.GetDefault(languageServices),
};

public ExtractMethodGenerationOptions()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,14 @@
// 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.Runtime.Serialization;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.AddImport;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Options;

namespace Microsoft.CodeAnalysis.OrganizeImports;

[DataContract]
internal readonly record struct OrganizeImportsOptions
Copy link
Member Author

Choose a reason for hiding this comment

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

options type moved to tis own file in the CodeStyle layer. The 'Providers' types move to the CodeFixes layer. This removes the need for CODE_STYLE checks, and properly layers things.

{
[DataMember] public bool PlaceSystemNamespaceFirst { get; init; } = AddImportPlacementOptions.Default.PlaceSystemNamespaceFirst;
[DataMember] public bool SeparateImportDirectiveGroups { get; init; } = SyntaxFormattingOptions.CommonDefaults.SeparateImportDirectiveGroups;
[DataMember] public string NewLine { get; init; } = LineFormattingOptions.Default.NewLine;

public OrganizeImportsOptions()
{
}

public static readonly OrganizeImportsOptions Default = new();
}

internal static class OrganizeImportsOptionsProviders
{
public static OrganizeImportsOptions GetOrganizeImportsOptions(this IOptionsReader options, string language)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#nullable disable

using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeCleanup;
using Microsoft.CodeAnalysis.CodeCleanup.Providers;
Expand Down Expand Up @@ -2717,7 +2718,7 @@ private static async Task VerifyAsync(string codeWithMarker, string expectedResu
var document = CreateDocument(codeWithoutMarker, LanguageNames.VisualBasic);
var codeCleanups = CodeCleaner.GetDefaultProviders(document).WhereAsArray(p => p.Name is PredefinedCodeCleanupProviderNames.AddMissingTokens or PredefinedCodeCleanupProviderNames.Format or PredefinedCodeCleanupProviderNames.Simplification);

var cleanDocument = await CodeCleaner.CleanupAsync(document, textSpans[0], CodeCleanupOptions.GetDefault(document.Project.Services), codeCleanups);
var cleanDocument = await CodeCleaner.CleanupAsync(document, textSpans[0], await document.GetCodeCleanupOptionsAsync(CancellationToken.None), codeCleanups);

Assert.Equal(expectedResult, (await cleanDocument.GetSyntaxRootAsync()).ToFullString());
}
Expand Down
Loading
Loading