5146: Simplify most of the inlay hints tests r=matklad a=matklad



bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-06-30 16:05:07 +00:00 committed by GitHub
commit 4a19d5954a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 199 additions and 631 deletions

File diff suppressed because it is too large Load diff

View file

@ -11,7 +11,7 @@ pub mod mark;
mod fixture; mod fixture;
use std::{ use std::{
convert::TryInto, convert::{TryFrom, TryInto},
env, fs, env, fs,
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
@ -169,10 +169,9 @@ pub fn extract_annotations(text: &str) -> Vec<(TextRange, String)> {
for line in lines_with_ends(text) { 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()]); let offset = prev_line_start.unwrap() + TextSize::of(&line[..idx + "//".len()]);
let marker_and_data = &line[idx + "//".len()..]; for (line_range, text) in extract_line_annotations(&line[idx + "//".len()..]) {
let len = marker_and_data.chars().take_while(|&it| it == '^').count(); res.push((line_range + offset, text))
let data = marker_and_data[len..].trim().to_string(); }
res.push((TextRange::at(offset, len.try_into().unwrap()), data))
} }
prev_line_start = Some(line_start); prev_line_start = Some(line_start);
line_start += TextSize::of(line); line_start += TextSize::of(line);
@ -180,13 +179,28 @@ pub fn extract_annotations(text: &str) -> Vec<(TextRange, String)> {
res 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] #[test]
fn test_extract_annotations() { fn test_extract_annotations() {
let text = stdx::trim_indent( let text = stdx::trim_indent(
r#" r#"
fn main() { fn main() {
let x = 92; let (x, y) = (9, 2);
//^ def //^ def ^ def
zoo + 1 zoo + 1
} //^^^ i32 } //^^^ i32
"#, "#,
@ -195,7 +209,7 @@ fn main() {
.into_iter() .into_iter()
.map(|(range, ann)| (&text[range], ann)) .map(|(range, ann)| (&text[range], ann))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
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: // Comparison functionality borrowed from cargo: