Add real handling for preserve indent.

Now that I know which flag changes this setting, we can handle it properly.
This commit is contained in:
Tom Alexander 2023-10-04 19:23:47 -04:00
parent e1e4ac75e4
commit 87ac18e6b2
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 46 additions and 21 deletions

View File

@ -1618,11 +1618,14 @@ fn compare_example_block<'b, 's>(
}; };
// Compare preserve-indent // Compare preserve-indent
let preserve_indent = get_property_boolean(emacs, ":preserve-indent")?; let preserve_indent: Option<CharOffsetInLine> =
if preserve_indent { get_property_numeric(emacs, ":preserve-indent")?;
// I don't know what :preserve-indent is for, but it seems to always be nil. This is here to alert me to value being non-nil so I can investigate. if preserve_indent != rust.preserve_indent {
this_status = DiffStatus::Bad; this_status = DiffStatus::Bad;
message = Some(format!("Non-nil :preserve-indent {:?}", preserve_indent)); message = Some(format!(
"Prserve indent mismatch (emacs != rust) {:?} != {:?}",
preserve_indent, rust.preserve_indent
));
} }
// Compare retain-labels // Compare retain-labels
@ -1731,8 +1734,6 @@ fn compare_src_block<'b, 's>(
let mut this_status = DiffStatus::Good; let mut this_status = DiffStatus::Good;
let mut message = None; let mut message = None;
// TODO: Compare :parameters
// Compare language // Compare language
let language = get_property_quoted_string(emacs, ":language")?; let language = get_property_quoted_string(emacs, ":language")?;
if language.as_ref().map(String::as_str) != rust.language { if language.as_ref().map(String::as_str) != rust.language {
@ -1771,7 +1772,7 @@ fn compare_src_block<'b, 's>(
} }
} }
// Compare switches // Compare parameters
let parameters = get_property_quoted_string(emacs, ":parameters")?; let parameters = get_property_quoted_string(emacs, ":parameters")?;
match (parameters.as_ref().map(String::as_str), rust.parameters) { match (parameters.as_ref().map(String::as_str), rust.parameters) {
(None, None) => {} (None, None) => {}
@ -1830,11 +1831,14 @@ fn compare_src_block<'b, 's>(
}; };
// Compare preserve-indent // Compare preserve-indent
let preserve_indent = get_property_boolean(emacs, ":preserve-indent")?; let preserve_indent: Option<CharOffsetInLine> =
if preserve_indent { get_property_numeric(emacs, ":preserve-indent")?;
// I don't know what :preserve-indent is for, but it seems to always be nil. This is here to alert me to value being non-nil so I can investigate. if preserve_indent != rust.preserve_indent {
this_status = DiffStatus::Bad; this_status = DiffStatus::Bad;
message = Some(format!("Non-nil :preserve-indent {:?}", preserve_indent)); message = Some(format!(
"Prserve indent mismatch (emacs != rust) {:?} != {:?}",
preserve_indent, rust.preserve_indent
));
} }
// Compare retain-labels // Compare retain-labels

View File

@ -160,7 +160,7 @@ pub(crate) fn example_block<'b, 'g, 'r, 's>(
let (remaining, _end) = lesser_block_end_specialized(&parser_context, remaining)?; let (remaining, _end) = lesser_block_end_specialized(&parser_context, remaining)?;
let source = get_consumed(input, remaining); let source = get_consumed(input, remaining);
let (switches, number_lines, retain_labels, use_labels, label_format) = { let (switches, number_lines, preserve_indent, retain_labels, use_labels, label_format) = {
if let Some(parameters) = parameters { if let Some(parameters) = parameters {
( (
if parameters.source.len() == 0 { if parameters.source.len() == 0 {
@ -169,12 +169,13 @@ pub(crate) fn example_block<'b, 'g, 'r, 's>(
Some(parameters.source) Some(parameters.source)
}, },
parameters.number_lines, parameters.number_lines,
parameters.preserve_indent,
parameters.retain_labels, parameters.retain_labels,
parameters.use_labels, parameters.use_labels,
parameters.label_format, parameters.label_format,
) )
} else { } else {
(None, None, RetainLabels::Yes, true, None) (None, None, None, RetainLabels::Yes, true, None)
} }
}; };
Ok(( Ok((
@ -183,6 +184,7 @@ pub(crate) fn example_block<'b, 'g, 'r, 's>(
source: source.into(), source: source.into(),
switches, switches,
number_lines, number_lines,
preserve_indent,
retain_labels, retain_labels,
use_labels, use_labels,
label_format, label_format,
@ -242,8 +244,7 @@ pub(crate) fn src_block<'b, 'g, 'r, 's>(
let (remaining, language) = opt(map(tuple((space1, switch_word(true))), |(_, language)| { let (remaining, language) = opt(map(tuple((space1, switch_word(true))), |(_, language)| {
language language
}))(remaining)?; }))(remaining)?;
let (remaining, switches) = let (remaining, switches) = opt(src_switches)(remaining)?;
opt(map(tuple((space1, src_switches)), |(_, switches)| switches))(remaining)?;
let (remaining, parameters) = opt(map(tuple((space1, src_parameters)), |(_, parameters)| { let (remaining, parameters) = opt(map(tuple((space1, src_parameters)), |(_, parameters)| {
parameters parameters
}))(remaining)?; }))(remaining)?;
@ -264,7 +265,7 @@ pub(crate) fn src_block<'b, 'g, 'r, 's>(
let (remaining, _end) = lesser_block_end_specialized(&parser_context, remaining)?; let (remaining, _end) = lesser_block_end_specialized(&parser_context, remaining)?;
let source = get_consumed(input, remaining); let source = get_consumed(input, remaining);
let (switches, number_lines, retain_labels, use_labels, label_format) = { let (switches, number_lines, preserve_indent, retain_labels, use_labels, label_format) = {
if let Some(switches) = switches { if let Some(switches) = switches {
( (
if switches.source.len() == 0 { if switches.source.len() == 0 {
@ -273,12 +274,13 @@ pub(crate) fn src_block<'b, 'g, 'r, 's>(
Some(switches.source) Some(switches.source)
}, },
switches.number_lines, switches.number_lines,
switches.preserve_indent,
switches.retain_labels, switches.retain_labels,
switches.use_labels, switches.use_labels,
switches.label_format, switches.label_format,
) )
} else { } else {
(None, None, RetainLabels::Yes, true, None) (None, None, None, RetainLabels::Yes, true, None)
} }
}; };
Ok(( Ok((
@ -289,6 +291,7 @@ pub(crate) fn src_block<'b, 'g, 'r, 's>(
switches, switches,
parameters: parameters.map(Into::<&str>::into), parameters: parameters.map(Into::<&str>::into),
number_lines, number_lines,
preserve_indent,
retain_labels, retain_labels,
use_labels, use_labels,
label_format, label_format,
@ -361,6 +364,7 @@ struct ExampleSrcSwitches<'s> {
source: &'s str, source: &'s str,
number_lines: Option<SwitchNumberLines>, number_lines: Option<SwitchNumberLines>,
retain_labels: RetainLabels, retain_labels: RetainLabels,
preserve_indent: Option<CharOffsetInLine>,
use_labels: bool, use_labels: bool,
label_format: Option<&'s str>, label_format: Option<&'s str>,
} }
@ -380,28 +384,35 @@ fn src_parameters<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>>
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn src_switches<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, ExampleSrcSwitches<'s>> { fn src_switches<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, ExampleSrcSwitches<'s>> {
example_src_switches(true)(input) let (remaining, leading_spaces) = space1(input)?;
let offset = Into::<&str>::into(leading_spaces).chars().count();
let offset = CharOffsetInLine::try_from(offset)
.expect("Character offset should fit in CharOffsetInLine");
example_src_switches(true, offset)(remaining)
} }
#[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>, ExampleSrcSwitches<'s>> { fn example_switches<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, ExampleSrcSwitches<'s>> {
let (remaining, switches) = example_src_switches(false)(input)?; let (remaining, switches) = example_src_switches(false, 0)(input)?;
Ok((remaining, switches)) Ok((remaining, switches))
} }
fn example_src_switches( fn example_src_switches(
stop_at_parameters: bool, stop_at_parameters: bool,
additional_char_offset: CharOffsetInLine,
) -> impl for<'s> Fn(OrgSource<'s>) -> Res<OrgSource<'s>, ExampleSrcSwitches<'s>> { ) -> impl for<'s> Fn(OrgSource<'s>) -> Res<OrgSource<'s>, ExampleSrcSwitches<'s>> {
move |input| _example_src_switches(input, stop_at_parameters) move |input| _example_src_switches(input, stop_at_parameters, additional_char_offset)
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn _example_src_switches<'s>( fn _example_src_switches<'s>(
input: OrgSource<'s>, input: OrgSource<'s>,
stop_at_parameters: bool, stop_at_parameters: bool,
additional_char_offset: CharOffsetInLine,
) -> Res<OrgSource<'s>, ExampleSrcSwitches<'s>> { ) -> Res<OrgSource<'s>, ExampleSrcSwitches<'s>> {
let mut number_lines = None; let mut number_lines = None;
let mut retain_labels = RetainLabels::Yes; let mut retain_labels = RetainLabels::Yes;
let mut preserve_indent = None;
let mut use_labels = true; let mut use_labels = true;
let mut label_format = None; let mut label_format = None;
let mut saw_r = false; let mut saw_r = false;
@ -440,7 +451,14 @@ fn _example_src_switches<'s>(
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 = RetainLabels::Keep(character_offset); retain_labels = RetainLabels::Keep(character_offset + additional_char_offset);
}
(SwitchState::Normal, "-i") => {
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");
preserve_indent = Some(character_offset + additional_char_offset);
} }
(SwitchState::NewLineNumber, _) => { (SwitchState::NewLineNumber, _) => {
let val = normalized_word.parse::<LineNumber>(); let val = normalized_word.parse::<LineNumber>();
@ -507,6 +525,7 @@ fn _example_src_switches<'s>(
source: Into::<&str>::into(source), source: Into::<&str>::into(source),
number_lines, number_lines,
retain_labels, retain_labels,
preserve_indent,
use_labels, use_labels,
label_format, label_format,
}, },

View File

@ -50,6 +50,7 @@ pub struct ExampleBlock<'s> {
pub source: &'s str, pub source: &'s str,
pub switches: Option<&'s str>, pub switches: Option<&'s str>,
pub number_lines: Option<SwitchNumberLines>, pub number_lines: Option<SwitchNumberLines>,
pub preserve_indent: Option<CharOffsetInLine>,
pub retain_labels: RetainLabels, pub retain_labels: RetainLabels,
pub use_labels: bool, pub use_labels: bool,
pub label_format: Option<&'s str>, pub label_format: Option<&'s str>,
@ -71,6 +72,7 @@ pub struct SrcBlock<'s> {
pub switches: Option<&'s str>, pub switches: Option<&'s str>,
pub parameters: Option<&'s str>, pub parameters: Option<&'s str>,
pub number_lines: Option<SwitchNumberLines>, pub number_lines: Option<SwitchNumberLines>,
pub preserve_indent: Option<CharOffsetInLine>,
pub retain_labels: RetainLabels, pub retain_labels: RetainLabels,
pub use_labels: bool, pub use_labels: bool,
pub label_format: Option<&'s str>, pub label_format: Option<&'s str>,