diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Definition/DefinitionEndpoint.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Definition/DefinitionEndpoint.cs index d986dc2e1a0..a656745ff40 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Definition/DefinitionEndpoint.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Definition/DefinitionEndpoint.cs @@ -160,7 +160,7 @@ public void ApplyCapabilities(VSInternalServerCapabilities serverCapabilities, V return (null, null); } - var node = owner.Parent?.FirstAncestorOrSelf(n => + var node = owner.FirstAncestorOrSelf(n => n.Kind == SyntaxKind.MarkupTagHelperStartTag || n.Kind == SyntaxKind.MarkupTagHelperEndTag); if (node is null) @@ -178,16 +178,21 @@ public void ApplyCapabilities(VSInternalServerCapabilities serverCapabilities, V string? propertyName = null; - if (!ignoreAttributes) + if (!ignoreAttributes && node is MarkupTagHelperStartTagSyntax startTag) { + // Include attributes where the end index also matches, since GetSyntaxNodeAsync will consider that the start tag but we behave + // as if the user wants to go to the attribute definitiion. + // ie: + var selectedAttribute = startTag.Attributes.FirstOrDefault(a => a.Span.Contains(absoluteIndex) || a.Span.End == absoluteIndex); + // If we're on an attribute then just validate against the attribute name - if (owner.Parent is MarkupTagHelperAttributeSyntax attribute) + if (selectedAttribute is MarkupTagHelperAttributeSyntax attribute) { // Normal attribute, ie name = attribute.Name; propertyName = attribute.TagHelperAttributeInfo.Name; } - else if (owner.Parent is MarkupMinimizedTagHelperAttributeSyntax minimizedAttribute) + else if (selectedAttribute is MarkupMinimizedTagHelperAttributeSyntax minimizedAttribute) { // Minimized attribute, ie name = minimizedAttribute.Name; diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/DocumentContext.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/DocumentContext.cs index 99f07d34eea..49a33306a8a 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/DocumentContext.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/DocumentContext.cs @@ -5,8 +5,8 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Razor.Language; -using Microsoft.AspNetCore.Razor.Language.Legacy; using Microsoft.AspNetCore.Razor.Language.Syntax; +using Microsoft.AspNetCore.Razor.LanguageServer.Extensions; using Microsoft.CodeAnalysis.Razor.ProjectSystem; using Microsoft.CodeAnalysis.Razor.Workspaces.Extensions; using Microsoft.CodeAnalysis.Text; @@ -108,13 +108,12 @@ public virtual async Task GetHtmlSourceTextAsync(CancellationToken c public async Task GetSyntaxNodeAsync(int absoluteIndex, CancellationToken cancellationToken) { - var change = new SourceChange(absoluteIndex, length: 0, newText: string.Empty); var syntaxTree = await GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false); if (syntaxTree.Root is null) { return null; } - return syntaxTree.Root.LocateOwner(change); + return syntaxTree.Root.FindInnermostNode(absoluteIndex); } } diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Refactoring/RenameEndpoint.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Refactoring/RenameEndpoint.cs index 3c282208ebc..b853e74130b 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Refactoring/RenameEndpoint.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Refactoring/RenameEndpoint.cs @@ -351,7 +351,7 @@ private static bool BindingContainsTagHelper(TagHelperDescriptor tagHelper, TagH return null; } - var node = owner.Parent?.FirstAncestorOrSelf(n => n.Kind == SyntaxKind.MarkupTagHelperStartTag); + var node = owner.FirstAncestorOrSelf(n => n.Kind == SyntaxKind.MarkupTagHelperStartTag); if (node is not MarkupTagHelperStartTagSyntax tagHelperStartTag) { return null;