From 442c13ba176a40491deb7f9d2a2e1e24eca29f63 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 30 Jun 2020 18:04:25 +0200 Subject: Simplify most of the inlay hints tests --- crates/test_utils/src/lib.rs | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 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 f9d6c6c96..e32a0a0c3 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -11,7 +11,7 @@ pub mod mark; mod fixture; use std::{ - convert::TryInto, + convert::{TryFrom, TryInto}, env, fs, path::{Path, PathBuf}, }; @@ -169,10 +169,9 @@ pub fn extract_annotations(text: &str) -> Vec<(TextRange, String)> { for line in lines_with_ends(text) { if let Some(idx) = line.find("//^") { let offset = prev_line_start.unwrap() + TextSize::of(&line[..idx + "//".len()]); - let marker_and_data = &line[idx + "//".len()..]; - let len = marker_and_data.chars().take_while(|&it| it == '^').count(); - let data = marker_and_data[len..].trim().to_string(); - res.push((TextRange::at(offset, len.try_into().unwrap()), data)) + for (line_range, text) in extract_line_annotations(&line[idx + "//".len()..]) { + res.push((line_range + offset, text)) + } } prev_line_start = Some(line_start); line_start += TextSize::of(line); @@ -180,13 +179,28 @@ pub fn extract_annotations(text: &str) -> Vec<(TextRange, String)> { res } +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() { + let len = line.chars().take_while(|&it| it == '^').count(); + assert!(len > 0); + let range = TextRange::at(offset, len.try_into().unwrap()); + let next = line[len..].find('^').map_or(line.len(), |it| it + len); + res.push((range, line[len..][..next - len].trim().to_string())); + line = &line[next..]; + offset += TextSize::try_from(next).unwrap(); + } + res +} + #[test] fn test_extract_annotations() { let text = stdx::trim_indent( r#" fn main() { - let x = 92; - //^ def + let (x, y) = (9, 2); + //^ def ^ def zoo + 1 } //^^^ i32 "#, @@ -195,7 +209,7 @@ fn main() { .into_iter() .map(|(range, ann)| (&text[range], ann)) .collect::>(); - assert_eq!(res, vec![("x", "def".into()), ("zoo", "i32".into()),]); + assert_eq!(res, vec![("x", "def".into()), ("y", "def".into()), ("zoo", "i32".into()),]); } // Comparison functionality borrowed from cargo: -- cgit v1.2.3