2019-01-03 11:15:12 -06:00
|
|
|
mod runnables;
|
|
|
|
|
2018-10-31 15:41:43 -05:00
|
|
|
use ra_syntax::TextRange;
|
2018-12-30 14:43:30 -06:00
|
|
|
use test_utils::{assert_eq_dbg, assert_eq_text};
|
2018-08-17 08:04:34 -05:00
|
|
|
|
2018-10-20 14:43:36 -05:00
|
|
|
use ra_analysis::{
|
2018-10-31 15:41:43 -05:00
|
|
|
mock_analysis::{analysis_and_position, single_file, single_file_with_position, MockAnalysis},
|
2019-01-08 09:33:01 -06:00
|
|
|
AnalysisChange, CrateGraph, FileId, Query
|
2018-10-20 14:43:36 -05:00
|
|
|
};
|
|
|
|
|
2018-08-27 12:58:38 -05:00
|
|
|
#[test]
|
|
|
|
fn test_unresolved_module_diagnostic() {
|
2018-10-31 14:34:31 -05:00
|
|
|
let (analysis, file_id) = single_file("mod foo;");
|
|
|
|
let diagnostics = analysis.diagnostics(file_id).unwrap();
|
2018-08-27 12:58:38 -05:00
|
|
|
assert_eq_dbg(
|
2018-08-29 10:35:28 -05:00
|
|
|
r#"[Diagnostic {
|
|
|
|
message: "unresolved module",
|
|
|
|
range: [4; 7),
|
|
|
|
fix: Some(SourceChange {
|
|
|
|
label: "create module",
|
|
|
|
source_file_edits: [],
|
2018-12-21 03:30:28 -06:00
|
|
|
file_system_edits: [CreateFile { source_root: SourceRootId(0), path: "foo.rs" }],
|
2018-12-23 10:39:33 -06:00
|
|
|
cursor_position: None }),
|
2018-12-24 09:01:16 -06:00
|
|
|
severity: Error }]"#,
|
2018-08-27 12:58:38 -05:00
|
|
|
&diagnostics,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-06 07:10:25 -06:00
|
|
|
// FIXME: move this test to hir
|
2018-10-24 07:00:03 -05:00
|
|
|
#[test]
|
|
|
|
fn test_unresolved_module_diagnostic_no_diag_for_inline_mode() {
|
2018-10-31 14:34:31 -05:00
|
|
|
let (analysis, file_id) = single_file("mod foo {}");
|
|
|
|
let diagnostics = analysis.diagnostics(file_id).unwrap();
|
2018-10-24 07:00:03 -05:00
|
|
|
assert_eq_dbg(r#"[]"#, &diagnostics);
|
|
|
|
}
|
2018-08-28 12:29:36 -05:00
|
|
|
|
2018-08-21 10:30:10 -05:00
|
|
|
#[test]
|
|
|
|
fn test_resolve_parent_module() {
|
2018-10-31 15:41:43 -05:00
|
|
|
let (analysis, pos) = analysis_and_position(
|
|
|
|
"
|
2018-10-31 14:34:31 -05:00
|
|
|
//- /lib.rs
|
|
|
|
mod foo;
|
|
|
|
//- /foo.rs
|
|
|
|
<|>// empty
|
2018-10-31 15:41:43 -05:00
|
|
|
",
|
|
|
|
);
|
2018-11-05 05:57:41 -06:00
|
|
|
let symbols = analysis.parent_module(pos).unwrap();
|
2018-08-21 10:30:10 -05:00
|
|
|
assert_eq_dbg(
|
2019-01-02 14:35:51 -06:00
|
|
|
r#"[NavigationTarget { file_id: FileId(1), name: "foo", kind: MODULE, range: [4; 7), ptr: None }]"#,
|
2018-11-05 05:10:20 -06: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 05:57:41 -06:00
|
|
|
let symbols = analysis.parent_module(pos).unwrap();
|
2018-11-05 05:10:20 -06:00
|
|
|
assert_eq_dbg(
|
2019-01-06 10:58:10 -06:00
|
|
|
r#"[NavigationTarget { file_id: FileId(1), name: "baz", kind: MODULE, range: [36; 39), ptr: None }]"#,
|
2018-08-21 10:30:10 -05:00
|
|
|
&symbols,
|
|
|
|
);
|
|
|
|
}
|
2018-08-31 11:14:08 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_resolve_crate_root() {
|
2018-10-31 15:41:43 -05:00
|
|
|
let mock = MockAnalysis::with_files(
|
|
|
|
"
|
2018-12-29 17:03:52 -06:00
|
|
|
//- /bar.rs
|
2018-10-31 14:34:31 -05:00
|
|
|
mod foo;
|
2018-12-29 17:03:52 -06:00
|
|
|
//- /bar/foo.rs
|
2018-10-31 14:34:31 -05:00
|
|
|
// emtpy <|>
|
2018-10-31 15:41:43 -05:00
|
|
|
",
|
|
|
|
);
|
2018-12-29 17:03:52 -06:00
|
|
|
let root_file = mock.id_of("/bar.rs");
|
|
|
|
let mod_file = mock.id_of("/bar/foo.rs");
|
2018-10-31 14:34:31 -05:00
|
|
|
let mut host = mock.analysis_host();
|
|
|
|
assert!(host.analysis().crate_for(mod_file).unwrap().is_empty());
|
|
|
|
|
2018-11-27 18:25:20 -06:00
|
|
|
let mut crate_graph = CrateGraph::default();
|
2018-10-31 14:34:31 -05:00
|
|
|
let crate_id = crate_graph.add_crate_root(root_file);
|
2018-10-25 02:57:55 -05:00
|
|
|
let mut change = AnalysisChange::new();
|
|
|
|
change.set_crate_graph(crate_graph);
|
|
|
|
host.apply_change(change);
|
2018-08-31 11:14:08 -05:00
|
|
|
|
2018-10-31 14:34:31 -05:00
|
|
|
assert_eq!(host.analysis().crate_for(mod_file).unwrap(), vec![crate_id]);
|
2018-08-31 11:14:08 -05:00
|
|
|
}
|
2018-10-09 09:08:17 -05:00
|
|
|
|
2018-10-18 12:40:12 -05:00
|
|
|
fn get_all_refs(text: &str) -> Vec<(FileId, TextRange)> {
|
2018-10-31 14:34:31 -05:00
|
|
|
let (analysis, position) = single_file_with_position(text);
|
2018-11-05 05:57:41 -06:00
|
|
|
analysis.find_all_refs(position).unwrap()
|
2018-10-18 12:40:12 -05: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 14:43:36 -05:00
|
|
|
}
|
2018-10-24 10:37:25 -05:00
|
|
|
|
2018-10-24 13:24:32 -05: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 14:43:30 -06: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 12:28:35 -06:00
|
|
|
|
2018-12-30 14:43:30 -06: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 10:06:51 -06:00
|
|
|
let mut text_edit_bulder = ra_text_edit::TextEditBuilder::default();
|
2018-12-30 14:43:30 -06: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 12:28:35 -06: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()));
|
|
|
|
}
|