Skip to content
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

Remove LocateOwner in formatting #9237

Merged
merged 6 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -314,18 +314,9 @@ private static bool ShouldFormatCore(FormattingContext context, TextSpan mapping
var sourceText = context.SourceText;
var absoluteIndex = mappingSpan.Start;

if (mappingSpan.Length > 0)
{
// Slightly ugly hack to get around the behavior of LocateOwner.
// In some cases, using the start of a mapping doesn't work well
// because LocateOwner returns the previous node due to it owning the edge.
// So, if we can try to find the owner using a position that fully belongs to the current mapping.
absoluteIndex = mappingSpan.Start + 1;
}

var change = new SourceChange(absoluteIndex, 0, string.Empty);
var syntaxTree = context.CodeDocument.GetSyntaxTree();
var owner = syntaxTree.Root.LocateOwner(change);
var span = new Language.Syntax.TextSpan(mappingSpan.Start, mappingSpan.Length);
var owner = syntaxTree.Root.FindNode(span, getInnermostNodeForTie: true);
if (owner is null)
{
// Can't determine owner of this position. Optimistically allow formatting.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,7 @@ private static List<TextChange> AdjustRazorIndentation(FormattingContext context
private static bool IsPartOfHtmlTag(FormattingContext context, int position)
{
var syntaxTree = context.CodeDocument.GetSyntaxTree();
var change = new SourceChange(position, 0, string.Empty);
var owner = syntaxTree.Root.LocateOwner(change);
var owner = syntaxTree.Root.FindInnermostNode(position, includeWhitespace: true);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Include whitespace may not be needed here...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the tests all pass with it as false then no objection here, but I didn't check

if (owner is null)
{
// Can't determine owner of this position.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ static bool TryGetNearestMarkupNameTokens(
[NotNullWhen(true)] out SyntaxToken? startTagNameToken,
[NotNullWhen(true)] out SyntaxToken? endTagNameToken)
{
var change = new SourceChange(location.AbsoluteIndex, length: 0, newText: "");
var owner = syntaxTree.Root.LocateOwner(change);
var owner = syntaxTree.Root.FindInnermostNode(location.AbsoluteIndex);
var element = owner?.FirstAncestorOrSelf<MarkupSyntaxNode>(
a => a.Kind is SyntaxKind.MarkupTagHelperElement || a.Kind is SyntaxKind.MarkupElement);

Expand Down