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
5 changed files with 39 additions and 17 deletions

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, _) => {}