diff --git a/src/Analyzers/CSharp/CodeFixes/AddInheritdoc/AddInheritdocCodeFixProvider.cs b/src/Analyzers/CSharp/CodeFixes/AddInheritdoc/AddInheritdocCodeFixProvider.cs index c835f9f901206..eab69a511fc47 100644 --- a/src/Analyzers/CSharp/CodeFixes/AddInheritdoc/AddInheritdocCodeFixProvider.cs +++ b/src/Analyzers/CSharp/CodeFixes/AddInheritdoc/AddInheritdocCodeFixProvider.cs @@ -11,6 +11,7 @@ using Microsoft.CodeAnalysis.CodeActions; using Microsoft.CodeAnalysis.CodeFixes; using Microsoft.CodeAnalysis.Editing; +using Microsoft.CodeAnalysis.Formatting; using Microsoft.CodeAnalysis.Host.Mef; using Microsoft.CodeAnalysis.Shared.Extensions; using Microsoft.CodeAnalysis.Text; @@ -89,8 +90,8 @@ protected override async Task FixAllAsync(Document document, ImmutableArray GetChangedDocumentAsync(CancellationToken cancellationToken) { var root = await _document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false); - var options = await _document.GetCSharpCodeFixOptionsProviderAsync(cancellationToken).ConfigureAwait(false); + var configOptions = await _document.GetAnalyzerConfigOptionsAsync(cancellationToken).ConfigureAwait(false); - var newNode = GetNewNode(_node, options.PreferredModifierOrder.Value); + var newNode = GetNewNode(_node, configOptions.GetOption(CSharpCodeStyleOptions.PreferredModifierOrder).Value); var newRoot = root.ReplaceNode(_node, newNode); return _document.WithSyntaxRoot(newRoot); diff --git a/src/Analyzers/CSharp/CodeFixes/InlineDeclaration/CSharpInlineDeclarationCodeFixProvider.cs b/src/Analyzers/CSharp/CodeFixes/InlineDeclaration/CSharpInlineDeclarationCodeFixProvider.cs index 526ca834a7acd..106b78d3d4ca1 100644 --- a/src/Analyzers/CSharp/CodeFixes/InlineDeclaration/CSharpInlineDeclarationCodeFixProvider.cs +++ b/src/Analyzers/CSharp/CodeFixes/InlineDeclaration/CSharpInlineDeclarationCodeFixProvider.cs @@ -14,6 +14,7 @@ using Microsoft.CodeAnalysis.CodeActions; using Microsoft.CodeAnalysis.CodeFixes; using Microsoft.CodeAnalysis.CSharp.Extensions; +using Microsoft.CodeAnalysis.CSharp.Simplification; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.Editing; @@ -44,7 +45,7 @@ protected override async Task FixAllAsync( Document document, ImmutableArray diagnostics, SyntaxEditor editor, CancellationToken cancellationToken) { - var options = await document.GetCSharpCodeFixOptionsProviderAsync(cancellationToken).ConfigureAwait(false); + var options = await document.GetCSharpSimplifierOptionsAsync(cancellationToken).ConfigureAwait(false); // Gather all statements to be removed // We need this to find the statements we can safely attach trivia to @@ -92,7 +93,7 @@ private static (VariableDeclaratorSyntax declarator, IdentifierNameSyntax identi private static SyntaxNode ReplaceIdentifierWithInlineDeclaration( Document document, - CSharpCodeFixOptionsProvider options, SemanticModel semanticModel, + CSharpSimplifierOptions options, SemanticModel semanticModel, SyntaxNode currentRoot, VariableDeclaratorSyntax declarator, IdentifierNameSyntax identifier, SyntaxNode currentNode, HashSet declarationsToRemove, @@ -238,7 +239,7 @@ private static SyntaxNode ReplaceIdentifierWithInlineDeclaration( } public static TypeSyntax GenerateTypeSyntaxOrVar( - ITypeSymbol symbol, CSharpCodeFixOptionsProvider options) + ITypeSymbol symbol, CSharpSimplifierOptions options) { var useVar = IsVarDesired(symbol, options); @@ -251,7 +252,7 @@ public static TypeSyntax GenerateTypeSyntaxOrVar( : symbol.GenerateTypeSyntax(); } - private static bool IsVarDesired(ITypeSymbol type, CSharpCodeFixOptionsProvider options) + private static bool IsVarDesired(ITypeSymbol type, CSharpSimplifierOptions options) { // If they want it for intrinsics, and this is an intrinsic, then use var. if (type.IsSpecialType() == true) diff --git a/src/Analyzers/CSharp/CodeFixes/MisplacedUsingDirectives/MisplacedUsingDirectivesCodeFixProvider.cs b/src/Analyzers/CSharp/CodeFixes/MisplacedUsingDirectives/MisplacedUsingDirectivesCodeFixProvider.cs index 6657044d72b52..706341426bd6c 100644 --- a/src/Analyzers/CSharp/CodeFixes/MisplacedUsingDirectives/MisplacedUsingDirectivesCodeFixProvider.cs +++ b/src/Analyzers/CSharp/CodeFixes/MisplacedUsingDirectives/MisplacedUsingDirectivesCodeFixProvider.cs @@ -13,15 +13,18 @@ using Microsoft.CodeAnalysis.CodeActions; using Microsoft.CodeAnalysis.CodeFixes; using Microsoft.CodeAnalysis.CodeStyle; +using Microsoft.CodeAnalysis.CSharp.Simplification; using Microsoft.CodeAnalysis.CSharp.Extensions; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.Formatting; using Microsoft.CodeAnalysis.LanguageService; +using Microsoft.CodeAnalysis.Options; using Microsoft.CodeAnalysis.PooledObjects; using Microsoft.CodeAnalysis.Shared.Extensions; using Microsoft.CodeAnalysis.Simplification; using Roslyn.Utilities; +using Microsoft.CodeAnalysis.CSharp.CodeStyle; namespace Microsoft.CodeAnalysis.CSharp.MisplacedUsingDirectives; @@ -57,12 +60,12 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context) var syntaxRoot = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false); var compilationUnit = (CompilationUnitSyntax)syntaxRoot; - var options = await document.GetCSharpCodeFixOptionsProviderAsync(cancellationToken).ConfigureAwait(false); - var simplifierOptions = options.GetSimplifierOptions(); + var configOptions = await document.GetAnalyzerConfigOptionsAsync(cancellationToken).ConfigureAwait(false); + var simplifierOptions = new CSharpSimplifierOptions(configOptions); // Read the preferred placement option and verify if it can be applied to this code file. There are cases // where we will not be able to fix the diagnostic and the user will need to resolve it manually. - var (placement, preferPreservation) = DeterminePlacement(compilationUnit, options.UsingDirectivePlacement); + var (placement, preferPreservation) = DeterminePlacement(compilationUnit, configOptions.GetOption(CSharpCodeStyleOptions.PreferredUsingDirectivePlacement)); if (preferPreservation) return; diff --git a/src/Analyzers/CSharp/CodeFixes/NewLines/EmbeddedStatementPlacement/EmbeddedStatementPlacementCodeFixProvider.cs b/src/Analyzers/CSharp/CodeFixes/NewLines/EmbeddedStatementPlacement/EmbeddedStatementPlacementCodeFixProvider.cs index bf6a8be42bb2e..2cb1247d4ee5b 100644 --- a/src/Analyzers/CSharp/CodeFixes/NewLines/EmbeddedStatementPlacement/EmbeddedStatementPlacementCodeFixProvider.cs +++ b/src/Analyzers/CSharp/CodeFixes/NewLines/EmbeddedStatementPlacement/EmbeddedStatementPlacementCodeFixProvider.cs @@ -11,6 +11,7 @@ using System.Threading.Tasks; using Microsoft.CodeAnalysis.CodeActions; using Microsoft.CodeAnalysis.CodeFixes; +using Microsoft.CodeAnalysis.CSharp.Formatting; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.Editing; @@ -47,7 +48,7 @@ public static async Task FixAllAsync(Document document, ImmutableArray var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false); var editor = new SyntaxEditor(root, document.Project.Solution.Services); - var options = await document.GetCSharpCodeFixOptionsProviderAsync(cancellationToken).ConfigureAwait(false); + var options = await document.GetCSharpSyntaxFormattingOptionsAsync(cancellationToken).ConfigureAwait(false); var endOfLineTrivia = ElasticEndOfLine(options.NewLine); diff --git a/src/Analyzers/CSharp/CodeFixes/UseCollectionExpression/CSharpCollectionExpressionRewriter.cs b/src/Analyzers/CSharp/CodeFixes/UseCollectionExpression/CSharpCollectionExpressionRewriter.cs index 8d138887a1187..c5f2083c7c2a7 100644 --- a/src/Analyzers/CSharp/CodeFixes/UseCollectionExpression/CSharpCollectionExpressionRewriter.cs +++ b/src/Analyzers/CSharp/CodeFixes/UseCollectionExpression/CSharpCollectionExpressionRewriter.cs @@ -46,8 +46,7 @@ public static async Task CreateCollectionExpressionA var document = await ParsedDocument.CreateAsync(workspaceDocument, cancellationToken).ConfigureAwait(false); - var options = await workspaceDocument.GetCodeFixOptionsAsync(cancellationToken).ConfigureAwait(false); - var formattingOptions = (CSharpSyntaxFormattingOptions)options.GetFormattingOptions(CSharpSyntaxFormatting.Instance); + var formattingOptions = await workspaceDocument.GetCSharpSyntaxFormattingOptionsAsync(cancellationToken).ConfigureAwait(false); var indentationOptions = new IndentationOptions(formattingOptions); var wrappingLength = formattingOptions.CollectionExpressionWrappingLength; diff --git a/src/Analyzers/Core/CodeFixes/AliasAmbiguousType/AbstractAliasAmbiguousTypeCodeFixProvider.cs b/src/Analyzers/Core/CodeFixes/AliasAmbiguousType/AbstractAliasAmbiguousTypeCodeFixProvider.cs index 9a471eb13795c..841a9a77dc4c9 100644 --- a/src/Analyzers/Core/CodeFixes/AliasAmbiguousType/AbstractAliasAmbiguousTypeCodeFixProvider.cs +++ b/src/Analyzers/Core/CodeFixes/AliasAmbiguousType/AbstractAliasAmbiguousTypeCodeFixProvider.cs @@ -48,7 +48,7 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) var syntaxGenerator = document.GetRequiredLanguageService(); var compilation = semanticModel.Compilation; - var placementOption = await document.GetAddImportPlacementOptionsAsync(addImportService, cancellationToken).ConfigureAwait(false); + var placementOption = await document.GetAddImportPlacementOptionsAsync(cancellationToken).ConfigureAwait(false); using var _ = ArrayBuilder.GetInstance(out var actions); foreach (var symbol in Sort(symbolInfo.CandidateSymbols.Cast(), placementOption.PlaceSystemNamespaceFirst)) diff --git a/src/Analyzers/Core/CodeFixes/FileHeaders/AbstractFileHeaderCodeFixProvider.cs b/src/Analyzers/Core/CodeFixes/FileHeaders/AbstractFileHeaderCodeFixProvider.cs index f2996a7356445..40061d4fb71dd 100644 --- a/src/Analyzers/Core/CodeFixes/FileHeaders/AbstractFileHeaderCodeFixProvider.cs +++ b/src/Analyzers/Core/CodeFixes/FileHeaders/AbstractFileHeaderCodeFixProvider.cs @@ -48,7 +48,7 @@ private async Task GetTransformedDocumentAsync(Document document, Canc private async Task GetTransformedSyntaxRootAsync(Document document, CancellationToken cancellationToken) { - var options = await document.GetCodeFixOptionsAsync(cancellationToken).ConfigureAwait(false); + var options = await document.GetLineFormattingOptionsAsync(cancellationToken).ConfigureAwait(false); var generator = document.GetRequiredLanguageService(); var newLineTrivia = generator.EndOfLine(options.NewLine); diff --git a/src/Analyzers/Core/CodeFixes/Formatting/FormattingCodeFixProvider.cs b/src/Analyzers/Core/CodeFixes/Formatting/FormattingCodeFixProvider.cs index df7fba56ed205..65b24aaac1ae2 100644 --- a/src/Analyzers/Core/CodeFixes/Formatting/FormattingCodeFixProvider.cs +++ b/src/Analyzers/Core/CodeFixes/Formatting/FormattingCodeFixProvider.cs @@ -71,8 +71,7 @@ private async Task FixOneAsync(CodeFixContext context, Diagnostic diag text.Lines[diagnosticLinePositionSpan.Start.Line].Start, text.Lines[diagnosticLinePositionSpan.End.Line].End); - var options = await context.Document.GetCodeFixOptionsAsync(cancellationToken).ConfigureAwait(false); - var formattingOptions = options.GetFormattingOptions(SyntaxFormatting); + var formattingOptions = await context.Document.GetSyntaxFormattingOptionsAsync(SyntaxFormatting, cancellationToken).ConfigureAwait(false); var formattedRoot = SyntaxFormatting.GetFormattingResult(root, [spanToFormat], formattingOptions, rules: default, cancellationToken: cancellationToken).GetFormattedRoot(cancellationToken); return context.Document.WithSyntaxRoot(formattedRoot); @@ -80,8 +79,7 @@ private async Task FixOneAsync(CodeFixContext context, Diagnostic diag protected override async Task FixAllAsync(Document document, ImmutableArray diagnostics, SyntaxEditor editor, CancellationToken cancellationToken) { - var options = await document.GetCodeFixOptionsAsync(cancellationToken).ConfigureAwait(false); - var formattingOptions = options.GetFormattingOptions(SyntaxFormatting); + var formattingOptions = await document.GetSyntaxFormattingOptionsAsync(SyntaxFormatting, cancellationToken).ConfigureAwait(false); var updatedRoot = SyntaxFormatting.GetFormattingResult(editor.OriginalRoot, [editor.OriginalRoot.FullSpan], formattingOptions, rules: default, cancellationToken).GetFormattedRoot(cancellationToken); editor.ReplaceNode(editor.OriginalRoot, updatedRoot); } diff --git a/src/Analyzers/Core/CodeFixes/NewLines/ConsecutiveStatementPlacement/ConsecutiveStatementPlacementCodeFixProvider.cs b/src/Analyzers/Core/CodeFixes/NewLines/ConsecutiveStatementPlacement/ConsecutiveStatementPlacementCodeFixProvider.cs index b2f596fd3bafb..b79c4bc2147e8 100644 --- a/src/Analyzers/Core/CodeFixes/NewLines/ConsecutiveStatementPlacement/ConsecutiveStatementPlacementCodeFixProvider.cs +++ b/src/Analyzers/Core/CodeFixes/NewLines/ConsecutiveStatementPlacement/ConsecutiveStatementPlacementCodeFixProvider.cs @@ -12,6 +12,7 @@ using Microsoft.CodeAnalysis.CodeFixes; using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.Editing; +using Microsoft.CodeAnalysis.Formatting; using Microsoft.CodeAnalysis.Host.Mef; using Microsoft.CodeAnalysis.Shared.Extensions; @@ -43,7 +44,7 @@ private static Task UpdateDocumentAsync(Document document, Diagnostic public static async Task FixAllAsync(Document document, ImmutableArray diagnostics, CancellationToken cancellationToken) { var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false); - var options = await document.GetCodeFixOptionsAsync(cancellationToken).ConfigureAwait(false); + var options = await document.GetLineFormattingOptionsAsync(cancellationToken).ConfigureAwait(false); var generator = document.GetRequiredLanguageService(); var endOfLineTrivia = generator.EndOfLine(options.NewLine); diff --git a/src/Analyzers/Core/CodeFixes/RemoveUnusedParametersAndValues/AbstractRemoveUnusedValuesCodeFixProvider.cs b/src/Analyzers/Core/CodeFixes/RemoveUnusedParametersAndValues/AbstractRemoveUnusedValuesCodeFixProvider.cs index 0de67926e1b30..9e78db585b68e 100644 --- a/src/Analyzers/Core/CodeFixes/RemoveUnusedParametersAndValues/AbstractRemoveUnusedValuesCodeFixProvider.cs +++ b/src/Analyzers/Core/CodeFixes/RemoveUnusedParametersAndValues/AbstractRemoveUnusedValuesCodeFixProvider.cs @@ -284,8 +284,7 @@ private static async Task PreprocessDocumentAsync(Document document, I protected sealed override async Task FixAllAsync(Document document, ImmutableArray diagnostics, SyntaxEditor editor, CancellationToken cancellationToken) { - var options = await document.GetCodeFixOptionsAsync(cancellationToken).ConfigureAwait(false); - var formattingOptions = options.GetFormattingOptions(SyntaxFormatting); + var formattingOptions = await document.GetSyntaxFormattingOptionsAsync(SyntaxFormatting, cancellationToken).ConfigureAwait(false); var preprocessedDocument = await PreprocessDocumentAsync(document, diagnostics, cancellationToken).ConfigureAwait(false); var newRoot = await GetNewRootAsync(preprocessedDocument, formattingOptions, diagnostics, cancellationToken).ConfigureAwait(false); editor.ReplaceNode(editor.OriginalRoot, newRoot); diff --git a/src/Analyzers/Core/CodeFixes/UseConditionalExpression/AbstractUseConditionalExpressionCodeFixProvider.cs b/src/Analyzers/Core/CodeFixes/UseConditionalExpression/AbstractUseConditionalExpressionCodeFixProvider.cs index 0daa06f16a045..fcba559fe08f2 100644 --- a/src/Analyzers/Core/CodeFixes/UseConditionalExpression/AbstractUseConditionalExpressionCodeFixProvider.cs +++ b/src/Analyzers/Core/CodeFixes/UseConditionalExpression/AbstractUseConditionalExpressionCodeFixProvider.cs @@ -48,8 +48,7 @@ protected override async Task FixAllAsync( { var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false); - var options = await document.GetCodeFixOptionsAsync(cancellationToken).ConfigureAwait(false); - var formattingOptions = options.GetFormattingOptions(SyntaxFormatting); + var formattingOptions = await document.GetSyntaxFormattingOptionsAsync(SyntaxFormatting, cancellationToken).ConfigureAwait(false); // Defer to our callback to actually make the edits for each diagnostic. In turn, it // will return 'true' if it made a multi-line conditional expression. In that case, diff --git a/src/Features/CSharp/Portable/CodeRefactorings/EnableNullable/EnableNullableCodeRefactoringProvider.cs b/src/Features/CSharp/Portable/CodeRefactorings/EnableNullable/EnableNullableCodeRefactoringProvider.cs index 9178dc16f1f98..347374fd1813c 100644 --- a/src/Features/CSharp/Portable/CodeRefactorings/EnableNullable/EnableNullableCodeRefactoringProvider.cs +++ b/src/Features/CSharp/Portable/CodeRefactorings/EnableNullable/EnableNullableCodeRefactoringProvider.cs @@ -11,6 +11,7 @@ using System.Threading.Tasks; using Microsoft.CodeAnalysis.CodeActions; using Microsoft.CodeAnalysis.CodeRefactorings; +using Microsoft.CodeAnalysis.CSharp.Formatting; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Shared.Extensions; using Microsoft.CodeAnalysis.Shared.Utilities; @@ -159,7 +160,7 @@ private static (SyntaxNode root, SyntaxToken firstToken) RewriteExistingDirectiv private static async Task DisableNullableReferenceTypesInExistingDocumentIfNecessaryAsync(Document document, SyntaxNode root, SyntaxToken firstToken, CancellationToken cancellationToken) { - var options = await document.GetCSharpCodeFixOptionsProviderAsync(cancellationToken).ConfigureAwait(false); + var options = await document.GetCSharpSyntaxFormattingOptionsAsync(cancellationToken).ConfigureAwait(false); var newLine = SyntaxFactory.EndOfLine(options.NewLine); // Add a new '#nullable disable' to the top of each file diff --git a/src/Features/CSharp/Portable/ConvertNamespace/ConvertNamespaceCodeRefactoringProvider.cs b/src/Features/CSharp/Portable/ConvertNamespace/ConvertNamespaceCodeRefactoringProvider.cs index 40c6be6b2b608..fd2273161d404 100644 --- a/src/Features/CSharp/Portable/ConvertNamespace/ConvertNamespaceCodeRefactoringProvider.cs +++ b/src/Features/CSharp/Portable/ConvertNamespace/ConvertNamespaceCodeRefactoringProvider.cs @@ -12,6 +12,7 @@ using Microsoft.CodeAnalysis.CodeFixes; using Microsoft.CodeAnalysis.CodeRefactorings; using Microsoft.CodeAnalysis.CodeStyle; +using Microsoft.CodeAnalysis.CSharp.Formatting; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Editing; using Microsoft.CodeAnalysis.Shared.Extensions; @@ -47,18 +48,18 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte if (!IsValidPosition(namespaceDecl, position)) return; - var options = await document.GetCSharpCodeFixOptionsProviderAsync(cancellationToken).ConfigureAwait(false); + var options = await document.GetCSharpSyntaxFormattingOptionsAsync(cancellationToken).ConfigureAwait(false); if (!CanOfferRefactoring(namespaceDecl, root, options, out var info)) return; context.RegisterRefactoring(CodeAction.Create( - info.Value.title, c => ConvertAsync(document, namespaceDecl, options.GetFormattingOptions(), c), info.Value.equivalenceKey)); + info.Value.title, c => ConvertAsync(document, namespaceDecl, options, c), info.Value.equivalenceKey)); } private static bool CanOfferRefactoring( [NotNullWhen(true)] BaseNamespaceDeclarationSyntax? namespaceDecl, CompilationUnitSyntax root, - CSharpCodeFixOptionsProvider options, + CSharpSyntaxFormattingOptions options, [NotNullWhen(true)] out (string title, string equivalenceKey)? info) { info = @@ -91,7 +92,7 @@ protected override async Task FixAllAsync( CancellationToken cancellationToken) { var root = (CompilationUnitSyntax)await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false); - var options = await document.GetCSharpCodeFixOptionsProviderAsync(cancellationToken).ConfigureAwait(false); + var options = await document.GetCSharpSyntaxFormattingOptionsAsync(cancellationToken).ConfigureAwait(false); var namespaceDecl = root.DescendantNodes().OfType().FirstOrDefault(); if (!CanOfferRefactoring(namespaceDecl, root, options, out var info) || info.Value.equivalenceKey != equivalenceKey) @@ -99,7 +100,7 @@ protected override async Task FixAllAsync( return; } - document = await ConvertAsync(document, namespaceDecl, options.GetFormattingOptions(), cancellationToken).ConfigureAwait(false); + document = await ConvertAsync(document, namespaceDecl, options, cancellationToken).ConfigureAwait(false); var newRoot = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false); editor.ReplaceNode(editor.OriginalRoot, newRoot); } diff --git a/src/Features/CSharp/Portable/ConvertProgram/ConvertToProgramMainCodeFixProvider.cs b/src/Features/CSharp/Portable/ConvertProgram/ConvertToProgramMainCodeFixProvider.cs index 3da827966e8c0..66fb3d762e5c3 100644 --- a/src/Features/CSharp/Portable/ConvertProgram/ConvertToProgramMainCodeFixProvider.cs +++ b/src/Features/CSharp/Portable/ConvertProgram/ConvertToProgramMainCodeFixProvider.cs @@ -9,8 +9,10 @@ using System.Threading.Tasks; using Microsoft.CodeAnalysis.CodeActions; using Microsoft.CodeAnalysis.CodeFixes; +using Microsoft.CodeAnalysis.CSharp.Formatting; using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.Editing; +using Microsoft.CodeAnalysis.Formatting; using Microsoft.CodeAnalysis.Host.Mef; using Microsoft.CodeAnalysis.Shared.Extensions; @@ -35,7 +37,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context) var document = context.Document; var cancellationToken = context.CancellationToken; - var options = await document.GetCSharpCodeFixOptionsProviderAsync(cancellationToken).ConfigureAwait(false); + var options = await document.GetCSharpSyntaxFormattingOptionsAsync(cancellationToken).ConfigureAwait(false); var priority = options.PreferTopLevelStatements.Notification.Severity == ReportDiagnostic.Hidden ? CodeActionPriority.Low : CodeActionPriority.Default; @@ -46,7 +48,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context) protected override async Task FixAllAsync( Document document, ImmutableArray diagnostics, SyntaxEditor editor, CancellationToken cancellationToken) { - var options = await document.GetCodeFixOptionsAsync(cancellationToken).ConfigureAwait(false); + var options = await document.GetSyntaxFormattingOptionsAsync(cancellationToken).ConfigureAwait(false); var fixedDocument = await ConvertToProgramMainAsync(document, options.AccessibilityModifiersRequired, cancellationToken).ConfigureAwait(false); var fixedRoot = await fixedDocument.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false); diff --git a/src/Features/CSharp/Portable/ConvertProgram/ConvertToProgramMainCodeRefactoringProvider.cs b/src/Features/CSharp/Portable/ConvertProgram/ConvertToProgramMainCodeRefactoringProvider.cs index ffaa168778071..fccadd6b9fda5 100644 --- a/src/Features/CSharp/Portable/ConvertProgram/ConvertToProgramMainCodeRefactoringProvider.cs +++ b/src/Features/CSharp/Portable/ConvertProgram/ConvertToProgramMainCodeRefactoringProvider.cs @@ -9,6 +9,7 @@ using Microsoft.CodeAnalysis.CodeRefactorings; using Microsoft.CodeAnalysis.CSharp.Analyzers.ConvertProgram; using Microsoft.CodeAnalysis.CSharp.Extensions; +using Microsoft.CodeAnalysis.CSharp.Formatting; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Host.Mef; using Microsoft.CodeAnalysis.Shared.Extensions; @@ -46,7 +47,7 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte if (!acceptableLocation.SourceSpan.IntersectsWith(position)) return; - var options = await document.GetCSharpCodeFixOptionsProviderAsync(cancellationToken).ConfigureAwait(false); + var options = await document.GetCSharpSyntaxFormattingOptionsAsync(cancellationToken).ConfigureAwait(false); var compilation = await document.Project.GetRequiredCompilationAsync(cancellationToken).ConfigureAwait(false); if (!CanOfferUseProgramMain(options.PreferTopLevelStatements, root, compilation, forAnalyzer: false)) @@ -54,7 +55,7 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte context.RegisterRefactoring(CodeAction.Create( CSharpAnalyzersResources.Convert_to_Program_Main_style_program, - c => ConvertToProgramMainAsync(document, options.AccessibilityModifiersRequired.Value, c), + c => ConvertToProgramMainAsync(document, options.AccessibilityModifiersRequired, c), nameof(CSharpAnalyzersResources.Convert_to_Program_Main_style_program), CodeActionPriority.Low)); } diff --git a/src/Features/CSharp/Portable/ConvertProgram/ConvertToTopLevelStatementsCodeFixProvider.cs b/src/Features/CSharp/Portable/ConvertProgram/ConvertToTopLevelStatementsCodeFixProvider.cs index a39e3138992d6..aa3dbd4a0b1fe 100644 --- a/src/Features/CSharp/Portable/ConvertProgram/ConvertToTopLevelStatementsCodeFixProvider.cs +++ b/src/Features/CSharp/Portable/ConvertProgram/ConvertToTopLevelStatementsCodeFixProvider.cs @@ -9,6 +9,7 @@ using System.Threading.Tasks; using Microsoft.CodeAnalysis.CodeActions; using Microsoft.CodeAnalysis.CodeFixes; +using Microsoft.CodeAnalysis.CSharp.Formatting; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.Editing; @@ -36,7 +37,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context) var document = context.Document; var cancellationToken = context.CancellationToken; - var options = await document.GetCSharpCodeFixOptionsProviderAsync(cancellationToken).ConfigureAwait(false); + var options = await document.GetCSharpSyntaxFormattingOptionsAsync(cancellationToken).ConfigureAwait(false); var priority = options.PreferTopLevelStatements.Notification.Severity == ReportDiagnostic.Hidden ? CodeActionPriority.Low : CodeActionPriority.Default; diff --git a/src/Features/CSharp/Portable/ConvertProgram/ConvertToTopLevelStatementsCodeRefactoringProvider.cs b/src/Features/CSharp/Portable/ConvertProgram/ConvertToTopLevelStatementsCodeRefactoringProvider.cs index 997ca9cfad008..41952a70f6a69 100644 --- a/src/Features/CSharp/Portable/ConvertProgram/ConvertToTopLevelStatementsCodeRefactoringProvider.cs +++ b/src/Features/CSharp/Portable/ConvertProgram/ConvertToTopLevelStatementsCodeRefactoringProvider.cs @@ -9,6 +9,7 @@ using Microsoft.CodeAnalysis.CodeRefactorings; using Microsoft.CodeAnalysis.CSharp.Analyzers.ConvertProgram; using Microsoft.CodeAnalysis.CSharp.Extensions; +using Microsoft.CodeAnalysis.CSharp.Formatting; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Host.Mef; using Microsoft.CodeAnalysis.Shared.Extensions; @@ -42,7 +43,7 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte if (methodDeclaration is null) return; - var options = await document.GetCSharpCodeFixOptionsProviderAsync(cancellationToken).ConfigureAwait(false); + var options = await document.GetCSharpSyntaxFormattingOptionsAsync(cancellationToken).ConfigureAwait(false); if (!CanOfferUseTopLevelStatements(options.PreferTopLevelStatements, forAnalyzer: false)) return; diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/AddImport/AddImportPlacementOptions.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/AddImport/AddImportPlacementOptions.cs index b06105cca50e2..ce7d53e0b3f64 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/AddImport/AddImportPlacementOptions.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/AddImport/AddImportPlacementOptions.cs @@ -38,23 +38,3 @@ internal sealed record class AddImportPlacementOptions public static readonly AddImportPlacementOptions Default = new(); } - -internal static partial class AddImportPlacementOptionsProviders -{ -#if !CODE_STYLE - public static AddImportPlacementOptions GetAddImportPlacementOptions(this IOptionsReader options, LanguageServices languageServices, bool? allowInHiddenRegions) - => languageServices.GetRequiredService().GetAddImportOptions(options, allowInHiddenRegions ?? AddImportPlacementOptions.Default.AllowInHiddenRegions); - - public static async ValueTask GetAddImportPlacementOptionsAsync(this Document document, CancellationToken cancellationToken) - { - var configOptions = await document.GetAnalyzerConfigOptionsAsync(cancellationToken).ConfigureAwait(false); - return configOptions.GetAddImportPlacementOptions(document.Project.Services, document.AllowImportsInHiddenRegions()); - } - - // Normally we don't allow generation into a hidden region in the file. However, if we have a - // modern span mapper at our disposal, we do allow it as that host span mapper can handle mapping - // our edit to their domain appropriate. - public static bool AllowImportsInHiddenRegions(this Document document) - => document.Services.GetService()?.SupportsMappingImportDirectives == true; -#endif -} diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Formatting/LineFormattingOptions.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Formatting/LineFormattingOptions.cs index 6e034d7b50492..794d03784b3f6 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Formatting/LineFormattingOptions.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Formatting/LineFormattingOptions.cs @@ -4,15 +4,12 @@ using System; using System.Runtime.Serialization; -using System.Threading; -using System.Threading.Tasks; -using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.Options; namespace Microsoft.CodeAnalysis.Formatting; [DataContract] -internal sealed record class LineFormattingOptions +internal sealed record class LineFormattingOptions() { public static readonly LineFormattingOptions Default = new(); @@ -20,25 +17,14 @@ internal sealed record class LineFormattingOptions [DataMember] public int TabSize { get; init; } = 4; [DataMember] public int IndentationSize { get; init; } = 4; [DataMember] public string NewLine { get; init; } = Environment.NewLine; -} - -internal static partial class LineFormattingOptionsProviders -{ - public static LineFormattingOptions GetLineFormattingOptions(this IOptionsReader options, string language) - => new() - { - UseTabs = options.GetOption(FormattingOptions2.UseTabs, language), - TabSize = options.GetOption(FormattingOptions2.TabSize, language), - IndentationSize = options.GetOption(FormattingOptions2.IndentationSize, language), - NewLine = options.GetOption(FormattingOptions2.NewLine, language), - }; -#if !CODE_STYLE - public static async ValueTask GetLineFormattingOptionsAsync(this Document document, CancellationToken cancellationToken) + public LineFormattingOptions(IOptionsReader options, string language) + : this() { - var configOptions = await document.GetAnalyzerConfigOptionsAsync(cancellationToken).ConfigureAwait(false); - return configOptions.GetLineFormattingOptions(document.Project.Language); + UseTabs = options.GetOption(FormattingOptions2.UseTabs, language); + TabSize = options.GetOption(FormattingOptions2.TabSize, language); + IndentationSize = options.GetOption(FormattingOptions2.IndentationSize, language); + NewLine = options.GetOption(FormattingOptions2.NewLine, language); } -#endif } diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Formatting/SyntaxFormattingOptions.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Formatting/SyntaxFormattingOptions.cs index f97d20a65fc66..2163bb66630ea 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Formatting/SyntaxFormattingOptions.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Formatting/SyntaxFormattingOptions.cs @@ -7,12 +7,6 @@ using Microsoft.CodeAnalysis.Editing; using Microsoft.CodeAnalysis.Options; -#if !CODE_STYLE -using System.Threading; -using System.Threading.Tasks; -using Microsoft.CodeAnalysis.Host; -#endif - namespace Microsoft.CodeAnalysis.Formatting; internal record class SyntaxFormattingOptions @@ -34,7 +28,7 @@ private protected SyntaxFormattingOptions() private protected SyntaxFormattingOptions(IOptionsReader options, string language) { - LineFormatting = options.GetLineFormattingOptions(language); + LineFormatting = new LineFormattingOptions(options, language); SeparateImportDirectiveGroups = options.GetOption(GenerationOptions.SeparateImportDirectiveGroups, language); AccessibilityModifiersRequired = options.GetOptionValue(CodeStyleOptions2.AccessibilityModifiersRequired, language); WrappingColumn = options.GetOption(FormattingOptions2.WrappingColumn, language); @@ -47,21 +41,7 @@ private protected SyntaxFormattingOptions(IOptionsReader options, string languag public string NewLine => LineFormatting.NewLine; #if !CODE_STYLE - public static SyntaxFormattingOptions GetDefault(LanguageServices languageServices) + public static SyntaxFormattingOptions GetDefault(Host.LanguageServices languageServices) => languageServices.GetRequiredService().DefaultOptions; #endif } - -internal static partial class SyntaxFormattingOptionsProviders -{ -#if !CODE_STYLE - public static SyntaxFormattingOptions GetSyntaxFormattingOptions(this IOptionsReader options, LanguageServices languageServices) - => languageServices.GetRequiredService().GetFormattingOptions(options); - - public static async ValueTask GetSyntaxFormattingOptionsAsync(this Document document, CancellationToken cancellationToken) - { - var configOptions = await document.GetAnalyzerConfigOptionsAsync(cancellationToken).ConfigureAwait(false); - return configOptions.GetSyntaxFormattingOptions(document.Project.Services); - } -#endif -} diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Simplification/SimplifierOptions.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Simplification/SimplifierOptions.cs index c0d000aa20b0a..6d5818eb6d6d3 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Simplification/SimplifierOptions.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Simplification/SimplifierOptions.cs @@ -7,12 +7,6 @@ using Microsoft.CodeAnalysis.CodeStyle; using Microsoft.CodeAnalysis.Options; -#if !CODE_STYLE -using System.Threading; -using System.Threading.Tasks; -using Microsoft.CodeAnalysis.Host; -#endif - namespace Microsoft.CodeAnalysis.Simplification; internal record class SimplifierOptions @@ -60,21 +54,7 @@ public bool TryGetQualifyMemberAccessOption(SymbolKind symbolKind, [NotNullWhen( } #if !CODE_STYLE - public static SimplifierOptions GetDefault(LanguageServices languageServices) + public static SimplifierOptions GetDefault(Host.LanguageServices languageServices) => languageServices.GetRequiredService().DefaultOptions; #endif } - -internal static partial class SimplifierOptionsProviders -{ -#if !CODE_STYLE - public static SimplifierOptions GetSimplifierOptions(this IOptionsReader options, LanguageServices languageServices) - => languageServices.GetService()?.GetSimplifierOptions(options) ?? SimplifierOptions.CommonDefaults; - - public static async ValueTask GetSimplifierOptionsAsync(this Document document, CancellationToken cancellationToken) - { - var configOptions = await document.GetAnalyzerConfigOptionsAsync(cancellationToken).ConfigureAwait(false); - return configOptions.GetSimplifierOptions(document.Project.Services); - } -#endif -} diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/CSharpWorkspaceExtensions.projitems b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/CSharpWorkspaceExtensions.projitems index f2cba6f9d9789..3d1ed9d703fd6 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/CSharpWorkspaceExtensions.projitems +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/CSharpWorkspaceExtensions.projitems @@ -9,7 +9,6 @@ Microsoft.CodeAnalysis.CSharp.Shared - @@ -87,6 +86,8 @@ + + diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/CodeActions/CSharpCodeFixOptionsProvider.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/CodeActions/CSharpCodeFixOptionsProvider.cs deleted file mode 100644 index 985ac67a657e5..0000000000000 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/CodeActions/CSharpCodeFixOptionsProvider.cs +++ /dev/null @@ -1,65 +0,0 @@ -// 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.Threading; -using System.Threading.Tasks; -using Microsoft.CodeAnalysis.AddImport; -using Microsoft.CodeAnalysis.CodeStyle; -using Microsoft.CodeAnalysis.CSharp.CodeStyle; -using Microsoft.CodeAnalysis.CSharp.Formatting; -using Microsoft.CodeAnalysis.CSharp.Simplification; -using Microsoft.CodeAnalysis.Formatting; -using Microsoft.CodeAnalysis.Host; -using Microsoft.CodeAnalysis.Options; -using Microsoft.CodeAnalysis.Shared.Extensions; -using Microsoft.CodeAnalysis.Simplification; - -namespace Microsoft.CodeAnalysis.CodeActions; - -internal readonly struct CSharpCodeFixOptionsProvider(IOptionsReader options, HostLanguageServices languageServices) -{ - // LineFormattingOptions - - public string NewLine => GetOption(FormattingOptions2.NewLine); - - // SimplifierOptions - - public CodeStyleOption2 VarForBuiltInTypes => GetOption(CSharpCodeStyleOptions.VarForBuiltInTypes); - public CodeStyleOption2 VarElsewhere => GetOption(CSharpCodeStyleOptions.VarElsewhere); - - public SimplifierOptions GetSimplifierOptions() - => new CSharpSimplifierOptions(options); - - // FormattingOptions - - public CodeStyleOption2 NamespaceDeclarations => GetOption(CSharpCodeStyleOptions.NamespaceDeclarations); - public CodeStyleOption2 PreferTopLevelStatements => GetOption(CSharpCodeStyleOptions.PreferTopLevelStatements); - - internal CSharpSyntaxFormattingOptions GetFormattingOptions() - => new(options); - - // AddImportPlacementOptions - - public CodeStyleOption2 UsingDirectivePlacement => GetOption(CSharpCodeStyleOptions.PreferredUsingDirectivePlacement); - - // CodeStyleOptions - - public CodeStyleOption2 PreferredModifierOrder => GetOption(CSharpCodeStyleOptions.PreferredModifierOrder); - public CodeStyleOption2 AccessibilityModifiersRequired => GetOption(CodeStyleOptions2.AccessibilityModifiersRequired); - - private TValue GetOption(Option2 option) - => options.GetOption(option); - - private TValue GetOption(PerLanguageOption2 option) - => options.GetOption(option, languageServices.Language); -} - -internal static class CSharpCodeFixOptionsProviders -{ - public static async ValueTask GetCSharpCodeFixOptionsProviderAsync(this Document document, CancellationToken cancellationToken) - { - var configOptions = await document.GetAnalyzerConfigOptionsAsync(cancellationToken).ConfigureAwait(false); - return new CSharpCodeFixOptionsProvider(configOptions.GetOptionsReader(), document.Project.GetExtendedLanguageServices()); - } -} diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/Formatting/CSharpSyntaxFormattingOptionsProviders.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/Formatting/CSharpSyntaxFormattingOptionsProviders.cs new file mode 100644 index 0000000000000..29a385575213a --- /dev/null +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/Formatting/CSharpSyntaxFormattingOptionsProviders.cs @@ -0,0 +1,19 @@ +// 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.Threading; +using System.Threading.Tasks; +using Microsoft.CodeAnalysis.Options; +using Microsoft.CodeAnalysis.Shared.Extensions; + +namespace Microsoft.CodeAnalysis.CSharp.Formatting; + +internal static class CSharpSyntaxFormattingOptionsProviders +{ + public static async ValueTask GetCSharpSyntaxFormattingOptionsAsync(this Document document, CancellationToken cancellationToken) + { + var configOptions = await document.GetAnalyzerConfigOptionsAsync(cancellationToken).ConfigureAwait(false); + return new CSharpSyntaxFormattingOptions(configOptions); + } +} diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/LanguageServices/CSharpRemoveUnnecessaryImportsService.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/LanguageServices/CSharpRemoveUnnecessaryImportsService.cs index 71cd7d40e4490..f014b6b6ce952 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/LanguageServices/CSharpRemoveUnnecessaryImportsService.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/LanguageServices/CSharpRemoveUnnecessaryImportsService.cs @@ -62,8 +62,7 @@ public override async Task RemoveUnnecessaryImportsAsync( var spansToFormat = new List(); AddFormattingSpans(newRoot, spansToFormat, cancellationToken); - var options = await document.GetCodeFixOptionsAsync(cancellationToken).ConfigureAwait(false); - var formattingOptions = options.GetFormattingOptions(SyntaxFormatting); + var formattingOptions = await document.GetSyntaxFormattingOptionsAsync(SyntaxFormatting, cancellationToken).ConfigureAwait(false); var formattedRoot = SyntaxFormatting.GetFormattingResult(newRoot, spansToFormat, formattingOptions, rules: default, cancellationToken).GetFormattedRoot(cancellationToken); return document.WithSyntaxRoot(formattedRoot); diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/Simplification/CSharpSimplifierOptionsProviders.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/Simplification/CSharpSimplifierOptionsProviders.cs new file mode 100644 index 0000000000000..b7a8e7688ef50 --- /dev/null +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/Simplification/CSharpSimplifierOptionsProviders.cs @@ -0,0 +1,19 @@ +// 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.Threading; +using System.Threading.Tasks; +using Microsoft.CodeAnalysis.Options; +using Microsoft.CodeAnalysis.Shared.Extensions; + +namespace Microsoft.CodeAnalysis.CSharp.Simplification; + +internal static class CSharpSimplifierOptionsProviders +{ + public static async ValueTask GetCSharpSimplifierOptionsAsync(this Document document, CancellationToken cancellationToken) + { + var configOptions = await document.GetAnalyzerConfigOptionsAsync(cancellationToken).ConfigureAwait(false); + return new CSharpSimplifierOptions(configOptions); + } +} diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/AddImport/AddImportPlacementOptionsProviders.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/AddImport/AddImportPlacementOptionsProviders.cs index 44ee410c56294..121804f0dfacd 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/AddImport/AddImportPlacementOptionsProviders.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/AddImport/AddImportPlacementOptionsProviders.cs @@ -4,21 +4,32 @@ using System.Threading; using System.Threading.Tasks; -using Microsoft.CodeAnalysis.Shared.Extensions; using Microsoft.CodeAnalysis.Options; +using Microsoft.CodeAnalysis.Shared.Extensions; namespace Microsoft.CodeAnalysis.AddImport; -internal static partial class AddImportPlacementOptionsProviders +internal static class AddImportPlacementOptionsProviders { - internal static async ValueTask GetAddImportPlacementOptionsAsync(this Document document, IAddImportsService addImportsService, CancellationToken cancellationToken) - { + // Normally we don't allow generation into a hidden region in the file. However, if we have a + // modern span mapper at our disposal, we do allow it as that host span mapper can handle mapping + // our edit to their domain appropriate. + public static bool AllowImportsInHiddenRegions(this Document document) #if CODE_STYLE - var syntaxTree = await document.GetRequiredSyntaxTreeAsync(cancellationToken).ConfigureAwait(false); - var configOptions = document.Project.AnalyzerOptions.AnalyzerConfigOptionsProvider.GetOptions(syntaxTree).GetOptionsReader(); - return addImportsService.GetAddImportOptions(configOptions, allowInHiddenRegions: false); + => AddImportPlacementOptions.Default.AllowInHiddenRegions; #else - return await document.GetAddImportPlacementOptionsAsync(cancellationToken).ConfigureAwait(false); + => document.Services.GetService()?.SupportsMappingImportDirectives == true; #endif + +#if !CODE_STYLE + public static AddImportPlacementOptions GetAddImportPlacementOptions(this IOptionsReader options, Host.LanguageServices languageServices, bool? allowInHiddenRegions) + => languageServices.GetRequiredService().GetAddImportOptions(options, allowInHiddenRegions ?? AddImportPlacementOptions.Default.AllowInHiddenRegions); +#endif + + public static async ValueTask GetAddImportPlacementOptionsAsync(this Document document, CancellationToken cancellationToken) + { + var configOptions = await document.GetAnalyzerConfigOptionsAsync(cancellationToken).ConfigureAwait(false); + var service = document.GetRequiredLanguageService(); + return service.GetAddImportOptions(configOptions, document.AllowImportsInHiddenRegions()); } } diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/CodeActions/CodeFixOptionsProvider.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/CodeActions/CodeFixOptionsProvider.cs deleted file mode 100644 index 521f11283500d..0000000000000 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/CodeActions/CodeFixOptionsProvider.cs +++ /dev/null @@ -1,41 +0,0 @@ -// 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.Threading; -using System.Threading.Tasks; -using Microsoft.CodeAnalysis.CodeStyle; -using Microsoft.CodeAnalysis.Formatting; -using Microsoft.CodeAnalysis.Options; -using Microsoft.CodeAnalysis.Shared.Extensions; - -namespace Microsoft.CodeAnalysis.CodeActions; - -internal readonly struct CodeFixOptionsProvider(IOptionsReader options, string language) -{ - // LineFormattingOptions - - public string NewLine => GetOption(FormattingOptions2.NewLine); - - public LineFormattingOptions GetLineFormattingOptions() - => options.GetLineFormattingOptions(language); - - // SyntaxFormattingOptions - - public SyntaxFormattingOptions GetFormattingOptions(ISyntaxFormatting formatting) - => formatting.GetFormattingOptions(options); - - public AccessibilityModifiersRequired AccessibilityModifiersRequired => options.GetOptionValue(CodeStyleOptions2.AccessibilityModifiersRequired, language); - - private TValue GetOption(PerLanguageOption2 option) - => options.GetOption(option, language); -} - -internal static class CodeFixOptionsProviders -{ - public static async ValueTask GetCodeFixOptionsAsync(this Document document, CancellationToken cancellationToken) - { - var configOptions = await document.GetAnalyzerConfigOptionsAsync(cancellationToken).ConfigureAwait(false); - return new CodeFixOptionsProvider(configOptions.GetOptionsReader(), document.Project.Language); - } -} diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/Extensions/DocumentExtensions.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/Extensions/DocumentExtensions.cs index d7d5fa8da2911..33e5d7ffda476 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/Extensions/DocumentExtensions.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/Extensions/DocumentExtensions.cs @@ -15,6 +15,7 @@ using Microsoft.CodeAnalysis.Text; using Roslyn.Utilities; using Microsoft.CodeAnalysis.Editing; +using Microsoft.CodeAnalysis.Options; #if DEBUG using System.Collections.Immutable; @@ -222,10 +223,10 @@ public static IEnumerable GetLinkedDocuments(this Document document) } #if CODE_STYLE - public static async ValueTask GetAnalyzerConfigOptionsAsync(this Document document, CancellationToken cancellationToken) + public static async ValueTask GetAnalyzerConfigOptionsAsync(this Document document, CancellationToken cancellationToken) { var syntaxTree = await document.GetRequiredSyntaxTreeAsync(cancellationToken).ConfigureAwait(false); - return document.Project.AnalyzerOptions.AnalyzerConfigOptionsProvider.GetOptions(syntaxTree); + return document.Project.AnalyzerOptions.AnalyzerConfigOptionsProvider.GetOptions(syntaxTree).GetOptionsReader(); } #endif } diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/Formatting/LineFormattingOptionsProviders.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/Formatting/LineFormattingOptionsProviders.cs new file mode 100644 index 0000000000000..04c840f43ede5 --- /dev/null +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/Formatting/LineFormattingOptionsProviders.cs @@ -0,0 +1,23 @@ +// 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.Threading; +using System.Threading.Tasks; +using Microsoft.CodeAnalysis.Options; +using Microsoft.CodeAnalysis.Shared.Extensions; + +namespace Microsoft.CodeAnalysis.Formatting; + +internal static class LineFormattingOptionsProviders +{ + public static LineFormattingOptions GetLineFormattingOptions(this IOptionsReader options, string language) + => new(options, language); + + public static async ValueTask GetLineFormattingOptionsAsync(this Document document, CancellationToken cancellationToken) + { + var configOptions = await document.GetAnalyzerConfigOptionsAsync(cancellationToken).ConfigureAwait(false); + return configOptions.GetLineFormattingOptions(document.Project.Language); + } +} + diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/Formatting/SyntaxFormattingOptionsProviders.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/Formatting/SyntaxFormattingOptionsProviders.cs new file mode 100644 index 0000000000000..8bbc96f499ceb --- /dev/null +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/Formatting/SyntaxFormattingOptionsProviders.cs @@ -0,0 +1,27 @@ +// 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.Threading; +using System.Threading.Tasks; +using Microsoft.CodeAnalysis.Options; +using Microsoft.CodeAnalysis.Shared.Extensions; + +namespace Microsoft.CodeAnalysis.Formatting; + +internal static class SyntaxFormattingOptionsProviders +{ +#if !CODE_STYLE + public static SyntaxFormattingOptions GetSyntaxFormattingOptions(this IOptionsReader options, Host.LanguageServices languageServices) + => languageServices.GetRequiredService().GetFormattingOptions(options); + + public static ValueTask GetSyntaxFormattingOptionsAsync(this Document document, CancellationToken cancellationToken) + => GetSyntaxFormattingOptionsAsync(document, document.Project.Services.GetRequiredService(), cancellationToken); +#endif + + public static async ValueTask GetSyntaxFormattingOptionsAsync(this Document document, ISyntaxFormatting formatting, CancellationToken cancellationToken) + { + var configOptions = await document.GetAnalyzerConfigOptionsAsync(cancellationToken).ConfigureAwait(false); + return formatting.GetFormattingOptions(configOptions); + } +} diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/Simplification/SimplifierOptionsProviders.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/Simplification/SimplifierOptionsProviders.cs new file mode 100644 index 0000000000000..d2e85d01ad8e6 --- /dev/null +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/Simplification/SimplifierOptionsProviders.cs @@ -0,0 +1,28 @@ +// 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.Threading; +using System.Threading.Tasks; +using Microsoft.CodeAnalysis.Options; +using Microsoft.CodeAnalysis.Shared.Extensions; + +namespace Microsoft.CodeAnalysis.Simplification; + +internal static class SimplifierOptionsProviders +{ +#if !CODE_STYLE + public static SimplifierOptions GetSimplifierOptions(this IOptionsReader options, Host.LanguageServices languageServices) + => languageServices.GetService()?.GetSimplifierOptions(options) ?? SimplifierOptions.CommonDefaults; + + public static ValueTask GetSimplifierOptionsAsync(this Document document, CancellationToken cancellationToken) + => GetSimplifierOptionsAsync(document, document.Project.Services.GetRequiredService(), cancellationToken); +#endif + + public static async ValueTask GetSimplifierOptionsAsync(this Document document, ISimplification simplification, CancellationToken cancellationToken) + { + var configOptions = await document.GetAnalyzerConfigOptionsAsync(cancellationToken).ConfigureAwait(false); + return simplification.GetSimplifierOptions(configOptions); + } +} + diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/WorkspaceExtensions.projitems b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/WorkspaceExtensions.projitems index 9da175ee39a6b..493506bc67572 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/WorkspaceExtensions.projitems +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/WorkspaceExtensions.projitems @@ -12,7 +12,6 @@ - @@ -145,6 +144,9 @@ + + +