Skip to content

Commit 384830b

Browse files
committed
feat(pycharm): add related class provider for Python #358
- Implement PythonRelatedClassProvider to find related classes/functions for a given Python element - Register the provider in the plugin XML configuration
1 parent 7a964d0 commit 384830b

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cc.unitmesh.python.provider
2+
3+
import cc.unitmesh.devti.provider.RelatedClassesProvider
4+
import com.intellij.openapi.project.Project
5+
import com.intellij.psi.PsiElement
6+
import com.intellij.psi.PsiFile
7+
import com.intellij.psi.PsiNamedElement
8+
import com.jetbrains.python.codeInsight.PyPsiIndexUtil
9+
10+
class PythonRelatedClassProvider : RelatedClassesProvider {
11+
override fun lookupIO(element: PsiElement): List<PsiElement> {
12+
return emptyList()
13+
}
14+
15+
override fun lookupIO(element: PsiFile): List<PsiElement> {
16+
return emptyList()
17+
}
18+
19+
override fun lookupCaller(
20+
project: Project,
21+
element: PsiElement
22+
): List<PsiNamedElement> {
23+
if(element is PsiNamedElement) {
24+
val findUsages = PyPsiIndexUtil.findUsages(element, false)
25+
return findUsages.mapNotNull { usageInfo ->
26+
when (usageInfo.element) {
27+
is PsiNamedElement -> {
28+
return@mapNotNull usageInfo.element as PsiNamedElement
29+
}
30+
31+
else -> return@mapNotNull null
32+
}
33+
}
34+
}
35+
36+
return emptyList()
37+
}
38+
}

pycharm/src/main/resources/cc.unitmesh.pycharm.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,8 @@
2727

2828
<runService implementation="cc.unitmesh.python.provider.PythonRunService"/>
2929

30+
<relatedClassProvider
31+
language="Python"
32+
implementationClass="cc.unitmesh.python.provider.PythonRelatedClassProvider"/>
3033
</extensions>
3134
</idea-plugin>

0 commit comments

Comments
 (0)