Skip to content

Commit cba27fe

Browse files
authored
Merge pull request #8807 from davidwengier/DontUsePreviousSpan
Don't use PreviousSpan method, because it's slow
2 parents d4d24e2 + 0c28d27 commit cba27fe

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Extensions/SyntaxNodeExtensions.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,31 @@ body.Keyword is CSharpStatementLiteralSyntax literal &&
3434
return false;
3535
}
3636

37+
/// <summary>
38+
/// Walks up the tree through the <paramref name="owner"/>'s parents to find the outermost node that starts at the same position.
39+
/// </summary>
40+
internal static SyntaxNode? GetOutermostNode(this SyntaxNode owner)
41+
{
42+
var node = owner.Parent;
43+
if (node is null)
44+
{
45+
return owner;
46+
}
47+
48+
var lastNode = node;
49+
while (node.SpanStart == owner.SpanStart)
50+
{
51+
lastNode = node;
52+
node = node.Parent;
53+
if (node is null)
54+
{
55+
break;
56+
}
57+
}
58+
59+
return lastNode;
60+
}
61+
3762
internal static bool TryGetPreviousSibling(this SyntaxNode syntaxNode, [NotNullWhen(true)] out SyntaxNode? previousSibling)
3863
{
3964
var syntaxNodeParent = syntaxNode.Parent;

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/CSharpFormattingPassBase.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ bool IsImplicitStatementStart()
368368
if (owner.SpanStart == mappingSpan.Start &&
369369
owner is CSharpStatementLiteralSyntax &&
370370
owner.Parent is CSharpCodeBlockSyntax &&
371-
owner.PreviousSpan() is CSharpTransitionSyntax)
371+
owner.TryGetPreviousSibling(out var transition) && transition is CSharpTransitionSyntax)
372372
{
373373
return true;
374374
}
@@ -487,12 +487,16 @@ private static SyntaxNode FixOwnerToWorkaroundCompilerQuirks(SyntaxNode owner)
487487
// Workaround for https://github.com/dotnet/aspnetcore/issues/36689
488488
// A tags owner comes back as itself if it is preceeded by a HTML comment,
489489
// because the whitespace between the comment and the tag is reported as not editable
490-
if (owner is MarkupTextLiteralSyntax &&
491-
owner.PreviousSpan() is MarkupTextLiteralSyntax literal &&
492-
literal.ContainsOnlyWhitespace() &&
493-
literal.PreviousSpan()?.Parent is MarkupCommentBlockSyntax)
490+
491+
// Get to the outermost node first. eg in "<span" we might be on the node for the "<", which is parented
492+
// by some other intermediate node, which is parented by the actual start tag node. We need to get out to
493+
// the start tag node, in order to reason about its siblings. The siblings of the "<" are not helpful :)
494+
var outerNode = owner.GetOutermostNode();
495+
if (outerNode is not null &&
496+
outerNode.TryGetPreviousSibling(out var whiteSpace) && whiteSpace.ContainsOnlyWhitespace() &&
497+
whiteSpace.TryGetPreviousSibling(out var comment) && comment is MarkupCommentBlockSyntax)
494498
{
495-
owner = literal;
499+
return whiteSpace;
496500
}
497501

498502
return owner;

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/CSharpOnTypeFormattingPass.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ private static void CleanupSourceMappingStart(FormattingContext context, Range s
346346
}
347347

348348
if (owner is CSharpStatementLiteralSyntax &&
349-
owner.PreviousSpan() is { } prevNode &&
349+
owner.TryGetPreviousSibling(out var prevNode) &&
350350
prevNode.AncestorsAndSelf().FirstOrDefault(a => a is CSharpTemplateBlockSyntax) is { } template &&
351351
owner.SpanStart == template.Span.End &&
352352
IsOnSingleLine(template, text))

0 commit comments

Comments
 (0)