From 301a6db83e008dd9b44843df5618f49396becc6f Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Wed, 4 Oct 2023 15:20:57 -0400 Subject: [PATCH] Fix retain labels. This is a numeric value based on the character offset of -k from the beginning of the switches. --- .../example/switches_with_label.org | 16 ++++++++-- src/compare/diff.rs | 5 ++-- src/parser/lesser_block.rs | 30 +++++++++++-------- src/types/lesser_element.rs | 4 ++- src/types/mod.rs | 1 + 5 files changed, 39 insertions(+), 17 deletions(-) diff --git a/org_mode_samples/lesser_element/lesser_block/example/switches_with_label.org b/org_mode_samples/lesser_element/lesser_block/example/switches_with_label.org index a450ab8b..57b01a21 100644 --- a/org_mode_samples/lesser_element/lesser_block/example/switches_with_label.org +++ b/org_mode_samples/lesser_element/lesser_block/example/switches_with_label.org @@ -1,3 +1,15 @@ -#+BEGIN_SRC elisp -n -r -l "((%s))" +#+BEGIN_EXAMPLE elisp -n -r -l "((%s))" foo -#+END_SRC +#+END_EXAMPLE + +#+BEGIN_EXAMPLE elisp -k -n -r -l "((%s))" + foo +#+END_EXAMPLE + +#+BEGIN_EXAMPLE elisp -k 8 -n -r -l "((%s))" + foo +#+END_EXAMPLE + +#+BEGIN_EXAMPLE elisp -n -r -k -l "((%s))" + foo +#+END_EXAMPLE diff --git a/src/compare/diff.rs b/src/compare/diff.rs index 6b5487f1..87dd403c 100644 --- a/src/compare/diff.rs +++ b/src/compare/diff.rs @@ -18,6 +18,7 @@ use crate::types::AstNode; use crate::types::BabelCall; use crate::types::Bold; use crate::types::CenterBlock; +use crate::types::CharOffsetInLine; use crate::types::CheckboxType; use crate::types::Citation; use crate::types::CitationReference; @@ -1543,7 +1544,7 @@ fn compare_example_block<'b, 's>( _source: &'s str, emacs: &'b Token<'s>, rust: &'b ExampleBlock<'s>, -) -> Result, Box> { +) -> Result, Box> { let mut this_status = DiffStatus::Good; let mut message = None; @@ -1624,7 +1625,7 @@ fn compare_example_block<'b, 's>( } // Compare retain-labels - let retain_labels = get_property_boolean(emacs, ":retain-labels")?; + let retain_labels: Option = get_property_numeric(emacs, ":retain-labels")?; if retain_labels != rust.retain_labels { this_status = DiffStatus::Bad; message = Some(format!( diff --git a/src/parser/lesser_block.rs b/src/parser/lesser_block.rs index 0dde4a0d..c225b4e2 100644 --- a/src/parser/lesser_block.rs +++ b/src/parser/lesser_block.rs @@ -31,6 +31,7 @@ use crate::parser::util::exit_matcher_parser; use crate::parser::util::get_consumed; use crate::parser::util::start_of_line; use crate::parser::util::text_until_exit; +use crate::types::CharOffsetInLine; use crate::types::CommentBlock; use crate::types::ExampleBlock; use crate::types::ExportBlock; @@ -167,7 +168,7 @@ pub(crate) fn example_block<'b, 'g, 'r, 's>( parameters.label_format, ) } else { - (None, None, true, true, None) + (None, None, None, true, None) } }; Ok(( @@ -330,7 +331,7 @@ fn _lesser_block_begin<'b, 'g, 'r, 's, 'c>( struct ExampleSwitches<'s> { source: &'s str, number_lines: Option, - retain_labels: bool, + retain_labels: Option, use_labels: bool, label_format: Option<&'s str>, } @@ -346,18 +347,17 @@ enum SwitchState { #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn example_switches<'s>(input: OrgSource<'s>) -> Res, ExampleSwitches<'s>> { let mut number_lines = None; - let mut retain_labels = true; + let mut retain_labels = None; let mut use_labels = true; let mut label_format = None; - let (remaining, (source, (words, _))) = consumed(tuple(( - separated_list1(space1, map(switch_word, |val| Into::<&str>::into(val))), - space0, - )))(input)?; + let (remaining, (source, (words, _))) = + consumed(tuple((separated_list1(space1, switch_word), space0)))(input)?; let mut state = SwitchState::Normal; for word in words { + let normalized_word = Into::<&str>::into(word); loop { - match (&state, word) { + match (&state, normalized_word) { (SwitchState::Normal, "-n") => { state = SwitchState::NewLineNumber; } @@ -365,14 +365,20 @@ fn example_switches<'s>(input: OrgSource<'s>) -> Res, ExampleSwitc state = SwitchState::ContinuedLineNumber; } (SwitchState::Normal, "-r") => { - retain_labels = false; use_labels = false; } (SwitchState::Normal, "-l") => { state = SwitchState::LabelFormat; } + (SwitchState::Normal, "-k") => { + let text_until_flag = input.get_until(word); + let character_offset = Into::<&str>::into(text_until_flag).chars().count(); + let character_offset = CharOffsetInLine::try_from(character_offset) + .expect("Character offset should fit in CharOffsetInLine"); + retain_labels = Some(character_offset); + } (SwitchState::NewLineNumber, _) => { - let val = word.parse::(); + let val = normalized_word.parse::(); if let Ok(val) = val { if val < 0 { number_lines = Some(SwitchNumberLines::New(0)); @@ -388,7 +394,7 @@ fn example_switches<'s>(input: OrgSource<'s>) -> Res, ExampleSwitc } } (SwitchState::ContinuedLineNumber, _) => { - let val = word.parse::(); + let val = normalized_word.parse::(); if let Ok(val) = val { if val < 0 { number_lines = Some(SwitchNumberLines::Continued(0)); @@ -404,7 +410,7 @@ fn example_switches<'s>(input: OrgSource<'s>) -> Res, ExampleSwitc } } (SwitchState::LabelFormat, _) => { - label_format = Some(word); + label_format = Some(normalized_word); state = SwitchState::Normal; } (SwitchState::Normal, _) => {} diff --git a/src/types/lesser_element.rs b/src/types/lesser_element.rs index 5f1f29c8..b0c261d0 100644 --- a/src/types/lesser_element.rs +++ b/src/types/lesser_element.rs @@ -36,13 +36,15 @@ pub struct CommentBlock<'s> { pub contents: &'s str, } +pub type CharOffsetInLine = u16; + #[derive(Debug)] pub struct ExampleBlock<'s> { pub source: &'s str, pub name: &'s str, pub switches: Option<&'s str>, pub number_lines: Option, - pub retain_labels: bool, + pub retain_labels: Option, pub use_labels: bool, pub label_format: Option<&'s str>, pub contents: String, diff --git a/src/types/mod.rs b/src/types/mod.rs index e5177957..4072ae5a 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -37,6 +37,7 @@ pub use greater_element::Table; pub use greater_element::TableRow; pub use greater_element::TableRowType; pub use lesser_element::BabelCall; +pub use lesser_element::CharOffsetInLine; pub use lesser_element::Clock; pub use lesser_element::Comment; pub use lesser_element::CommentBlock;