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
#+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::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<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 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<CharOffsetInLine> = get_property_numeric(emacs, ":retain-labels")?;
if retain_labels != rust.retain_labels {
this_status = DiffStatus::Bad;
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::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<SwitchNumberLines>,
retain_labels: bool,
retain_labels: Option<CharOffsetInLine>,
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<OrgSource<'s>, 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<OrgSource<'s>, 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::<LineNumber>();
let val = normalized_word.parse::<LineNumber>();
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<OrgSource<'s>, ExampleSwitc
}
}
(SwitchState::ContinuedLineNumber, _) => {
let val = word.parse::<LineNumber>();
let val = normalized_word.parse::<LineNumber>();
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<OrgSource<'s>, ExampleSwitc
}
}
(SwitchState::LabelFormat, _) => {
label_format = Some(word);
label_format = Some(normalized_word);
state = SwitchState::Normal;
}
(SwitchState::Normal, _) => {}

View File

@ -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<SwitchNumberLines>,
pub retain_labels: bool,
pub retain_labels: Option<CharOffsetInLine>,
pub use_labels: bool,
pub label_format: Option<&'s str>,
pub contents: String,

View File

@ -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;