@@ -16,7 +16,6 @@ import com.intellij.execution.ui.ExecutionConsole
16
16
import com.intellij.execution.ui.RunContentManager
17
17
import com.intellij.openapi.Disposable
18
18
import com.intellij.openapi.application.runReadAction
19
- import com.intellij.openapi.editor.markup.RangeHighlighter
20
19
import com.intellij.openapi.progress.ProgressIndicator
21
20
import com.intellij.openapi.progress.ProgressManager
22
21
import com.intellij.openapi.progress.Task
@@ -59,7 +58,11 @@ class TestAgentObserver : AgentObserver, Disposable {
59
58
val language = psiElement?.language?.displayName ? : " "
60
59
val filepath = psiElement?.containingFile?.virtualFile?.relativePath(project) ? : " "
61
60
val code = runReadAction<String > { psiElement?.text ? : " " }
62
- val formatedRelatedCode = " \n ## related code:\n ```$language \n ${relatedCode.joinToString(" \n " )} \n ```"
61
+ val formatedRelatedCode = if (relatedCode.isNotEmpty()) {
62
+ " \n ## Related Code:\n ${relatedCode.joinToString(" \n\n " )} "
63
+ } else {
64
+ " "
65
+ }
63
66
val prompt = """ Help me fix follow test issue:
64
67
|## ErrorMessage:
65
68
|```
@@ -73,7 +76,7 @@ class TestAgentObserver : AgentObserver, Disposable {
73
76
|```$language
74
77
|$code
75
78
|```
76
- |${ formatedRelatedCode}
79
+ |$formatedRelatedCode
77
80
|""" .trimMargin()
78
81
79
82
sendErrorNotification(project, prompt)
@@ -95,45 +98,41 @@ class TestAgentObserver : AgentObserver, Disposable {
95
98
val consoleViewImpl: ConsoleViewImpl = getConsoleView(executionConsole) ? : return null
96
99
val editor = consoleViewImpl.editor ? : return null
97
100
98
- val startOffset = 0
99
- val endOffset = editor.document.textLength
100
- val textRange = TextRange (startOffset, endOffset)
101
- val highlighters: Array <RangeHighlighter > = editor.markupModel.allHighlighters
102
-
103
- // / todo: first collect file path and line number, then build by range
104
- val relatedCode = highlighters.mapNotNull { highlighter ->
105
- if (textRange.contains(highlighter.range!! )) {
106
- // call be .DiffHyperlink
107
- val hyperlinkInfo: FileHyperlinkInfo ? = EditorHyperlinkSupport .getHyperlinkInfo(highlighter) as ? FileHyperlinkInfo
108
- val descriptor = hyperlinkInfo?.descriptor ? : return @mapNotNull null
109
- val virtualFile: VirtualFile = descriptor.file
110
-
111
- val isProjectFile = runReadAction { project.isInProject(virtualFile) }
112
- if (isProjectFile) {
113
- val lineNumber = descriptor.line
114
- val allText = virtualFile.readText()
115
- val startLine = if (lineNumber - 10 < 0 ) {
116
- 0
117
- } else {
118
- lineNumber - 10
119
- }
120
- // endLine should be less than allText.lines().size
121
- val endLine = if (lineNumber + 10 > allText.lines().size) {
122
- allText.lines().size
123
- } else {
124
- lineNumber + 10
125
- }
101
+ val textRange = TextRange (0 , editor.document.textLength)
102
+ val highlighters = editor.markupModel.allHighlighters
103
+
104
+ val fileLineMap = mutableMapOf<VirtualFile , MutableSet <Int >>()
105
+ highlighters.forEach { highlighter ->
106
+ if (! textRange.contains(highlighter.range!! )) return @forEach
107
+
108
+ val hyperlinkInfo = EditorHyperlinkSupport .getHyperlinkInfo(highlighter) as ? FileHyperlinkInfo ? : return @forEach
109
+ val descriptor = hyperlinkInfo.descriptor ? : return @forEach
110
+ val virtualFile = descriptor.file
111
+
112
+ if (runReadAction { project.isInProject(virtualFile) }) {
113
+ fileLineMap.computeIfAbsent(virtualFile) { mutableSetOf () }.add(descriptor.line)
114
+ }
115
+ }
126
116
127
- return @mapNotNull allText.lines().subList(startLine, endLine).joinToString(" \n " )
128
- } else {
129
- return @mapNotNull null
117
+ return fileLineMap.map { (virtualFile, lineNumbers) ->
118
+ val contentLines = virtualFile.readText().lines()
119
+ val context = buildString {
120
+ append(" ## File: ${runReadAction { virtualFile.relativePath(project) }} \n " )
121
+ val displayLines = mutableSetOf<Int >()
122
+ lineNumbers.forEach { lineNum ->
123
+ val range = (maxOf(0 , lineNum - 10 ) until minOf(contentLines.size, lineNum + 10 ))
124
+ displayLines.addAll(range)
125
+ }
126
+
127
+ displayLines.sorted().forEach { lineIdx ->
128
+ contentLines.getOrNull(lineIdx)?.let { line ->
129
+ append(" ${lineIdx + 1 } : $line \n " )
130
+ }
130
131
}
131
- } else {
132
- return @mapNotNull null
133
132
}
133
+
134
+ context
134
135
}
135
-
136
- return relatedCode.distinct()
137
136
}
138
137
139
138
0 commit comments