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,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();

View file

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

View file

@ -27,6 +27,8 @@
text="Create Buffer with Content"/>
<action id="codemp.buffer.attach" class="com.codemp.intellij.actions.buffer.BufferAttachAction"
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"
text="Detach from Buffer"/>
</group>