Fix handling of start of line in OrgSource.
All checks were successful
rust-test Build rust-test has succeeded
rust-build Build rust-build has succeeded

This commit is contained in:
Tom Alexander 2023-08-24 17:23:16 -04:00
parent e84e2b5147
commit 32071ce74d
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 18 additions and 19 deletions

View File

@ -156,7 +156,7 @@ fn body<'r, 's>(
let span = span!(
tracing::Level::DEBUG,
"outside end body",
remaining = remaining
remaining = Into::<&str>::into(remaining)
);
#[cfg(feature = "tracing")]
let _enter = span.enter();
@ -193,7 +193,7 @@ fn body_end<'r, 's>(
let span = span!(
tracing::Level::DEBUG,
"inside end body",
remaining = input,
remaining = Into::<&str>::into(input),
current_depth = current_depth
);
#[cfg(feature = "tracing")]

View File

@ -16,7 +16,7 @@ pub struct OrgSource<'s> {
full_source: &'s str,
start: usize,
end: usize, // exclusive
preceding_line_break: Option<usize>,
start_of_line: usize,
preceding_character: Option<char>,
}
@ -29,15 +29,14 @@ impl<'s> OrgSource<'s> {
full_source: input,
start: 0,
end: input.len(),
preceding_line_break: None,
start_of_line: 0,
preceding_character: None,
}
}
/// Get the text since the line break preceding the start of this WrappedInput.
pub fn text_since_line_break(&self) -> &'s str {
let start = self.preceding_line_break.unwrap_or(0);
&self.full_source[start..self.start]
&self.full_source[self.start_of_line..self.start]
}
pub fn len(&self) -> usize {
@ -49,10 +48,7 @@ impl<'s> OrgSource<'s> {
}
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,
}
self.start == self.start_of_line
}
}
@ -117,17 +113,18 @@ where
}
let skipped_text = &self.full_source[self.start..new_start];
let last_line_feed = skipped_text
let start_of_line = skipped_text
.rfind('\n')
.map(|idx| idx + self.preceding_line_break.unwrap_or(0) + 1);
.map(|idx| self.start + idx + 1)
.unwrap_or(self.start_of_line);
// TODO: calculate updated values for WrappedInput
OrgSource {
full_source: self.full_source,
start: new_start,
end: new_end,
preceding_line_break: last_line_feed,
preceding_character: skipped_text.chars().last()
start_of_line,
preceding_character: skipped_text.chars().last(),
}
}
}
@ -326,9 +323,9 @@ mod tests {
#[test]
fn line_break() {
let input = OrgSource::new("lorem\nfoo\nbar\nbaz\nipsum");
assert_eq!(input.slice(5..).preceding_line_break, None);
assert_eq!(input.slice(6..).preceding_line_break, Some(6));
assert_eq!(input.slice(6..).slice(10..).preceding_line_break, Some(14));
assert_eq!(input.slice(5..).start_of_line, 0);
assert_eq!(input.slice(6..).start_of_line, 6);
assert_eq!(input.slice(6..).slice(10..).start_of_line, 14);
}
#[test]
@ -346,7 +343,10 @@ mod tests {
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'));
assert_eq!(
input.slice(6..).slice(10..).get_preceding_character(),
Some('a')
);
}
#[test]
@ -364,5 +364,4 @@ mod tests {
assert_eq!(input.get_preceding_character(), None);
assert_eq!(input.slice(8..).get_preceding_character(), Some('💛'));
}
}