From e732e6a9386ff58eb868d191849047da10b4d1c1 Mon Sep 17 00:00:00 2001 From: cschen Date: Thu, 15 Aug 2024 17:58:02 +0200 Subject: [PATCH] feat(python): adapted glue to latest changes, still todo workspace events. --- src/api/event.rs | 3 +- src/ffi/python/controllers.rs | 77 ++++++++++++++++++----------------- src/ffi/python/workspace.rs | 17 +++++++- 3 files changed, 56 insertions(+), 41 deletions(-) diff --git a/src/api/event.rs b/src/api/event.rs index 5102a7b..31cfad0 100644 --- a/src/api/event.rs +++ b/src/api/event.rs @@ -13,8 +13,7 @@ impl From<&WorkspaceEventInner> for Event { WorkspaceEventInner::Leave(e) => Self::UserLeave(e.user.id.clone()), WorkspaceEventInner::Create(_) | WorkspaceEventInner::Rename(_) - | WorkspaceEventInner::Delete(_) - => Self::FileTreeUpdated, + | WorkspaceEventInner::Delete(_) => Self::FileTreeUpdated, } } } diff --git a/src/ffi/python/controllers.rs b/src/ffi/python/controllers.rs index e7b8537..68d5e1e 100644 --- a/src/ffi/python/controllers.rs +++ b/src/ffi/python/controllers.rs @@ -4,37 +4,44 @@ use crate::api::TextChange; use crate::buffer::Controller as BufferController; use crate::cursor::Controller as CursorController; use pyo3::prelude::*; -use pyo3::types::PyType; +use pyo3_asyncio::tokio::future_into_py; // use super::CodempController; #[pymethods] impl CursorController { #[pyo3(name = "send")] - pub fn pysend(&self, path: String, start: (i32, i32), end: (i32, i32)) -> PyResult<()> { + pub fn pysend<'p>( + &self, + py: Python<'p>, + path: String, + start: (i32, i32), + end: (i32, i32), + ) -> PyResult<&'p PyAny> { + let rc = self.clone(); let pos = Cursor { start, end, buffer: path, user: None, }; - - Ok(self.send(pos)?) + let rc = self.clone(); + future_into_py(py, async move { Ok(rc.send(pos).await?) }) } #[pyo3(name = "try_recv")] - pub fn pytry_recv(&self, py: Python<'_>) -> PyResult>> { - match self.try_recv()? { - Some(cur_event) => Ok(Some(Py::new(py, cur_event)?)), - None => Ok(None), - } + pub fn pytry_recv<'p>(&self, py: Python<'p>) -> PyResult<&'p PyAny> { + //PyResult>> + let rc = self.clone(); + + future_into_py(py, async move { Ok(rc.try_recv().await?) }) } #[pyo3(name = "recv")] pub fn pyrecv<'p>(&'p self, py: Python<'p>) -> PyResult<&'p PyAny> { let rc = self.clone(); - pyo3_asyncio::tokio::future_into_py(py, async move { + future_into_py(py, async move { let cur_event: Cursor = rc.recv().await?; Python::with_gil(|py| Py::new(py, cur_event)) }) @@ -44,7 +51,7 @@ impl CursorController { pub fn pypoll<'p>(&'p self, py: Python<'p>) -> PyResult<&'p PyAny> { let rc = self.clone(); - pyo3_asyncio::tokio::future_into_py(py, async move { Ok(rc.poll().await?) }) + future_into_py(py, async move { Ok(rc.poll().await?) }) } #[pyo3(name = "stop")] @@ -82,36 +89,42 @@ impl Cursor { #[pymethods] impl BufferController { #[pyo3(name = "content")] - fn pycontent(&self) -> String { - self.content().clone() + fn pycontent<'p>(&self, py: Python<'p>) -> PyResult<&'p PyAny> { + let rc = self.clone(); + future_into_py(py, async move { Ok(rc.content().await?) }) } #[pyo3(name = "send")] - fn pysend(&self, start: u32, end: u32, txt: String) -> PyResult<()> { + fn pysend<'p>(&self, py: Python<'p>, start: u32, end: u32, txt: String) -> PyResult<&'p PyAny> { let op = TextChange { start, end, content: txt, + hash: None, }; - Ok(self.send(op)?) + let rc = self.clone(); + future_into_py(py, async move { Ok(rc.send(op).await?) }) } #[pyo3(name = "try_recv")] - fn pytry_recv(&self, py: Python<'_>) -> PyResult { - match self.try_recv()? { - Some(txt_change) => { - let evt = txt_change; - Ok(evt.into_py(py)) - } - None => Ok(py.None()), - } + fn pytry_recv<'p>(&self, py: Python<'p>) -> PyResult<&'p PyAny> { + // match self.try_recv()? { + // Some(txt_change) => { + // let evt = txt_change; + // Ok(evt.into_py(py)) + // } + // None => Ok(py.None()), + // } + let rc = self.clone(); + + future_into_py(py, async move { Ok(rc.try_recv().await?) }) } #[pyo3(name = "recv")] fn pyrecv<'p>(&'p self, py: Python<'p>) -> PyResult<&'p PyAny> { let rc = self.clone(); - pyo3_asyncio::tokio::future_into_py(py, async move { + future_into_py(py, async move { let txt_change: TextChange = rc.recv().await?; Python::with_gil(|py| Py::new(py, txt_change)) }) @@ -129,12 +142,12 @@ impl BufferController { impl TextChange { #[pyo3(name = "is_deletion")] fn pyis_deletion(&self) -> bool { - self.is_deletion() + self.is_delete() } #[pyo3(name = "is_addition")] fn pyis_addition(&self) -> bool { - self.is_addition() + self.is_insert() } #[pyo3(name = "is_empty")] @@ -146,16 +159,4 @@ impl TextChange { fn pyapply(&self, txt: &str) -> String { self.apply(txt) } - - #[classmethod] - #[pyo3(name = "from_diff")] - fn pyfrom_diff(_cls: &PyType, before: &str, after: &str) -> TextChange { - TextChange::from_diff(before, after) - } - - #[classmethod] - #[pyo3(name = "index_to_rowcol")] - fn pyindex_to_rowcol(_cls: &PyType, txt: &str, index: usize) -> (i32, i32) { - TextChange::index_to_rowcol(txt, index).into() - } } diff --git a/src/ffi/python/workspace.rs b/src/ffi/python/workspace.rs index d583835..425301d 100644 --- a/src/ffi/python/workspace.rs +++ b/src/ffi/python/workspace.rs @@ -3,6 +3,7 @@ use crate::cursor::Controller as CursorController; use crate::workspace::Workspace; use pyo3::prelude::*; use pyo3::types::PyString; +use pyo3_asyncio::generic::future_into_py; #[pymethods] impl Workspace { @@ -22,7 +23,8 @@ impl Workspace { pyo3_asyncio::tokio::future_into_py(py, async move { let buffctl: BufferController = ws.attach(path.as_str()).await?; - Python::with_gil(|py| Py::new(py, buffctl)) + Ok(buffctl) + // Python::with_gil(|py| Py::new(py, buffctl)) }) } @@ -35,6 +37,12 @@ impl Workspace { } } + // #[pyo3(name = "event")] + // fn pyevent(&self, py: Python<'_>, path: String) -> PyResult<&PyAny> { + // let rc = self.clone(); + // future_into_py(py, async move { Ok(rc.event().await?) }) + // } + #[pyo3(name = "fetch_buffers")] fn pyfetch_buffers<'p>(&'p self, py: Python<'p>) -> PyResult<&PyAny> { let ws = self.clone(); @@ -114,3 +122,10 @@ impl Workspace { self.filetree() } } + +// #[pyclass] +// enum PyEvent { +// FileTreeUpdated, +// UserJoin { name: String }, +// UserLeave { name: String }, +// }