-
Notifications
You must be signed in to change notification settings - Fork 212
Add VS Code IMappingService #11760
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add VS Code IMappingService #11760
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
dc942f4
Add an LSP mapping service that can be used in VS Code. We previously
d906fcf
Update roslyn packages
94a2b9b
Add some tests
0faa533
Merge branch 'main' into vscode_mapping_service
edf5127
Fix namespace and name
110b552
Apply suggestions from code review
38e6d96
Early bail for Razor request
ea287fb
Merge branch 'vscode_mapping_service' of https://github.com/ryzngard/…
255955d
Merge branch 'main' into vscode_mapping_service
375aab1
Get rid of tests :( cannot implement IRazorClientManagerService
8fb070e
PR feedback
4a5cc04
Undo version bump
3ad5481
Fix test
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Extensions/LspExtensions_Range.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...azor/src/Microsoft.VisualStudioCode.RazorExtension/Services/LspDocumentServiceProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
using Microsoft.AspNetCore.Razor; | ||
using Microsoft.CodeAnalysis.ExternalAccess.Razor; | ||
|
||
namespace Microsoft.VisualStudioCode.RazorExtension.Services; | ||
|
||
internal sealed class LspDocumentServiceProvider(IRazorClientLanguageServerManager razorClientLanguageServerManager) : IRazorDocumentServiceProvider | ||
{ | ||
public bool CanApplyChange => true; | ||
|
||
public bool SupportDiagnostics => false; | ||
|
||
private IRazorMappingService? _mappingService; | ||
|
||
public TService? GetService<TService>() where TService : class | ||
{ | ||
var serviceType = typeof(TService); | ||
|
||
if (serviceType == typeof(IRazorMappingService)) | ||
{ | ||
var mappingService = _mappingService ?? InterlockedOperations.Initialize(ref _mappingService, CreateMappingService()); | ||
return (TService?)mappingService; | ||
} | ||
|
||
return this as TService; | ||
} | ||
|
||
private IRazorMappingService CreateMappingService() | ||
{ | ||
return new MappingService(razorClientLanguageServerManager); | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
src/Razor/src/Microsoft.VisualStudioCode.RazorExtension/Services/MappingService.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Immutable; | ||
using System.Diagnostics; | ||
using Microsoft.AspNetCore.Razor.PooledObjects; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.ExternalAccess.Razor; | ||
using Microsoft.CodeAnalysis.Razor; | ||
using Microsoft.CodeAnalysis.Razor.Protocol; | ||
using Microsoft.CodeAnalysis.Text; | ||
|
||
namespace Microsoft.VisualStudioCode.RazorExtension.Services; | ||
|
||
internal sealed class MappingService(IRazorClientLanguageServerManager razorClientLanguageServerManager) : IRazorMappingService | ||
{ | ||
private const string RazorMapSpansEndpoint = "razor/mapSpans"; | ||
private const string RazorMapTextChangesEndpoint = "razor/mapTextChanges"; | ||
|
||
private readonly IRazorClientLanguageServerManager _razorClientLanguageServerManager = razorClientLanguageServerManager; | ||
|
||
public async Task<ImmutableArray<RazorMappedSpanResult>> MapSpansAsync(Document document, IEnumerable<TextSpan> spans, CancellationToken cancellationToken) | ||
{ | ||
if (string.IsNullOrWhiteSpace(document.FilePath) || | ||
(spans.TryGetNonEnumeratedCount(out var count) && count == 0)) | ||
{ | ||
return []; | ||
} | ||
|
||
var sourceText = await document.GetTextAsync(cancellationToken).ConfigureAwait(false); | ||
|
||
var mapParams = new RazorMapSpansParams() | ||
{ | ||
CSharpDocument = new() | ||
{ | ||
Uri = new(document.FilePath) | ||
}, | ||
Ranges = [.. spans.Select(sourceText.GetRange)] | ||
}; | ||
|
||
var response = await _razorClientLanguageServerManager.SendRequestAsync<RazorMapSpansParams, RazorMapSpansResponse?>( | ||
RazorMapSpansEndpoint, | ||
mapParams, | ||
cancellationToken).ConfigureAwait(false); | ||
|
||
if (response is not { Spans.Length: > 0, Ranges.Length: > 0 }) | ||
{ | ||
return []; | ||
} | ||
|
||
Debug.Assert(response.Spans.Length == spans.Count(), "The number of mapped spans should match the number of input spans."); | ||
Debug.Assert(response.Ranges.Length == spans.Count(), "The number of mapped ranges should match the number of input spans."); | ||
|
||
using var builder = new PooledArrayBuilder<RazorMappedSpanResult>(response.Spans.Length); | ||
var filePath = response.RazorDocument.Uri.GetDocumentFilePath(); | ||
|
||
for (var i = 0; i < response.Spans.Length; i++) | ||
{ | ||
var span = response.Spans[i]; | ||
var range = response.Ranges[i]; | ||
|
||
if (range.IsUndefined()) | ||
{ | ||
continue; | ||
} | ||
|
||
builder.Add(new RazorMappedSpanResult(filePath, range.ToLinePositionSpan(), span.ToTextSpan())); | ||
ryzngard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
return builder.DrainToImmutable(); | ||
} | ||
|
||
public async Task<ImmutableArray<RazorMappedEditResult>> MapTextChangesAsync(Document oldDocument, Document newDocument, CancellationToken cancellationToken) | ||
{ | ||
if (string.IsNullOrWhiteSpace(newDocument.FilePath)) | ||
{ | ||
return []; | ||
} | ||
|
||
var changes = await newDocument.GetTextChangesAsync(oldDocument, cancellationToken).ConfigureAwait(false); | ||
var textChanges = changes.Select(c => c.ToRazorTextChange()).ToArray(); | ||
|
||
if (textChanges.Length == 0) | ||
{ | ||
return []; | ||
} | ||
|
||
var mapParams = new RazorMapTextChangesParams() | ||
{ | ||
CSharpDocument = new() | ||
{ | ||
Uri = new(newDocument.FilePath) | ||
}, | ||
TextChanges = textChanges | ||
}; | ||
|
||
var response = await _razorClientLanguageServerManager.SendRequestAsync<RazorMapTextChangesParams, RazorMapTextChangesResponse?>( | ||
RazorMapTextChangesEndpoint, | ||
mapParams, | ||
cancellationToken).ConfigureAwait(false); | ||
|
||
if (response is not { MappedTextChanges.Length: > 0 }) | ||
{ | ||
return []; | ||
} | ||
|
||
Debug.Assert(response.MappedTextChanges.Length == changes.Count(), "The number of mapped text changes should match the number of input text changes."); | ||
var filePath = response.RazorDocument.Uri.GetDocumentFilePath(); | ||
var convertedChanges = Array.ConvertAll(response.MappedTextChanges, mappedChange => mappedChange.ToTextChange()); | ||
var result = new RazorMappedEditResult(filePath, convertedChanges); | ||
return [result]; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/Razor/src/Microsoft.VisualStudioCode.RazorExtension/Services/RazorMapSpansParams.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
using System.Text.Json.Serialization; | ||
|
||
namespace Microsoft.VisualStudioCode.RazorExtension.Services; | ||
|
||
internal sealed class RazorMapSpansParams | ||
{ | ||
[JsonPropertyName("csharpDocument")] | ||
public required TextDocumentIdentifier CSharpDocument { get; internal set; } | ||
|
||
[JsonPropertyName("ranges")] | ||
public required LspRange[] Ranges { get; internal set; } | ||
} |
19 changes: 19 additions & 0 deletions
19
src/Razor/src/Microsoft.VisualStudioCode.RazorExtension/Services/RazorMapSpansResponse.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
using System.Text.Json.Serialization; | ||
using Microsoft.CodeAnalysis.Razor.Protocol; | ||
|
||
namespace Microsoft.VisualStudioCode.RazorExtension.Services; | ||
|
||
internal sealed class RazorMapSpansResponse | ||
{ | ||
[JsonPropertyName("ranges")] | ||
public required LspRange[] Ranges { get; set; } | ||
|
||
[JsonPropertyName("spans")] | ||
public required RazorTextSpan[] Spans { get; set; } | ||
|
||
[JsonPropertyName("razorDocument")] | ||
public required TextDocumentIdentifier RazorDocument { get; set; } | ||
} |
16 changes: 16 additions & 0 deletions
16
...Razor/src/Microsoft.VisualStudioCode.RazorExtension/Services/RazorMapTextChangesParams.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
using System.Text.Json.Serialization; | ||
using Microsoft.CodeAnalysis.Razor.Protocol; | ||
|
||
namespace Microsoft.VisualStudioCode.RazorExtension.Services; | ||
|
||
internal sealed class RazorMapTextChangesParams | ||
{ | ||
[JsonPropertyName("csharpDocument")] | ||
public required TextDocumentIdentifier CSharpDocument { get; set; } | ||
|
||
[JsonPropertyName("textChanges")] | ||
public required RazorTextChange[] TextChanges { get; set; } | ||
} |
17 changes: 17 additions & 0 deletions
17
...zor/src/Microsoft.VisualStudioCode.RazorExtension/Services/RazorMapTextChangesResponse.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
using System.Text.Json.Serialization; | ||
using Microsoft.CodeAnalysis.Razor.Protocol; | ||
|
||
namespace Microsoft.VisualStudioCode.RazorExtension.Services; | ||
|
||
internal sealed class RazorMapTextChangesResponse | ||
{ | ||
[JsonPropertyName("razorDocument")] | ||
public required TextDocumentIdentifier RazorDocument { get; set; } | ||
|
||
[JsonPropertyName("mappedTextChanges")] | ||
public required RazorTextChange[] MappedTextChanges { get; set; } | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.