chore: a bit of cleanup

This commit is contained in:
zaaarf 2023-11-16 16:36:25 +01:00
parent 5b7d3c95dd
commit cebcad7327
No known key found for this signature in database
GPG key ID: 82240E075E31FA4C
14 changed files with 102 additions and 52 deletions

View file

@ -1,3 +1,8 @@
package com.codemp.intellij; package com.codemp.intellij;
public class CodeMP {} import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CodeMP {
public static Logger LOGGER = LoggerFactory.getLogger(CodeMP.class);
}

View file

@ -1,5 +1,6 @@
package com.codemp.intellij.actions; package com.codemp.intellij.actions;
import com.codemp.intellij.CodeMP;
import com.codemp.intellij.jni.CodeMPHandler; import com.codemp.intellij.jni.CodeMPHandler;
import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.actionSystem.AnActionEvent;
@ -11,7 +12,6 @@ import java.io.IOException;
public class ConnectAction extends AnAction { public class ConnectAction extends AnAction {
public ConnectAction() { public ConnectAction() {
super();
/*try { /*try {
NativeUtils.loadLibraryFromJar("/resources/libHelloJNI.so"); NativeUtils.loadLibraryFromJar("/resources/libHelloJNI.so");
} catch(IOException e) { } catch(IOException e) {
@ -21,19 +21,18 @@ public class ConnectAction extends AnAction {
System.load("O:/dev/IRL/Rust/codemp/client/intellij/target/debug/codemp_intellij.dll"); System.load("O:/dev/IRL/Rust/codemp/client/intellij/target/debug/codemp_intellij.dll");
} }
public void connect(String url) throws Exception { public static void connect(String url, boolean silent) throws Exception {
CodeMPHandler.connect(url); CodeMPHandler.connect(url);
//Messages.showInfoMessage(String.format("Connected to %s!", url), "CodeMP"); if(!silent) Messages.showInfoMessage(String.format("Connected to %s!", url), "CodeMP");
System.out.printf("Connected to %s!\n", url); CodeMP.LOGGER.debug("Connected to {}!", url);
} }
@Override @Override
public void actionPerformed(@NotNull AnActionEvent e) { public void actionPerformed(@NotNull AnActionEvent e) {
String url = Messages.showInputDialog("URL to CodeMP instance:", "CodeMP Connect", String url = Messages.showInputDialog("URL to CodeMP instance:", "CodeMP Connect",
Messages.getQuestionIcon()); Messages.getQuestionIcon());
try { try {
this.connect(url); connect(url, false);
} catch(Exception ex) { } catch(Exception ex) {
Messages.showErrorDialog(String.format("Failed to connect to %s: %s!", url, ex.getMessage()), "CodeMP"); Messages.showErrorDialog(String.format("Failed to connect to %s: %s!", url, ex.getMessage()), "CodeMP");
} }

View file

@ -5,16 +5,16 @@ import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.actionSystem.AnActionEvent;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/**
* Used exclusively to streamline debugging.
*/
public class FastForwardAction extends AnAction { public class FastForwardAction extends AnAction {
@Override @Override
public void actionPerformed(@NotNull AnActionEvent e) { public void actionPerformed(@NotNull AnActionEvent e) {
ConnectAction connectAction = new ConnectAction();
JoinAction joinAction = new JoinAction();
BufferAttachAction attachAction = new BufferAttachAction();
try { try {
connectAction.connect("http://alemi.dev:50051"); ConnectAction.connect("http://alemi.dev:50051", true);
joinAction.join(e, "default"); JoinAction.join(e, "default", true);
attachAction.attach(e, "test"); BufferAttachAction.attach(e, "test", true);
} catch(Exception ex) { } catch(Exception ex) {
throw new RuntimeException(ex); throw new RuntimeException(ex);
} }

View file

@ -1,9 +1,11 @@
package com.codemp.intellij.actions; package com.codemp.intellij.actions;
import com.codemp.intellij.CodeMP;
import com.codemp.intellij.jni.CodeMPHandler; import com.codemp.intellij.jni.CodeMPHandler;
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.listeners.CursorEventListener; import com.codemp.intellij.listeners.CursorEventListener;
import com.codemp.intellij.util.ActionUtil;
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.application.ApplicationManager; import com.intellij.openapi.application.ApplicationManager;
@ -15,7 +17,6 @@ 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;
import com.intellij.openapi.editor.markup.TextAttributes; import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.progress.ProgressIndicator; import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager; import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task; import com.intellij.openapi.progress.Task;
@ -26,26 +27,25 @@ import org.jetbrains.annotations.NotNull;
import java.awt.*; import java.awt.*;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects;
public class JoinAction extends AnAction { public class JoinAction extends AnAction {
private final Map<String, RangeHighlighter> highlighterMap = new HashMap<>(); private static final Map<String, RangeHighlighter> highlighterMap = new HashMap<>();
private static final TextAttributes HIGHLIGHTED = new TextAttributes( private static final TextAttributes HIGHLIGHTED = new TextAttributes(
null, JBColor.BLUE, null, null, Font.PLAIN null, JBColor.BLUE, null, null, Font.PLAIN
); );
public void join(AnActionEvent e, String session) throws Exception { public static void join(AnActionEvent e, String session, boolean silent) throws Exception {
CursorHandler cursorHandler = CodeMPHandler.join(session); CursorHandler cursorHandler = CodeMPHandler.join(session);
EditorFactory.getInstance() EditorFactory.getInstance()
.getEventMulticaster() .getEventMulticaster()
.addCaretListener(new CursorEventListener()); .addCaretListener(new CursorEventListener());
//Messages.showInfoMessage(String.format("Joined %s!", session), "CodeMP");
System.out.printf(String.format("Joined %s!\n", session)); if(!silent) Messages.showInfoMessage(String.format("Joined session %s!", session), "CodeMP");
Editor editor = FileEditorManager.getInstance(Objects.requireNonNull(e.getProject())) else CodeMP.LOGGER.debug("Joined session {}!", session);
.getSelectedTextEditor();
assert editor != null; Editor editor = ActionUtil.getCurrentEditor(e);
Document document = editor.getDocument(); Document document = editor.getDocument();
ProgressManager.getInstance().run(new Task.Backgroundable(e.getProject(), "Awaiting CodeMP cursor events") { ProgressManager.getInstance().run(new Task.Backgroundable(e.getProject(), "Awaiting CodeMP cursor events") {
@Override @Override
@ -59,8 +59,8 @@ public class JoinAction extends AnAction {
if(h != null) if(h != null)
h.dispose(); h.dispose();
System.out.printf( CodeMP.LOGGER.debug(
"Cursor moved by user %s! Start pos: x%d y%d; end pos: x%d y%d with buffer %s!\n", "Cursor moved by user {}! Start pos: {}x {}y; end pos: {}x {}y with buffer {}!",
event.getUser(), event.getUser(),
event.getStartCol(), event.getStartCol(), event.getStartCol(), event.getStartCol(),
event.getEndRow(), event.getEndCol(), event.getEndRow(), event.getEndCol(),
@ -94,7 +94,7 @@ public class JoinAction extends AnAction {
Messages.getQuestionIcon()); Messages.getQuestionIcon());
try { try {
this.join(e, session); join(e, session, false);
} catch(Exception ex) { } catch(Exception ex) {
Messages.showErrorDialog(String.format( Messages.showErrorDialog(String.format(
"Failed to join session %s: %s!", "Failed to join session %s: %s!",

View file

@ -1,37 +1,33 @@
package com.codemp.intellij.actions.buffer; package com.codemp.intellij.actions.buffer;
import com.codemp.intellij.CodeMP;
import com.codemp.intellij.jni.BufferHandler; import com.codemp.intellij.jni.BufferHandler;
import com.codemp.intellij.jni.CodeMPHandler; import com.codemp.intellij.jni.CodeMPHandler;
import com.codemp.intellij.jni.TextChangeWrapper; import com.codemp.intellij.jni.TextChangeWrapper;
import com.codemp.intellij.listeners.BufferEventListener; import com.codemp.intellij.listeners.BufferEventListener;
import com.codemp.intellij.util.ActionUtil;
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.application.ApplicationManager; import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.progress.ProgressIndicator; import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager; import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task; import com.intellij.openapi.progress.Task;
import com.intellij.openapi.ui.Messages; import com.intellij.openapi.ui.Messages;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import javax.print.Doc;
import java.util.Objects;
public class BufferAttachAction extends AnAction { public class BufferAttachAction extends AnAction {
public void attach(AnActionEvent e, String buffer) throws Exception { public static void attach(AnActionEvent e, String buffer, boolean silent) throws Exception {
BufferHandler bufferHandler = CodeMPHandler.attach(buffer); BufferHandler bufferHandler = CodeMPHandler.attach(buffer);
//Messages.showInfoMessage(String.format("Connected to %s!", url), "CodeMP"); if(!silent) Messages.showInfoMessage(String.format("Attached to buffer to %s!", buffer),
"CodeMP Buffer Attach");
CodeMP.LOGGER.debug("Attached to buffer to {}!", buffer);
//register buffer change listener //register buffer change listener
//TODO "get" the Document corresponding to buffer, for now use the current one //TODO "get" the Document corresponding to buffer, for now use the current one
BufferEventListener listener = new BufferEventListener(buffer); BufferEventListener listener = new BufferEventListener(buffer);
assert e.getProject() != null; Document document = ActionUtil.getCurrentEditor(e).getDocument();
Editor editor = FileEditorManager.getInstance(e.getProject()).getSelectedTextEditor();
assert editor != null;
Document document = editor.getDocument();
document.addDocumentListener(listener); document.addDocumentListener(listener);
ProgressManager.getInstance().run(new Task.Backgroundable(e.getProject(), "Awaiting CodeMP buffer events") { ProgressManager.getInstance().run(new Task.Backgroundable(e.getProject(), "Awaiting CodeMP buffer events") {
@ -42,7 +38,7 @@ public class BufferAttachAction extends AnAction {
TextChangeWrapper event = bufferHandler.recv(); TextChangeWrapper event = bufferHandler.recv();
ApplicationManager.getApplication().invokeLaterOnWriteThread(() -> ApplicationManager.getApplication().invokeLaterOnWriteThread(() ->
ApplicationManager.getApplication().runWriteAction(() -> { ApplicationManager.getApplication().runWriteAction(() -> {
System.out.printf("Received text change %s from offset %d to %d!\n", CodeMP.LOGGER.debug("Received text change {} from offset {} to {}!\n",
event.getContent(), event.getStart(), event.getEnd()); event.getContent(), event.getStart(), event.getEnd());
document.replaceString( //TODO this doesn't work document.replaceString( //TODO this doesn't work
(int) event.getStart(), (int) event.getStart(),
@ -56,7 +52,6 @@ public class BufferAttachAction extends AnAction {
} }
} }
}); });
System.out.printf("Created buffer %s!\n", buffer);
} }
@Override @Override
@ -65,9 +60,8 @@ public class BufferAttachAction extends AnAction {
"Buffer name:", "Buffer name:",
"Attach to CodeMP Buffer", "Attach to CodeMP Buffer",
Messages.getQuestionIcon()); Messages.getQuestionIcon());
try { try {
this.attach(e, buffer); attach(e, buffer, false);
} catch(Exception ex) { } catch(Exception ex) {
Messages.showErrorDialog(String.format( Messages.showErrorDialog(String.format(
"Failed to attach to buffer %s: %s!", "Failed to attach to buffer %s: %s!",

View file

@ -1,5 +1,6 @@
package com.codemp.intellij.actions.buffer; package com.codemp.intellij.actions.buffer;
import com.codemp.intellij.CodeMP;
import com.codemp.intellij.jni.CodeMPHandler; import com.codemp.intellij.jni.CodeMPHandler;
import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.actionSystem.AnActionEvent;
@ -7,6 +8,12 @@ import com.intellij.openapi.ui.Messages;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
public class BufferCreateAction extends AnAction { public class BufferCreateAction extends AnAction {
public static void create(String buffer, boolean silent) throws Exception {
CodeMPHandler.create(buffer);
if(!silent) Messages.showInfoMessage(String.format("Created buffer %s!", buffer),
"Create CodeMP Buffer" );
CodeMP.LOGGER.debug("Created buffer {}!", buffer);
}
@Override @Override
public void actionPerformed(@NotNull AnActionEvent e) { public void actionPerformed(@NotNull AnActionEvent e) {
@ -16,9 +23,7 @@ public class BufferCreateAction extends AnAction {
Messages.getQuestionIcon()); Messages.getQuestionIcon());
try { try {
CodeMPHandler.create(buffer); create(buffer, false);
//Messages.showInfoMessage(String.format("Created buffer %s!", url), "Create CodeMP Buffer" );
System.out.printf("Created buffer %s!\n", buffer);
} catch(Exception ex) { } catch(Exception ex) {
Messages.showErrorDialog(String.format( Messages.showErrorDialog(String.format(
"Failed to create buffer with name %s: %s!", "Failed to create buffer with name %s: %s!",

View file

@ -1,6 +1,6 @@
package com.codemp.intellij.exceptions; package com.codemp.intellij.exceptions;
public class CodeMPException extends Exception { public class CodeMPException extends RuntimeException {
public CodeMPException(String s) { public CodeMPException(String s) {
super(s); super(s);
} }

View file

@ -0,0 +1,14 @@
package com.codemp.intellij.exceptions.ide;
import com.codemp.intellij.exceptions.CodeMPException;
import com.intellij.openapi.actionSystem.AnActionEvent;
/**
* Fired when trying to use {@link com.intellij.openapi.actionSystem.AnActionEvent}'s context
* from a state where that use is not supported.
*/
public class BadActionEventStateException extends CodeMPException {
public BadActionEventStateException(String s) {
super(s);
}
}

View file

@ -1,4 +1,6 @@
package com.codemp.intellij.exceptions; package com.codemp.intellij.exceptions.rust;
import com.codemp.intellij.exceptions.CodeMPException;
public class ChannelException extends CodeMPException { public class ChannelException extends CodeMPException {
public ChannelException(String input) { public ChannelException(String input) {

View file

@ -1,4 +1,6 @@
package com.codemp.intellij.exceptions; package com.codemp.intellij.exceptions.rust;
import com.codemp.intellij.exceptions.CodeMPException;
public class InvalidStateException extends CodeMPException { public class InvalidStateException extends CodeMPException {
public InvalidStateException(String message) { public InvalidStateException(String message) {

View file

@ -1,4 +1,6 @@
package com.codemp.intellij.exceptions; package com.codemp.intellij.exceptions.rust;
import com.codemp.intellij.exceptions.CodeMPException;
public class TransportException extends CodeMPException { public class TransportException extends CodeMPException {
public TransportException(String message) { public TransportException(String message) {

View file

@ -1,5 +1,6 @@
package com.codemp.intellij.listeners; package com.codemp.intellij.listeners;
import com.codemp.intellij.CodeMP;
import com.codemp.intellij.jni.CodeMPHandler; import com.codemp.intellij.jni.CodeMPHandler;
import com.intellij.openapi.editor.Caret; import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.VisualPosition; import com.intellij.openapi.editor.VisualPosition;
@ -10,16 +11,14 @@ import org.jetbrains.annotations.NotNull;
public class CursorEventListener implements CaretListener { public class CursorEventListener implements CaretListener {
@Override @Override
public void caretPositionChanged(@NotNull CaretEvent event) { public void caretPositionChanged(@NotNull CaretEvent event) {
System.out.println("called!");
Caret caret = event.getCaret(); Caret caret = event.getCaret();
if(caret == null) if(caret == null)
return; return;
System.out.println("valid caret!");
try { try {
VisualPosition startPos = caret.getSelectionStartPosition(); VisualPosition startPos = caret.getSelectionStartPosition();
VisualPosition endPos = caret.getSelectionEndPosition(); VisualPosition endPos = caret.getSelectionEndPosition();
System.out.printf("start %dx %dy end %dx %dy", CodeMP.LOGGER.debug("Caret moved from {}x {}y to {}x {}y",
startPos.line, startPos.column, endPos.line, endPos.column); startPos.line, startPos.column, endPos.line, endPos.column);
CodeMPHandler.getCursor().send( CodeMPHandler.getCursor().send(
"", startPos.line, startPos.column, endPos.line, endPos.column "", startPos.line, startPos.column, endPos.line, endPos.column

View file

@ -0,0 +1,24 @@
package com.codemp.intellij.util;
import com.codemp.intellij.exceptions.ide.BadActionEventStateException;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
public class ActionUtil {
public static Project getCurrentProject(AnActionEvent event) {
Project project = event.getProject();
if(project == null)
throw new BadActionEventStateException("Project was null!");
return project;
}
public static Editor getCurrentEditor(AnActionEvent event) {
Editor editor = FileEditorManager.getInstance(getCurrentProject(event))
.getSelectedTextEditor();
if(editor == null)
throw new BadActionEventStateException("Editor was null!");
return editor;
}
}

View file

@ -25,12 +25,15 @@
<actions> <actions>
<group id="codemp" text="CodeMP" popup="true"> <group id="codemp" text="CodeMP" popup="true">
<add-to-group group-id="ToolsMenu" anchor="first"/> <add-to-group group-id="ToolsMenu" anchor="first"/>
<action id="codemp.fast-forward" class="com.codemp.intellij.actions.FastForwardAction" text="Pls 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.join" class="com.codemp.intellij.actions.JoinAction" text="Join..."/> <action id="codemp.join" class="com.codemp.intellij.actions.JoinAction" text="Join..."/>
<group id="codemp.buffer" text="Buffer" popup="true"> <group id="codemp.buffer" text="Buffer" popup="true">
<action id="codemp.buffer.create" class="com.codemp.intellij.actions.buffer.BufferCreateAction" <action id="codemp.buffer.create" class="com.codemp.intellij.actions.buffer.BufferCreateAction"
text="Create Buffer"/> 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" <action id="codemp.buffer.attach" class="com.codemp.intellij.actions.buffer.BufferAttachAction"
text="Attach to Buffer"/> text="Attach to Buffer"/>
</group> </group>
@ -38,8 +41,9 @@
</actions> </actions>
<!-- Extension points defined by the plugin. <!-- Extension points defined by the plugin.
Read more: https://plugins.jetbrains.com/docs/intellij/plugin-extension-points.html --> Read more: https://plugins.jetbrains.com/docs/intellij/plugin-extension-points.html -
<extensions defaultExtensionNs="com.intellij"> <extensions defaultExtensionNs="com.intellij">
</extensions> </extensions>
</idea-plugin> </idea-plugin>
->