Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format list patterns #57568

Merged
merged 9 commits into from
Feb 18, 2022
Prev Previous commit
Next Next commit
Merge remote-tracking branch 'upstream/main' into FormatListPatterns
  • Loading branch information
davidwengier committed Feb 17, 2022
commit c1851c38876380fb844df448dd42809391c011f6
Original file line number Diff line number Diff line change
@@ -28,25 +28,28 @@ internal abstract class AbstractCurlyBraceOrBracketCompletionService : AbstractB
/// </summary>
private static readonly SyntaxAnnotation s_closingBraceSyntaxAnnotation = new(nameof(s_closingBraceSyntaxAnnotation));

protected abstract ImmutableArray<AbstractFormattingRule> GetBraceFormattingIndentationRulesAfterReturn(DocumentOptionSet documentOptions);
protected abstract ImmutableArray<AbstractFormattingRule> GetBraceFormattingIndentationRulesAfterReturn(IndentationOptions options);

public override async Task<BraceCompletionResult?> GetTextChangesAfterCompletionAsync(BraceCompletionContext braceCompletionContext, CancellationToken cancellationToken)
public override async Task<BraceCompletionResult?> GetTextChangesAfterCompletionAsync(BraceCompletionContext context, IndentationOptions options, CancellationToken cancellationToken)
{
var documentOptions = await braceCompletionContext.Document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);

// After the closing brace is completed we need to format the span from the opening point to the closing point.
// E.g. when the user triggers completion for an if statement ($$ is the caret location) we insert braces to get
// if (true){$$}
// We then need to format this to
// if (true) { $$}

if (!options.AutoFormattingOptions.FormatOnCloseBrace)
{
return null;
}

var (formattingChanges, finalCurlyBraceEnd) = await FormatTrackingSpanAsync(
braceCompletionContext.Document,
braceCompletionContext.OpeningPoint,
braceCompletionContext.ClosingPoint,
shouldHonorAutoFormattingOnCloseBraceOption: true,
context.Document,
context.OpeningPoint,
context.ClosingPoint,
// We're not trying to format the indented block here, so no need to pass in additional rules.
braceFormattingIndentationRules: ImmutableArray<AbstractFormattingRule>.Empty,
documentOptions,
options,
cancellationToken).ConfigureAwait(false);

if (formattingChanges.IsEmpty)
@@ -55,7 +58,7 @@ internal abstract class AbstractCurlyBraceOrBracketCompletionService : AbstractB
}

// The caret location should be at the start of the closing brace character.
var originalText = await braceCompletionContext.Document.GetTextAsync(cancellationToken).ConfigureAwait(false);
var originalText = await context.Document.GetTextAsync(cancellationToken).ConfigureAwait(false);
var formattedText = originalText.WithChanges(formattingChanges);
var caretLocation = formattedText.Lines.GetLinePosition(finalCurlyBraceEnd - 1);

@@ -81,9 +84,9 @@ private static bool ContainsOnlyWhitespace(SourceText text, int openingPosition,
}

public override async Task<BraceCompletionResult?> GetTextChangeAfterReturnAsync(
davidwengier marked this conversation as resolved.
Show resolved Hide resolved
BraceCompletionContext context,
DocumentOptionSet documentOptions,
CancellationToken cancellationToken)
BraceCompletionContext context,
IndentationOptions options,
CancellationToken cancellationToken)
{
var document = context.Document;
var closingPoint = context.ClosingPoint;
@@ -112,7 +115,7 @@ private static bool ContainsOnlyWhitespace(SourceText text, int openingPosition,
var textToFormat = originalDocumentText;
if (closingPointLine - openingPointLine == 1)
{
var newLineString = documentOptions.GetOption(FormattingOptions2.NewLine);
var newLineString = options.FormattingOptions.NewLine;
newLineEdit = new TextChange(new TextSpan(closingPoint - 1, 0), newLineString);
textToFormat = originalDocumentText.WithChanges(newLineEdit.Value);

@@ -125,10 +128,10 @@ private static bool ContainsOnlyWhitespace(SourceText text, int openingPosition,
document.WithText(textToFormat),
openingPoint,
closingPoint,
shouldHonorAutoFormattingOnCloseBraceOption: false,
braceFormattingIndentationRules: GetBraceFormattingIndentationRulesAfterReturn(documentOptions),
documentOptions,
braceFormattingIndentationRules: GetBraceFormattingIndentationRulesAfterReturn(options),
options,
cancellationToken).ConfigureAwait(false);

closingPoint = newClosingPoint;
var formattedText = textToFormat.WithChanges(formattingChanges);

@@ -201,17 +204,10 @@ static ImmutableArray<TextChange> GetMergedChanges(TextChange newLineEdit, Immut
Document document,
int openingPoint,
int closingPoint,
bool shouldHonorAutoFormattingOnCloseBraceOption,
ImmutableArray<AbstractFormattingRule> braceFormattingIndentationRules,
DocumentOptionSet documentOptions,
IndentationOptions options,
CancellationToken cancellationToken)
{
var option = document.Project.Solution.Options.GetOption(BraceCompletionOptions.AutoFormattingOnCloseBrace, document.Project.Language);
if (!option && shouldHonorAutoFormattingOnCloseBraceOption)
{
return (ImmutableArray<TextChange>.Empty, closingPoint);
}

// Annotate the original closing brace so we can find it after formatting.
document = await GetDocumentWithAnnotatedClosingBraceAsync(document, closingPoint, cancellationToken).ConfigureAwait(false);

@@ -221,8 +217,7 @@ static ImmutableArray<TextChange> GetMergedChanges(TextChange newLineEdit, Immut
var startPoint = openingPoint;
var endPoint = AdjustFormattingEndPoint(text, root, startPoint, closingPoint);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code in CurlyBraceCompletionService.AdjustFormattingEndPoint was previously inline at this point


var style = documentOptions.GetOption(FormattingOptions.SmartIndent);
if (style == FormattingOptions.IndentStyle.Smart)
if (options.AutoFormattingOptions.IndentStyle == FormattingOptions.IndentStyle.Smart)
{
// Set the formatting start point to be the beginning of the first word to the left
// of the opening brace location.
@@ -242,8 +237,9 @@ static ImmutableArray<TextChange> GetMergedChanges(TextChange newLineEdit, Immut

var spanToFormat = TextSpan.FromBounds(Math.Max(startPoint, 0), endPoint);
var rules = document.GetFormattingRules(spanToFormat, braceFormattingIndentationRules);
var services = document.Project.Solution.Workspace.Services;
var result = Formatter.GetFormattingResult(
root, SpecializedCollections.SingletonEnumerable(spanToFormat), document.Project.Solution.Workspace, documentOptions, rules, cancellationToken);
root, SpecializedCollections.SingletonEnumerable(spanToFormat), services, options.FormattingOptions, rules, cancellationToken);
if (result == null)
{
return (ImmutableArray<TextChange>.Empty, closingPoint);
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Formatting.Rules;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Indentation;
using Microsoft.CodeAnalysis.Options;
using Roslyn.Utilities;

@@ -38,7 +39,7 @@ public override Task<bool> AllowOverTypeAsync(BraceCompletionContext context, Ca

protected override bool IsValidClosingBraceToken(SyntaxToken token) => token.IsKind(SyntaxKind.CloseBracketToken);

protected override ImmutableArray<AbstractFormattingRule> GetBraceFormattingIndentationRulesAfterReturn(DocumentOptionSet documentOptions)
protected override ImmutableArray<AbstractFormattingRule> GetBraceFormattingIndentationRulesAfterReturn(IndentationOptions options)
{
return ImmutableArray.Create(BraceCompletionFormattingRule.Instance);
}
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Formatting.Rules;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Indentation;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
@@ -80,9 +81,9 @@ protected override int AdjustFormattingEndPoint(SourceText text, SyntaxNode root
return endPoint;
}

protected override ImmutableArray<AbstractFormattingRule> GetBraceFormattingIndentationRulesAfterReturn(DocumentOptionSet documentOptions)
protected override ImmutableArray<AbstractFormattingRule> GetBraceFormattingIndentationRulesAfterReturn(IndentationOptions options)
{
var indentStyle = documentOptions.GetOption(FormattingOptions.SmartIndent);
var indentStyle = options.AutoFormattingOptions.IndentStyle;
return ImmutableArray.Create(BraceCompletionFormattingRule.ForIndentStyle(indentStyle));
}

You are viewing a condensed version of this merge commit. You can view the full changes here.