Skip to content

Commit e266b1c

Browse files
committed
chore(jvm): default to not include inner classes on getFunctions()
1 parent d695b05 commit e266b1c

File tree

9 files changed

+79
-11
lines changed

9 files changed

+79
-11
lines changed

core/src/main/kotlin/spp/jetbrains/artifact/service/ArtifactScopeService.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ object ArtifactScopeService : AbstractSourceMarkerService<IArtifactScopeService>
3939
return getService(element.language).getLoops(element)
4040
}
4141

42-
override fun getFunctions(element: PsiElement): List<PsiNamedElement> {
43-
return getService(element.language).getFunctions(element)
42+
override fun getFunctions(element: PsiElement, includeInnerClasses: Boolean): List<PsiNamedElement> {
43+
return getService(element.language).getFunctions(element, includeInnerClasses)
4444
}
4545

4646
override fun getClasses(element: PsiElement): List<PsiNamedElement> {
@@ -118,8 +118,8 @@ object ArtifactScopeService : AbstractSourceMarkerService<IArtifactScopeService>
118118

119119
// Extensions
120120

121-
fun PsiElement.getFunctions(): List<PsiNamedElement> {
122-
return ArtifactScopeService.getService(language).getFunctions(this)
121+
fun PsiElement.getFunctions(includeInnerClasses: Boolean = false): List<PsiNamedElement> {
122+
return ArtifactScopeService.getService(language).getFunctions(this, includeInnerClasses)
123123
}
124124

125125
fun PsiElement.getClasses(): List<PsiNamedElement> {

core/src/main/kotlin/spp/jetbrains/artifact/service/define/IArtifactScopeService.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import com.intellij.psi.PsiNamedElement
3232
interface IArtifactScopeService : ISourceMarkerService {
3333

3434
fun getLoops(element: PsiElement): List<PsiElement>
35-
fun getFunctions(element: PsiElement): List<PsiNamedElement>
35+
fun getFunctions(element: PsiElement, includeInnerClasses: Boolean = false): List<PsiNamedElement>
3636
fun getClasses(element: PsiElement): List<PsiNamedElement>
3737
fun getChildIfs(element: PsiElement): List<PsiElement>
3838
fun getParentIf(element: PsiElement): PsiElement?

marker/js-marker/src/main/kotlin/spp/jetbrains/marker/js/service/JavascriptArtifactScopeService.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class JavascriptArtifactScopeService : IArtifactScopeService {
5454
return element.descendantsOfType<JSLoopStatement>().toList()
5555
}
5656

57-
override fun getFunctions(element: PsiElement): List<PsiNamedElement> {
57+
override fun getFunctions(element: PsiElement, includeInnerClasses: Boolean): List<PsiNamedElement> {
5858
require(ArtifactTypeService.isJavaScript(element))
5959
return element.descendantsOfType<JSFunction>().toList()
6060
}

marker/jvm-marker/src/main/kotlin/spp/jetbrains/marker/jvm/service/JVMArtifactScopeService.kt

+19-4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import com.siyeh.ig.psiutils.ControlFlowUtils
3636
import org.jetbrains.kotlin.backend.jvm.ir.psiElement
3737
import org.jetbrains.kotlin.idea.caches.resolve.resolveToCall
3838
import org.jetbrains.kotlin.psi.*
39+
import org.jetbrains.kotlin.psi.psiUtil.containingClass
3940
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall
4041
import org.jetbrains.plugins.scala.lang.psi.api.statements.ScFunctionDefinition
4142
import org.joor.Reflect
@@ -64,11 +65,25 @@ class JVMArtifactScopeService : IArtifactScopeService {
6465
}
6566
}
6667

67-
override fun getFunctions(element: PsiElement): List<PsiNamedElement> {
68+
override fun getFunctions(element: PsiElement, includeInnerClasses: Boolean): List<PsiNamedElement> {
6869
return when {
69-
ArtifactTypeService.isKotlin(element) -> element.descendantsOfType<KtNamedFunction>().toList()
70-
ArtifactTypeService.isScala(element) -> element.descendantsOfType<ScFunctionDefinition>().toList()
71-
else -> element.descendantsOfType<PsiMethod>().toList()
70+
ArtifactTypeService.isKotlin(element) -> element.descendantsOfType<KtNamedFunction>().filter {
71+
if (element is KtClass && !includeInnerClasses) {
72+
element == it.containingClass()
73+
} else true
74+
}.toList()
75+
76+
ArtifactTypeService.isScala(element) -> element.descendantsOfType<ScFunctionDefinition>().filter {
77+
if (element is PsiClass && !includeInnerClasses) {
78+
element == it.containingClass
79+
} else true
80+
}.toList()
81+
82+
else -> element.descendantsOfType<PsiMethod>().filter {
83+
if (element is PsiClass && !includeInnerClasses) {
84+
element == it.containingClass
85+
} else true
86+
}.toList()
7287
}
7388
}
7489

marker/jvm-marker/src/test/kotlin/spp/jetbrains/marker/jvm/service/JVMArtifactScopeServiceTest.kt

+26
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import com.intellij.testFramework.TestDataPath
2424
import com.intellij.testFramework.fixtures.BasePlatformTestCase
2525
import kotlinx.coroutines.runBlocking
2626
import spp.jetbrains.artifact.service.ArtifactScopeService
27+
import spp.jetbrains.artifact.service.getClasses
28+
import spp.jetbrains.artifact.service.getFunctions
2729
import spp.jetbrains.marker.SourceMarker
2830
import spp.jetbrains.marker.jvm.JVMLanguageProvider
2931

@@ -104,4 +106,28 @@ class JVMArtifactScopeServiceTest : BasePlatformTestCase() {
104106
// assertEquals("directCalledFunction", internalCallerFunctions.first().name)
105107
// assertEquals("callerFunction", internalCallerFunctions.last().name)
106108
}
109+
110+
fun testGetFunctionsOnInnerClass() {
111+
doGetFunctionsOnInnerClass("groovy")
112+
doGetFunctionsOnInnerClass("java")
113+
doGetFunctionsOnInnerClass("kt")
114+
}
115+
116+
private fun doGetFunctionsOnInnerClass(extension: String) {
117+
val psiFile = myFixture.configureByFile(getTestName(false) + ".$extension")
118+
assertEquals(2, psiFile.getClasses().size)
119+
120+
val classFunctions = psiFile.getClasses().first().getFunctions()
121+
assertEquals(1, classFunctions.size)
122+
assertEquals("function1", classFunctions.first().name)
123+
124+
val innerClassFunctions = psiFile.getClasses().last().getFunctions()
125+
assertEquals(1, innerClassFunctions.size)
126+
assertEquals("function2", innerClassFunctions.first().name)
127+
128+
val allFunctions = psiFile.getClasses().first().getFunctions(true)
129+
assertEquals(2, allFunctions.size)
130+
assertEquals("function1", allFunctions.first().name)
131+
assertEquals("function2", allFunctions.last().name)
132+
}
107133
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class GetFunctionsInnerClass {
2+
private void function1() {
3+
}
4+
5+
class InnerClass {
6+
private void function2() {
7+
}
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
private class GetFunctionsInnerClass {
2+
private void function1() {
3+
}
4+
5+
class InnerClass {
6+
private void function2() {
7+
}
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class GetFunctionsInnerClass {
2+
fun function1() {
3+
}
4+
5+
class InnerClass {
6+
fun function2() {
7+
}
8+
}
9+
}

marker/py-marker/src/main/kotlin/spp/jetbrains/marker/py/service/PythonArtifactScopeService.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class PythonArtifactScopeService : IArtifactScopeService {
5454
return element.descendantsOfType<PyLoopStatement>().toList()
5555
}
5656

57-
override fun getFunctions(element: PsiElement): List<PsiNamedElement> {
57+
override fun getFunctions(element: PsiElement, includeInnerClasses: Boolean): List<PsiNamedElement> {
5858
require(ArtifactTypeService.isPython(element))
5959
return element.descendantsOfType<PyFunction>().toList()
6060
}

0 commit comments

Comments
 (0)