@@ -36,6 +36,7 @@ import kotlinx.coroutines.flow.Flow
36
36
import kotlinx.coroutines.flow.cancellable
37
37
import kotlinx.coroutines.flow.flowOf
38
38
import kotlinx.coroutines.launch
39
+ import java.awt.EventQueue.invokeLater
39
40
import kotlin.math.min
40
41
41
42
@@ -119,47 +120,26 @@ class DiffStreamHandler(
119
120
resetState()
120
121
}
121
122
122
- fun streamDiffLinesToEditor (originContent : String , prompt : String ) {
123
- val lines = originContent.lines()
124
-
123
+ fun streamDiffLinesToEditor (originContent : String , prompt : String , editor : Editor ) {
125
124
isRunning = true
126
125
val flow: Flow <String > = LlmFactory .create(project).stream(prompt, " " , false )
127
- var lastLineNo = 0
128
126
AutoDevCoroutineScope .scope(project).launch {
129
127
val suggestion = StringBuilder ()
130
128
flow.cancellable().collect { char ->
131
129
suggestion.append(char)
132
130
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)
156
135
}
157
136
}
158
137
}
159
138
}
160
139
161
140
val code = CodeFence .parse(suggestion.toString())
162
141
newCode = code.text
142
+ normalDiff(originContent, code.text)
163
143
handleFinishedResponse(code.text)
164
144
}
165
145
}
0 commit comments