Skip to content

Commit ef24e71

Browse files
committed
fix(java): improve symbol resolution logic #101
Improve symbol resolution by handling package names, class names, method names, and their combinations, as per the requested logic. Refactored the `JavaCustomDevInsSymbolProvider.kt` file to handle symbol resolution according to package names, class names, method names, and combinations of these. Also, updated the `quick-start.md` file with an example of the method output.
1 parent fd6c134 commit ef24e71

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

docs/devins/quick-start.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,20 @@ The output will be:
7979
cc.unitmesh.untitled.demo.MathHelperTest
8080
cc.unitmesh.untitled.demo.DemoApplicationTests
8181
```
82+
83+
Get method will return code:
84+
85+
/symbol:cc.unitmesh.untitled.demo.MathHelper.calculateInsurance
86+
87+
The output will be:
88+
89+
```java
90+
public static double calculateInsurance(double income) {
91+
if (income <= 10000) {
92+
return income * 0.365;
93+
} else {
94+
return income * 0.365 + 1000;
95+
}
96+
}
97+
```
98+

java/src/main/kotlin/cc/unitmesh/idea/provider/JavaCustomDevInsSymbolProvider.kt

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,59 @@ class JavaCustomDevInsSymbolProvider : DevInsSymbolProvider {
5454
override fun resolveSymbol(project: Project, symbol: String): Iterable<String> {
5555
val scope = GlobalSearchScope.allScope(project)
5656

57+
if (symbol.isEmpty()) return emptyList()
58+
59+
// className only, like `String` not Dot
60+
if (symbol.contains(".").not()) {
61+
val psiClasses = PsiShortNamesCache.getInstance(project).getClassesByName(symbol, scope)
62+
if (psiClasses.isNotEmpty()) {
63+
return psiClasses.map { it.qualifiedName!! }
64+
}
65+
}
66+
5767
// for package name only, like `cc.unitmesh`
5868
JavaFileManagerImpl(project).findPackage(symbol)?.let { pkg ->
5969
return pkg.classes.map { it.qualifiedName!! }
6070
}
6171

62-
// for class name only, like `cc.unitmesh.idea.provider.JavaCustomDevInsSymbolProvider`
63-
val psiClasses = PsiShortNamesCache.getInstance(project).getClassesByName(symbol, scope)
64-
if (psiClasses.isNotEmpty()) {
65-
return psiClasses.map { it.qualifiedName!! }
66-
}
67-
6872
// for single class, with function name, like `cc.unitmesh.idea.provider.JavaCustomDevInsSymbolProvider`
6973
val clazz = JavaFileManagerImpl(project).findClass(symbol, scope)
7074
if (clazz != null) {
7175
return clazz.methods.map { "${clazz.qualifiedName}#${it.name}" }
7276
}
7377

7478
// for lookup for method
79+
val method = symbol.split("#")
80+
if (method.size == 2) {
81+
val clazzName = method[0]
82+
val methodName = method[1]
83+
return lookupWithMethodName(project, clazzName, scope, methodName)
84+
}
7585

86+
// may by not our format, like <package>.<class>.<method> split last
87+
val lastDotIndex = symbol.lastIndexOf(".")
88+
if (lastDotIndex != -1) {
89+
val clazzName = symbol.substring(0, lastDotIndex)
90+
val methodName = symbol.substring(lastDotIndex + 1)
91+
return lookupWithMethodName(project, clazzName, scope, methodName)
92+
}
93+
94+
return emptyList()
95+
}
96+
97+
private fun lookupWithMethodName(
98+
project: Project,
99+
clazzName: String,
100+
scope: GlobalSearchScope,
101+
methodName: String
102+
): List<String> {
103+
val psiClass = JavaFileManagerImpl(project).findClass(clazzName, scope)
104+
if (psiClass != null) {
105+
val psiMethod = psiClass.findMethodsByName(methodName, true).firstOrNull()
106+
if (psiMethod != null) {
107+
return listOf(psiMethod.text)
108+
}
109+
}
76110

77111
return emptyList()
78112
}

0 commit comments

Comments
 (0)