Compare plain list item bullets.

This commit is contained in:
Tom Alexander 2023-09-29 17:28:50 -04:00
parent 13697df7ea
commit 967e74c147
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
4 changed files with 53 additions and 17 deletions

View File

@ -9,6 +9,7 @@ use super::sexp::unquote;
use super::sexp::Token; use super::sexp::Token;
use super::util::compare_standard_properties; use super::util::compare_standard_properties;
use super::util::get_property; use super::util::get_property;
use super::util::get_property_quoted_string;
use super::util::get_property_unquoted_atom; use super::util::get_property_unquoted_atom;
use crate::types::AngleLink; use crate::types::AngleLink;
use crate::types::BabelCall; use crate::types::BabelCall;
@ -830,7 +831,16 @@ fn compare_plain_list_item<'s>(
.collect::<Result<Vec<_>, _>>()?; .collect::<Result<Vec<_>, _>>()?;
child_status.push(artificial_diff_scope("contents", contents_status)?); child_status.push(artificial_diff_scope("contents", contents_status)?);
// TODO: Compare :bullet :counter :pre-blank // TODO: Compare :counter :pre-blank
let bullet = get_property_quoted_string(emacs, ":bullet")?
.ok_or("Plain list items must have a :bullet.")?;
if bullet != rust.bullet {
this_status = DiffStatus::Bad;
message = Some(format!(
"Plain list item bullet mismatch (emacs != rust) {:?} != {:?}",
bullet, rust.bullet
));
}
// Compare checkbox // Compare checkbox
let checkbox = get_property(emacs, ":checkbox")? let checkbox = get_property(emacs, ":checkbox")?

View File

@ -1,5 +1,6 @@
use super::elisp_fact::GetElispFact; use super::elisp_fact::GetElispFact;
use super::sexp::Token; use super::sexp::Token;
use crate::compare::sexp::unquote;
use crate::types::GetStandardProperties; use crate::types::GetStandardProperties;
use crate::types::StandardProperties; use crate::types::StandardProperties;
@ -202,3 +203,17 @@ pub(crate) fn get_property_unquoted_atom<'s, 'x>(
.map(Token::as_atom) .map(Token::as_atom)
.map_or(Ok(None), |r| r.map(Some))?) .map_or(Ok(None), |r| r.map(Some))?)
} }
/// Get a named property containing an quoted string from the emacs token.
///
/// Returns None if key is not found.
pub(crate) fn get_property_quoted_string<'s, 'x>(
emacs: &'s Token<'s>,
key: &'x str,
) -> Result<Option<String>, Box<dyn std::error::Error>> {
Ok(get_property(emacs, key)?
.map(Token::as_atom)
.map_or(Ok(None), |r| r.map(Some))?
.map(unquote)
.map_or(Ok(None), |r| r.map(Some))?)
}

View File

@ -78,6 +78,12 @@ impl<'s> OrgSource<'s> {
self.slice(..(other.start - self.start)) self.slice(..(other.start - self.start))
} }
pub(crate) fn get_until_end_of(&self, other: OrgSource<'s>) -> OrgSource<'s> {
debug_assert!(other.start >= self.start);
debug_assert!(other.end <= self.end);
self.slice(..(other.end - self.start))
}
pub(crate) fn get_start_of_line(&self) -> OrgSource<'s> { pub(crate) fn get_start_of_line(&self) -> OrgSource<'s> {
let skipped_text = self.text_since_line_break(); let skipped_text = self.text_since_line_break();
let mut bracket_depth = self.bracket_depth; let mut bracket_depth = self.bracket_depth;

View File

@ -54,12 +54,12 @@ pub(crate) fn detect_plain_list<'b, 'g, 'r, 's>(
if verify( if verify(
tuple(( tuple((
start_of_line, start_of_line,
space0, parser_with_context!(indentation_level)(context),
parser_with_context!(bullet)(context), parser_with_context!(bullet)(context),
alt((space1, line_ending, eof)), alt((space1, line_ending, eof)),
)), )),
|(_start, indent, (_bullet_type, bull), _after_whitespace)| { |(_start, (indent_level, _), (_bullet_type, bull), _after_whitespace)| {
Into::<&str>::into(bull) != "*" || indent.len() > 0 !Into::<&str>::into(bull).starts_with("*") || *indent_level > 0
}, },
)(input) )(input)
.is_ok() .is_ok()
@ -163,7 +163,7 @@ fn plain_list_item<'b, 'g, 'r, 's>(
let (remaining, (indent_level, _leading_whitespace)) = indentation_level(context, input)?; let (remaining, (indent_level, _leading_whitespace)) = indentation_level(context, input)?;
let (remaining, (bullet_type, bull)) = verify( let (remaining, (bullet_type, bull)) = verify(
parser_with_context!(bullet)(context), parser_with_context!(bullet)(context),
|(_bullet_type, bull)| Into::<&str>::into(bull) != "*" || indent_level > 0, |(_bullet_type, bull)| !Into::<&str>::into(bull).starts_with("*") || indent_level > 0,
)(remaining)?; )(remaining)?;
let (remaining, _maybe_counter_set) = let (remaining, _maybe_counter_set) =
@ -279,18 +279,23 @@ fn bullet<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>, context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>, input: OrgSource<'s>,
) -> Res<OrgSource<'s>, (BulletType, OrgSource<'s>)> { ) -> Res<OrgSource<'s>, (BulletType, OrgSource<'s>)> {
alt(( let (remaining, ((bullet_type, _without_space), peek_trailing_space)) = tuple((
map(tag("*"), |bull| (BulletType::Unordered, bull)), alt((
map(tag("-"), |bull| (BulletType::Unordered, bull)), map(tag("*"), |bull| (BulletType::Unordered, bull)),
map(tag("+"), |bull| (BulletType::Unordered, bull)), map(tag("-"), |bull| (BulletType::Unordered, bull)),
map( map(tag("+"), |bull| (BulletType::Unordered, bull)),
recognize(tuple(( map(
parser_with_context!(counter)(context), recognize(tuple((
alt((tag("."), tag(")"))), parser_with_context!(counter)(context),
))), alt((tag("."), tag(")"))),
|bull| (BulletType::Ordered, bull), ))),
), |bull| (BulletType::Ordered, bull),
))(input) ),
)),
peek(space0),
))(input)?;
let with_space = input.get_until_end_of(peek_trailing_space);
Ok((remaining, (bullet_type, with_space)))
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]