From 2b9eaea9d40fbdaceff2fa147de38edbb46a0981 Mon Sep 17 00:00:00 2001 From: Carl-Robert Linnupuu Date: Fri, 12 Jul 2024 11:52:43 +0300 Subject: [PATCH] fix: replace code selection behaviour (fixes #614) --- .../toolwindow/ReplaceCodeInMainEditorAction.java | 9 ++++++--- .../chat/editor/actions/ReplaceSelectionAction.java | 5 ++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/java/ee/carlrobert/codegpt/actions/toolwindow/ReplaceCodeInMainEditorAction.java b/src/main/java/ee/carlrobert/codegpt/actions/toolwindow/ReplaceCodeInMainEditorAction.java index dcfe8e05..6c6b7402 100644 --- a/src/main/java/ee/carlrobert/codegpt/actions/toolwindow/ReplaceCodeInMainEditorAction.java +++ b/src/main/java/ee/carlrobert/codegpt/actions/toolwindow/ReplaceCodeInMainEditorAction.java @@ -30,9 +30,12 @@ public class ReplaceCodeInMainEditorAction extends AnAction { var project = event.getProject(); var toolWindowEditor = event.getData(PlatformDataKeys.EDITOR); if (project != null && toolWindowEditor != null) { - EditorUtil.replaceEditorSelection( - toolWindowEditor, - requireNonNull(toolWindowEditor.getSelectionModel().getSelectedText())); + var mainEditor = EditorUtil.getSelectedEditor(project); + if (mainEditor != null) { + EditorUtil.replaceEditorSelection( + mainEditor, + requireNonNull(toolWindowEditor.getSelectionModel().getSelectedText())); + } } } diff --git a/src/main/java/ee/carlrobert/codegpt/toolwindow/chat/editor/actions/ReplaceSelectionAction.java b/src/main/java/ee/carlrobert/codegpt/toolwindow/chat/editor/actions/ReplaceSelectionAction.java index 64dcc594..6bfd0b8c 100644 --- a/src/main/java/ee/carlrobert/codegpt/toolwindow/chat/editor/actions/ReplaceSelectionAction.java +++ b/src/main/java/ee/carlrobert/codegpt/toolwindow/chat/editor/actions/ReplaceSelectionAction.java @@ -27,7 +27,10 @@ public class ReplaceSelectionAction extends TrackableAction { public void handleAction(@NotNull AnActionEvent event) { var project = requireNonNull(event.getProject()); if (EditorUtil.isMainEditorTextSelected(project)) { - EditorUtil.replaceEditorSelection(editor, editor.getDocument().getText()); + var mainEditor = EditorUtil.getSelectedEditor(project); + if (mainEditor != null) { + EditorUtil.replaceEditorSelection(mainEditor, editor.getDocument().getText()); + } } else { OverlayUtil.showSelectedEditorSelectionWarning(event); }