-
Notifications
You must be signed in to change notification settings - Fork 226
Try adjusting caret position to provide better results from Roslyn #8716
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
davidwengier
merged 7 commits into
dotnet:main
from
davidwengier:AttributePrefixAndSuffix
May 31, 2023
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
756c235
Try adjusting caret position to provide better results from Roslyn
davidwengier cf6b675
Remove unnecessary boolean property
davidwengier 09ca076
Fix issue when caret is at the attribute value edge
davidwengier 5b2c829
Extract position adjustment to a new service
davidwengier 7957731
PR feedback
davidwengier 8101f2c
Merge remote-tracking branch 'upstream/main' into AttributePrefixAndS…
davidwengier 61815fd
Fix nullability
davidwengier 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,6 +34,10 @@ public ImplementationEndpoint( | |
|
|
||
| protected override string CustomMessageTarget => RazorLanguageServerCustomMessageTargets.RazorImplementationEndpointName; | ||
|
|
||
| protected override bool PreferCSharpOverHtmlIfPossible => true; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This strictly out of scope, and was an oversight from a previous PR that turned this on for Go To Def that I just noticed, but seemed simple enough to fix. |
||
|
|
||
| protected override bool TreatAnyAttributePositionAsAttributeName => true; | ||
|
|
||
| public RegistrationExtensionResult GetRegistration(VSInternalClientCapabilities clientCapabilities) | ||
| { | ||
| const string ServerCapability = "implementationProvider"; | ||
|
|
||
103 changes: 103 additions & 0 deletions
103
src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/RazorSyntaxFacts.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,103 @@ | ||
| // 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 Microsoft.AspNetCore.Razor.Language; | ||
| using Microsoft.AspNetCore.Razor.Language.Syntax; | ||
| using Microsoft.AspNetCore.Razor.LanguageServer.Extensions; | ||
|
|
||
| namespace Microsoft.AspNetCore.Razor.LanguageServer; | ||
|
|
||
| internal static class RazorSyntaxFacts | ||
| { | ||
| /// <summary> | ||
| /// Given an absolute index positioned in an attribute, finds the absolute index of the part of the | ||
| /// attribute that represents the attribute name. eg. for @bi$$nd-Value it will find the absolute index | ||
| /// of "Value" | ||
| /// </summary> | ||
| public static bool TryGetAttributeNameAbsoluteIndex(RazorCodeDocument codeDocument, int absoluteIndex, out int attributeNameAbsoluteIndex, out TextSpan fullAttributeNameSpan) | ||
| { | ||
| attributeNameAbsoluteIndex = 0; | ||
| fullAttributeNameSpan = default; | ||
|
|
||
| var tree = codeDocument.GetSyntaxTree(); | ||
| var owner = tree.GetOwner(absoluteIndex); | ||
|
|
||
| var attributeName = owner?.Parent switch | ||
| { | ||
| MarkupTagHelperAttributeSyntax att => att.Name, | ||
| MarkupMinimizedTagHelperAttributeSyntax att => att.Name, | ||
| MarkupTagHelperDirectiveAttributeSyntax att => att.Name, | ||
| MarkupMinimizedTagHelperDirectiveAttributeSyntax att => att.Name, | ||
| _ => null | ||
| }; | ||
|
|
||
| if (attributeName is null) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| // Can't get to this point if owner was null, but the compiler doesn't know that | ||
| Assumes.NotNull(owner); | ||
|
|
||
| fullAttributeNameSpan = GetFullAttributeNameSpan(owner.Parent); | ||
|
|
||
| // The GetOwner method can be surprising, eg. Foo="$$Bar" will return the starting quote of the attribute value, | ||
| // but its parent is the attribute name. Easy enough to filter that sort of thing out by just requiring | ||
| // the caret position to be somewhere within the attribute name. | ||
| if (!fullAttributeNameSpan.Contains(absoluteIndex)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| if (attributeName.LiteralTokens is [{ } name]) | ||
| { | ||
| var attribute = name.Content; | ||
| if (attribute.StartsWith("bind-")) | ||
| { | ||
| attributeNameAbsoluteIndex = attributeName.SpanStart + 5; | ||
| } | ||
| else | ||
| { | ||
| attributeNameAbsoluteIndex = attributeName.SpanStart; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| private static TextSpan GetFullAttributeNameSpan(SyntaxNode node) | ||
| { | ||
| return node switch | ||
| { | ||
| MarkupTagHelperAttributeSyntax att => att.Name.Span, | ||
| MarkupMinimizedTagHelperAttributeSyntax att => att.Name.Span, | ||
| MarkupTagHelperDirectiveAttributeSyntax att => CalculateFullSpan(att.Name, att.ParameterName, att.Transition), | ||
| MarkupMinimizedTagHelperDirectiveAttributeSyntax att => CalculateFullSpan(att.Name, att.ParameterName, att.Transition), | ||
| _ => Assumed.Unreachable<TextSpan>(), | ||
| }; | ||
|
|
||
| static TextSpan CalculateFullSpan(MarkupTextLiteralSyntax attributeName, MarkupTextLiteralSyntax? parameterName, RazorMetaCodeSyntax? transition) | ||
| { | ||
| var start = attributeName.SpanStart; | ||
| var length = attributeName.Span.Length; | ||
|
|
||
| // The transition is the "@" if its present | ||
| if (transition is not null) | ||
| { | ||
| start -= 1; | ||
| length += 1; | ||
| } | ||
|
|
||
| // The parameter is, for example, the ":after" but does not include the colon, so we have to account for it | ||
| if (parameterName is not null) | ||
| { | ||
| length += 1 + parameterName.Span.Length; | ||
| } | ||
|
|
||
| return new TextSpan(start, length); | ||
| } | ||
| } | ||
| } |
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
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.