Skip to content

Commit 7a964d0

Browse files
committed
feat(devins-lang): enhance usage command output with file path and line number #358
- Add file path and line number information to the usage command output - Improve the readability and usefulness of the command result - Update usage command example in toolExamples
1 parent 70e86e7 commit 7a964d0

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@ import cc.unitmesh.devti.devin.dataprovider.BuiltinCommand
55
import cc.unitmesh.devti.language.compiler.error.DEVINS_ERROR
66
import cc.unitmesh.devti.provider.RelatedClassesProvider
77
import cc.unitmesh.devti.provider.devins.DevInsSymbolProvider
8+
import cc.unitmesh.devti.util.relativePath
89
import com.intellij.openapi.application.runReadAction
910
import com.intellij.openapi.project.Project
11+
import com.intellij.psi.PsiNamedElement
12+
import com.intellij.psi.PsiElement
13+
import com.intellij.psi.PsiFile
14+
import com.intellij.openapi.editor.Document
15+
import com.intellij.psi.PsiDocumentManager
1016

1117
class UsageInsCommand(val myProject: Project, private val symbol: String) : InsCommand {
1218
override val commandName: BuiltinCommand = BuiltinCommand.RELATED
@@ -24,8 +30,25 @@ class UsageInsCommand(val myProject: Project, private val symbol: String) : InsC
2430

2531
if (psiElements.isEmpty()) return "$DEVINS_ERROR: No usage found for $symbol"
2632

27-
return "Here is related to $symbol usage" + psiElements.joinToString("\n\n") {
28-
runReadAction { it.text }
33+
return "Here is related to $symbol usage:\n\n" + psiElements.joinToString("\n\n") { element: PsiNamedElement ->
34+
runReadAction {
35+
val (filePath, lineRange) = getFileAndLineInfo(element)
36+
"Follow code from $filePath (line $lineRange):\n${element.text}"
37+
}
2938
}
3039
}
40+
41+
private fun getFileAndLineInfo(element: PsiElement): Pair<String, String> {
42+
val containingFile = element.containingFile
43+
val virtualFile = containingFile.virtualFile
44+
val filePath = virtualFile?.relativePath(myProject) ?: "unknown file"
45+
46+
val document = PsiDocumentManager.getInstance(myProject).getDocument(containingFile)
47+
val startLine = document?.getLineNumber(element.textRange.startOffset)?.plus(1) ?: 0
48+
val endLine = document?.getLineNumber(element.textRange.endOffset)?.plus(1) ?: 0
49+
50+
val lineRange = if (startLine == endLine) "$startLine" else "$startLine-$endLine"
51+
52+
return Pair(filePath, lineRange)
53+
}
3154
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
/usage:cc.unitmesh.untitled.demo.service.BlogService.createBlog
1+
Query usage of some method, should use following format: <package>.<class>.<method>
2+
/usage:cc.unitmesh.untitled.demo.service.BlogService.createBlog
3+
the return include file path, usage's method (caller) text.

exts/ext-vue/src/233/main/kotlin/cc/unitmesh/vue/provider/VueRelatedClassProvider.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,9 @@ class VueRelatedClassProvider : RelatedClassesProvider {
134134
private fun symbolLocationFromPropertySignature(property: TypeScriptPropertySignature): WebTypesSymbolLocation? {
135135
if (!property.isValid) return null
136136

137-
// TypeScript GlobalComponents definition
138137
val symbolName = property.memberName.takeIf { it.isNotEmpty() }
139138
?: return null
140139

141-
// Locate module
142140
val packageName =
143141
property.containingFile?.originalFile?.virtualFile?.let { PackageJsonUtil.findUpPackageJson(it) }
144142
?.let { PackageJsonData.getOrCreate(it) }

0 commit comments

Comments
 (0)