feat(python): adapted glue to latest changes, still todo workspace events.

This commit is contained in:
cschen 2024-08-15 17:58:02 +02:00
parent b51e55570f
commit e732e6a938
3 changed files with 56 additions and 41 deletions

View file

@ -13,8 +13,7 @@ impl From<&WorkspaceEventInner> for Event {
WorkspaceEventInner::Leave(e) => Self::UserLeave(e.user.id.clone()), WorkspaceEventInner::Leave(e) => Self::UserLeave(e.user.id.clone()),
WorkspaceEventInner::Create(_) WorkspaceEventInner::Create(_)
| WorkspaceEventInner::Rename(_) | WorkspaceEventInner::Rename(_)
| WorkspaceEventInner::Delete(_) | WorkspaceEventInner::Delete(_) => Self::FileTreeUpdated,
=> Self::FileTreeUpdated,
} }
} }
} }

View file

@ -4,37 +4,44 @@ use crate::api::TextChange;
use crate::buffer::Controller as BufferController; use crate::buffer::Controller as BufferController;
use crate::cursor::Controller as CursorController; use crate::cursor::Controller as CursorController;
use pyo3::prelude::*; use pyo3::prelude::*;
use pyo3::types::PyType; use pyo3_asyncio::tokio::future_into_py;
// use super::CodempController; // use super::CodempController;
#[pymethods] #[pymethods]
impl CursorController { impl CursorController {
#[pyo3(name = "send")] #[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 { let pos = Cursor {
start, start,
end, end,
buffer: path, buffer: path,
user: None, user: None,
}; };
let rc = self.clone();
Ok(self.send(pos)?) future_into_py(py, async move { Ok(rc.send(pos).await?) })
} }
#[pyo3(name = "try_recv")] #[pyo3(name = "try_recv")]
pub fn pytry_recv(&self, py: Python<'_>) -> PyResult<Option<Py<Cursor>>> { pub fn pytry_recv<'p>(&self, py: Python<'p>) -> PyResult<&'p PyAny> {
match self.try_recv()? { //PyResult<Option<Py<Cursor>>>
Some(cur_event) => Ok(Some(Py::new(py, cur_event)?)), let rc = self.clone();
None => Ok(None),
} future_into_py(py, async move { Ok(rc.try_recv().await?) })
} }
#[pyo3(name = "recv")] #[pyo3(name = "recv")]
pub fn pyrecv<'p>(&'p self, py: Python<'p>) -> PyResult<&'p PyAny> { pub fn pyrecv<'p>(&'p self, py: Python<'p>) -> PyResult<&'p PyAny> {
let rc = self.clone(); 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?; let cur_event: Cursor = rc.recv().await?;
Python::with_gil(|py| Py::new(py, cur_event)) 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> { pub fn pypoll<'p>(&'p self, py: Python<'p>) -> PyResult<&'p PyAny> {
let rc = self.clone(); 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")] #[pyo3(name = "stop")]
@ -82,36 +89,42 @@ impl Cursor {
#[pymethods] #[pymethods]
impl BufferController { impl BufferController {
#[pyo3(name = "content")] #[pyo3(name = "content")]
fn pycontent(&self) -> String { fn pycontent<'p>(&self, py: Python<'p>) -> PyResult<&'p PyAny> {
self.content().clone() let rc = self.clone();
future_into_py(py, async move { Ok(rc.content().await?) })
} }
#[pyo3(name = "send")] #[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 { let op = TextChange {
start, start,
end, end,
content: txt, 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")] #[pyo3(name = "try_recv")]
fn pytry_recv(&self, py: Python<'_>) -> PyResult<PyObject> { fn pytry_recv<'p>(&self, py: Python<'p>) -> PyResult<&'p PyAny> {
match self.try_recv()? { // match self.try_recv()? {
Some(txt_change) => { // Some(txt_change) => {
let evt = txt_change; // let evt = txt_change;
Ok(evt.into_py(py)) // Ok(evt.into_py(py))
} // }
None => Ok(py.None()), // None => Ok(py.None()),
} // }
let rc = self.clone();
future_into_py(py, async move { Ok(rc.try_recv().await?) })
} }
#[pyo3(name = "recv")] #[pyo3(name = "recv")]
fn pyrecv<'p>(&'p self, py: Python<'p>) -> PyResult<&'p PyAny> { fn pyrecv<'p>(&'p self, py: Python<'p>) -> PyResult<&'p PyAny> {
let rc = self.clone(); 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?; let txt_change: TextChange = rc.recv().await?;
Python::with_gil(|py| Py::new(py, txt_change)) Python::with_gil(|py| Py::new(py, txt_change))
}) })
@ -129,12 +142,12 @@ impl BufferController {
impl TextChange { impl TextChange {
#[pyo3(name = "is_deletion")] #[pyo3(name = "is_deletion")]
fn pyis_deletion(&self) -> bool { fn pyis_deletion(&self) -> bool {
self.is_deletion() self.is_delete()
} }
#[pyo3(name = "is_addition")] #[pyo3(name = "is_addition")]
fn pyis_addition(&self) -> bool { fn pyis_addition(&self) -> bool {
self.is_addition() self.is_insert()
} }
#[pyo3(name = "is_empty")] #[pyo3(name = "is_empty")]
@ -146,16 +159,4 @@ impl TextChange {
fn pyapply(&self, txt: &str) -> String { fn pyapply(&self, txt: &str) -> String {
self.apply(txt) 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()
}
} }

View file

@ -3,6 +3,7 @@ use crate::cursor::Controller as CursorController;
use crate::workspace::Workspace; use crate::workspace::Workspace;
use pyo3::prelude::*; use pyo3::prelude::*;
use pyo3::types::PyString; use pyo3::types::PyString;
use pyo3_asyncio::generic::future_into_py;
#[pymethods] #[pymethods]
impl Workspace { impl Workspace {
@ -22,7 +23,8 @@ impl Workspace {
pyo3_asyncio::tokio::future_into_py(py, async move { pyo3_asyncio::tokio::future_into_py(py, async move {
let buffctl: BufferController = ws.attach(path.as_str()).await?; 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")] #[pyo3(name = "fetch_buffers")]
fn pyfetch_buffers<'p>(&'p self, py: Python<'p>) -> PyResult<&PyAny> { fn pyfetch_buffers<'p>(&'p self, py: Python<'p>) -> PyResult<&PyAny> {
let ws = self.clone(); let ws = self.clone();
@ -114,3 +122,10 @@ impl Workspace {
self.filetree() self.filetree()
} }
} }
// #[pyclass]
// enum PyEvent {
// FileTreeUpdated,
// UserJoin { name: String },
// UserLeave { name: String },
// }