Skip to content

Commit 035acab

Browse files
authored
Cohost span mapping (#12055)
Fixes #12054 Part of #9519 Needs ~dotnet/roslyn#79604 and~ dotnet/roslyn#79677 to insert before this will build This PR implements the workspace version of our span mapping service, since the old one is hooked up via a document service, which we can't do in cohosting.
2 parents 7b7e235 + 077fe0f commit 035acab

File tree

18 files changed

+600
-107
lines changed

18 files changed

+600
-107
lines changed

eng/Version.Details.props

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,27 @@ This file should be imported by eng/Versions.props
77
<Project>
88
<PropertyGroup>
99
<!-- dotnet/roslyn dependencies -->
10-
<MicrosoftCodeAnalysisAnalyzersPackageVersion>5.0.0-2.25375.9</MicrosoftCodeAnalysisAnalyzersPackageVersion>
11-
<MicrosoftCodeAnalysisCommonPackageVersion>5.0.0-2.25375.9</MicrosoftCodeAnalysisCommonPackageVersion>
12-
<MicrosoftCodeAnalysisCSharpPackageVersion>5.0.0-2.25375.9</MicrosoftCodeAnalysisCSharpPackageVersion>
13-
<MicrosoftCodeAnalysisCSharpEditorFeaturesPackageVersion>5.0.0-2.25375.9</MicrosoftCodeAnalysisCSharpEditorFeaturesPackageVersion>
14-
<MicrosoftCodeAnalysisCSharpFeaturesPackageVersion>5.0.0-2.25375.9</MicrosoftCodeAnalysisCSharpFeaturesPackageVersion>
15-
<MicrosoftCodeAnalysisCSharpWorkspacesPackageVersion>5.0.0-2.25375.9</MicrosoftCodeAnalysisCSharpWorkspacesPackageVersion>
16-
<MicrosoftCodeAnalysisEditorFeaturesPackageVersion>5.0.0-2.25375.9</MicrosoftCodeAnalysisEditorFeaturesPackageVersion>
17-
<MicrosoftCodeAnalysisEditorFeaturesCommonPackageVersion>5.0.0-2.25375.9</MicrosoftCodeAnalysisEditorFeaturesCommonPackageVersion>
18-
<MicrosoftCodeAnalysisEditorFeaturesTextPackageVersion>5.0.0-2.25375.9</MicrosoftCodeAnalysisEditorFeaturesTextPackageVersion>
19-
<MicrosoftCodeAnalysisExternalAccessFSharpPackageVersion>5.0.0-2.25375.9</MicrosoftCodeAnalysisExternalAccessFSharpPackageVersion>
20-
<MicrosoftCodeAnalysisExternalAccessRazorEditorFeaturesPackageVersion>5.0.0-2.25375.9</MicrosoftCodeAnalysisExternalAccessRazorEditorFeaturesPackageVersion>
21-
<MicrosoftCodeAnalysisExternalAccessRazorFeaturesPackageVersion>5.0.0-2.25375.9</MicrosoftCodeAnalysisExternalAccessRazorFeaturesPackageVersion>
22-
<MicrosoftCodeAnalysisLanguageServerProtocolPackageVersion>5.0.0-2.25375.9</MicrosoftCodeAnalysisLanguageServerProtocolPackageVersion>
23-
<MicrosoftCodeAnalysisRemoteServiceHubPackageVersion>5.0.0-2.25375.9</MicrosoftCodeAnalysisRemoteServiceHubPackageVersion>
24-
<MicrosoftCodeAnalysisTestUtilitiesPackageVersion>5.0.0-2.25375.9</MicrosoftCodeAnalysisTestUtilitiesPackageVersion>
25-
<MicrosoftCodeAnalysisVisualBasicWorkspacesPackageVersion>5.0.0-2.25375.9</MicrosoftCodeAnalysisVisualBasicWorkspacesPackageVersion>
26-
<MicrosoftCodeAnalysisWorkspacesCommonPackageVersion>5.0.0-2.25375.9</MicrosoftCodeAnalysisWorkspacesCommonPackageVersion>
27-
<MicrosoftCodeAnalysisWorkspacesMSBuildPackageVersion>5.0.0-2.25375.9</MicrosoftCodeAnalysisWorkspacesMSBuildPackageVersion>
28-
<MicrosoftCommonLanguageServerProtocolFrameworkPackageVersion>5.0.0-2.25375.9</MicrosoftCommonLanguageServerProtocolFrameworkPackageVersion>
29-
<MicrosoftNetCompilersToolsetPackageVersion>5.0.0-2.25375.9</MicrosoftNetCompilersToolsetPackageVersion>
30-
<MicrosoftVisualStudioLanguageServicesPackageVersion>5.0.0-2.25375.9</MicrosoftVisualStudioLanguageServicesPackageVersion>
10+
<MicrosoftCodeAnalysisAnalyzersPackageVersion>5.0.0-2.25380.11</MicrosoftCodeAnalysisAnalyzersPackageVersion>
11+
<MicrosoftCodeAnalysisCommonPackageVersion>5.0.0-2.25380.11</MicrosoftCodeAnalysisCommonPackageVersion>
12+
<MicrosoftCodeAnalysisCSharpPackageVersion>5.0.0-2.25380.11</MicrosoftCodeAnalysisCSharpPackageVersion>
13+
<MicrosoftCodeAnalysisCSharpEditorFeaturesPackageVersion>5.0.0-2.25380.11</MicrosoftCodeAnalysisCSharpEditorFeaturesPackageVersion>
14+
<MicrosoftCodeAnalysisCSharpFeaturesPackageVersion>5.0.0-2.25380.11</MicrosoftCodeAnalysisCSharpFeaturesPackageVersion>
15+
<MicrosoftCodeAnalysisCSharpWorkspacesPackageVersion>5.0.0-2.25380.11</MicrosoftCodeAnalysisCSharpWorkspacesPackageVersion>
16+
<MicrosoftCodeAnalysisEditorFeaturesPackageVersion>5.0.0-2.25380.11</MicrosoftCodeAnalysisEditorFeaturesPackageVersion>
17+
<MicrosoftCodeAnalysisEditorFeaturesCommonPackageVersion>5.0.0-2.25380.11</MicrosoftCodeAnalysisEditorFeaturesCommonPackageVersion>
18+
<MicrosoftCodeAnalysisEditorFeaturesTextPackageVersion>5.0.0-2.25380.11</MicrosoftCodeAnalysisEditorFeaturesTextPackageVersion>
19+
<MicrosoftCodeAnalysisExternalAccessFSharpPackageVersion>5.0.0-2.25380.11</MicrosoftCodeAnalysisExternalAccessFSharpPackageVersion>
20+
<MicrosoftCodeAnalysisExternalAccessRazorEditorFeaturesPackageVersion>5.0.0-2.25380.11</MicrosoftCodeAnalysisExternalAccessRazorEditorFeaturesPackageVersion>
21+
<MicrosoftCodeAnalysisExternalAccessRazorFeaturesPackageVersion>5.0.0-2.25380.11</MicrosoftCodeAnalysisExternalAccessRazorFeaturesPackageVersion>
22+
<MicrosoftCodeAnalysisLanguageServerProtocolPackageVersion>5.0.0-2.25380.11</MicrosoftCodeAnalysisLanguageServerProtocolPackageVersion>
23+
<MicrosoftCodeAnalysisRemoteServiceHubPackageVersion>5.0.0-2.25380.11</MicrosoftCodeAnalysisRemoteServiceHubPackageVersion>
24+
<MicrosoftCodeAnalysisTestUtilitiesPackageVersion>5.0.0-2.25380.11</MicrosoftCodeAnalysisTestUtilitiesPackageVersion>
25+
<MicrosoftCodeAnalysisVisualBasicWorkspacesPackageVersion>5.0.0-2.25380.11</MicrosoftCodeAnalysisVisualBasicWorkspacesPackageVersion>
26+
<MicrosoftCodeAnalysisWorkspacesCommonPackageVersion>5.0.0-2.25380.11</MicrosoftCodeAnalysisWorkspacesCommonPackageVersion>
27+
<MicrosoftCodeAnalysisWorkspacesMSBuildPackageVersion>5.0.0-2.25380.11</MicrosoftCodeAnalysisWorkspacesMSBuildPackageVersion>
28+
<MicrosoftCommonLanguageServerProtocolFrameworkPackageVersion>5.0.0-2.25380.11</MicrosoftCommonLanguageServerProtocolFrameworkPackageVersion>
29+
<MicrosoftNetCompilersToolsetPackageVersion>5.0.0-2.25380.11</MicrosoftNetCompilersToolsetPackageVersion>
30+
<MicrosoftVisualStudioLanguageServicesPackageVersion>5.0.0-2.25380.11</MicrosoftVisualStudioLanguageServicesPackageVersion>
3131
<!-- dotnet/arcade dependencies -->
3232
<MicrosoftDotNetArcadeSdkPackageVersion>9.0.0-beta.25255.5</MicrosoftDotNetArcadeSdkPackageVersion>
3333
<!-- dotnet/aspnetcore dependencies -->

eng/Version.Details.xml

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,89 +2,89 @@
22
<Dependencies>
33
<Source Uri="https://github.com/dotnet/dotnet" Mapping="razor" Sha="8b29a1682219da555ee27e4fdda55dc3884b316f" BarId="277635" />
44
<ProductDependencies>
5-
<Dependency Name="Microsoft.Net.Compilers.Toolset" Version="5.0.0-2.25375.9">
5+
<Dependency Name="Microsoft.Net.Compilers.Toolset" Version="5.0.0-2.25380.11">
66
<Uri>https://github.com/dotnet/roslyn</Uri>
7-
<Sha>e644f864fb302c088d751f6face7bd54cda17259</Sha>
7+
<Sha>512fe5197056a877a86dff3054d8bd0028af13ed</Sha>
88
</Dependency>
9-
<Dependency Name="Microsoft.CommonLanguageServerProtocol.Framework" Version="5.0.0-2.25375.9">
9+
<Dependency Name="Microsoft.CommonLanguageServerProtocol.Framework" Version="5.0.0-2.25380.11">
1010
<Uri>https://github.com/dotnet/roslyn</Uri>
11-
<Sha>e644f864fb302c088d751f6face7bd54cda17259</Sha>
11+
<Sha>512fe5197056a877a86dff3054d8bd0028af13ed</Sha>
1212
</Dependency>
13-
<Dependency Name="Microsoft.CodeAnalysis.Common" Version="5.0.0-2.25375.9">
13+
<Dependency Name="Microsoft.CodeAnalysis.Common" Version="5.0.0-2.25380.11">
1414
<Uri>https://github.com/dotnet/roslyn</Uri>
15-
<Sha>e644f864fb302c088d751f6face7bd54cda17259</Sha>
15+
<Sha>512fe5197056a877a86dff3054d8bd0028af13ed</Sha>
1616
</Dependency>
17-
<Dependency Name="Microsoft.CodeAnalysis.CSharp" Version="5.0.0-2.25375.9">
17+
<Dependency Name="Microsoft.CodeAnalysis.CSharp" Version="5.0.0-2.25380.11">
1818
<Uri>https://github.com/dotnet/roslyn</Uri>
19-
<Sha>e644f864fb302c088d751f6face7bd54cda17259</Sha>
19+
<Sha>512fe5197056a877a86dff3054d8bd0028af13ed</Sha>
2020
</Dependency>
21-
<Dependency Name="Microsoft.CodeAnalysis.CSharp.EditorFeatures" Version="5.0.0-2.25375.9">
21+
<Dependency Name="Microsoft.CodeAnalysis.CSharp.EditorFeatures" Version="5.0.0-2.25380.11">
2222
<Uri>https://github.com/dotnet/roslyn</Uri>
23-
<Sha>e644f864fb302c088d751f6face7bd54cda17259</Sha>
23+
<Sha>512fe5197056a877a86dff3054d8bd0028af13ed</Sha>
2424
</Dependency>
25-
<Dependency Name="Microsoft.CodeAnalysis.CSharp.Features" Version="5.0.0-2.25375.9">
25+
<Dependency Name="Microsoft.CodeAnalysis.CSharp.Features" Version="5.0.0-2.25380.11">
2626
<Uri>https://github.com/dotnet/roslyn</Uri>
27-
<Sha>e644f864fb302c088d751f6face7bd54cda17259</Sha>
27+
<Sha>512fe5197056a877a86dff3054d8bd0028af13ed</Sha>
2828
</Dependency>
29-
<Dependency Name="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="5.0.0-2.25375.9">
29+
<Dependency Name="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="5.0.0-2.25380.11">
3030
<Uri>https://github.com/dotnet/roslyn</Uri>
31-
<Sha>e644f864fb302c088d751f6face7bd54cda17259</Sha>
31+
<Sha>512fe5197056a877a86dff3054d8bd0028af13ed</Sha>
3232
</Dependency>
33-
<Dependency Name="Microsoft.CodeAnalysis.EditorFeatures" Version="5.0.0-2.25375.9">
33+
<Dependency Name="Microsoft.CodeAnalysis.EditorFeatures" Version="5.0.0-2.25380.11">
3434
<Uri>https://github.com/dotnet/roslyn</Uri>
35-
<Sha>e644f864fb302c088d751f6face7bd54cda17259</Sha>
35+
<Sha>512fe5197056a877a86dff3054d8bd0028af13ed</Sha>
3636
</Dependency>
37-
<Dependency Name="Microsoft.CodeAnalysis.EditorFeatures.Common" Version="5.0.0-2.25375.9">
37+
<Dependency Name="Microsoft.CodeAnalysis.EditorFeatures.Common" Version="5.0.0-2.25380.11">
3838
<Uri>https://github.com/dotnet/roslyn</Uri>
39-
<Sha>e644f864fb302c088d751f6face7bd54cda17259</Sha>
39+
<Sha>512fe5197056a877a86dff3054d8bd0028af13ed</Sha>
4040
</Dependency>
41-
<Dependency Name="Microsoft.CodeAnalysis.EditorFeatures.Text" Version="5.0.0-2.25375.9">
41+
<Dependency Name="Microsoft.CodeAnalysis.EditorFeatures.Text" Version="5.0.0-2.25380.11">
4242
<Uri>https://github.com/dotnet/roslyn</Uri>
43-
<Sha>e644f864fb302c088d751f6face7bd54cda17259</Sha>
43+
<Sha>512fe5197056a877a86dff3054d8bd0028af13ed</Sha>
4444
</Dependency>
45-
<Dependency Name="Microsoft.CodeAnalysis.Remote.ServiceHub" Version="5.0.0-2.25375.9">
45+
<Dependency Name="Microsoft.CodeAnalysis.Remote.ServiceHub" Version="5.0.0-2.25380.11">
4646
<Uri>https://github.com/dotnet/roslyn</Uri>
47-
<Sha>e644f864fb302c088d751f6face7bd54cda17259</Sha>
47+
<Sha>512fe5197056a877a86dff3054d8bd0028af13ed</Sha>
4848
</Dependency>
49-
<Dependency Name="Microsoft.CodeAnalysis.VisualBasic.Workspaces" Version="5.0.0-2.25375.9">
49+
<Dependency Name="Microsoft.CodeAnalysis.VisualBasic.Workspaces" Version="5.0.0-2.25380.11">
5050
<Uri>https://github.com/dotnet/roslyn</Uri>
51-
<Sha>e644f864fb302c088d751f6face7bd54cda17259</Sha>
51+
<Sha>512fe5197056a877a86dff3054d8bd0028af13ed</Sha>
5252
</Dependency>
53-
<Dependency Name="Microsoft.CodeAnalysis.Workspaces.Common" Version="5.0.0-2.25375.9">
53+
<Dependency Name="Microsoft.CodeAnalysis.Workspaces.Common" Version="5.0.0-2.25380.11">
5454
<Uri>https://github.com/dotnet/roslyn</Uri>
55-
<Sha>e644f864fb302c088d751f6face7bd54cda17259</Sha>
55+
<Sha>512fe5197056a877a86dff3054d8bd0028af13ed</Sha>
5656
</Dependency>
57-
<Dependency Name="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="5.0.0-2.25375.9">
57+
<Dependency Name="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="5.0.0-2.25380.11">
5858
<Uri>https://github.com/dotnet/roslyn</Uri>
59-
<Sha>e644f864fb302c088d751f6face7bd54cda17259</Sha>
59+
<Sha>512fe5197056a877a86dff3054d8bd0028af13ed</Sha>
6060
</Dependency>
61-
<Dependency Name="Microsoft.VisualStudio.LanguageServices" Version="5.0.0-2.25375.9">
61+
<Dependency Name="Microsoft.VisualStudio.LanguageServices" Version="5.0.0-2.25380.11">
6262
<Uri>https://github.com/dotnet/roslyn</Uri>
63-
<Sha>e644f864fb302c088d751f6face7bd54cda17259</Sha>
63+
<Sha>512fe5197056a877a86dff3054d8bd0028af13ed</Sha>
6464
</Dependency>
65-
<Dependency Name="Microsoft.CodeAnalysis.Test.Utilities" Version="5.0.0-2.25375.9">
65+
<Dependency Name="Microsoft.CodeAnalysis.Test.Utilities" Version="5.0.0-2.25380.11">
6666
<Uri>https://github.com/dotnet/roslyn</Uri>
67-
<Sha>e644f864fb302c088d751f6face7bd54cda17259</Sha>
67+
<Sha>512fe5197056a877a86dff3054d8bd0028af13ed</Sha>
6868
</Dependency>
69-
<Dependency Name="Microsoft.CodeAnalysis.ExternalAccess.Razor.Features" Version="5.0.0-2.25375.9">
69+
<Dependency Name="Microsoft.CodeAnalysis.ExternalAccess.Razor.Features" Version="5.0.0-2.25380.11">
7070
<Uri>https://github.com/dotnet/roslyn</Uri>
71-
<Sha>e644f864fb302c088d751f6face7bd54cda17259</Sha>
71+
<Sha>512fe5197056a877a86dff3054d8bd0028af13ed</Sha>
7272
</Dependency>
73-
<Dependency Name="Microsoft.CodeAnalysis.ExternalAccess.Razor.EditorFeatures" Version="5.0.0-2.25375.9">
73+
<Dependency Name="Microsoft.CodeAnalysis.ExternalAccess.Razor.EditorFeatures" Version="5.0.0-2.25380.11">
7474
<Uri>https://github.com/dotnet/roslyn</Uri>
75-
<Sha>e644f864fb302c088d751f6face7bd54cda17259</Sha>
75+
<Sha>512fe5197056a877a86dff3054d8bd0028af13ed</Sha>
7676
</Dependency>
77-
<Dependency Name="Microsoft.CodeAnalysis.LanguageServer.Protocol" Version="5.0.0-2.25375.9">
77+
<Dependency Name="Microsoft.CodeAnalysis.LanguageServer.Protocol" Version="5.0.0-2.25380.11">
7878
<Uri>https://github.com/dotnet/roslyn</Uri>
79-
<Sha>e644f864fb302c088d751f6face7bd54cda17259</Sha>
79+
<Sha>512fe5197056a877a86dff3054d8bd0028af13ed</Sha>
8080
</Dependency>
81-
<Dependency Name="Microsoft.CodeAnalysis.ExternalAccess.FSharp" Version="5.0.0-2.25375.9">
81+
<Dependency Name="Microsoft.CodeAnalysis.ExternalAccess.FSharp" Version="5.0.0-2.25380.11">
8282
<Uri>https://github.com/dotnet/roslyn</Uri>
83-
<Sha>e644f864fb302c088d751f6face7bd54cda17259</Sha>
83+
<Sha>512fe5197056a877a86dff3054d8bd0028af13ed</Sha>
8484
</Dependency>
85-
<Dependency Name="Microsoft.CodeAnalysis.Analyzers" Version="5.0.0-2.25375.9">
85+
<Dependency Name="Microsoft.CodeAnalysis.Analyzers" Version="5.0.0-2.25380.11">
8686
<Uri>https://github.com/dotnet/roslyn</Uri>
87-
<Sha>e644f864fb302c088d751f6face7bd54cda17259</Sha>
87+
<Sha>512fe5197056a877a86dff3054d8bd0028af13ed</Sha>
8888
</Dependency>
8989
</ProductDependencies>
9090
<ToolsetDependencies>

eng/targets/Services.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,6 @@
4141
<ServiceHubService Include="Microsoft.VisualStudio.Razor.InlineCompletion" ClassName="Microsoft.CodeAnalysis.Remote.Razor.RemoteInlineCompletionService+Factory" />
4242
<ServiceHubService Include="Microsoft.VisualStudio.Razor.DebugInfo" ClassName="Microsoft.CodeAnalysis.Remote.Razor.RemoteDebugInfoService+Factory" />
4343
<ServiceHubService Include="Microsoft.VisualStudio.Razor.WrapWithTag" ClassName="Microsoft.CodeAnalysis.Remote.Razor.RemoteWrapWithTagService+Factory" />
44+
<ServiceHubService Include="Microsoft.VisualStudio.Razor.SpanMapping" ClassName="Microsoft.CodeAnalysis.Remote.Razor.RemoteSpanMappingService+Factory" />
4445
</ItemGroup>
4546
</Project>

src/Razor/src/Microsoft.CodeAnalysis.Razor.CohostingShared/Microsoft.CodeAnalysis.Razor.CohostingShared.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<Compile Include="$(MSBuildThisFileDirectory)OnAutoInsert\CohostOnAutoInsertEndpoint.cs" />
3636
<Compile Include="$(MSBuildThisFileDirectory)OnAutoInsert\CohostOnAutoInsertTriggerCharacterProviders.cs" />
3737
<Compile Include="$(MSBuildThisFileDirectory)RazorAnalyzerAssemblyRedirector.cs" />
38+
<Compile Include="$(MSBuildThisFileDirectory)RazorSourceGeneratedDocumentSpanMappingService.cs" />
3839
<Compile Include="$(MSBuildThisFileDirectory)Rename\CohostRenameEndpoint.cs" />
3940
<Compile Include="$(MSBuildThisFileDirectory)SignatureHelp\CohostSignatureHelpEndpoint.cs" />
4041
<Compile Include="$(MSBuildThisFileDirectory)FoldingRange\CohostFoldingRangeEndpoint.cs" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Collections.Immutable;
5+
using System.ComponentModel.Composition;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.ExternalAccess.Razor;
9+
using Microsoft.CodeAnalysis.Razor.Remote;
10+
using Microsoft.CodeAnalysis.Text;
11+
12+
namespace Microsoft.CodeAnalysis.Razor.CohostingShared;
13+
14+
[Export(typeof(IRazorSourceGeneratedDocumentSpanMappingService))]
15+
[method: ImportingConstructor]
16+
internal sealed class RazorSourceGeneratedDocumentSpanMappingService(IRemoteServiceInvoker remoteServiceInvoker) : IRazorSourceGeneratedDocumentSpanMappingService
17+
{
18+
private readonly IRemoteServiceInvoker _remoteServiceInvoker = remoteServiceInvoker;
19+
20+
public async Task<ImmutableArray<RazorMappedEditResult>> GetMappedTextChangesAsync(SourceGeneratedDocument oldDocument, SourceGeneratedDocument newDocument, CancellationToken cancellationToken)
21+
{
22+
if (!oldDocument.IsRazorSourceGeneratedDocument() || !newDocument.IsRazorSourceGeneratedDocument())
23+
{
24+
// If either document is not a Razor source generated document, we cannot map text changes.
25+
return [];
26+
}
27+
28+
// We have to get the text changes on this side, because we're dealing with changed source generated documents, and we can't
29+
// expect to transfer the Ids over to OOP and see the same changes
30+
var changes = await newDocument.GetTextChangesAsync(oldDocument, cancellationToken).ConfigureAwait(false);
31+
var changesArray = changes.ToImmutableArray();
32+
if (changesArray.IsDefaultOrEmpty)
33+
{
34+
return [];
35+
}
36+
37+
return await _remoteServiceInvoker.TryInvokeAsync<IRemoteSpanMappingService, ImmutableArray<RazorMappedEditResult>>(
38+
oldDocument.Project.Solution,
39+
(service, solutionInfo, cancellationToken) => service.MapTextChangesAsync(solutionInfo, newDocument.Id, changesArray, cancellationToken),
40+
cancellationToken).ConfigureAwait(false);
41+
}
42+
43+
public async Task<ImmutableArray<RazorMappedSpanResult>> MapSpansAsync(SourceGeneratedDocument document, ImmutableArray<TextSpan> spans, CancellationToken cancellationToken)
44+
{
45+
if (!document.IsRazorSourceGeneratedDocument())
46+
{
47+
// If the document is not a Razor source generated document, we cannot map spans.
48+
return [];
49+
}
50+
51+
return await _remoteServiceInvoker.TryInvokeAsync<IRemoteSpanMappingService, ImmutableArray<RazorMappedSpanResult>>(
52+
document.Project.Solution,
53+
(service, solutionInfo, cancellationToken) => service.MapSpansAsync(solutionInfo, document.Id, spans, cancellationToken),
54+
cancellationToken).ConfigureAwait(false);
55+
}
56+
}

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,49 @@
77
using System.Linq;
88
using System.Threading;
99
using System.Threading.Tasks;
10+
using Microsoft.AspNetCore.Razor.Language;
1011
using Microsoft.AspNetCore.Razor.PooledObjects;
1112
using Microsoft.AspNetCore.Razor.Utilities;
1213
using Microsoft.CodeAnalysis.Razor.Formatting;
1314
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
1415
using Microsoft.CodeAnalysis.Razor.Protocol;
1516
using Microsoft.CodeAnalysis.Razor.Telemetry;
17+
using Microsoft.CodeAnalysis.Text;
1618

1719
namespace Microsoft.CodeAnalysis.Razor.DocumentMapping;
1820

1921
internal static partial class RazorEditHelper
2022
{
23+
internal static bool TryGetMappedSpans(TextSpan span, SourceText source, RazorCSharpDocument output, out LinePositionSpan linePositionSpan, out TextSpan mappedSpan)
24+
{
25+
foreach (var mapping in output.SourceMappings)
26+
{
27+
var original = mapping.OriginalSpan.AsTextSpan();
28+
var generated = mapping.GeneratedSpan.AsTextSpan();
29+
30+
if (!generated.Contains(span))
31+
{
32+
// If the search span isn't contained within the generated span, it is not a match.
33+
// A C# identifier won't cover multiple generated spans.
34+
continue;
35+
}
36+
37+
var leftOffset = span.Start - generated.Start;
38+
var rightOffset = span.End - generated.End;
39+
if (leftOffset >= 0 && rightOffset <= 0)
40+
{
41+
// This span mapping contains the span.
42+
mappedSpan = new TextSpan(original.Start + leftOffset, (original.End + rightOffset) - (original.Start + leftOffset));
43+
linePositionSpan = source.GetLinePositionSpan(mappedSpan);
44+
return true;
45+
}
46+
}
47+
48+
mappedSpan = default;
49+
linePositionSpan = default;
50+
return false;
51+
}
52+
2153
/// <summary>
2254
/// Maps the given text edits for a razor file based on changes in csharp. It special
2355
/// cases usings directives to insure they are added correctly. All other edits

0 commit comments

Comments
 (0)