From 2278b3d05357748c24e023bb65ae4f8db5bc3094 Mon Sep 17 00:00:00 2001 From: Todd Grunke Date: Wed, 14 Jun 2023 18:20:56 -0700 Subject: [PATCH] Small perf improvement during code action computation. 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. --- .../Wrapping/AbstractCodeActionComputer.cs | 6 ++---- .../SeparatedSyntaxListCodeActionComputer.cs | 21 ++++++++++--------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/Features/Core/Portable/Wrapping/AbstractCodeActionComputer.cs b/src/Features/Core/Portable/Wrapping/AbstractCodeActionComputer.cs index 822324aa2458d..5602423a1d5cc 100644 --- a/src/Features/Core/Portable/Wrapping/AbstractCodeActionComputer.cs +++ b/src/Features/Core/Portable/Wrapping/AbstractCodeActionComputer.cs @@ -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 { @@ -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); diff --git a/src/Features/Core/Portable/Wrapping/SeparatedSyntaxList/SeparatedSyntaxListCodeActionComputer.cs b/src/Features/Core/Portable/Wrapping/SeparatedSyntaxList/SeparatedSyntaxListCodeActionComputer.cs index 52b856e0096cf..1d6679ae7954f 100644 --- a/src/Features/Core/Portable/Wrapping/SeparatedSyntaxList/SeparatedSyntaxListCodeActionComputer.cs +++ b/src/Features/Core/Portable/Wrapping/SeparatedSyntaxList/SeparatedSyntaxListCodeActionComputer.cs @@ -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; @@ -45,7 +46,7 @@ private class SeparatedSyntaxListCodeActionComputer : AbstractCodeActionComputer /// ^ /// | /// - private readonly SyntaxTrivia _singleIndentationTrivia; + private readonly Lazy _singleIndentationTrivia; /// /// Indentation to use when placing brace. e.g.: @@ -54,7 +55,7 @@ private class SeparatedSyntaxListCodeActionComputer : AbstractCodeActionComputer /// ^ /// | /// - private readonly SyntaxTrivia _braceIndentationTrivia; + private readonly Lazy _braceIndentationTrivia; /// /// 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( var generator = SyntaxGenerator.GetGenerator(OriginalDocument); _afterOpenTokenIndentationTrivia = generator.Whitespace(GetAfterOpenTokenIdentation()); - _singleIndentationTrivia = generator.Whitespace(GetSingleIdentation()); - _braceIndentationTrivia = generator.Whitespace(GetBraceTokenIndentation()); + _singleIndentationTrivia = new Lazy(() => generator.Whitespace(GetSingleIdentation())); + _braceIndentationTrivia = new Lazy(() => generator.Whitespace(GetBraceTokenIndentation())); } private void AddTextChangeBetweenOpenAndFirstItem( WrappingStyle wrappingStyle, ArrayBuilder 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])); } @@ -124,7 +125,7 @@ private SyntaxTrivia GetIndentationTrivia(WrappingStyle wrappingStyle) { return wrappingStyle == WrappingStyle.UnwrapFirst_AlignRest ? _afterOpenTokenIndentationTrivia - : _singleIndentationTrivia; + : _singleIndentationTrivia.Value; } private string GetBraceTokenIndentation() @@ -270,7 +271,7 @@ private ImmutableArray GetWrapLongLinesEdits( using var _ = ArrayBuilder.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); @@ -317,7 +318,7 @@ private ImmutableArray 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 { @@ -396,7 +397,7 @@ private ImmutableArray GetWrapEachEdits( using var _ = ArrayBuilder.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); @@ -419,7 +420,7 @@ private ImmutableArray 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 {