From 3956d4a27d559046a55f9446b2a862cdb0c402b9 Mon Sep 17 00:00:00 2001 From: cschen Date: Sat, 26 Oct 2024 19:05:42 +0200 Subject: [PATCH] fix: missing ack method in python glue. Fixed python constructors for TextChange and Selection. fixed code hints. --- dist/py/src/codemp/codemp.pyi | 19 ++++++-- src/ffi/python/controllers.rs | 6 +++ src/ffi/python/mod.rs | 82 ++++++++++++----------------------- 3 files changed, 49 insertions(+), 58 deletions(-) diff --git a/dist/py/src/codemp/codemp.pyi b/dist/py/src/codemp/codemp.pyi index 73bfa0b..94271ff 100644 --- a/dist/py/src/codemp/codemp.pyi +++ b/dist/py/src/codemp/codemp.pyi @@ -95,6 +95,8 @@ class TextChange: end: int content: str + def __new__(cls, *, start: int, end: int, content: str, **kwargs): ... + def is_delete(self) -> bool: ... def is_insert(self) -> bool: ... def is_empty(self) -> bool: ... @@ -118,8 +120,8 @@ class BufferController: def content(self) -> Promise[str]: ... def ack(self, v: list[int]) -> None: ... def send(self, op: TextChange) -> None: ... - def try_recv(self) -> Promise[Optional[TextChange]]: ... - def recv(self) -> Promise[TextChange]: ... + def try_recv(self) -> Promise[Optional[BufferUpdate]]: ... + def recv(self) -> Promise[BufferUpdate]: ... def poll(self) -> Promise[None]: ... def callback(self, cb: Callable[[BufferController], None]) -> None: ... @@ -131,10 +133,19 @@ class Selection: """ An Editor agnostic cursor position representation """ - start: Tuple[int, int] - end: Tuple[int, int] + start_row: int + start_col: int + end_row: int + end_col: int buffer: str + def __new__(cls, * + start_row: int, + start_col: int, + end_row: int, + end_col: int, + buffer: str, **kwargs): ... + class Cursor: """ A remote cursor event diff --git a/src/ffi/python/controllers.rs b/src/ffi/python/controllers.rs index 01b2cc5..50718af 100644 --- a/src/ffi/python/controllers.rs +++ b/src/ffi/python/controllers.rs @@ -71,6 +71,12 @@ impl BufferController { a_sync_allow_threads!(py, this.content().await) } + #[pyo3(name = "ack")] + fn pyack(&self, py: Python, v: Vec) -> PyResult<()> { + let this = self.clone(); + a_sync_allow_threads!(py, this.ack(v)) + } + #[pyo3(name = "send")] fn pysend(&self, _py: Python, op: TextChange) -> PyResult<()> { let this = self.clone(); diff --git a/src/ffi/python/mod.rs b/src/ffi/python/mod.rs index a0f7b8e..f9f8963 100644 --- a/src/ffi/python/mod.rs +++ b/src/ffi/python/mod.rs @@ -227,39 +227,16 @@ impl Cursor { #[pymethods] impl Selection { #[new] - #[pyo3(signature = (**kwds))] - pub fn py_new(kwds: Option<&Bound<'_, PyDict>>) -> PyResult { + #[pyo3(signature = (*, start_row, start_col, end_row, end_col, buffer, **kwds))] + pub fn py_new( + start_row: i32, + start_col: i32, + end_row: i32, + end_col: i32, + buffer: String, + kwds: Option<&Bound<'_, PyDict>>, + ) -> PyResult { if let Some(kwds) = kwds { - let start_row = if let Some(e) = kwds.get_item("start_row")? { - e.extract()? - } else { - 0 - }; - - let start_col = if let Some(e) = kwds.get_item("start_col")? { - e.extract()? - } else { - 0 - }; - - let end_row = if let Some(e) = kwds.get_item("end_row")? { - e.extract()? - } else { - 0 - }; - - let end_col = if let Some(e) = kwds.get_item("end_col")? { - e.extract()? - } else { - 0 - }; - - let buffer = if let Some(e) = kwds.get_item("buffer")? { - e.extract()? - } else { - String::default() - }; - Ok(Self { start_row, start_col, @@ -268,7 +245,13 @@ impl Selection { buffer, }) } else { - Ok(Self::default()) + Ok(Self { + start_row, + start_col, + end_row, + end_col, + buffer, + }) } } @@ -287,34 +270,25 @@ impl BufferUpdate { #[pymethods] impl TextChange { #[new] - #[pyo3(signature = (**kwds))] - pub fn py_new(kwds: Option<&Bound<'_, PyDict>>) -> PyResult { + #[pyo3(signature = (*, start, end, content, **kwds))] + pub fn py_new( + start: u32, + end: u32, + content: String, + kwds: Option<&Bound<'_, PyDict>>, + ) -> PyResult { if let Some(kwds) = kwds { - let start_idx = if let Some(e) = kwds.get_item("start")? { - e.extract()? - } else { - 0 - }; - - let end_idx = if let Some(e) = kwds.get_item("end")? { - e.extract()? - } else { - 0 - }; - - let content = if let Some(e) = kwds.get_item("content")? { - e.extract()? - } else { - String::default() - }; - Ok(Self { start_idx, end_idx, content, }) } else { - Ok(Self::default()) + Ok(Self { + start_idx, + end_idx, + content, + }) } }