feat: apply post-processing for code completions (#404)

This commit is contained in:
Carl-Robert 2024-03-11 23:13:10 +02:00 committed by GitHub
parent 12cf5198f8
commit 91dd7bdb43
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 363 additions and 19 deletions

View file

@ -7,10 +7,13 @@ import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.impl.EditorImpl;
import com.intellij.openapi.progress.impl.BackgroundableProcessIndicator;
import ee.carlrobert.codegpt.CodeGPTBundle;
import ee.carlrobert.codegpt.actions.OpenSettingsAction;
import ee.carlrobert.codegpt.treesitter.CodeCompletionParserFactory;
import ee.carlrobert.codegpt.ui.OverlayUtil;
import ee.carlrobert.codegpt.util.file.FileUtil;
import ee.carlrobert.llm.client.openai.completion.ErrorDetails;
import ee.carlrobert.llm.completion.CompletionEventListener;
import java.io.IOException;
@ -25,14 +28,17 @@ class CodeCompletionEventListener implements CompletionEventListener<String> {
private final Editor editor;
private final int caretOffset;
private final InfillRequestDetails requestDetails;
private final BackgroundableProcessIndicator progressIndicator;
public CodeCompletionEventListener(
Editor editor,
int caretOffset,
InfillRequestDetails requestDetails,
@Nullable BackgroundableProcessIndicator progressIndicator) {
this.editor = editor;
this.caretOffset = caretOffset;
this.requestDetails = requestDetails;
this.progressIndicator = progressIndicator;
}
@ -42,15 +48,32 @@ class CodeCompletionEventListener implements CompletionEventListener<String> {
progressIndicator.processFinish();
}
PREVIOUS_INLAY_TEXT.set(editor, messageBuilder.toString());
try {
var project = editor.getProject();
if (project != null) {
var fileExtension =
FileUtil.getFileExtension(((EditorImpl) editor).getVirtualFile().getName());
var processedOutput = CodeCompletionParserFactory
.getParserForFileExtension(fileExtension)
.parse(
requestDetails.getPrefix(),
requestDetails.getSuffix(),
messageBuilder.toString());
handleComplete(processedOutput);
}
} catch (IllegalArgumentException e) {
handleComplete(messageBuilder.toString());
}
}
private void handleComplete(String processedOutput) {
PREVIOUS_INLAY_TEXT.set(editor, processedOutput);
CodeGPTEditorManager.getInstance().disposeEditorInlays(editor);
SwingUtilities.invokeLater(() -> {
if (editor.getCaretModel().getOffset() == caretOffset) {
var inlayText = messageBuilder.toString();
if (!inlayText.isEmpty()) {
CodeCompletionService.getInstance(requireNonNull(editor.getProject()))
.addInlays(editor, caretOffset, inlayText);
}
if (editor.getCaretModel().getOffset() == caretOffset && !processedOutput.isEmpty()) {
CodeCompletionService
.getInstance(requireNonNull(editor.getProject()))
.addInlays(editor, caretOffset, processedOutput);
}
});
}

View file

@ -101,7 +101,7 @@ public final class CodeCompletionService implements Disposable {
Void.class,
(progressIndicator) -> CompletionRequestService.getInstance().getCodeCompletionAsync(
request,
new CodeCompletionEventListener(editor, offset, progressIndicator)),
new CodeCompletionEventListener(editor, offset, request, progressIndicator)),
750,
TimeUnit.MILLISECONDS);
}
@ -156,7 +156,7 @@ public final class CodeCompletionService implements Disposable {
if (inlay != null) {
applyCompletion(editor, text, inlay.getOffset());
CodeGPTEditorManager.getInstance().disposeEditorInlays(editor);
return;
break;
}
}
editor.putUserData(CodeGPTKeys.PREVIOUS_INLAY_TEXT, null);