From 7507544fcb566bb95f1384f3ebab9e2a8724a6d8 Mon Sep 17 00:00:00 2001 From: zaaarf Date: Wed, 9 Oct 2024 23:40:22 +0200 Subject: [PATCH] feat: send empty cursor when leaving an active buffer --- .../listeners/CursorEventListener.java | 36 +++++++++++++++---- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/src/main/java/mp/code/intellij/listeners/CursorEventListener.java b/src/main/java/mp/code/intellij/listeners/CursorEventListener.java index abb19d8..330b2bc 100644 --- a/src/main/java/mp/code/intellij/listeners/CursorEventListener.java +++ b/src/main/java/mp/code/intellij/listeners/CursorEventListener.java @@ -3,7 +3,6 @@ package mp.code.intellij.listeners; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.editor.LogicalPosition; import com.intellij.openapi.vfs.VirtualFile; -import lombok.SneakyThrows; import mp.code.exceptions.ControllerException; import mp.code.intellij.CodeMP; import mp.code.intellij.util.FileUtil; @@ -18,15 +17,14 @@ import org.jetbrains.annotations.NotNull; import java.util.Optional; public class CursorEventListener implements CaretListener { + private boolean once = false; @Override - @SneakyThrows public void caretPositionChanged(@NotNull CaretEvent event) { - // TODO instead of returning, should un-set remote cursor position (once) + // TODO instead of returning, should un-set remote ursor position (once) Caret caret = event.getCaret(); - if (caret == null) return; - + if(caret == null) return; Editor editor = event.getEditor(); VirtualFile file = editor.getVirtualFile(); @@ -37,13 +35,26 @@ public class CursorEventListener implements CaretListener { Optional.ofNullable(CodeMP.BUFFER_MAPPER.get(file.toNioPath())) .flatMap(n -> CodeMP.getActiveWorkspace().getBuffer(n)) .isEmpty() - ) return; + ) { + if(!this.once) { + this.sendEmptyCursor(); + this.once = true; + } + + return; + } } catch(UnsupportedOperationException ex) { // probably won't be like this long term, but for now we work with real physical files // so converting to nio path is always legal when it's the right file + if(!this.once) { + this.sendEmptyCursor(); + this.once = true; + } return; } + this.once = false; + LogicalPosition startPos = editor.offsetToLogicalPosition(caret.getSelectionStart()); LogicalPosition endPos = editor.offsetToLogicalPosition(caret.getSelectionEnd()); @@ -70,4 +81,17 @@ public class CursorEventListener implements CaretListener { } })).start(); } + + // temp fix + private void sendEmptyCursor() { + new Thread(() -> ApplicationManager.getApplication().runReadAction(() -> { + try { + CodeMP.getActiveWorkspace() + .getCursor() + .send(new Cursor(0, 0, 0, 0, "", null)); + } catch(ControllerException e) { + throw new RuntimeException(e); + } + })).start(); + } } \ No newline at end of file