Add left/right joinability for tokens

This commit is contained in:
Andras Schmelczer 2025-04-05 13:48:02 +01:00
parent b0c6c082a1
commit b230d34b88
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
13 changed files with 313 additions and 75 deletions

View file

@ -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"),
}
}
}