1
1
package ee.carlrobert.codegpt.actions.editor
2
2
3
3
import com.intellij.notification.NotificationType
4
+ import com.intellij.openapi.application.runInEdt
5
+ import com.intellij.openapi.application.runUndoTransparentWriteAction
4
6
import com.intellij.openapi.command.WriteCommandAction.runWriteCommandAction
5
7
import com.intellij.openapi.components.service
6
8
import com.intellij.openapi.editor.Editor
@@ -15,7 +17,7 @@ import ee.carlrobert.llm.client.openai.completion.ErrorDetails
15
17
import ee.carlrobert.llm.completion.CompletionEventListener
16
18
import okhttp3.sse.EventSource
17
19
18
- class EditCodeCompletionHandler (
20
+ class EditCodeCompletionListener (
19
21
private val editor : Editor ,
20
22
private val observableProperties : ObservableProperties ,
21
23
private val selectionTextRange : TextRange
@@ -25,11 +27,11 @@ class EditCodeCompletionHandler(
25
27
private var currentHighlighter: RangeHighlighter ? = null
26
28
27
29
override fun onMessage (message : String , eventSource : EventSource ) {
28
- handleDiff(message)
30
+ runInEdt { handleDiff(message) }
29
31
}
30
32
31
33
override fun onComplete (messageBuilder : StringBuilder ) {
32
- cleanupAndFormat()
34
+ runInEdt { cleanupAndFormat() }
33
35
observableProperties.loading.set(false )
34
36
}
35
37
@@ -40,8 +42,8 @@ class EditCodeCompletionHandler(
40
42
)
41
43
}
42
44
43
- private fun highlightCurrentRow (editor : Editor ) {
44
- currentHighlighter?. let { editor.markupModel.removeHighlighter(it) }
45
+ private fun updateHighlighter (editor : Editor ) {
46
+ cleanupHighlighter()
45
47
46
48
val document = editor.document
47
49
val lineNumber = document.getLineNumber(editor.caretModel.offset)
@@ -59,13 +61,12 @@ class EditCodeCompletionHandler(
59
61
)
60
62
}
61
63
62
-
63
64
private fun handleDiff (message : String ) {
64
- runWriteCommandAction(editor.project) {
65
- val document = editor.document
66
- val startOffset = selectionTextRange.startOffset
67
- val endOffset = selectionTextRange.endOffset
65
+ val document = editor.document
66
+ val startOffset = selectionTextRange.startOffset
67
+ val endOffset = selectionTextRange.endOffset
68
68
69
+ runUndoTransparentWriteAction {
69
70
val remainingOriginalLength = endOffset - (startOffset + replacedLength)
70
71
if (remainingOriginalLength > 0 ) {
71
72
document.replaceString(
@@ -79,41 +80,40 @@ class EditCodeCompletionHandler(
79
80
} else {
80
81
document.insertString(startOffset + replacedLength, message)
81
82
}
82
-
83
- replacedLength + = message.length
84
- editor.caretModel.moveToOffset(startOffset + replacedLength)
85
- highlightCurrentRow(editor)
86
83
}
87
- }
88
84
89
- private fun cleanupHighlighter () {
90
- currentHighlighter?. let { editor.markupModel.removeHighlighter(it) }
91
- currentHighlighter = null
85
+ replacedLength + = message.length
86
+ editor.caretModel.moveToOffset(startOffset + replacedLength)
87
+ updateHighlighter(editor)
92
88
}
93
89
94
90
private fun cleanupAndFormat () {
95
91
val project = editor.project ? : return
96
- runWriteCommandAction(project) {
97
- val document = editor.document
98
- val psiDocumentManager = project.service<PsiDocumentManager >()
99
- val psiFile = psiDocumentManager.getPsiFile(document)
100
- ? : return @runWriteCommandAction
101
- val startOffset = selectionTextRange.startOffset
102
- val endOffset = selectionTextRange.endOffset
103
- val newEndOffset = startOffset + replacedLength
92
+ val document = editor.document
93
+ val psiDocumentManager = project.service<PsiDocumentManager >()
94
+ val psiFile = psiDocumentManager.getPsiFile(document) ? : return
95
+ val startOffset = selectionTextRange.startOffset
96
+ val endOffset = selectionTextRange.endOffset
97
+ val newEndOffset = startOffset + replacedLength
104
98
99
+ runWriteCommandAction(project) {
105
100
if (newEndOffset < endOffset) {
106
101
document.deleteString(newEndOffset, endOffset)
107
102
}
108
-
109
103
psiDocumentManager.commitDocument(document)
110
104
project.service<CodeStyleManager >().reformatText(
111
105
psiFile,
112
106
listOf (TextRange (startOffset, newEndOffset))
113
107
)
114
- editor.caretModel.moveToOffset(newEndOffset)
115
- psiDocumentManager.doPostponedOperationsAndUnblockDocument(document)
116
- cleanupHighlighter()
117
108
}
109
+
110
+ editor.caretModel.moveToOffset(newEndOffset)
111
+ psiDocumentManager.doPostponedOperationsAndUnblockDocument(document)
112
+ cleanupHighlighter()
113
+ }
114
+
115
+ private fun cleanupHighlighter () {
116
+ currentHighlighter?.let { editor.markupModel.removeHighlighter(it) }
117
+ currentHighlighter = null
118
118
}
119
119
}
0 commit comments