@@ -9,10 +9,12 @@ import com.intellij.lang.LanguageStructureViewBuilder
9
9
import com.intellij.openapi.application.ApplicationManager
10
10
import com.intellij.openapi.application.runReadAction
11
11
import com.intellij.openapi.diagnostic.logger
12
+ import com.intellij.openapi.editor.Document
12
13
import com.intellij.openapi.fileEditor.FileEditor
13
14
import com.intellij.openapi.fileEditor.FileEditorManager
14
15
import com.intellij.openapi.project.Project
15
16
import com.intellij.openapi.vfs.VirtualFile
17
+ import com.intellij.psi.PsiDocumentManager
16
18
import com.intellij.psi.PsiElement
17
19
import com.intellij.psi.PsiFile
18
20
import com.intellij.psi.PsiManager
@@ -23,6 +25,14 @@ import kotlinx.coroutines.withContext
23
25
class StructureInCommand (val myProject : Project , val prop : String ) : InsCommand {
24
26
override val commandName: BuiltinCommand = BuiltinCommand .STRUCTURE
25
27
28
+ /* *
29
+ * ```
30
+ * (1000-9999)
31
+ * ```
32
+ */
33
+ private val maxLineWith = 11
34
+ private val maxDepth = 5
35
+
26
36
private val logger = logger<StructureInCommand >()
27
37
override suspend fun execute (): String? {
28
38
val virtualFile = file(myProject, prop)
@@ -43,14 +53,9 @@ class StructureInCommand(val myProject: Project, val prop: String) : InsCommand
43
53
}
44
54
45
55
fun getFileStructure (project : Project , file : VirtualFile , psiFile : PsiFile ): String {
46
- var openFiles: Array <FileEditor > = arrayOf()
47
- ApplicationManager .getApplication().invokeAndWait {
48
- openFiles = FileEditorManager .getInstance(myProject).openFile(file, true )
49
- }
50
-
51
- val fileEditor = openFiles.firstOrNull() ? : return " No FileEditor found."
52
-
53
56
val viewFactory = LanguageStructureViewBuilder .INSTANCE .forLanguage(psiFile.language)
57
+ val fileEditor: FileEditor = FileEditorManager .getInstance(project).getEditors(file).firstOrNull()
58
+ ? : return " No FileEditor found."
54
59
55
60
if (viewFactory != null ) {
56
61
val view: StructureView = viewFactory.getStructureViewBuilder(psiFile)
@@ -64,36 +69,22 @@ class StructureInCommand(val myProject: Project, val prop: String) : InsCommand
64
69
return " No StructureViewModel found."
65
70
}
66
71
67
- // (1000-9999)
68
- private val maxLineWith = 11
69
-
70
72
/* *
71
73
* Display format
72
74
* ```
73
75
* elementName (location) - line number or navigate to element ?
74
76
* ```
75
77
*/
76
78
private fun traverseStructure (element : StructureViewTreeElement , depth : Int , sb : StringBuilder ): StringBuilder {
77
- val indent = if (element.value is PsiElement ) {
78
- val psiElement = element.value as PsiElement
79
- val line = " (" + psiElement.textRange.startOffset + " ," + psiElement.textRange.endOffset + " )"
80
- if (line.length < maxLineWith) {
81
- line + " " .repeat(maxLineWith - line.length) + " " .repeat(depth)
82
- } else {
83
- line + " " .repeat(depth)
84
- }
85
- } else {
86
- " " .repeat(depth)
87
- }
88
-
89
- // / todo: add element line
79
+ val indent = formatBeforeCode(element, depth)
90
80
var str = element.presentation.presentableText
91
- if (! str.isNullOrBlank() && ! element.presentation.locationString.isNullOrBlank()) {
92
- str + = " (${element.presentation.locationString} )"
81
+ // if (!str.isNullOrBlank() && !element.presentation.locationString.isNullOrBlank()) {
82
+ // str += " (${element.presentation.locationString})"
83
+ // }
84
+ if (! str.isNullOrBlank()) {
85
+ sb.append(indent).append(str).append(" \n " )
93
86
}
94
87
95
- sb.append(indent).append(str).append(" \n " )
96
-
97
88
for (child in element.children) {
98
89
if (child is StructureViewTreeElement ) {
99
90
traverseStructure(child, depth + 1 , sb)
@@ -103,6 +94,33 @@ class StructureInCommand(val myProject: Project, val prop: String) : InsCommand
103
94
return sb
104
95
}
105
96
97
+ private fun formatBeforeCode (element : StructureViewTreeElement , depth : Int ): String {
98
+ if (depth > maxDepth) {
99
+ return " " .repeat(maxLineWith) + " " .repeat(depth)
100
+ }
101
+
102
+ return if (element.value is PsiElement ) {
103
+ val psiElement = element.value as PsiElement
104
+ val line = formatLine(psiElement)
105
+ if (line.length < maxLineWith) {
106
+ line + " " .repeat(maxLineWith - line.length) + " " .repeat(depth)
107
+ } else {
108
+ line + " " .repeat(depth)
109
+ }
110
+ } else {
111
+ " " .repeat(maxLineWith) + " " .repeat(depth)
112
+ }
113
+ }
114
+
115
+ private fun formatLine (psiElement : PsiElement ): String {
116
+ val psiFile: PsiFile = psiElement.containingFile
117
+ val document: Document = PsiDocumentManager .getInstance(psiFile.project).getDocument(psiFile) ? : return " "
118
+ val start = document.getLineNumber(psiElement.textRange.startOffset)
119
+ val end = document.getLineNumber(psiElement.textRange.endOffset)
120
+
121
+ return " (${start + 1 } -${end + 1 } )"
122
+ }
123
+
106
124
fun file (project : Project , path : String ): VirtualFile ? {
107
125
val filename = path.split(" #" )[0 ]
108
126
val virtualFile = project.lookupFile(filename)
0 commit comments