Skip to content

Commit d20961f

Browse files
committed
feat(devins-java): add resolveSymbol method to DevInsSymbolProvider #101
Add resolveSymbol method to DevInsSymbolProvider interface to resolve symbols for different programming languages.
1 parent bbf4b35 commit d20961f

File tree

4 files changed

+44
-7
lines changed

4 files changed

+44
-7
lines changed

exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/compiler/DevInsCompiler.kt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import cc.unitmesh.devti.language.parser.CodeBlockElement
66
import cc.unitmesh.devti.language.psi.DevInFile
77
import cc.unitmesh.devti.language.psi.DevInTypes
88
import cc.unitmesh.devti.language.psi.DevInUsed
9+
import cc.unitmesh.devti.provider.devins.DevInsSymbolProvider
910
import com.intellij.openapi.diagnostic.logger
1011
import com.intellij.openapi.editor.Editor
1112
import com.intellij.openapi.project.Project
@@ -114,7 +115,8 @@ class DevInsCompiler(private val myProject: Project, val file: DevInFile, val ed
114115
}
115116

116117
BuiltinCommand.SYMBOL -> {
117-
PrintInsCommand("/" + commandNode.commandName + ":" + prop)
118+
result.isLocalCommand = true
119+
SymbolInsCommand(myProject, prop)
118120
}
119121

120122
BuiltinCommand.WRITE -> {
@@ -192,4 +194,19 @@ class DevInsCompiler(private val myProject: Project, val file: DevInFile, val ed
192194
}
193195
}
194196

197+
class SymbolInsCommand(val myProject: Project, val prop: String) :
198+
InsCommand {
199+
override fun execute(): String? {
200+
val result = DevInsSymbolProvider.all().map {
201+
it.resolveSymbol(myProject, prop).joinToString("\n")
202+
}
203+
204+
if (result.isEmpty()) {
205+
return "<DevliError> No symbol found: $prop"
206+
}
207+
208+
return result.joinToString("\n")
209+
}
210+
}
211+
195212

exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/completion/BuiltinCommandProvider.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ enum class BuiltinCommand(
2323
* - Kotlin: [org.jetbrains.kotlin.idea.completion.KotlinCompletionContributor]
2424
* - Python: [com.jetbrains.python.codeInsight.completion.PyClassNameCompletionContributor]
2525
*/
26-
SYMBOL("symbol", "[TODO] Read content by Java/Kotlin canonicalName", AllIcons.Actions.GroupBy, true),
26+
SYMBOL("symbol", "[TODO] Read content by Java/Kotlin canonicalName", AllIcons.Actions.GroupBy, true, true),
2727
WRITE("write", "Write content to a file, /write:path/to/file:L1-L2", AllIcons.Actions.Edit, true, true),
2828
PATCH("patch", "Apply patch to a file, /patch:path/to/file", AllIcons.Vcs.Patch_file, false),
2929
RUN("run", "Run the content of a file", AllIcons.Actions.Execute, false),

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ import com.intellij.openapi.project.Project
99
import com.intellij.psi.PsiManager
1010
import com.intellij.psi.PsiPackageStatement
1111
import com.intellij.psi.search.FileTypeIndex
12+
import com.intellij.psi.search.GlobalSearchScope
1213
import com.intellij.psi.search.ProjectScope
14+
import com.intellij.psi.search.PsiShortNamesCache
1315
import com.intellij.psi.util.PsiTreeUtil
1416
import com.intellij.util.SmartList
1517

18+
1619
class JavaCustomDevInsSymbolProvider : DevInsSymbolProvider {
1720
override fun lookupSymbol(
1821
project: Project,
@@ -43,4 +46,16 @@ class JavaCustomDevInsSymbolProvider : DevInsSymbolProvider {
4346

4447
return lookupElements
4548
}
49+
50+
override fun resolveSymbol(project: Project, symbol: String): Iterable<String> {
51+
val scope = GlobalSearchScope.allScope(project)
52+
53+
// for class name only
54+
val psiClasses = PsiShortNamesCache.getInstance(project).getClassesByName(symbol, scope)
55+
if (psiClasses.isNotEmpty()) {
56+
return psiClasses.map { it.qualifiedName!! }
57+
}
58+
59+
return emptyList()
60+
}
4661
}

src/main/kotlin/cc/unitmesh/devti/provider/devins/DevInsSymbolProvider.kt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,9 @@ import com.intellij.openapi.project.Project
1111
* - Completion will be triggered by like `/symbol:`, and the symbol provider will provide the completion for the symbol.
1212
* - Execution will be triggered by like `/symbol:java.lang.String`, all load children level elements, like `java.lang.String#length()`
1313
*
14-
* For execution:
15-
* - If parent is Root, the children will be packages
16-
* - If parent is Package, the children will be classes
17-
* - If parent is Class, the children will be methods and fields
14+
* For execution, see in [DevInsSymbolProvider.resolveSymbol]
1815
*/
1916
interface DevInsSymbolProvider {
20-
2117
/**
2218
* Lookup canonical name for different language
2319
*/
@@ -27,6 +23,15 @@ interface DevInsSymbolProvider {
2723
result: CompletionResultSet
2824
): Iterable<LookupElement>
2925

26+
/**
27+
* Resolves the symbol for different programming languages.
28+
* For example, in Java:
29+
* - If the parent is Root, the children will be packages
30+
* - If the parent is Package, the children will be classes
31+
* - If the parent is Class, the children will be methods and fields
32+
*/
33+
fun resolveSymbol(project: Project, symbol: String): Iterable<String>
34+
3035
companion object {
3136
private val EP_NAME: ExtensionPointName<DevInsSymbolProvider> =
3237
ExtensionPointName("cc.unitmesh.customDevInsCompletionProvider")

0 commit comments

Comments
 (0)