Skip to content

Commit 30a98ce

Browse files
committed
refactor(structure): enhance structure output formatting #309
1 parent 8e13356 commit 30a98ce

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import com.intellij.openapi.editor.Document
1313
import com.intellij.openapi.fileEditor.FileEditor
1414
import com.intellij.openapi.fileEditor.FileEditorManager
1515
import com.intellij.openapi.project.Project
16+
import com.intellij.openapi.project.guessProjectDir
1617
import com.intellij.openapi.vfs.VirtualFile
1718
import com.intellij.psi.PsiDocumentManager
1819
import com.intellij.psi.PsiElement
@@ -32,6 +33,7 @@ class StructureInCommand(val myProject: Project, val prop: String) : InsCommand
3233
*/
3334
private val maxLineWith = 11
3435
private val maxDepth = 5
36+
private val maxLinesForShowLinNO = 150
3537

3638
private val logger = logger<StructureInCommand>()
3739
override suspend fun execute(): String? {
@@ -49,7 +51,10 @@ class StructureInCommand(val myProject: Project, val prop: String) : InsCommand
4951
}.get()
5052
} ?: return null
5153

52-
return "```\n" + getFileStructure(myProject, virtualFile, psiFile) + "\n```"
54+
val structure = getFileStructure(myProject, virtualFile, psiFile)
55+
val baseDir = myProject.guessProjectDir().toString()
56+
val filepath = virtualFile.path.removePrefix(baseDir)
57+
return "// $filepath\n```\n" + structure + "\n```"
5358
}
5459

5560
fun getFileStructure(project: Project, file: VirtualFile, psiFile: PsiFile): String {
@@ -95,10 +100,6 @@ class StructureInCommand(val myProject: Project, val prop: String) : InsCommand
95100
}
96101

97102
private fun formatBeforeCode(element: StructureViewTreeElement, depth: Int): String {
98-
if (depth > maxDepth) {
99-
return " ".repeat(maxLineWith) + " ".repeat(depth)
100-
}
101-
102103
return if (element.value is PsiElement) {
103104
val psiElement = element.value as PsiElement
104105
val line = formatLine(psiElement)
@@ -118,7 +119,11 @@ class StructureInCommand(val myProject: Project, val prop: String) : InsCommand
118119
val start = document.getLineNumber(psiElement.textRange.startOffset)
119120
val end = document.getLineNumber(psiElement.textRange.endOffset)
120121

121-
return "(${start + 1}-${end + 1})"
122+
if (end - start > maxLinesForShowLinNO) {
123+
return "(${start + 1}-${end + 1})"
124+
}
125+
126+
return " ".repeat(maxDepth)
122127
}
123128

124129
fun file(project: Project, path: String): VirtualFile? {

0 commit comments

Comments
 (0)