-
Notifications
You must be signed in to change notification settings - Fork 229
Support Linked Editing Ranges in Cohosting. #10349
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
49f810f
Support Linked Editing Ranges in Cohosting.
alexgav 5aa0138
Removed unnecessary code and comment (CR feedback)
alexgav e990d86
Adding RazorDocumentServiceBase to deal with common document-related …
alexgav 1dab139
Fixing build break (removing unnecessary using)
alexgav 7de21f1
Additional methods that allows not to duplicate work
alexgav 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
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
96 changes: 96 additions & 0 deletions
96
...rc/Microsoft.CodeAnalysis.Razor.Workspaces/LinkedEditingRange/LinkedEditingRangeHelper.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,96 @@ | ||
| // 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.Diagnostics.CodeAnalysis; | ||
| using Microsoft.AspNetCore.Razor.Language; | ||
| using Microsoft.AspNetCore.Razor.Language.Syntax; | ||
| using Microsoft.CodeAnalysis.Razor.Logging; | ||
| using Microsoft.CodeAnalysis.Razor.Workspaces; | ||
| using Microsoft.CodeAnalysis.Text; | ||
|
|
||
| using RazorSyntaxToken = Microsoft.AspNetCore.Razor.Language.Syntax.SyntaxToken; | ||
|
|
||
| namespace Microsoft.CodeAnalysis.Razor.LinkedEditingRange; | ||
|
|
||
| internal static class LinkedEditingRangeHelper | ||
| { | ||
| // The regex below excludes characters that can never be valid in a TagHelper name. | ||
| // This is loosely based off logic from the Razor compiler: | ||
| // https://github.com/dotnet/aspnetcore/blob/9da42b9fab4c61fe46627ac0c6877905ec845d5a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/HtmlTokenizer.cs | ||
| public static readonly string WordPattern = @"!?[^ <>!\/\?\[\]=""\\@" + Environment.NewLine + "]+"; | ||
|
|
||
| public static LinePositionSpan[]? GetLinkedSpans(LinePosition linePosition, RazorCodeDocument codeDocument, ILogger logger) | ||
| { | ||
| if (GetSourceLocation(linePosition, codeDocument, logger) is not { } validLocation) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| var syntaxTree = codeDocument.GetSyntaxTree(); | ||
|
|
||
| // We only care if the user is within a TagHelper or HTML tag with a valid start and end tag. | ||
| if (TryGetNearestMarkupNameTokens(syntaxTree, validLocation, out var startTagNameToken, out var endTagNameToken) && | ||
| (startTagNameToken.Span.Contains(validLocation.AbsoluteIndex) || endTagNameToken.Span.Contains(validLocation.AbsoluteIndex) || | ||
| startTagNameToken.Span.End == validLocation.AbsoluteIndex || endTagNameToken.Span.End == validLocation.AbsoluteIndex)) | ||
| { | ||
| var startSpan = startTagNameToken.GetLinePositionSpan(codeDocument.Source); | ||
| var endSpan = endTagNameToken.GetLinePositionSpan(codeDocument.Source); | ||
|
|
||
| return [startSpan, endSpan]; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| private static SourceLocation? GetSourceLocation( | ||
| LinePosition linePosition, | ||
| RazorCodeDocument codeDocument, | ||
| ILogger logger) | ||
| { | ||
| var sourceText = codeDocument.GetSourceText(); | ||
|
|
||
| if (linePosition.ToPosition().TryGetSourceLocation(sourceText, logger, out var location)) | ||
| { | ||
| return location; | ||
| } | ||
| else | ||
| { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| private static bool TryGetNearestMarkupNameTokens( | ||
| RazorSyntaxTree syntaxTree, | ||
| SourceLocation location, | ||
| [NotNullWhen(true)] out RazorSyntaxToken? startTagNameToken, | ||
| [NotNullWhen(true)] out RazorSyntaxToken? endTagNameToken) | ||
| { | ||
| var owner = syntaxTree.Root.FindInnermostNode(location.AbsoluteIndex); | ||
| var element = owner?.FirstAncestorOrSelf<MarkupSyntaxNode>( | ||
| a => a.Kind is SyntaxKind.MarkupTagHelperElement || a.Kind is SyntaxKind.MarkupElement); | ||
|
|
||
| if (element is null) | ||
| { | ||
| startTagNameToken = null; | ||
| endTagNameToken = null; | ||
| return false; | ||
| } | ||
|
|
||
| switch (element) | ||
| { | ||
| // Tag helper | ||
| case MarkupTagHelperElementSyntax markupTagHelperElement: | ||
| startTagNameToken = markupTagHelperElement.StartTag?.Name; | ||
| endTagNameToken = markupTagHelperElement.EndTag?.Name; | ||
| return startTagNameToken is not null && endTagNameToken is not null; | ||
| // HTML | ||
| case MarkupElementSyntax markupElement: | ||
| startTagNameToken = markupElement.StartTag?.Name; | ||
| endTagNameToken = markupElement.EndTag?.Name; | ||
| return startTagNameToken is not null && endTagNameToken is not null; | ||
| default: | ||
| throw new InvalidOperationException("Element is expected to be a MarkupTagHelperElement or MarkupElement."); | ||
| } | ||
| } | ||
| } | ||
14 changes: 14 additions & 0 deletions
14
...or/src/Microsoft.CodeAnalysis.Razor.Workspaces/Remote/IRemoteLinkedEditingRangeService.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,14 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
|
||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.CodeAnalysis.ExternalAccess.Razor; | ||
| using Microsoft.CodeAnalysis.Text; | ||
|
|
||
| namespace Microsoft.CodeAnalysis.Razor.Remote; | ||
|
|
||
| interface IRemoteLinkedEditingRangeService | ||
| { | ||
| ValueTask<LinePositionSpan[]?> GetRangesAsync(RazorPinnedSolutionInfoWrapper solutionInfo, DocumentId razorDocumentId, LinePosition linePosition, CancellationToken cancellationToken); | ||
| } |
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
46 changes: 46 additions & 0 deletions
46
...Microsoft.CodeAnalysis.Remote.Razor/LinkedEditingRange/RemoteLinkedEditingRangeService.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,46 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
|
||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.CodeAnalysis.ExternalAccess.Razor; | ||
| using Microsoft.CodeAnalysis.ExternalAccess.Razor.Api; | ||
| using Microsoft.CodeAnalysis.Razor.LinkedEditingRange; | ||
| using Microsoft.CodeAnalysis.Razor.Logging; | ||
| using Microsoft.CodeAnalysis.Razor.Remote; | ||
| using Microsoft.CodeAnalysis.Remote.Razor.ProjectSystem; | ||
| using Microsoft.CodeAnalysis.Text; | ||
| using Microsoft.ServiceHub.Framework; | ||
|
|
||
| namespace Microsoft.CodeAnalysis.Remote.Razor; | ||
|
|
||
| internal sealed class RemoteLinkedEditingRangeService( | ||
| IServiceBroker serviceBroker, | ||
| DocumentSnapshotFactory documentSnapshotFactory, | ||
| ILoggerFactory loggerFactory) | ||
| : RazorDocumentServiceBase(serviceBroker, documentSnapshotFactory), IRemoteLinkedEditingRangeService | ||
| { | ||
| private readonly ILogger _logger = loggerFactory.GetOrCreateLogger<RemoteLinkedEditingRangeService>(); | ||
|
|
||
| public ValueTask<LinePositionSpan[]?> GetRangesAsync(RazorPinnedSolutionInfoWrapper solutionInfo, DocumentId razorDocumentId, LinePosition linePosition, CancellationToken cancellationToken) | ||
| => RazorBrokeredServiceImplementation.RunServiceAsync( | ||
| solutionInfo, | ||
| ServiceBrokerClient, | ||
| solution => GetRangesAsync(solution, razorDocumentId, linePosition, cancellationToken), | ||
| cancellationToken); | ||
|
|
||
| public async ValueTask<LinePositionSpan[]?> GetRangesAsync(Solution solution, DocumentId razorDocumentId, LinePosition linePosition, CancellationToken cancellationToken) | ||
| { | ||
| if (cancellationToken.IsCancellationRequested) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| if (await GetRazorCodeDocumentAsync(solution, razorDocumentId).ConfigureAwait(false) is not { } codeDocument) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| return LinkedEditingRangeHelper.GetLinkedSpans(linePosition, codeDocument, _logger); | ||
| } | ||
| } |
27 changes: 27 additions & 0 deletions
27
...ft.CodeAnalysis.Remote.Razor/LinkedEditingRange/RemoteLinkedEditingRangeServiceFactory.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,27 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
|
||
| using Microsoft.CodeAnalysis.Razor.Logging; | ||
| using Microsoft.CodeAnalysis.Razor.Remote; | ||
| using Microsoft.CodeAnalysis.Remote.Razor.ProjectSystem; | ||
| using Microsoft.ServiceHub.Framework; | ||
| using Microsoft.VisualStudio.Composition; | ||
|
|
||
| namespace Microsoft.CodeAnalysis.Remote.Razor; | ||
|
|
||
| internal sealed class RemoteLinkedEditingRangeServiceFactory : RazorServiceFactoryBase<IRemoteLinkedEditingRangeService> | ||
| { | ||
| // WARNING: We must always have a parameterless constructor in order to be properly handled by ServiceHub. | ||
| public RemoteLinkedEditingRangeServiceFactory() | ||
| : base(RazorServices.Descriptors) | ||
| { | ||
| } | ||
|
|
||
| protected override IRemoteLinkedEditingRangeService CreateService(IServiceBroker serviceBroker, ExportProvider exportProvider) | ||
| { | ||
| var documentSnapshotFactory = exportProvider.GetExportedValue<DocumentSnapshotFactory>(); | ||
| var loggerFactory = exportProvider.GetExportedValue<ILoggerFactory>(); | ||
|
|
||
| return new RemoteLinkedEditingRangeService(serviceBroker, documentSnapshotFactory, loggerFactory); | ||
| } | ||
| } |
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.
Decided to make this a static helper - one of the factored out methods was already static and I believe only one call needed a logger. Other than logger it doesn't really keep any state (and even logger seems like it's nice to have from whichever endpoing is using the helper code). Plus it's all temporary until we switch to cohosting full time. The three-pronged service seemed like an overkill here.
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.
We'll see 😁