From 0b0865ab226d57c88e22b6b395d033f68f2c11af Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 14 Jul 2020 14:01:54 +0200 Subject: Generaize annotation extraction --- crates/test_utils/src/lib.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'crates/test_utils') diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index e4aa894ac..4c89ed87b 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -180,7 +180,7 @@ pub fn extract_annotations(text: &str) -> Vec<(TextRange, String)> { let mut prev_line_start: Option = None; let mut line_start: TextSize = 0.into(); for line in lines_with_ends(text) { - if let Some(idx) = line.find("//^") { + if let Some(idx) = line.find("//") { let offset = prev_line_start.unwrap() + TextSize::of(&line[..idx + "//".len()]); for (line_range, text) in extract_line_annotations(&line[idx + "//".len()..]) { res.push((line_range + offset, text)) @@ -195,7 +195,15 @@ pub fn extract_annotations(text: &str) -> Vec<(TextRange, String)> { fn extract_line_annotations(mut line: &str) -> Vec<(TextRange, String)> { let mut res = Vec::new(); let mut offset: TextSize = 0.into(); - while !line.is_empty() { + loop { + match line.find('^') { + Some(idx) => { + offset += TextSize::try_from(idx).unwrap(); + line = &line[idx..]; + } + None => break, + }; + let len = line.chars().take_while(|&it| it == '^').count(); assert!(len > 0); let range = TextRange::at(offset, len.try_into().unwrap()); -- cgit v1.2.3