Update getting the previous character and previous line.

This can be done a lot more efficiently now that we are keeping track of this information in the wrapped input type instead of having to fetch to the original document out of the context tree.
This commit is contained in:
Tom Alexander
2023-08-24 16:55:56 -04:00
parent dab598e5e7
commit 720afa5d32
9 changed files with 59 additions and 114 deletions

View File

@@ -17,6 +17,7 @@ pub struct OrgSource<'s> {
start: usize,
end: usize, // exclusive
preceding_line_break: Option<usize>,
preceding_character: Option<char>,
}
impl<'s> OrgSource<'s> {
@@ -29,6 +30,7 @@ impl<'s> OrgSource<'s> {
start: 0,
end: input.len(),
preceding_line_break: None,
preceding_character: None,
}
}
@@ -41,6 +43,17 @@ impl<'s> OrgSource<'s> {
pub fn len(&self) -> usize {
self.end - self.start
}
pub fn get_preceding_character(&self) -> Option<char> {
todo!()
}
pub fn is_at_start_of_line(&self) -> bool {
self.start == 0 || match self.preceding_line_break {
Some(offset) => offset == self.start - 1,
None => false,
}
}
}
impl<'s> InputTake for OrgSource<'s> {
@@ -114,6 +127,7 @@ where
start: new_start,
end: new_end,
preceding_line_break: last_line_feed,
preceding_character: skipped_text.chars().last()
}
}
}
@@ -325,4 +339,22 @@ mod tests {
assert_eq!(input.slice(6..).text_since_line_break(), "");
assert_eq!(input.slice(6..).slice(10..).text_since_line_break(), "ba");
}
#[test]
fn preceding_character() {
let input = OrgSource::new("lorem\nfoo\nbar\nbaz\nipsum");
assert_eq!(input.get_preceding_character(), None);
assert_eq!(input.slice(5..).get_preceding_character(), Some('m'));
assert_eq!(input.slice(6..).get_preceding_character(), Some('\n'));
assert_eq!(input.slice(6..).slice(10..).get_preceding_character(), Some('a'));
}
#[test]
fn is_at_start_of_line() {
let input = OrgSource::new("lorem\nfoo\nbar\nbaz\nipsum");
assert_eq!(input.is_at_start_of_line(), true);
assert_eq!(input.slice(5..).is_at_start_of_line(), false);
assert_eq!(input.slice(6..).is_at_start_of_line(), true);
assert_eq!(input.slice(6..).slice(10..).is_at_start_of_line(), false);
}
}