aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/references
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide/src/references')
-rw-r--r--crates/ra_ide/src/references/classify.rs30
-rw-r--r--crates/ra_ide/src/references/rename.rs28
2 files changed, 27 insertions, 31 deletions
diff --git a/crates/ra_ide/src/references/classify.rs b/crates/ra_ide/src/references/classify.rs
index 478e18871..91b21429a 100644
--- a/crates/ra_ide/src/references/classify.rs
+++ b/crates/ra_ide/src/references/classify.rs
@@ -1,34 +1,32 @@
1//! Functions that are used to classify an element from its definition or reference. 1//! Functions that are used to classify an element from its definition or reference.
2 2
3use hir::{InFile, PathResolution, SourceBinder}; 3use hir::{PathResolution, Semantics};
4use ra_ide_db::defs::NameDefinition;
5use ra_ide_db::RootDatabase;
4use ra_prof::profile; 6use ra_prof::profile;
5use ra_syntax::{ast, AstNode}; 7use ra_syntax::{ast, AstNode};
6use test_utils::tested_by; 8use test_utils::tested_by;
7 9
8use super::NameDefinition; 10pub use ra_ide_db::defs::{from_module_def, from_struct_field};
9use ra_ide_db::RootDatabase;
10
11pub use ra_ide_db::defs::{classify_name, from_module_def, from_struct_field};
12 11
13pub(crate) fn classify_name_ref( 12pub(crate) fn classify_name_ref(
14 sb: &mut SourceBinder<RootDatabase>, 13 sema: &Semantics<RootDatabase>,
15 name_ref: InFile<&ast::NameRef>, 14 name_ref: &ast::NameRef,
16) -> Option<NameDefinition> { 15) -> Option<NameDefinition> {
17 let _p = profile("classify_name_ref"); 16 let _p = profile("classify_name_ref");
18 17
19 let parent = name_ref.value.syntax().parent()?; 18 let parent = name_ref.syntax().parent()?;
20 let analyzer = sb.analyze(name_ref.map(|it| it.syntax()), None);
21 19
22 if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) { 20 if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) {
23 tested_by!(goto_def_for_methods); 21 tested_by!(goto_def_for_methods);
24 if let Some(func) = analyzer.resolve_method_call(&method_call) { 22 if let Some(func) = sema.resolve_method_call(&method_call) {
25 return Some(from_module_def(func.into())); 23 return Some(from_module_def(func.into()));
26 } 24 }
27 } 25 }
28 26
29 if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) { 27 if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) {
30 tested_by!(goto_def_for_fields); 28 tested_by!(goto_def_for_fields);
31 if let Some(field) = analyzer.resolve_field(&field_expr) { 29 if let Some(field) = sema.resolve_field(&field_expr) {
32 return Some(from_struct_field(field)); 30 return Some(from_struct_field(field));
33 } 31 }
34 } 32 }
@@ -36,22 +34,20 @@ pub(crate) fn classify_name_ref(
36 if let Some(record_field) = ast::RecordField::cast(parent.clone()) { 34 if let Some(record_field) = ast::RecordField::cast(parent.clone()) {
37 tested_by!(goto_def_for_record_fields); 35 tested_by!(goto_def_for_record_fields);
38 tested_by!(goto_def_for_field_init_shorthand); 36 tested_by!(goto_def_for_field_init_shorthand);
39 if let Some(field_def) = analyzer.resolve_record_field(&record_field) { 37 if let Some(field_def) = sema.resolve_record_field(&record_field) {
40 return Some(from_struct_field(field_def)); 38 return Some(from_struct_field(field_def));
41 } 39 }
42 } 40 }
43 41
44 if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) { 42 if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) {
45 tested_by!(goto_def_for_macros); 43 tested_by!(goto_def_for_macros);
46 if let Some(macro_def) = 44 if let Some(macro_def) = sema.resolve_macro_call(&macro_call) {
47 analyzer.resolve_macro_call(sb.db, name_ref.with_value(&macro_call))
48 {
49 return Some(NameDefinition::Macro(macro_def)); 45 return Some(NameDefinition::Macro(macro_def));
50 } 46 }
51 } 47 }
52 48
53 let path = name_ref.value.syntax().ancestors().find_map(ast::Path::cast)?; 49 let path = name_ref.syntax().ancestors().find_map(ast::Path::cast)?;
54 let resolved = analyzer.resolve_path(sb.db, &path)?; 50 let resolved = sema.resolve_path(&path)?;
55 let res = match resolved { 51 let res = match resolved {
56 PathResolution::Def(def) => from_module_def(def), 52 PathResolution::Def(def) => from_module_def(def),
57 PathResolution::AssocItem(item) => { 53 PathResolution::AssocItem(item) => {
diff --git a/crates/ra_ide/src/references/rename.rs b/crates/ra_ide/src/references/rename.rs
index bdb90020b..5b4bcf434 100644
--- a/crates/ra_ide/src/references/rename.rs
+++ b/crates/ra_ide/src/references/rename.rs
@@ -1,7 +1,7 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use hir::ModuleSource; 3use hir::{ModuleSource, Semantics};
4use ra_db::{RelativePath, RelativePathBuf, SourceDatabase, SourceDatabaseExt}; 4use ra_db::{RelativePath, RelativePathBuf, SourceDatabaseExt};
5use ra_ide_db::RootDatabase; 5use ra_ide_db::RootDatabase;
6use ra_syntax::{ 6use ra_syntax::{
7 algo::find_node_at_offset, ast, lex_single_valid_syntax_kind, AstNode, SyntaxKind, SyntaxNode, 7 algo::find_node_at_offset, ast, lex_single_valid_syntax_kind, AstNode, SyntaxKind, SyntaxNode,
@@ -24,15 +24,16 @@ pub(crate) fn rename(
24 _ => return None, 24 _ => return None,
25 } 25 }
26 26
27 let parse = db.parse(position.file_id); 27 let sema = Semantics::new(db);
28 let source_file = sema.parse(position.file_id);
28 if let Some((ast_name, ast_module)) = 29 if let Some((ast_name, ast_module)) =
29 find_name_and_module_at_offset(parse.tree().syntax(), position) 30 find_name_and_module_at_offset(source_file.syntax(), position)
30 { 31 {
31 let range = ast_name.syntax().text_range(); 32 let range = ast_name.syntax().text_range();
32 rename_mod(db, &ast_name, &ast_module, position, new_name) 33 rename_mod(&sema, &ast_name, &ast_module, position, new_name)
33 .map(|info| RangeInfo::new(range, info)) 34 .map(|info| RangeInfo::new(range, info))
34 } else { 35 } else {
35 rename_reference(db, position, new_name) 36 rename_reference(sema.db, position, new_name)
36 } 37 }
37} 38}
38 39
@@ -54,7 +55,7 @@ fn source_edit_from_file_id_range(
54} 55}
55 56
56fn rename_mod( 57fn rename_mod(
57 db: &RootDatabase, 58 sema: &Semantics<RootDatabase>,
58 ast_name: &ast::Name, 59 ast_name: &ast::Name,
59 ast_module: &ast::Module, 60 ast_module: &ast::Module,
60 position: FilePosition, 61 position: FilePosition,
@@ -62,13 +63,12 @@ fn rename_mod(
62) -> Option<SourceChange> { 63) -> Option<SourceChange> {
63 let mut source_file_edits = Vec::new(); 64 let mut source_file_edits = Vec::new();
64 let mut file_system_edits = Vec::new(); 65 let mut file_system_edits = Vec::new();
65 let module_src = hir::InFile { file_id: position.file_id.into(), value: ast_module.clone() }; 66 if let Some(module) = sema.to_def(ast_module) {
66 if let Some(module) = hir::SourceBinder::new(db).to_def(module_src) { 67 let src = module.definition_source(sema.db);
67 let src = module.definition_source(db); 68 let file_id = src.file_id.original_file(sema.db);
68 let file_id = src.file_id.original_file(db);
69 match src.value { 69 match src.value {
70 ModuleSource::SourceFile(..) => { 70 ModuleSource::SourceFile(..) => {
71 let mod_path: RelativePathBuf = db.file_relative_path(file_id); 71 let mod_path: RelativePathBuf = sema.db.file_relative_path(file_id);
72 // mod is defined in path/to/dir/mod.rs 72 // mod is defined in path/to/dir/mod.rs
73 let dst_path = if mod_path.file_stem() == Some("mod") { 73 let dst_path = if mod_path.file_stem() == Some("mod") {
74 mod_path 74 mod_path
@@ -82,7 +82,7 @@ fn rename_mod(
82 if let Some(path) = dst_path { 82 if let Some(path) = dst_path {
83 let move_file = FileSystemEdit::MoveFile { 83 let move_file = FileSystemEdit::MoveFile {
84 src: file_id, 84 src: file_id,
85 dst_source_root: db.file_source_root(position.file_id), 85 dst_source_root: sema.db.file_source_root(position.file_id),
86 dst_path: path, 86 dst_path: path,
87 }; 87 };
88 file_system_edits.push(move_file); 88 file_system_edits.push(move_file);
@@ -98,7 +98,7 @@ fn rename_mod(
98 }; 98 };
99 source_file_edits.push(edit); 99 source_file_edits.push(edit);
100 100
101 if let Some(RangeInfo { range: _, info: refs }) = find_all_refs(db, position, None) { 101 if let Some(RangeInfo { range: _, info: refs }) = find_all_refs(sema.db, position, None) {
102 let ref_edits = refs.references.into_iter().map(|reference| { 102 let ref_edits = refs.references.into_iter().map(|reference| {
103 source_edit_from_file_id_range( 103 source_edit_from_file_id_range(
104 reference.file_range.file_id, 104 reference.file_range.file_id,