From 63fd72d3d9d7ded652968b3f07a89c2676090e6b Mon Sep 17 00:00:00 2001 From: Brandon Fergerson Date: Wed, 21 Feb 2024 14:23:20 -0600 Subject: [PATCH] chore: get fields --- .../artifact/service/ArtifactScopeService.kt | 8 ++++++++ .../artifact/service/define/IArtifactScopeService.kt | 1 + .../js/service/JavascriptArtifactScopeService.kt | 5 +++++ .../marker/jvm/service/JVMArtifactScopeService.kt | 7 +++++++ .../marker/jvm/service/utils/JVMMarkerUtils.kt | 12 ++++++++++++ .../marker/py/service/PythonArtifactScopeService.kt | 5 +++++ 6 files changed, 38 insertions(+) diff --git a/core/src/main/kotlin/spp/jetbrains/artifact/service/ArtifactScopeService.kt b/core/src/main/kotlin/spp/jetbrains/artifact/service/ArtifactScopeService.kt index d29058d0f..cfd7efeaa 100644 --- a/core/src/main/kotlin/spp/jetbrains/artifact/service/ArtifactScopeService.kt +++ b/core/src/main/kotlin/spp/jetbrains/artifact/service/ArtifactScopeService.kt @@ -35,6 +35,10 @@ import spp.protocol.artifact.ArtifactType @Suppress("MemberVisibilityCanBePrivate", "TooManyFunctions") // public API object ArtifactScopeService : AbstractSourceMarkerService(), IArtifactScopeService { + override fun getFields(element: PsiElement): List { + return getService(element.language).getFields(element) + } + override fun getLoops(element: PsiElement): List { return getService(element.language).getLoops(element) } @@ -118,6 +122,10 @@ object ArtifactScopeService : AbstractSourceMarkerService // Extensions +fun PsiElement.getFields(): List { + return ArtifactScopeService.getService(language).getFields(this) +} + fun PsiElement.getFunctions(includeInnerClasses: Boolean = false): List { return ArtifactScopeService.getService(language).getFunctions(this, includeInnerClasses) } diff --git a/core/src/main/kotlin/spp/jetbrains/artifact/service/define/IArtifactScopeService.kt b/core/src/main/kotlin/spp/jetbrains/artifact/service/define/IArtifactScopeService.kt index dafc6e5ee..9b03d376b 100644 --- a/core/src/main/kotlin/spp/jetbrains/artifact/service/define/IArtifactScopeService.kt +++ b/core/src/main/kotlin/spp/jetbrains/artifact/service/define/IArtifactScopeService.kt @@ -31,6 +31,7 @@ import com.intellij.psi.PsiNamedElement @Suppress("TooManyFunctions") // public API interface IArtifactScopeService : ISourceMarkerService { + fun getFields(element: PsiElement): List fun getLoops(element: PsiElement): List fun getFunctions(element: PsiElement, includeInnerClasses: Boolean = false): List fun getClasses(element: PsiElement): List diff --git a/marker/js-marker/src/main/kotlin/spp/jetbrains/marker/js/service/JavascriptArtifactScopeService.kt b/marker/js-marker/src/main/kotlin/spp/jetbrains/marker/js/service/JavascriptArtifactScopeService.kt index a24b13346..8a846217f 100644 --- a/marker/js-marker/src/main/kotlin/spp/jetbrains/marker/js/service/JavascriptArtifactScopeService.kt +++ b/marker/js-marker/src/main/kotlin/spp/jetbrains/marker/js/service/JavascriptArtifactScopeService.kt @@ -49,6 +49,11 @@ import spp.jetbrains.marker.SourceMarkerUtils @Suppress("TooManyFunctions") // public API class JavascriptArtifactScopeService : IArtifactScopeService { + override fun getFields(element: PsiElement): List { + require(ArtifactTypeService.isJavaScript(element)) + return element.descendantsOfType().toList() + } + override fun getLoops(element: PsiElement): List { require(ArtifactTypeService.isJavaScript(element)) return element.descendantsOfType().toList() diff --git a/marker/jvm-marker/src/main/kotlin/spp/jetbrains/marker/jvm/service/JVMArtifactScopeService.kt b/marker/jvm-marker/src/main/kotlin/spp/jetbrains/marker/jvm/service/JVMArtifactScopeService.kt index 69fb84651..6ccff0442 100644 --- a/marker/jvm-marker/src/main/kotlin/spp/jetbrains/marker/jvm/service/JVMArtifactScopeService.kt +++ b/marker/jvm-marker/src/main/kotlin/spp/jetbrains/marker/jvm/service/JVMArtifactScopeService.kt @@ -57,6 +57,13 @@ import spp.jetbrains.marker.SourceMarkerUtils.doOnReadThread @Suppress("TooManyFunctions") // public API class JVMArtifactScopeService : IArtifactScopeService { + override fun getFields(element: PsiElement): List { + return when { + ArtifactTypeService.isKotlin(element) -> element.descendantsOfType().toList() + else -> element.descendantsOfType().toList() + } + } + override fun getLoops(element: PsiElement): List { return when { ArtifactTypeService.isKotlin(element) -> element.descendantsOfType().toList() diff --git a/marker/jvm-marker/src/main/kotlin/spp/jetbrains/marker/jvm/service/utils/JVMMarkerUtils.kt b/marker/jvm-marker/src/main/kotlin/spp/jetbrains/marker/jvm/service/utils/JVMMarkerUtils.kt index 97fb92289..57e0fa2a0 100755 --- a/marker/jvm-marker/src/main/kotlin/spp/jetbrains/marker/jvm/service/utils/JVMMarkerUtils.kt +++ b/marker/jvm-marker/src/main/kotlin/spp/jetbrains/marker/jvm/service/utils/JVMMarkerUtils.kt @@ -55,6 +55,7 @@ object JVMMarkerUtils { element is PsiAnnotation -> return getFullyQualifiedName(element) element is PsiClass -> return getFullyQualifiedName(element) element is PsiMethod -> return getFullyQualifiedName(element) + element is PsiField -> return getFullyQualifiedName(element) else -> Unit } @@ -158,6 +159,17 @@ object JVMMarkerUtils { ) } + private fun getFullyQualifiedName(psiField: PsiField): ArtifactQualifiedName { + val classQualifiedName = psiField.findAnyContainingStrict(PsiClass::class.java)?.let { + getFullyQualifiedName(it).identifier + } + return ArtifactQualifiedName( + "$classQualifiedName.${psiField.name}", + type = ArtifactType.EXPRESSION, //todo: ArtifactType.VARIABLE + lineNumber = psiField.nameIdentifier.let { SourceMarkerUtils.getLineNumber(it) } + ) + } + private fun getFullyQualifiedName(method: KtNamedFunction): ArtifactQualifiedName { val classQualifiedName = method.findAnyContainingStrict(KtClass::class.java)?.let { getFullyQualifiedName(it).identifier diff --git a/marker/py-marker/src/main/kotlin/spp/jetbrains/marker/py/service/PythonArtifactScopeService.kt b/marker/py-marker/src/main/kotlin/spp/jetbrains/marker/py/service/PythonArtifactScopeService.kt index 517389f18..293c8e048 100644 --- a/marker/py-marker/src/main/kotlin/spp/jetbrains/marker/py/service/PythonArtifactScopeService.kt +++ b/marker/py-marker/src/main/kotlin/spp/jetbrains/marker/py/service/PythonArtifactScopeService.kt @@ -49,6 +49,11 @@ import spp.jetbrains.marker.SourceMarkerUtils @Suppress("TooManyFunctions") // public API class PythonArtifactScopeService : IArtifactScopeService { + override fun getFields(element: PsiElement): List { + require(ArtifactTypeService.isPython(element)) + return element.descendantsOfType().toList() + } + override fun getLoops(element: PsiElement): List { require(ArtifactTypeService.isPython(element)) return element.descendantsOfType().toList()