Skip to content

Commit fcfb859

Browse files
authored
Small perf improvement during code action computation. (#68631)
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.
1 parent 421d20f commit fcfb859

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

src/Features/Core/Portable/Wrapping/AbstractCodeActionComputer.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
using Microsoft.CodeAnalysis.Shared.Extensions;
1818
using Microsoft.CodeAnalysis.Text;
1919
using Roslyn.Utilities;
20-
using static Microsoft.CodeAnalysis.CodeActions.CodeAction;
2120

2221
namespace Microsoft.CodeAnalysis.Wrapping
2322
{
@@ -88,9 +87,8 @@ protected string GetSmartIndentationAfter(SyntaxNodeOrToken nodeOrToken)
8887
protected string GetIndentationAfter(SyntaxNodeOrToken nodeOrToken, FormattingOptions2.IndentStyle indentStyle)
8988
{
9089
var newLine = Options.FormattingOptions.NewLine;
91-
var newSourceText = OriginalSourceText.WithChanges(new TextChange(new TextSpan(nodeOrToken.Span.End, 0), newLine));
92-
newSourceText = newSourceText.WithChanges(
93-
new TextChange(TextSpan.FromBounds(nodeOrToken.Span.End + newLine.Length, newSourceText.Length), ""));
90+
var newSourceText = OriginalSourceText.WithChanges(
91+
new TextChange(TextSpan.FromBounds(nodeOrToken.Span.End, OriginalSourceText.Length), newLine));
9492

9593
var newDocument = OriginalDocument.WithText(newSourceText);
9694

src/Features/Core/Portable/Wrapping/SeparatedSyntaxList/SeparatedSyntaxListCodeActionComputer.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System;
56
using System.Collections.Immutable;
67
using System.Threading;
78
using System.Threading.Tasks;
@@ -45,7 +46,7 @@ private class SeparatedSyntaxListCodeActionComputer : AbstractCodeActionComputer
4546
/// ^
4647
/// |
4748
/// </summary>
48-
private readonly SyntaxTrivia _singleIndentationTrivia;
49+
private readonly Lazy<SyntaxTrivia> _singleIndentationTrivia;
4950

5051
/// <summary>
5152
/// Indentation to use when placing brace. e.g.:
@@ -54,7 +55,7 @@ private class SeparatedSyntaxListCodeActionComputer : AbstractCodeActionComputer
5455
/// ^
5556
/// |
5657
/// </summary>
57-
private readonly SyntaxTrivia _braceIndentationTrivia;
58+
private readonly Lazy<SyntaxTrivia> _braceIndentationTrivia;
5859

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

9192
_afterOpenTokenIndentationTrivia = generator.Whitespace(GetAfterOpenTokenIdentation());
92-
_singleIndentationTrivia = generator.Whitespace(GetSingleIdentation());
93-
_braceIndentationTrivia = generator.Whitespace(GetBraceTokenIndentation());
93+
_singleIndentationTrivia = new Lazy<SyntaxTrivia>(() => generator.Whitespace(GetSingleIdentation()));
94+
_braceIndentationTrivia = new Lazy<SyntaxTrivia>(() => generator.Whitespace(GetBraceTokenIndentation()));
9495
}
9596

9697
private void AddTextChangeBetweenOpenAndFirstItem(
9798
WrappingStyle wrappingStyle, ArrayBuilder<Edit> result)
9899
{
99100
result.Add(wrappingStyle == WrappingStyle.WrapFirst_IndentRest
100-
? Edit.UpdateBetween(_listSyntax.GetFirstToken(), NewLineTrivia, _singleIndentationTrivia, _listItems[0])
101+
? Edit.UpdateBetween(_listSyntax.GetFirstToken(), NewLineTrivia, _singleIndentationTrivia.Value, _listItems[0])
101102
: Edit.DeleteBetween(_listSyntax.GetFirstToken(), _listItems[0]));
102103
}
103104

@@ -124,7 +125,7 @@ private SyntaxTrivia GetIndentationTrivia(WrappingStyle wrappingStyle)
124125
{
125126
return wrappingStyle == WrappingStyle.UnwrapFirst_AlignRest
126127
? _afterOpenTokenIndentationTrivia
127-
: _singleIndentationTrivia;
128+
: _singleIndentationTrivia.Value;
128129
}
129130

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

272273
if (_shouldMoveOpenBraceToNewLine)
273-
result.Add(Edit.UpdateBetween(_listSyntax.GetFirstToken().GetPreviousToken(), NewLineTrivia, _braceIndentationTrivia, _listSyntax.GetFirstToken()));
274+
result.Add(Edit.UpdateBetween(_listSyntax.GetFirstToken().GetPreviousToken(), NewLineTrivia, _braceIndentationTrivia.Value, _listSyntax.GetFirstToken()));
274275

275276
AddTextChangeBetweenOpenAndFirstItem(wrappingStyle, result);
276277

@@ -317,7 +318,7 @@ private ImmutableArray<Edit> GetWrapLongLinesEdits(
317318

318319
if (this.Wrapper.ShouldMoveCloseBraceToNewLine)
319320
{
320-
result.Add(Edit.UpdateBetween(itemsAndSeparators.Last(), NewLineTrivia, _braceIndentationTrivia, _listSyntax.GetLastToken()));
321+
result.Add(Edit.UpdateBetween(itemsAndSeparators.Last(), NewLineTrivia, _braceIndentationTrivia.Value, _listSyntax.GetLastToken()));
321322
}
322323
else
323324
{
@@ -396,7 +397,7 @@ private ImmutableArray<Edit> GetWrapEachEdits(
396397
using var _ = ArrayBuilder<Edit>.GetInstance(out var result);
397398

398399
if (_shouldMoveOpenBraceToNewLine)
399-
result.Add(Edit.UpdateBetween(_listSyntax.GetFirstToken().GetPreviousToken(), NewLineTrivia, _braceIndentationTrivia, _listSyntax.GetFirstToken()));
400+
result.Add(Edit.UpdateBetween(_listSyntax.GetFirstToken().GetPreviousToken(), NewLineTrivia, _braceIndentationTrivia.Value, _listSyntax.GetFirstToken()));
400401

401402
AddTextChangeBetweenOpenAndFirstItem(wrappingStyle, result);
402403

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

420421
if (_shouldMoveCloseBraceToNewLine)
421422
{
422-
result.Add(Edit.UpdateBetween(itemsAndSeparators.Last(), NewLineTrivia, _braceIndentationTrivia, _listSyntax.GetLastToken()));
423+
result.Add(Edit.UpdateBetween(itemsAndSeparators.Last(), NewLineTrivia, _braceIndentationTrivia.Value, _listSyntax.GetLastToken()));
423424
}
424425
else
425426
{

0 commit comments

Comments
 (0)