Skip to content

Commit 2e26370

Browse files
committed
[ASTGen] Generalize findSyntaxNodeInSourceFile(wantOutermost:true)
When 'wantOutermost' is true, traverse the parent as long as the position is the same as the target position.
1 parent f3975de commit 2e26370

File tree

1 file changed

+9
-20
lines changed

1 file changed

+9
-20
lines changed

lib/ASTGen/Sources/ASTGen/Macros.swift

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -636,12 +636,11 @@ func findSyntaxNodeInSourceFile<Node: SyntaxProtocol>(
636636
var currentSyntax = Syntax(token)
637637
var resultSyntax: Node? = nil
638638
while let parentSyntax = currentSyntax.parent {
639-
if let typedParent = parentSyntax.as(type) {
639+
currentSyntax = parentSyntax
640+
if let typedParent = currentSyntax.as(type) {
640641
resultSyntax = typedParent
641642
break
642643
}
643-
644-
currentSyntax = parentSyntax
645644
}
646645

647646
// If we didn't find anything, complain and fail.
@@ -651,26 +650,16 @@ func findSyntaxNodeInSourceFile<Node: SyntaxProtocol>(
651650
}
652651

653652
// If we want the outermost node, keep looking.
654-
// FIXME: This is VERY SPECIFIC to handling of types. We must be able to
655-
// do better.
653+
// E.g. for 'foo.bar' we want the member ref expression instead of the
654+
// identifier expression.
656655
if wantOutermost {
657-
while let parentSyntax = resultSyntax.parent {
658-
// Look through type compositions.
659-
if let compositionElement = parentSyntax.as(CompositionTypeElementSyntax.self),
660-
let compositionList = compositionElement.parent?.as(CompositionTypeElementListSyntax.self),
661-
let typedParent = compositionList.parent?.as(type)
662-
{
656+
while
657+
let parentSyntax = currentSyntax.parent,
658+
parentSyntax.position == resultSyntax.position {
659+
currentSyntax = parentSyntax
660+
if let typedParent = currentSyntax.as(type) {
663661
resultSyntax = typedParent
664-
continue
665-
}
666-
667-
guard let typedParent = parentSyntax.as(type),
668-
typedParent.position == resultSyntax.position
669-
else {
670-
break
671662
}
672-
673-
resultSyntax = typedParent
674663
}
675664
}
676665

0 commit comments

Comments
 (0)