feat: bundle library, generate hash colors from name, misc cleanup

This commit is contained in:
zaaarf 2023-11-16 21:35:15 +01:00
parent 4e28c9a65a
commit d85c4370f6
No known key found for this signature in database
GPG key ID: 82240E075E31FA4C
9 changed files with 131 additions and 75 deletions

4
.gitignore vendored
View file

@ -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

View file

@ -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'
}

View file

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

View file

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

View file

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

View file

@ -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 {

View file

@ -27,11 +27,3 @@ public class BufferEventListener implements DocumentListener {
}
}
}
/*
ABCD
ABAACD
getOffset() -> B + 1
*/

View file

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

View file

@ -40,10 +40,8 @@
</group>
</actions>
<!-- Extension points defined by the plugin.
Read more: https://plugins.jetbrains.com/docs/intellij/plugin-extension-points.html -
<extensions defaultExtensionNs="com.intellij">
<!-- Extension points defined by the plugin.
Read more: https://plugins.jetbrains.com/docs/intellij/plugin-extension-points.html -->
</extensions>
</idea-plugin>
->
</idea-plugin>