-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new 'Insert at Caret' toolwindow editor action
- Loading branch information
1 parent
dc02fd3
commit c417cca
Showing
12 changed files
with
109 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/main/java/ee/carlrobert/codegpt/toolwindow/chat/editor/actions/InsertAtCaretAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package ee.carlrobert.codegpt.toolwindow.chat.editor.actions; | ||
|
||
import static com.intellij.openapi.application.ActionsKt.runUndoTransparentWriteAction; | ||
|
||
import com.intellij.openapi.actionSystem.AnActionEvent; | ||
import com.intellij.openapi.editor.Editor; | ||
import com.intellij.openapi.fileEditor.FileEditorManager; | ||
import ee.carlrobert.codegpt.CodeGPTBundle; | ||
import ee.carlrobert.codegpt.Icons; | ||
import ee.carlrobert.codegpt.actions.ActionType; | ||
import ee.carlrobert.codegpt.actions.TrackableAction; | ||
import ee.carlrobert.codegpt.ui.OverlayUtil; | ||
import java.awt.Point; | ||
import java.util.Optional; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public class InsertAtCaretAction extends TrackableAction { | ||
|
||
public InsertAtCaretAction(@NotNull Editor editor) { | ||
super( | ||
editor, | ||
CodeGPTBundle.get("toolwindow.chat.editor.action.insertAtCaret.title"), | ||
CodeGPTBundle.get("toolwindow.chat.editor.action.insertAtCaret.description"), | ||
Icons.SendToTheLeft, | ||
ActionType.INSERT_AT_CARET); | ||
} | ||
|
||
@Override | ||
public void handleAction(@NotNull AnActionEvent event) { | ||
Point locationOnScreen = getLocationOnScreen(event); | ||
Editor mainEditor = getSelectedTextEditor(); | ||
|
||
if (mainEditor == null) { | ||
OverlayUtil.showWarningBalloon("Active editor not found", locationOnScreen); | ||
return; | ||
} | ||
|
||
insertTextAtCaret(mainEditor, locationOnScreen); | ||
} | ||
|
||
@Nullable | ||
private Point getLocationOnScreen(AnActionEvent event) { | ||
return Optional.ofNullable(event.getInputEvent()) | ||
.map(inputEvent -> inputEvent.getComponent().getLocationOnScreen()) | ||
.orElse(null); | ||
} | ||
|
||
@Nullable | ||
private Editor getSelectedTextEditor() { | ||
return Optional.ofNullable(editor.getProject()) | ||
.map(FileEditorManager::getInstance) | ||
.map(FileEditorManager::getSelectedTextEditor) | ||
.orElse(null); | ||
} | ||
|
||
private void insertTextAtCaret(Editor mainEditor, @Nullable Point locationOnScreen) { | ||
runUndoTransparentWriteAction(() -> { | ||
mainEditor.getDocument().insertString( | ||
mainEditor.getCaretModel().getOffset(), | ||
editor.getDocument().getText()); | ||
if (locationOnScreen != null) { | ||
OverlayUtil.showInfoBalloon( | ||
"Text successfully inserted at the current cursor position.", | ||
locationOnScreen); | ||
} | ||
return null; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters