Yes, you can filter out simple name references that are part of qualified expressions by checking the parent node. When you have com.somepackage.SomeObject, each part is indeed a USimpleNameReferenceExpression, but they're contained within a UQualifiedReferenceExpression.
Here's how you can filter for only unqualified simple name references:
override fun visitSimpleNameReferenceExpression(node: USimpleNameReferenceExpression): Boolean {
// Check if this simple name reference is NOT part of a qualified expression
if (node.uastParent !is UQualifiedReferenceExpression) {
// This is an unqualified simple name reference like "SomeObject"
// Your logic here
println("Unqualified reference: ${node.identifier}")
}
return super.visitSimpleNameReferenceExpression(node)
}
Alternatively, you might want to be more specific and check if it's the receiver (left side) of a qualified expression:
override fun visitSimpleNameReferenceExpression(node: USimpleNameReferenceExpression): Boolean {
val parent = node.uastParent
// Skip if this is part of a qualified expression (but not the final selector)
if (parent is UQualifiedReferenceExpression && parent.receiver == node) {
return super.visitSimpleNameReferenceExpression(node)
}
// This handles both unqualified references and the final part of qualified references
// Your logic here
println("Reference: ${node.identifier}")
return super.visitSimpleNameReferenceExpression(node)
}
The first approach will only catch truly unqualified references like SomeObject in println(SomeObject). The second approach will catch both unqualified references and the final selector in qualified expressions (so SomeObject in both println(SomeObject) and println(com.somepackage.SomeObject)), but will skip the intermediate parts like com and somepackage.
Choose the approach that best fits your use case!