Add left/right joinability for tokens
This commit is contained in:
parent
b0c6c082a1
commit
b230d34b88
13 changed files with 313 additions and 75 deletions
|
|
@ -28,10 +28,26 @@ where
|
|||
|
||||
pub fn get_original_text(self) -> String { self.tokens().iter().map(Token::original).collect() }
|
||||
|
||||
/// Extends the operation with another operation if returning the new
|
||||
/// operation. Only operations of the same type can be used to extend.
|
||||
/// If the operations are of different types, returns None.
|
||||
pub fn is_left_joinable(&self) -> bool {
|
||||
let first_token = self.tokens().first();
|
||||
first_token.map_or(true, |t| t.get_is_left_joinable())
|
||||
}
|
||||
|
||||
pub fn is_right_joinable(&self) -> bool {
|
||||
let last_token = self.tokens().last();
|
||||
last_token.map_or(true, |t| t.get_is_right_joinable())
|
||||
}
|
||||
|
||||
/// Extends the operation with another operation when it returns Some
|
||||
/// operation. Only operations of the same type as self can be used to
|
||||
/// extend self. If the operations are of different types, returns None.
|
||||
pub fn extend(self, other: RawOperation<T>) -> Option<RawOperation<T>> {
|
||||
debug_assert!(
|
||||
std::mem::discriminant(&self) == std::mem::discriminant(&other),
|
||||
"Cannot extend operations of different types. This should have been handled before \
|
||||
calling this function."
|
||||
);
|
||||
|
||||
match (self, other) {
|
||||
(RawOperation::Insert(tokens1), RawOperation::Insert(tokens2)) => Some(
|
||||
RawOperation::Insert(tokens1.into_iter().chain(tokens2).collect()),
|
||||
|
|
@ -42,7 +58,7 @@ where
|
|||
(RawOperation::Equal(tokens1), RawOperation::Equal(tokens2)) => Some(
|
||||
RawOperation::Equal(tokens1.into_iter().chain(tokens2).collect()),
|
||||
),
|
||||
_ => None,
|
||||
_ => unreachable!("Only operations of the same type can be extended"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue