2018-08-09 04:47:34 -05:00
|
|
|
extern crate libeditor;
|
2018-08-12 10:50:16 -05:00
|
|
|
extern crate libsyntax2;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate assert_eq_text;
|
2018-08-09 04:47:34 -05:00
|
|
|
|
2018-08-17 08:04:34 -05:00
|
|
|
use assert_eq_text::{assert_eq_dbg};
|
2018-08-12 10:50:16 -05:00
|
|
|
use libeditor::{
|
2018-08-22 04:58:34 -05:00
|
|
|
ParsedFile, TextUnit, TextRange, ActionResult,
|
2018-08-15 15:24:20 -05:00
|
|
|
highlight, runnables, extend_selection, file_structure,
|
2018-08-15 16:23:22 -05:00
|
|
|
flip_comma, add_derive, matching_brace,
|
2018-08-12 10:50:16 -05:00
|
|
|
};
|
2018-08-09 04:47:34 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extend_selection() {
|
2018-08-22 03:56:36 -05:00
|
|
|
fn do_check(before: &str, afters: &[&str]) {
|
|
|
|
let (cursor, before) = extract_cursor(before);
|
|
|
|
let file = file(&before);
|
|
|
|
let mut range = TextRange::offset_len(cursor, 0.into());
|
|
|
|
for &after in afters {
|
|
|
|
range = extend_selection(&file, range)
|
|
|
|
.unwrap();
|
|
|
|
let actual = &before[range];
|
|
|
|
assert_eq!(after, actual);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
do_check(
|
|
|
|
r#"fn foo() { <|>1 + 1 }"#,
|
|
|
|
&["1", "1 + 1", "{ 1 + 1 }"],
|
|
|
|
);
|
|
|
|
|
|
|
|
do_check(
|
|
|
|
r#"
|
|
|
|
impl S {
|
|
|
|
<|> fn foo() {
|
|
|
|
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
&["fn foo() {\n\n }"]
|
|
|
|
);
|
2018-08-09 04:47:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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-17 08:04:34 -05:00
|
|
|
assert_eq_dbg(
|
2018-08-09 04:47:34 -05:00
|
|
|
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-17 08:04:34 -05:00
|
|
|
assert_eq_dbg(
|
2018-08-09 08:03:21 -05:00
|
|
|
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]
|
2018-08-14 04:38:20 -05:00
|
|
|
fn test_file_structure() {
|
2018-08-11 04:28:59 -05:00
|
|
|
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-14 03:20:09 -05:00
|
|
|
|
|
|
|
impl E {}
|
|
|
|
|
|
|
|
impl fmt::Debug for E {}
|
2018-08-11 04:28:59 -05:00
|
|
|
"#);
|
2018-08-14 03:20:09 -05:00
|
|
|
let symbols = file_structure(&file);
|
2018-08-17 08:04:34 -05:00
|
|
|
assert_eq_dbg(
|
2018-08-14 03:20:09 -05:00
|
|
|
r#"[StructureNode { parent: None, label: "Foo", navigation_range: [8; 11), node_range: [1; 26), kind: STRUCT_DEF },
|
2018-08-14 06:45:56 -05:00
|
|
|
StructureNode { parent: Some(0), label: "x", navigation_range: [18; 19), node_range: [18; 24), kind: NAMED_FIELD },
|
|
|
|
StructureNode { parent: None, label: "m", navigation_range: [32; 33), node_range: [28; 53), kind: MODULE },
|
|
|
|
StructureNode { parent: Some(2), label: "bar", navigation_range: [43; 46), node_range: [40; 51), kind: FN_DEF },
|
|
|
|
StructureNode { parent: None, label: "E", navigation_range: [60; 61), node_range: [55; 75), kind: ENUM_DEF },
|
|
|
|
StructureNode { parent: None, label: "T", navigation_range: [81; 82), node_range: [76; 88), kind: TYPE_DEF },
|
|
|
|
StructureNode { parent: None, label: "S", navigation_range: [96; 97), node_range: [89; 108), kind: STATIC_DEF },
|
|
|
|
StructureNode { parent: None, label: "C", navigation_range: [115; 116), node_range: [109; 127), kind: CONST_DEF },
|
|
|
|
StructureNode { parent: None, label: "impl E", navigation_range: [134; 135), node_range: [129; 138), kind: IMPL_ITEM },
|
|
|
|
StructureNode { parent: None, label: "impl fmt::Debug for E", navigation_range: [160; 161), node_range: [140; 164), kind: IMPL_ITEM }]"#,
|
2018-08-12 10:50:16 -05:00
|
|
|
&symbols,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_swap_comma() {
|
2018-08-15 15:24:20 -05:00
|
|
|
check_action(
|
2018-08-12 10:50:16 -05:00
|
|
|
"fn foo(x: i32,<|> y: Result<(), ()>) {}",
|
2018-08-15 15:24:20 -05:00
|
|
|
"fn foo(y: Result<(), ()>,<|> x: i32) {}",
|
|
|
|
|file, off| flip_comma(file, off).map(|f| f()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_add_derive() {
|
|
|
|
check_action(
|
|
|
|
"struct Foo { a: i32, <|>}",
|
|
|
|
"#[derive(<|>)]\nstruct Foo { a: i32, }",
|
|
|
|
|file, off| add_derive(file, off).map(|f| f()),
|
2018-08-16 05:11:20 -05:00
|
|
|
);
|
2018-08-17 11:54:08 -05:00
|
|
|
check_action(
|
|
|
|
"struct Foo { <|> a: i32, }",
|
|
|
|
"#[derive(<|>)]\nstruct Foo { a: i32, }",
|
|
|
|
|file, off| add_derive(file, off).map(|f| f()),
|
|
|
|
);
|
2018-08-16 05:11:20 -05:00
|
|
|
check_action(
|
|
|
|
"#[derive(Clone)]\nstruct Foo { a: i32<|>, }",
|
|
|
|
"#[derive(Clone<|>)]\nstruct Foo { a: i32, }",
|
|
|
|
|file, off| add_derive(file, off).map(|f| f()),
|
|
|
|
);
|
2018-08-11 04:28:59 -05:00
|
|
|
}
|
|
|
|
|
2018-08-15 16:23:22 -05:00
|
|
|
#[test]
|
|
|
|
fn test_matching_brace() {
|
|
|
|
fn do_check(before: &str, after: &str) {
|
|
|
|
let (pos, before) = extract_cursor(before);
|
|
|
|
let file = file(&before);
|
|
|
|
let new_pos = match matching_brace(&file, pos) {
|
|
|
|
None => pos,
|
|
|
|
Some(pos) => pos,
|
|
|
|
};
|
|
|
|
let actual = add_cursor(&before, new_pos);
|
|
|
|
assert_eq_text!(after, &actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
do_check(
|
|
|
|
"struct Foo { a: i32, }<|>",
|
|
|
|
"struct Foo <|>{ a: i32, }",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-08-17 14:00:13 -05:00
|
|
|
fn file(text: &str) -> ParsedFile {
|
|
|
|
ParsedFile::parse(text)
|
2018-08-09 04:47:34 -05:00
|
|
|
}
|
|
|
|
|
2018-08-17 14:00:13 -05:00
|
|
|
fn check_action<F: Fn(&ParsedFile, TextUnit) -> Option<ActionResult>>(
|
2018-08-12 10:50:16 -05:00
|
|
|
before: &str,
|
|
|
|
after: &str,
|
2018-08-15 15:24:20 -05:00
|
|
|
f: F,
|
2018-08-12 10:50:16 -05:00
|
|
|
) {
|
2018-08-15 15:24:20 -05:00
|
|
|
let (before_cursor_pos, before) = extract_cursor(before);
|
|
|
|
let file = file(&before);
|
|
|
|
let result = f(&file, before_cursor_pos).expect("code action is not applicable");
|
|
|
|
let actual = result.edit.apply(&before);
|
2018-08-15 16:23:22 -05:00
|
|
|
let actual_cursor_pos = match result.cursor_position {
|
2018-08-22 04:58:34 -05:00
|
|
|
None => result.edit.apply_to_offset(before_cursor_pos).unwrap(),
|
|
|
|
Some(off) => off,
|
2018-08-15 16:23:22 -05:00
|
|
|
};
|
|
|
|
let actual = add_cursor(&actual, actual_cursor_pos);
|
|
|
|
assert_eq_text!(after, &actual);
|
2018-08-15 15:24:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn extract_cursor(text: &str) -> (TextUnit, String) {
|
2018-08-12 10:50:16 -05:00
|
|
|
let cursor = "<|>";
|
2018-08-15 15:24:20 -05:00
|
|
|
let cursor_pos = match text.find(cursor) {
|
|
|
|
None => panic!("text should contain cursor marker"),
|
2018-08-12 10:50:16 -05:00
|
|
|
Some(pos) => pos,
|
|
|
|
};
|
2018-08-15 15:24:20 -05:00
|
|
|
let mut new_text = String::with_capacity(text.len() - cursor.len());
|
|
|
|
new_text.push_str(&text[..cursor_pos]);
|
|
|
|
new_text.push_str(&text[cursor_pos + cursor.len()..]);
|
2018-08-12 10:50:16 -05:00
|
|
|
let cursor_pos = TextUnit::from(cursor_pos as u32);
|
2018-08-15 15:24:20 -05:00
|
|
|
(cursor_pos, new_text)
|
2018-08-09 04:47:34 -05:00
|
|
|
}
|
2018-08-15 16:23:22 -05:00
|
|
|
|
|
|
|
fn add_cursor(text: &str, offset: TextUnit) -> String {
|
|
|
|
let offset: u32 = offset.into();
|
|
|
|
let offset: usize = offset as usize;
|
|
|
|
let mut res = String::new();
|
|
|
|
res.push_str(&text[..offset]);
|
|
|
|
res.push_str("<|>");
|
|
|
|
res.push_str(&text[offset..]);
|
|
|
|
res
|
|
|
|
}
|