Skip to content

Commit

Permalink
SyntaxFormattingOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat committed Jul 1, 2024
1 parent 49c0e8f commit 23e4d4a
Show file tree
Hide file tree
Showing 54 changed files with 78 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ internal static class CSharpCollectionExpressionRewriter
#if CODE_STYLE
var formattingOptions = CSharpSyntaxFormattingOptions.Default;
#else
var formattingOptions = (CSharpSyntaxFormattingOptions)await workspaceDocument.GetSyntaxFormattingOptionsAsync(
fallbackOptions, cancellationToken).ConfigureAwait(false);
var formattingOptions = (CSharpSyntaxFormattingOptions)await workspaceDocument.GetSyntaxFormattingOptionsAsync(cancellationToken).ConfigureAwait(false);
#endif

var indentationOptions = new IndentationOptions(formattingOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ internal partial class CSharpUseCollectionExpressionForFluentCodeFixProvider()
#if CODE_STYLE
var formattingOptions = SyntaxFormattingOptions.CommonDefaults;
#else
var formattingOptions = await document.GetSyntaxFormattingOptionsAsync(
fallbackOptions, cancellationToken).ConfigureAwait(false);
var formattingOptions = await document.GetSyntaxFormattingOptionsAsync(cancellationToken).ConfigureAwait(false);
#endif

using var _ = ArrayBuilder<SyntaxNodeOrToken>.GetInstance(out var nodesAndTokens);
Expand Down
2 changes: 1 addition & 1 deletion src/Analyzers/Core/Analyzers/AnalyzerOptionsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public SimplifierOptions GetSimplifierOptions(ISimplification simplification)
// SyntaxFormattingOptions

public SyntaxFormattingOptions GetSyntaxFormattingOptions(ISyntaxFormatting formatting)
=> formatting.GetFormattingOptions(_options, _fallbackOptions.CleanupOptions?.FormattingOptions);
=> formatting.GetFormattingOptions(_options);

// CodeGenerationOptions

Expand Down
9 changes: 4 additions & 5 deletions src/EditorFeatures/Core/Options/TextBufferOptionProviders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@ private static LineFormattingOptions GetLineFormattingOptionsImpl(ITextBuffer te
}

public static SyntaxFormattingOptions GetSyntaxFormattingOptions(this ITextBuffer textBuffer, EditorOptionsService optionsProvider, LanguageServices languageServices, bool explicitFormat)
=> GetSyntaxFormattingOptionsImpl(textBuffer, optionsProvider.Factory.GetOptions(textBuffer), optionsProvider.IndentationManager, optionsProvider.GlobalOptions, languageServices, explicitFormat);
=> GetSyntaxFormattingOptionsImpl(textBuffer, optionsProvider.Factory.GetOptions(textBuffer), optionsProvider.IndentationManager, languageServices, explicitFormat);

private static SyntaxFormattingOptions GetSyntaxFormattingOptionsImpl(ITextBuffer textBuffer, IEditorOptions editorOptions, IIndentationManagerService indentationManager, IGlobalOptionService globalOptions, LanguageServices languageServices, bool explicitFormat)
private static SyntaxFormattingOptions GetSyntaxFormattingOptionsImpl(ITextBuffer textBuffer, IEditorOptions editorOptions, IIndentationManagerService indentationManager, LanguageServices languageServices, bool explicitFormat)
{
var configOptions = editorOptions.ToAnalyzerConfigOptions();
var fallbackOptions = globalOptions.GetSyntaxFormattingOptions(languageServices);
var options = configOptions.GetSyntaxFormattingOptions(languageServices, fallbackOptions);
var options = configOptions.GetSyntaxFormattingOptions(languageServices);
var lineFormattingOptions = GetLineFormattingOptionsImpl(textBuffer, editorOptions, indentationManager, explicitFormat);

return options with { LineFormatting = lineFormattingOptions };
Expand All @@ -56,7 +55,7 @@ private static SyntaxFormattingOptions GetSyntaxFormattingOptionsImpl(ITextBuffe
public static IndentationOptions GetIndentationOptions(this ITextBuffer textBuffer, EditorOptionsService optionsProvider, LanguageServices languageServices, bool explicitFormat)
{
var editorOptions = optionsProvider.Factory.GetOptions(textBuffer);
var formattingOptions = GetSyntaxFormattingOptionsImpl(textBuffer, editorOptions, optionsProvider.IndentationManager, optionsProvider.GlobalOptions, languageServices, explicitFormat);
var formattingOptions = GetSyntaxFormattingOptionsImpl(textBuffer, editorOptions, optionsProvider.IndentationManager, languageServices, explicitFormat);

return new IndentationOptions(formattingOptions)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public bool ExecuteCommand(SortAndRemoveUnnecessaryImportsCommandArgs args, Comm
async (document, cancellationToken) =>
{
var formattingOptions = document.SupportsSyntaxTree
? await document.GetSyntaxFormattingOptionsAsync(_globalOptions, cancellationToken).ConfigureAwait(false)
? await document.GetSyntaxFormattingOptionsAsync(cancellationToken).ConfigureAwait(false)
: null;
var removeImportsService = document.GetRequiredLanguageService<IRemoveUnnecessaryImportsService>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ private protected async Task AssertFormatAsync(string expected, string code, IEn
var formattingService = document.GetRequiredLanguageService<ISyntaxFormattingService>();

var formattingOptions = (options != null)
? formattingService.GetFormattingOptions(options, fallbackOptions: null)
? formattingService.GetFormattingOptions(options)
: formattingService.DefaultOptions;

ImmutableArray<AbstractFormattingRule> rules = [formattingRuleProvider.CreateRule(documentSyntax, 0), .. Formatter.GetDefaultFormattingRules(document)];
Expand Down Expand Up @@ -286,7 +286,7 @@ protected static void AssertFormatOnArbitraryNode(SyntaxNode node, string expect
{
using var workspace = new AdhocWorkspace();
var formattingService = workspace.Services.GetLanguageServices(node.Language).GetRequiredService<ISyntaxFormattingService>();
var options = formattingService.GetFormattingOptions(StructuredAnalyzerConfigOptions.Empty, fallbackOptions: null);
var options = formattingService.GetFormattingOptions(StructuredAnalyzerConfigOptions.Empty);
var result = Formatter.Format(node, workspace.Services.SolutionServices, options, CancellationToken.None);
var actual = result.GetText().ToString();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.NavigationBar
End If

Dim simplifierOptions = Await newDocument.GetSimplifierOptionsAsync(cancellationToken).ConfigureAwait(False)
Dim formattingOptions = Await newDocument.GetSyntaxFormattingOptionsAsync(globalOptions, cancellationToken).ConfigureAwait(False)
Dim formattingOptions = Await newDocument.GetSyntaxFormattingOptionsAsync(cancellationToken).ConfigureAwait(False)

newDocument = Await Simplifier.ReduceAsync(newDocument, Simplifier.Annotation, simplifierOptions, cancellationToken).ConfigureAwait(False)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
return;

var options = context.Options;
var formattingOptions = await document.GetSyntaxFormattingOptionsAsync(options, cancellationToken).ConfigureAwait(false);
var formattingOptions = await document.GetSyntaxFormattingOptionsAsync(cancellationToken).ConfigureAwait(false);
var parsedDocument = await ParsedDocument.CreateAsync(document, cancellationToken).ConfigureAwait(false);

if (!CanConvert(parsedDocument, parentExpression, formattingOptions, out var convertParams, out var provider, cancellationToken))
Expand All @@ -90,7 +90,7 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
context.RegisterRefactoring(
CodeAction.Create(
CSharpFeaturesResources.Convert_to_raw_string,
cancellationToken => UpdateDocumentAsync(document, parentExpression, ConvertToRawKind.SingleLine, options, provider, cancellationToken),
cancellationToken => UpdateDocumentAsync(document, parentExpression, ConvertToRawKind.SingleLine, provider, cancellationToken),
s_kindToEquivalenceKeyMap[ConvertToRawKind.SingleLine],
priority),
token.Span);
Expand All @@ -100,7 +100,7 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
context.RegisterRefactoring(
CodeAction.Create(
CSharpFeaturesResources.Convert_to_raw_string,
cancellationToken => UpdateDocumentAsync(document, parentExpression, ConvertToRawKind.MultiLineIndented, options, provider, cancellationToken),
cancellationToken => UpdateDocumentAsync(document, parentExpression, ConvertToRawKind.MultiLineIndented, provider, cancellationToken),
s_kindToEquivalenceKeyMap[ConvertToRawKind.MultiLineIndented],
priority),
token.Span);
Expand All @@ -110,7 +110,7 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
context.RegisterRefactoring(
CodeAction.Create(
CSharpFeaturesResources.without_leading_whitespace_may_change_semantics,
cancellationToken => UpdateDocumentAsync(document, parentExpression, ConvertToRawKind.MultiLineWithoutLeadingWhitespace, options, provider, cancellationToken),
cancellationToken => UpdateDocumentAsync(document, parentExpression, ConvertToRawKind.MultiLineWithoutLeadingWhitespace, provider, cancellationToken),
s_kindToEquivalenceKeyMap[ConvertToRawKind.MultiLineWithoutLeadingWhitespace],
priority),
token.Span);
Expand All @@ -122,11 +122,10 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
Document document,
ExpressionSyntax expression,
ConvertToRawKind kind,
CodeActionOptionsProvider optionsProvider,
IConvertStringProvider provider,
CancellationToken cancellationToken)
{
var options = await document.GetSyntaxFormattingOptionsAsync(optionsProvider, cancellationToken).ConfigureAwait(false);
var options = await document.GetSyntaxFormattingOptionsAsync(cancellationToken).ConfigureAwait(false);
var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

var parsedDocument = await ParsedDocument.CreateAsync(document, cancellationToken).ConfigureAwait(false);
Expand All @@ -146,7 +145,7 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
Debug.Assert(equivalenceKey != null);
var kind = s_kindToEquivalenceKeyMap[equivalenceKey];

var formattingOptions = await document.GetSyntaxFormattingOptionsAsync(optionsProvider, cancellationToken).ConfigureAwait(false);
var formattingOptions = await document.GetSyntaxFormattingOptionsAsync(cancellationToken).ConfigureAwait(false);
var parsedDocument = await ParsedDocument.CreateAsync(document, cancellationToken).ConfigureAwait(false);

foreach (var fixSpan in fixAllSpans)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
if (parameterNameParts.BaseName == "")
return;

var formattingOptions = await document.GetSyntaxFormattingOptionsAsync(fallbackOptions, cancellationToken).ConfigureAwait(false);
var formattingOptions = await document.GetSyntaxFormattingOptionsAsync(cancellationToken).ConfigureAwait(false);

var fieldOrProperty = TryFindMatchingUninitializedFieldOrPropertySymbol();
var refactorings = fieldOrProperty == null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ protected override async Task<Document> AddIndentationToDocumentAsync(Document d
{
var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

var syntaxFormattingOptions = await document.GetSyntaxFormattingOptionsAsync(fallbackOptions: null, cancellationToken).ConfigureAwait(false);
var syntaxFormattingOptions = await document.GetSyntaxFormattingOptionsAsync(cancellationToken).ConfigureAwait(false);
var indentationString = CSharpSnippetHelpers.GetBlockLikeIndentationString(document, typeDeclaration.OpenBraceToken.SpanStart, syntaxFormattingOptions, cancellationToken);

var newTypeDeclaration = typeDeclaration.WithCloseBraceToken(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected override async Task<Document> AddIndentationToDocumentAsync(Document d
var body = methodDeclaration.Body!;
var returnStatement = body.Statements.First();

var syntaxFormattingOptions = await document.GetSyntaxFormattingOptionsAsync(fallbackOptions: null, cancellationToken).ConfigureAwait(false);
var syntaxFormattingOptions = await document.GetSyntaxFormattingOptionsAsync(cancellationToken).ConfigureAwait(false);
var indentationString = CSharpSnippetHelpers.GetBlockLikeIndentationString(document, body.OpenBraceToken.SpanStart, syntaxFormattingOptions, cancellationToken);

var updatedReturnStatement = returnStatement.WithPrependedLeadingTrivia(SyntaxFactory.SyntaxTrivia(SyntaxKind.WhitespaceTrivia, indentationString));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static string GetBlockLikeIndentationString(Document document, int startP
var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var block = getBlock(targetNode);

var syntaxFormattingOptions = await document.GetSyntaxFormattingOptionsAsync(fallbackOptions: null, cancellationToken).ConfigureAwait(false);
var syntaxFormattingOptions = await document.GetSyntaxFormattingOptionsAsync(cancellationToken).ConfigureAwait(false);
var indentationString = GetBlockLikeIndentationString(document, block.SpanStart, syntaxFormattingOptions, cancellationToken);

var updatedBlock = block.WithCloseBraceToken(block.CloseBraceToken.WithPrependedLeadingTrivia(SyntaxFactory.SyntaxTrivia(SyntaxKind.WhitespaceTrivia, indentationString)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ async Task<ChangeSignatureResult> GetChangeSignatureResultAsync(ChangeSignatureA
});

var annotatedNodes = newRoot.GetAnnotatedNodes<SyntaxNode>(syntaxAnnotation: changeSignatureFormattingAnnotation);
var formattingOptions = await doc.GetSyntaxFormattingOptionsAsync(context.FallbackOptions, cancellationToken).ConfigureAwait(false);
var formattingOptions = await doc.GetSyntaxFormattingOptionsAsync(cancellationToken).ConfigureAwait(false);

var formattedRoot = Formatter.Format(
newRoot,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,19 @@ internal abstract partial class AbstractSuppressionCodeFixProvider : IConfigurat
internal sealed class GlobalSuppressMessageCodeAction(
ISymbol targetSymbol, INamedTypeSymbol suppressMessageAttribute,
Project project, Diagnostic diagnostic,
AbstractSuppressionCodeFixProvider fixer,
CodeActionOptionsProvider fallbackOptions) : AbstractGlobalSuppressMessageCodeAction(fixer, project)
AbstractSuppressionCodeFixProvider fixer) : AbstractGlobalSuppressMessageCodeAction(fixer, project)
{
private readonly ISymbol _targetSymbol = targetSymbol;
private readonly INamedTypeSymbol _suppressMessageAttribute = suppressMessageAttribute;
private readonly Diagnostic _diagnostic = diagnostic;
private readonly CodeActionOptionsProvider _fallbackOptions = fallbackOptions;

protected override async Task<Document> GetChangedSuppressionDocumentAsync(CancellationToken cancellationToken)
{
var suppressionsDoc = await GetOrCreateSuppressionsDocumentAsync(cancellationToken).ConfigureAwait(false);
var services = suppressionsDoc.Project.Solution.Services;
var suppressionsRoot = await suppressionsDoc.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var addImportsService = suppressionsDoc.GetRequiredLanguageService<IAddImportsService>();
var options = await suppressionsDoc.GetSyntaxFormattingOptionsAsync(_fallbackOptions, cancellationToken).ConfigureAwait(false);
var options = await suppressionsDoc.GetSyntaxFormattingOptionsAsync(cancellationToken).ConfigureAwait(false);

suppressionsRoot = Fixer.AddGlobalSuppressMessageAttribute(
suppressionsRoot, _targetSymbol, _suppressMessageAttribute, _diagnostic, services, options, addImportsService, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ internal abstract partial class RemoveSuppressionCodeAction : AbstractSuppressio
Project project,
Diagnostic diagnostic,
AbstractSuppressionCodeFixProvider fixer,
CodeActionOptionsProvider options,
CancellationToken cancellationToken)
{
var compilation = await project.GetCompilationAsync(cancellationToken).ConfigureAwait(false);
Expand All @@ -38,7 +37,7 @@ internal abstract partial class RemoveSuppressionCodeAction : AbstractSuppressio
}
else if (documentOpt != null && !SuppressionHelpers.IsSynthesizedExternalSourceDiagnostic(diagnostic))
{
var formattingOptions = await documentOpt.GetSyntaxFormattingOptionsAsync(options, cancellationToken).ConfigureAwait(false);
var formattingOptions = await documentOpt.GetSyntaxFormattingOptionsAsync(cancellationToken).ConfigureAwait(false);
return PragmaRemoveAction.Create(suppressionTargetInfo, documentOpt, formattingOptions, diagnostic, fixer);
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ internal async Task<ImmutableArray<PragmaWarningCodeAction>> GetPragmaSuppressio
if (diagnostic.Location.IsInSource && documentOpt != null)
{
// pragma warning disable.
lazyFormattingOptions ??= await documentOpt.GetSyntaxFormattingOptionsAsync(fallbackOptions, cancellationToken).ConfigureAwait(false);
lazyFormattingOptions ??= await documentOpt.GetSyntaxFormattingOptionsAsync(cancellationToken).ConfigureAwait(false);
nestedActions.Add(PragmaWarningCodeAction.Create(suppressionTargetInfo, documentOpt, lazyFormattingOptions, diagnostic, this));
}

Expand All @@ -221,7 +221,7 @@ internal async Task<ImmutableArray<PragmaWarningCodeAction>> GetPragmaSuppressio
{
// global assembly-level suppress message attribute.
nestedActions.Add(new GlobalSuppressMessageCodeAction(
suppressionTargetInfo.TargetSymbol, suppressMessageAttribute, project, diagnostic, this, fallbackOptions));
suppressionTargetInfo.TargetSymbol, suppressMessageAttribute, project, diagnostic, this));

// local suppress message attribute
// please note that in order to avoid issues with existing unit tests referencing the code fix
Expand All @@ -242,7 +242,7 @@ internal async Task<ImmutableArray<PragmaWarningCodeAction>> GetPragmaSuppressio
}
else if (!skipUnsuppress)
{
var codeAction = await RemoveSuppressionCodeAction.CreateAsync(suppressionTargetInfo, documentOpt, project, diagnostic, this, fallbackOptions, cancellationToken).ConfigureAwait(false);
var codeAction = await RemoveSuppressionCodeAction.CreateAsync(suppressionTargetInfo, documentOpt, project, diagnostic, this, cancellationToken).ConfigureAwait(false);
if (codeAction != null)
{
result.Add(new CodeFix(project, codeAction, diagnostic));
Expand Down
Loading

0 comments on commit 23e4d4a

Please sign in to comment.