Extract types into a separate module
This commit is contained in:
parent
806a5bc113
commit
b18a692d46
8 changed files with 57 additions and 32 deletions
|
|
@ -10,7 +10,8 @@ use crate::{
|
||||||
},
|
},
|
||||||
raw_operation::RawOperation,
|
raw_operation::RawOperation,
|
||||||
tokenizer::{Tokenizer, word_tokenizer::word_tokenizer},
|
tokenizer::{Tokenizer, word_tokenizer::word_tokenizer},
|
||||||
utils::{history::History, side::Side, string_builder::StringBuilder},
|
types::{history::History, side::Side, text_with_history::TextWithHistory},
|
||||||
|
utils::string_builder::StringBuilder,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// A text document and a sequence of operations that can be applied to the text
|
/// A text document and a sequence of operations that can be applied to the text
|
||||||
|
|
@ -228,7 +229,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn apply_with_history(&self) -> Vec<(History, String)> {
|
pub fn apply_with_history(&self) -> Vec<TextWithHistory> {
|
||||||
let mut builder: StringBuilder<'_> = StringBuilder::new(self.text);
|
let mut builder: StringBuilder<'_> = StringBuilder::new(self.text);
|
||||||
|
|
||||||
let mut history = Vec::with_capacity(self.operations.len());
|
let mut history = Vec::with_capacity(self.operations.len());
|
||||||
|
|
@ -237,10 +238,17 @@ where
|
||||||
builder = operation.apply(builder);
|
builder = operation.apply(builder);
|
||||||
|
|
||||||
match operation {
|
match operation {
|
||||||
Operation::Equal { .. } => history.push((History::Unchanged, builder.take())),
|
Operation::Equal { .. } => {
|
||||||
|
history.push(TextWithHistory::new(History::Unchanged, builder.take()))
|
||||||
|
}
|
||||||
Operation::Insert { side, .. } => match side {
|
Operation::Insert { side, .. } => match side {
|
||||||
Side::Left => history.push((History::AddedFromLeft, builder.take())),
|
Side::Left => {
|
||||||
Side::Right => history.push((History::AddedFromRight, builder.take())),
|
history.push(TextWithHistory::new(History::AddedFromLeft, builder.take()))
|
||||||
|
}
|
||||||
|
Side::Right => history.push(TextWithHistory::new(
|
||||||
|
History::AddedFromRight,
|
||||||
|
builder.take(),
|
||||||
|
)),
|
||||||
},
|
},
|
||||||
Operation::Delete {
|
Operation::Delete {
|
||||||
deleted_character_count,
|
deleted_character_count,
|
||||||
|
|
@ -250,8 +258,12 @@ where
|
||||||
} => {
|
} => {
|
||||||
let deleted = self.text[*order..*order + *deleted_character_count].to_string();
|
let deleted = self.text[*order..*order + *deleted_character_count].to_string();
|
||||||
match side {
|
match side {
|
||||||
Side::Left => history.push((History::RemovedFromLeft, deleted)),
|
Side::Left => {
|
||||||
Side::Right => history.push((History::RemovedFromRight, deleted)),
|
history.push(TextWithHistory::new(History::RemovedFromLeft, deleted))
|
||||||
|
}
|
||||||
|
Side::Right => {
|
||||||
|
history.push(TextWithHistory::new(History::RemovedFromRight, deleted))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,9 @@ use core::fmt::{Debug, Display};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Token,
|
Side, Token,
|
||||||
utils::{
|
utils::{
|
||||||
find_longest_prefix_contained_within::find_longest_prefix_contained_within, side::Side,
|
find_longest_prefix_contained_within::find_longest_prefix_contained_within,
|
||||||
string_builder::StringBuilder,
|
string_builder::StringBuilder,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
||||||
3
src/types.rs
Normal file
3
src/types.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
pub mod history;
|
||||||
|
pub mod side;
|
||||||
|
pub mod text_with_history;
|
||||||
|
|
@ -1,7 +1,10 @@
|
||||||
|
#[cfg(feature = "serde")]
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
#[cfg(feature = "wasm")]
|
#[cfg(feature = "wasm")]
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
#[cfg_attr(feature = "wasm", wasm_bindgen)]
|
#[cfg_attr(feature = "wasm", wasm_bindgen)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
#[cfg(feature = "wasm")]
|
#[cfg(feature = "wasm")]
|
||||||
pub enum History {
|
pub enum History {
|
||||||
|
|
@ -16,6 +19,7 @@ pub enum History {
|
||||||
/// When compiled to WASM, the enum values are the same as their names.
|
/// When compiled to WASM, the enum values are the same as their names.
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
#[cfg(not(feature = "wasm"))]
|
#[cfg(not(feature = "wasm"))]
|
||||||
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||||
pub enum History {
|
pub enum History {
|
||||||
Unchanged,
|
Unchanged,
|
||||||
AddedFromLeft,
|
AddedFromLeft,
|
||||||
26
src/types/text_with_history.rs
Normal file
26
src/types/text_with_history.rs
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
#[cfg(feature = "serde")]
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
#[cfg(feature = "wasm")]
|
||||||
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
|
use crate::types::history::History;
|
||||||
|
|
||||||
|
/// Wrapper type to expose `(History, String)` to JS.
|
||||||
|
#[cfg_attr(feature = "wasm", wasm_bindgen)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||||
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
pub struct TextWithHistory {
|
||||||
|
history: History,
|
||||||
|
text: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg_attr(feature = "wasm", wasm_bindgen)]
|
||||||
|
impl TextWithHistory {
|
||||||
|
pub fn new(history: History, text: String) -> Self { TextWithHistory { history, text } }
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
|
pub fn history(&self) -> History { self.history }
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
|
pub fn text(&self) -> String { self.text.clone() }
|
||||||
|
}
|
||||||
|
|
@ -1,7 +1,5 @@
|
||||||
pub mod common_prefix_len;
|
pub mod common_prefix_len;
|
||||||
pub mod common_suffix_len;
|
pub mod common_suffix_len;
|
||||||
pub mod find_longest_prefix_contained_within;
|
pub mod find_longest_prefix_contained_within;
|
||||||
pub mod history;
|
|
||||||
pub mod myers_diff;
|
pub mod myers_diff;
|
||||||
pub mod side;
|
|
||||||
pub mod string_builder;
|
pub mod string_builder;
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,6 @@
|
||||||
|
#[cfg(feature = "serde")]
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
#[cfg(feature = "wasm")]
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
use crate::History;
|
use crate::History;
|
||||||
|
|
@ -88,24 +91,3 @@ impl From<crate::CursorPosition> for JsCursorPosition {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Wrapper type to expose `(History, String)` to JS.
|
|
||||||
#[wasm_bindgen]
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
|
||||||
pub struct JsTextWithHistory {
|
|
||||||
history: History,
|
|
||||||
text: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<(History, String)> for JsTextWithHistory {
|
|
||||||
fn from((history, text): (History, String)) -> Self { JsTextWithHistory { history, text } }
|
|
||||||
}
|
|
||||||
|
|
||||||
#[wasm_bindgen]
|
|
||||||
impl JsTextWithHistory {
|
|
||||||
#[must_use]
|
|
||||||
pub fn history(&self) -> History { self.history }
|
|
||||||
|
|
||||||
#[must_use]
|
|
||||||
pub fn text(&self) -> String { self.text.clone() }
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue