diff --git a/Cargo.toml b/Cargo.toml index 12d9773..1651462 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,12 +4,11 @@ version = "0.1.0" edition = "2021" [dependencies] -codemp = { git = "ssh://git@github.com/codewithotherpeopleandchangenamelater/codemp.git", tag = "v0.5.0", features = ["global", "sync"] } +codemp = { git = "ssh://git@github.com/codewithotherpeopleandchangenamelater/codemp.git", tag = "v0.5.1", features = ["global", "sync"] } jni = { version = "0.21.1", features = ["invocation"] } jni-sys = "0.3.0" log = "0.4.20" rifgen = { git = "https://github.com/Kofituo/rifgen.git", rev = "d27d9785b2febcf5527f1deb6a846be5d583f7d7"} -tokio = "1.34.0" [build-dependencies] flapigen = "0.6.0" diff --git a/src/main/java/com/codemp/intellij/task/BufferEventAwaiterTask.java b/src/main/java/com/codemp/intellij/task/BufferEventAwaiterTask.java index d0eb127..3ad4384 100644 --- a/src/main/java/com/codemp/intellij/task/BufferEventAwaiterTask.java +++ b/src/main/java/com/codemp/intellij/task/BufferEventAwaiterTask.java @@ -59,18 +59,16 @@ public class BufferEventAwaiterTask extends Task.Backgroundable implements Dispo StringVec buffers = new StringVec(); //jni moment CodeMP.ACTIVE_BUFFERS.keySet().forEach(buffers::push); - Optional bufferOptional = CodeMPHandler.selectBuffer(buffers, 100L); + Optional bufferOptional = CodeMPHandler.selectBuffer(buffers, 100L); if(bufferOptional.isEmpty()) continue; - String buffer = bufferOptional.get(); - - BufferHandler handler = CodeMPHandler.getBuffer(buffer); + BufferHandler buffer = bufferOptional.get(); List changeList = new ArrayList<>(); while(true) { Optional changeOptional; try { - changeOptional = handler.tryRecv(); + changeOptional = buffer.tryRecv(); } catch(DeadlockedException e) { CodeMP.LOGGER.error(e.getMessage()); continue; @@ -83,7 +81,7 @@ public class BufferEventAwaiterTask extends Task.Backgroundable implements Dispo changeList.add(change); } - Editor bufferEditor = CodeMP.ACTIVE_BUFFERS.get(buffer); + Editor bufferEditor = CodeMP.ACTIVE_BUFFERS.get(buffer.getName()); ApplicationManager.getApplication().invokeLaterOnWriteThread(() -> ApplicationManager.getApplication().runWriteAction(() -> CommandProcessor.getInstance().executeCommand( diff --git a/src/main/rust/lib.rs b/src/main/rust/lib.rs index bfa3b46..4a8b048 100644 --- a/src/main/rust/lib.rs +++ b/src/main/rust/lib.rs @@ -1,6 +1,7 @@ use std::sync::Arc; use std::time::Duration; use codemp::prelude::*; +use codemp::tools; use rifgen::rifgen_attr::{generate_access_methods, generate_interface, generate_interface_doc}; pub mod glue { //rifgen generated code @@ -67,7 +68,7 @@ impl CodeMPHandler { } #[generate_interface] - fn select_buffer(mut buffer_ids: StringVec, timeout: i64) -> CodempResult> { + fn select_buffer(mut buffer_ids: StringVec, timeout: i64) -> CodempResult> { let mut buffers = Vec::new(); for id in buffer_ids.v.iter_mut() { match CODEMP_INSTANCE.get_buffer(id.as_str()) { @@ -76,35 +77,15 @@ impl CodeMPHandler { } } - let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel(); - let mut tasks = Vec::new(); - for buffer in buffers { - let _tx = tx.clone(); - let _buffer = buffer.clone(); - tasks.push(CODEMP_INSTANCE.rt().spawn(async move { - match _buffer.poll().await { - Ok(()) => _tx.send(Ok(Some(_buffer.name.clone()))), - Err(_) => _tx.send(Err(CodempError::Channel { send: true })), - } - })) - } - let _tx = tx.clone(); - tasks.push(CODEMP_INSTANCE.rt().spawn(async move { - tokio::time::sleep(Duration::from_millis(timeout as u64)).await; - _tx.send(Ok(None)) - })); - loop { - match CODEMP_INSTANCE.rt().block_on(rx.recv()) { - None => return Err(CodempError::Channel { send: false }), - Some(Err(_)) => continue, - Some(Ok(None)) => return Ok(None), - Some(Ok(Some(x))) => { - for t in tasks { - t.abort(); - } - return Ok(Some(x.clone())); - }, - } + let result = CODEMP_INSTANCE.rt().block_on(tools::select_buffer( + buffers.as_slice(), + Some(Duration::from_millis(timeout as u64)) + )); + + match result { + Err(e) => Err(e), + Ok(buffer) => + Ok(buffer.map(|buffer| BufferHandler { buffer })) } } } @@ -180,7 +161,7 @@ impl BufferHandler { #[generate_interface] fn get_name(&self) -> String { - self.buffer.name.clone() + self.buffer.name().to_string() } #[generate_interface]