Use ImmutableArray in SourceTextDiffer too

This commit is contained in:
David Wengier 2024-09-09 11:58:15 +10:00
Родитель 04f2ca6f3c
Коммит 64786251c1
3 изменённых файлов: 15 добавлений и 23 удалений

Просмотреть файл

@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Threading;
@ -62,7 +63,7 @@ internal sealed class GeneratedDocumentPublisher : IGeneratedDocumentPublisher,
: new DocumentKey(ProjectKey.Unknown, filePath);
PublishData? previouslyPublishedData;
IReadOnlyList<TextChange> textChanges;
ImmutableArray<TextChange> textChanges;
lock (_publishedCSharpData)
{
@ -80,7 +81,7 @@ internal sealed class GeneratedDocumentPublisher : IGeneratedDocumentPublisher,
}
textChanges = SourceTextDiffer.GetMinimalTextChanges(previouslyPublishedData.SourceText, sourceText);
if (textChanges.Count == 0 && hostDocumentVersion == previouslyPublishedData.HostDocumentVersion)
if (textChanges.Length == 0 && hostDocumentVersion == previouslyPublishedData.HostDocumentVersion)
{
// Source texts match along with host document versions. We've already published something that looks like this. No-op.
return;
@ -89,7 +90,7 @@ internal sealed class GeneratedDocumentPublisher : IGeneratedDocumentPublisher,
_logger.LogDebug(
$"Updating C# buffer of {filePath} for project {documentKey.ProjectKey} to correspond with host document " +
$"version {hostDocumentVersion}. {previouslyPublishedData.SourceText.Length} -> {sourceText.Length} = Change delta of " +
$"{sourceText.Length - previouslyPublishedData.SourceText.Length} via {textChanges.Count} text changes.");
$"{sourceText.Length - previouslyPublishedData.SourceText.Length} via {textChanges.Length} text changes.");
_publishedCSharpData[documentKey] = new PublishData(sourceText, hostDocumentVersion);
}
@ -109,7 +110,7 @@ internal sealed class GeneratedDocumentPublisher : IGeneratedDocumentPublisher,
public void PublishHtml(ProjectKey projectKey, string filePath, SourceText sourceText, int hostDocumentVersion)
{
PublishData? previouslyPublishedData;
IReadOnlyList<TextChange> textChanges;
ImmutableArray<TextChange> textChanges;
lock (_publishedHtmlData)
{
@ -126,14 +127,14 @@ internal sealed class GeneratedDocumentPublisher : IGeneratedDocumentPublisher,
}
textChanges = SourceTextDiffer.GetMinimalTextChanges(previouslyPublishedData.SourceText, sourceText);
if (textChanges.Count == 0 && hostDocumentVersion == previouslyPublishedData.HostDocumentVersion)
if (textChanges.Length == 0 && hostDocumentVersion == previouslyPublishedData.HostDocumentVersion)
{
// Source texts match along with host document versions. We've already published something that looks like this. No-op.
return;
}
_logger.LogDebug(
$"Updating HTML buffer of {filePath} to correspond with host document version {hostDocumentVersion}. {previouslyPublishedData.SourceText.Length} -> {sourceText.Length} = Change delta of {sourceText.Length - previouslyPublishedData.SourceText.Length} via {textChanges.Count} text changes.");
$"Updating HTML buffer of {filePath} to correspond with host document version {hostDocumentVersion}. {previouslyPublishedData.SourceText.Length} -> {sourceText.Length} = Change delta of {sourceText.Length - previouslyPublishedData.SourceText.Length} via {textChanges.Length} text changes.");
_publishedHtmlData[filePath] = new PublishData(sourceText, hostDocumentVersion);
}

Просмотреть файл

@ -291,7 +291,7 @@ internal static class SourceTextExtensions
return [];
}
return SourceTextDiffer.GetMinimalTextChanges(text, originalTextWithChanges, DiffKind.Char).ToImmutableArray();
return SourceTextDiffer.GetMinimalTextChanges(text, originalTextWithChanges, DiffKind.Char);
}
/// <summary>

Просмотреть файл

@ -4,6 +4,7 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text;
@ -63,12 +64,12 @@ internal abstract partial class SourceTextDiffer : TextDiffer, IDisposable
}
}
private IReadOnlyList<TextChange> ConsolidateEdits(List<DiffEdit> edits)
private ImmutableArray<TextChange> ConsolidateEdits(List<DiffEdit> edits)
{
// Scan through the list of edits and collapse them into a minimal set of TextChanges.
// This method assumes that there are no overlapping changes and the changes are sorted.
var minimalChanges = new List<TextChange>();
using var minimalChanges = new PooledArrayBuilder<TextChange>(capacity: edits.Count);
var start = 0;
var end = 0;
@ -99,28 +100,18 @@ internal abstract partial class SourceTextDiffer : TextDiffer, IDisposable
minimalChanges.Add(new TextChange(TextSpan.FromBounds(start, end), builder.ToString()));
}
return minimalChanges;
return minimalChanges.DrainToImmutable();
}
public static IReadOnlyList<TextChange> GetMinimalTextChanges(SourceText oldText, SourceText newText, DiffKind kind = DiffKind.Line)
public static ImmutableArray<TextChange> GetMinimalTextChanges(SourceText oldText, SourceText newText, DiffKind kind = DiffKind.Line)
{
if (oldText is null)
{
throw new ArgumentNullException(nameof(oldText));
}
if (newText is null)
{
throw new ArgumentNullException(nameof(newText));
}
if (oldText.ContentEquals(newText))
{
return Array.Empty<TextChange>();
return [];
}
else if (oldText.Length == 0 || newText.Length == 0)
{
return newText.GetTextChanges(oldText);
return newText.GetTextChangesArray(oldText);
}
using SourceTextDiffer differ = kind == DiffKind.Line