Compare number lines for example blocks.

This commit is contained in:
Tom Alexander 2023-10-04 10:31:01 -04:00
parent 650cbc17db
commit 3d7f411cf9
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
4 changed files with 56 additions and 5 deletions

View File

@ -52,6 +52,7 @@ use crate::types::Keyword;
use crate::types::LatexEnvironment;
use crate::types::LatexFragment;
use crate::types::LineBreak;
use crate::types::LineNumber;
use crate::types::Minute;
use crate::types::MinuteInner;
use crate::types::Month;
@ -83,6 +84,7 @@ use crate::types::StatisticsCookie;
use crate::types::StrikeThrough;
use crate::types::Subscript;
use crate::types::Superscript;
use crate::types::SwitchNumberLines;
use crate::types::Table;
use crate::types::TableCell;
use crate::types::TableRow;
@ -1545,7 +1547,7 @@ fn compare_example_block<'b, 's>(
let mut this_status = DiffStatus::Good;
let mut message = None;
// TODO: Compare :number-lines :preserve-indent :retain-labels :use-labels :label-fmt
// TODO: Compare :preserve-indent :retain-labels :use-labels :label-fmt
// Compare value
let contents = get_property_quoted_string(emacs, ":value")?.unwrap_or(String::new());
@ -1567,6 +1569,46 @@ fn compare_example_block<'b, 's>(
));
}
// Compare number-lines
let number_lines = get_property(emacs, ":number-lines")?;
match (number_lines, &rust.number_lines) {
(None, None) => {}
(Some(number_lines), Some(rust_number_lines)) => {
let token_list = number_lines.as_list()?;
let number_type = token_list
.get(0)
.map(Token::as_atom)
.map_or(Ok(None), |r| r.map(Some))?
.ok_or(":number-lines should have a type.")?;
let number_value = token_list
.get(2)
.map(Token::as_atom)
.map_or(Ok(None), |r| r.map(Some))?
.map(|val| val.parse::<LineNumber>())
.map_or(Ok(None), |r| r.map(Some))?
.ok_or(":number-lines should have a value.")?;
match (number_type, number_value, rust_number_lines) {
("new", emacs_val, SwitchNumberLines::New(rust_val)) if emacs_val == *rust_val => {}
("continued", emacs_val, SwitchNumberLines::Continued(rust_val))
if emacs_val == *rust_val => {}
_ => {
this_status = DiffStatus::Bad;
message = Some(format!(
"Number lines mismatch (emacs != rust) {:?} != {:?}",
number_lines, rust.number_lines
));
}
}
}
_ => {
this_status = DiffStatus::Bad;
message = Some(format!(
"Number lines mismatch (emacs != rust) {:?} != {:?}",
number_lines, rust.number_lines
));
}
};
Ok(DiffResult {
status: this_status,
name: rust.get_elisp_name(),

View File

@ -144,10 +144,7 @@ pub(crate) fn example_block<'b, 'g, 'r, 's>(
let parser_context = context.with_additional_node(&contexts[0]);
let parser_context = parser_context.with_additional_node(&contexts[1]);
let parser_context = parser_context.with_additional_node(&contexts[2]);
let parameters = match parameters {
Some((_ws, parameters)) => Some(parameters),
None => None,
};
let parameters = parameters.map(|(_, parameters)| parameters);
let (remaining, contents) = parser_with_context!(text_until_exit)(&parser_context)(remaining)?;
let (remaining, _end) = lesser_block_end_specialized(&parser_context, remaining)?;
@ -159,6 +156,7 @@ pub(crate) fn example_block<'b, 'g, 'r, 's>(
source: source.into(),
name: source.into(),
switches: parameters.map(|parameters| Into::<&str>::into(parameters)),
number_lines: None, // TODO
contents: contents.into(),
},
))

View File

@ -41,6 +41,7 @@ pub struct ExampleBlock<'s> {
pub source: &'s str,
pub name: &'s str,
pub switches: Option<&'s str>,
pub number_lines: Option<SwitchNumberLines>,
pub contents: &'s str,
}
@ -107,6 +108,14 @@ pub struct LatexEnvironment<'s> {
pub source: &'s str,
}
pub type LineNumber = usize;
#[derive(Debug)]
pub enum SwitchNumberLines {
New(LineNumber),
Continued(LineNumber),
}
impl<'s> Paragraph<'s> {
pub(crate) fn of_text(input: &'s str) -> Self {
let mut objects = Vec::with_capacity(1);

View File

@ -47,9 +47,11 @@ pub use lesser_element::FixedWidthArea;
pub use lesser_element::HorizontalRule;
pub use lesser_element::Keyword;
pub use lesser_element::LatexEnvironment;
pub use lesser_element::LineNumber;
pub use lesser_element::Paragraph;
pub use lesser_element::Planning;
pub use lesser_element::SrcBlock;
pub use lesser_element::SwitchNumberLines;
pub use lesser_element::TableCell;
pub use lesser_element::VerseBlock;
pub use object::AngleLink;