Fix handling of start of line in OrgSource.
rust-test Build rust-test has succeeded Details
rust-build Build rust-build has succeeded Details

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

View File

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