diff --git a/src/Analyzers/CSharp/CodeFixes/RemoveUnusedParametersAndValues/CSharpRemoveUnusedValuesCodeFixProvider.cs b/src/Analyzers/CSharp/CodeFixes/RemoveUnusedParametersAndValues/CSharpRemoveUnusedValuesCodeFixProvider.cs index 434d14f295da9..b459db309e58d 100644 --- a/src/Analyzers/CSharp/CodeFixes/RemoveUnusedParametersAndValues/CSharpRemoveUnusedValuesCodeFixProvider.cs +++ b/src/Analyzers/CSharp/CodeFixes/RemoveUnusedParametersAndValues/CSharpRemoveUnusedValuesCodeFixProvider.cs @@ -30,7 +30,7 @@ internal sealed class CSharpRemoveUnusedValuesCodeFixProvider() ExpressionStatementSyntax, LocalDeclarationStatementSyntax, VariableDeclaratorSyntax, ForEachStatementSyntax, SwitchSectionSyntax, SwitchLabelSyntax, CatchClauseSyntax, CatchClauseSyntax> { - protected override ISyntaxFormatting GetSyntaxFormatting() + protected override ISyntaxFormatting SyntaxFormatting => CSharpSyntaxFormatting.Instance; protected override BlockSyntax WrapWithBlockIfNecessary(IEnumerable statements) diff --git a/src/Analyzers/CSharp/CodeFixes/UseCollectionExpression/CSharpCollectionExpressionRewriter.cs b/src/Analyzers/CSharp/CodeFixes/UseCollectionExpression/CSharpCollectionExpressionRewriter.cs index 3912db017c814..8d138887a1187 100644 --- a/src/Analyzers/CSharp/CodeFixes/UseCollectionExpression/CSharpCollectionExpressionRewriter.cs +++ b/src/Analyzers/CSharp/CodeFixes/UseCollectionExpression/CSharpCollectionExpressionRewriter.cs @@ -46,12 +46,8 @@ public static async Task CreateCollectionExpressionA var document = await ParsedDocument.CreateAsync(workspaceDocument, cancellationToken).ConfigureAwait(false); -#if CODE_STYLE - var formattingOptions = CSharpSyntaxFormattingOptions.Default; -#else - var formattingOptions = (CSharpSyntaxFormattingOptions)await workspaceDocument.GetSyntaxFormattingOptionsAsync(cancellationToken).ConfigureAwait(false); -#endif - + var options = await workspaceDocument.GetCodeFixOptionsAsync(cancellationToken).ConfigureAwait(false); + var formattingOptions = (CSharpSyntaxFormattingOptions)options.GetFormattingOptions(CSharpSyntaxFormatting.Instance); var indentationOptions = new IndentationOptions(formattingOptions); var wrappingLength = formattingOptions.CollectionExpressionWrappingLength; diff --git a/src/Analyzers/CSharp/CodeFixes/UseConditionalExpression/CSharpUseConditionalExpressionForAssignmentCodeFixProvider.cs b/src/Analyzers/CSharp/CodeFixes/UseConditionalExpression/CSharpUseConditionalExpressionForAssignmentCodeFixProvider.cs index 39bcf0dc146c5..8fb28e7def2a0 100644 --- a/src/Analyzers/CSharp/CodeFixes/UseConditionalExpression/CSharpUseConditionalExpressionForAssignmentCodeFixProvider.cs +++ b/src/Analyzers/CSharp/CodeFixes/UseConditionalExpression/CSharpUseConditionalExpressionForAssignmentCodeFixProvider.cs @@ -57,6 +57,6 @@ protected override StatementSyntax WrapWithBlockIfAppropriate( protected override ExpressionSyntax ConvertToExpression(IThrowOperation throwOperation) => CSharpUseConditionalExpressionHelpers.ConvertToExpression(throwOperation); - protected override ISyntaxFormatting GetSyntaxFormatting() + protected override ISyntaxFormatting SyntaxFormatting => CSharpSyntaxFormatting.Instance; } diff --git a/src/Analyzers/CSharp/CodeFixes/UseConditionalExpression/CSharpUseConditionalExpressionForReturnCodeFixProvider.cs b/src/Analyzers/CSharp/CodeFixes/UseConditionalExpression/CSharpUseConditionalExpressionForReturnCodeFixProvider.cs index ba4a932007522..e1dca2d1db1ae 100644 --- a/src/Analyzers/CSharp/CodeFixes/UseConditionalExpression/CSharpUseConditionalExpressionForReturnCodeFixProvider.cs +++ b/src/Analyzers/CSharp/CodeFixes/UseConditionalExpression/CSharpUseConditionalExpressionForReturnCodeFixProvider.cs @@ -60,6 +60,6 @@ protected override ExpressionSyntax WrapReturnExpressionIfNecessary(ExpressionSy protected override ExpressionSyntax ConvertToExpression(IThrowOperation throwOperation) => CSharpUseConditionalExpressionHelpers.ConvertToExpression(throwOperation); - protected override ISyntaxFormatting GetSyntaxFormatting() + protected override ISyntaxFormatting SyntaxFormatting => CSharpSyntaxFormatting.Instance; } diff --git a/src/Analyzers/Core/Analyzers/Analyzers.projitems b/src/Analyzers/Core/Analyzers/Analyzers.projitems index 845981b50d1b3..bc9457e004538 100644 --- a/src/Analyzers/Core/Analyzers/Analyzers.projitems +++ b/src/Analyzers/Core/Analyzers/Analyzers.projitems @@ -33,8 +33,6 @@ - - diff --git a/src/Analyzers/Core/Analyzers/Formatting/AbstractFormattingAnalyzer.cs b/src/Analyzers/Core/Analyzers/Formatting/AbstractFormattingAnalyzer.cs index 82238c1b92d24..7d4f8392dff2c 100644 --- a/src/Analyzers/Core/Analyzers/Formatting/AbstractFormattingAnalyzer.cs +++ b/src/Analyzers/Core/Analyzers/Formatting/AbstractFormattingAnalyzer.cs @@ -4,6 +4,8 @@ using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.Formatting; +using Microsoft.CodeAnalysis.Text; +using Roslyn.Utilities; namespace Microsoft.CodeAnalysis.CodeStyle; @@ -41,7 +43,63 @@ private void AnalyzeSyntaxTree(SyntaxTreeAnalysisContext context, CompilationOpt if (ShouldSkipAnalysis(context, compilationOptions, notification: null)) return; - var options = context.GetAnalyzerOptions().GetSyntaxFormattingOptions(SyntaxFormatting); - FormattingAnalyzerHelper.AnalyzeSyntaxTree(context, SyntaxFormatting, Descriptor, options); + var tree = context.Tree; + var cancellationToken = context.CancellationToken; + + var oldText = tree.GetText(cancellationToken); + var root = tree.GetRoot(cancellationToken); + var span = context.FilterSpan.HasValue ? context.FilterSpan.GetValueOrDefault() : root.FullSpan; + var spans = SpecializedCollections.SingletonEnumerable(span); + var formattingOptions = context.GetAnalyzerOptions().GetSyntaxFormattingOptions(SyntaxFormatting); + var formattingChanges = SyntaxFormatting.GetFormattingResult(root, spans, formattingOptions, rules: default, cancellationToken).GetTextChanges(cancellationToken); + + // formattingChanges could include changes that impact a larger section of the original document than + // necessary. Before reporting diagnostics, process the changes to minimize the span of individual + // diagnostics. + foreach (var formattingChange in formattingChanges) + { + var change = formattingChange; + Contract.ThrowIfNull(change.NewText); + + if (change.NewText.Length > 0 && !change.Span.IsEmpty) + { + // Handle cases where the change is a substring removal from the beginning. In these cases, we want + // the diagnostic span to cover the unwanted leading characters (which should be removed), and + // nothing more. + var offset = change.Span.Length - change.NewText.Length; + if (offset >= 0) + { + if (oldText.GetSubText(new TextSpan(change.Span.Start + offset, change.NewText.Length)).ContentEquals(SourceText.From(change.NewText))) + { + change = new TextChange(new TextSpan(change.Span.Start, offset), ""); + } + else + { + // Handle cases where the change is a substring removal from the end. In these cases, we want + // the diagnostic span to cover the unwanted trailing characters (which should be removed), and + // nothing more. + if (oldText.GetSubText(new TextSpan(change.Span.Start, change.NewText.Length)).ContentEquals(SourceText.From(change.NewText))) + { + change = new TextChange(new TextSpan(change.Span.Start + change.NewText.Length, offset), ""); + } + } + } + } + + Contract.ThrowIfNull(change.NewText); + if (change.NewText.Length == 0 && change.Span.IsEmpty) + { + // No actual change (allows for the formatter to report a NOP change without triggering a + // diagnostic that can't be fixed). + continue; + } + + var location = Location.Create(tree, change.Span); + context.ReportDiagnostic(Diagnostic.Create( + Descriptor, + location, + additionalLocations: null, + properties: null)); + } } } diff --git a/src/Analyzers/Core/Analyzers/Formatting/FormatterHelper.cs b/src/Analyzers/Core/Analyzers/Formatting/FormatterHelper.cs deleted file mode 100644 index ad221cea502b9..0000000000000 --- a/src/Analyzers/Core/Analyzers/Formatting/FormatterHelper.cs +++ /dev/null @@ -1,68 +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.Collections.Generic; -using System.Collections.Immutable; -using System.Threading; -using Microsoft.CodeAnalysis.Formatting.Rules; -using Microsoft.CodeAnalysis.Text; -using Roslyn.Utilities; -using static Microsoft.CodeAnalysis.Formatting.FormattingExtensions; - -namespace Microsoft.CodeAnalysis.Formatting; - -/// -/// Formats whitespace in documents or syntax trees. -/// -internal static class FormatterHelper -{ - /// - /// Gets the formatting rules that would be applied if left unspecified. - /// - internal static IEnumerable GetDefaultFormattingRules(ISyntaxFormatting syntaxFormattingService) - => syntaxFormattingService.GetDefaultFormattingRules(); - - /// - /// Formats the whitespace of a syntax tree. - /// - /// The root node of a syntax tree to format. - /// An optional set of formatting options. If these options are not supplied the current set of options from the workspace will be used. - /// An optional cancellation token. - /// The formatted tree's root node. - public static SyntaxNode Format(SyntaxNode node, ISyntaxFormatting syntaxFormattingService, SyntaxFormattingOptions options, CancellationToken cancellationToken) - => Format(node, [node.FullSpan], syntaxFormattingService, options, rules: default, cancellationToken: cancellationToken); - - public static SyntaxNode Format(SyntaxNode node, TextSpan spanToFormat, ISyntaxFormatting syntaxFormattingService, SyntaxFormattingOptions options, CancellationToken cancellationToken) - => Format(node, [spanToFormat], syntaxFormattingService, options, rules: default, cancellationToken: cancellationToken); - - /// - /// Formats the whitespace of a syntax tree. - /// - /// The root node of a syntax tree. - /// The descendant nodes of the root to format. - /// An optional set of formatting options. If these options are not supplied the current set of options from the workspace will be used. - /// An optional cancellation token. - /// The formatted tree's root node. - public static SyntaxNode Format(SyntaxNode node, SyntaxAnnotation annotation, ISyntaxFormatting syntaxFormattingService, SyntaxFormattingOptions options, IEnumerable? rules, CancellationToken cancellationToken) - => Format(node, GetAnnotatedSpans(node, annotation), syntaxFormattingService, options, rules.AsImmutableOrNull(), cancellationToken: cancellationToken); - - internal static SyntaxNode Format(SyntaxNode node, IEnumerable spans, ISyntaxFormatting syntaxFormattingService, SyntaxFormattingOptions options, ImmutableArray rules, CancellationToken cancellationToken) - => GetFormattingResult(node, spans, syntaxFormattingService, options, rules, cancellationToken).GetFormattedRoot(cancellationToken); - - internal static IList GetFormattedTextChanges(SyntaxNode node, IEnumerable spans, ISyntaxFormatting syntaxFormattingService, SyntaxFormattingOptions options, ImmutableArray rules, CancellationToken cancellationToken) - => GetFormattingResult(node, spans, syntaxFormattingService, options, rules, cancellationToken).GetTextChanges(cancellationToken); - - internal static IFormattingResult GetFormattingResult(SyntaxNode node, IEnumerable spans, ISyntaxFormatting syntaxFormattingService, SyntaxFormattingOptions options, ImmutableArray rules, CancellationToken cancellationToken) - => syntaxFormattingService.GetFormattingResult(node, spans, options, rules, cancellationToken); - - /// - /// Determines the changes necessary to format the whitespace of a syntax tree. - /// - /// The root node of a syntax tree to format. - /// An optional set of formatting options. If these options are not supplied the current set of options from the workspace will be used. - /// An optional cancellation token. - /// The changes necessary to format the tree. - public static IList GetFormattedTextChanges(SyntaxNode node, ISyntaxFormatting syntaxFormattingService, SyntaxFormattingOptions options, CancellationToken cancellationToken) - => GetFormattedTextChanges(node, [node.FullSpan], syntaxFormattingService, options, rules: default, cancellationToken: cancellationToken); -} diff --git a/src/Analyzers/Core/Analyzers/Formatting/FormattingAnalyzerHelper.cs b/src/Analyzers/Core/Analyzers/Formatting/FormattingAnalyzerHelper.cs deleted file mode 100644 index 7eb990634811a..0000000000000 --- a/src/Analyzers/Core/Analyzers/Formatting/FormattingAnalyzerHelper.cs +++ /dev/null @@ -1,75 +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. - -#nullable disable - -using Microsoft.CodeAnalysis.Diagnostics; -using Microsoft.CodeAnalysis.Formatting; -using Microsoft.CodeAnalysis.Text; -using Roslyn.Utilities; -using Formatter = Microsoft.CodeAnalysis.Formatting.FormatterHelper; -using FormattingProvider = Microsoft.CodeAnalysis.Formatting.ISyntaxFormatting; - -namespace Microsoft.CodeAnalysis.CodeStyle; - -internal static class FormattingAnalyzerHelper -{ - internal static void AnalyzeSyntaxTree(SyntaxTreeAnalysisContext context, FormattingProvider formattingProvider, DiagnosticDescriptor descriptor, SyntaxFormattingOptions options) - { - var tree = context.Tree; - var cancellationToken = context.CancellationToken; - - var oldText = tree.GetText(cancellationToken); - var root = tree.GetRoot(cancellationToken); - var span = context.FilterSpan.HasValue ? context.FilterSpan.GetValueOrDefault() : root.FullSpan; - var spans = SpecializedCollections.SingletonEnumerable(span); - var formattingChanges = Formatter.GetFormattedTextChanges(root, spans, formattingProvider, options, rules: default, cancellationToken); - - // formattingChanges could include changes that impact a larger section of the original document than - // necessary. Before reporting diagnostics, process the changes to minimize the span of individual - // diagnostics. - foreach (var formattingChange in formattingChanges) - { - var change = formattingChange; - if (change.NewText.Length > 0 && !change.Span.IsEmpty) - { - // Handle cases where the change is a substring removal from the beginning. In these cases, we want - // the diagnostic span to cover the unwanted leading characters (which should be removed), and - // nothing more. - var offset = change.Span.Length - change.NewText.Length; - if (offset >= 0) - { - if (oldText.GetSubText(new TextSpan(change.Span.Start + offset, change.NewText.Length)).ContentEquals(SourceText.From(change.NewText))) - { - change = new TextChange(new TextSpan(change.Span.Start, offset), ""); - } - else - { - // Handle cases where the change is a substring removal from the end. In these cases, we want - // the diagnostic span to cover the unwanted trailing characters (which should be removed), and - // nothing more. - if (oldText.GetSubText(new TextSpan(change.Span.Start, change.NewText.Length)).ContentEquals(SourceText.From(change.NewText))) - { - change = new TextChange(new TextSpan(change.Span.Start + change.NewText.Length, offset), ""); - } - } - } - } - - if (change.NewText.Length == 0 && change.Span.IsEmpty) - { - // No actual change (allows for the formatter to report a NOP change without triggering a - // diagnostic that can't be fixed). - continue; - } - - var location = Location.Create(tree, change.Span); - context.ReportDiagnostic(Diagnostic.Create( - descriptor, - location, - additionalLocations: null, - properties: null)); - } - } -} diff --git a/src/Analyzers/Core/CodeFixes/CodeFixes.projitems b/src/Analyzers/Core/CodeFixes/CodeFixes.projitems index 82fd681da3329..407477c962427 100644 --- a/src/Analyzers/Core/CodeFixes/CodeFixes.projitems +++ b/src/Analyzers/Core/CodeFixes/CodeFixes.projitems @@ -36,7 +36,6 @@ - diff --git a/src/Analyzers/Core/CodeFixes/Formatting/FormattingCodeFixHelper.cs b/src/Analyzers/Core/CodeFixes/Formatting/FormattingCodeFixHelper.cs deleted file mode 100644 index a20c0646ab33c..0000000000000 --- a/src/Analyzers/Core/CodeFixes/Formatting/FormattingCodeFixHelper.cs +++ /dev/null @@ -1,32 +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.Formatting; -using Microsoft.CodeAnalysis.Text; - -using Formatter = Microsoft.CodeAnalysis.Formatting.FormatterHelper; -using FormattingProvider = Microsoft.CodeAnalysis.Formatting.ISyntaxFormatting; - -namespace Microsoft.CodeAnalysis; - -internal static class FormattingCodeFixHelper -{ - internal static async Task FixOneAsync(SyntaxTree syntaxTree, FormattingProvider formattingProvider, SyntaxFormattingOptions options, Diagnostic diagnostic, CancellationToken cancellationToken) - { - // The span to format is the full line(s) containing the diagnostic - var text = await syntaxTree.GetTextAsync(cancellationToken).ConfigureAwait(false); - var diagnosticSpan = diagnostic.Location.SourceSpan; - var diagnosticLinePositionSpan = text.Lines.GetLinePositionSpan(diagnosticSpan); - var spanToFormat = TextSpan.FromBounds( - text.Lines[diagnosticLinePositionSpan.Start.Line].Start, - text.Lines[diagnosticLinePositionSpan.End.Line].End); - - var root = await syntaxTree.GetRootAsync(cancellationToken).ConfigureAwait(false); - var formattedRoot = Formatter.Format(root, spanToFormat, formattingProvider, options, cancellationToken); - - return syntaxTree.WithRootAndOptions(formattedRoot, syntaxTree.Options); - } -} diff --git a/src/Analyzers/Core/CodeFixes/Formatting/FormattingCodeFixProvider.cs b/src/Analyzers/Core/CodeFixes/Formatting/FormattingCodeFixProvider.cs index 15d5ceb71cbd3..df7fba56ed205 100644 --- a/src/Analyzers/Core/CodeFixes/Formatting/FormattingCodeFixProvider.cs +++ b/src/Analyzers/Core/CodeFixes/Formatting/FormattingCodeFixProvider.cs @@ -11,8 +11,7 @@ using Microsoft.CodeAnalysis.Editing; using Microsoft.CodeAnalysis.Formatting; using Microsoft.CodeAnalysis.Shared.Extensions; - -using Formatter = Microsoft.CodeAnalysis.Formatting.FormatterHelper; +using Microsoft.CodeAnalysis.Text; namespace Microsoft.CodeAnalysis.CodeStyle; @@ -62,18 +61,28 @@ public sealed override Task RegisterCodeFixesAsync(CodeFixContext context) private async Task FixOneAsync(CodeFixContext context, Diagnostic diagnostic, CancellationToken cancellationToken) { + var root = await context.Document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false); + var text = root.GetText(); + + // The span to format is the full line(s) containing the diagnostic + var diagnosticSpan = diagnostic.Location.SourceSpan; + var diagnosticLinePositionSpan = text.Lines.GetLinePositionSpan(diagnosticSpan); + var spanToFormat = TextSpan.FromBounds( + 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 tree = await context.Document.GetRequiredSyntaxTreeAsync(cancellationToken).ConfigureAwait(false); - var updatedTree = await FormattingCodeFixHelper.FixOneAsync(tree, SyntaxFormatting, formattingOptions, diagnostic, cancellationToken).ConfigureAwait(false); - return context.Document.WithText(await updatedTree.GetTextAsync(cancellationToken).ConfigureAwait(false)); + var formattedRoot = SyntaxFormatting.GetFormattingResult(root, [spanToFormat], formattingOptions, rules: default, cancellationToken: cancellationToken).GetFormattedRoot(cancellationToken); + + return context.Document.WithSyntaxRoot(formattedRoot); } 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 updatedRoot = Formatter.Format(editor.OriginalRoot, SyntaxFormatting, formattingOptions, cancellationToken); + 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/RemoveUnnecessaryImports/AbstractRemoveUnnecessaryImportsCodeFixProvider.cs b/src/Analyzers/Core/CodeFixes/RemoveUnnecessaryImports/AbstractRemoveUnnecessaryImportsCodeFixProvider.cs index e922d0b210d18..a731d282a912c 100644 --- a/src/Analyzers/Core/CodeFixes/RemoveUnnecessaryImports/AbstractRemoveUnnecessaryImportsCodeFixProvider.cs +++ b/src/Analyzers/Core/CodeFixes/RemoveUnnecessaryImports/AbstractRemoveUnnecessaryImportsCodeFixProvider.cs @@ -38,14 +38,11 @@ public sealed override Task RegisterCodeFixesAsync(CodeFixContext context) protected abstract string GetTitle(); - private async Task RemoveUnnecessaryImportsAsync( + private static async Task RemoveUnnecessaryImportsAsync( Document document, CancellationToken cancellationToken) { var service = document.GetRequiredLanguageService(); - - var options = await document.GetCodeFixOptionsAsync(cancellationToken).ConfigureAwait(false); - var formattingOptions = options.GetFormattingOptions(GetSyntaxFormatting()); - return await service.RemoveUnnecessaryImportsAsync(document, formattingOptions, cancellationToken).ConfigureAwait(false); + return await service.RemoveUnnecessaryImportsAsync(document, cancellationToken).ConfigureAwait(false); } } diff --git a/src/Analyzers/Core/CodeFixes/RemoveUnusedParametersAndValues/AbstractRemoveUnusedValuesCodeFixProvider.cs b/src/Analyzers/Core/CodeFixes/RemoveUnusedParametersAndValues/AbstractRemoveUnusedValuesCodeFixProvider.cs index 1b23c3f0ee10b..0de67926e1b30 100644 --- a/src/Analyzers/Core/CodeFixes/RemoveUnusedParametersAndValues/AbstractRemoveUnusedValuesCodeFixProvider.cs +++ b/src/Analyzers/Core/CodeFixes/RemoveUnusedParametersAndValues/AbstractRemoveUnusedValuesCodeFixProvider.cs @@ -63,7 +63,7 @@ internal abstract class AbstractRemoveUnusedValuesCodeFixProvider FixableDiagnosticIds => [IDEDiagnosticIds.ExpressionValueIsUnusedDiagnosticId, IDEDiagnosticIds.ValueAssignedIsUnusedDiagnosticId]; - protected abstract ISyntaxFormatting GetSyntaxFormatting(); + protected abstract ISyntaxFormatting SyntaxFormatting { get; } /// /// Method to update the identifier token for the local/parameter declaration or reference @@ -285,7 +285,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(GetSyntaxFormatting()); + var formattingOptions = options.GetFormattingOptions(SyntaxFormatting); var preprocessedDocument = await PreprocessDocumentAsync(document, diagnostics, cancellationToken).ConfigureAwait(false); var newRoot = await GetNewRootAsync(preprocessedDocument, formattingOptions, diagnostics, cancellationToken).ConfigureAwait(false); editor.ReplaceNode(editor.OriginalRoot, newRoot); @@ -846,15 +846,10 @@ private async Task AdjustLocalDeclarationsAsync( // Finally, we apply replace the memberDeclaration in the originalEditor as a single edit. var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false); var rootWithTrackedNodes = root.TrackNodes(originalDeclStatementsToMoveOrRemove); + var spansToFormat = originalDeclStatementsToMoveOrRemove.Select(s => s.Span); // Run formatter prior to invoking IMoveDeclarationNearReferenceService. -#if CODE_STYLE - var provider = GetSyntaxFormatting(); - rootWithTrackedNodes = FormatterHelper.Format(rootWithTrackedNodes, originalDeclStatementsToMoveOrRemove.Select(s => s.Span), provider, options, rules: default, cancellationToken); -#else - var provider = document.Project.Solution.Services; - rootWithTrackedNodes = Formatter.Format(rootWithTrackedNodes, originalDeclStatementsToMoveOrRemove.Select(s => s.Span), provider, options, rules: default, cancellationToken); -#endif + rootWithTrackedNodes = SyntaxFormatting.GetFormattingResult(rootWithTrackedNodes, spansToFormat, options, rules: default, cancellationToken).GetFormattedRoot(cancellationToken); document = document.WithSyntaxRoot(rootWithTrackedNodes); await OnDocumentUpdatedAsync().ConfigureAwait(false); diff --git a/src/Analyzers/Core/CodeFixes/UseConditionalExpression/AbstractUseConditionalExpressionCodeFixProvider.cs b/src/Analyzers/Core/CodeFixes/UseConditionalExpression/AbstractUseConditionalExpressionCodeFixProvider.cs index 580548d1cd46d..0daa06f16a045 100644 --- a/src/Analyzers/Core/CodeFixes/UseConditionalExpression/AbstractUseConditionalExpressionCodeFixProvider.cs +++ b/src/Analyzers/Core/CodeFixes/UseConditionalExpression/AbstractUseConditionalExpressionCodeFixProvider.cs @@ -2,7 +2,6 @@ // 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.Collections.Generic; using System.Collections.Immutable; using System.Threading; using System.Threading.Tasks; @@ -16,12 +15,6 @@ using Microsoft.CodeAnalysis.Shared.Extensions; using Microsoft.CodeAnalysis.Simplification; -#if CODE_STYLE -using Formatter = Microsoft.CodeAnalysis.Formatting.FormatterHelper; -#else -using Formatter = Microsoft.CodeAnalysis.Formatting.Formatter; -#endif - namespace Microsoft.CodeAnalysis.UseConditionalExpression; using static UseConditionalExpressionCodeFixHelpers; @@ -40,7 +33,7 @@ internal abstract class AbstractUseConditionalExpressionCodeFixProvider< protected abstract ISyntaxFacts SyntaxFacts { get; } protected abstract AbstractFormattingRule GetMultiLineFormattingRule(); - protected abstract ISyntaxFormatting GetSyntaxFormatting(); + protected abstract ISyntaxFormatting SyntaxFormatting { get; } protected abstract TExpressionSyntax ConvertToExpression(IThrowOperation throwOperation); protected abstract TStatementSyntax WrapWithBlockIfAppropriate(TIfStatementSyntax ifStatement, TStatementSyntax statement); @@ -55,13 +48,8 @@ protected override async Task FixAllAsync( { var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false); -#if CODE_STYLE - var provider = GetSyntaxFormatting(); -#else - var provider = document.Project.Solution.Services; -#endif var options = await document.GetCodeFixOptionsAsync(cancellationToken).ConfigureAwait(false); - var formattingOptions = options.GetFormattingOptions(GetSyntaxFormatting()); + var formattingOptions = options.GetFormattingOptions(SyntaxFormatting); // 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, @@ -81,9 +69,9 @@ await FixOneAsync( // conditional expression as that's the only node that has the appropriate // annotation on it. var rules = ImmutableArray.Create(GetMultiLineFormattingRule()); + var spansToFormat = FormattingExtensions.GetAnnotatedSpans(changedRoot, SpecializedFormattingAnnotation); - var formattedRoot = Formatter.Format(changedRoot, SpecializedFormattingAnnotation, provider, formattingOptions, rules, cancellationToken); - + var formattedRoot = SyntaxFormatting.GetFormattingResult(changedRoot, spansToFormat, formattingOptions, rules, cancellationToken).GetFormattedRoot(cancellationToken); changedRoot = formattedRoot; editor.ReplaceNode(root, changedRoot); diff --git a/src/Analyzers/VisualBasic/CodeFixes/RemoveUnusedParametersAndValues/VisualBasicRemoveUnusedValuesCodeFixProvider.vb b/src/Analyzers/VisualBasic/CodeFixes/RemoveUnusedParametersAndValues/VisualBasicRemoveUnusedValuesCodeFixProvider.vb index 7f22c5dd7e244..7a5e5970879cf 100644 --- a/src/Analyzers/VisualBasic/CodeFixes/RemoveUnusedParametersAndValues/VisualBasicRemoveUnusedValuesCodeFixProvider.vb +++ b/src/Analyzers/VisualBasic/CodeFixes/RemoveUnusedParametersAndValues/VisualBasicRemoveUnusedValuesCodeFixProvider.vb @@ -25,9 +25,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.RemoveUnusedParametersAndValues Public Sub New() End Sub - Protected Overrides Function GetSyntaxFormatting() As ISyntaxFormatting - Return VisualBasicSyntaxFormatting.Instance - End Function + Protected Overrides ReadOnly Property SyntaxFormatting As ISyntaxFormatting = VisualBasicSyntaxFormatting.Instance Protected Overrides Function WrapWithBlockIfNecessary(statements As IEnumerable(Of StatementSyntax)) As StatementSyntax ' Unreachable code path as VB statements don't need to be wrapped in special BlockSyntax. diff --git a/src/Analyzers/VisualBasic/CodeFixes/UseConditionalExpression/VisualBasicUseConditionalExpressionForAssignmentCodeFixProvider.vb b/src/Analyzers/VisualBasic/CodeFixes/UseConditionalExpression/VisualBasicUseConditionalExpressionForAssignmentCodeFixProvider.vb index 1a6b8ee8fb0aa..05be72bc80158 100644 --- a/src/Analyzers/VisualBasic/CodeFixes/UseConditionalExpression/VisualBasicUseConditionalExpressionForAssignmentCodeFixProvider.vb +++ b/src/Analyzers/VisualBasic/CodeFixes/UseConditionalExpression/VisualBasicUseConditionalExpressionForAssignmentCodeFixProvider.vb @@ -56,8 +56,6 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.UseConditionalExpression Return statement End Function - Protected Overrides Function GetSyntaxFormatting() As ISyntaxFormatting - Return VisualBasicSyntaxFormatting.Instance - End Function + Protected Overrides ReadOnly Property SyntaxFormatting As ISyntaxFormatting = VisualBasicSyntaxFormatting.Instance End Class End Namespace diff --git a/src/Analyzers/VisualBasic/CodeFixes/UseConditionalExpression/VisualBasicUseConditionalExpressionForReturnCodeFixProvider.vb b/src/Analyzers/VisualBasic/CodeFixes/UseConditionalExpression/VisualBasicUseConditionalExpressionForReturnCodeFixProvider.vb index d52a4a8f6aafd..71031eef222f0 100644 --- a/src/Analyzers/VisualBasic/CodeFixes/UseConditionalExpression/VisualBasicUseConditionalExpressionForReturnCodeFixProvider.vb +++ b/src/Analyzers/VisualBasic/CodeFixes/UseConditionalExpression/VisualBasicUseConditionalExpressionForReturnCodeFixProvider.vb @@ -40,8 +40,6 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.UseConditionalExpression Return statement End Function - Protected Overrides Function GetSyntaxFormatting() As ISyntaxFormatting - Return VisualBasicSyntaxFormatting.Instance - End Function + Protected Overrides ReadOnly Property SyntaxFormatting As ISyntaxFormatting = VisualBasicSyntaxFormatting.Instance End Class End Namespace diff --git a/src/EditorFeatures/Core/Organizing/OrganizeDocumentCommandHandler.cs b/src/EditorFeatures/Core/Organizing/OrganizeDocumentCommandHandler.cs index a119ec7c5ab9e..e884b3129b9c7 100644 --- a/src/EditorFeatures/Core/Organizing/OrganizeDocumentCommandHandler.cs +++ b/src/EditorFeatures/Core/Organizing/OrganizeDocumentCommandHandler.cs @@ -191,7 +191,7 @@ public bool ExecuteCommand(SortAndRemoveUnnecessaryImportsCommandArgs args, Comm var removeImportsService = document.GetRequiredLanguageService(); var organizeImportsService = document.GetRequiredLanguageService(); - var newDocument = await removeImportsService.RemoveUnnecessaryImportsAsync(document, formattingOptions, cancellationToken).ConfigureAwait(false); + var newDocument = await removeImportsService.RemoveUnnecessaryImportsAsync(document, cancellationToken).ConfigureAwait(false); var options = await document.GetOrganizeImportsOptionsAsync(cancellationToken).ConfigureAwait(false); return await organizeImportsService.OrganizeImportsAsync(newDocument, options, cancellationToken).ConfigureAwait(false); }); diff --git a/src/Features/CSharp/Portable/ConvertProgram/ConvertProgramTransform_TopLevelStatements.cs b/src/Features/CSharp/Portable/ConvertProgram/ConvertProgramTransform_TopLevelStatements.cs index d56c030d2ee27..384f28f7892ea 100644 --- a/src/Features/CSharp/Portable/ConvertProgram/ConvertProgramTransform_TopLevelStatements.cs +++ b/src/Features/CSharp/Portable/ConvertProgram/ConvertProgramTransform_TopLevelStatements.cs @@ -83,7 +83,7 @@ private static async Task AddUsingDirectivesAsync( cancellationToken)); return await removeImportsService.RemoveUnnecessaryImportsAsync( - documentWithImportsAdded, n => n.HasAnnotation(annotation), options.FormattingOptions, cancellationToken).ConfigureAwait(false); + documentWithImportsAdded, n => n.HasAnnotation(annotation), cancellationToken).ConfigureAwait(false); } private static void AddUsingDirectives(NameSyntax name, SyntaxAnnotation annotation, ArrayBuilder directives) diff --git a/src/Features/Core/Portable/CodeRefactorings/MoveType/AbstractMoveTypeService.MoveTypeEditor.cs b/src/Features/Core/Portable/CodeRefactorings/MoveType/AbstractMoveTypeService.MoveTypeEditor.cs index ed50eee129f6c..29f4b9c7a8fe5 100644 --- a/src/Features/Core/Portable/CodeRefactorings/MoveType/AbstractMoveTypeService.MoveTypeEditor.cs +++ b/src/Features/Core/Portable/CodeRefactorings/MoveType/AbstractMoveTypeService.MoveTypeEditor.cs @@ -76,13 +76,12 @@ private async Task RemoveUnnecessaryImportsAsync( Solution solution, DocumentId sourceDocumentId, DocumentId documentWithMovedTypeId) { var documentWithMovedType = solution.GetRequiredDocument(documentWithMovedTypeId); - var documentWithMovedTypeFormattingOptions = await documentWithMovedType.GetSyntaxFormattingOptionsAsync(CancellationToken).ConfigureAwait(false); var syntaxFacts = documentWithMovedType.GetRequiredLanguageService(); var removeUnnecessaryImports = documentWithMovedType.GetRequiredLanguageService(); // Remove all unnecessary imports from the new document we've created. - documentWithMovedType = await removeUnnecessaryImports.RemoveUnnecessaryImportsAsync(documentWithMovedType, documentWithMovedTypeFormattingOptions, CancellationToken).ConfigureAwait(false); + documentWithMovedType = await removeUnnecessaryImports.RemoveUnnecessaryImportsAsync(documentWithMovedType, CancellationToken).ConfigureAwait(false); solution = solution.WithDocumentSyntaxRoot( documentWithMovedTypeId, await documentWithMovedType.GetRequiredSyntaxRootAsync(CancellationToken).ConfigureAwait(false)); @@ -95,11 +94,9 @@ private async Task RemoveUnnecessaryImportsAsync( // Now remove any unnecessary imports from the original doc that moved to the new doc. var sourceDocument = solution.GetRequiredDocument(sourceDocumentId); - var sourceDocumentFormattingOptions = await sourceDocument.GetSyntaxFormattingOptionsAsync(CancellationToken).ConfigureAwait(false); sourceDocument = await removeUnnecessaryImports.RemoveUnnecessaryImportsAsync( sourceDocument, n => movedImports.Contains(i => syntaxFacts.AreEquivalent(i, n)), - sourceDocumentFormattingOptions, CancellationToken).ConfigureAwait(false); return solution.WithDocumentSyntaxRoot( diff --git a/src/Features/Core/Portable/CodeRefactorings/SyncNamespace/AbstractChangeNamespaceService.cs b/src/Features/Core/Portable/CodeRefactorings/SyncNamespace/AbstractChangeNamespaceService.cs index cdc645c34fe07..e94836ac33f5f 100644 --- a/src/Features/Core/Portable/CodeRefactorings/SyncNamespace/AbstractChangeNamespaceService.cs +++ b/src/Features/Core/Portable/CodeRefactorings/SyncNamespace/AbstractChangeNamespaceService.cs @@ -798,7 +798,6 @@ async static Task RemoveUnnecessaryImportsWorkerAsync( return await removeImportService.RemoveUnnecessaryImportsAsync( doc, import => importsToRemove.Any(importToRemove => syntaxFacts.AreEquivalent(importToRemove, import)), - formattingOptions, token).ConfigureAwait(false); } } diff --git a/src/Features/Core/Portable/PullMemberUp/MembersPuller.cs b/src/Features/Core/Portable/PullMemberUp/MembersPuller.cs index f17ef72b7a1d2..889b117aefd05 100644 --- a/src/Features/Core/Portable/PullMemberUp/MembersPuller.cs +++ b/src/Features/Core/Portable/PullMemberUp/MembersPuller.cs @@ -386,7 +386,6 @@ private static async Task PullMembersIntoClassAsync( var destinationDocument = await removeImportsService.RemoveUnnecessaryImportsAsync( destinationEditor.GetChangedDocument(), node => node.HasAnnotation(s_removableImportAnnotation), - options.CleanupOptions.FormattingOptions, cancellationToken).ConfigureAwait(false); // Format whitespace trivia within the import statements we pull up diff --git a/src/LanguageServer/Protocol/ExternalAccess/Razor/FormatNewFileHandler.cs b/src/LanguageServer/Protocol/ExternalAccess/Razor/FormatNewFileHandler.cs index ecb2e80929404..1036b6cafa5dc 100644 --- a/src/LanguageServer/Protocol/ExternalAccess/Razor/FormatNewFileHandler.cs +++ b/src/LanguageServer/Protocol/ExternalAccess/Razor/FormatNewFileHandler.cs @@ -73,7 +73,7 @@ public FormatNewFileHandler(IGlobalOptionService globalOptions) var removeImportsService = document.GetLanguageService(); if (removeImportsService is not null) { - document = await removeImportsService.RemoveUnnecessaryImportsAsync(document, syntaxFormattingOptions, cancellationToken).ConfigureAwait(false); + document = await removeImportsService.RemoveUnnecessaryImportsAsync(document, cancellationToken).ConfigureAwait(false); } // Now format the document so indentation etc. is correct diff --git a/src/LanguageServer/Protocol/Features/CodeCleanup/AbstractCodeCleanupService.cs b/src/LanguageServer/Protocol/Features/CodeCleanup/AbstractCodeCleanupService.cs index 0030292e7994e..0c3b268d504a4 100644 --- a/src/LanguageServer/Protocol/Features/CodeCleanup/AbstractCodeCleanupService.cs +++ b/src/LanguageServer/Protocol/Features/CodeCleanup/AbstractCodeCleanupService.cs @@ -116,7 +116,7 @@ private static async Task RemoveSortUsingsAsync( using (Logger.LogBlock(FunctionId.CodeCleanup_RemoveUnusedImports, cancellationToken)) { var formattingOptions = await document.GetSyntaxFormattingOptionsAsync(cancellationToken).ConfigureAwait(false); - document = await removeUsingsService.RemoveUnnecessaryImportsAsync(document, formattingOptions, cancellationToken).ConfigureAwait(false); + document = await removeUsingsService.RemoveUnnecessaryImportsAsync(document, cancellationToken).ConfigureAwait(false); } } diff --git a/src/VisualStudio/Xaml/Impl/CodeFixes/RemoveUnnecessaryUsings/XamlRemoveUnnecessaryUsingsCodeFixProvider.cs b/src/VisualStudio/Xaml/Impl/CodeFixes/RemoveUnnecessaryUsings/XamlRemoveUnnecessaryUsingsCodeFixProvider.cs index 667ff5583be79..55d251f56d3d8 100644 --- a/src/VisualStudio/Xaml/Impl/CodeFixes/RemoveUnnecessaryUsings/XamlRemoveUnnecessaryUsingsCodeFixProvider.cs +++ b/src/VisualStudio/Xaml/Impl/CodeFixes/RemoveUnnecessaryUsings/XamlRemoveUnnecessaryUsingsCodeFixProvider.cs @@ -55,7 +55,7 @@ private static async Task RemoveUnnecessaryImportsAsync( Document document, CancellationToken cancellationToken) { var service = document.GetLanguageService(); - return await service.RemoveUnnecessaryImportsAsync(document, formattingOptions: null, cancellationToken).ConfigureAwait(false); + return await service.RemoveUnnecessaryImportsAsync(document, cancellationToken).ConfigureAwait(false); } } } diff --git a/src/VisualStudio/Xaml/Impl/Features/OrganizeImports/XamlRemoveUnnecessaryImportsService.cs b/src/VisualStudio/Xaml/Impl/Features/OrganizeImports/XamlRemoveUnnecessaryImportsService.cs index a100ffd03fea8..97d01f20e52c6 100644 --- a/src/VisualStudio/Xaml/Impl/Features/OrganizeImports/XamlRemoveUnnecessaryImportsService.cs +++ b/src/VisualStudio/Xaml/Impl/Features/OrganizeImports/XamlRemoveUnnecessaryImportsService.cs @@ -25,11 +25,11 @@ public XamlRemoveUnnecessaryImportsService(IXamlRemoveUnnecessaryNamespacesServi _removeService = removeService; } - public Task RemoveUnnecessaryImportsAsync(Document document, SyntaxFormattingOptions? formattingOptions, CancellationToken cancellationToken) - => RemoveUnnecessaryImportsAsync(document, predicate: null, formattingOptions, cancellationToken: cancellationToken); + public Task RemoveUnnecessaryImportsAsync(Document document, CancellationToken cancellationToken) + => RemoveUnnecessaryImportsAsync(document, predicate: null, cancellationToken: cancellationToken); public Task RemoveUnnecessaryImportsAsync( - Document document, Func? predicate, SyntaxFormattingOptions? formattingOptions, CancellationToken cancellationToken) + Document document, Func? predicate, CancellationToken cancellationToken) { return _removeService.RemoveUnnecessaryNamespacesAsync(document, cancellationToken) ?? Task.FromResult(document); } diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/LanguageServices/CSharpRemoveUnnecessaryImportsService.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/LanguageServices/CSharpRemoveUnnecessaryImportsService.cs index f59a9424eb31b..71cd7d40e4490 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/LanguageServices/CSharpRemoveUnnecessaryImportsService.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/CSharp/LanguageServices/CSharpRemoveUnnecessaryImportsService.cs @@ -5,29 +5,20 @@ using System; using System.Collections.Generic; using System.Composition; -using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Threading; using System.Threading.Tasks; +using Microsoft.CodeAnalysis.CodeActions; using Microsoft.CodeAnalysis.CSharp.Formatting; -using Microsoft.CodeAnalysis.CSharp.LanguageService; using Microsoft.CodeAnalysis.CSharp.Syntax; -using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.Formatting; using Microsoft.CodeAnalysis.Host.Mef; using Microsoft.CodeAnalysis.Internal.Log; -using Microsoft.CodeAnalysis.LanguageService; using Microsoft.CodeAnalysis.RemoveUnnecessaryImports; using Microsoft.CodeAnalysis.Shared.Extensions; using Microsoft.CodeAnalysis.Text; using Roslyn.Utilities; -#if CODE_STYLE -using Formatter = Microsoft.CodeAnalysis.Formatting.FormatterHelper; -#else -using Formatter = Microsoft.CodeAnalysis.Formatting.Formatter; -#endif - namespace Microsoft.CodeAnalysis.CSharp.RemoveUnnecessaryImports; [ExportLanguageService(typeof(IRemoveUnnecessaryImportsService), LanguageNames.CSharp), Shared] @@ -40,10 +31,8 @@ public CSharpRemoveUnnecessaryImportsService() { } -#if CODE_STYLE - private static ISyntaxFormatting GetSyntaxFormatting() + private static ISyntaxFormatting SyntaxFormatting => CSharpSyntaxFormatting.Instance; -#endif protected override IUnnecessaryImportsProvider UnnecessaryImportsProvider => CSharpUnnecessaryImportsProvider.Instance; @@ -51,11 +40,8 @@ protected override IUnnecessaryImportsProvider Unnecessary public override async Task RemoveUnnecessaryImportsAsync( Document document, Func? predicate, - SyntaxFormattingOptions? formattingOptions, CancellationToken cancellationToken) { - Contract.ThrowIfNull(formattingOptions); - predicate ??= Functions.True; using (Logger.LogBlock(FunctionId.Refactoring_RemoveUnnecessaryImports_CSharp, cancellationToken)) { @@ -72,14 +58,13 @@ public override async Task RemoveUnnecessaryImportsAsync( var newRoot = (CompilationUnitSyntax)new Rewriter(unnecessaryImports, cancellationToken).Visit(oldRoot); cancellationToken.ThrowIfCancellationRequested(); -#if CODE_STYLE - var provider = GetSyntaxFormatting(); -#else - var provider = document.Project.Solution.Services; -#endif - var spans = new List(); - AddFormattingSpans(newRoot, spans, cancellationToken); - var formattedRoot = Formatter.Format(newRoot, spans, provider, formattingOptions, rules: default, cancellationToken); + + var spansToFormat = new List(); + AddFormattingSpans(newRoot, spansToFormat, cancellationToken); + + var options = await document.GetCodeFixOptionsAsync(cancellationToken).ConfigureAwait(false); + var formattingOptions = options.GetFormattingOptions(SyntaxFormatting); + var formattedRoot = SyntaxFormatting.GetFormattingResult(newRoot, spansToFormat, formattingOptions, rules: default, cancellationToken).GetFormattedRoot(cancellationToken); return document.WithSyntaxRoot(formattedRoot); } diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/LanguageServices/RemoveUnnecessaryImports/AbstractRemoveUnnecessaryImportsService.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/LanguageServices/RemoveUnnecessaryImports/AbstractRemoveUnnecessaryImportsService.cs index a9ae9a3bcd3d1..be6a0da6fe3b4 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/LanguageServices/RemoveUnnecessaryImports/AbstractRemoveUnnecessaryImportsService.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/LanguageServices/RemoveUnnecessaryImports/AbstractRemoveUnnecessaryImportsService.cs @@ -20,10 +20,10 @@ internal abstract class AbstractRemoveUnnecessaryImportsService : { protected abstract IUnnecessaryImportsProvider UnnecessaryImportsProvider { get; } - public Task RemoveUnnecessaryImportsAsync(Document document, SyntaxFormattingOptions? formattingOptions, CancellationToken cancellationToken) - => RemoveUnnecessaryImportsAsync(document, predicate: null, formattingOptions, cancellationToken); + public Task RemoveUnnecessaryImportsAsync(Document document, CancellationToken cancellationToken) + => RemoveUnnecessaryImportsAsync(document, predicate: null, cancellationToken: cancellationToken); - public abstract Task RemoveUnnecessaryImportsAsync(Document fromDocument, Func? predicate, SyntaxFormattingOptions? formattingOptions, CancellationToken cancellationToken); + public abstract Task RemoveUnnecessaryImportsAsync(Document fromDocument, Func? predicate, CancellationToken cancellationToken); protected async Task> GetCommonUnnecessaryImportsOfAllContextAsync( Document document, Func predicate, CancellationToken cancellationToken) diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/LanguageServices/RemoveUnnecessaryImports/IRemoveUnnecessaryImportsService.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/LanguageServices/RemoveUnnecessaryImports/IRemoveUnnecessaryImportsService.cs index 8793966fef14e..76752273fe457 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/LanguageServices/RemoveUnnecessaryImports/IRemoveUnnecessaryImportsService.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/LanguageServices/RemoveUnnecessaryImports/IRemoveUnnecessaryImportsService.cs @@ -5,16 +5,13 @@ using System; using System.Threading; using System.Threading.Tasks; -using Microsoft.CodeAnalysis.Formatting; using Microsoft.CodeAnalysis.Host; namespace Microsoft.CodeAnalysis.RemoveUnnecessaryImports; internal interface IRemoveUnnecessaryImportsService : ILanguageService { - /// Null if the document does not support syntax trees. - Task RemoveUnnecessaryImportsAsync(Document document, SyntaxFormattingOptions? formattingOptions, CancellationToken cancellationToken); + Task RemoveUnnecessaryImportsAsync(Document document, CancellationToken cancellationToken); - /// Null if the document does not support syntax trees. - Task RemoveUnnecessaryImportsAsync(Document fromDocument, Func? predicate, SyntaxFormattingOptions? formattingOptions, CancellationToken cancellationToken); + Task RemoveUnnecessaryImportsAsync(Document fromDocument, Func? predicate, CancellationToken cancellationToken); } diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/VisualBasic/LanguageServices/VisualBasicRemoveUnnecessaryImportsService.vb b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/VisualBasic/LanguageServices/VisualBasicRemoveUnnecessaryImportsService.vb index a27384ea397b2..078dc07fb0be5 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/VisualBasic/LanguageServices/VisualBasicRemoveUnnecessaryImportsService.vb +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Workspace/VisualBasic/LanguageServices/VisualBasicRemoveUnnecessaryImportsService.vb @@ -26,11 +26,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.RemoveUnnecessaryImports Public Overrides Async Function RemoveUnnecessaryImportsAsync( document As Document, predicate As Func(Of SyntaxNode, Boolean), - formattingOptions As SyntaxFormattingOptions, cancellationToken As CancellationToken) As Task(Of Document) - Contract.ThrowIfNull(formattingOptions) - predicate = If(predicate, Functions(Of SyntaxNode).True) Using Logger.LogBlock(FunctionId.Refactoring_RemoveUnnecessaryImports_VisualBasic, cancellationToken)