2019-01-03 17:15:12 +00:00
|
|
|
mod runnables;
|
|
|
|
|
2018-10-31 20:41:43 +00:00
|
|
|
use ra_syntax::TextRange;
|
2018-12-30 20:43:30 +00:00
|
|
|
use test_utils::{assert_eq_dbg, assert_eq_text};
|
2018-08-17 13:04:34 +00:00
|
|
|
|
2018-10-20 19:43:36 +00:00
|
|
|
use ra_analysis::{
|
2018-10-31 20:41:43 +00:00
|
|
|
mock_analysis::{analysis_and_position, single_file, single_file_with_position, MockAnalysis},
|
2019-01-08 15:33:01 +00:00
|
|
|
AnalysisChange, CrateGraph, FileId, Query
|
2018-10-20 19:43:36 +00:00
|
|
|
};
|
|
|
|
|
2018-08-27 17:58:38 +00:00
|
|
|
#[test]
|
|
|
|
fn test_unresolved_module_diagnostic() {
|
2018-10-31 19:34:31 +00:00
|
|
|
let (analysis, file_id) = single_file("mod foo;");
|
|
|
|
let diagnostics = analysis.diagnostics(file_id).unwrap();
|
2018-08-27 17:58:38 +00:00
|
|
|
assert_eq_dbg(
|
2018-08-29 15:35:28 +00:00
|
|
|
r#"[Diagnostic {
|
|
|
|
message: "unresolved module",
|
|
|
|
range: [4; 7),
|
|
|
|
fix: Some(SourceChange {
|
|
|
|
label: "create module",
|
|
|
|
source_file_edits: [],
|
2018-12-21 09:30:28 +00:00
|
|
|
file_system_edits: [CreateFile { source_root: SourceRootId(0), path: "foo.rs" }],
|
2018-12-23 16:39:33 +00:00
|
|
|
cursor_position: None }),
|
2018-12-24 15:01:16 +00:00
|
|
|
severity: Error }]"#,
|
2018-08-27 17:58:38 +00:00
|
|
|
&diagnostics,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-06 13:10:25 +00:00
|
|
|
// FIXME: move this test to hir
|
2018-10-24 12:00:03 +00:00
|
|
|
#[test]
|
|
|
|
fn test_unresolved_module_diagnostic_no_diag_for_inline_mode() {
|
2018-10-31 19:34:31 +00:00
|
|
|
let (analysis, file_id) = single_file("mod foo {}");
|
|
|
|
let diagnostics = analysis.diagnostics(file_id).unwrap();
|
2018-10-24 12:00:03 +00:00
|
|
|
assert_eq_dbg(r#"[]"#, &diagnostics);
|
|
|
|
}
|
2018-08-28 17:29:36 +00:00
|
|
|
|
2018-08-21 15:30:10 +00:00
|
|
|
#[test]
|
|
|
|
fn test_resolve_parent_module() {
|
2018-10-31 20:41:43 +00:00
|
|
|
let (analysis, pos) = analysis_and_position(
|
|
|
|
"
|
2018-10-31 19:34:31 +00:00
|
|
|
//- /lib.rs
|
|
|
|
mod foo;
|
|
|
|
//- /foo.rs
|
|
|
|
<|>// empty
|
2018-10-31 20:41:43 +00:00
|
|
|
",
|
|
|
|
);
|
2018-11-05 11:57:41 +00:00
|
|
|
let symbols = analysis.parent_module(pos).unwrap();
|
2018-08-21 15:30:10 +00:00
|
|
|
assert_eq_dbg(
|
2019-01-02 20:35:51 +00:00
|
|
|
r#"[NavigationTarget { file_id: FileId(1), name: "foo", kind: MODULE, range: [4; 7), ptr: None }]"#,
|
2018-11-05 11:10:20 +00:00
|
|
|
&symbols,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_resolve_parent_module_for_inline() {
|
|
|
|
let (analysis, pos) = analysis_and_position(
|
|
|
|
"
|
|
|
|
//- /lib.rs
|
|
|
|
mod foo {
|
|
|
|
mod bar {
|
|
|
|
mod baz { <|> }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
2018-11-05 11:57:41 +00:00
|
|
|
let symbols = analysis.parent_module(pos).unwrap();
|
2018-11-05 11:10:20 +00:00
|
|
|
assert_eq_dbg(
|
2019-01-06 16:58:10 +00:00
|
|
|
r#"[NavigationTarget { file_id: FileId(1), name: "baz", kind: MODULE, range: [36; 39), ptr: None }]"#,
|
2018-08-21 15:30:10 +00:00
|
|
|
&symbols,
|
|
|
|
);
|
|
|
|
}
|
2018-08-31 16:14:08 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_resolve_crate_root() {
|
2018-10-31 20:41:43 +00:00
|
|
|
let mock = MockAnalysis::with_files(
|
|
|
|
"
|
2018-12-29 23:03:52 +00:00
|
|
|
//- /bar.rs
|
2018-10-31 19:34:31 +00:00
|
|
|
mod foo;
|
2018-12-29 23:03:52 +00:00
|
|
|
//- /bar/foo.rs
|
2018-10-31 19:34:31 +00:00
|
|
|
// emtpy <|>
|
2018-10-31 20:41:43 +00:00
|
|
|
",
|
|
|
|
);
|
2018-12-29 23:03:52 +00:00
|
|
|
let root_file = mock.id_of("/bar.rs");
|
|
|
|
let mod_file = mock.id_of("/bar/foo.rs");
|
2018-10-31 19:34:31 +00:00
|
|
|
let mut host = mock.analysis_host();
|
|
|
|
assert!(host.analysis().crate_for(mod_file).unwrap().is_empty());
|
|
|
|
|
2018-11-28 00:25:20 +00:00
|
|
|
let mut crate_graph = CrateGraph::default();
|
2018-10-31 19:34:31 +00:00
|
|
|
let crate_id = crate_graph.add_crate_root(root_file);
|
2018-10-25 07:57:55 +00:00
|
|
|
let mut change = AnalysisChange::new();
|
|
|
|
change.set_crate_graph(crate_graph);
|
|
|
|
host.apply_change(change);
|
2018-08-31 16:14:08 +00:00
|
|
|
|
2018-10-31 19:34:31 +00:00
|
|
|
assert_eq!(host.analysis().crate_for(mod_file).unwrap(), vec![crate_id]);
|
2018-08-31 16:14:08 +00:00
|
|
|
}
|
2018-10-09 14:08:17 +00:00
|
|
|
|
2018-10-18 17:40:12 +00:00
|
|
|
fn get_all_refs(text: &str) -> Vec<(FileId, TextRange)> {
|
2018-10-31 19:34:31 +00:00
|
|
|
let (analysis, position) = single_file_with_position(text);
|
2018-11-05 11:57:41 +00:00
|
|
|
analysis.find_all_refs(position).unwrap()
|
2018-10-18 17:40:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_find_all_refs_for_local() {
|
|
|
|
let code = r#"
|
|
|
|
fn main() {
|
|
|
|
let mut i = 1;
|
|
|
|
let j = 1;
|
|
|
|
i = i<|> + j;
|
|
|
|
|
|
|
|
{
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = 5;
|
|
|
|
}"#;
|
|
|
|
|
|
|
|
let refs = get_all_refs(code);
|
|
|
|
assert_eq!(refs.len(), 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_find_all_refs_for_param_inside() {
|
|
|
|
let code = r#"
|
|
|
|
fn foo(i : u32) -> u32 {
|
|
|
|
i<|>
|
|
|
|
}"#;
|
|
|
|
|
|
|
|
let refs = get_all_refs(code);
|
|
|
|
assert_eq!(refs.len(), 2);
|
2018-10-20 19:43:36 +00:00
|
|
|
}
|
2018-10-24 15:37:25 +00:00
|
|
|
|
2018-10-24 18:24:32 +00:00
|
|
|
#[test]
|
|
|
|
fn test_find_all_refs_for_fn_param() {
|
|
|
|
let code = r#"
|
|
|
|
fn foo(i<|> : u32) -> u32 {
|
|
|
|
i
|
|
|
|
}"#;
|
|
|
|
|
|
|
|
let refs = get_all_refs(code);
|
|
|
|
assert_eq!(refs.len(), 2);
|
|
|
|
}
|
2018-12-30 20:43:30 +00:00
|
|
|
#[test]
|
|
|
|
fn test_rename_for_local() {
|
|
|
|
test_rename(
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
let mut i = 1;
|
|
|
|
let j = 1;
|
|
|
|
i = i<|> + j;
|
|
|
|
|
|
|
|
{
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = 5;
|
|
|
|
}"#,
|
|
|
|
"k",
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
let mut k = 1;
|
|
|
|
let j = 1;
|
|
|
|
k = k + j;
|
|
|
|
|
|
|
|
{
|
|
|
|
k = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
k = 5;
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rename_for_param_inside() {
|
|
|
|
test_rename(
|
|
|
|
r#"
|
|
|
|
fn foo(i : u32) -> u32 {
|
|
|
|
i<|>
|
|
|
|
}"#,
|
|
|
|
"j",
|
|
|
|
r#"
|
|
|
|
fn foo(j : u32) -> u32 {
|
|
|
|
j
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rename_refs_for_fn_param() {
|
|
|
|
test_rename(
|
|
|
|
r#"
|
|
|
|
fn foo(i<|> : u32) -> u32 {
|
|
|
|
i
|
|
|
|
}"#,
|
|
|
|
"new_name",
|
|
|
|
r#"
|
|
|
|
fn foo(new_name : u32) -> u32 {
|
|
|
|
new_name
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rename_for_mut_param() {
|
|
|
|
test_rename(
|
|
|
|
r#"
|
|
|
|
fn foo(mut i<|> : u32) -> u32 {
|
|
|
|
i
|
|
|
|
}"#,
|
|
|
|
"new_name",
|
|
|
|
r#"
|
|
|
|
fn foo(mut new_name : u32) -> u32 {
|
|
|
|
new_name
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
2019-01-03 18:28:35 +00:00
|
|
|
|
2018-12-30 20:43:30 +00:00
|
|
|
fn test_rename(text: &str, new_name: &str, expected: &str) {
|
|
|
|
let (analysis, position) = single_file_with_position(text);
|
|
|
|
let edits = analysis.rename(position, new_name).unwrap();
|
2019-01-03 16:06:51 +00:00
|
|
|
let mut text_edit_bulder = ra_text_edit::TextEditBuilder::default();
|
2018-12-30 20:43:30 +00:00
|
|
|
let mut file_id: Option<FileId> = None;
|
|
|
|
for edit in edits {
|
|
|
|
file_id = Some(edit.file_id);
|
|
|
|
for atom in edit.edit.as_atoms() {
|
|
|
|
text_edit_bulder.replace(atom.delete, atom.insert.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let result = text_edit_bulder
|
|
|
|
.finish()
|
|
|
|
.apply(&*analysis.file_text(file_id.unwrap()));
|
|
|
|
assert_eq_text!(expected, &*result);
|
|
|
|
}
|
2019-01-03 18:28:35 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn world_symbols_include_stuff_from_macros() {
|
|
|
|
let (analysis, _) = single_file(
|
|
|
|
"
|
|
|
|
salsa::query_group! {
|
|
|
|
pub trait HirDatabase: SyntaxDatabase {}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut symbols = analysis.symbol_search(Query::new("Hir".into())).unwrap();
|
|
|
|
let s = symbols.pop().unwrap();
|
|
|
|
assert_eq!(s.name(), "HirDatabase");
|
|
|
|
assert_eq!(s.range(), TextRange::from_to(33.into(), 44.into()));
|
|
|
|
}
|