Skip to content

Commit a44f4ea

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

File tree

3 files changed

+53
-10
lines changed

3 files changed

+53
-10
lines changed

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

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ 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
4039
import kotlin.math.min
4140

4241

@@ -120,26 +119,47 @@ class DiffStreamHandler(
120119
resetState()
121120
}
122121

123-
fun streamDiffLinesToEditor(originContent: String, prompt: String, editor: Editor) {
122+
fun streamDiffLinesToEditor(originContent: String, prompt: String) {
123+
val lines = originContent.lines()
124+
124125
isRunning = true
125126
val flow: Flow<String> = LlmFactory.create(project).stream(prompt, "", false)
127+
var lastLineNo = 0
126128
AutoDevCoroutineScope.scope(project).launch {
127129
val suggestion = StringBuilder()
128130
flow.cancellable().collect { char ->
129131
suggestion.append(char)
130132
val code = CodeFence.parse(suggestion.toString())
131-
if (code.text.isNotEmpty()) {
132-
invokeLater {
133-
runWriteAction {
134-
editor.document.setText(code.text)
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)
135156
}
136157
}
137158
}
138159
}
139160

140161
val code = CodeFence.parse(suggestion.toString())
141162
newCode = code.text
142-
normalDiff(originContent, code.text)
143163
handleFinishedResponse(code.text)
144164
}
145165
}

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@ package cc.unitmesh.devti.sketch.ui.patch
22

33
import cc.unitmesh.devti.AutoDevBundle
44
import cc.unitmesh.devti.diff.DiffStreamHandler
5+
import cc.unitmesh.devti.llms.LlmFactory
56
import cc.unitmesh.devti.sketch.ui.LangSketch
67
import cc.unitmesh.devti.template.GENIUS_CODE
78
import cc.unitmesh.devti.template.TemplateRender
89
import cc.unitmesh.devti.template.context.TemplateContext
10+
import cc.unitmesh.devti.util.AutoDevCoroutineScope
11+
import cc.unitmesh.devti.util.parser.CodeFence
912
import com.intellij.diff.editor.DiffVirtualFileBase
1013
import com.intellij.icons.AllIcons
1114
import com.intellij.lang.Language
15+
import com.intellij.openapi.application.ApplicationManager
16+
import com.intellij.openapi.application.ModalityState
17+
import com.intellij.openapi.application.runWriteAction
1218
import com.intellij.openapi.command.CommandProcessor
1319
import com.intellij.openapi.command.WriteCommandAction
1420
import com.intellij.openapi.command.undo.UndoManager
@@ -28,6 +34,9 @@ import com.intellij.ui.JBColor
2834
import com.intellij.ui.components.JBLabel
2935
import com.intellij.ui.components.panels.HorizontalLayout
3036
import com.intellij.ui.components.panels.VerticalLayout
37+
import kotlinx.coroutines.flow.Flow
38+
import kotlinx.coroutines.flow.cancellable
39+
import kotlinx.coroutines.launch
3140
import java.awt.BorderLayout
3241
import java.awt.event.MouseAdapter
3342
import java.awt.event.MouseEvent
@@ -228,7 +237,21 @@ fun applyDiffRepairSuggestion(project: Project, editor: Editor, oldCode: String,
228237
templateRender.context = DiffRepairContext(oldCode, patchedCode)
229238
val prompt = templateRender.renderTemplate(template)
230239

231-
diffStreamHandler.streamDiffLinesToEditor(oldCode, prompt, editor)
240+
val flow: Flow<String> = LlmFactory.create(project).stream(prompt, "", false)
241+
AutoDevCoroutineScope.scope(project).launch {
242+
val suggestion = StringBuilder()
243+
flow.cancellable().collect { char ->
244+
suggestion.append(char)
245+
val code = CodeFence.parse(suggestion.toString())
246+
if (code.text.isNotEmpty()) {
247+
ApplicationManager.getApplication().invokeLater({
248+
runWriteAction {
249+
editor.document.setText(code.text)
250+
}
251+
}, ModalityState.defaultModalityState())
252+
}
253+
}
254+
}
232255
}
233256

234257
data class DiffRepairContext(

core/src/main/resources/genius/zh/code/sketch.vm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ have created a routes.py and main.js file, and updated the main.html file.
109109
</devin>
110110
// step 4.1, call tools to create test code and run test code
111111
<devin>
112-
/patch:src/test/test_routes.py
113-
```patch
112+
/write:src/test/test_routes.py
113+
```python
114114
// the test code
115115
```
116116
</devin>

0 commit comments

Comments
 (0)