Fix retain labels.

This is a numeric value based on the character offset of -k from the beginning of the switches.
This commit is contained in:
Tom Alexander 2023-10-04 15:20:57 -04:00
parent 32da06776c
commit 301a6db83e
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
5 changed files with 39 additions and 17 deletions

View File

@ -1,3 +1,15 @@
#+BEGIN_SRC elisp -n -r -l "((%s))" #+BEGIN_EXAMPLE elisp -n -r -l "((%s))"
foo 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

View File

@ -18,6 +18,7 @@ use crate::types::AstNode;
use crate::types::BabelCall; use crate::types::BabelCall;
use crate::types::Bold; use crate::types::Bold;
use crate::types::CenterBlock; use crate::types::CenterBlock;
use crate::types::CharOffsetInLine;
use crate::types::CheckboxType; use crate::types::CheckboxType;
use crate::types::Citation; use crate::types::Citation;
use crate::types::CitationReference; use crate::types::CitationReference;
@ -1543,7 +1544,7 @@ fn compare_example_block<'b, 's>(
_source: &'s str, _source: &'s str,
emacs: &'b Token<'s>, emacs: &'b Token<'s>,
rust: &'b ExampleBlock<'s>, rust: &'b ExampleBlock<'s>,
) -> Result<DiffEntry<'b, 's>, Box<dyn std::error::Error>> { ) -> Result<DiffEntry<'b, 's>, Box<dyn std::error::Error + 's>> {
let mut this_status = DiffStatus::Good; let mut this_status = DiffStatus::Good;
let mut message = None; let mut message = None;
@ -1624,7 +1625,7 @@ fn compare_example_block<'b, 's>(
} }
// Compare retain-labels // Compare retain-labels
let retain_labels = get_property_boolean(emacs, ":retain-labels")?; let retain_labels: Option<CharOffsetInLine> = get_property_numeric(emacs, ":retain-labels")?;
if retain_labels != rust.retain_labels { if retain_labels != rust.retain_labels {
this_status = DiffStatus::Bad; this_status = DiffStatus::Bad;
message = Some(format!( message = Some(format!(

View File

@ -31,6 +31,7 @@ use crate::parser::util::exit_matcher_parser;
use crate::parser::util::get_consumed; use crate::parser::util::get_consumed;
use crate::parser::util::start_of_line; use crate::parser::util::start_of_line;
use crate::parser::util::text_until_exit; use crate::parser::util::text_until_exit;
use crate::types::CharOffsetInLine;
use crate::types::CommentBlock; use crate::types::CommentBlock;
use crate::types::ExampleBlock; use crate::types::ExampleBlock;
use crate::types::ExportBlock; use crate::types::ExportBlock;
@ -167,7 +168,7 @@ pub(crate) fn example_block<'b, 'g, 'r, 's>(
parameters.label_format, parameters.label_format,
) )
} else { } else {
(None, None, true, true, None) (None, None, None, true, None)
} }
}; };
Ok(( Ok((
@ -330,7 +331,7 @@ fn _lesser_block_begin<'b, 'g, 'r, 's, 'c>(
struct ExampleSwitches<'s> { struct ExampleSwitches<'s> {
source: &'s str, source: &'s str,
number_lines: Option<SwitchNumberLines>, number_lines: Option<SwitchNumberLines>,
retain_labels: bool, retain_labels: Option<CharOffsetInLine>,
use_labels: bool, use_labels: bool,
label_format: Option<&'s str>, label_format: Option<&'s str>,
} }
@ -346,18 +347,17 @@ enum SwitchState {
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn example_switches<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, ExampleSwitches<'s>> { fn example_switches<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, ExampleSwitches<'s>> {
let mut number_lines = None; let mut number_lines = None;
let mut retain_labels = true; let mut retain_labels = None;
let mut use_labels = true; let mut use_labels = true;
let mut label_format = None; let mut label_format = None;
let (remaining, (source, (words, _))) = consumed(tuple(( let (remaining, (source, (words, _))) =
separated_list1(space1, map(switch_word, |val| Into::<&str>::into(val))), consumed(tuple((separated_list1(space1, switch_word), space0)))(input)?;
space0,
)))(input)?;
let mut state = SwitchState::Normal; let mut state = SwitchState::Normal;
for word in words { for word in words {
let normalized_word = Into::<&str>::into(word);
loop { loop {
match (&state, word) { match (&state, normalized_word) {
(SwitchState::Normal, "-n") => { (SwitchState::Normal, "-n") => {
state = SwitchState::NewLineNumber; state = SwitchState::NewLineNumber;
} }
@ -365,14 +365,20 @@ fn example_switches<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, ExampleSwitc
state = SwitchState::ContinuedLineNumber; state = SwitchState::ContinuedLineNumber;
} }
(SwitchState::Normal, "-r") => { (SwitchState::Normal, "-r") => {
retain_labels = false;
use_labels = false; use_labels = false;
} }
(SwitchState::Normal, "-l") => { (SwitchState::Normal, "-l") => {
state = SwitchState::LabelFormat; 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, _) => { (SwitchState::NewLineNumber, _) => {
let val = word.parse::<LineNumber>(); let val = normalized_word.parse::<LineNumber>();
if let Ok(val) = val { if let Ok(val) = val {
if val < 0 { if val < 0 {
number_lines = Some(SwitchNumberLines::New(0)); number_lines = Some(SwitchNumberLines::New(0));
@ -388,7 +394,7 @@ fn example_switches<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, ExampleSwitc
} }
} }
(SwitchState::ContinuedLineNumber, _) => { (SwitchState::ContinuedLineNumber, _) => {
let val = word.parse::<LineNumber>(); let val = normalized_word.parse::<LineNumber>();
if let Ok(val) = val { if let Ok(val) = val {
if val < 0 { if val < 0 {
number_lines = Some(SwitchNumberLines::Continued(0)); number_lines = Some(SwitchNumberLines::Continued(0));
@ -404,7 +410,7 @@ fn example_switches<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, ExampleSwitc
} }
} }
(SwitchState::LabelFormat, _) => { (SwitchState::LabelFormat, _) => {
label_format = Some(word); label_format = Some(normalized_word);
state = SwitchState::Normal; state = SwitchState::Normal;
} }
(SwitchState::Normal, _) => {} (SwitchState::Normal, _) => {}

View File

@ -36,13 +36,15 @@ pub struct CommentBlock<'s> {
pub contents: &'s str, pub contents: &'s str,
} }
pub type CharOffsetInLine = u16;
#[derive(Debug)] #[derive(Debug)]
pub struct ExampleBlock<'s> { pub struct ExampleBlock<'s> {
pub source: &'s str, pub source: &'s str,
pub name: &'s str, pub name: &'s str,
pub switches: Option<&'s str>, pub switches: Option<&'s str>,
pub number_lines: Option<SwitchNumberLines>, pub number_lines: Option<SwitchNumberLines>,
pub retain_labels: bool, pub retain_labels: Option<CharOffsetInLine>,
pub use_labels: bool, pub use_labels: bool,
pub label_format: Option<&'s str>, pub label_format: Option<&'s str>,
pub contents: String, pub contents: String,

View File

@ -37,6 +37,7 @@ pub use greater_element::Table;
pub use greater_element::TableRow; pub use greater_element::TableRow;
pub use greater_element::TableRowType; pub use greater_element::TableRowType;
pub use lesser_element::BabelCall; pub use lesser_element::BabelCall;
pub use lesser_element::CharOffsetInLine;
pub use lesser_element::Clock; pub use lesser_element::Clock;
pub use lesser_element::Comment; pub use lesser_element::Comment;
pub use lesser_element::CommentBlock; pub use lesser_element::CommentBlock;