Skip to content

Commit 0e32fd1

Browse files
committed
feat(diff): enhance SingleFileDiffView with editor param and optimize stream handling```
refactor(d
1 parent ab3a095 commit 0e32fd1

File tree

2 files changed

+8
-28
lines changed

2 files changed

+8
-28
lines changed

core/src/main/kotlin/cc/unitmesh/devti/diff/DiffStreamHandler.kt

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import kotlinx.coroutines.flow.Flow
3636
import kotlinx.coroutines.flow.cancellable
3737
import kotlinx.coroutines.flow.flowOf
3838
import kotlinx.coroutines.launch
39+
import java.awt.EventQueue.invokeLater
3940
import kotlin.math.min
4041

4142

@@ -119,47 +120,26 @@ class DiffStreamHandler(
119120
resetState()
120121
}
121122

122-
fun streamDiffLinesToEditor(originContent: String, prompt: String) {
123-
val lines = originContent.lines()
124-
123+
fun streamDiffLinesToEditor(originContent: String, prompt: String, editor: Editor) {
125124
isRunning = true
126125
val flow: Flow<String> = LlmFactory.create(project).stream(prompt, "", false)
127-
var lastLineNo = 0
128126
AutoDevCoroutineScope.scope(project).launch {
129127
val suggestion = StringBuilder()
130128
flow.cancellable().collect { char ->
131129
suggestion.append(char)
132130
val code = CodeFence.parse(suggestion.toString())
133-
var value: List<String> = code.text.lines()
134-
if (value.isEmpty()) return@collect
135-
136-
val newLines = if (lastLineNo < value.size) {
137-
value.subList(lastLineNo, value.size)
138-
} else {
139-
listOf()
140-
}
141-
142-
if (newLines.isEmpty()) return@collect
143-
144-
val flowValue: Flow<String> = flowOf(*newLines.toTypedArray())
145-
val oldLinesContent = if (lastLineNo + newLines.size <= lines.size) {
146-
lines.subList(lastLineNo, lastLineNo + newLines.size)
147-
} else {
148-
listOf()
149-
}
150-
lastLineNo = value.size
151-
152-
streamDiff(oldLinesContent, flowValue).collect {
153-
ApplicationManager.getApplication().invokeLater {
154-
WriteCommandAction.runWriteCommandAction(project) {
155-
updateByDiffType(it)
131+
if (code.text.isNotEmpty()) {
132+
invokeLater {
133+
runWriteAction {
134+
editor.document.setText(code.text)
156135
}
157136
}
158137
}
159138
}
160139

161140
val code = CodeFence.parse(suggestion.toString())
162141
newCode = code.text
142+
normalDiff(originContent, code.text)
163143
handleFinishedResponse(code.text)
164144
}
165145
}

core/src/main/kotlin/cc/unitmesh/devti/sketch/ui/patch/SingleFileDiffView.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ fun applyDiffRepairSuggestion(project: Project, editor: Editor, oldCode: String,
228228
templateRender.context = DiffRepairContext(oldCode, patchedCode)
229229
val prompt = templateRender.renderTemplate(template)
230230

231-
diffStreamHandler.streamDiffLinesToEditor(oldCode, prompt)
231+
diffStreamHandler.streamDiffLinesToEditor(oldCode, prompt, editor)
232232
}
233233

234234
data class DiffRepairContext(

0 commit comments

Comments
 (0)