From d85c4370f6bb302c9d393f5282d47ddbc052cf11 Mon Sep 17 00:00:00 2001 From: zaaarf Date: Thu, 16 Nov 2023 21:35:15 +0100 Subject: [PATCH] feat: bundle library, generate hash colors from name, misc cleanup --- .gitignore | 4 +- build.gradle | 11 +- src/main/java/com/codemp/intellij/CodeMP.java | 21 ++- .../intellij/actions/ConnectAction.java | 14 +- .../codemp/intellij/actions/JoinAction.java | 120 +++++++++++------- .../actions/buffer/BufferAttachAction.java | 1 + .../listeners/BufferEventListener.java | 8 -- .../com/codemp/intellij/util/ColorUtil.java | 19 +++ src/main/resources/META-INF/plugin.xml | 8 +- 9 files changed, 131 insertions(+), 75 deletions(-) create mode 100644 src/main/java/com/codemp/intellij/util/ColorUtil.java diff --git a/.gitignore b/.gitignore index 6a739b6..cfbd361 100644 --- a/.gitignore +++ b/.gitignore @@ -47,5 +47,7 @@ Cargo.lock .cargo target -# Do not inclue generated code +# Do not include generated code src/main/java/com/codemp/intellij/jni +src/main/resources/*.so +src/main/resources/*.dll \ No newline at end of file diff --git a/build.gradle b/build.gradle index a2dc4bb..4bdf3b9 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,6 @@ plugins { id 'java' - id 'org.jetbrains.intellij' version '1.14.1' + id 'org.jetbrains.intellij' version '1.16.0' } group = "com.codemp" @@ -53,6 +53,15 @@ tasks.register('cargoBuild', Exec) { compileJava.dependsOn cargoBuild +tasks.register('copyBinary', Copy) { + from "target/debug" + into "src/main/resources" + include "*.so", "*.dll" + dependsOn cargoBuild +} + +patchPluginXml.dependsOn copyBinary + tasks.register('deleteGeneratedNativeInterface', Delete) { delete 'src/main/java/com/codemp/intellij/jni' } diff --git a/src/main/java/com/codemp/intellij/CodeMP.java b/src/main/java/com/codemp/intellij/CodeMP.java index 5efb84e..f87303a 100644 --- a/src/main/java/com/codemp/intellij/CodeMP.java +++ b/src/main/java/com/codemp/intellij/CodeMP.java @@ -1,8 +1,27 @@ package com.codemp.intellij; +import com.intellij.openapi.util.SystemInfo; +import cz.adamh.utils.NativeUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.IOException; + public class CodeMP { public static Logger LOGGER = LoggerFactory.getLogger(CodeMP.class); -} + + private static boolean loadedLibrary = false; + public static void loadLibrary() { + if(!loadedLibrary) { + try { + if(SystemInfo.isWindows) //TODO on win for some reason it bundles it twice + NativeUtils.loadLibraryFromJar("/codemp_intellij.dll"); + else NativeUtils.loadLibraryFromJar("/libcodemp_intellij.so"); + } catch(IOException e) { + throw new RuntimeException(e); + } finally { + loadedLibrary = false; + } + } + } +} \ No newline at end of file diff --git a/src/main/java/com/codemp/intellij/actions/ConnectAction.java b/src/main/java/com/codemp/intellij/actions/ConnectAction.java index 45de1dd..d9c73f6 100644 --- a/src/main/java/com/codemp/intellij/actions/ConnectAction.java +++ b/src/main/java/com/codemp/intellij/actions/ConnectAction.java @@ -5,23 +5,11 @@ import com.codemp.intellij.jni.CodeMPHandler; import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.ui.Messages; -import cz.adamh.utils.NativeUtils; import org.jetbrains.annotations.NotNull; -import java.io.IOException; - public class ConnectAction extends AnAction { - public ConnectAction() { - /*try { - NativeUtils.loadLibraryFromJar("/resources/libHelloJNI.so"); - } catch(IOException e) { - throw new RuntimeException(e); - }*/ - //System.load("/home/zaaarf/dev/irl/rust/codemp/client/intellij/target/debug/libcodemp_intellij.so"); - System.load("O:/dev/IRL/Rust/codemp/client/intellij/target/debug/codemp_intellij.dll"); - } - public static void connect(String url, boolean silent) throws Exception { + CodeMP.loadLibrary(); //will only load it the first time CodeMPHandler.connect(url); if(!silent) Messages.showInfoMessage(String.format("Connected to %s!", url), "CodeMP"); CodeMP.LOGGER.debug("Connected to {}!", url); diff --git a/src/main/java/com/codemp/intellij/actions/JoinAction.java b/src/main/java/com/codemp/intellij/actions/JoinAction.java index fb514c3..480dea3 100644 --- a/src/main/java/com/codemp/intellij/actions/JoinAction.java +++ b/src/main/java/com/codemp/intellij/actions/JoinAction.java @@ -6,13 +6,13 @@ import com.codemp.intellij.jni.CursorEventWrapper; import com.codemp.intellij.jni.CursorHandler; import com.codemp.intellij.listeners.CursorEventListener; import com.codemp.intellij.util.ActionUtil; +import com.codemp.intellij.util.ColorUtil; +import com.intellij.openapi.Disposable; import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.application.ApplicationManager; -import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.EditorFactory; -import com.intellij.openapi.editor.colors.TextAttributesKey; import com.intellij.openapi.editor.markup.HighlighterLayer; import com.intellij.openapi.editor.markup.HighlighterTargetArea; import com.intellij.openapi.editor.markup.RangeHighlighter; @@ -20,8 +20,8 @@ import com.intellij.openapi.editor.markup.TextAttributes; import com.intellij.openapi.progress.ProgressIndicator; import com.intellij.openapi.progress.ProgressManager; import com.intellij.openapi.progress.Task; +import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.Messages; -import com.intellij.ui.JBColor; import org.jetbrains.annotations.NotNull; import java.awt.*; @@ -32,58 +32,26 @@ public class JoinAction extends AnAction { private static final Map highlighterMap = new HashMap<>(); - private static final TextAttributes HIGHLIGHTED = new TextAttributes( - null, JBColor.BLUE, null, null, Font.PLAIN - ); - public static void join(AnActionEvent e, String session, boolean silent) throws Exception { CursorHandler cursorHandler = CodeMPHandler.join(session); - EditorFactory.getInstance() - .getEventMulticaster() - .addCaretListener(new CursorEventListener()); if(!silent) Messages.showInfoMessage(String.format("Joined session %s!", session), "CodeMP"); else CodeMP.LOGGER.debug("Joined session {}!", session); Editor editor = ActionUtil.getCurrentEditor(e); - Document document = editor.getDocument(); - ProgressManager.getInstance().run(new Task.Backgroundable(e.getProject(), "Awaiting CodeMP cursor events") { - @Override - public void run(@NotNull ProgressIndicator indicator) { - while(true) { - try { - CursorEventWrapper event = cursorHandler.recv(); - ApplicationManager.getApplication().invokeLater(() -> { - try { - RangeHighlighter h = highlighterMap.get(event.getUser()); - if(h != null) - h.dispose(); - CodeMP.LOGGER.debug( - "Cursor moved by user {}! Start pos: {}x {}y; end pos: {}x {}y with buffer {}!", - event.getUser(), - event.getStartCol(), event.getStartCol(), - event.getEndRow(), event.getEndCol(), - event.getBuffer()); + CursorEventAwaiter task = new CursorEventAwaiter( + e.getProject(), + "Awaiting CodeMP cursor events", + cursorHandler, + editor + ); - highlighterMap.put(event.getUser(), editor - .getMarkupModel() - .addRangeHighlighter(TextAttributesKey.createTextAttributesKey("codemp", HIGHLIGHTED), - document.getLineStartOffset(event.getStartRow()) + event.getStartCol(), - document.getLineStartOffset(event.getEndRow()) + event.getEndCol(), - HighlighterLayer.SELECTION, - HighlighterTargetArea.EXACT_RANGE - )); - } catch(Exception ex) { - throw new RuntimeException(); - } - }); - } catch(Exception ex) { - throw new RuntimeException(ex); - } - } - } - }); + EditorFactory.getInstance() + .getEventMulticaster() + .addCaretListener(new CursorEventListener(), task); + + ProgressManager.getInstance().run(task); } @Override @@ -103,4 +71,64 @@ public class JoinAction extends AnAction { "CodeMP Join"); } } + + //TODO this is janky as it shows a progress bar it doesn't use tbh + //implements disposable so i can use it as lifetime ig + private static class CursorEventAwaiter extends Task.Backgroundable implements Disposable { + + private final CursorHandler handler; + private final Editor editor; + + public CursorEventAwaiter(Project project, String title, CursorHandler handler, Editor editor) { + super(project, title); + this.handler = handler; + this.editor = editor; + } + + @Override + public void dispose() {} + + @Override + @SuppressWarnings("InfiniteLoopStatement") + public void run(@NotNull ProgressIndicator indicator) { + while(true) { + try { + CursorEventWrapper event = handler.recv(); + ApplicationManager.getApplication().invokeLater(() -> { + try { + RangeHighlighter highlighter = highlighterMap.get(event.getUser()); + if(highlighter != null) + highlighter.dispose(); + + CodeMP.LOGGER.debug( + "Cursor moved by user {}! Start pos: {}x {}y; end pos: {}x {}y with buffer {}!", + event.getUser(), + event.getStartCol(), event.getStartCol(), + event.getEndRow(), event.getEndCol(), + event.getBuffer()); + + highlighterMap.put(event.getUser(), this.editor + .getMarkupModel() + .addRangeHighlighter( + this.editor.getDocument().getLineStartOffset(event.getStartRow()) + event.getStartCol(), + this.editor.getDocument().getLineStartOffset(event.getEndRow()) + event.getEndCol(), + HighlighterLayer.SELECTION, + new TextAttributes( + null, + ColorUtil.colorFromUsername(event.getUser()), + null, + null, + Font.PLAIN + ), HighlighterTargetArea.EXACT_RANGE + )); + } catch(Exception ex) { + throw new RuntimeException(); + } + }); + } catch(Exception ex) { + throw new RuntimeException(ex); + } + } + } + } } diff --git a/src/main/java/com/codemp/intellij/actions/buffer/BufferAttachAction.java b/src/main/java/com/codemp/intellij/actions/buffer/BufferAttachAction.java index e1fdf36..b9c02d2 100644 --- a/src/main/java/com/codemp/intellij/actions/buffer/BufferAttachAction.java +++ b/src/main/java/com/codemp/intellij/actions/buffer/BufferAttachAction.java @@ -32,6 +32,7 @@ public class BufferAttachAction extends AnAction { ProgressManager.getInstance().run(new Task.Backgroundable(e.getProject(), "Awaiting CodeMP buffer events") { @Override + @SuppressWarnings({"InfiniteLoopStatement", "UnstableApiUsage"}) public void run(@NotNull ProgressIndicator indicator) { while(true) { try { diff --git a/src/main/java/com/codemp/intellij/listeners/BufferEventListener.java b/src/main/java/com/codemp/intellij/listeners/BufferEventListener.java index e07320a..09e5bbc 100644 --- a/src/main/java/com/codemp/intellij/listeners/BufferEventListener.java +++ b/src/main/java/com/codemp/intellij/listeners/BufferEventListener.java @@ -27,11 +27,3 @@ public class BufferEventListener implements DocumentListener { } } } -/* -ABCD -ABAACD - -getOffset() -> B + 1 - - - */ diff --git a/src/main/java/com/codemp/intellij/util/ColorUtil.java b/src/main/java/com/codemp/intellij/util/ColorUtil.java new file mode 100644 index 0000000..b7a0164 --- /dev/null +++ b/src/main/java/com/codemp/intellij/util/ColorUtil.java @@ -0,0 +1,19 @@ +package com.codemp.intellij.util; + +import com.intellij.ui.JBColor; + +import java.awt.Color; + +public class ColorUtil { + public static JBColor colorFromUsername(String username) { + int hash = username.hashCode(); + + Color hashColor = Color.decode( //Integer.toHexString(((hash >> 24) & 0xFF)) + + Integer.toHexString(((hash >> 16) & 0xFF)) + + Integer.toHexString(((hash >> 8) & 0xFF)) + + Integer.toHexString((hash & 0xFF)) + ); + + return new JBColor(hashColor, hashColor.darker()); + } +} diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 8219456..03cf9b9 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -40,10 +40,8 @@ - - - -> \ No newline at end of file + \ No newline at end of file