feat: working BufferAttach, various fixes, upgrade to woot

This commit is contained in:
zaaarf 2023-11-17 02:46:39 +01:00
parent 9e16c21454
commit dfae6d6b9d
No known key found for this signature in database
GPG key ID: 82240E075E31FA4C
7 changed files with 52 additions and 34 deletions

View file

@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021"
[dependencies]
codemp = { git = "ssh://git@github.com/codewithotherpeopleandchangenamelater/codemp.git", tag = "v0.4.5", features = ["global", "sync"] }
codemp = { git = "ssh://git@github.com/codewithotherpeopleandchangenamelater/codemp.git", branch = "woot", features = ["global", "sync"] }
jni = { version = "0.21.1", features = ["invocation"] }
jni-sys = "0.3.0"
log = "0.4.20"

View file

@ -12,7 +12,7 @@ public class FastForwardAction extends AnAction {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
try {
ConnectAction.connect("http://alemi.dev:50051", true);
ConnectAction.connect("http://alemi.dev:50052", true);
JoinAction.join(e, "default", true);
BufferAttachAction.attach(e, "test", true);
} catch(Exception ex) {

View file

@ -94,6 +94,9 @@ public class JoinAction extends AnAction {
while(true) {
try {
CursorEventWrapper event = handler.recv();
int startOffset = this.editor.getDocument().getLineStartOffset(event.getStartRow()) + event.getStartCol();
int endOffset = this.editor.getDocument().getLineStartOffset(event.getEndRow()) + event.getEndCol();
ApplicationManager.getApplication().invokeLater(() -> {
try {
RangeHighlighter highlighter = highlighterMap.get(event.getUser());
@ -110,8 +113,8 @@ public class JoinAction extends AnAction {
highlighterMap.put(event.getUser(), this.editor
.getMarkupModel()
.addRangeHighlighter(
this.editor.getDocument().getLineStartOffset(event.getStartRow()) + event.getStartCol(),
this.editor.getDocument().getLineStartOffset(event.getEndRow()) + event.getEndCol(),
startOffset,
endOffset,
HighlighterLayer.SELECTION,
new TextAttributes(
null,
@ -121,8 +124,12 @@ public class JoinAction extends AnAction {
Font.PLAIN
), HighlighterTargetArea.EXACT_RANGE
));
} catch(IllegalArgumentException ex) {
//suppress if the cursor only exceeds length by one, it's probably just him adding something at EOF
if(endOffset - this.editor.getDocument().getTextLength() != 1)
throw ex;
} catch(Exception ex) {
throw new RuntimeException();
throw new RuntimeException(ex);
}
});
} catch(Exception ex) {

View file

@ -9,10 +9,12 @@ 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.Document;
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 org.jetbrains.annotations.NotNull;
@ -27,6 +29,8 @@ public class BufferAttachAction extends AnAction {
//register buffer change listener
//TODO "get" the Document corresponding to buffer, for now use the current one
BufferEventListener listener = new BufferEventListener(buffer);
Project project = ActionUtil.getCurrentProject(e);
Document document = ActionUtil.getCurrentEditor(e).getDocument();
document.addDocumentListener(listener);
@ -37,17 +41,21 @@ public class BufferAttachAction extends AnAction {
while(true) {
try {
TextChangeWrapper event = bufferHandler.recv();
ApplicationManager.getApplication().invokeLaterOnWriteThread(() ->
CodeMP.LOGGER.debug("Received text change {} from offset {} to {}!",
event.getContent(), event.getStart(), event.getEnd());
ApplicationManager.getApplication().invokeLaterOnWriteThread(() -> {
ApplicationManager.getApplication().runWriteAction(() -> {
CodeMP.LOGGER.debug("Received text change {} from offset {} to {}!\n",
event.getContent(), event.getStart(), event.getEnd());
CodeMP.LOGGER.info("is writable: {}", document.isWritable());
document.replaceString( //TODO this doesn't work
(int) event.getStart(),
(int) event.getEnd(),
event.getContent()
);
}));
CommandProcessor.getInstance().executeCommand(
project,
() -> document.replaceString(
(int) event.getStart(), (int) event.getEnd(), event.getContent()),
"CodeMPBufferReceive",
"codemp-buffer-receive", //TODO: mark this with the name
document);
});
});
} catch(Exception ex) {
throw new RuntimeException(ex);
}

View file

@ -2,6 +2,7 @@ package com.codemp.intellij.listeners;
import com.codemp.intellij.jni.BufferHandler;
import com.codemp.intellij.jni.CodeMPHandler;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.editor.event.DocumentEvent;
import com.intellij.openapi.editor.event.DocumentListener;
import org.jetbrains.annotations.NotNull;
@ -16,11 +17,16 @@ public class BufferEventListener implements DocumentListener {
@Override
public void documentChanged(@NotNull DocumentEvent event) {
try {
Object group = CommandProcessor.getInstance().getCurrentCommandGroupId();
if(group instanceof String groupString && groupString.startsWith("codemp-buffer-receive"))
return;
try { //TODO move actions break
int changeOffset = event.getOffset();
CharSequence newFragment = event.getNewFragment();
BufferHandler bufferHandler = CodeMPHandler.getBuffer(this.bufferName);
bufferHandler.send(changeOffset, changeOffset + event.getOldFragment().length(),
bufferHandler.send(changeOffset,
changeOffset + event.getOldFragment().length(),
newFragment.toString());
} catch(Exception e) {
throw new RuntimeException(e);

View file

@ -8,11 +8,8 @@ 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))
);
@SuppressWarnings("all")
Color hashColor = new Color(hash | (0xFF << 24));
return new JBColor(hashColor, hashColor.darker());
}

View file

@ -184,8 +184,8 @@ impl TextChangeWrapper {
}
#[generate_interface]
fn get_content(&self) -> &str {
&self.content
fn get_content(&self) -> String {
self.content.clone()
}
}
@ -205,20 +205,20 @@ impl BufferHandler {
fn recv(&self) -> Result<TextChangeWrapper, String> {
match self.buffer.blocking_recv(CODEMP_INSTANCE.rt()) {
Err(err) => Err(ErrorWrapper::from(err).get_error_message()),
Ok(change) => Ok(TextChangeWrapper {
start: change.span.start,
end: change.span.end,
content: change.content.clone()
})
Ok(change) => {
println!("test {:?}", change);
Ok(TextChangeWrapper {
start: change.span.start,
end: change.span.end,
content: change.content.clone()
})
}
}
}
#[generate_interface]
fn send(&self, start_offset: usize, end_offset: usize, content: String) -> Result<(), String> {
match self.buffer.delta(start_offset, &content, end_offset) {
None => Err("Cannot send a no-op".to_string()),
Some(op_seq) => self.buffer.send(op_seq)
.map_err(|err| ErrorWrapper::from(err).get_error_message())
}
self.buffer.send(CodempTextChange { span: start_offset..end_offset, content, after: "".to_string() })
.map_err(|err| ErrorWrapper::from(err).get_error_message())
}
}