Fix whitespaces
This commit is contained in:
parent
667b324a88
commit
bf8d00c5e2
5 changed files with 53 additions and 22 deletions
|
|
@ -73,7 +73,8 @@ mod test {
|
||||||
"original_1 edit_1 original_3",
|
"original_1 edit_1 original_3",
|
||||||
);
|
);
|
||||||
|
|
||||||
// One deleted a large range, the other deleted subranges and inserted as well
|
// One deleted a large range, the other deleted subranges and inserted as
|
||||||
|
// well
|
||||||
test_merge_both_ways(
|
test_merge_both_ways(
|
||||||
"original_1 original_2 original_3 original_4 original_5",
|
"original_1 original_2 original_3 original_4 original_5",
|
||||||
"original_1 original_5",
|
"original_1 original_5",
|
||||||
|
|
@ -161,6 +162,8 @@ mod test {
|
||||||
"hi there my friend ",
|
"hi there my friend ",
|
||||||
"hi there you my friend ",
|
"hi there you my friend ",
|
||||||
);
|
);
|
||||||
|
|
||||||
|
test_merge_both_ways("a", "a b c", "a b c d", "a b c d");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test_matrix( [
|
#[test_matrix( [
|
||||||
|
|
|
||||||
|
|
@ -65,7 +65,6 @@ where
|
||||||
|
|
||||||
Self::new(
|
Self::new(
|
||||||
original,
|
original,
|
||||||
// Self::cook_operations(diff),
|
|
||||||
Self::cook_operations(Self::elongate_operations(diff)).collect(),
|
Self::cook_operations(Self::elongate_operations(diff)).collect(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
@ -191,7 +190,7 @@ where
|
||||||
pub fn merge(self, other: Self) -> Self {
|
pub fn merge(self, other: Self) -> Self {
|
||||||
debug_assert_eq!(
|
debug_assert_eq!(
|
||||||
self.text, other.text,
|
self.text, other.text,
|
||||||
"EditedText-s must be derived from the same text to be mergable"
|
"`EditedText`-s must be derived from the same text to be mergable"
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut left_merge_context = MergeContext::default();
|
let mut left_merge_context = MergeContext::default();
|
||||||
|
|
@ -285,7 +284,7 @@ mod tests {
|
||||||
let original = "hello world! ...";
|
let original = "hello world! ...";
|
||||||
let left = "Hello world! I'm Andras.";
|
let left = "Hello world! I'm Andras.";
|
||||||
let right = "Hello world! How are you?";
|
let right = "Hello world! How are you?";
|
||||||
let expected = "Hello world! How are you?I'm Andras.";
|
let expected = "Hello world! I'm Andras. How are you?";
|
||||||
|
|
||||||
let operations_1 = EditedText::from_strings(original, left);
|
let operations_1 = EditedText::from_strings(original, left);
|
||||||
let operations_2 = EditedText::from_strings(original, right);
|
let operations_2 = EditedText::from_strings(original, right);
|
||||||
|
|
|
||||||
|
|
@ -107,15 +107,8 @@ where
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Tries to apply the operation to the given `ropey::Rope` text, returning
|
/// Applies the operation to the given `StringBuilder`, returning the
|
||||||
/// the modified text.
|
/// modified `StringBuilder`.
|
||||||
///
|
|
||||||
/// # Errors
|
|
||||||
///
|
|
||||||
/// Returns a `SyncLibError::OperationApplicationError` if the operation
|
|
||||||
/// cannot be applied.
|
|
||||||
///
|
|
||||||
/// # Panics
|
|
||||||
///
|
///
|
||||||
/// When compiled in debug mode, panics if a delete operation is attempted
|
/// When compiled in debug mode, panics if a delete operation is attempted
|
||||||
/// on a range of text that does not match the text to be deleted.
|
/// on a range of text that does not match the text to be deleted.
|
||||||
|
|
|
||||||
|
|
@ -15,12 +15,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&str> for Token<String> {
|
impl From<&str> for Token<String> {
|
||||||
fn from(s: &str) -> Self {
|
fn from(s: &str) -> Self { Token::new(s.trim().to_owned(), s.to_owned()) }
|
||||||
Token {
|
|
||||||
normalised: s.to_owned(),
|
|
||||||
original: s.to_owned(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Token<T>
|
impl<T> Token<T>
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,48 @@
|
||||||
use super::token::Token;
|
use super::token::Token;
|
||||||
|
|
||||||
|
/// Splits on whitespace keeping the leading whitespace.
|
||||||
|
///
|
||||||
|
///
|
||||||
|
/// ## Example
|
||||||
|
///
|
||||||
|
/// "Hi there!" -> ["Hi", " there!"]
|
||||||
pub fn word_tokenizer(text: &str) -> Vec<Token<String>> {
|
pub fn word_tokenizer(text: &str) -> Vec<Token<String>> {
|
||||||
text.split_inclusive(char::is_whitespace)
|
let mut result: Vec<Token<String>> = Vec::new();
|
||||||
.map(|s| Token::new(s.to_owned(), s.to_owned()))
|
|
||||||
.collect()
|
let mut last_whitespace = 0;
|
||||||
|
let mut previous_char_is_whitespace = true;
|
||||||
|
|
||||||
|
for (i, c) in text.char_indices() {
|
||||||
|
let is_current_char_whitespace = c.is_whitespace();
|
||||||
|
if !previous_char_is_whitespace && is_current_char_whitespace {
|
||||||
|
result.push(text[last_whitespace..i].into());
|
||||||
|
last_whitespace = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
previous_char_is_whitespace = is_current_char_whitespace;
|
||||||
|
}
|
||||||
|
|
||||||
|
if last_whitespace < text.len() {
|
||||||
|
result.push(text[last_whitespace..].into());
|
||||||
|
}
|
||||||
|
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use insta::assert_debug_snapshot;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_with_snapshots() {
|
||||||
|
assert_debug_snapshot!(word_tokenizer("Hi there!"));
|
||||||
|
|
||||||
|
assert_debug_snapshot!(word_tokenizer(""));
|
||||||
|
|
||||||
|
assert_debug_snapshot!(word_tokenizer(" what? "));
|
||||||
|
|
||||||
|
assert_debug_snapshot!(word_tokenizer(" hello, \nwhere are you?"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue