Skip to content

Commit fbf88bc

Browse files
committed
fix: undo action after completion
1 parent 0d8a182 commit fbf88bc

File tree

5 files changed

+48
-46
lines changed

5 files changed

+48
-46
lines changed

src/main/java/ee/carlrobert/codegpt/completions/CompletionRequestProvider.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,20 @@
7171

7272
public class CompletionRequestProvider {
7373

74-
public static final String COMPLETION_SYSTEM_PROMPT = getResourceContent(
75-
"/prompts/default-completion-system-prompt.txt");
74+
public static final String COMPLETION_SYSTEM_PROMPT =
75+
getResourceContent("/prompts/default-completion.txt");
7676

77-
public static final String GENERATE_COMMIT_MESSAGE_SYSTEM_PROMPT = getResourceContent(
78-
"/prompts/generate-commit-message-system-prompt.txt");
77+
public static final String GENERATE_COMMIT_MESSAGE_SYSTEM_PROMPT =
78+
getResourceContent("/prompts/generate-commit-message.txt");
7979

80-
public static final String FIX_COMPILE_ERRORS_SYSTEM_PROMPT = getResourceContent(
81-
"/prompts/fix-compile-errors.txt");
80+
public static final String FIX_COMPILE_ERRORS_SYSTEM_PROMPT =
81+
getResourceContent("/prompts/fix-compile-errors.txt");
82+
83+
public static final String GENERATE_METHOD_NAMES_SYSTEM_PROMPT =
84+
getResourceContent("/prompts/method-name-generator.txt");
85+
86+
public static final String EDIT_CODE_SYSTEM_PROMPT =
87+
getResourceContent("/prompts/edit-code.txt");
8288

8389
private final EncodingManager encodingManager = EncodingManager.getInstance();
8490
private final Conversation conversation;
@@ -113,9 +119,7 @@ public static OpenAIChatCompletionRequest buildOpenAILookupCompletionRequest(
113119
String model) {
114120
return new OpenAIChatCompletionRequest.Builder(
115121
List.of(
116-
new OpenAIChatCompletionStandardMessage(
117-
"system",
118-
getResourceContent("/prompts/method-name-generator.txt")),
122+
new OpenAIChatCompletionStandardMessage("system", GENERATE_METHOD_NAMES_SYSTEM_PROMPT),
119123
new OpenAIChatCompletionStandardMessage("user", context)))
120124
.setModel(model)
121125
.setStream(false)
@@ -127,9 +131,7 @@ public static OpenAIChatCompletionRequest buildEditCodeRequest(
127131
String model) {
128132
return new OpenAIChatCompletionRequest.Builder(
129133
List.of(
130-
new OpenAIChatCompletionStandardMessage(
131-
"system",
132-
getResourceContent("/prompts/edit-code.txt")),
134+
new OpenAIChatCompletionStandardMessage("system", EDIT_CODE_SYSTEM_PROMPT),
133135
new OpenAIChatCompletionStandardMessage("user", context)))
134136
.setModel(model)
135137
.setStream(true)
@@ -169,14 +171,14 @@ public static Request buildCustomOpenAILookupCompletionRequest(String context) {
169171
List.of(
170172
new OpenAIChatCompletionStandardMessage(
171173
"system",
172-
getResourceContent("/prompts/method-name-generator.txt")),
174+
GENERATE_COMMIT_MESSAGE_SYSTEM_PROMPT),
173175
new OpenAIChatCompletionStandardMessage("user", context)),
174176
false);
175177
}
176178

177179
public static LlamaCompletionRequest buildLlamaLookupCompletionRequest(String context) {
178-
return new LlamaCompletionRequest.Builder(PromptTemplate.LLAMA
179-
.buildPrompt(getResourceContent("/prompts/method-name-generator.txt"), context, List.of()))
180+
return new LlamaCompletionRequest.Builder(
181+
PromptTemplate.LLAMA.buildPrompt(GENERATE_COMMIT_MESSAGE_SYSTEM_PROMPT, context, List.of()))
180182
.setStream(false)
181183
.build();
182184
}
Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package ee.carlrobert.codegpt.actions.editor
22

33
import com.intellij.notification.NotificationType
4+
import com.intellij.openapi.application.runInEdt
5+
import com.intellij.openapi.application.runUndoTransparentWriteAction
46
import com.intellij.openapi.command.WriteCommandAction.runWriteCommandAction
57
import com.intellij.openapi.components.service
68
import com.intellij.openapi.editor.Editor
@@ -15,7 +17,7 @@ import ee.carlrobert.llm.client.openai.completion.ErrorDetails
1517
import ee.carlrobert.llm.completion.CompletionEventListener
1618
import okhttp3.sse.EventSource
1719

18-
class EditCodeCompletionHandler(
20+
class EditCodeCompletionListener(
1921
private val editor: Editor,
2022
private val observableProperties: ObservableProperties,
2123
private val selectionTextRange: TextRange
@@ -25,11 +27,11 @@ class EditCodeCompletionHandler(
2527
private var currentHighlighter: RangeHighlighter? = null
2628

2729
override fun onMessage(message: String, eventSource: EventSource) {
28-
handleDiff(message)
30+
runInEdt { handleDiff(message) }
2931
}
3032

3133
override fun onComplete(messageBuilder: StringBuilder) {
32-
cleanupAndFormat()
34+
runInEdt { cleanupAndFormat() }
3335
observableProperties.loading.set(false)
3436
}
3537

@@ -40,8 +42,8 @@ class EditCodeCompletionHandler(
4042
)
4143
}
4244

43-
private fun highlightCurrentRow(editor: Editor) {
44-
currentHighlighter?.let { editor.markupModel.removeHighlighter(it) }
45+
private fun updateHighlighter(editor: Editor) {
46+
cleanupHighlighter()
4547

4648
val document = editor.document
4749
val lineNumber = document.getLineNumber(editor.caretModel.offset)
@@ -59,13 +61,12 @@ class EditCodeCompletionHandler(
5961
)
6062
}
6163

62-
6364
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
6868

69+
runUndoTransparentWriteAction {
6970
val remainingOriginalLength = endOffset - (startOffset + replacedLength)
7071
if (remainingOriginalLength > 0) {
7172
document.replaceString(
@@ -79,41 +80,40 @@ class EditCodeCompletionHandler(
7980
} else {
8081
document.insertString(startOffset + replacedLength, message)
8182
}
82-
83-
replacedLength += message.length
84-
editor.caretModel.moveToOffset(startOffset + replacedLength)
85-
highlightCurrentRow(editor)
8683
}
87-
}
8884

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)
9288
}
9389

9490
private fun cleanupAndFormat() {
9591
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
10498

99+
runWriteCommandAction(project) {
105100
if (newEndOffset < endOffset) {
106101
document.deleteString(newEndOffset, endOffset)
107102
}
108-
109103
psiDocumentManager.commitDocument(document)
110104
project.service<CodeStyleManager>().reformatText(
111105
psiFile,
112106
listOf(TextRange(startOffset, newEndOffset))
113107
)
114-
editor.caretModel.moveToOffset(newEndOffset)
115-
psiDocumentManager.doPostponedOperationsAndUnblockDocument(document)
116-
cleanupHighlighter()
117108
}
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
118118
}
119119
}

src/main/kotlin/ee/carlrobert/codegpt/actions/editor/EditCodeSubmissionHandler.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class EditCodeSubmissionHandler(
4141
"$userPrompt\n\n$selectedText",
4242
service<CodeGPTServiceSettings>().state.chatCompletionSettings.model
4343
),
44-
EditCodeCompletionHandler(editor, observableProperties, selectionTextRange)
44+
EditCodeCompletionListener(editor, observableProperties, selectionTextRange)
4545
)
4646
} finally {
4747
observableProperties.loading.set(false)
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)