Change style
This commit is contained in:
parent
408ce5268f
commit
deffa195b3
23 changed files with 72 additions and 76 deletions
|
|
@ -152,7 +152,7 @@
|
|||
//! );
|
||||
//! ```
|
||||
//!
|
||||
//! ## Efficiently serialize changes
|
||||
//! ## Compact change serialization
|
||||
//!
|
||||
//! The edits can be serialized into a compact representation without the full
|
||||
//! original text, making the size depend only on the changes made.
|
||||
|
|
|
|||
|
|
@ -24,8 +24,7 @@ use crate::{Tokenizer, types::text_with_cursors::TextWithCursors};
|
|||
/// into that span, the inserted text will be present in the return
|
||||
/// value.
|
||||
///
|
||||
/// The function supports UTF-8. The arguments are tokenized at the
|
||||
/// granularity of words.
|
||||
/// Supports UTF-8. Arguments are tokenized using the provided `tokenizer`.
|
||||
///
|
||||
/// ```
|
||||
/// use reconcile_text::{reconcile, BuiltinTokenizer};
|
||||
|
|
|
|||
|
|
@ -54,7 +54,7 @@ where
|
|||
T: PartialEq + Clone + Debug,
|
||||
{
|
||||
/// Create an `EditedText` from the given original and updated strings
|
||||
/// using the provided tokenizer.
|
||||
/// using the provided tokenizer
|
||||
pub fn from_strings_with_tokenizer(
|
||||
original: &'a str,
|
||||
updated: &TextWithCursors,
|
||||
|
|
@ -256,7 +256,7 @@ where
|
|||
)
|
||||
}
|
||||
|
||||
/// Apply the operations to the text and return the resulting text.
|
||||
/// Apply the operations to the text and return the resulting text
|
||||
#[must_use]
|
||||
pub fn apply(&self) -> TextWithCursors {
|
||||
let mut builder: StringBuilder<'_> = StringBuilder::new(self.text);
|
||||
|
|
@ -355,8 +355,8 @@ where
|
|||
/// This is useful for sending text diffs over the network if there's a
|
||||
/// clear consensus on the original text.
|
||||
///
|
||||
/// Inserts are represented as strings, deletes as negative integers,
|
||||
/// and equal spans as positive integers.
|
||||
/// Inserts are strings, deletes are negative integers (character count),
|
||||
/// and retained spans are positive integers (character count).
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ use crate::{
|
|||
},
|
||||
};
|
||||
|
||||
/// Represents a change that can be applied on a `StringBuilder`.
|
||||
/// Represents a change that can be applied on a `StringBuilder`
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Clone, PartialEq)]
|
||||
pub enum Operation<T>
|
||||
|
|
@ -47,7 +47,7 @@ where
|
|||
T: PartialEq + Clone + Debug,
|
||||
{
|
||||
/// Creates an equal (retain) operation starting at the given character
|
||||
/// offset in the original text.
|
||||
/// offset in the original text
|
||||
pub fn create_equal(order: usize, length: usize) -> Self {
|
||||
Operation::Equal {
|
||||
order,
|
||||
|
|
@ -69,13 +69,13 @@ where
|
|||
}
|
||||
|
||||
/// Creates an insert operation at the given character offset with the
|
||||
/// given tokens.
|
||||
/// given tokens
|
||||
pub fn create_insert(order: usize, text: Vec<Token<T>>) -> Self {
|
||||
Operation::Insert { order, text }
|
||||
}
|
||||
|
||||
/// Creates a delete operation at the given character offset for the
|
||||
/// specified number of characters.
|
||||
/// specified number of characters
|
||||
pub fn create_delete(order: usize, deleted_character_count: usize) -> Self {
|
||||
Operation::Delete {
|
||||
order,
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ use std::fmt::Debug;
|
|||
use crate::{operation_transformation::Operation, raw_operation::RawOperation};
|
||||
|
||||
/// Turn raw operations into ordered operations while keeping track of the
|
||||
/// original token's indexes.
|
||||
/// original token's indexes
|
||||
pub fn cook_operations<I, T>(raw_operations: I) -> impl Iterator<Item = Operation<T>>
|
||||
where
|
||||
I: IntoIterator<Item = RawOperation<T>>,
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ use wasm_bindgen::prelude::*;
|
|||
|
||||
pub mod token;
|
||||
|
||||
/// Type alias for tokenizer functions that split a string into tokens.
|
||||
/// Type alias for tokenizer functions that split a string into tokens
|
||||
pub type Tokenizer<T> = dyn Fn(&str) -> Vec<Token<T>>;
|
||||
|
||||
#[cfg_attr(feature = "wasm", wasm_bindgen)]
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use super::token::Token;
|
||||
|
||||
/// Splits text into UTF-8 characters.
|
||||
/// Splits text into UTF-8 characters
|
||||
///
|
||||
/// ```not_rust
|
||||
/// "Hey!" -> ["H", "e", "y", "!"]
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use super::token::Token;
|
||||
|
||||
/// Splits text into lines, preserving line endings as separate tokens.
|
||||
/// Splits text into lines, preserving line endings as separate tokens
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
|
|
|
|||
|
|
@ -14,21 +14,21 @@ pub struct Token<T>
|
|||
where
|
||||
T: PartialEq + Clone + Debug,
|
||||
{
|
||||
/// The normalized form of the token used deriving the diff.
|
||||
/// The normalized form of the token used deriving the diff
|
||||
normalized: T,
|
||||
|
||||
/// The original string, that should be inserted or deleted in the document.
|
||||
/// The original string, that should be inserted or deleted in the document
|
||||
original: String,
|
||||
|
||||
/// Whether the token is semantically joinable with the previous token.
|
||||
/// Whether the token is semantically joinable with the previous token
|
||||
pub is_left_joinable: bool,
|
||||
|
||||
/// Whether the token is semantically joinable with the next token.
|
||||
/// Whether the token is semantically joinable with the next token
|
||||
pub is_right_joinable: bool,
|
||||
}
|
||||
|
||||
/// Trivial implementation of Token when the normalized form is the same as the
|
||||
/// original string.
|
||||
/// original string
|
||||
impl From<&str> for Token<String> {
|
||||
fn from(text: &str) -> Self { Token::new(text.to_owned(), text.to_owned(), true, true) }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use super::token::Token;
|
||||
|
||||
/// Splits text on word boundaries, creating tokens of alternating words and
|
||||
/// whitespace with the whitespace getting unique IDs.
|
||||
/// whitespace with the whitespace getting unique IDs
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
|
|||
use wasm_bindgen::prelude::*;
|
||||
|
||||
/// `CursorPosition` represents the position of an identifiable cursor in a text
|
||||
/// document based on its (UTF-8) character index.
|
||||
/// document based on its (UTF-8) character index
|
||||
#[allow(clippy::unsafe_derive_deserialize)]
|
||||
#[cfg_attr(feature = "wasm", wasm_bindgen)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ pub enum History {
|
|||
RemovedFromRight = "RemovedFromRight",
|
||||
}
|
||||
|
||||
/// Provenance label for each span returned by `apply_with_history`.
|
||||
/// Provenance label for each span returned by `apply_with_history`
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[cfg(not(feature = "wasm"))]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ use std::fmt::Display;
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Pretty-printable flag to tell which conflicting edit (side)
|
||||
/// an operation is associated with.
|
||||
/// an operation is associated with
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Side {
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ use wasm_bindgen::prelude::*;
|
|||
|
||||
use crate::types::history::History;
|
||||
|
||||
/// A text span annotated with its origin in a merge result.
|
||||
/// A text span annotated with its origin in a merge result
|
||||
#[allow(clippy::unsafe_derive_deserialize)]
|
||||
#[cfg_attr(feature = "wasm", wasm_bindgen)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ use std::fmt::Debug;
|
|||
use crate::Token;
|
||||
|
||||
/// Given two lists of tokens, returns `length` where the `old` list
|
||||
/// somewhere within contains the `length` prefix of the `new` list.
|
||||
/// somewhere within contains the `length` prefix of the `new` list
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
|
|
|
|||
|
|
@ -77,7 +77,7 @@ where
|
|||
/// We can't use a traditional Vec to represent `V` since we use `k` as an index
|
||||
/// and it can take on negative values. So instead `V` is represented as a
|
||||
/// light-weight wrapper around a Vec plus an `offset` which is the maximum
|
||||
/// value `k` can take on in order to map negative `k`'s back to a value >= 0.
|
||||
/// value `k` can take on to map negative `k`'s back to a value >= 0.
|
||||
#[derive(Debug)]
|
||||
struct V {
|
||||
offset: isize,
|
||||
|
|
|
|||
|
|
@ -34,11 +34,11 @@ impl StringBuilder<'_> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Insert a string at the end of the built buffer.
|
||||
/// Insert a string at the end of the built buffer
|
||||
pub fn insert(&mut self, text: &str) { self.buffer.push_str(text); }
|
||||
|
||||
/// Skip copying `length` characters from the original string to the built
|
||||
/// buffer.
|
||||
/// buffer
|
||||
pub fn delete(&mut self, length: usize) {
|
||||
if length == 0 {
|
||||
return;
|
||||
|
|
@ -52,7 +52,7 @@ impl StringBuilder<'_> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Copy `length` characters from the original string to the built buffer.
|
||||
/// Copy `length` characters from the original string to the built buffer
|
||||
pub fn retain(&mut self, length: usize) {
|
||||
self.buffer.extend(self.original.by_ref().take(length));
|
||||
|
||||
|
|
|
|||
16
src/wasm.rs
16
src/wasm.rs
|
|
@ -8,7 +8,7 @@ use crate::{BuiltinTokenizer, CursorPosition, EditedText, SpanWithHistory, TextW
|
|||
#[global_allocator]
|
||||
static ALLOC: wee_alloc::WeeAlloc<'_> = wee_alloc::WeeAlloc::INIT;
|
||||
|
||||
/// WASM wrapper around `crate::reconcile` for merging text.
|
||||
/// WASM wrapper around `crate::reconcile` for merging text
|
||||
#[wasm_bindgen(js_name = reconcile)]
|
||||
#[must_use]
|
||||
pub fn reconcile(
|
||||
|
|
@ -22,7 +22,7 @@ pub fn reconcile(
|
|||
crate::reconcile(parent, left, right, &*tokenizer).apply()
|
||||
}
|
||||
|
||||
/// WASM wrapper around `crate::reconcile` that also returns provenance history.
|
||||
/// WASM wrapper around `crate::reconcile` that also returns provenance history
|
||||
#[wasm_bindgen(js_name = reconcileWithHistory)]
|
||||
#[must_use]
|
||||
pub fn reconcile_with_history(
|
||||
|
|
@ -48,13 +48,13 @@ pub fn reconcile_with_history(
|
|||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// - `parent`: The common parent document.
|
||||
/// - `left`: The left document updated by one user.
|
||||
/// - `right`: The right document updated by another user.
|
||||
/// - `parent`: The common parent document
|
||||
/// - `left`: The left document updated by one user
|
||||
/// - `right`: The right document updated by another user
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// The merged document.
|
||||
/// The merged document
|
||||
#[wasm_bindgen(js_name = genericReconcile)]
|
||||
#[must_use]
|
||||
pub fn generic_reconcile(
|
||||
|
|
@ -80,7 +80,7 @@ pub fn generic_reconcile(
|
|||
}
|
||||
|
||||
/// WASM wrapper around getting a compact diff representation of two texts as a
|
||||
/// list of numbers and strings.
|
||||
/// list of numbers and strings
|
||||
#[wasm_bindgen(js_name = diff)]
|
||||
#[must_use]
|
||||
pub fn diff(parent: &str, changed: &TextWithCursors, tokenizer: BuiltinTokenizer) -> Vec<JsValue> {
|
||||
|
|
@ -94,7 +94,7 @@ pub fn diff(parent: &str, changed: &TextWithCursors, tokenizer: BuiltinTokenizer
|
|||
.collect()
|
||||
}
|
||||
|
||||
/// Inverse of `diff`, applies a compact diff representation to a parent text.
|
||||
/// Inverse of `diff`, applies a compact diff representation to a parent text
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue