Skip to content

Commit 818bd2b

Browse files
committed
refactor(test): optimize related code formatting and collection
- Simplify related code formatting logic and improve readability. - Refactor related code collection to use a map for better performance and clarity. - Remove unused imports and redundant code.
1 parent ac0b670 commit 818bd2b

File tree

1 file changed

+37
-38
lines changed

1 file changed

+37
-38
lines changed

core/src/main/kotlin/cc/unitmesh/devti/observer/TestAgentObserver.kt

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import com.intellij.execution.ui.ExecutionConsole
1616
import com.intellij.execution.ui.RunContentManager
1717
import com.intellij.openapi.Disposable
1818
import com.intellij.openapi.application.runReadAction
19-
import com.intellij.openapi.editor.markup.RangeHighlighter
2019
import com.intellij.openapi.progress.ProgressIndicator
2120
import com.intellij.openapi.progress.ProgressManager
2221
import com.intellij.openapi.progress.Task
@@ -59,7 +58,11 @@ class TestAgentObserver : AgentObserver, Disposable {
5958
val language = psiElement?.language?.displayName ?: ""
6059
val filepath = psiElement?.containingFile?.virtualFile?.relativePath(project) ?: ""
6160
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+
}
6366
val prompt = """Help me fix follow test issue:
6467
|## ErrorMessage:
6568
|```
@@ -73,7 +76,7 @@ class TestAgentObserver : AgentObserver, Disposable {
7376
|```$language
7477
|$code
7578
|```
76-
|${formatedRelatedCode}
79+
|$formatedRelatedCode
7780
|""".trimMargin()
7881

7982
sendErrorNotification(project, prompt)
@@ -95,45 +98,41 @@ class TestAgentObserver : AgentObserver, Disposable {
9598
val consoleViewImpl: ConsoleViewImpl = getConsoleView(executionConsole) ?: return null
9699
val editor = consoleViewImpl.editor ?: return null
97100

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+
}
126116

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+
}
130131
}
131-
} else {
132-
return@mapNotNull null
133132
}
133+
134+
context
134135
}
135-
136-
return relatedCode.distinct()
137136
}
138137

139138

0 commit comments

Comments
 (0)