2018-08-09 04:47:34 -05:00
|
|
|
extern crate libeditor;
|
2018-08-12 10:50:16 -05:00
|
|
|
extern crate libsyntax2;
|
2018-08-09 04:47:34 -05:00
|
|
|
extern crate itertools;
|
2018-08-12 10:50:16 -05:00
|
|
|
#[macro_use]
|
|
|
|
extern crate assert_eq_text;
|
2018-08-09 04:47:34 -05:00
|
|
|
|
|
|
|
use std::fmt;
|
|
|
|
use itertools::Itertools;
|
2018-08-12 10:50:16 -05:00
|
|
|
use libsyntax2::AstNode;
|
|
|
|
use libeditor::{
|
|
|
|
File, TextUnit, TextRange,
|
|
|
|
highlight, runnables, extend_selection, file_symbols, flip_comma,
|
|
|
|
};
|
2018-08-09 04:47:34 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extend_selection() {
|
|
|
|
let file = file(r#"fn foo() {
|
|
|
|
1 + 1
|
|
|
|
}
|
|
|
|
"#);
|
|
|
|
let range = TextRange::offset_len(18.into(), 0.into());
|
2018-08-10 09:49:45 -05:00
|
|
|
let range = extend_selection(&file, range).unwrap();
|
2018-08-09 04:47:34 -05:00
|
|
|
assert_eq!(range, TextRange::from_to(17.into(), 18.into()));
|
2018-08-10 09:49:45 -05:00
|
|
|
let range = extend_selection(&file, range).unwrap();
|
2018-08-09 04:47:34 -05:00
|
|
|
assert_eq!(range, TextRange::from_to(15.into(), 20.into()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_highlighting() {
|
|
|
|
let file = file(r#"
|
|
|
|
// comment
|
|
|
|
fn main() {}
|
|
|
|
println!("Hello, {}!", 92);
|
|
|
|
"#);
|
2018-08-10 09:49:45 -05:00
|
|
|
let hls = highlight(&file);
|
2018-08-09 04:47:34 -05:00
|
|
|
dbg_eq(
|
|
|
|
r#"[HighlightedRange { range: [1; 11), tag: "comment" },
|
|
|
|
HighlightedRange { range: [12; 14), tag: "keyword" },
|
|
|
|
HighlightedRange { range: [15; 19), tag: "function" },
|
|
|
|
HighlightedRange { range: [29; 36), tag: "text" },
|
|
|
|
HighlightedRange { range: [38; 50), tag: "string" },
|
2018-08-12 10:50:16 -05:00
|
|
|
HighlightedRange { range: [52; 54), tag: "literal" }]"#,
|
|
|
|
&hls,
|
2018-08-09 04:47:34 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-08-09 08:03:21 -05:00
|
|
|
#[test]
|
|
|
|
fn test_runnables() {
|
|
|
|
let file = file(r#"
|
|
|
|
fn main() {}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_foo() {}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[ignore]
|
|
|
|
fn test_foo() {}
|
|
|
|
"#);
|
2018-08-10 09:49:45 -05:00
|
|
|
let runnables = runnables(&file);
|
2018-08-09 08:03:21 -05:00
|
|
|
dbg_eq(
|
|
|
|
r#"[Runnable { range: [1; 13), kind: Bin },
|
|
|
|
Runnable { range: [15; 39), kind: Test { name: "test_foo" } },
|
|
|
|
Runnable { range: [41; 75), kind: Test { name: "test_foo" } }]"#,
|
2018-08-12 10:50:16 -05:00
|
|
|
&runnables,
|
2018-08-09 08:03:21 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-08-11 04:28:59 -05:00
|
|
|
#[test]
|
|
|
|
fn symbols() {
|
|
|
|
let file = file(r#"
|
|
|
|
struct Foo {
|
|
|
|
x: i32
|
|
|
|
}
|
|
|
|
|
|
|
|
mod m {
|
|
|
|
fn bar() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum E { X, Y(i32) }
|
2018-08-11 08:20:37 -05:00
|
|
|
type T = ();
|
|
|
|
static S: i32 = 92;
|
|
|
|
const C: i32 = 92;
|
2018-08-11 04:28:59 -05:00
|
|
|
"#);
|
|
|
|
let symbols = file_symbols(&file);
|
|
|
|
dbg_eq(
|
|
|
|
r#"[FileSymbol { parent: None, name: "Foo", name_range: [8; 11), node_range: [1; 26), kind: STRUCT },
|
|
|
|
FileSymbol { parent: None, name: "m", name_range: [32; 33), node_range: [28; 53), kind: MODULE },
|
|
|
|
FileSymbol { parent: Some(1), name: "bar", name_range: [43; 46), node_range: [40; 51), kind: FUNCTION },
|
2018-08-11 08:20:37 -05:00
|
|
|
FileSymbol { parent: None, name: "E", name_range: [60; 61), node_range: [55; 75), kind: ENUM },
|
|
|
|
FileSymbol { parent: None, name: "T", name_range: [81; 82), node_range: [76; 88), kind: TYPE_ITEM },
|
|
|
|
FileSymbol { parent: None, name: "S", name_range: [96; 97), node_range: [89; 108), kind: STATIC_ITEM },
|
|
|
|
FileSymbol { parent: None, name: "C", name_range: [115; 116), node_range: [109; 127), kind: CONST_ITEM }]"#,
|
2018-08-12 10:50:16 -05:00
|
|
|
&symbols,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_swap_comma() {
|
|
|
|
check_modification(
|
|
|
|
"fn foo(x: i32,<|> y: Result<(), ()>) {}",
|
|
|
|
"fn foo(y: Result<(), ()>, x: i32) {}",
|
|
|
|
&|file, offset| {
|
|
|
|
let edit = flip_comma(file, offset).unwrap()();
|
|
|
|
edit.apply(&file.syntax().text())
|
|
|
|
},
|
2018-08-11 04:28:59 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-08-11 00:56:13 -05:00
|
|
|
fn file(text: &str) -> File {
|
|
|
|
File::parse(text)
|
2018-08-09 04:47:34 -05:00
|
|
|
}
|
|
|
|
|
2018-08-12 10:50:16 -05:00
|
|
|
fn dbg_eq(expected: &str, actual: &impl fmt::Debug) {
|
2018-08-09 08:03:21 -05:00
|
|
|
let actual = format!("{:?}", actual);
|
2018-08-09 04:47:34 -05:00
|
|
|
let expected = expected.lines().map(|l| l.trim()).join(" ");
|
2018-08-12 10:50:16 -05:00
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_modification(
|
|
|
|
before: &str,
|
|
|
|
after: &str,
|
|
|
|
f: &impl Fn(&File, TextUnit) -> String,
|
|
|
|
) {
|
|
|
|
let cursor = "<|>";
|
|
|
|
let cursor_pos = match before.find(cursor) {
|
|
|
|
None => panic!("before text should contain cursor marker"),
|
|
|
|
Some(pos) => pos,
|
|
|
|
};
|
|
|
|
let mut text = String::with_capacity(before.len() - cursor.len());
|
|
|
|
text.push_str(&before[..cursor_pos]);
|
|
|
|
text.push_str(&before[cursor_pos + cursor.len()..]);
|
|
|
|
let cursor_pos = TextUnit::from(cursor_pos as u32);
|
|
|
|
let file = file(&text);
|
|
|
|
let actual = f(&file, cursor_pos);
|
|
|
|
assert_eq_text!(after, &actual);
|
2018-08-09 04:47:34 -05:00
|
|
|
}
|