@@ -3,6 +3,8 @@ package cc.unitmesh.devti.editor.inlay
3
3
import com.intellij.temporary.inlay.presentation.LLMInlayRenderer
4
4
import cc.unitmesh.devti.intentions.action.task.CodeCompletionTask
5
5
import cc.unitmesh.devti.intentions.action.task.CodeCompletionRequest
6
+ import cc.unitmesh.devti.util.parser.Code
7
+ import cc.unitmesh.devti.util.parser.PostCodeProcessor
6
8
import com.intellij.injected.editor.EditorWindow
7
9
import com.intellij.openapi.Disposable
8
10
import com.intellij.openapi.application.ApplicationManager
@@ -22,7 +24,6 @@ import com.intellij.openapi.util.TextRange
22
24
import com.intellij.psi.util.PsiUtilBase
23
25
import com.intellij.util.concurrency.annotations.RequiresBackgroundThread
24
26
import com.intellij.util.concurrency.annotations.RequiresEdt
25
- import java.util.function.Consumer
26
27
27
28
28
29
class LLMInlayManagerImpl : LLMInlayManager {
@@ -114,13 +115,23 @@ class LLMInlayManagerImpl : LLMInlayManager {
114
115
115
116
override fun editorModified (editor : Editor , changeOffset : Int ) {
116
117
disposeInlays(editor, InlayDisposeContext .Typing )
117
- requestCompletions(editor, changeOffset) { completion ->
118
- if (completion.isEmpty()) return @requestCompletions
118
+ requestCompletions(editor, changeOffset);
119
+ }
120
+
121
+ @RequiresBackgroundThread
122
+ private fun requestCompletions (editor : Editor , changeOffset : Int ) {
123
+ val element = PsiUtilBase .getElementAtCaret(editor) ? : return
124
+ val request = CodeCompletionRequest .create(editor, changeOffset, element, null , null ) ? : return
125
+
126
+ KEY_LAST_REQUEST [editor] = request
127
+ CodeCompletionTask (request).execute { completion ->
128
+ if (completion.isEmpty()) return @execute
119
129
120
- currentCompletion = completion
130
+ val completeCode = Code .parse(completion).text
131
+ currentCompletion = PostCodeProcessor (request.prefixText, request.suffixText, completeCode).execute()
121
132
122
133
WriteCommandAction .runWriteCommandAction(editor.project) {
123
- val renderer = LLMInlayRenderer (editor, completion .lines())
134
+ val renderer = LLMInlayRenderer (editor, currentCompletion .lines())
124
135
renderer.apply {
125
136
val inlay: Inlay <EditorCustomElementRenderer >? = editor.inlayModel
126
137
.addBlockElement(changeOffset, true , false , 0 , this )
@@ -132,15 +143,6 @@ class LLMInlayManagerImpl : LLMInlayManager {
132
143
}
133
144
}
134
145
135
- @RequiresBackgroundThread
136
- private fun requestCompletions (editor : Editor , changeOffset : Int , onFirstCompletion : Consumer <String >? ) {
137
- val element = PsiUtilBase .getElementAtCaret(editor) ? : return
138
- val request = CodeCompletionRequest .create(editor, changeOffset, element, null , null ) ? : return
139
-
140
- KEY_LAST_REQUEST [editor] = request
141
- CodeCompletionTask (request).execute(onFirstCompletion)
142
- }
143
-
144
146
override fun editorModified (editor : Editor ) {
145
147
editorModified(editor, editor.caretModel.offset)
146
148
}
0 commit comments