Skip to content

Commit 0138c9f

Browse files
Copilotdavidwengier
andcommitted
Complete WrapWithTag cohosting implementation with text edit fixing
Co-authored-by: davidwengier <[email protected]>
1 parent e5417bd commit 0138c9f

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Remote/IRemoteWrapWithTagService.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
using System.Threading.Tasks;
66
using Microsoft.CodeAnalysis.ExternalAccess.Razor;
77
using Microsoft.CodeAnalysis.Razor.Protocol;
8+
using Microsoft.CodeAnalysis.Text;
89

910
namespace Microsoft.CodeAnalysis.Razor.Remote;
1011

1112
internal interface IRemoteWrapWithTagService
1213
{
1314
ValueTask<RemoteResponse<bool>> IsValidWrapWithTagLocationAsync(RazorPinnedSolutionInfoWrapper solutionInfo, DocumentId razorDocumentId, LspRange range, CancellationToken cancellationToken);
15+
16+
ValueTask<RemoteResponse<TextEdit[]?>> FixHtmlTextEditsAsync(RazorPinnedSolutionInfoWrapper solutionInfo, DocumentId razorDocumentId, TextEdit[] textEdits, CancellationToken cancellationToken);
1417
}

src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/WrapWithTag/RemoteWrapWithTagService.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
using Microsoft.AspNetCore.Razor.Language;
77
using Microsoft.AspNetCore.Razor.Language.Syntax;
88
using Microsoft.CodeAnalysis.ExternalAccess.Razor;
9+
using Microsoft.CodeAnalysis.Razor.Formatting;
910
using Microsoft.CodeAnalysis.Razor.Protocol;
1011
using Microsoft.CodeAnalysis.Razor.Remote;
1112
using Microsoft.CodeAnalysis.Razor.Workspaces;
1213
using Microsoft.CodeAnalysis.Remote.Razor.ProjectSystem;
1314
using Microsoft.CodeAnalysis.Text;
1415
using Response = Microsoft.CodeAnalysis.Razor.Remote.RemoteResponse<bool>;
16+
using TextEditResponse = Microsoft.CodeAnalysis.Razor.Remote.RemoteResponse<Microsoft.CodeAnalysis.Text.TextEdit[]?>;
1517

1618
namespace Microsoft.CodeAnalysis.Remote.Razor;
1719

@@ -34,6 +36,17 @@ public ValueTask<Response> IsValidWrapWithTagLocationAsync(
3436
context => IsValidWrapWithTagLocationAsync(context, range, cancellationToken),
3537
cancellationToken);
3638

39+
public ValueTask<TextEditResponse> FixHtmlTextEditsAsync(
40+
RazorPinnedSolutionInfoWrapper solutionInfo,
41+
DocumentId razorDocumentId,
42+
TextEdit[] textEdits,
43+
CancellationToken cancellationToken)
44+
=> RunServiceAsync(
45+
solutionInfo,
46+
razorDocumentId,
47+
context => FixHtmlTextEditsAsync(context, textEdits, cancellationToken),
48+
cancellationToken);
49+
3750
private async ValueTask<Response> IsValidWrapWithTagLocationAsync(
3851
RemoteDocumentContext context,
3952
LspRange range,
@@ -114,4 +127,21 @@ private async ValueTask<Response> IsValidWrapWithTagLocationAsync(
114127

115128
return Response.Results(true);
116129
}
130+
131+
private async ValueTask<TextEditResponse> FixHtmlTextEditsAsync(
132+
RemoteDocumentContext context,
133+
TextEdit[] textEdits,
134+
CancellationToken cancellationToken)
135+
{
136+
if (textEdits.Length == 0)
137+
{
138+
return TextEditResponse.Results(textEdits);
139+
}
140+
141+
var codeDocument = await context.GetCodeDocumentAsync(cancellationToken).ConfigureAwait(false);
142+
var htmlSourceText = codeDocument.GetHtmlSourceText();
143+
144+
var fixedEdits = FormattingUtilities.FixHtmlTextEdits(htmlSourceText, textEdits);
145+
return TextEditResponse.Results(fixedEdits);
146+
}
117147
}

src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/LanguageClient/Cohost/CohostWrapWithTagEndpoint.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,19 @@ public ImmutableArray<Registration> GetRegistrations(VSInternalClientCapabilitie
7878
request,
7979
cancellationToken).ConfigureAwait(false);
8080

81-
// TODO: Consider if we need to fix HTML text edits in the cohost scenario
82-
// The original language server implementation calls FormattingUtilities.FixHtmlTextEdits
83-
// but this might not be necessary in the cohost context
81+
if (htmlResponse?.TextEdits is not null)
82+
{
83+
// Fix the HTML text edits to handle any tilde characters in the generated document
84+
var fixedEdits = await _remoteServiceInvoker.TryInvokeAsync<IRemoteWrapWithTagService, RemoteResponse<TextEdit[]?>>(
85+
razorDocument.Project.Solution,
86+
(service, solutionInfo, cancellationToken) => service.FixHtmlTextEditsAsync(solutionInfo, razorDocument.Id, htmlResponse.TextEdits, cancellationToken),
87+
cancellationToken).ConfigureAwait(false);
88+
89+
if (fixedEdits.Result is not null)
90+
{
91+
htmlResponse.TextEdits = fixedEdits.Result;
92+
}
93+
}
8494

8595
return htmlResponse;
8696
}

0 commit comments

Comments
 (0)