Skip to content

Commit 39a5c88

Browse files
author
Andrew Hall
committed
Consolidate code
1 parent be26139 commit 39a5c88

File tree

4 files changed

+127
-117
lines changed

4 files changed

+127
-117
lines changed

src/Razor/src/Microsoft.VisualStudioCode.RazorExtension/Services/DynamicFileInfoProvider.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,13 @@
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.Composition;
54
using Microsoft.CodeAnalysis;
65
using Microsoft.CodeAnalysis.ExternalAccess.Razor;
76
using Microsoft.CodeAnalysis.ExternalAccess.Razor.Features;
87

98
namespace Microsoft.VisualStudioCode.RazorExtension.Services;
109

11-
[ExportRazorLspServiceFactory(typeof(RazorLspDynamicFileInfoProvider)), Shared]
12-
internal sealed class DynamicFileProviderFactory : AbstractRazorLspServiceFactory
13-
{
14-
protected override AbstractRazorLspService CreateService(IRazorLspServices lspServices)
15-
{
16-
var clientLanguageServerManager = lspServices.GetRequiredService<IRazorClientLanguageServerManager>();
17-
return new LspDynamicFileProvider(clientLanguageServerManager);
18-
}
19-
}
20-
21-
file sealed class LspDynamicFileProvider(IRazorClientLanguageServerManager clientLanguageServerManager) : RazorLspDynamicFileInfoProvider
10+
internal sealed partial class LspDynamicFileProvider(IRazorClientLanguageServerManager clientLanguageServerManager) : RazorLspDynamicFileInfoProvider
2211
{
2312
private const string ProvideRazorDynamicFileInfoMethodName = "razor/provideDynamicFileInfo";
2413
private const string RemoveRazorDynamicFileInfoMethodName = "razor/removeDynamicFileInfo";
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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.Composition;
5+
using Microsoft.CodeAnalysis.ExternalAccess.Razor;
6+
using Microsoft.CodeAnalysis.ExternalAccess.Razor.Features;
7+
8+
namespace Microsoft.VisualStudioCode.RazorExtension.Services;
9+
10+
[ExportRazorLspServiceFactory(typeof(RazorLspDynamicFileInfoProvider)), Shared]
11+
internal sealed class DynamicFileProviderFactory : AbstractRazorLspServiceFactory
12+
{
13+
protected override AbstractRazorLspService CreateService(IRazorLspServices lspServices)
14+
{
15+
var clientLanguageServerManager = lspServices.GetRequiredService<IRazorClientLanguageServerManager>();
16+
return new LspDynamicFileProvider(clientLanguageServerManager);
17+
}
18+
}
19+
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+

src/Razor/src/Microsoft.VisualStudioCode.RazorExtension/Services/LspTextChangesTextLoader.cs

Lines changed: 0 additions & 105 deletions
This file was deleted.

0 commit comments

Comments
 (0)