diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Extensions/SourceTextExtensions.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Extensions/SourceTextExtensions.cs index 1a6cc86db4..35be7286ad 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Extensions/SourceTextExtensions.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Extensions/SourceTextExtensions.cs @@ -322,4 +322,17 @@ internal static class SourceTextExtensions return lfCount > crlfCount; } + + public static ImmutableArray GetTextChangesArray(this SourceText newText, SourceText oldText) + { + var list = newText.GetTextChanges(oldText); + + // Fast path for the common case. The base SourceText.GetTextChanges method returns an ImmutableArray + if (list is ImmutableArray array) + { + return array; + } + + return list.ToImmutableArray(); + } } diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/Passes/CSharpFormattingPass.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/Passes/CSharpFormattingPass.cs index edbabe766d..efea029797 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/Passes/CSharpFormattingPass.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/Passes/CSharpFormattingPass.cs @@ -2,7 +2,6 @@ // Licensed under the MIT license. See License.txt in the project root for license information. using System; -using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using System.Threading; @@ -63,9 +62,7 @@ internal sealed class CSharpFormattingPass( _logger.LogTestOnly($"Generated C#:\r\n{context.CSharpSourceText}"); - var finalChanges = changedText.GetTextChanges(originalText); - - return finalChanges.ToImmutableArray(); + return changedText.GetTextChangesArray(originalText); } private async Task> FormatCSharpAsync(FormattingContext context, CancellationToken cancellationToken) diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/Passes/CSharpOnTypeFormattingPass.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/Passes/CSharpOnTypeFormattingPass.cs index 6dae9bdcda..cad882f26b 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/Passes/CSharpOnTypeFormattingPass.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/Passes/CSharpOnTypeFormattingPass.cs @@ -177,7 +177,7 @@ internal sealed class CSharpOnTypeFormattingPass( } // Now that we have made all the necessary changes to the document. Let's diff the original vs final version and return the diff. - var finalChanges = cleanedText.GetTextChanges(originalText).ToImmutableArray(); + var finalChanges = cleanedText.GetTextChangesArray(originalText); finalChanges = await AddUsingStatementEditsIfNecessaryAsync(context, codeDocument, csharpText, changes, originalTextWithChanges, finalChanges, cancellationToken).ConfigureAwait(false); diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/Passes/HtmlFormattingPassBase.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/Passes/HtmlFormattingPassBase.cs index d90cfa4a5d..a2f62eecec 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/Passes/HtmlFormattingPassBase.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/Passes/HtmlFormattingPassBase.cs @@ -45,9 +45,7 @@ internal abstract class HtmlFormattingPassBase(ILogger logger) : IFormattingPass _logger.LogTestOnly($"After AdjustRazorIndentation:\r\n{changedText}"); } - var finalChanges = changedText.GetTextChanges(originalText); - - return finalChanges.ToImmutableArray(); + return changedText.GetTextChangesArray(originalText); } private static ImmutableArray AdjustRazorIndentation(FormattingContext context) diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/Passes/RazorFormattingPass.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/Passes/RazorFormattingPass.cs index fb2f8150e0..ae6af5f540 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/Passes/RazorFormattingPass.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/Passes/RazorFormattingPass.cs @@ -1,7 +1,6 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT license. See License.txt in the project root for license information. -using System.Collections.Generic; using System.Collections.Immutable; using System.Diagnostics.CodeAnalysis; using System.Threading; @@ -43,9 +42,7 @@ internal sealed class RazorFormattingPass : IFormattingPass // Compute the final combined set of edits changedText = changedText.WithChanges(razorChanges); - var finalChanges = changedText.GetTextChanges(originalText); - - return finalChanges.ToImmutableArray(); + return changedText.GetTextChangesArray(originalText); } private static ImmutableArray FormatRazor(FormattingContext context, RazorSyntaxTree syntaxTree) diff --git a/src/Shared/Microsoft.AspNetCore.Razor.Utilities.Shared/ReadOnlyListExtensions.cs b/src/Shared/Microsoft.AspNetCore.Razor.Utilities.Shared/ReadOnlyListExtensions.cs index 7ac856fa2c..01c96def82 100644 --- a/src/Shared/Microsoft.AspNetCore.Razor.Utilities.Shared/ReadOnlyListExtensions.cs +++ b/src/Shared/Microsoft.AspNetCore.Razor.Utilities.Shared/ReadOnlyListExtensions.cs @@ -59,11 +59,6 @@ internal static class ReadOnlyListExtensions public static ImmutableArray ToImmutableArray(this IReadOnlyList list) { - if (list is ImmutableArray array) - { - return array; - } - return list switch { [] => [],