Skip to content

Commit

Permalink
Small perf improvement during code action computation. (#68631)
Browse files Browse the repository at this point in the history
1) Changed AbstractCodeActionComputer.GetIndentationAfter to only create two snapshots per invocation.
2) Slight optimization around codeaction computation in parameter list context as there are scenarios I was hitting where neither  _singleIndentationTrivia nor _braceIndentationTrivia were used during ComputeWrappingGroupsAsync.
  • Loading branch information
ToddGrun authored Jun 15, 2023
1 parent 421d20f commit fcfb859
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
using static Microsoft.CodeAnalysis.CodeActions.CodeAction;

namespace Microsoft.CodeAnalysis.Wrapping
{
Expand Down Expand Up @@ -88,9 +87,8 @@ protected string GetSmartIndentationAfter(SyntaxNodeOrToken nodeOrToken)
protected string GetIndentationAfter(SyntaxNodeOrToken nodeOrToken, FormattingOptions2.IndentStyle indentStyle)
{
var newLine = Options.FormattingOptions.NewLine;
var newSourceText = OriginalSourceText.WithChanges(new TextChange(new TextSpan(nodeOrToken.Span.End, 0), newLine));
newSourceText = newSourceText.WithChanges(
new TextChange(TextSpan.FromBounds(nodeOrToken.Span.End + newLine.Length, newSourceText.Length), ""));
var newSourceText = OriginalSourceText.WithChanges(
new TextChange(TextSpan.FromBounds(nodeOrToken.Span.End, OriginalSourceText.Length), newLine));

var newDocument = OriginalDocument.WithText(newSourceText);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -45,7 +46,7 @@ private class SeparatedSyntaxListCodeActionComputer : AbstractCodeActionComputer
/// ^
/// |
/// </summary>
private readonly SyntaxTrivia _singleIndentationTrivia;
private readonly Lazy<SyntaxTrivia> _singleIndentationTrivia;

/// <summary>
/// Indentation to use when placing brace. e.g.:
Expand All @@ -54,7 +55,7 @@ private class SeparatedSyntaxListCodeActionComputer : AbstractCodeActionComputer
/// ^
/// |
/// </summary>
private readonly SyntaxTrivia _braceIndentationTrivia;
private readonly Lazy<SyntaxTrivia> _braceIndentationTrivia;

/// <summary>
/// Whether or not we should move the open brace of this separated list to a new line. Many separated lists
Expand Down Expand Up @@ -89,15 +90,15 @@ public SeparatedSyntaxListCodeActionComputer(
var generator = SyntaxGenerator.GetGenerator(OriginalDocument);

_afterOpenTokenIndentationTrivia = generator.Whitespace(GetAfterOpenTokenIdentation());
_singleIndentationTrivia = generator.Whitespace(GetSingleIdentation());
_braceIndentationTrivia = generator.Whitespace(GetBraceTokenIndentation());
_singleIndentationTrivia = new Lazy<SyntaxTrivia>(() => generator.Whitespace(GetSingleIdentation()));
_braceIndentationTrivia = new Lazy<SyntaxTrivia>(() => generator.Whitespace(GetBraceTokenIndentation()));
}

private void AddTextChangeBetweenOpenAndFirstItem(
WrappingStyle wrappingStyle, ArrayBuilder<Edit> result)
{
result.Add(wrappingStyle == WrappingStyle.WrapFirst_IndentRest
? Edit.UpdateBetween(_listSyntax.GetFirstToken(), NewLineTrivia, _singleIndentationTrivia, _listItems[0])
? Edit.UpdateBetween(_listSyntax.GetFirstToken(), NewLineTrivia, _singleIndentationTrivia.Value, _listItems[0])
: Edit.DeleteBetween(_listSyntax.GetFirstToken(), _listItems[0]));
}

Expand All @@ -124,7 +125,7 @@ private SyntaxTrivia GetIndentationTrivia(WrappingStyle wrappingStyle)
{
return wrappingStyle == WrappingStyle.UnwrapFirst_AlignRest
? _afterOpenTokenIndentationTrivia
: _singleIndentationTrivia;
: _singleIndentationTrivia.Value;
}

private string GetBraceTokenIndentation()
Expand Down Expand Up @@ -270,7 +271,7 @@ private ImmutableArray<Edit> GetWrapLongLinesEdits(
using var _ = ArrayBuilder<Edit>.GetInstance(out var result);

if (_shouldMoveOpenBraceToNewLine)
result.Add(Edit.UpdateBetween(_listSyntax.GetFirstToken().GetPreviousToken(), NewLineTrivia, _braceIndentationTrivia, _listSyntax.GetFirstToken()));
result.Add(Edit.UpdateBetween(_listSyntax.GetFirstToken().GetPreviousToken(), NewLineTrivia, _braceIndentationTrivia.Value, _listSyntax.GetFirstToken()));

AddTextChangeBetweenOpenAndFirstItem(wrappingStyle, result);

Expand Down Expand Up @@ -317,7 +318,7 @@ private ImmutableArray<Edit> GetWrapLongLinesEdits(

if (this.Wrapper.ShouldMoveCloseBraceToNewLine)
{
result.Add(Edit.UpdateBetween(itemsAndSeparators.Last(), NewLineTrivia, _braceIndentationTrivia, _listSyntax.GetLastToken()));
result.Add(Edit.UpdateBetween(itemsAndSeparators.Last(), NewLineTrivia, _braceIndentationTrivia.Value, _listSyntax.GetLastToken()));
}
else
{
Expand Down Expand Up @@ -396,7 +397,7 @@ private ImmutableArray<Edit> GetWrapEachEdits(
using var _ = ArrayBuilder<Edit>.GetInstance(out var result);

if (_shouldMoveOpenBraceToNewLine)
result.Add(Edit.UpdateBetween(_listSyntax.GetFirstToken().GetPreviousToken(), NewLineTrivia, _braceIndentationTrivia, _listSyntax.GetFirstToken()));
result.Add(Edit.UpdateBetween(_listSyntax.GetFirstToken().GetPreviousToken(), NewLineTrivia, _braceIndentationTrivia.Value, _listSyntax.GetFirstToken()));

AddTextChangeBetweenOpenAndFirstItem(wrappingStyle, result);

Expand All @@ -419,7 +420,7 @@ private ImmutableArray<Edit> GetWrapEachEdits(

if (_shouldMoveCloseBraceToNewLine)
{
result.Add(Edit.UpdateBetween(itemsAndSeparators.Last(), NewLineTrivia, _braceIndentationTrivia, _listSyntax.GetLastToken()));
result.Add(Edit.UpdateBetween(itemsAndSeparators.Last(), NewLineTrivia, _braceIndentationTrivia.Value, _listSyntax.GetLastToken()));
}
else
{
Expand Down

0 comments on commit fcfb859

Please sign in to comment.