Retain labels is actually either a boolean or a number.

This commit is contained in:
Tom Alexander 2023-10-04 15:43:09 -04:00
parent 301a6db83e
commit bcade66e68
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
4 changed files with 61 additions and 12 deletions

View File

@ -77,6 +77,7 @@ use crate::types::RadioTarget;
use crate::types::RegularLink; use crate::types::RegularLink;
use crate::types::RepeaterType; use crate::types::RepeaterType;
use crate::types::RepeaterWarningDelayValueType; use crate::types::RepeaterWarningDelayValueType;
use crate::types::RetainLabels;
use crate::types::Section; use crate::types::Section;
use crate::types::SpecialBlock; use crate::types::SpecialBlock;
use crate::types::SrcBlock; use crate::types::SrcBlock;
@ -1625,13 +1626,45 @@ fn compare_example_block<'b, 's>(
} }
// Compare retain-labels // Compare retain-labels
let retain_labels: Option<CharOffsetInLine> = get_property_numeric(emacs, ":retain-labels")?; // retain-labels is t by default, nil if -r is set, or a number if -k is set.
if retain_labels != rust.retain_labels { let retain_labels = get_property_unquoted_atom(emacs, ":retain-labels")?;
this_status = DiffStatus::Bad; if let Some(retain_labels) = retain_labels {
message = Some(format!( if retain_labels == "t" {
"Retain labels mismatch (emacs != rust) {:?} != {:?}", match rust.retain_labels {
retain_labels, rust.retain_labels RetainLabels::Yes => {}
)); _ => {
this_status = DiffStatus::Bad;
message = Some(format!(
"Retain labels mismatch (emacs != rust) {:?} != {:?}",
retain_labels, rust.retain_labels
));
}
}
} else {
let retain_labels: CharOffsetInLine = get_property_numeric(emacs, ":retain-labels")?.expect("Cannot be None or else the earlier get_property_unquoted_atom would have been None.");
match (retain_labels, &rust.retain_labels) {
(e, RetainLabels::Keep(r)) if e == *r => {}
_ => {
this_status = DiffStatus::Bad;
message = Some(format!(
"Retain labels mismatch (emacs != rust) {:?} != {:?}",
retain_labels, rust.retain_labels
));
}
}
// foo
}
} else {
match rust.retain_labels {
RetainLabels::No => {}
_ => {
this_status = DiffStatus::Bad;
message = Some(format!(
"Retain labels mismatch (emacs != rust) {:?} != {:?}",
retain_labels, rust.retain_labels
));
}
}
} }
// Compare use-labels // Compare use-labels

View File

@ -38,6 +38,7 @@ use crate::types::ExportBlock;
use crate::types::LineNumber; use crate::types::LineNumber;
use crate::types::Object; use crate::types::Object;
use crate::types::PlainText; use crate::types::PlainText;
use crate::types::RetainLabels;
use crate::types::SrcBlock; use crate::types::SrcBlock;
use crate::types::SwitchNumberLines; use crate::types::SwitchNumberLines;
use crate::types::VerseBlock; use crate::types::VerseBlock;
@ -168,7 +169,7 @@ pub(crate) fn example_block<'b, 'g, 'r, 's>(
parameters.label_format, parameters.label_format,
) )
} else { } else {
(None, None, None, true, None) (None, None, RetainLabels::Yes, true, None)
} }
}; };
Ok(( Ok((
@ -331,7 +332,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: Option<CharOffsetInLine>, retain_labels: RetainLabels,
use_labels: bool, use_labels: bool,
label_format: Option<&'s str>, label_format: Option<&'s str>,
} }
@ -347,7 +348,7 @@ 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 = None; let mut retain_labels = RetainLabels::Yes;
let mut use_labels = true; let mut use_labels = true;
let mut label_format = None; let mut label_format = None;
let (remaining, (source, (words, _))) = let (remaining, (source, (words, _))) =
@ -366,6 +367,12 @@ fn example_switches<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, ExampleSwitc
} }
(SwitchState::Normal, "-r") => { (SwitchState::Normal, "-r") => {
use_labels = false; use_labels = false;
match retain_labels {
RetainLabels::Yes => {
retain_labels = RetainLabels::No;
}
_ => {}
}
} }
(SwitchState::Normal, "-l") => { (SwitchState::Normal, "-l") => {
state = SwitchState::LabelFormat; state = SwitchState::LabelFormat;
@ -375,7 +382,7 @@ fn example_switches<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, ExampleSwitc
let character_offset = Into::<&str>::into(text_until_flag).chars().count(); let character_offset = Into::<&str>::into(text_until_flag).chars().count();
let character_offset = CharOffsetInLine::try_from(character_offset) let character_offset = CharOffsetInLine::try_from(character_offset)
.expect("Character offset should fit in CharOffsetInLine"); .expect("Character offset should fit in CharOffsetInLine");
retain_labels = Some(character_offset); retain_labels = RetainLabels::Keep(character_offset);
} }
(SwitchState::NewLineNumber, _) => { (SwitchState::NewLineNumber, _) => {
let val = normalized_word.parse::<LineNumber>(); let val = normalized_word.parse::<LineNumber>();

View File

@ -38,13 +38,21 @@ pub struct CommentBlock<'s> {
pub type CharOffsetInLine = u16; pub type CharOffsetInLine = u16;
#[derive(Debug)]
pub enum RetainLabels {
No,
Yes,
/// When adding -k to the switches on an example or src block, the labels are kept in the source code and links will use line numbers.
Keep(CharOffsetInLine),
}
#[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: Option<CharOffsetInLine>, pub retain_labels: RetainLabels,
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

@ -51,6 +51,7 @@ pub use lesser_element::LatexEnvironment;
pub use lesser_element::LineNumber; pub use lesser_element::LineNumber;
pub use lesser_element::Paragraph; pub use lesser_element::Paragraph;
pub use lesser_element::Planning; pub use lesser_element::Planning;
pub use lesser_element::RetainLabels;
pub use lesser_element::SrcBlock; pub use lesser_element::SrcBlock;
pub use lesser_element::SwitchNumberLines; pub use lesser_element::SwitchNumberLines;
pub use lesser_element::TableCell; pub use lesser_element::TableCell;