feat: updated actions

This commit is contained in:
zaaarf 2024-01-31 23:56:41 +01:00
parent 7c5c851f6f
commit 5a42577a1f
No known key found for this signature in database
GPG key ID: 102E445F4C3F829B
17 changed files with 106 additions and 328 deletions

View file

@ -10,7 +10,6 @@ import org.jetbrains.annotations.NotNull;
public class ConnectAction extends AnAction { public class ConnectAction extends AnAction {
public static void connect(AnActionEvent e, String url, boolean silent) { public static void connect(AnActionEvent e, String url, boolean silent) {
CodeMP.connect(url); CodeMP.connect(url);
if(!silent) ActionUtil.notify(e, if(!silent) ActionUtil.notify(e,
"Success", String.format("Connected to %s!", url)); "Success", String.format("Connected to %s!", url));
CodeMP.LOGGER.debug("Connected to {}!", url); CodeMP.LOGGER.debug("Connected to {}!", url);

View file

@ -9,9 +9,7 @@ import org.jetbrains.annotations.NotNull;
public class DisconnectAction extends AnAction { public class DisconnectAction extends AnAction {
public static void disconnect(AnActionEvent e, boolean silent) { public static void disconnect(AnActionEvent e, boolean silent) {
String url = CodeMP.getClient("disconnect").getUrl(); String url = CodeMP.getClient("disconnect").getUrl();
CodeMP.disconnect(); CodeMP.disconnect();
if(!silent) ActionUtil.notify(e, if(!silent) ActionUtil.notify(e,
"Success", String.format("Disconnected from %s!", url)); "Success", String.format("Disconnected from %s!", url));
CodeMP.LOGGER.debug("Connected to {}!", url); CodeMP.LOGGER.debug("Connected to {}!", url);

View file

@ -1,7 +1,6 @@
package com.codemp.intellij.actions; package com.codemp.intellij.actions;
import com.codemp.intellij.CodeMP; import com.codemp.intellij.CodeMP;
import com.codemp.intellij.actions.buffer.BufferAttachAction;
import com.codemp.intellij.actions.workspace.WorkspaceJoinAction; import com.codemp.intellij.actions.workspace.WorkspaceJoinAction;
import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.actionSystem.AnActionEvent;
@ -15,7 +14,6 @@ public class FastForwardAction extends AnAction {
public void actionPerformed(@NotNull AnActionEvent e) { public void actionPerformed(@NotNull AnActionEvent e) {
ConnectAction.connect(e, "http://alemi.dev:50052", true); ConnectAction.connect(e, "http://alemi.dev:50052", true);
WorkspaceJoinAction.join(e, "default", true); WorkspaceJoinAction.join(e, "default", true);
BufferAttachAction.attach(e, "fucl", true);
CodeMP.LOGGER.debug("Completed quick startup for testing!"); CodeMP.LOGGER.debug("Completed quick startup for testing!");
} }
} }

View file

@ -1,41 +0,0 @@
package com.codemp.intellij.actions.buffer;
import com.codemp.intellij.CodeMP;
import com.codemp.intellij.jni.BufferHandler;
import com.codemp.intellij.util.ActionUtil;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.ui.Messages;
import org.jetbrains.annotations.NotNull;
public class BufferAttachAction extends AnAction {
public static void attach(AnActionEvent e, String buffer, boolean silent) {
BufferHandler bufferHandler = CodeMPHandler.attach(buffer);
if(!silent) ActionUtil.notify(e, "Success",
String.format("Successfully attached to buffer to buffer to %s!", buffer));
CodeMP.LOGGER.debug("Attached to buffer to {}!", buffer);
//TODO "get" the Editor corresponding to buffer, for now use the current one
Editor editor = ActionUtil.getCurrentEditor(e);
WorkspaceManager
.getOrCreateBufferTask(ActionUtil.getCurrentProject(e))
.registerListener(bufferHandler, editor);
}
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
String buffer = Messages.showInputDialog(
"Buffer name:",
"Attach to CodeMP Buffer",
Messages.getQuestionIcon());
try {
attach(e, buffer, false);
} catch(Exception ex) {
ActionUtil.notifyError(e, String.format(
"Failed to attach to buffer %s!",
buffer), ex);
}
}
}

View file

@ -1,34 +0,0 @@
package com.codemp.intellij.actions.buffer;
import com.codemp.intellij.CodeMP;
import com.codemp.intellij.util.ActionUtil;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.ui.Messages;
import org.jetbrains.annotations.NotNull;
public class BufferCreateAction extends AnAction {
public static void create(AnActionEvent e, String buffer, boolean silent) {
CodeMPHandler.create(buffer);
if(!silent) ActionUtil.notify(e, "Success",
String.format("Created buffer %s!", buffer)
);
CodeMP.LOGGER.debug("Created buffer {}!", buffer);
}
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
String buffer = Messages.showInputDialog(
"Buffer name:",
"Create CodeMP Buffer",
Messages.getQuestionIcon());
try {
create(e, buffer, false);
} catch(Exception ex) {
ActionUtil.notifyError(e, String.format(
"Failed to create buffer with name %s!",
buffer), ex);
}
}
}

View file

@ -1,35 +0,0 @@
package com.codemp.intellij.actions.buffer;
import com.codemp.intellij.CodeMP;
import com.codemp.intellij.util.ActionUtil;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.ui.Messages;
import org.jetbrains.annotations.NotNull;
public class BufferCreateWithContentAction extends AnAction {
public static void createWithContent(AnActionEvent event, String buffer, boolean silent) {
String content = ActionUtil.getCurrentEditor(event).getDocument().getText();
CodeMPHandler.createWithContent(buffer, content);
if(!silent) ActionUtil.notify(event, "Success", String.format(
"Created buffer %s with content %s!", buffer, content));
CodeMP.LOGGER.debug("Created buffer {} with content {}!", buffer, content);
}
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
String buffer = Messages.showInputDialog(
"Buffer name:",
"Create CodeMP Buffer with Content",
Messages.getQuestionIcon());
try {
createWithContent(e, buffer, false);
} catch(Exception ex) {
ActionUtil.notifyError(e, String.format(
"Failed to create buffer with name %s!",
buffer), ex);
}
}
}

View file

@ -1,46 +0,0 @@
package com.codemp.intellij.actions.buffer;
import com.codemp.intellij.CodeMP;
import com.codemp.intellij.exceptions.ide.BufferDetachException;
import com.codemp.intellij.task.BufferEventAwaiterTask;
import com.codemp.intellij.util.ActionUtil;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.ui.Messages;
import org.jetbrains.annotations.NotNull;
public class BufferDetachAction extends AnAction {
public static void detach(AnActionEvent e, String buffer, boolean silent) {
boolean res = CodeMPHandler.detach(buffer);
if(!res) throw new BufferDetachException(buffer);
CodeMP.ACTIVE_BUFFERS.remove(buffer);
if(task != null) {
task.unregisterListener(buffer);
if(!silent) ActionUtil.notify(e, "Success",
String.format("Detached from buffer %s!", buffer)
);
CodeMP.LOGGER.debug("Detached from buffer {}!", buffer);
} else {
if(!silent) ActionUtil.notifyError(e, String.format("Failed to detach from %s", buffer),
"Buffer event task was dead!");
CodeMP.LOGGER.debug("Failed to detach from {}: buffer event task was dead!", buffer);
}
}
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
String buffer = Messages.showInputDialog(
"Buffer name:",
"Detach from CodeMP Buffer",
Messages.getQuestionIcon());
try {
detach(e, buffer, false);
} catch(Exception ex) {
ActionUtil.notifyError(e, String.format(
"Failed to detach from buffer with name %s!",
buffer), ex);
}
}
}

View file

@ -1,42 +0,0 @@
package com.codemp.intellij.actions.buffer;
import com.codemp.intellij.CodeMP;
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 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

@ -18,7 +18,6 @@ public class WorkspaceJoinAction extends AnAction {
if(!silent) ActionUtil.notify(e, if(!silent) ActionUtil.notify(e,
"Success", String.format("Joined workspace %s!", workspaceId)); "Success", String.format("Joined workspace %s!", workspaceId));
CodeMP.LOGGER.debug("Joined workspace {}!", workspaceId); CodeMP.LOGGER.debug("Joined workspace {}!", workspaceId);
} }
@Override @Override

View file

@ -2,7 +2,6 @@ package com.codemp.intellij.actions.workspace;
import com.codemp.intellij.CodeMP; import com.codemp.intellij.CodeMP;
import com.codemp.intellij.util.ActionUtil; import com.codemp.intellij.util.ActionUtil;
import com.codemp.intellij.workspace.Workspace;
import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.ui.Messages; import com.intellij.openapi.ui.Messages;

View file

@ -2,15 +2,12 @@ package com.codemp.intellij.listeners;
import com.codemp.intellij.CodeMP; import com.codemp.intellij.CodeMP;
import com.codemp.intellij.jni.CursorHandler; import com.codemp.intellij.jni.CursorHandler;
import com.codemp.intellij.task.CursorEventAwaiterTask;
import com.codemp.intellij.util.FileUtil; import com.codemp.intellij.util.FileUtil;
import com.codemp.intellij.workspace.Workspace;
import com.intellij.openapi.editor.Caret; import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.VisualPosition; import com.intellij.openapi.editor.VisualPosition;
import com.intellij.openapi.editor.event.CaretEvent; import com.intellij.openapi.editor.event.CaretEvent;
import com.intellij.openapi.editor.event.CaretListener; import com.intellij.openapi.editor.event.CaretListener;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
public class CursorEventListener implements CaretListener { public class CursorEventListener implements CaretListener {

View file

@ -3,14 +3,9 @@ package com.codemp.intellij.listeners;
import com.codemp.intellij.jni.WorkspaceHandler; import com.codemp.intellij.jni.WorkspaceHandler;
import com.codemp.intellij.task.BufferEventAwaiterTask; import com.codemp.intellij.task.BufferEventAwaiterTask;
import com.codemp.intellij.util.FileUtil; import com.codemp.intellij.util.FileUtil;
import com.codemp.intellij.workspace.Workspace;
import com.intellij.openapi.Disposable; import com.intellij.openapi.Disposable;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileEditorManager; import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.FileEditorManagerListener; import com.intellij.openapi.fileEditor.FileEditorManagerListener;
import com.intellij.openapi.fileEditor.TextEditor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer; import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;

View file

@ -1,5 +1,6 @@
package com.codemp.intellij.listeners; package com.codemp.intellij.listeners;
import com.codemp.intellij.exceptions.lib.TransportException;
import com.codemp.intellij.jni.BufferHandler; import com.codemp.intellij.jni.BufferHandler;
import com.codemp.intellij.jni.WorkspaceHandler; import com.codemp.intellij.jni.WorkspaceHandler;
import com.codemp.intellij.task.BufferEventAwaiterTask; import com.codemp.intellij.task.BufferEventAwaiterTask;
@ -39,7 +40,7 @@ public class WorkspaceFileOpenedListener implements FileOpenedSyncListener {
String path = FileUtil.getRelativePath(editor.getProject(), file); String path = FileUtil.getRelativePath(editor.getProject(), file);
if(path == null) return; if(path == null) return;
BufferHandler bufferHandler = this.handler.attachToBuffer(path); BufferHandler bufferHandler = this.getBufferForPath(path);
Disposable disp = Disposer.newDisposable(String.format("codemp-buffer-%s", path)); Disposable disp = Disposer.newDisposable(String.format("codemp-buffer-%s", path));
editor.getDocument().addDocumentListener(new BufferEventListener(bufferHandler), disp); editor.getDocument().addDocumentListener(new BufferEventListener(bufferHandler), disp);
@ -47,4 +48,17 @@ public class WorkspaceFileOpenedListener implements FileOpenedSyncListener {
this.task.activeBuffers.put(path, disp); this.task.activeBuffers.put(path, disp);
}); });
} }
/**
* Attach to a buffer or, if it does not exist, implicitly create it.
* @param path the buffer's name (which is the path relative to project root)
* @return the {@link BufferHandler} for it
*/
private BufferHandler getBufferForPath(String path) {
try {
return this.handler.attachToBuffer(path);
} catch (TransportException ignored) {
return this.handler.createBuffer(path);
}
}
} }

View file

@ -1,7 +1,6 @@
package com.codemp.intellij.task; package com.codemp.intellij.task;
import com.codemp.intellij.CodeMP; import com.codemp.intellij.CodeMP;
import com.codemp.intellij.exceptions.lib.ChannelException;
import com.codemp.intellij.exceptions.lib.DeadlockedException; import com.codemp.intellij.exceptions.lib.DeadlockedException;
import com.codemp.intellij.jni.BufferHandler; import com.codemp.intellij.jni.BufferHandler;
import com.codemp.intellij.jni.StringVec; import com.codemp.intellij.jni.StringVec;
@ -15,10 +14,12 @@ import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.progress.ProgressIndicator; import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.Task; import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project; import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.*; import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
public class BufferEventAwaiterTask extends Task.Backgroundable implements Disposable { public class BufferEventAwaiterTask extends Task.Backgroundable implements Disposable {
@ -33,7 +34,6 @@ public class BufferEventAwaiterTask extends Task.Backgroundable implements Dispo
@Override @Override
@SuppressWarnings("InfiniteLoopStatement") @SuppressWarnings("InfiniteLoopStatement")
public void run(@NotNull ProgressIndicator indicator) { public void run(@NotNull ProgressIndicator indicator) {
try {
while(true) { while(true) {
StringVec buffers = new StringVec(); //jni moment StringVec buffers = new StringVec(); //jni moment
this.activeBuffers.keySet().forEach(buffers::push); this.activeBuffers.keySet().forEach(buffers::push);
@ -52,6 +52,7 @@ public class BufferEventAwaiterTask extends Task.Backgroundable implements Dispo
CodeMP.LOGGER.error(e.getMessage()); CodeMP.LOGGER.error(e.getMessage());
continue; continue;
} }
if(changeOptional.isEmpty()) if(changeOptional.isEmpty())
break; break;
TextChangeWrapper change = changeOptional.get(); TextChangeWrapper change = changeOptional.get();
@ -74,10 +75,6 @@ public class BufferEventAwaiterTask extends Task.Backgroundable implements Dispo
bufferEditor.getDocument() bufferEditor.getDocument()
))); )));
} }
} catch(ChannelException ex) { //exited
//TODO handle stop
Disposer.dispose(this); //stopped
}
} }
@Override @Override

View file

@ -1,7 +1,6 @@
package com.codemp.intellij.task; package com.codemp.intellij.task;
import com.codemp.intellij.CodeMP; import com.codemp.intellij.CodeMP;
import com.codemp.intellij.exceptions.lib.ChannelException;
import com.codemp.intellij.jni.CursorEventWrapper; import com.codemp.intellij.jni.CursorEventWrapper;
import com.codemp.intellij.jni.CursorHandler; import com.codemp.intellij.jni.CursorHandler;
import com.codemp.intellij.util.ColorUtil; import com.codemp.intellij.util.ColorUtil;
@ -9,6 +8,7 @@ import com.codemp.intellij.util.FileUtil;
import com.intellij.openapi.Disposable; import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.RangeMarker;
import com.intellij.openapi.editor.markup.HighlighterLayer; import com.intellij.openapi.editor.markup.HighlighterLayer;
import com.intellij.openapi.editor.markup.HighlighterTargetArea; import com.intellij.openapi.editor.markup.HighlighterTargetArea;
import com.intellij.openapi.editor.markup.RangeHighlighter; import com.intellij.openapi.editor.markup.RangeHighlighter;
@ -36,7 +36,6 @@ public class CursorEventAwaiterTask extends Task.Backgroundable implements Dispo
@Override @Override
@SuppressWarnings("InfiniteLoopStatement") @SuppressWarnings("InfiniteLoopStatement")
public void run(@NotNull ProgressIndicator indicator) { public void run(@NotNull ProgressIndicator indicator) {
try {
while(true) { while(true) {
CursorEventWrapper event = this.handler.recv(); CursorEventWrapper event = this.handler.recv();
Editor editor = FileUtil.getActiveEditorByPath(this.myProject, event.getBuffer()); Editor editor = FileUtil.getActiveEditorByPath(this.myProject, event.getBuffer());
@ -85,13 +84,10 @@ public class CursorEventAwaiterTask extends Task.Backgroundable implements Dispo
}); });
} catch(IndexOutOfBoundsException ignored) {} } catch(IndexOutOfBoundsException ignored) {}
} }
} catch(ChannelException ex) { //exited
this.run(indicator);
}
} }
@Override @Override
public void dispose() { public void dispose() {
this.highlighterMap.forEach((s, r) -> r.dispose()); this.highlighterMap.values().forEach(RangeMarker::dispose);
} }
} }

View file

@ -13,27 +13,11 @@
<action id="codemp.fast-forward" class="com.codemp.intellij.actions.FastForwardAction" text="Just Hurry"/> <action id="codemp.fast-forward" class="com.codemp.intellij.actions.FastForwardAction" text="Just Hurry"/>
<action id="codemp.connect" class="com.codemp.intellij.actions.ConnectAction" text="Connect..."/> <action id="codemp.connect" class="com.codemp.intellij.actions.ConnectAction" text="Connect..."/>
<action id="codemp.connect" class="com.codemp.intellij.actions.DisconnectAction" text="Disconnect"/> <action id="codemp.connect" class="com.codemp.intellij.actions.DisconnectAction" text="Disconnect"/>
<group id="codemp.workspace" text="Workspace" popup="true">
<action id="codemp.workspace.join" class="com.codemp.intellij.actions.workspace.WorkspaceJoinAction" <action id="codemp.workspace.join" class="com.codemp.intellij.actions.workspace.WorkspaceJoinAction"
text="Join Workspace"/> text="Join Workspace"/>
<action id="codemp.workspace.leave" class="com.codemp.intellij.actions.workspace.WorkspaceLeaveAction" <action id="codemp.workspace.leave" class="com.codemp.intellij.actions.workspace.WorkspaceLeaveAction"
text="Leave Workspace"/> text="Leave Workspace"/>
</group> </group>
<group id="codemp.buffer" text="Buffer" popup="true">
<action id="codemp.buffer.create" class="com.codemp.intellij.actions.buffer.BufferCreateAction"
text="Create Buffer"/>
<action id="codemp.buffer.create-with-content"
class="com.codemp.intellij.actions.buffer.BufferCreateWithContentAction"
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>
</group>
</actions> </actions>
<extensions defaultExtensionNs="com.intellij"> <extensions defaultExtensionNs="com.intellij">

View file

@ -76,9 +76,9 @@ impl WorkspaceHandler {
#[generate_interface] #[generate_interface]
/// create a new buffer in current workspace /// create a new buffer in current workspace
fn create_buffer(&mut self, path: &str) -> CodempResult<Option<BufferHandler>> { fn create_buffer(&mut self, path: &str) -> CodempResult<BufferHandler> {
RT.block_on(RT.block_on(self.workspace.write()).create(path))?; RT.block_on(RT.block_on(self.workspace.write()).create(path))?;
Ok(self.get_buffer(path)) Ok(self.get_buffer(path).unwrap())
} }
#[generate_interface] #[generate_interface]
@ -144,7 +144,7 @@ impl WorkspaceHandler {
/// get a [crate::BufferHandler] for one of the workspace's buffers /// get a [crate::BufferHandler] for one of the workspace's buffers
fn get_buffer(&self, path: &str) -> Option<BufferHandler> { fn get_buffer(&self, path: &str) -> Option<BufferHandler> {
RT.block_on(self.workspace.read()).buffer_by_name(path) RT.block_on(self.workspace.read()).buffer_by_name(path)
.map(|b| BufferHandler { buffer: b }) .map(|buffer| BufferHandler { buffer })
} }
#[generate_interface] #[generate_interface]