aboutsummaryrefslogtreecommitdiff
path: root/api
diff options
context:
space:
mode:
authorJiaxiang Chen <jiaxiang@google.com>2021-08-28 23:01:07 -0700
committerJiaxiang Chen <jiaxiang@google.com>2021-08-31 22:00:22 -0700
commit909c310f7b9e3c7653096b2d2c2a21034e56e767 (patch)
treedf376575e52ac993e85164bcbff638711d6f49c8 /api
parentd5e1aedc690a0d7eb4a1cce402868a76c62a6a87 (diff)
downloadksp-909c310f7b9e3c7653096b2d2c2a21034e56e767.tar.gz
change KSDeclaration.containingFile to an extension property on KSNode
Diffstat (limited to 'api')
-rw-r--r--api/src/main/kotlin/com/google/devtools/ksp/utils.kt14
1 files changed, 14 insertions, 0 deletions
diff --git a/api/src/main/kotlin/com/google/devtools/ksp/utils.kt b/api/src/main/kotlin/com/google/devtools/ksp/utils.kt
index 7e35f968..fe916f49 100644
--- a/api/src/main/kotlin/com/google/devtools/ksp/utils.kt
+++ b/api/src/main/kotlin/com/google/devtools/ksp/utils.kt
@@ -66,6 +66,20 @@ fun Resolver.getPropertyDeclarationByName(name: String, includeTopLevel: Boolean
getPropertyDeclarationByName(getKSNameFromString(name), includeTopLevel)
/**
+ * Find the containing file of a KSNode.
+ * @return KSFile if the given KSNode has a containing file.
+ * exmample of symbols without a containing file: symbols from class files, synthetic symbols craeted by user.
+ */
+val KSNode.containingFile: KSFile?
+ get() {
+ var parent = this.parent
+ while (parent != null && parent !is KSFile) {
+ parent = parent.parent
+ }
+ return parent as? KSFile?
+ }
+
+/**
* Get functions directly declared inside the class declaration.
*
* What are included: member functions, constructors, extension functions declared inside it, etc.