From 154e8c26b5ab1cb2d6e539a29443579cc755b0b2 Mon Sep 17 00:00:00 2001 From: zaaarf Date: Wed, 29 Nov 2023 01:30:04 +0100 Subject: [PATCH] feat: force sync buffer --- .../actions/buffer/BufferSyncAction.java | 45 +++++++++++++++++++ .../listeners/BufferEventListener.java | 5 ++- .../com/codemp/intellij/util/ActionUtil.java | 2 + src/main/resources/META-INF/plugin.xml | 2 + 4 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/codemp/intellij/actions/buffer/BufferSyncAction.java diff --git a/src/main/java/com/codemp/intellij/actions/buffer/BufferSyncAction.java b/src/main/java/com/codemp/intellij/actions/buffer/BufferSyncAction.java new file mode 100644 index 0000000..c4c3cbb --- /dev/null +++ b/src/main/java/com/codemp/intellij/actions/buffer/BufferSyncAction.java @@ -0,0 +1,45 @@ +package com.codemp.intellij.actions.buffer; + +import com.codemp.intellij.CodeMP; +import com.codemp.intellij.jni.CodeMPHandler; +import com.codemp.intellij.task.TaskManager; +import com.codemp.intellij.util.ActionUtil; +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.command.CommandProcessor; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.ui.Messages; +import org.jetbrains.annotations.NotNull; + +public class BufferSyncAction extends AnAction { + public static void sync(AnActionEvent e, String bufferName, boolean silent) { + Editor editor = ActionUtil.getCurrentEditor(e); + ApplicationManager.getApplication().runWriteAction(() -> + CommandProcessor.getInstance().executeCommand( + editor.getProject(), + () -> editor.getDocument().setText(CodeMPHandler.getBuffer(bufferName).getContent()), + "CodeMPBufferSync", + "codemp-buffer-sync", + editor.getDocument() + )); + + if(!silent) ActionUtil.notify(e, + String.format("Synced buffer %s", bufferName), + "The buffer was synced successfully."); + CodeMP.LOGGER.debug("The buffer {} was synced successfully.", bufferName); + } + + @Override + public void actionPerformed(@NotNull AnActionEvent e) { + Editor editor = ActionUtil.getCurrentEditor(e); + String bufferName = CodeMP.ACTIVE_BUFFERS_REVERSE.get(editor); + try { + sync(e, bufferName, false); + } catch(Exception ex) { + ActionUtil.notifyError(e, String.format( + "Failed to attach to buffer %s!", + bufferName), ex); + } + } +} diff --git a/src/main/java/com/codemp/intellij/listeners/BufferEventListener.java b/src/main/java/com/codemp/intellij/listeners/BufferEventListener.java index 1f35d94..3a95da5 100644 --- a/src/main/java/com/codemp/intellij/listeners/BufferEventListener.java +++ b/src/main/java/com/codemp/intellij/listeners/BufferEventListener.java @@ -22,8 +22,9 @@ public class BufferEventListener implements DocumentListener { event.getOldFragment(), event.getNewFragment(), event.getOffset()); Object group = CommandProcessor.getInstance().getCurrentCommandGroupId(); - if(group instanceof String groupString && groupString.startsWith("codemp-buffer-receive")) - return; + if(group instanceof String groupString) + if(groupString.startsWith("codemp-buffer-receive") || groupString.startsWith("codemp-buffer-sync")) + return; //TODO move actions break int changeOffset = event.getOffset(); diff --git a/src/main/java/com/codemp/intellij/util/ActionUtil.java b/src/main/java/com/codemp/intellij/util/ActionUtil.java index 08ee5f3..70db9e7 100644 --- a/src/main/java/com/codemp/intellij/util/ActionUtil.java +++ b/src/main/java/com/codemp/intellij/util/ActionUtil.java @@ -1,5 +1,6 @@ package com.codemp.intellij.util; +import com.codemp.intellij.CodeMP; import com.codemp.intellij.exceptions.ide.BadActionEventStateException; import com.intellij.notification.Notification; import com.intellij.notification.NotificationType; @@ -43,5 +44,6 @@ public class ActionUtil { String.format("%s: %s", t.getClass().getCanonicalName(), t.getMessage()), NotificationType.ERROR ), event.getProject()); + CodeMP.LOGGER.error(title, t); } } diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index be16e4e..8df0177 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -27,6 +27,8 @@ text="Create Buffer with Content"/> +