Skip to content

Commit

Permalink
SyntaxFormattingOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat committed Jan 12, 2022
1 parent 5ee88dc commit 2a8ed8e
Show file tree
Hide file tree
Showing 47 changed files with 316 additions and 288 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Formatting;
using Microsoft.CodeAnalysis.CSharp.Shared.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.RemoveUnusedParametersAndValues;

Expand All @@ -31,6 +33,11 @@ public CSharpRemoveUnusedValuesCodeFixProvider()
{
}

#if CODE_STYLE
protected override ISyntaxFormattingService GetSyntaxFormattingService()
=> CSharpSyntaxFormattingService.Instance;
#endif

protected override BlockSyntax WrapWithBlockIfNecessary(IEnumerable<StatementSyntax> statements)
=> SyntaxFactory.Block(statements);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
using Microsoft.CodeAnalysis.Simplification;
using Roslyn.Utilities;

#if CODE_STYLE
using Formatter = Microsoft.CodeAnalysis.Formatting.FormatterHelper;
#else
using Formatter = Microsoft.CodeAnalysis.Formatting.Formatter;
#endif

namespace Microsoft.CodeAnalysis.RemoveUnusedParametersAndValues
{
/// <summary>
Expand Down Expand Up @@ -67,6 +73,8 @@ public sealed override ImmutableArray<string> FixableDiagnosticIds

internal sealed override CodeFixCategory CodeFixCategory => CodeFixCategory.CodeQuality;

protected abstract ISyntaxFormattingService GetSyntaxFormattingService();

/// <summary>
/// Method to update the identifier token for the local/parameter declaration or reference
/// that was flagged as an unused value write by the analyzer.
Expand Down Expand Up @@ -270,14 +278,14 @@ private static async Task<Document> PreprocessDocumentAsync(Document document, I
protected sealed override async Task FixAllAsync(Document document, ImmutableArray<Diagnostic> diagnostics, SyntaxEditor editor, CancellationToken cancellationToken)
{
var preprocessedDocument = await PreprocessDocumentAsync(document, diagnostics, cancellationToken).ConfigureAwait(false);
var options = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);
var options = await SyntaxFormattingOptions.FromDocumentAsync(document, cancellationToken).ConfigureAwait(false);
var newRoot = await GetNewRootAsync(preprocessedDocument, options, diagnostics, cancellationToken).ConfigureAwait(false);
editor.ReplaceNode(editor.OriginalRoot, newRoot);
}

private async Task<SyntaxNode> GetNewRootAsync(
Document document,
OptionSet options,
SyntaxFormattingOptions options,
ImmutableArray<Diagnostic> diagnostics,
CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -721,7 +729,7 @@ bool ShouldRemoveStatement(TLocalDeclarationStatementSyntax localDeclarationStat

private async Task<SyntaxNode> PostProcessDocumentAsync(
Document document,
OptionSet options,
SyntaxFormattingOptions options,
SyntaxNode currentRoot,
string diagnosticId,
UnusedValuePreference preference,
Expand Down Expand Up @@ -749,10 +757,10 @@ private async Task<SyntaxNode> PostProcessDocumentAsync(
}

private static async Task<SyntaxNode> PostProcessDocumentCoreAsync(
Func<SyntaxNode, Document, OptionSet, CancellationToken, Task<SyntaxNode>> processMemberDeclarationAsync,
Func<SyntaxNode, Document, SyntaxFormattingOptions, CancellationToken, Task<SyntaxNode>> processMemberDeclarationAsync,
SyntaxNode currentRoot,
Document document,
OptionSet options,
SyntaxFormattingOptions options,
CancellationToken cancellationToken)
{
// Process each member declaration which had at least one diagnostic reported in the original tree and hence
Expand Down Expand Up @@ -780,7 +788,7 @@ private static async Task<SyntaxNode> PostProcessDocumentCoreAsync(
/// This is needed to prevent the code fix/FixAll from generating code with
/// multiple local variables named '_', which is a compiler error.
/// </summary>
private async Task<SyntaxNode> ReplaceDiscardDeclarationsWithAssignmentsAsync(SyntaxNode memberDeclaration, Document document, OptionSet options, CancellationToken cancellationToken)
private async Task<SyntaxNode> ReplaceDiscardDeclarationsWithAssignmentsAsync(SyntaxNode memberDeclaration, Document document, SyntaxFormattingOptions options, CancellationToken cancellationToken)
{
var service = document.GetLanguageService<IReplaceDiscardDeclarationsWithAssignmentsService>();
if (service == null)
Expand All @@ -802,7 +810,7 @@ private async Task<SyntaxNode> ReplaceDiscardDeclarationsWithAssignmentsAsync(Sy
private static async Task<SyntaxNode> AdjustLocalDeclarationsAsync(
SyntaxNode memberDeclaration,
Document document,
OptionSet options,
SyntaxFormattingOptions options,
CancellationToken cancellationToken)
{
var moveDeclarationService = document.GetRequiredLanguageService<IMoveDeclarationNearReferenceService>();
Expand All @@ -826,7 +834,12 @@ private static async Task<SyntaxNode> AdjustLocalDeclarationsAsync(
var rootWithTrackedNodes = root.TrackNodes(originalDeclStatementsToMoveOrRemove);

// Run formatter prior to invoking IMoveDeclarationNearReferenceService.
rootWithTrackedNodes = Formatter.Format(rootWithTrackedNodes, originalDeclStatementsToMoveOrRemove.Select(s => s.Span), document.Project.Solution.Workspace, options, cancellationToken: cancellationToken);
#if CODE_STYLE
var provider = document.Project.Solution.Workspace.Services;
#else
var provider = document.Project.Solution.Workspace.Services;
#endif
rootWithTrackedNodes = Formatter.Format(rootWithTrackedNodes, originalDeclStatementsToMoveOrRemove.Select(s => s.Span), provider, options, rules: null, cancellationToken);

document = document.WithSyntaxRoot(rootWithTrackedNodes);
await OnDocumentUpdatedAsync().ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,15 @@ await FixOneAsync(
// annotation on it.
var rules = new List<AbstractFormattingRule> { GetMultiLineFormattingRule() };

var options = document.Project.AnalyzerOptions.GetAnalyzerOptionSet(root.SyntaxTree, cancellationToken);
var options = SyntaxFormattingOptions.Create(document.Project.AnalyzerOptions.GetAnalyzerOptionSet(root.SyntaxTree, cancellationToken));

#if CODE_STYLE
var formattedRoot = FormatterHelper.Format(changedRoot,
GetSyntaxFormattingService(),
SpecializedFormattingAnnotation,
options,
rules, cancellationToken);
var provider = GetSyntaxFormattingService();
#else
var formattedRoot = Formatter.Format(changedRoot,
SpecializedFormattingAnnotation,
document.Project.Solution.Workspace,
options,
rules, cancellationToken);
var provider = document.Project.Solution.Workspace.Services;
#endif
var formattedRoot = Formatter.Format(changedRoot, SpecializedFormattingAnnotation, provider, options, rules, cancellationToken);

changedRoot = formattedRoot;

editor.ReplaceNode(root, changedRoot);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ Imports System.Composition
Imports System.Diagnostics.CodeAnalysis
Imports Microsoft.CodeAnalysis.CodeFixes
Imports Microsoft.CodeAnalysis.Editing
Imports Microsoft.CodeAnalysis.Formatting
Imports Microsoft.CodeAnalysis.LanguageServices
Imports Microsoft.CodeAnalysis.RemoveUnusedParametersAndValues
Imports Microsoft.CodeAnalysis.VisualBasic.Formatting
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax

Namespace Microsoft.CodeAnalysis.VisualBasic.RemoveUnusedParametersAndValues
Expand All @@ -23,6 +25,12 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.RemoveUnusedParametersAndValues
Public Sub New()
End Sub

#If CODE_STYLE Then
Protected Overrides Function GetSyntaxFormattingService() As ISyntaxFormattingService
Return VisualBasicSyntaxFormattingService.Instance
End Function
#End If

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.
Throw ExceptionUtilities.Unreachable
Expand Down
6 changes: 2 additions & 4 deletions src/CodeStyle/Core/Analyzers/AbstractFormattingAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +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.

#nullable disable

using System.Collections.Immutable;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Formatting;
Expand Down Expand Up @@ -36,8 +34,8 @@ protected sealed override void InitializeWorker(AnalysisContext context)

private void AnalyzeSyntaxTree(SyntaxTreeAnalysisContext context)
{
var analyzerConfigOptions = context.Options.AnalyzerConfigOptionsProvider.GetOptions(context.Tree);
FormattingAnalyzerHelper.AnalyzeSyntaxTree(context, SyntaxFormattingService, Descriptor, analyzerConfigOptions);
var options = SyntaxFormattingOptions.Create(context.Options.AnalyzerConfigOptionsProvider.GetOptions(context.Tree));
FormattingAnalyzerHelper.AnalyzeSyntaxTree(context, SyntaxFormattingService, Descriptor, options);
}
}
}
Loading

0 comments on commit 2a8ed8e

Please sign in to comment.