-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update logic for checking annotations to avoid using internal analyze…
… API Bug #49
- Loading branch information
1 parent
6de415c
commit e720433
Showing
3 changed files
with
54 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
plugin/src/main/kotlin/com/varabyte/kobweb/intellij/util/psi/KtElementExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.varabyte.kobweb.intellij.util.psi | ||
|
||
import com.intellij.openapi.roots.ProjectRootModificationTracker | ||
import com.intellij.psi.util.CachedValueProvider | ||
import com.intellij.psi.util.CachedValuesManager | ||
import org.jetbrains.kotlin.idea.base.psi.kotlinFqName | ||
import org.jetbrains.kotlin.idea.caches.resolve.analyze | ||
import org.jetbrains.kotlin.psi.KtAnnotated | ||
import org.jetbrains.kotlin.psi.KtAnnotationEntry | ||
import org.jetbrains.kotlin.resolve.BindingContext | ||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode | ||
|
||
/** | ||
* Determines whether this [KtAnnotationEntry] has the specified qualified name. | ||
* Careful: this does *not* currently take into account Kotlin type aliases (https://kotlinlang.org/docs/reference/type-aliases.html). | ||
* Fortunately, type aliases are extremely uncommon for simple annotation types. | ||
*/ | ||
private fun KtAnnotationEntry.fqNameMatches(fqName: String): Boolean { | ||
// For inspiration, see IDELightClassGenerationSupport.KtUltraLightSupportImpl.findAnnotation in the Kotlin plugin. | ||
val shortName = shortName?.asString() ?: return false | ||
return fqName.endsWith(".$shortName") && fqName == getQualifiedName() | ||
} | ||
|
||
/** | ||
* Computes the qualified name of this [KtAnnotationEntry]. | ||
* Prefer to use [fqNameMatches], which checks the short name first and thus has better performance. | ||
*/ | ||
private fun KtAnnotationEntry.getQualifiedName(): String? = | ||
analyze(BodyResolveMode.PARTIAL).get(BindingContext.ANNOTATION, this)?.fqName?.asString() | ||
|
||
/** | ||
* Returns true if the function is tagged with any one of the given annotations. | ||
* | ||
* The annotation name must be fully-qualified, as in "androidx.compose.runtime.Composable". | ||
*/ | ||
internal fun KtAnnotated.hasAnyAnnotation(vararg annotationFqns: String): Boolean { | ||
// Code adapted from https://github.com/JetBrains/compose-multiplatform/blob/b501e0f794aecde9a6ce47cb4b5308939cbc7cc5/idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/ide/preview/locationUtils.kt#L135 | ||
return CachedValuesManager.getCachedValue(this) { | ||
CachedValueProvider.Result.create( | ||
run { | ||
this.annotationEntries.any { annotationEntry -> | ||
annotationFqns.any { fqName -> annotationEntry.fqNameMatches(fqName) } | ||
} | ||
}, | ||
this.containingKtFile, | ||
ProjectRootModificationTracker.getInstance(project), | ||
*annotationFqns | ||
) | ||
} | ||
} |