11// Copyright (c) .NET Foundation. All rights reserved.
22// Licensed under the MIT license. See License.txt in the project root for license information.
33
4+ using System . Collections . Immutable ;
45using System . Text ;
56using Microsoft . CodeAnalysis ;
67using Microsoft . CodeAnalysis . ExternalAccess . Razor ;
@@ -11,7 +12,7 @@ namespace Microsoft.VisualStudioCode.RazorExtension.Services;
1112
1213internal class LspTextChangesTextLoader (
1314 TextDocument ? document ,
14- RazorTextChange change ,
15+ RazorTextChange [ ] changes ,
1516 byte [ ] checksum ,
1617 SourceHashAlgorithm checksumAlgorithm ,
1718 int ? codePage ,
@@ -21,13 +22,13 @@ internal class LspTextChangesTextLoader(
2122 private const string ProvideRazorDynamicFileInfoMethodName = "razor/provideDynamicFileInfo" ;
2223
2324 private readonly TextDocument ? _document = document ;
24- private readonly RazorTextChange _change = change ;
25+ private readonly ImmutableArray < TextChange > _changes = changes . SelectAsArray ( c => c . ToTextChange ( ) ) ;
2526 private readonly byte [ ] _checksum = checksum ;
2627 private readonly SourceHashAlgorithm _checksumAlgorithm = checksumAlgorithm ;
2728 private readonly int ? _codePage = codePage ;
2829 private readonly Uri _razorUri = razorUri ;
2930 private readonly IRazorClientLanguageServerManager _razorClientLanguageServerManager = razorClientLanguageServerManager ;
30- private readonly Lazy < SourceText > _emptySourceText = new Lazy < SourceText > ( ( ) =>
31+ private readonly Lazy < SourceText > _emptySourceText = new ( ( ) =>
3132 {
3233 var encoding = codePage is null ? null : Encoding . GetEncoding ( codePage . Value ) ;
3334 return SourceText . From ( "" , checksumAlgorithm : checksumAlgorithm , encoding : encoding ) ;
@@ -37,7 +38,7 @@ public override async Task<TextAndVersion> LoadTextAndVersionAsync(LoadTextOptio
3738 {
3839 if ( _document is null )
3940 {
40- var text = _emptySourceText . Value . WithChanges ( _change . ToTextChange ( ) ) ;
41+ var text = ApplyChanges ( _emptySourceText . Value , _changes ) ;
4142 return TextAndVersion . Create ( text , VersionStamp . Default . GetNewerVersion ( ) ) ;
4243 }
4344
@@ -47,7 +48,7 @@ public override async Task<TextAndVersion> LoadTextAndVersionAsync(LoadTextOptio
4748 if ( IsSourceTextMatching ( sourceText ) )
4849 {
4950 var version = await _document . GetTextVersionAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
50- var newText = sourceText . WithChanges ( _change . ToTextChange ( ) ) ;
51+ var newText = ApplyChanges ( sourceText , _changes ) ;
5152 return TextAndVersion . Create ( newText , version . GetNewerVersion ( ) ) ;
5253 }
5354
@@ -88,7 +89,17 @@ private async Task<TextAndVersion> GetFullDocumentFromServerAsync(CancellationTo
8889 } ,
8990 cancellationToken ) . ConfigureAwait ( false ) ;
9091
91- var text = _emptySourceText . Value . WithChanges ( response . Edit . ToTextChange ( ) ) ;
92+ var text = ApplyChanges ( _emptySourceText . Value , response . Edits . SelectAsArray ( e => e . ToTextChange ( ) ) ) ;
9293 return TextAndVersion . Create ( text , VersionStamp . Default . GetNewerVersion ( ) ) ;
9394 }
95+
96+ private static SourceText ApplyChanges ( SourceText sourceText , ImmutableArray < TextChange > changes )
97+ {
98+ foreach ( var change in changes )
99+ {
100+ sourceText = sourceText . WithChanges ( change ) ;
101+ }
102+
103+ return sourceText ;
104+ }
94105}
0 commit comments