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