Improve formatting
This commit is contained in:
parent
1b46e5d237
commit
a1a339b23d
2 changed files with 19 additions and 3 deletions
|
|
@ -180,6 +180,20 @@ export function reconcile(
|
||||||
return jsResult;
|
return jsResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates a compact diff representation between an original and changed text.
|
||||||
|
*
|
||||||
|
* These can be parsed and unpacked using Rust crate's EditedText::from_change_set.
|
||||||
|
*
|
||||||
|
* This function computes the differences between two versions of text and returns
|
||||||
|
* a compact string representation of those changes. The returned format is
|
||||||
|
* serialised JSON.
|
||||||
|
*
|
||||||
|
* @param original - The original/base version of the text
|
||||||
|
* @param changed - The modified version of the text (either string or TextWithCursors with cursor positions)
|
||||||
|
* @param tokenizer - The tokenisation strategy, which is the same as used in `reconcile`.
|
||||||
|
* @returns A compact string representation of the diff between original and changed text
|
||||||
|
*/
|
||||||
export function getCompactDiff(
|
export function getCompactDiff(
|
||||||
original: string,
|
original: string,
|
||||||
changed: string | TextWithOptionalCursors,
|
changed: string | TextWithOptionalCursors,
|
||||||
|
|
|
||||||
|
|
@ -12,9 +12,7 @@ use crate::{CursorPosition, Tokenizer, operation_transformation::Operation};
|
||||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
||||||
pub enum SimpleOperation {
|
pub enum SimpleOperation {
|
||||||
Equal { length: usize },
|
Equal { length: usize },
|
||||||
|
|
||||||
Insert { text: String },
|
Insert { text: String },
|
||||||
|
|
||||||
Delete { length: usize },
|
Delete { length: usize },
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -35,6 +33,7 @@ impl SimpleOperation {
|
||||||
previous_equal = Some(*length);
|
previous_equal = Some(*length);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Operation::Insert { text, .. } => {
|
Operation::Insert { text, .. } => {
|
||||||
if let Some(prev_length) = previous_equal {
|
if let Some(prev_length) = previous_equal {
|
||||||
result.push(SimpleOperation::Equal {
|
result.push(SimpleOperation::Equal {
|
||||||
|
|
@ -49,6 +48,7 @@ impl SimpleOperation {
|
||||||
.collect();
|
.collect();
|
||||||
result.push(SimpleOperation::Insert { text });
|
result.push(SimpleOperation::Insert { text });
|
||||||
}
|
}
|
||||||
|
|
||||||
Operation::Delete {
|
Operation::Delete {
|
||||||
deleted_character_count,
|
deleted_character_count,
|
||||||
..
|
..
|
||||||
|
|
@ -76,6 +76,7 @@ impl SimpleOperation {
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This is similar to `crate::operation_transformation::utils::cook_operations`
|
||||||
pub fn to_operations<T>(
|
pub fn to_operations<T>(
|
||||||
simple_operations: Vec<Self>,
|
simple_operations: Vec<Self>,
|
||||||
original_text: &str,
|
original_text: &str,
|
||||||
|
|
@ -85,7 +86,6 @@ impl SimpleOperation {
|
||||||
T: PartialEq + Clone + Debug,
|
T: PartialEq + Clone + Debug,
|
||||||
{
|
{
|
||||||
let mut operations: Vec<Operation<T>> = Vec::with_capacity(simple_operations.len());
|
let mut operations: Vec<Operation<T>> = Vec::with_capacity(simple_operations.len());
|
||||||
|
|
||||||
let mut order = 0;
|
let mut order = 0;
|
||||||
|
|
||||||
for simple_operation in simple_operations {
|
for simple_operation in simple_operations {
|
||||||
|
|
@ -101,10 +101,12 @@ impl SimpleOperation {
|
||||||
order += token.get_original_length();
|
order += token.get_original_length();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SimpleOperation::Insert { text } => {
|
SimpleOperation::Insert { text } => {
|
||||||
let tokens = tokenizer(&text);
|
let tokens = tokenizer(&text);
|
||||||
operations.push(Operation::create_insert(order, tokens));
|
operations.push(Operation::create_insert(order, tokens));
|
||||||
}
|
}
|
||||||
|
|
||||||
SimpleOperation::Delete { length } => {
|
SimpleOperation::Delete { length } => {
|
||||||
operations.push(Operation::create_delete(order, length));
|
operations.push(Operation::create_delete(order, length));
|
||||||
order += length;
|
order += length;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue