feat: force sync buffer

This commit is contained in:
zaaarf 2023-11-29 01:30:04 +01:00
parent 878df9716f
commit 154e8c26b5
No known key found for this signature in database
GPG key ID: 6445A5CD15E5B40C
4 changed files with 52 additions and 2 deletions

View file

@ -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);
}
}
}

View file

@ -22,7 +22,8 @@ public class BufferEventListener implements DocumentListener {
event.getOldFragment(), event.getNewFragment(), event.getOffset()); event.getOldFragment(), event.getNewFragment(), event.getOffset());
Object group = CommandProcessor.getInstance().getCurrentCommandGroupId(); Object group = CommandProcessor.getInstance().getCurrentCommandGroupId();
if(group instanceof String groupString && groupString.startsWith("codemp-buffer-receive")) if(group instanceof String groupString)
if(groupString.startsWith("codemp-buffer-receive") || groupString.startsWith("codemp-buffer-sync"))
return; return;
//TODO move actions break //TODO move actions break

View file

@ -1,5 +1,6 @@
package com.codemp.intellij.util; package com.codemp.intellij.util;
import com.codemp.intellij.CodeMP;
import com.codemp.intellij.exceptions.ide.BadActionEventStateException; import com.codemp.intellij.exceptions.ide.BadActionEventStateException;
import com.intellij.notification.Notification; import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType; import com.intellij.notification.NotificationType;
@ -43,5 +44,6 @@ public class ActionUtil {
String.format("%s: %s", t.getClass().getCanonicalName(), t.getMessage()), String.format("%s: %s", t.getClass().getCanonicalName(), t.getMessage()),
NotificationType.ERROR NotificationType.ERROR
), event.getProject()); ), event.getProject());
CodeMP.LOGGER.error(title, t);
} }
} }

View file

@ -27,6 +27,8 @@
text="Create Buffer with Content"/> text="Create Buffer with Content"/>
<action id="codemp.buffer.attach" class="com.codemp.intellij.actions.buffer.BufferAttachAction" <action id="codemp.buffer.attach" class="com.codemp.intellij.actions.buffer.BufferAttachAction"
text="Attach to Buffer"/> text="Attach to Buffer"/>
<action id="codemp.buffer.sync" class="com.codemp.intellij.actions.buffer.BufferSyncAction"
text="Force Sync Buffer"/>
<action id="codemp.buffer.detach" class="com.codemp.intellij.actions.buffer.BufferDetachAction" <action id="codemp.buffer.detach" class="com.codemp.intellij.actions.buffer.BufferDetachAction"
text="Detach from Buffer"/> text="Detach from Buffer"/>
</group> </group>