Generalize annotations

This commit is contained in:
Aleksey Kladov 2020-06-30 12:13:08 +02:00
parent e87cba85ef
commit d21c84abd4

View file

@ -11,6 +11,7 @@ pub mod mark;
mod fixture; mod fixture;
use std::{ use std::{
convert::TryInto,
env, fs, env, fs,
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
@ -168,8 +169,10 @@ 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 data = line[idx + "//^".len()..].trim().to_string(); let marker_and_data = &line[idx + "//".len()..];
res.push((TextRange::at(offset, 1.into()), data)) 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))
} }
prev_line_start = Some(line_start); prev_line_start = Some(line_start);
line_start += TextSize::of(line); line_start += TextSize::of(line);
@ -184,15 +187,15 @@ fn test_extract_annotations() {
fn main() { fn main() {
let x = 92; let x = 92;
//^ def //^ def
z + 1 zoo + 1
} //^ i32 } //^^^ i32
"#, "#,
); );
let res = extract_annotations(&text) let res = extract_annotations(&text)
.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()), ("z", "i32".into()),]); assert_eq!(res, vec![("x", "def".into()), ("zoo", "i32".into()),]);
} }
// Comparison functionality borrowed from cargo: // Comparison functionality borrowed from cargo: