Skip to content

Commit 5eecf11

Browse files
committed
feat(devins): add usage command to get class, method, or field usage in the project #358
- Add USAGE command to BuiltinCommand enum - Implement UsageInsCommand to handle usage retrieval - Update DevInsCompiler to support the new command - Extend completion contributor for USAGE command support - Add example for usage command in toolExamples
1 parent a8fc7bf commit 5eecf11

File tree

6 files changed

+44
-1
lines changed

6 files changed

+44
-1
lines changed

core/src/main/kotlin/cc/unitmesh/devti/devin/dataprovider/BuiltinCommand.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@ enum class BuiltinCommand(
121121
true,
122122
true
123123
),
124+
USAGE(
125+
"usage",
126+
"Get the usage of a class, method, or field in the project",
127+
AllIcons.Actions.DynamicUsages,
128+
true,
129+
true
130+
),
124131
TOOLCHAIN_COMMAND(
125132
"x",
126133
"Execute custom toolchain command",

core/src/main/kotlin/cc/unitmesh/devti/sketch/AutoSketchMode.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class AutoSketchMode(val project: Project) {
8585
RELATED,
8686
RIPGREP_SEARCH,
8787
RULE,
88+
USAGE,
8889
BROWSE,
8990
)
9091

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,11 @@ class DevInsCompiler(
310310
RuleInsCommand(myProject, prop)
311311
}
312312

313+
BuiltinCommand.USAGE -> {
314+
result.isLocalCommand = true
315+
UsageInsCommand(myProject, prop)
316+
}
317+
313318
BuiltinCommand.OPEN -> {
314319
result.isLocalCommand = true
315320
OpenInsCommand(myProject, prop)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package cc.unitmesh.devti.language.compiler.exec
2+
3+
import cc.unitmesh.devti.devin.InsCommand
4+
import cc.unitmesh.devti.devin.dataprovider.BuiltinCommand
5+
import cc.unitmesh.devti.language.compiler.error.DEVINS_ERROR
6+
import cc.unitmesh.devti.provider.RelatedClassesProvider
7+
import cc.unitmesh.devti.provider.devins.DevInsSymbolProvider
8+
import com.intellij.openapi.application.runReadAction
9+
import com.intellij.openapi.project.Project
10+
11+
class UsageInsCommand(val myProject: Project, private val symbol: String) : InsCommand {
12+
override val commandName: BuiltinCommand = BuiltinCommand.RELATED
13+
14+
override suspend fun execute(): String? {
15+
val elements = DevInsSymbolProvider.all().map {
16+
runReadAction { it.resolveElement(myProject, symbol) }
17+
}.flatten()
18+
19+
if (elements.isEmpty()) return "$DEVINS_ERROR: No symbol found for $symbol"
20+
21+
val psiElements = elements.mapNotNull {
22+
RelatedClassesProvider.provide(it.language)?.lookupCallee(myProject, it)
23+
}.flatten()
24+
25+
if (psiElements.isEmpty()) return "$DEVINS_ERROR: No usage found for $symbol"
26+
27+
return psiElements.joinToString("\n") { it.text }
28+
}
29+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class DevInCompletionContributor : CompletionContributor() {
3838
)
3939
extend(
4040
CompletionType.BASIC,
41-
(valuePatterns(listOf(BuiltinCommand.SYMBOL, BuiltinCommand.RELATED))),
41+
(valuePatterns(listOf(BuiltinCommand.SYMBOL, BuiltinCommand.RELATED, BuiltinCommand.USAGE))),
4242
SymbolReferenceLanguageProvider()
4343
)
4444
extend(
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/usage:cc.unitmesh.untitled.demo.service.BlogService.createBlog

0 commit comments

Comments
 (0)