Skip to content

Commit

Permalink
chore: get fields
Browse files Browse the repository at this point in the history
  • Loading branch information
BFergerson committed Feb 21, 2024
1 parent 87f6b14 commit 63fd72d
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ import spp.protocol.artifact.ArtifactType
@Suppress("MemberVisibilityCanBePrivate", "TooManyFunctions") // public API
object ArtifactScopeService : AbstractSourceMarkerService<IArtifactScopeService>(), IArtifactScopeService {

override fun getFields(element: PsiElement): List<PsiElement> {
return getService(element.language).getFields(element)
}

override fun getLoops(element: PsiElement): List<PsiElement> {
return getService(element.language).getLoops(element)
}
Expand Down Expand Up @@ -118,6 +122,10 @@ object ArtifactScopeService : AbstractSourceMarkerService<IArtifactScopeService>

// Extensions

fun PsiElement.getFields(): List<PsiElement> {
return ArtifactScopeService.getService(language).getFields(this)
}

fun PsiElement.getFunctions(includeInnerClasses: Boolean = false): List<PsiNamedElement> {
return ArtifactScopeService.getService(language).getFunctions(this, includeInnerClasses)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import com.intellij.psi.PsiNamedElement
@Suppress("TooManyFunctions") // public API
interface IArtifactScopeService : ISourceMarkerService {

fun getFields(element: PsiElement): List<PsiElement>
fun getLoops(element: PsiElement): List<PsiElement>
fun getFunctions(element: PsiElement, includeInnerClasses: Boolean = false): List<PsiNamedElement>
fun getClasses(element: PsiElement): List<PsiNamedElement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ import spp.jetbrains.marker.SourceMarkerUtils
@Suppress("TooManyFunctions") // public API
class JavascriptArtifactScopeService : IArtifactScopeService {

override fun getFields(element: PsiElement): List<PsiElement> {
require(ArtifactTypeService.isJavaScript(element))
return element.descendantsOfType<JSField>().toList()
}

override fun getLoops(element: PsiElement): List<PsiElement> {
require(ArtifactTypeService.isJavaScript(element))
return element.descendantsOfType<JSLoopStatement>().toList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ import spp.jetbrains.marker.SourceMarkerUtils.doOnReadThread
@Suppress("TooManyFunctions") // public API
class JVMArtifactScopeService : IArtifactScopeService {

override fun getFields(element: PsiElement): List<PsiElement> {
return when {
ArtifactTypeService.isKotlin(element) -> element.descendantsOfType<KtProperty>().toList()
else -> element.descendantsOfType<PsiField>().toList()
}
}

override fun getLoops(element: PsiElement): List<PsiElement> {
return when {
ArtifactTypeService.isKotlin(element) -> element.descendantsOfType<KtLoopExpression>().toList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ import spp.jetbrains.marker.SourceMarkerUtils
@Suppress("TooManyFunctions") // public API
class PythonArtifactScopeService : IArtifactScopeService {

override fun getFields(element: PsiElement): List<PsiElement> {
require(ArtifactTypeService.isPython(element))
return element.descendantsOfType<PyTargetExpression>().toList()
}

override fun getLoops(element: PsiElement): List<PsiElement> {
require(ArtifactTypeService.isPython(element))
return element.descendantsOfType<PyLoopStatement>().toList()
Expand Down

0 comments on commit 63fd72d

Please sign in to comment.