Add left/right joinability for tokens
This commit is contained in:
parent
b0c6c082a1
commit
b230d34b88
6 changed files with 312 additions and 74 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"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,8 @@ snapshot_kind: text
|
|||
Token {
|
||||
normalised: "a",
|
||||
original: "a",
|
||||
is_left_joinable: true,
|
||||
is_right_joinable: true,
|
||||
},
|
||||
],
|
||||
),
|
||||
|
|
@ -17,6 +19,8 @@ snapshot_kind: text
|
|||
Token {
|
||||
normalised: "x",
|
||||
original: "x",
|
||||
is_left_joinable: true,
|
||||
is_right_joinable: true,
|
||||
},
|
||||
],
|
||||
),
|
||||
|
|
@ -25,6 +29,8 @@ snapshot_kind: text
|
|||
Token {
|
||||
normalised: "b",
|
||||
original: "b",
|
||||
is_left_joinable: true,
|
||||
is_right_joinable: true,
|
||||
},
|
||||
],
|
||||
),
|
||||
|
|
@ -33,6 +39,8 @@ snapshot_kind: text
|
|||
Token {
|
||||
normalised: "c",
|
||||
original: "c",
|
||||
is_left_joinable: true,
|
||||
is_right_joinable: true,
|
||||
},
|
||||
],
|
||||
),
|
||||
|
|
@ -41,6 +49,8 @@ snapshot_kind: text
|
|||
Token {
|
||||
normalised: "y",
|
||||
original: "y",
|
||||
is_left_joinable: true,
|
||||
is_right_joinable: true,
|
||||
},
|
||||
],
|
||||
),
|
||||
|
|
@ -49,6 +59,8 @@ snapshot_kind: text
|
|||
Token {
|
||||
normalised: "d",
|
||||
original: "d",
|
||||
is_left_joinable: true,
|
||||
is_right_joinable: true,
|
||||
},
|
||||
],
|
||||
),
|
||||
|
|
|
|||
|
|
@ -9,10 +9,14 @@ snapshot_kind: text
|
|||
Token {
|
||||
normalised: "a",
|
||||
original: "a",
|
||||
is_left_joinable: true,
|
||||
is_right_joinable: true,
|
||||
},
|
||||
Token {
|
||||
normalised: "b",
|
||||
original: "b",
|
||||
is_left_joinable: true,
|
||||
is_right_joinable: true,
|
||||
},
|
||||
],
|
||||
),
|
||||
|
|
|
|||
|
|
@ -9,14 +9,20 @@ snapshot_kind: text
|
|||
Token {
|
||||
normalised: "a",
|
||||
original: "a",
|
||||
is_left_joinable: true,
|
||||
is_right_joinable: true,
|
||||
},
|
||||
Token {
|
||||
normalised: "b",
|
||||
original: "b",
|
||||
is_left_joinable: true,
|
||||
is_right_joinable: true,
|
||||
},
|
||||
Token {
|
||||
normalised: "c",
|
||||
original: "c",
|
||||
is_left_joinable: true,
|
||||
is_right_joinable: true,
|
||||
},
|
||||
],
|
||||
),
|
||||
|
|
|
|||
|
|
@ -9,10 +9,14 @@ snapshot_kind: text
|
|||
Token {
|
||||
normalised: "a",
|
||||
original: "a",
|
||||
is_left_joinable: true,
|
||||
is_right_joinable: true,
|
||||
},
|
||||
Token {
|
||||
normalised: "b",
|
||||
original: "b",
|
||||
is_left_joinable: true,
|
||||
is_right_joinable: true,
|
||||
},
|
||||
],
|
||||
),
|
||||
|
|
|
|||
|
|
@ -9,6 +9,8 @@ snapshot_kind: text
|
|||
Token {
|
||||
normalised: "a",
|
||||
original: "a",
|
||||
is_left_joinable: true,
|
||||
is_right_joinable: true,
|
||||
},
|
||||
],
|
||||
),
|
||||
|
|
@ -17,10 +19,14 @@ snapshot_kind: text
|
|||
Token {
|
||||
normalised: "b",
|
||||
original: "b",
|
||||
is_left_joinable: true,
|
||||
is_right_joinable: true,
|
||||
},
|
||||
Token {
|
||||
normalised: "c",
|
||||
original: "c",
|
||||
is_left_joinable: true,
|
||||
is_right_joinable: true,
|
||||
},
|
||||
],
|
||||
),
|
||||
|
|
@ -29,6 +35,8 @@ snapshot_kind: text
|
|||
Token {
|
||||
normalised: "x",
|
||||
original: "x",
|
||||
is_left_joinable: true,
|
||||
is_right_joinable: true,
|
||||
},
|
||||
],
|
||||
),
|
||||
|
|
@ -37,6 +45,8 @@ snapshot_kind: text
|
|||
Token {
|
||||
normalised: "d",
|
||||
original: "d",
|
||||
is_left_joinable: true,
|
||||
is_right_joinable: true,
|
||||
},
|
||||
],
|
||||
),
|
||||
|
|
|
|||
Loading…
Reference in a new issue