Skip to content

Commit 9a774ae

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 9ea3146 commit 9a774ae

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
@@ -650,12 +650,11 @@ func findSyntaxNodeInSourceFile<Node: SyntaxProtocol>(
650650
var currentSyntax = Syntax(token)
651651
var resultSyntax: Node? = nil
652652
while let parentSyntax = currentSyntax.parent {
653-
if let typedParent = parentSyntax.as(type) {
653+
currentSyntax = parentSyntax
654+
if let typedParent = currentSyntax.as(type) {
654655
resultSyntax = typedParent
655656
break
656657
}
657-
658-
currentSyntax = parentSyntax
659658
}
660659

661660
// If we didn't find anything, complain and fail.
@@ -665,26 +664,16 @@ func findSyntaxNodeInSourceFile<Node: SyntaxProtocol>(
665664
}
666665

667666
// If we want the outermost node, keep looking.
668-
// FIXME: This is VERY SPECIFIC to handling of types. We must be able to
669-
// do better.
667+
// E.g. for 'foo.bar' we want the member ref expression instead of the
668+
// identifier expression.
670669
if wantOutermost {
671-
while let parentSyntax = resultSyntax.parent {
672-
// Look through type compositions.
673-
if let compositionElement = parentSyntax.as(CompositionTypeElementSyntax.self),
674-
let compositionList = compositionElement.parent?.as(CompositionTypeElementListSyntax.self),
675-
let typedParent = compositionList.parent?.as(type)
676-
{
670+
while
671+
let parentSyntax = currentSyntax.parent,
672+
parentSyntax.position == resultSyntax.position {
673+
currentSyntax = parentSyntax
674+
if let typedParent = currentSyntax.as(type) {
677675
resultSyntax = typedParent
678-
continue
679-
}
680-
681-
guard let typedParent = parentSyntax.as(type),
682-
typedParent.position == resultSyntax.position
683-
else {
684-
break
685676
}
686-
687-
resultSyntax = typedParent
688677
}
689678
}
690679

0 commit comments

Comments
 (0)