-
Notifications
You must be signed in to change notification settings - Fork 200
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
Add mapped edits helper #11146
Merged
Merged
Add mapped edits helper #11146
Changes from 16 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
7c647e8
Add mapped edits helper
ryzngard ad48741
Add endpoint registration
ryzngard ca4d979
Change name
ryzngard a411f87
Cleanup
ryzngard ef830e5
Naming
ryzngard da1bb46
PR Feedback
ryzngard 398fbf7
Unify the 'blocking' behavior of finding usings and add a few more tests
ryzngard 626112f
Cleanup based on PR feedback
ryzngard 358e69e
Add skipped test
ryzngard 0ae4bdb
Cleanup some more
ryzngard 2d883c9
Nullable stuff
ryzngard 5b6a83f
Change to use RazorTextChange
ryzngard 579b12f
Log failures. Fix namespace finding from csharp. Fix LSP endpoint
ryzngard 880cc24
Feedback, formatting, and usings
ryzngard 94a3d75
🤷
ryzngard acc6f71
🤷
ryzngard 7565f89
Update src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Document…
ryzngard 67ab783
Update src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Document…
ryzngard 99d61ab
PR Feedback
ryzngard 0197cc0
I hate mocks
ryzngard 674cc43
More mocks...
ryzngard fa9741b
Remove more mocks because I don't know the correct way to do out args
ryzngard 5cd1b61
😢
ryzngard 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
3 changes: 0 additions & 3 deletions
3
src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/CSharp/TextChangeExtensions.cs
This file contains 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 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 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
88 changes: 88 additions & 0 deletions
88
.../src/Microsoft.AspNetCore.Razor.LanguageServer/Mapping/RazorMapToDocumentEditsEndpoint.cs
This file contains 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,88 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Razor.Language; | ||
using Microsoft.AspNetCore.Razor.LanguageServer.EndpointContracts; | ||
using Microsoft.AspNetCore.Razor.Telemetry; | ||
using Microsoft.CodeAnalysis.Razor.DocumentMapping; | ||
using Microsoft.CodeAnalysis.Razor.Logging; | ||
using Microsoft.CodeAnalysis.Razor.Protocol; | ||
using Microsoft.CodeAnalysis.Razor.Protocol.DocumentMapping; | ||
using Microsoft.CodeAnalysis.Text; | ||
using Microsoft.CommonLanguageServerProtocol.Framework; | ||
using Microsoft.VisualStudio.LanguageServer.Protocol; | ||
|
||
namespace Microsoft.AspNetCore.Razor.LanguageServer.Mapping; | ||
|
||
[RazorLanguageServerEndpoint(LanguageServerConstants.RazorMapToDocumentEditsEndpoint)] | ||
internal partial class RazorMapToDocumentEditsEndpoint(IDocumentMappingService documentMappingService, ITelemetryReporter telemetryReporter, ILoggerFactory loggerFactory) : | ||
IRazorDocumentlessRequestHandler<RazorMapToDocumentEditsParams, RazorMapToDocumentEditsResponse?>, | ||
ITextDocumentIdentifierHandler<RazorMapToDocumentEditsParams, Uri> | ||
{ | ||
private readonly IDocumentMappingService _documentMappingService = documentMappingService; | ||
private readonly ITelemetryReporter _telemetryReporter = telemetryReporter; | ||
private readonly ILogger _logger = loggerFactory.GetOrCreateLogger<RazorMapToDocumentEditsEndpoint>(); | ||
|
||
public bool MutatesSolutionState => false; | ||
|
||
public Uri GetTextDocumentIdentifier(RazorMapToDocumentEditsParams request) | ||
{ | ||
return request.RazorDocumentUri; | ||
} | ||
|
||
public async Task<RazorMapToDocumentEditsResponse?> HandleRequestAsync(RazorMapToDocumentEditsParams request, RazorRequestContext requestContext, CancellationToken cancellationToken) | ||
{ | ||
var documentContext = requestContext.DocumentContext; | ||
if (documentContext is null) | ||
{ | ||
return null; | ||
} | ||
|
||
if (request.TextChanges.Length == 0) | ||
{ | ||
return null; | ||
} | ||
|
||
if (request.Kind != RazorLanguageKind.CSharp) | ||
{ | ||
// All other non-C# requests map directly to where they are in the document, | ||
// so the edits do as well | ||
return new RazorMapToDocumentEditsResponse() | ||
{ | ||
TextChanges = request.TextChanges, | ||
HostDocumentVersion = documentContext.Snapshot.Version, | ||
}; | ||
} | ||
|
||
var mappedEdits = await RazorEditHelper.MapCSharpEditsAsync( | ||
request.TextChanges.ToImmutableArray(), | ||
documentContext.Snapshot, | ||
_documentMappingService, | ||
_telemetryReporter, | ||
cancellationToken).ConfigureAwait(false); | ||
|
||
_logger.LogTrace($""" | ||
Before: | ||
{DisplayEdits(request.TextChanges)} | ||
|
||
After: | ||
{DisplayEdits(mappedEdits)} | ||
"""); | ||
|
||
return new RazorMapToDocumentEditsResponse() | ||
{ | ||
TextChanges = mappedEdits.ToArray(), | ||
}; | ||
} | ||
|
||
private string DisplayEdits(IEnumerable<RazorTextChange> changes) | ||
ryzngard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
=> string.Join( | ||
Environment.NewLine, | ||
changes.Select(e => $"{e.Span} => '{e.NewText}'")); | ||
} |
This file contains 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
27 changes: 27 additions & 0 deletions
27
src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/DocumentMapping/RangeComparer.cs
This file contains 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,27 @@ | ||
// 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.Generic; | ||
using Microsoft.VisualStudio.LanguageServer.Protocol; | ||
|
||
namespace Microsoft.CodeAnalysis.Razor.Workspaces.DocumentMapping; | ||
|
||
internal sealed class RangeComparer : IComparer<Range> | ||
{ | ||
public static readonly RangeComparer Instance = new(); | ||
|
||
public int Compare(Range? x, Range? y) | ||
{ | ||
if (x is null) | ||
{ | ||
return y is null ? 0 : 1; | ||
} | ||
|
||
if (y is null) | ||
{ | ||
return -1; | ||
} | ||
|
||
return x.CompareTo(y); | ||
} | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change LGTM