From 8295dc42a0fc9e8641606f75a5ba2a46fe48379c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 1 Jul 2020 18:17:08 +0200 Subject: Fold multiline calls --- crates/test_utils/src/lib.rs | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) (limited to 'crates/test_utils/src/lib.rs') diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index fba5f4281..e4aa894ac 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -118,8 +118,8 @@ pub fn extract_range_or_offset(text: &str) -> (RangeOrOffset, String) { } /// Extracts ranges, marked with ` ` pairs from the `text` -pub fn extract_ranges(mut text: &str, tag: &str) -> (Vec, String) { - let open = format!("<{}>", tag); +pub fn extract_tags(mut text: &str, tag: &str) -> (Vec<(TextRange, Option)>, String) { + let open = format!("<{}", tag); let close = format!("", tag); let mut ranges = Vec::new(); let mut res = String::new(); @@ -134,22 +134,35 @@ pub fn extract_ranges(mut text: &str, tag: &str) -> (Vec, String) { res.push_str(&text[..i]); text = &text[i..]; if text.starts_with(&open) { - text = &text[open.len()..]; + let close_open = text.find('>').unwrap(); + let attr = text[open.len()..close_open].trim(); + let attr = if attr.is_empty() { None } else { Some(attr.to_string()) }; + text = &text[close_open + '>'.len_utf8()..]; let from = TextSize::of(&res); - stack.push(from); + stack.push((from, attr)); } else if text.starts_with(&close) { text = &text[close.len()..]; - let from = stack.pop().unwrap_or_else(|| panic!("unmatched ", tag)); + let (from, attr) = + stack.pop().unwrap_or_else(|| panic!("unmatched ", tag)); let to = TextSize::of(&res); - ranges.push(TextRange::new(from, to)); + ranges.push((TextRange::new(from, to), attr)); + } else { + res.push('<'); + text = &text['<'.len_utf8()..]; } } } } assert!(stack.is_empty(), "unmatched <{}>", tag); - ranges.sort_by_key(|r| (r.start(), r.end())); + ranges.sort_by_key(|r| (r.0.start(), r.0.end())); (ranges, res) } +#[test] +fn test_extract_tags() { + let (tags, text) = extract_tags(r#"fn main() {}"#, "tag"); + let actual = tags.into_iter().map(|(range, attr)| (&text[range], attr)).collect::>(); + assert_eq!(actual, vec![("fn main() {}", Some("fn".into())), ("main", None),]); +} /// Inserts `<|>` marker into the `text` at `offset`. pub fn add_cursor(text: &str, offset: TextSize) -> String { -- cgit v1.2.3