feat: updated how the textchange is handled python side. No longer splatted, added access to type methods.

Former-commit-id: 5eb0f10d187d5e9c97781c46cd8d24c919b52e04
This commit is contained in:
Camillo Schenone 2023-12-02 22:16:41 +01:00
parent 4152ad41d8
commit 5741a91ce2
4 changed files with 62 additions and 14 deletions

View file

@ -9,7 +9,7 @@ name = "codemp_client"
crate-type = ["cdylib"]
[dependencies]
codemp = { git = "ssh://git@github.com/codewithotherpeopleandchangenamelater/codemp.git", tag="v0.5.0"}
codemp = { git = "ssh://git@github.com/codewithotherpeopleandchangenamelater/codemp.git", tag="v0.5.1"}
pyo3 = { version = "0.20", features = ["extension-module"] }
pyo3-asyncio = { version = "0.20", features = ["tokio-runtime"] }
tokio = "1.29.1"

View file

@ -1 +1 @@
55ed2ebf31cde48dafa1993cd5227bbe01669172
ef547495939b20dc50fcf0505d8ebd77dc3721f1

View file

@ -3,6 +3,11 @@ import sublime_plugin
# import Codemp.codemp_client as codemp
from Codemp.src.codemp_client import *
# we import the PyTextChange type to be able to access its @classmethods: from_diff and index_to_rowcol
# PyTextChange instances are not meant to be created from python, but only received immutable from codemp.
from Codemp.bindings.codemp_client import PyTextChange
import Codemp.ext.sublime_asyncio as sublime_asyncio
import asyncio
import os
@ -272,6 +277,10 @@ class CodempSublimeBuffer():
while text_change := await self.controller.recv():
# In case a change arrives to a background buffer, just apply it. We are not listening on it.
# Otherwise, interrupt the listening to avoid echoing back the change just received.
if text_change.is_empty():
status_log("change is empty. skipping.")
continue
active = is_active(self.view)
if active:
safe_listener_detach(_txt_change_listener)

View file

@ -6,7 +6,7 @@ use codemp::errors::Error as CodempError;
use pyo3::{
prelude::*,
exceptions::{PyConnectionError, PyRuntimeError, PyBaseException},
types::PyString,
types::{PyString, PyType},
};
struct PyCodempError(CodempError);
@ -324,21 +324,60 @@ impl From<CodempCursorEvent> for PyCursorEvent {
}
}
// TODO: change the python text change to hold a wrapper to the original text change, with helper getter
// and setters for unpacking the span, instead of a flattened version of text change.
#[pyclass]
struct PyTextChange {
#[pyo3(get, set)]
start_incl: usize,
#[pyo3(get, set)]
end_excl: usize,
#[pyo3(get, set)]
content: String
}
struct PyTextChange(CodempTextChange);
impl From<CodempTextChange> for PyTextChange {
fn from(value: CodempTextChange) -> Self {
PyTextChange { start_incl: value.span.start, end_excl: value.span.end, content: value.content }
PyTextChange(value)
}
}
#[pymethods]
impl PyTextChange {
#[getter]
fn start_incl(&self) -> PyResult<usize> {
Ok(self.0.span.start)
}
#[getter]
fn end_excl(&self) -> PyResult<usize> {
Ok(self.0.span.end)
}
#[getter]
fn content(&self) -> PyResult<String> {
Ok(self.0.content.clone())
}
fn is_deletion(&self) -> bool {
self.0.is_deletion()
}
fn is_addition(&self) -> bool {
self.0.is_addition()
}
fn is_empty(&self) -> bool {
self.0.is_empty()
}
fn apply(&self, txt: &str) -> String {
self.0.apply(txt)
}
#[classmethod]
fn from_diff(_cls: &PyType, before: &str, after: &str) -> PyTextChange {
PyTextChange(CodempTextChange::from_diff(before, after))
}
#[classmethod]
fn index_to_rowcol(_cls: &PyType, txt: &str, index: usize) -> (i32, i32) {
CodempTextChange::index_to_rowcol(txt, index).into()
}
}