Skip to content

Commit

Permalink
fix: missing ack method in python glue. Fixed python constructors for…
Browse files Browse the repository at this point in the history
… TextChange

and Selection. fixed code hints.
  • Loading branch information
ghyatzo committed Oct 26, 2024
1 parent 6a8fb38 commit 3956d4a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 58 deletions.
19 changes: 15 additions & 4 deletions dist/py/src/codemp/codemp.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...
Expand All @@ -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: ...
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/ffi/python/controllers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ impl BufferController {
a_sync_allow_threads!(py, this.content().await)
}

#[pyo3(name = "ack")]
fn pyack(&self, py: Python, v: Vec<u64>) -> 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();
Expand Down
82 changes: 28 additions & 54 deletions src/ffi/python/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,39 +227,16 @@ impl Cursor {
#[pymethods]
impl Selection {
#[new]
#[pyo3(signature = (**kwds))]
pub fn py_new(kwds: Option<&Bound<'_, PyDict>>) -> PyResult<Self> {
#[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<Self> {
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,
Expand All @@ -268,7 +245,13 @@ impl Selection {
buffer,
})
} else {
Ok(Self::default())
Ok(Self {
start_row,
start_col,
end_row,
end_col,
buffer,
})
}
}

Expand All @@ -287,34 +270,25 @@ impl BufferUpdate {
#[pymethods]
impl TextChange {
#[new]
#[pyo3(signature = (**kwds))]
pub fn py_new(kwds: Option<&Bound<'_, PyDict>>) -> PyResult<Self> {
#[pyo3(signature = (*, start, end, content, **kwds))]
pub fn py_new(
start: u32,
end: u32,
content: String,
kwds: Option<&Bound<'_, PyDict>>,
) -> PyResult<Self> {
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,
})
}
}

Expand Down

0 comments on commit 3956d4a

Please sign in to comment.