@@ -2,16 +2,25 @@ package cc.unitmesh.devti.observer
2
2
3
3
import cc.unitmesh.devti.provider.observer.AgentObserver
4
4
import cc.unitmesh.devti.util.relativePath
5
+ import com.intellij.execution.impl.ConsoleViewImpl
5
6
import com.intellij.execution.testframework.sm.runner.SMTRunnerEventsAdapter
6
7
import com.intellij.execution.testframework.sm.runner.SMTRunnerEventsListener
7
8
import com.intellij.execution.testframework.sm.runner.SMTestProxy
9
+ import com.intellij.execution.testframework.sm.runner.ui.SMTRunnerConsoleView
10
+ import com.intellij.execution.ui.ExecutionConsole
11
+ import com.intellij.execution.ui.RunContentManager
8
12
import com.intellij.openapi.Disposable
9
13
import com.intellij.openapi.application.runInEdt
10
14
import com.intellij.openapi.application.runReadAction
15
+ import com.intellij.openapi.editor.Editor
16
+ import com.intellij.openapi.editor.markup.RangeHighlighter
11
17
import com.intellij.openapi.project.Project
18
+ import com.intellij.openapi.util.TextRange
12
19
import com.intellij.psi.search.GlobalSearchScope
13
20
import com.intellij.psi.search.ProjectScope
21
+ import com.intellij.refactoring.suggested.range
14
22
import com.intellij.util.messages.MessageBusConnection
23
+ import java.lang.reflect.Field
15
24
16
25
class TestAgentObserver : AgentObserver , Disposable {
17
26
private var connection: MessageBusConnection ? = null
@@ -26,31 +35,76 @@ class TestAgentObserver : AgentObserver, Disposable {
26
35
27
36
private fun sendResult (test : SMTestProxy , project : Project , searchScope : GlobalSearchScope ) {
28
37
runInEdt {
38
+ getConsoleEditor(project)
29
39
val sourceCode = test.getLocation(project, searchScope)
30
40
val psiElement = sourceCode?.psiElement
31
41
val language = psiElement?.language?.displayName ? : " "
32
42
val filepath = psiElement?.containingFile?.virtualFile?.relativePath(project) ? : " "
33
43
val code = runReadAction<String > { psiElement?.text ? : " " }
34
44
val prompt = """ Help me fix follow test issue:
35
- | ErrorMessage:
45
+ |## ErrorMessage:
36
46
|```
37
47
|${test.errorMessage}
38
48
|```
39
- |stacktrace details:
49
+ |## stacktrace details:
40
50
|${test.stacktrace}
41
51
|
42
- |// filepath: $filepath
52
+ |## filepath: $filepath
43
53
|origin code:
44
54
|```$language
45
55
|$code
46
56
|```
47
57
|""" .trimMargin()
48
58
49
-
50
59
sendErrorNotification(project, prompt)
51
60
}
52
61
}
53
62
63
+ /* *
64
+ *```kotlin
65
+ * (content.component.components.firstOrNull() as? NonOpaquePanel)?.components?.firstOrNull { it is JBRunnerTabs }
66
+ *```
67
+ */
68
+ private fun getConsoleEditor (project : Project ): Editor ? {
69
+ val content = RunContentManager .getInstance(project).selectedContent ? : return null
70
+ val executionConsole = content.executionConsole ? : return null
71
+ val consoleViewImpl: ConsoleViewImpl = getConsoleView(executionConsole) ? : return null
72
+ val editor = consoleViewImpl.editor ? : return null
73
+
74
+ val startOffset = 0
75
+ val allText = editor.document.text
76
+ val endOffset = editor.document.textLength
77
+ val textRange = TextRange (startOffset, endOffset)
78
+ val highlighters: Array <RangeHighlighter > = editor.markupModel.allHighlighters
79
+
80
+ for (highlighter in highlighters) {
81
+ if (textRange.contains(highlighter.range!! )) {
82
+ // val hyperlinkInfo: FileHyperlinkInfo? = EditorHyperlinkSupport.getHyperlinkInfo(highlighter) as FileHyperlinkInfo
83
+ // val descriptor = hyperlinkInfo?.descriptor ?: return null
84
+ val range = highlighter.range!!
85
+ val hyperlinkText = allText.substring(range.startOffset, range.endOffset)
86
+ println (" hyperlinkText: $hyperlinkText " )
87
+ }
88
+ }
89
+
90
+ return consoleViewImpl.editor
91
+ }
92
+
93
+ private fun getConsoleView (executionConsole : ExecutionConsole ): ConsoleViewImpl ? {
94
+ if (executionConsole is SMTRunnerConsoleView ) {
95
+ try {
96
+ val resultsViewerClass = executionConsole.resultsViewer::class .java
97
+ val myConsoleViewField: Field = resultsViewerClass.getDeclaredField(" myConsoleView" )
98
+ myConsoleViewField.isAccessible = true
99
+ val myConsoleView = myConsoleViewField.get(executionConsole.resultsViewer) as ? ConsoleViewImpl
100
+ return myConsoleView
101
+ } catch (e: Exception ) {
102
+ e.printStackTrace()
103
+ }
104
+ }
105
+ return executionConsole as ? ConsoleViewImpl
106
+ }
107
+
54
108
override fun dispose () {
55
109
connection?.disconnect()
56
110
}
0 commit comments