Add cursor moving #19
2 changed files with 22 additions and 3 deletions
Add apply_merge_context to cursor
commit
3744b0a633
|
|
@ -1,11 +1,10 @@
|
||||||
mod cursor;
|
|
||||||
mod diffs;
|
mod diffs;
|
||||||
mod operation_transformation;
|
mod operation_transformation;
|
||||||
mod tokenizer;
|
mod tokenizer;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
pub use cursor::{CursorPosition, TextWithCursors};
|
|
||||||
pub use operation_transformation::{
|
pub use operation_transformation::{
|
||||||
EditedText, reconcile, reconcile_with_cursors, reconcile_with_tokenizer,
|
CursorPosition, EditedText, TextWithCursors, reconcile, reconcile_with_cursors,
|
||||||
|
reconcile_with_tokenizer,
|
||||||
};
|
};
|
||||||
pub use tokenizer::{Tokenizer, token::Token};
|
pub use tokenizer::{Tokenizer, token::Token};
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,9 @@ use std::borrow::Cow;
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use super::merge_context::MergeContext;
|
||||||
|
use crate::operation_transformation::Operation;
|
||||||
|
|
||||||
// CursorPosition is a wrapper around usize to represent the position of an
|
// CursorPosition is a wrapper around usize to represent the position of an
|
||||||
// identifiable cursor in a text document based on the character index.
|
// identifiable cursor in a text document based on the character index.
|
||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||||
|
|
@ -12,6 +15,23 @@ pub struct CursorPosition {
|
||||||
pub char_index: usize,
|
pub char_index: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl CursorPosition {
|
||||||
|
pub fn apply_merge_context<T>(&self, context: &MergeContext<T>) -> Self
|
||||||
|
where
|
||||||
|
T: PartialEq + Clone + std::fmt::Debug,
|
||||||
|
{
|
||||||
|
let char_index = match context.last_operation() {
|
||||||
|
Some(Operation::Delete { index, .. }) => (*index) as i64,
|
||||||
|
_ => self.char_index as i64 + context.shift,
|
||||||
|
};
|
||||||
|
|
||||||
|
CursorPosition {
|
||||||
|
id: self.id,
|
||||||
|
char_index: char_index.max(0) as usize,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||||
#[derive(Debug, Clone, PartialEq, Default)]
|
#[derive(Debug, Clone, PartialEq, Default)]
|
||||||
pub struct TextWithCursors<'a> {
|
pub struct TextWithCursors<'a> {
|
||||||
Loading…
Add table
Add a link
Reference in a new issue