Skip to content

Commit 5a6f2de

Browse files
committed
Specialize methods to get a Razor Uri from a generated document Uri, etc.
1 parent 4fe57a7 commit 5a6f2de

File tree

5 files changed

+56
-15
lines changed

5 files changed

+56
-15
lines changed

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/LspEditMappingService.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515

1616
namespace Microsoft.AspNetCore.Razor.LanguageServer;
1717

18-
internal class LspEditMappingService(
18+
internal sealed class LspEditMappingService(
1919
IDocumentMappingService documentMappingService,
2020
IFilePathService filePathService,
2121
IDocumentContextFactory documentContextFactory) : AbstractEditMappingService(documentMappingService, filePathService)
2222
{
23+
private readonly IFilePathService _filePathService = filePathService;
2324
private readonly IDocumentContextFactory _documentContextFactory = documentContextFactory;
2425

2526
protected override bool TryGetDocumentContext(IDocumentSnapshot contextDocumentSnapshot, Uri razorDocumentUri, VSProjectContext? projectContext, [NotNullWhen(true)] out DocumentContext? documentContext)
@@ -31,4 +32,9 @@ protected override bool TryGetDocumentContext(IDocumentSnapshot contextDocumentS
3132

3233
return true;
3334
}
35+
36+
protected override Task<Uri?> GetRazorDocumentUriAsync(IDocumentSnapshot contextDocumentSnapshot, Uri uri, CancellationToken cancellationToken)
37+
{
38+
return Task.FromResult<Uri?>(_filePathService.GetRazorDocumentUri(uri));
39+
}
3440
}

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/DocumentMapping/AbstractEditMappingService.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,29 +53,34 @@ private async Task<Dictionary<string, TextEdit[]>> RemapDocumentEditsAsync(IDocu
5353

5454
foreach (var (uriString, edits) in changes)
5555
{
56-
var uri = new Uri(uriString);
56+
var generatedDocumentUri = new Uri(uriString);
5757

5858
// Check if the edit is actually for a generated document, because if not we don't need to do anything
59-
if (!_filePathService.IsVirtualDocumentUri(uri))
59+
if (!_filePathService.IsVirtualDocumentUri(generatedDocumentUri))
6060
{
6161
remappedChanges[uriString] = edits;
6262
continue;
6363
}
6464

65-
if (!TryGetDocumentContext(contextDocumentSnapshot, uri, projectContext: null, out var documentContext))
65+
var razorDocumentUri = await GetRazorDocumentUriAsync(contextDocumentSnapshot, generatedDocumentUri, cancellationToken).ConfigureAwait(false);
66+
if (razorDocumentUri is null)
67+
{
68+
continue;
69+
}
70+
71+
if (!TryGetDocumentContext(contextDocumentSnapshot, razorDocumentUri, projectContext: null, out var documentContext))
6672
{
6773
continue;
6874
}
6975

7076
var codeDocument = await documentContext.GetCodeDocumentAsync(cancellationToken).ConfigureAwait(false);
71-
var remappedEdits = RemapTextEditsCore(uri, codeDocument, edits);
77+
var remappedEdits = RemapTextEditsCore(generatedDocumentUri, codeDocument, edits);
7278
if (remappedEdits.Length == 0)
7379
{
7480
// Nothing to do.
7581
continue;
7682
}
7783

78-
var razorDocumentUri = _filePathService.GetRazorDocumentUri(uri);
7984
remappedChanges[razorDocumentUri.AbsoluteUri] = remappedEdits;
8085
}
8186

@@ -123,7 +128,11 @@ private async Task<TextDocumentEdit[]> RemapTextDocumentEditsAsync(IDocumentSnap
123128
continue;
124129
}
125130

126-
var razorDocumentUri = _filePathService.GetRazorDocumentUri(generatedDocumentUri);
131+
var razorDocumentUri = await GetRazorDocumentUriAsync(contextDocumentSnapshot, generatedDocumentUri, cancellationToken).ConfigureAwait(false);
132+
if (razorDocumentUri is null)
133+
{
134+
continue;
135+
}
127136

128137
if (!TryGetDocumentContext(contextDocumentSnapshot, razorDocumentUri, entry.TextDocument.GetProjectContext(), out var documentContext))
129138
{
@@ -154,4 +163,6 @@ private async Task<TextDocumentEdit[]> RemapTextDocumentEditsAsync(IDocumentSnap
154163
}
155164

156165
protected abstract bool TryGetDocumentContext(IDocumentSnapshot contextDocumentSnapshot, Uri razorDocumentUri, VSProjectContext? projectContext, [NotNullWhen(true)] out DocumentContext? documentContext);
166+
167+
protected abstract Task<Uri?> GetRazorDocumentUriAsync(IDocumentSnapshot contextDocumentSnapshot, Uri uri, CancellationToken cancellationToken);
157168
}

src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/DocumentMapping/RemoteDocumentMappingService.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22
// Licensed under the MIT license. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Collections.Generic;
65
using System.Composition;
76
using System.Threading;
87
using System.Threading.Tasks;
9-
using Microsoft.AspNetCore.Razor;
10-
using Microsoft.CodeAnalysis.ExternalAccess.Razor;
118
using Microsoft.CodeAnalysis.Razor.DocumentMapping;
129
using Microsoft.CodeAnalysis.Razor.Logging;
1310
using Microsoft.CodeAnalysis.Razor.Workspaces;
@@ -55,11 +52,8 @@ internal sealed class RemoteDocumentMappingService(
5552

5653
if (TryMapToHostDocumentRange(razorCodeDocument.GetRequiredCSharpDocument(), generatedDocumentRange, MappingBehavior.Strict, out var mappedRange))
5754
{
58-
var solution = project.Solution;
59-
var filePath = razorCodeDocument.Source.FilePath;
60-
var documentId = solution.GetDocumentIdsWithFilePath(filePath).First();
61-
var document = solution.GetAdditionalDocument(documentId).AssumeNotNull();
62-
return (document.CreateUri(), mappedRange);
55+
var razorDocumentUri = project.Solution.GetRazorDocumentUri(razorCodeDocument);
56+
return (razorDocumentUri, mappedRange);
6357
}
6458

6559
return (generatedDocumentUri, generatedDocumentRange);

src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/DocumentMapping/RemoteEditMappingService.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using System;
55
using System.Composition;
66
using System.Diagnostics.CodeAnalysis;
7+
using System.Threading;
8+
using System.Threading.Tasks;
79
using Microsoft.CodeAnalysis.Razor.DocumentMapping;
810
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
911
using Microsoft.CodeAnalysis.Razor.Workspaces;
@@ -39,4 +41,21 @@ protected override bool TryGetDocumentContext(IDocumentSnapshot contextDocumentS
3941
documentContext = new RemoteDocumentContext(razorDocumentUri, razorDocumentSnapshot);
4042
return true;
4143
}
44+
45+
protected override async Task<Uri?> GetRazorDocumentUriAsync(IDocumentSnapshot contextDocumentSnapshot, Uri generatedDocumentUri, CancellationToken cancellationToken)
46+
{
47+
if (contextDocumentSnapshot is not RemoteDocumentSnapshot originSnapshot)
48+
{
49+
throw new InvalidOperationException("RemoteEditMappingService can only be used with RemoteDocumentSnapshot instances.");
50+
}
51+
52+
var project = originSnapshot.TextDocument.Project;
53+
var razorCodeDocument = await _snapshotManager.GetSnapshot(project).TryGetCodeDocumentFromGeneratedDocumentUriAsync(generatedDocumentUri, cancellationToken).ConfigureAwait(false);
54+
if (razorCodeDocument is null)
55+
{
56+
return null;
57+
}
58+
59+
return project.Solution.GetRazorDocumentUri(razorCodeDocument);
60+
}
4261
}

src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/ProjectSystem/Extensions.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
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;
45
using System.Linq;
56
using Microsoft.AspNetCore.Razor;
7+
using Microsoft.AspNetCore.Razor.Language;
8+
using Microsoft.CodeAnalysis.ExternalAccess.Razor;
69

710
namespace Microsoft.CodeAnalysis.Remote.Razor.ProjectSystem;
811

@@ -26,4 +29,12 @@ document.FilePath is string filePath &&
2629

2730
public static bool ContainsRazorDocuments(this Project project)
2831
=> project.AdditionalDocuments.Any(static d => d.IsRazorDocument());
32+
33+
public static Uri GetRazorDocumentUri(this Solution solution, RazorCodeDocument codeDocument)
34+
{
35+
var filePath = codeDocument.Source.FilePath;
36+
var documentId = solution.GetDocumentIdsWithFilePath(filePath).First();
37+
var document = solution.GetAdditionalDocument(documentId).AssumeNotNull();
38+
return document.CreateUri();
39+
}
2940
}

0 commit comments

Comments
 (0)