Coalesce whitespace in macro args.
This commit is contained in:
		
							parent
							
								
									37bc5ef712
								
							
						
					
					
						commit
						a32cea8139
					
				| @ -108,10 +108,10 @@ pub(crate) fn compare_property_list_of_quoted_string< | ||||
|     'b, | ||||
|     's, | ||||
|     'x, | ||||
|     'y, | ||||
|     R, | ||||
|     RV: AsRef<str> + std::fmt::Debug + 'y, | ||||
|     RG: Fn(R) -> Option<&'y Vec<RV>>, | ||||
|     RV: AsRef<str> + std::fmt::Debug, | ||||
|     RI: Iterator<Item = RV>, | ||||
|     RG: Fn(R) -> Option<RI>, | ||||
| >( | ||||
|     emacs: &'b Token<'s>, | ||||
|     rust_node: R, | ||||
| @ -122,7 +122,9 @@ pub(crate) fn compare_property_list_of_quoted_string< | ||||
|         .map(Token::as_list) | ||||
|         .map_or(Ok(None), |r| r.map(Some))?; | ||||
|     let rust_value = rust_value_getter(rust_node); | ||||
|     match (value, rust_value) { | ||||
|     // TODO: Seems we are needlessly coverting to a vec here.
 | ||||
|     let rust_value: Option<Vec<RV>> = rust_value.map(|it| it.collect()); | ||||
|     match (value, &rust_value) { | ||||
|         (None, None) => {} | ||||
|         (None, Some(_)) | (Some(_), None) => { | ||||
|             let this_status = DiffStatus::Bad; | ||||
|  | ||||
| @ -3090,7 +3090,7 @@ fn compare_org_macro<'b, 's>( | ||||
|             |r| if r.macro_args.is_empty() { | ||||
|                 None | ||||
|             } else { | ||||
|                 Some(&r.macro_args) | ||||
|                 Some(r.get_macro_args()) | ||||
|             }, | ||||
|             compare_property_list_of_quoted_string | ||||
|         ) | ||||
|  | ||||
| @ -1,6 +1,7 @@ | ||||
| use std::borrow::Borrow; | ||||
| use std::borrow::Cow; | ||||
| 
 | ||||
| use super::util::coalesce_whitespace; | ||||
| use super::util::coalesce_whitespace_if_line_break; | ||||
| use super::util::remove_line_break; | ||||
| use super::util::remove_whitespace_if_line_break; | ||||
| @ -149,6 +150,9 @@ pub struct AngleLink<'s> { | ||||
| pub struct OrgMacro<'s> { | ||||
|     pub source: &'s str, | ||||
|     pub macro_name: &'s str, | ||||
|     /// The macro args from the source.
 | ||||
|     ///
 | ||||
|     /// This does not take into account the post-processing that you would get from the upstream emacs org-mode AST. Use `get_macro_args` for an equivalent value.
 | ||||
|     pub macro_args: Vec<&'s str>, | ||||
|     pub macro_value: &'s str, | ||||
| } | ||||
| @ -733,3 +737,9 @@ impl<'s> AngleLink<'s> { | ||||
|         self.search_option.map(remove_whitespace_if_line_break) | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| impl<'s> OrgMacro<'s> { | ||||
|     pub fn get_macro_args<'b>(&'b self) -> impl Iterator<Item = Cow<'s, str>> + 'b { | ||||
|         self.macro_args.iter().map(|arg| coalesce_whitespace(*arg)) | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -197,3 +197,49 @@ enum CoalesceWhitespaceIfLineBreakState { | ||||
|         ret: String, | ||||
|     }, | ||||
| } | ||||
| 
 | ||||
| /// Removes all whitespace from a string.
 | ||||
| ///
 | ||||
| /// Example: "foo bar" => "foobar" and "foo \n bar" => "foobar".
 | ||||
| pub(crate) fn coalesce_whitespace<'s>(input: &'s str) -> Cow<'s, str> { | ||||
|     let mut state = CoalesceWhitespace::Normal; | ||||
|     for (offset, c) in input.char_indices() { | ||||
|         match (&mut state, c) { | ||||
|             (CoalesceWhitespace::Normal, ' ' | '\t' | '\r' | '\n') => { | ||||
|                 let mut ret = String::with_capacity(input.len()); | ||||
|                 ret.push_str(&input[..offset]); | ||||
|                 ret.push(' '); | ||||
|                 state = CoalesceWhitespace::HasWhitespace { | ||||
|                     in_whitespace: true, | ||||
|                     ret, | ||||
|                 }; | ||||
|             } | ||||
|             (CoalesceWhitespace::Normal, _) => {} | ||||
|             ( | ||||
|                 CoalesceWhitespace::HasWhitespace { in_whitespace, ret }, | ||||
|                 ' ' | '\t' | '\r' | '\n', | ||||
|             ) => { | ||||
|                 if !*in_whitespace { | ||||
|                     *in_whitespace = true; | ||||
|                     ret.push(' '); | ||||
|                 } | ||||
|             } | ||||
|             (CoalesceWhitespace::HasWhitespace { in_whitespace, ret }, _) => { | ||||
|                 *in_whitespace = false; | ||||
|                 ret.push(c); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|     match state { | ||||
|         CoalesceWhitespace::Normal => Cow::Borrowed(input), | ||||
|         CoalesceWhitespace::HasWhitespace { | ||||
|             in_whitespace: _, | ||||
|             ret, | ||||
|         } => Cow::Owned(ret), | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| enum CoalesceWhitespace { | ||||
|     Normal, | ||||
|     HasWhitespace { in_whitespace: bool, ret: String }, | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Tom Alexander
						Tom Alexander