|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT license. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System.Collections.Immutable; |
| 5 | +using System.Text; |
| 6 | +using Microsoft.CodeAnalysis; |
| 7 | +using Microsoft.CodeAnalysis.ExternalAccess.Razor; |
| 8 | +using Microsoft.CodeAnalysis.Razor.Protocol; |
| 9 | +using Microsoft.CodeAnalysis.Text; |
| 10 | + |
| 11 | +namespace Microsoft.VisualStudioCode.RazorExtension.Services; |
| 12 | + |
| 13 | +internal sealed partial class LspDynamicFileProvider |
| 14 | +{ |
| 15 | + private sealed class LspTextChangesTextLoader( |
| 16 | + TextDocument? document, |
| 17 | + RazorTextChange[] changes, |
| 18 | + byte[] checksum, |
| 19 | + SourceHashAlgorithm checksumAlgorithm, |
| 20 | + int? codePage, |
| 21 | + Uri razorUri, |
| 22 | + IRazorClientLanguageServerManager razorClientLanguageServerManager) : TextLoader |
| 23 | + { |
| 24 | + private readonly TextDocument? _document = document; |
| 25 | + private readonly ImmutableArray<TextChange> _changes = changes.SelectAsArray(c => c.ToTextChange()); |
| 26 | + private readonly byte[] _checksum = checksum; |
| 27 | + private readonly SourceHashAlgorithm _checksumAlgorithm = checksumAlgorithm; |
| 28 | + private readonly int? _codePage = codePage; |
| 29 | + private readonly Uri _razorUri = razorUri; |
| 30 | + private readonly IRazorClientLanguageServerManager _razorClientLanguageServerManager = razorClientLanguageServerManager; |
| 31 | + private readonly Lazy<SourceText> _emptySourceText = new(() => |
| 32 | + { |
| 33 | + var encoding = codePage is null ? null : Encoding.GetEncoding(codePage.Value); |
| 34 | + return SourceText.From("", checksumAlgorithm: checksumAlgorithm, encoding: encoding); |
| 35 | + }); |
| 36 | + |
| 37 | + public override async Task<TextAndVersion> LoadTextAndVersionAsync(LoadTextOptions options, CancellationToken cancellationToken) |
| 38 | + { |
| 39 | + if (_document is null) |
| 40 | + { |
| 41 | + var text = ApplyChanges(_emptySourceText.Value, _changes); |
| 42 | + return TextAndVersion.Create(text, VersionStamp.Default.GetNewerVersion()); |
| 43 | + } |
| 44 | + |
| 45 | + var sourceText = await _document.GetTextAsync(cancellationToken).ConfigureAwait(false); |
| 46 | + |
| 47 | + // Validate the checksum information so the edits are known to be correct |
| 48 | + if (IsSourceTextMatching(sourceText)) |
| 49 | + { |
| 50 | + var version = await _document.GetTextVersionAsync(cancellationToken).ConfigureAwait(false); |
| 51 | + var newText = ApplyChanges(sourceText, _changes); |
| 52 | + return TextAndVersion.Create(newText, version.GetNewerVersion()); |
| 53 | + } |
| 54 | + |
| 55 | + return await GetFullDocumentFromServerAsync(cancellationToken).ConfigureAwait(false); |
| 56 | + } |
| 57 | + |
| 58 | + private bool IsSourceTextMatching(SourceText sourceText) |
| 59 | + { |
| 60 | + if (sourceText.ChecksumAlgorithm != _checksumAlgorithm) |
| 61 | + { |
| 62 | + return false; |
| 63 | + } |
| 64 | + |
| 65 | + if (sourceText.Encoding?.CodePage != _codePage) |
| 66 | + { |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + if (!sourceText.GetChecksum().SequenceEqual(_checksum)) |
| 71 | + { |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + return true; |
| 76 | + } |
| 77 | + |
| 78 | + private async Task<TextAndVersion> GetFullDocumentFromServerAsync(CancellationToken cancellationToken) |
| 79 | + { |
| 80 | + var response = await _razorClientLanguageServerManager.SendRequestAsync<RazorProvideDynamicFileParams, RazorProvideDynamicFileResponse>( |
| 81 | + ProvideRazorDynamicFileInfoMethodName, |
| 82 | + new RazorProvideDynamicFileParams |
| 83 | + { |
| 84 | + RazorDocument = new() |
| 85 | + { |
| 86 | + Uri = _razorUri, |
| 87 | + }, |
| 88 | + FullText = true |
| 89 | + }, |
| 90 | + cancellationToken).ConfigureAwait(false); |
| 91 | + |
| 92 | + var text = ApplyChanges(_emptySourceText.Value, response.Edits.SelectAsArray(e => e.ToTextChange())); |
| 93 | + return TextAndVersion.Create(text, VersionStamp.Default.GetNewerVersion()); |
| 94 | + } |
| 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 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
0 commit comments