feat: send empty cursor when leaving an active buffer

This commit is contained in:
zaaarf 2024-10-09 23:40:22 +02:00
parent 044b1f4470
commit 7507544fcb
No known key found for this signature in database
GPG key ID: C91CFF9E2262BBA1

View file

@ -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,16 +17,15 @@ 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;
Editor editor = event.getEditor();
VirtualFile file = editor.getVirtualFile();
if(file == null) return;
@ -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();
}
}