Skip to content

Commit

Permalink
SyntaxFormattingOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat committed Jul 2, 2024
1 parent 863962d commit 8a1397c
Show file tree
Hide file tree
Showing 63 changed files with 104 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ public static async Task<CollectionExpressionSyntax> CreateCollectionExpressionA
#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 @@ static async Task<SeparatedSyntaxList<ArgumentSyntax>> GetArgumentsAsync(
#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 @@ private static async Task<Document> UpdateDocumentAsync(
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 @@ protected override async Task FixAllAsync(
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 async Task<Document> AddBlockIndentationToDocumentAsync<TTargetNod
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 @@ private static async Task<ImmutableArray<ReferencedSymbol>> FindChangeSignatureR
});

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 @@ -53,10 +53,10 @@ public bool IsFixableDiagnostic(Diagnostic diagnostic)
public FixAllProvider? GetFixAllProvider()
=> null;

public Task<ImmutableArray<CodeFix>> GetFixesAsync(TextDocument document, TextSpan span, IEnumerable<Diagnostic> diagnostics, CodeActionOptionsProvider fallbackOptions, CancellationToken cancellationToken)
public Task<ImmutableArray<CodeFix>> GetFixesAsync(TextDocument document, TextSpan span, IEnumerable<Diagnostic> diagnostics, CancellationToken cancellationToken)
=> Task.FromResult(GetConfigurations(document.Project, diagnostics));

public Task<ImmutableArray<CodeFix>> GetFixesAsync(Project project, IEnumerable<Diagnostic> diagnostics, CodeActionOptionsProvider fallbackOptions, CancellationToken cancellationToken)
public Task<ImmutableArray<CodeFix>> GetFixesAsync(Project project, IEnumerable<Diagnostic> diagnostics, CancellationToken cancellationToken)
=> Task.FromResult(GetConfigurations(project, diagnostics));

private static ImmutableArray<CodeFix> GetConfigurations(Project project, IEnumerable<Diagnostic> diagnostics)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ public bool IsFixableDiagnostic(Diagnostic diagnostic)
public FixAllProvider? GetFixAllProvider()
=> null;

public Task<ImmutableArray<CodeFix>> GetFixesAsync(TextDocument document, TextSpan span, IEnumerable<Diagnostic> diagnostics, CodeActionOptionsProvider fallbackOptions, CancellationToken cancellationToken)
public Task<ImmutableArray<CodeFix>> GetFixesAsync(TextDocument document, TextSpan span, IEnumerable<Diagnostic> diagnostics, CancellationToken cancellationToken)
=> Task.FromResult(GetConfigurations(document.Project, diagnostics, cancellationToken));

public Task<ImmutableArray<CodeFix>> GetFixesAsync(Project project, IEnumerable<Diagnostic> diagnostics, CodeActionOptionsProvider fallbackOptions, CancellationToken cancellationToken)
public Task<ImmutableArray<CodeFix>> GetFixesAsync(Project project, IEnumerable<Diagnostic> diagnostics, CancellationToken cancellationToken)
=> Task.FromResult(GetConfigurations(project, diagnostics, cancellationToken));

private static ImmutableArray<CodeFix> GetConfigurations(Project project, IEnumerable<Diagnostic> diagnostics, CancellationToken cancellationToken)
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 @@ -86,7 +86,7 @@ private static async Task<Document> BatchPragmaFixesAsync(
properties: diagnostic.Properties,
isSuppressed: diagnostic.IsSuppressed);

var newSuppressionFixes = await suppressionFixProvider.GetFixesAsync(currentDocument, currentDiagnosticSpan, [diagnostic], fallbackOptions, cancellationToken).ConfigureAwait(false);
var newSuppressionFixes = await suppressionFixProvider.GetFixesAsync(currentDocument, currentDiagnosticSpan, fallbackOptions, cancellationToken).ConfigureAwait(false);

Check failure on line 89 in src/Features/Core/Portable/CodeFixes/Suppression/AbstractSuppressionCodeFixProvider.PragmaBatchFixHelpers.cs

View check run for this annotation

Azure Pipelines / roslyn-CI (Correctness Correctness_Analyzers)

src/Features/Core/Portable/CodeFixes/Suppression/AbstractSuppressionCodeFixProvider.PragmaBatchFixHelpers.cs#L89

src/Features/Core/Portable/CodeFixes/Suppression/AbstractSuppressionCodeFixProvider.PragmaBatchFixHelpers.cs(89,126): error CS1503: (NETCORE_ENGINEERING_TELEMETRY=Build) Argument 3: cannot convert from 'Microsoft.CodeAnalysis.CodeActions.CodeActionOptionsProvider' to 'System.Collections.Generic.IEnumerable<Microsoft.CodeAnalysis.Diagnostic>'
var newSuppressionFix = newSuppressionFixes.SingleOrDefault();
if (newSuppressionFix != null)
{
Expand Down
Loading

0 comments on commit 8a1397c

Please sign in to comment.