Skip to content

Commit

Permalink
Syntax formatting options (#74223)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat committed Jul 8, 2024
1 parent 87faf90 commit f4fada1
Show file tree
Hide file tree
Showing 92 changed files with 210 additions and 237 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
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
using Microsoft.VisualStudio.Utilities;
using Roslyn.Utilities;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Diagnostics;

namespace Microsoft.CodeAnalysis.Editor.CSharp.AutomaticCompletion;

Expand Down Expand Up @@ -313,9 +314,10 @@ protected override void ModifySelectedNode(
SyntaxNode selectedNode,
bool addBrace,
int caretPosition,
StructuredAnalyzerConfigOptions fallbackOptions,
CancellationToken cancellationToken)
{
var formattingOptions = args.SubjectBuffer.GetSyntaxFormattingOptions(EditorOptionsService, document.LanguageServices, explicitFormat: false);
var formattingOptions = args.SubjectBuffer.GetSyntaxFormattingOptions(EditorOptionsService, fallbackOptions, document.LanguageServices, explicitFormat: false);

// Add braces for the selected node
if (addBrace)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public void ExecuteCommand(TypeCharCommandArgs args, Action nextCommandHandler,
if (!ConvertNamespaceAnalysis.CanOfferUseFileScoped(s_fileScopedNamespacePreferenceOption, (CompilationUnitSyntax)parsedDocument.Root, namespaceDecl, forAnalyzer: true, LanguageVersion.CSharp10))
return default;

var formattingOptions = subjectBuffer.GetSyntaxFormattingOptions(_editorOptionsService, document.Project.Services, explicitFormat: false);
var formattingOptions = subjectBuffer.GetSyntaxFormattingOptions(_editorOptionsService, document.Project.GetFallbackAnalyzerOptions(), document.Project.Services, explicitFormat: false);
return ConvertNamespaceTransform.ConvertNamespaceDeclaration(parsedDocument, namespaceDecl, formattingOptions, cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public Task<ImmutableArray<TextChange>> GetFormattingChangesAsync(
CancellationToken cancellationToken)
{
var parsedDocument = ParsedDocument.CreateSynchronously(document, cancellationToken);
var options = textBuffer.GetSyntaxFormattingOptions(_editorOptionsService, parsedDocument.LanguageServices, explicitFormat: true);
var options = textBuffer.GetSyntaxFormattingOptions(_editorOptionsService, document.Project.GetFallbackAnalyzerOptions(), parsedDocument.LanguageServices, explicitFormat: true);

var span = textSpan ?? new TextSpan(0, parsedDocument.Root.FullSpan.Length);
var formattingSpan = CommonFormattingHelpers.GetFormattingSpan(parsedDocument.Root, span);
Expand All @@ -97,7 +97,7 @@ public Task<ImmutableArray<TextChange>> GetFormattingChangesAsync(
public Task<ImmutableArray<TextChange>> GetFormattingChangesOnPasteAsync(Document document, ITextBuffer textBuffer, TextSpan textSpan, CancellationToken cancellationToken)
{
var parsedDocument = ParsedDocument.CreateSynchronously(document, cancellationToken);
var options = textBuffer.GetSyntaxFormattingOptions(_editorOptionsService, parsedDocument.LanguageServices, explicitFormat: true);
var options = textBuffer.GetSyntaxFormattingOptions(_editorOptionsService, document.Project.GetFallbackAnalyzerOptions(), parsedDocument.LanguageServices, explicitFormat: true);
var service = parsedDocument.LanguageServices.GetRequiredService<ISyntaxFormattingService>();
return Task.FromResult(service.GetFormattingChangesOnPaste(parsedDocument, textSpan, options, cancellationToken));
}
Expand All @@ -112,7 +112,7 @@ public Task<ImmutableArray<TextChange>> GetFormattingChangesAsync(Document docum

if (service.ShouldFormatOnTypedCharacter(parsedDocument, typedChar, position, cancellationToken))
{
var indentationOptions = textBuffer.GetIndentationOptions(_editorOptionsService, parsedDocument.LanguageServices, explicitFormat: false);
var indentationOptions = textBuffer.GetIndentationOptions(_editorOptionsService, document.Project.GetFallbackAnalyzerOptions(), parsedDocument.LanguageServices, explicitFormat: false);
return Task.FromResult(service.GetFormattingChangesOnTypedCharacter(parsedDocument, position, indentationOptions, cancellationToken));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ SyntaxKind.InterpolatedSingleLineRawStringStartToken or
return false;
}

var indentationOptions = subjectBuffer.GetIndentationOptions(_editorOptionsService, document.Project.Services, explicitFormat: false);
var indentationOptions = subjectBuffer.GetIndentationOptions(_editorOptionsService, document.Project.GetFallbackAnalyzerOptions(), document.Project.Services, explicitFormat: false);
var indentation = token.GetPreferredIndentation(parsedDocument, indentationOptions, cancellationToken);

var newLine = indentationOptions.FormattingOptions.NewLine;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public bool ExecuteCommandWorker(ReturnKeyCommandArgs args, CancellationToken ca
return false;

var parsedDocument = ParsedDocument.CreateSynchronously(document, CancellationToken.None);
var indentationOptions = subjectBuffer.GetIndentationOptions(_editorOptionsService, parsedDocument.LanguageServices, explicitFormat: false);
var indentationOptions = subjectBuffer.GetIndentationOptions(_editorOptionsService, document.Project.GetFallbackAnalyzerOptions(), parsedDocument.LanguageServices, explicitFormat: false);

// We now go through the verified string literals and split each of them.
// The list of spans is traversed in reverse order so we do not have to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.Editor.StringCopyPaste;
Expand Down Expand Up @@ -186,7 +187,7 @@ ImmutableArray<TextChange> GetEdits(CancellationToken cancellationToken)
{
var newLine = textView.Options.GetNewLineCharacter();
var indentationWhitespace = DetermineIndentationWhitespace(
parsedDocumentBeforePaste, subjectBuffer, snapshotBeforePaste.AsText(), stringExpressionBeforePaste, cancellationToken);
parsedDocumentBeforePaste, subjectBuffer, snapshotBeforePaste.AsText(), stringExpressionBeforePaste, documentBeforePaste.Project.GetFallbackAnalyzerOptions(), cancellationToken);

// See if this is a paste of the last copy that we heard about.
var edits = TryGetEditsFromKnownCopySource(newLine, indentationWhitespace);
Expand Down Expand Up @@ -235,6 +236,7 @@ private string DetermineIndentationWhitespace(
ITextBuffer textBuffer,
SourceText textBeforePaste,
ExpressionSyntax stringExpressionBeforePaste,
StructuredAnalyzerConfigOptions fallbackOptions,
CancellationToken cancellationToken)
{
// Only raw strings care about indentation. Don't bother computing if we don't need it.
Expand All @@ -252,7 +254,7 @@ private string DetermineIndentationWhitespace(

// Otherwise, we have a single-line raw string. Determine the default indentation desired here.
// We'll use that if we have to convert this single-line raw string to a multi-line one.
var indentationOptions = textBuffer.GetIndentationOptions(_editorOptionsService, documentBeforePaste.LanguageServices, explicitFormat: false);
var indentationOptions = textBuffer.GetIndentationOptions(_editorOptionsService, fallbackOptions, documentBeforePaste.LanguageServices, explicitFormat: false);
return stringExpressionBeforePaste.GetFirstToken().GetPreferredIndentation(documentBeforePaste, indentationOptions, cancellationToken);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.Threading;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Options;
Expand Down Expand Up @@ -61,7 +62,7 @@ protected AbstractAutomaticLineEnderCommandHandler(
/// <summary>
/// Add or remove the braces for <param name="selectedNode"/>.
/// </summary>
protected abstract void ModifySelectedNode(AutomaticLineEnderCommandArgs args, ParsedDocument document, SyntaxNode selectedNode, bool addBrace, int caretPosition, CancellationToken cancellationToken);
protected abstract void ModifySelectedNode(AutomaticLineEnderCommandArgs args, ParsedDocument document, SyntaxNode selectedNode, bool addBrace, int caretPosition, StructuredAnalyzerConfigOptions fallbackOptions, CancellationToken cancellationToken);

/// <summary>
/// Get the syntax node needs add/remove braces.
Expand Down Expand Up @@ -131,7 +132,7 @@ public void ExecuteCommand(AutomaticLineEnderCommandArgs args, Action nextHandle
{
var (selectedNode, addBrace) = selectNodeAndOperationKind.Value;
using var transaction = args.TextView.CreateEditTransaction(EditorFeaturesResources.Automatic_Line_Ender, _undoRegistry, _editorOperationsFactoryService);
ModifySelectedNode(args, parsedDocument, selectedNode, addBrace, caretPosition, cancellationToken);
ModifySelectedNode(args, parsedDocument, selectedNode, addBrace, caretPosition, document.Project.GetFallbackAnalyzerOptions(), cancellationToken);
NextAction(operations, nextHandler);
transaction.Complete();
return;
Expand All @@ -142,7 +143,7 @@ public void ExecuteCommand(AutomaticLineEnderCommandArgs args, Action nextHandle
if (endingInsertionPosition != null)
{
using var transaction = args.TextView.CreateEditTransaction(EditorFeaturesResources.Automatic_Line_Ender, _undoRegistry, _editorOperationsFactoryService);
var formattingOptions = args.SubjectBuffer.GetSyntaxFormattingOptions(EditorOptionsService, parsedDocument.LanguageServices, explicitFormat: false);
var formattingOptions = args.SubjectBuffer.GetSyntaxFormattingOptions(EditorOptionsService, document.Project.GetFallbackAnalyzerOptions(), parsedDocument.LanguageServices, explicitFormat: false);
InsertEnding(args.TextView, args.SubjectBuffer, parsedDocument, endingInsertionPosition.Value, caretPosition, formattingOptions, cancellationToken);
NextAction(operations, nextHandler);
transaction.Complete();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Diagnostics;
using System.Threading;
using Microsoft.CodeAnalysis.BraceCompletion;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.ErrorReporting;
Expand Down Expand Up @@ -105,7 +106,7 @@ private bool TryStart(CancellationToken cancellationToken)
}

var parsedDocument = ParsedDocument.CreateSynchronously(document, cancellationToken);
var context = GetBraceCompletionContext(parsedDocument);
var context = GetBraceCompletionContext(parsedDocument, document.Project.GetFallbackAnalyzerOptions());

// Note: completes synchronously unless Semantic Model is needed to determine the result:
if (!_service.HasBraceCompletionAsync(context, document, cancellationToken).WaitAndGetResult(cancellationToken))
Expand All @@ -125,7 +126,7 @@ private bool TryStart(CancellationToken cancellationToken)

if (TryGetBraceCompletionContext(out var contextAfterStart, cancellationToken))
{
var indentationOptions = SubjectBuffer.GetIndentationOptions(_editorOptionsService, contextAfterStart.Document.LanguageServices, explicitFormat: false);
var indentationOptions = SubjectBuffer.GetIndentationOptions(_editorOptionsService, document.Project.GetFallbackAnalyzerOptions(), contextAfterStart.Document.LanguageServices, explicitFormat: false);
var changesAfterStart = _service.GetTextChangesAfterCompletion(contextAfterStart, indentationOptions, cancellationToken);
if (changesAfterStart != null)
{
Expand Down Expand Up @@ -283,7 +284,7 @@ public void PostReturn()
return;
}

var indentationOptions = SubjectBuffer.GetIndentationOptions(_editorOptionsService, context.Document.LanguageServices, explicitFormat: false);
var indentationOptions = SubjectBuffer.GetIndentationOptions(_editorOptionsService, context.FallbackOptions, context.Document.LanguageServices, explicitFormat: false);
var changesAfterReturn = _service.GetTextChangeAfterReturn(context, indentationOptions, CancellationToken.None);
if (changesAfterReturn != null)
{
Expand Down Expand Up @@ -397,11 +398,11 @@ private bool TryGetBraceCompletionContext(out BraceCompletionContext context, Ca
return false;
}

context = GetBraceCompletionContext(ParsedDocument.CreateSynchronously(document, cancellationToken));
context = GetBraceCompletionContext(ParsedDocument.CreateSynchronously(document, cancellationToken), document.Project.GetFallbackAnalyzerOptions());
return true;
}

private BraceCompletionContext GetBraceCompletionContext(ParsedDocument document)
private BraceCompletionContext GetBraceCompletionContext(ParsedDocument document, StructuredAnalyzerConfigOptions fallbackOptions)
{
_threadingContext.ThrowIfNotOnUIThread();
var snapshot = SubjectBuffer.CurrentSnapshot;
Expand All @@ -411,7 +412,7 @@ private BraceCompletionContext GetBraceCompletionContext(ParsedDocument document
// The user is actively typing so the caret position should not be null.
var caretPosition = this.GetCaretPosition().Value.Position;

return new BraceCompletionContext(document, openingSnapshotPoint, closingSnapshotPoint, caretPosition);
return new BraceCompletionContext(document, fallbackOptions, openingSnapshotPoint, closingSnapshotPoint, caretPosition);
}

private void ApplyBraceCompletionResult(BraceCompletionResult result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private void ApplyEdits(Document document, ITextView textView, ITextBuffer subje
var oldSyntaxTree = document.GetSyntaxTreeSynchronously(cancellationToken);
var newRoot = oldSyntaxTree.WithChangedText(newText).GetRoot(cancellationToken);

var formattingOptions = subjectBuffer.GetSyntaxFormattingOptions(_editorOptionsService, document.Project.Services, explicitFormat: false);
var formattingOptions = subjectBuffer.GetSyntaxFormattingOptions(_editorOptionsService, document.Project.GetFallbackAnalyzerOptions(), document.Project.Services, explicitFormat: false);
var formattingSpans = trackingSnapshotSpans.Select(change => CommonFormattingHelpers.GetFormattingSpan(newRoot, change.Span.ToTextSpan()));
var formattedChanges = Formatter.GetFormattedTextChanges(newRoot, formattingSpans, document.Project.Solution.Services, formattingOptions, rules: default, cancellationToken);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ private static bool IsLowercase(string str)

internal static partial class EditorOptionsExtensions
{
public static StructuredAnalyzerConfigOptions ToAnalyzerConfigOptions(this IEditorOptions editorOptions)
=> StructuredAnalyzerConfigOptions.Create(new EditorAnalyzerConfigOptions(editorOptions));
public static StructuredAnalyzerConfigOptions ToAnalyzerConfigOptions(this IEditorOptions editorOptions, StructuredAnalyzerConfigOptions fallbackOptions)
=> StructuredAnalyzerConfigOptions.Create(new EditorAnalyzerConfigOptions(editorOptions), fallbackOptions);
}
Loading

0 comments on commit f4fada1

Please sign in to comment.