ProxyAI/src/main/java/ee/carlrobert/codegpt/codecompletions/CodeGPTEditorManager.java
Phil 7387cf4536
Inline Autocompletion Pt.2 (#333)
* Add first draft of inline code completion with mock text

* Adds InsertInlineTextAction for inserting autocomplete suggestion with tab

- Changed to disable suggestions when text is selected
- Adds and removes the insert action based on when it shows the inlay hint

* Request inline code completion

* Move inline completion prompt into txt file

* Add inline completion settings to ConfigurationState

* Fix code style

* Use EditorTrackerListener instead of EditorFactoryListener to enable inline completion

* Code completion requests synchronously without SSE

* Use LlamaClient.getInfill() for inline code completion

* support inlay block element rendering, clean up code

* Use only enclosed Method or Class contents for code completion if possible

* Refactor extracting PsiElement contents in code completion

* bump llm-client

* fix completion call from triggering on EDT, force method params to be nonnull by default

* refactor request building, decrease delay value

* Trigger code completion if cursor is not inside a word

* Improve inlay rendering

* Support cancellable infill requests

* add statusbar widget, disable completions by default

* Show error notification if code completion failed

* Truely disable/enable EditorInlayHandler when completion is turned off/on

* Add CodeCompletionEnabledListener Topic to control enabling/disabling code-completion

* Add progress indicator for code-completion with option to cancel

* Add CodeCompletionServiceTest + refactor inlay ElementRenderers

* several improvements

- replace timer implementation with call debouncing
- use OpenAI /v1/completions API for completions
- code refactoring

* trigger progress indicator only for llama completions

* fix tests

---------

Co-authored-by: James Higgins <james.isaac.higgins@gmail.com>
Co-authored-by: Carl-Robert Linnupuu <carlrobertoh@gmail.com>
2024-01-31 01:05:31 +02:00

51 lines
1.9 KiB
Java

package ee.carlrobert.codegpt.codecompletions;
import static ee.carlrobert.codegpt.CodeGPTKeys.MULTI_LINE_INLAY;
import static ee.carlrobert.codegpt.CodeGPTKeys.SINGLE_LINE_INLAY;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.components.Service;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorCustomElementRenderer;
import com.intellij.openapi.editor.Inlay;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.TextEditor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
@Service
public final class CodeGPTEditorManager {
private CodeGPTEditorManager() {
}
public static CodeGPTEditorManager getInstance() {
return ApplicationManager.getApplication().getService(CodeGPTEditorManager.class);
}
public void disposeAllInlays(Project project) {
var allFileEditors = FileEditorManager.getInstance(project).getAllEditors();
for (FileEditor fileEditor : allFileEditors) {
if (fileEditor instanceof TextEditor) {
disposeEditorInlays(((TextEditor) fileEditor).getEditor());
}
}
}
public void disposeEditorInlays(Editor editor) {
ActionManager.getInstance().unregisterAction(CodeCompletionService.APPLY_INLAY_ACTION_ID);
disposeInlay(editor, SINGLE_LINE_INLAY);
disposeInlay(editor, MULTI_LINE_INLAY);
}
private void disposeInlay(Editor editor, Key<Inlay<EditorCustomElementRenderer>> inlayKey) {
Inlay<EditorCustomElementRenderer> inlay = editor.getUserData(inlayKey);
if (inlay != null) {
WriteCommandAction.runWriteCommandAction(editor.getProject(), inlay::dispose);
editor.putUserData(inlayKey, null);
}
}
}