From 70aeca3d17a18beb8b2c8a29ae803dda57d81f9a Mon Sep 17 00:00:00 2001 From: zaaarf Date: Thu, 10 Oct 2024 00:35:26 +0200 Subject: [PATCH] fix: assorted UX improvements --- .../actions/buffer/BufferShareAction.java | 85 ------------------- .../actions/buffer/BufferSyncAction.java | 6 +- .../workspace/WorkspaceLeaveAction.java | 11 +-- .../mp/code/intellij/ui/CodeMPToolPanel.java | 68 ++++++++++++--- src/main/resources/META-INF/plugin.xml | 2 - 5 files changed, 65 insertions(+), 107 deletions(-) delete mode 100644 src/main/java/mp/code/intellij/actions/buffer/BufferShareAction.java diff --git a/src/main/java/mp/code/intellij/actions/buffer/BufferShareAction.java b/src/main/java/mp/code/intellij/actions/buffer/BufferShareAction.java deleted file mode 100644 index c7d3d13..0000000 --- a/src/main/java/mp/code/intellij/actions/buffer/BufferShareAction.java +++ /dev/null @@ -1,85 +0,0 @@ -package mp.code.intellij.actions.buffer; - -import com.intellij.openapi.actionSystem.ActionUpdateThread; -import com.intellij.openapi.actionSystem.AnAction; -import com.intellij.openapi.actionSystem.AnActionEvent; -import com.intellij.openapi.application.ApplicationManager; -import com.intellij.openapi.fileEditor.FileEditor; -import com.intellij.openapi.fileEditor.FileEditorManager; -import com.intellij.openapi.project.Project; -import com.intellij.openapi.ui.Messages; -import mp.code.BufferController; -import mp.code.data.TextChange; -import mp.code.exceptions.ControllerException; -import mp.code.intellij.CodeMP; -import mp.code.intellij.util.FileUtil; -import mp.code.intellij.util.InteractionUtil; -import mp.code.intellij.util.cb.BufferCallback; -import org.jetbrains.annotations.NotNull; - -import java.io.IOException; -import java.util.Optional; -import java.util.OptionalLong; - -public class BufferShareAction extends AnAction { - @Override - public void actionPerformed(@NotNull AnActionEvent e) { - Project proj = e.getProject(); - FileEditor currentEditor = FileEditorManager.getInstance(proj).getSelectedEditor(); - if(currentEditor == null) { - Messages.showErrorDialog( - "No file is currently open!", - "CodeMP Buffer Share" - ); - return; - } - - String path = FileUtil.getRelativePath(proj, currentEditor.getFile()); - if(path == null) { - Messages.showErrorDialog( - "File must belong to project!", - "CodeMP Buffer Share" - ); - return; - } - - InteractionUtil.createBuffer(proj, path); - Optional controller = InteractionUtil.bufferAttach(proj, CodeMP.getActiveWorkspace(), path); - if(controller.isEmpty()) { - Messages.showErrorDialog( - "An unknown error has occurred!", - "CodeMP Buffer Share" - ); - return; - } - - try { - controller.get().send(new TextChange( - 0, - 0, - new String(currentEditor.getFile().contentsToByteArray()), - OptionalLong.empty() - )); - ApplicationManager.getApplication().runWriteAction(() -> { - try { - FileUtil.getAndRegisterBufferEquivalent(this, proj, controller.get()); - } catch(ControllerException | IOException ex) { - throw new RuntimeException(ex); - } catch(UnsupportedOperationException ignored) {} - }); - controller.get().callback(buf -> new BufferCallback(proj).accept(buf)); - } catch(ControllerException | IOException ex) { - throw new RuntimeException(ex); - } - } - - @Override - public void update(@NotNull AnActionEvent e) { - e.getPresentation().setEnabled(CodeMP.isInWorkspace()); - } - - @Override - public @NotNull ActionUpdateThread getActionUpdateThread() { - return ActionUpdateThread.EDT; - } -} diff --git a/src/main/java/mp/code/intellij/actions/buffer/BufferSyncAction.java b/src/main/java/mp/code/intellij/actions/buffer/BufferSyncAction.java index 77e699d..61f3508 100644 --- a/src/main/java/mp/code/intellij/actions/buffer/BufferSyncAction.java +++ b/src/main/java/mp/code/intellij/actions/buffer/BufferSyncAction.java @@ -21,15 +21,15 @@ public class BufferSyncAction extends AnAction { public void actionPerformed(@NotNull AnActionEvent e) { // TODO if current buffer is managed, sync that instead of making user choose - String[] active_buffers = CodeMP.getActiveWorkspace().activeBuffers(); + String[] activeBuffers = CodeMP.getActiveWorkspace().activeBuffers(); int choice = Messages.showChooseDialog( "Sync which buffer?", "CodeMP Buffer Detach", - active_buffers, + activeBuffers, "", Messages.getQuestionIcon() ); - String path = active_buffers[choice]; + String path = activeBuffers[choice]; Optional controller = CodeMP.getActiveWorkspace().getBuffer(path); if (controller.isEmpty()) { diff --git a/src/main/java/mp/code/intellij/actions/workspace/WorkspaceLeaveAction.java b/src/main/java/mp/code/intellij/actions/workspace/WorkspaceLeaveAction.java index f41ee11..c99ed09 100644 --- a/src/main/java/mp/code/intellij/actions/workspace/WorkspaceLeaveAction.java +++ b/src/main/java/mp/code/intellij/actions/workspace/WorkspaceLeaveAction.java @@ -3,20 +3,17 @@ package mp.code.intellij.actions.workspace; import com.intellij.openapi.actionSystem.ActionUpdateThread; import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; -import com.intellij.openapi.ui.Messages; import mp.code.intellij.CodeMP; import mp.code.intellij.util.InteractionUtil; import org.jetbrains.annotations.NotNull; +import java.util.Objects; + public class WorkspaceLeaveAction extends AnAction { @Override public void actionPerformed(@NotNull AnActionEvent e) { - String workspaceId = Messages.showInputDialog( - "Workspace to leave:", - "CodeMP Workspace Leave", - Messages.getQuestionIcon()); - - InteractionUtil.leaveWorkspace(e.getProject(), workspaceId, null); + String workspaceId = CodeMP.getActiveWorkspace().getWorkspaceId(); + InteractionUtil.leaveWorkspace(Objects.requireNonNull(e.getProject()), workspaceId, null); } @Override diff --git a/src/main/java/mp/code/intellij/ui/CodeMPToolPanel.java b/src/main/java/mp/code/intellij/ui/CodeMPToolPanel.java index bd7fa04..313db7c 100644 --- a/src/main/java/mp/code/intellij/ui/CodeMPToolPanel.java +++ b/src/main/java/mp/code/intellij/ui/CodeMPToolPanel.java @@ -1,11 +1,17 @@ package mp.code.intellij.ui; import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.editor.Editor; +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.ui.Messages; import com.intellij.ui.components.JBList; import com.intellij.ui.treeStructure.Tree; +import mp.code.BufferController; import mp.code.Workspace; +import mp.code.data.TextChange; import mp.code.exceptions.ControllerException; import mp.code.intellij.CodeMP; import mp.code.intellij.util.FileUtil; @@ -19,7 +25,10 @@ import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.MouseEvent; import java.io.IOException; +import java.util.Arrays; +import java.util.Objects; import java.util.Optional; +import java.util.OptionalLong; public class CodeMPToolPanel extends JPanel { public CodeMPToolPanel(Project project) { @@ -64,21 +73,60 @@ public class CodeMPToolPanel extends JPanel { } case JOINED -> { this.setLayout(new BorderLayout(1, 0)); - JButton createButton = new JButton(new AbstractAction("Create buffer") { + this.add(new JButton(new AbstractAction("Share buffer") { @Override public void actionPerformed(ActionEvent e) { - String bufferPath = Messages.showInputDialog( - "Name of buffer:", - "CodeMP Buffer Create", - Messages.getQuestionIcon() - ); + Project proj = Objects.requireNonNull(project); + FileEditor currentEditor = FileEditorManager.getInstance(proj).getSelectedEditor(); + if(currentEditor == null) { + Messages.showErrorDialog( + "No file is currently open!", + "CodeMP Buffer Share" + ); + return; + } - InteractionUtil.createBuffer(project, bufferPath); + String path = FileUtil.getRelativePath(proj, currentEditor.getFile()); + if(path == null) { + Messages.showErrorDialog( + "File must belong to project!", + "CodeMP Buffer Share" + ); + return; + } + + InteractionUtil.createBuffer(proj, path); CodeMPToolPanel.this.redraw(project); + Optional controller = InteractionUtil.bufferAttach(proj, CodeMP.getActiveWorkspace(), path); + if(controller.isEmpty()) { + Messages.showErrorDialog( + "An unknown error has occurred!", + "CodeMP Buffer Share" + ); + return; + } + + try { + Editor ed = ((TextEditor) currentEditor).getEditor(); + controller.get().send(new TextChange( + 0, + 0, + ed.getDocument().getText(), + OptionalLong.empty() + )); + ApplicationManager.getApplication().runWriteAction(() -> { + try { + FileUtil.getAndRegisterBufferEquivalent(this, proj, controller.get()); + } catch(ControllerException | IOException ex) { + throw new RuntimeException(ex); + } catch(UnsupportedOperationException ignored) {} + }); + controller.get().callback(buf -> new BufferCallback(proj).accept(buf)); + } catch(ControllerException ex) { + throw new RuntimeException(ex); + } } - }); - // createButton.setSize(createButton.getPreferredSize()); - this.add(createButton, BorderLayout.NORTH); + }), BorderLayout.NORTH); Workspace ws = CodeMP.getActiveWorkspace(); JTree tree = drawTree(ws.getWorkspaceId(), ws.getFileTree(Optional.empty(), false)); diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 1e85226..b0ad25f 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -25,8 +25,6 @@ text="Leave Workspace"/> -