mirror of
https://github.com/hexedtech/codemp.git
synced 2024-12-23 13:24:54 +01:00
fix: docs, tests, leftovers
This commit is contained in:
parent
cf80104132
commit
ed151e2213
4 changed files with 45 additions and 43 deletions
|
@ -5,7 +5,6 @@
|
|||
|
||||
use crate::Result;
|
||||
use std::sync::Arc;
|
||||
use tokio::runtime::Runtime;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
pub(crate) trait ControllerWorker<T : Sized + Send + Sync> {
|
||||
|
@ -60,7 +59,7 @@ pub trait Controller<T : Sized + Send + Sync> : Sized + Send + Sync {
|
|||
fn try_recv(&self) -> Result<Option<T>>;
|
||||
|
||||
/// sync variant of [Self::recv], blocking invoking thread
|
||||
fn blocking_recv(&self, rt: &Runtime) -> Result<T> {
|
||||
fn blocking_recv(&self, rt: &tokio::runtime::Handle) -> Result<T> {
|
||||
rt.block_on(self.recv())
|
||||
}
|
||||
|
||||
|
|
|
@ -40,64 +40,67 @@ pub fn op_effective_range(op: &OperationSeq) -> Range<u64> {
|
|||
/// ### examples
|
||||
///
|
||||
/// ```rust
|
||||
/// use codemp::buffer::factory::{OperationFactory, SimpleOperationFactory};
|
||||
/// use codemp::api::OperationFactory;
|
||||
///
|
||||
/// let mut factory = SimpleOperationFactory::from("");
|
||||
/// let op = factory.insert("asd", 0);
|
||||
/// factory.update(op)?;
|
||||
/// assert_eq!(factory.content(), "asd");
|
||||
/// let mut factory = String::new();
|
||||
/// let operation = factory.ins("asd", 0);
|
||||
/// factory = operation.apply(&factory)?;
|
||||
/// assert_eq!(factory, "asd");
|
||||
/// # Ok::<(), codemp::ot::OTError>(())
|
||||
/// ```
|
||||
///
|
||||
/// use [OperationFactory::insert] to add new characters at a specific index
|
||||
/// use [OperationFactory::ins] to add new characters at a specific index
|
||||
///
|
||||
/// ```rust
|
||||
/// # use codemp::buffer::factory::{OperationFactory, SimpleOperationFactory};
|
||||
/// # let mut factory = SimpleOperationFactory::from("asd");
|
||||
/// factory.update(factory.insert(" dsa", 3))?;
|
||||
/// assert_eq!(factory.content(), "asd dsa");
|
||||
/// # use codemp::api::OperationFactory;
|
||||
/// # let mut factory = String::from("asd");
|
||||
/// factory = factory.ins(" dsa", 3).apply(&factory)?;
|
||||
/// assert_eq!(factory, "asd dsa");
|
||||
/// # Ok::<(), codemp::ot::OTError>(())
|
||||
/// ```
|
||||
///
|
||||
/// use [OperationFactory::delta] to arbitrarily change text at any position
|
||||
/// use [OperationFactory::diff] to arbitrarily change text at any position
|
||||
///
|
||||
/// ```rust
|
||||
/// # use codemp::buffer::factory::{OperationFactory, SimpleOperationFactory};
|
||||
/// # let mut factory = SimpleOperationFactory::from("asd dsa");
|
||||
/// let op = factory.delta(2, " xxx ", 5).expect("replaced region is equal to origin");
|
||||
/// factory.update(op)?;
|
||||
/// assert_eq!(factory.content(), "as xxx sa");
|
||||
/// # use codemp::api::OperationFactory;
|
||||
/// # let mut factory = String::from("asd dsa");
|
||||
/// factory = factory
|
||||
/// .diff(2, " xxx ", 5)
|
||||
/// .expect("replaced region is equal to origin")
|
||||
/// .apply(&factory)?;
|
||||
/// assert_eq!(factory, "as xxx sa");
|
||||
/// # Ok::<(), codemp::ot::OTError>(())
|
||||
/// ```
|
||||
///
|
||||
/// use [OperationFactory::delete] to remove characters from given index
|
||||
/// use [OperationFactory::del] to remove characters from given index
|
||||
///
|
||||
/// ```rust
|
||||
/// # use codemp::buffer::factory::{OperationFactory, SimpleOperationFactory};
|
||||
/// # let mut factory = SimpleOperationFactory::from("as xxx sa");
|
||||
/// factory.update(factory.delete(2, 5))?;
|
||||
/// assert_eq!(factory.content(), "assa");
|
||||
/// # use codemp::api::OperationFactory;
|
||||
/// # let mut factory = String::from("as xxx sa");
|
||||
/// factory = factory.del(2, 5).apply(&factory)?;
|
||||
/// assert_eq!(factory, "assa");
|
||||
/// # Ok::<(), codemp::ot::OTError>(())
|
||||
/// ```
|
||||
///
|
||||
/// use [OperationFactory::replace] to completely replace buffer content
|
||||
///
|
||||
/// ```rust
|
||||
/// # use codemp::buffer::factory::{OperationFactory, SimpleOperationFactory};
|
||||
/// # let mut factory = SimpleOperationFactory::from("assa");
|
||||
/// let op = factory.replace("from scratch").expect("replace is equal to origin");
|
||||
/// factory.update(op)?;
|
||||
/// assert_eq!(factory.content(), "from scratch");
|
||||
/// # use codemp::api::OperationFactory;
|
||||
/// # let mut factory = String::from("assa");
|
||||
/// factory = factory.replace("from scratch")
|
||||
/// .expect("replace is equal to origin")
|
||||
/// .apply(&factory)?;
|
||||
/// assert_eq!(factory, "from scratch");
|
||||
/// # Ok::<(), codemp::ot::OTError>(())
|
||||
/// ```
|
||||
///
|
||||
/// use [OperationFactory::cancel] to remove characters at index, but backwards
|
||||
/// use [OperationFactory::canc] to remove characters at index, but backwards
|
||||
///
|
||||
/// ```rust
|
||||
/// # use codemp::buffer::factory::{OperationFactory, SimpleOperationFactory};
|
||||
/// # let mut factory = SimpleOperationFactory::from("from scratch");
|
||||
/// factory.update(factory.cancel(12, 8))?;
|
||||
/// assert_eq!(factory.content(), "from");
|
||||
/// # use codemp::api::OperationFactory;
|
||||
/// # let mut factory = String::from("from scratch");
|
||||
/// factory = factory.canc(12, 8).apply(&factory)?;
|
||||
/// assert_eq!(factory, "from");
|
||||
/// # Ok::<(), codemp::ot::OTError>(())
|
||||
/// ```
|
||||
pub trait OperationFactory {
|
||||
|
@ -106,11 +109,11 @@ pub trait OperationFactory {
|
|||
|
||||
/// completely replace the buffer with given text
|
||||
fn replace(&self, txt: &str) -> Option<OperationSeq> {
|
||||
self.delta(0, txt, self.content().len())
|
||||
self.diff(0, txt, self.content().len())
|
||||
}
|
||||
|
||||
/// transform buffer in range [start..end] with given text
|
||||
fn delta(&self, start: usize, txt: &str, end: usize) -> Option<OperationSeq> {
|
||||
fn diff(&self, start: usize, txt: &str, end: usize) -> Option<OperationSeq> {
|
||||
let mut out = OperationSeq::default();
|
||||
let content = self.content();
|
||||
let tail_skip = content.len() - end; // TODO len is number of bytes, not chars
|
||||
|
@ -139,7 +142,7 @@ pub trait OperationFactory {
|
|||
}
|
||||
|
||||
/// insert given chars at target position
|
||||
fn insert(&self, txt: &str, pos: u64) -> OperationSeq {
|
||||
fn ins(&self, txt: &str, pos: u64) -> OperationSeq {
|
||||
let mut out = OperationSeq::default();
|
||||
let total = self.content().len() as u64;
|
||||
out.retain(pos);
|
||||
|
@ -149,7 +152,7 @@ pub trait OperationFactory {
|
|||
}
|
||||
|
||||
/// delete n characters forward at given position
|
||||
fn delete(&self, pos: u64, count: u64) -> OperationSeq {
|
||||
fn del(&self, pos: u64, count: u64) -> OperationSeq {
|
||||
let mut out = OperationSeq::default();
|
||||
let len = self.content().len() as u64;
|
||||
out.retain(pos);
|
||||
|
@ -159,7 +162,7 @@ pub trait OperationFactory {
|
|||
}
|
||||
|
||||
/// delete n characters backwards at given position
|
||||
fn cancel(&self, pos: u64, count: u64) -> OperationSeq {
|
||||
fn canc(&self, pos: u64, count: u64) -> OperationSeq {
|
||||
let mut out = OperationSeq::default();
|
||||
let len = self.content().len() as u64;
|
||||
out.retain(pos - count);
|
||||
|
|
|
@ -124,7 +124,7 @@ impl Client {
|
|||
/// or [crate::api::Controller::recv] for text events using its [crate::buffer::Controller].
|
||||
/// to generate operation sequences use the [crate::api::OperationFactory]
|
||||
/// methods, which are implemented on [crate::buffer::Controller], such as
|
||||
/// [crate::api::OperationFactory::delta].
|
||||
/// [crate::api::OperationFactory::diff].
|
||||
pub async fn attach(&mut self, path: &str) -> Result<Arc<BufferController>, Error> {
|
||||
if let Some(workspace) = &mut self.workspace {
|
||||
let mut client = self.client.buffer.clone();
|
||||
|
|
|
@ -40,8 +40,7 @@
|
|||
//! [instance::a_sync::Instance]
|
||||
//!
|
||||
//! ```rust,no_run
|
||||
//! use codemp::api::Controller;
|
||||
//! use codemp::buffer::OperationFactory;
|
||||
//! use codemp::api::{Controller, OperationFactory};
|
||||
//! # use codemp::instance::a_sync::Instance;
|
||||
//!
|
||||
//! # async fn async_example() -> codemp::Result<()> {
|
||||
|
@ -63,8 +62,9 @@
|
|||
//! // attach to a new buffer and execute operations on it
|
||||
//! session.create("test.txt", None).await?; // create new buffer
|
||||
//! let buffer = session.attach("test.txt").await?; // attach to it
|
||||
//! buffer.send(buffer.insert("hello", 0))?; // insert some text
|
||||
//! if let Some(operation) = buffer.delta(4, "o world", 5) {
|
||||
//! let text = buffer.content(); // any string can be used as operation factory
|
||||
//! buffer.send(text.ins("hello", 0))?; // insert some text
|
||||
//! if let Some(operation) = text.diff(4, "o world", 5) {
|
||||
//! buffer.send(operation)?; // replace with precision, if valid
|
||||
//! }
|
||||
//! assert_eq!(buffer.content(), "hello world");
|
||||
|
|
Loading…
Reference in a new issue