From 32071ce74d0bd1480d01590e9e50b6c7f08659ce Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Thu, 24 Aug 2023 17:23:16 -0400 Subject: [PATCH] Fix handling of start of line in OrgSource. --- src/parser/inline_source_block.rs | 4 ++-- src/parser/org_source.rs | 33 +++++++++++++++---------------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/parser/inline_source_block.rs b/src/parser/inline_source_block.rs index 0d7bef7..304894c 100644 --- a/src/parser/inline_source_block.rs +++ b/src/parser/inline_source_block.rs @@ -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")] diff --git a/src/parser/org_source.rs b/src/parser/org_source.rs index 1aecc01..3562944 100644 --- a/src/parser/org_source.rs +++ b/src/parser/org_source.rs @@ -16,7 +16,7 @@ pub struct OrgSource<'s> { full_source: &'s str, start: usize, end: usize, // exclusive - preceding_line_break: Option, + start_of_line: usize, preceding_character: Option, } @@ -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('💛')); } - }