Merge branch 'compare_properties_the_other_nodes'
This commit is contained in:
commit
360b2d963d
3
org_mode_samples/greater_element/plain_list/checkbox.org
Normal file
3
org_mode_samples/greater_element/plain_list/checkbox.org
Normal file
@ -0,0 +1,3 @@
|
||||
- [ ] Foo
|
||||
- [-] Bar
|
||||
- [X] Baz
|
@ -1,4 +1,6 @@
|
||||
use std::collections::BTreeSet;
|
||||
use std::fmt::Debug;
|
||||
use std::str::FromStr;
|
||||
|
||||
use super::diff::artificial_diff_scope;
|
||||
use super::diff::compare_ast_node;
|
||||
@ -7,9 +9,14 @@ use super::diff::DiffStatus;
|
||||
use super::sexp::unquote;
|
||||
use super::sexp::Token;
|
||||
use super::util::get_property;
|
||||
use super::util::get_property_numeric;
|
||||
use super::util::get_property_quoted_string;
|
||||
use super::util::get_property_unquoted_atom;
|
||||
use crate::types::AstNode;
|
||||
use crate::types::CharOffsetInLine;
|
||||
use crate::types::LineNumber;
|
||||
use crate::types::RetainLabels;
|
||||
use crate::types::SwitchNumberLines;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) enum EmacsField<'s> {
|
||||
@ -117,6 +124,38 @@ pub(crate) fn compare_property_unquoted_atom<'b, 's, 'x, R, RG: Fn(R) -> Option<
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn compare_property_numeric<
|
||||
'b,
|
||||
's,
|
||||
'x,
|
||||
R,
|
||||
RV: FromStr + PartialEq + Debug,
|
||||
RG: Fn(R) -> Option<RV>,
|
||||
>(
|
||||
_source: &'s str,
|
||||
emacs: &'b Token<'s>,
|
||||
rust_node: R,
|
||||
emacs_field: &'x str,
|
||||
rust_value_getter: RG,
|
||||
) -> Result<ComparePropertiesResult<'b, 's>, Box<dyn std::error::Error + 's>>
|
||||
where
|
||||
<RV as FromStr>::Err: std::error::Error,
|
||||
<RV as FromStr>::Err: 's,
|
||||
{
|
||||
let value = get_property_numeric::<RV>(emacs, emacs_field)?;
|
||||
let rust_value = rust_value_getter(rust_node);
|
||||
if rust_value != value {
|
||||
let this_status = DiffStatus::Bad;
|
||||
let message = Some(format!(
|
||||
"{} mismatch (emacs != rust) {:?} != {:?}",
|
||||
emacs_field, value, rust_value
|
||||
));
|
||||
Ok(ComparePropertiesResult::SelfChange(this_status, message))
|
||||
} else {
|
||||
Ok(ComparePropertiesResult::NoChange)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn compare_property_list_of_quoted_string<
|
||||
'b,
|
||||
's,
|
||||
@ -174,6 +213,59 @@ pub(crate) fn compare_property_list_of_quoted_string<
|
||||
Ok(ComparePropertiesResult::NoChange)
|
||||
}
|
||||
|
||||
pub(crate) fn compare_property_set_of_quoted_string<
|
||||
'a,
|
||||
'b,
|
||||
's,
|
||||
'x,
|
||||
R,
|
||||
RV: AsRef<str> + std::fmt::Debug + Ord + 'a + ?Sized,
|
||||
RI: Iterator<Item = &'a RV>,
|
||||
RG: Fn(R) -> Option<RI>,
|
||||
>(
|
||||
_source: &'s str,
|
||||
emacs: &'b Token<'s>,
|
||||
rust_node: R,
|
||||
emacs_field: &'x str,
|
||||
rust_value_getter: RG,
|
||||
) -> Result<ComparePropertiesResult<'b, 's>, Box<dyn std::error::Error>> {
|
||||
let value = get_property(emacs, emacs_field)?
|
||||
.map(Token::as_list)
|
||||
.map_or(Ok(None), |r| r.map(Some))?;
|
||||
let empty = Vec::new();
|
||||
let value = value.unwrap_or(&empty);
|
||||
let rust_value = rust_value_getter(rust_node);
|
||||
let rust_value = if let Some(rust_value) = rust_value {
|
||||
let slices: BTreeSet<&str> = rust_value.map(|rv| rv.as_ref()).collect();
|
||||
slices
|
||||
} else {
|
||||
BTreeSet::new()
|
||||
};
|
||||
let value: Vec<&str> = value
|
||||
.iter()
|
||||
.map(|e| e.as_atom())
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
let value: Vec<String> = value
|
||||
.into_iter()
|
||||
.map(unquote)
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
let value: BTreeSet<&str> = value.iter().map(|e| e.as_str()).collect();
|
||||
let mismatched: Vec<_> = value
|
||||
.symmetric_difference(&rust_value)
|
||||
.map(|val| *val)
|
||||
.collect();
|
||||
if !mismatched.is_empty() {
|
||||
let this_status = DiffStatus::Bad;
|
||||
let message = Some(format!(
|
||||
"{} mismatch. Mismatched values: {}",
|
||||
emacs_field,
|
||||
mismatched.join(", ")
|
||||
));
|
||||
return Ok(ComparePropertiesResult::SelfChange(this_status, message));
|
||||
}
|
||||
Ok(ComparePropertiesResult::NoChange)
|
||||
}
|
||||
|
||||
pub(crate) fn compare_property_boolean<'b, 's, 'x, R, RG: Fn(R) -> bool>(
|
||||
_source: &'s str,
|
||||
emacs: &'b Token<'s>,
|
||||
@ -196,6 +288,45 @@ pub(crate) fn compare_property_boolean<'b, 's, 'x, R, RG: Fn(R) -> bool>(
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn compare_property_single_ast_node<
|
||||
'b,
|
||||
's,
|
||||
'x: 'b + 's,
|
||||
R,
|
||||
RV: std::fmt::Debug,
|
||||
RG: Fn(R) -> Option<RV>,
|
||||
>(
|
||||
source: &'s str,
|
||||
emacs: &'b Token<'s>,
|
||||
rust_node: R,
|
||||
emacs_field: &'x str,
|
||||
rust_value_getter: RG,
|
||||
) -> Result<ComparePropertiesResult<'b, 's>, Box<dyn std::error::Error>>
|
||||
where
|
||||
AstNode<'b, 's>: From<RV>,
|
||||
{
|
||||
let value = get_property(emacs, emacs_field)?;
|
||||
let rust_value = rust_value_getter(rust_node);
|
||||
match (value, rust_value) {
|
||||
(None, None) => {}
|
||||
(None, rv @ Some(_)) | (Some(_), rv @ None) => {
|
||||
let this_status = DiffStatus::Bad;
|
||||
let message = Some(format!(
|
||||
"{} mismatch (emacs != rust) {:?} != {:?}",
|
||||
emacs_field, value, rv
|
||||
));
|
||||
return Ok(ComparePropertiesResult::SelfChange(this_status, message));
|
||||
}
|
||||
(Some(ev), Some(rv)) => {
|
||||
let child_status: Vec<DiffEntry<'b, 's>> =
|
||||
vec![compare_ast_node(source, ev, rv.into())?];
|
||||
let diff_scope = artificial_diff_scope(emacs_field, child_status)?;
|
||||
return Ok(ComparePropertiesResult::DiffEntry(diff_scope));
|
||||
}
|
||||
}
|
||||
Ok(ComparePropertiesResult::NoChange)
|
||||
}
|
||||
|
||||
pub(crate) fn compare_property_list_of_ast_nodes<
|
||||
'b,
|
||||
's,
|
||||
@ -249,3 +380,115 @@ where
|
||||
}
|
||||
Ok(ComparePropertiesResult::NoChange)
|
||||
}
|
||||
|
||||
pub(crate) fn compare_property_number_lines<
|
||||
'b,
|
||||
's,
|
||||
'x,
|
||||
'y,
|
||||
R,
|
||||
RG: Fn(R) -> Option<&'y SwitchNumberLines>,
|
||||
>(
|
||||
_source: &'s str,
|
||||
emacs: &'b Token<'s>,
|
||||
rust_node: R,
|
||||
emacs_field: &'x str,
|
||||
rust_value_getter: RG,
|
||||
) -> Result<ComparePropertiesResult<'b, 's>, Box<dyn std::error::Error>> {
|
||||
let number_lines = get_property(emacs, emacs_field)?;
|
||||
let rust_value = rust_value_getter(rust_node);
|
||||
match (number_lines, &rust_value) {
|
||||
(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 => {}
|
||||
_ => {
|
||||
let this_status = DiffStatus::Bad;
|
||||
let message = Some(format!(
|
||||
"{} mismatch (emacs != rust) {:?} != {:?}",
|
||||
emacs_field, number_lines, rust_value
|
||||
));
|
||||
return Ok(ComparePropertiesResult::SelfChange(this_status, message));
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
let this_status = DiffStatus::Bad;
|
||||
let message = Some(format!(
|
||||
"{} mismatch (emacs != rust) {:?} != {:?}",
|
||||
emacs_field, number_lines, rust_value
|
||||
));
|
||||
return Ok(ComparePropertiesResult::SelfChange(this_status, message));
|
||||
}
|
||||
};
|
||||
|
||||
Ok(ComparePropertiesResult::NoChange)
|
||||
}
|
||||
|
||||
pub(crate) fn compare_property_retain_labels<'b, 's, 'x, 'y, R, RG: Fn(R) -> &'y RetainLabels>(
|
||||
_source: &'s str,
|
||||
emacs: &'b Token<'s>,
|
||||
rust_node: R,
|
||||
emacs_field: &'x str,
|
||||
rust_value_getter: RG,
|
||||
) -> Result<ComparePropertiesResult<'b, 's>, Box<dyn std::error::Error + 's>> {
|
||||
let rust_value = rust_value_getter(rust_node);
|
||||
let retain_labels = get_property_unquoted_atom(emacs, ":retain-labels")?;
|
||||
if let Some(retain_labels) = retain_labels {
|
||||
if retain_labels == "t" {
|
||||
match rust_value {
|
||||
RetainLabels::Yes => {}
|
||||
_ => {
|
||||
let this_status = DiffStatus::Bad;
|
||||
let message = Some(format!(
|
||||
"{} mismatch (emacs != rust) {:?} != {:?}",
|
||||
emacs_field, retain_labels, rust_value
|
||||
));
|
||||
return Ok(ComparePropertiesResult::SelfChange(this_status, message));
|
||||
}
|
||||
}
|
||||
} 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_value) {
|
||||
(e, RetainLabels::Keep(r)) if e == *r => {}
|
||||
_ => {
|
||||
let this_status = DiffStatus::Bad;
|
||||
let message = Some(format!(
|
||||
"{} mismatch (emacs != rust) {:?} != {:?}",
|
||||
emacs_field, retain_labels, rust_value
|
||||
));
|
||||
return Ok(ComparePropertiesResult::SelfChange(this_status, message));
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
match rust_value {
|
||||
RetainLabels::No => {}
|
||||
_ => {
|
||||
let this_status = DiffStatus::Bad;
|
||||
let message = Some(format!(
|
||||
"{} mismatch (emacs != rust) {:?} != {:?}",
|
||||
emacs_field, retain_labels, rust_value
|
||||
));
|
||||
return Ok(ComparePropertiesResult::SelfChange(this_status, message));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(ComparePropertiesResult::NoChange)
|
||||
}
|
||||
|
2680
src/compare/diff.rs
2680
src/compare/diff.rs
File diff suppressed because it is too large
Load Diff
@ -16,6 +16,7 @@ use nom::combinator::verify;
|
||||
use nom::multi::many0;
|
||||
use nom::multi::many_till;
|
||||
use nom::sequence::tuple;
|
||||
use nom::InputTake;
|
||||
|
||||
use super::keyword::affiliated_keyword;
|
||||
use super::org_source::OrgSource;
|
||||
@ -160,7 +161,20 @@ pub(crate) fn example_block<'b, 'g, 'r, 's>(
|
||||
) -> Res<OrgSource<'s>, ExampleBlock<'s>> {
|
||||
let (remaining, affiliated_keywords) = many0(affiliated_keyword)(input)?;
|
||||
let (remaining, _) = lesser_block_begin("example")(context, remaining)?;
|
||||
let (remaining, parameters) = opt(tuple((space1, example_switches)))(remaining)?;
|
||||
let (remaining, parameters) = opt(alt((
|
||||
map(tuple((space1, example_switches)), |(_, switches)| switches),
|
||||
map(space1, |ws: OrgSource<'_>| {
|
||||
let source = ws.take(0);
|
||||
ExampleSrcSwitches {
|
||||
source: source.into(),
|
||||
number_lines: None,
|
||||
retain_labels: RetainLabels::Yes,
|
||||
preserve_indent: None,
|
||||
use_labels: true,
|
||||
label_format: None,
|
||||
}
|
||||
}),
|
||||
)))(remaining)?;
|
||||
let (remaining, _nl) = recognize(tuple((space0, line_ending)))(remaining)?;
|
||||
let lesser_block_end_specialized = lesser_block_end("example");
|
||||
let contexts = [
|
||||
@ -174,7 +188,6 @@ 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 = parameters.map(|(_, parameters)| parameters);
|
||||
|
||||
let (remaining, contents) = content(&parser_context, remaining)?;
|
||||
let (remaining, _end) = lesser_block_end_specialized(&parser_context, remaining)?;
|
||||
@ -185,11 +198,7 @@ pub(crate) fn example_block<'b, 'g, 'r, 's>(
|
||||
let (switches, number_lines, preserve_indent, retain_labels, use_labels, label_format) = {
|
||||
if let Some(parameters) = parameters {
|
||||
(
|
||||
if parameters.source.len() == 0 {
|
||||
None
|
||||
} else {
|
||||
Some(parameters.source)
|
||||
},
|
||||
Some(parameters.source),
|
||||
parameters.number_lines,
|
||||
parameters.preserve_indent,
|
||||
parameters.retain_labels,
|
||||
|
@ -176,12 +176,13 @@ fn org_mode_table_cell<'b, 'g, 'r, 's>(
|
||||
let table_cell_set_object_matcher =
|
||||
parser_with_context!(table_cell_set_object)(&parser_context);
|
||||
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
|
||||
let (remaining, _) = space0(input)?;
|
||||
let (remaining, (children, _exit_contents)) = verify(
|
||||
many_till(table_cell_set_object_matcher, exit_matcher),
|
||||
|(children, exit_contents)| {
|
||||
!children.is_empty() || Into::<&str>::into(exit_contents).ends_with("|")
|
||||
},
|
||||
)(input)?;
|
||||
)(remaining)?;
|
||||
|
||||
let (remaining, _tail) = org_mode_table_cell_end(&parser_context, remaining)?;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user