aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorEkaterina Babshukova <[email protected]>2019-09-21 17:35:39 +0100
committerEkaterina Babshukova <[email protected]>2019-10-22 21:47:31 +0100
commitd6ae1b5f0f3e1b9b7f85dcdbd98e0c336b7a882d (patch)
treeb945f83081f91ad49b8636c3aa25761bbee22858 /crates
parent11577288c23b1f2de1aaba0137c9c2d6344b9a92 (diff)
refactor name_ref_kind.rs
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_ide_api/src/goto_definition.rs3
-rw-r--r--crates/ra_ide_api/src/hover.rs3
-rw-r--r--crates/ra_ide_api/src/lib.rs2
-rw-r--r--crates/ra_ide_api/src/name_kind.rs (renamed from crates/ra_ide_api/src/name_ref_kind.rs)67
-rw-r--r--crates/ra_ide_api/src/references.rs47
-rw-r--r--crates/ra_ide_api/src/syntax_highlighting.rs3
6 files changed, 61 insertions, 64 deletions
diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs
index 13e42bb35..7626a3fd1 100644
--- a/crates/ra_ide_api/src/goto_definition.rs
+++ b/crates/ra_ide_api/src/goto_definition.rs
@@ -10,7 +10,7 @@ use ra_syntax::{
10use crate::{ 10use crate::{
11 db::RootDatabase, 11 db::RootDatabase,
12 display::ShortLabel, 12 display::ShortLabel,
13 name_ref_kind::{classify_name_ref, NameKind::*}, 13 name_kind::{classify_name_ref, NameKind::*},
14 FilePosition, NavigationTarget, RangeInfo, 14 FilePosition, NavigationTarget, RangeInfo,
15}; 15};
16 16
@@ -60,7 +60,6 @@ pub(crate) fn reference_definition(
60 Some(Macro(mac)) => return Exact(NavigationTarget::from_macro_def(db, mac)), 60 Some(Macro(mac)) => return Exact(NavigationTarget::from_macro_def(db, mac)),
61 Some(FieldAccess(field)) => return Exact(NavigationTarget::from_field(db, field)), 61 Some(FieldAccess(field)) => return Exact(NavigationTarget::from_field(db, field)),
62 Some(AssocItem(assoc)) => return Exact(NavigationTarget::from_assoc_item(db, assoc)), 62 Some(AssocItem(assoc)) => return Exact(NavigationTarget::from_assoc_item(db, assoc)),
63 Some(Method(func)) => return Exact(NavigationTarget::from_def_source(db, func)),
64 Some(Def(def)) => match NavigationTarget::from_def(db, def) { 63 Some(Def(def)) => match NavigationTarget::from_def(db, def) {
65 Some(nav) => return Exact(nav), 64 Some(nav) => return Exact(nav),
66 None => return Approximate(vec![]), 65 None => return Approximate(vec![]),
diff --git a/crates/ra_ide_api/src/hover.rs b/crates/ra_ide_api/src/hover.rs
index 5c2549dc3..170278904 100644
--- a/crates/ra_ide_api/src/hover.rs
+++ b/crates/ra_ide_api/src/hover.rs
@@ -14,7 +14,7 @@ use crate::{
14 description_from_symbol, docs_from_symbol, macro_label, rust_code_markup, 14 description_from_symbol, docs_from_symbol, macro_label, rust_code_markup,
15 rust_code_markup_with_doc, ShortLabel, 15 rust_code_markup_with_doc, ShortLabel,
16 }, 16 },
17 name_ref_kind::{classify_name_ref, NameKind::*}, 17 name_kind::{classify_name_ref, NameKind::*},
18 FilePosition, FileRange, RangeInfo, 18 FilePosition, FileRange, RangeInfo,
19}; 19};
20 20
@@ -104,7 +104,6 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
104 let mut no_fallback = false; 104 let mut no_fallback = false;
105 105
106 match classify_name_ref(db, &analyzer, &name_ref) { 106 match classify_name_ref(db, &analyzer, &name_ref) {
107 Some(Method(it)) => res.extend(from_def_source(db, it)),
108 Some(Macro(it)) => { 107 Some(Macro(it)) => {
109 let src = it.source(db); 108 let src = it.source(db);
110 res.extend(hover_text(src.ast.doc_comment_text(), Some(macro_label(&src.ast)))); 109 res.extend(hover_text(src.ast.doc_comment_text(), Some(macro_label(&src.ast))));
diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs
index f7fd42f65..cbf79ce03 100644
--- a/crates/ra_ide_api/src/lib.rs
+++ b/crates/ra_ide_api/src/lib.rs
@@ -19,7 +19,7 @@ mod feature_flags;
19mod status; 19mod status;
20mod completion; 20mod completion;
21mod runnables; 21mod runnables;
22mod name_ref_kind; 22mod name_kind;
23mod goto_definition; 23mod goto_definition;
24mod goto_type_definition; 24mod goto_type_definition;
25mod extend_selection; 25mod extend_selection;
diff --git a/crates/ra_ide_api/src/name_ref_kind.rs b/crates/ra_ide_api/src/name_kind.rs
index eb8caabfe..64adb1784 100644
--- a/crates/ra_ide_api/src/name_ref_kind.rs
+++ b/crates/ra_ide_api/src/name_kind.rs
@@ -1,13 +1,13 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use hir::Either; 3use hir::{Either, FromSource};
4use ra_db::FileId;
4use ra_syntax::{ast, AstNode, AstPtr}; 5use ra_syntax::{ast, AstNode, AstPtr};
5use test_utils::tested_by; 6use test_utils::tested_by;
6 7
7use crate::db::RootDatabase; 8use crate::db::RootDatabase;
8 9
9pub enum NameKind { 10pub enum NameKind {
10 Method(hir::Function),
11 Macro(hir::MacroDef), 11 Macro(hir::MacroDef),
12 FieldAccess(hir::StructField), 12 FieldAccess(hir::StructField),
13 AssocItem(hir::AssocItem), 13 AssocItem(hir::AssocItem),
@@ -29,7 +29,7 @@ pub(crate) fn classify_name_ref(
29 if let Some(method_call) = name_ref.syntax().parent().and_then(ast::MethodCallExpr::cast) { 29 if let Some(method_call) = name_ref.syntax().parent().and_then(ast::MethodCallExpr::cast) {
30 tested_by!(goto_definition_works_for_methods); 30 tested_by!(goto_definition_works_for_methods);
31 if let Some(func) = analyzer.resolve_method_call(&method_call) { 31 if let Some(func) = analyzer.resolve_method_call(&method_call) {
32 return Some(Method(func)); 32 return Some(AssocItem(func.into()));
33 } 33 }
34 } 34 }
35 35
@@ -59,17 +59,12 @@ pub(crate) fn classify_name_ref(
59 if let Some(field_expr) = name_ref.syntax().parent().and_then(ast::RecordField::cast) { 59 if let Some(field_expr) = name_ref.syntax().parent().and_then(ast::RecordField::cast) {
60 tested_by!(goto_definition_works_for_record_fields); 60 tested_by!(goto_definition_works_for_record_fields);
61 61
62 let record_lit = field_expr.syntax().ancestors().find_map(ast::RecordLit::cast); 62 if let Some(record_lit) = field_expr.syntax().ancestors().find_map(ast::RecordLit::cast) {
63 63 let variant_def = analyzer.resolve_record_literal(&record_lit)?;
64 if let Some(ty) = record_lit.and_then(|lit| analyzer.type_of(db, &lit.into())) { 64 let hir_path = hir::Path::from_name_ref(name_ref);
65 if let Some((hir::Adt::Struct(s), _)) = ty.as_adt() { 65 let hir_name = hir_path.as_ident()?;
66 let hir_path = hir::Path::from_name_ref(name_ref); 66 let field = variant_def.field(db, hir_name)?;
67 let hir_name = hir_path.as_ident().unwrap(); 67 return Some(FieldAccess(field));
68
69 if let Some(field) = s.field(db, hir_name) {
70 return Some(FieldAccess(field));
71 }
72 }
73 } 68 }
74 } 69 }
75 70
@@ -96,3 +91,47 @@ pub(crate) fn classify_name_ref(
96 91
97 None 92 None
98} 93}
94
95pub(crate) fn classify_name(
96 db: &RootDatabase,
97 file_id: FileId,
98 name: &ast::Name,
99) -> Option<NameKind> {
100 use NameKind::*;
101
102 let parent = name.syntax().parent()?;
103 let file_id = file_id.into();
104
105 if let Some(pat) = ast::BindPat::cast(parent.clone()) {
106 return Some(Pat(AstPtr::new(&pat)));
107 }
108 if let Some(var) = ast::EnumVariant::cast(parent.clone()) {
109 let src = hir::Source { file_id, ast: var };
110 let var = hir::EnumVariant::from_source(db, src)?;
111 return Some(Def(var.into()));
112 }
113 if let Some(field) = ast::RecordFieldDef::cast(parent.clone()) {
114 let src = hir::Source { file_id, ast: hir::FieldSource::Named(field) };
115 let field = hir::StructField::from_source(db, src)?;
116 return Some(FieldAccess(field));
117 }
118 if let Some(field) = ast::TupleFieldDef::cast(parent.clone()) {
119 let src = hir::Source { file_id, ast: hir::FieldSource::Pos(field) };
120 let field = hir::StructField::from_source(db, src)?;
121 return Some(FieldAccess(field));
122 }
123 if let Some(_) = parent.parent().and_then(ast::ItemList::cast) {
124 let ast = ast::ImplItem::cast(parent.clone())?;
125 let src = hir::Source { file_id, ast };
126 let item = hir::AssocItem::from_source(db, src)?;
127 return Some(AssocItem(item));
128 }
129 if let Some(item) = ast::ModuleItem::cast(parent.clone()) {
130 let src = hir::Source { file_id, ast: item };
131 let def = hir::ModuleDef::from_source(db, src)?;
132 return Some(Def(def));
133 }
134 // FIXME: TYPE_PARAM, ALIAS, MACRO_CALL; Union
135
136 None
137}
diff --git a/crates/ra_ide_api/src/references.rs b/crates/ra_ide_api/src/references.rs
index 9335bc8ca..6a8407c51 100644
--- a/crates/ra_ide_api/src/references.rs
+++ b/crates/ra_ide_api/src/references.rs
@@ -1,14 +1,14 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use hir::{FromSource, ModuleSource}; 3use hir::ModuleSource;
4use ra_db::{SourceDatabase, SourceDatabaseExt}; 4use ra_db::{SourceDatabase, SourceDatabaseExt};
5use ra_syntax::{algo::find_node_at_offset, ast, AstNode, AstPtr, SyntaxNode}; 5use ra_syntax::{algo::find_node_at_offset, ast, AstNode, SyntaxNode};
6use relative_path::{RelativePath, RelativePathBuf}; 6use relative_path::{RelativePath, RelativePathBuf};
7 7
8use crate::{ 8use crate::{
9 db::RootDatabase, 9 db::RootDatabase,
10 name_ref_kind::{ 10 name_kind::{
11 classify_name_ref, 11 classify_name, classify_name_ref,
12 NameKind::{self, *}, 12 NameKind::{self, *},
13 }, 13 },
14 FileId, FilePosition, FileRange, FileSystemEdit, NavigationTarget, RangeInfo, SourceChange, 14 FileId, FilePosition, FileRange, FileSystemEdit, NavigationTarget, RangeInfo, SourceChange,
@@ -64,7 +64,6 @@ pub(crate) fn find_all_refs(
64 Macro(mac) => NavigationTarget::from_macro_def(db, mac), 64 Macro(mac) => NavigationTarget::from_macro_def(db, mac),
65 FieldAccess(field) => NavigationTarget::from_field(db, field), 65 FieldAccess(field) => NavigationTarget::from_field(db, field),
66 AssocItem(assoc) => NavigationTarget::from_assoc_item(db, assoc), 66 AssocItem(assoc) => NavigationTarget::from_assoc_item(db, assoc),
67 Method(func) => NavigationTarget::from_def_source(db, func),
68 Def(def) => NavigationTarget::from_def(db, def)?, 67 Def(def) => NavigationTarget::from_def(db, def)?,
69 SelfType(ref ty) => match ty.as_adt() { 68 SelfType(ref ty) => match ty.as_adt() {
70 Some((def_id, _)) => NavigationTarget::from_adt_def(db, def_id), 69 Some((def_id, _)) => NavigationTarget::from_adt_def(db, def_id),
@@ -105,44 +104,6 @@ pub(crate) fn find_all_refs(
105 } 104 }
106} 105}
107 106
108fn classify_name(db: &RootDatabase, file_id: FileId, name: &ast::Name) -> Option<NameKind> {
109 let parent = name.syntax().parent()?;
110 let file_id = file_id.into();
111
112 if let Some(pat) = ast::BindPat::cast(parent.clone()) {
113 return Some(Pat(AstPtr::new(&pat)));
114 }
115 if let Some(var) = ast::EnumVariant::cast(parent.clone()) {
116 let src = hir::Source { file_id, ast: var };
117 let var = hir::EnumVariant::from_source(db, src)?;
118 return Some(Def(var.into()));
119 }
120 if let Some(field) = ast::RecordFieldDef::cast(parent.clone()) {
121 let src = hir::Source { file_id, ast: hir::FieldSource::Named(field) };
122 let field = hir::StructField::from_source(db, src)?;
123 return Some(FieldAccess(field));
124 }
125 if let Some(field) = ast::TupleFieldDef::cast(parent.clone()) {
126 let src = hir::Source { file_id, ast: hir::FieldSource::Pos(field) };
127 let field = hir::StructField::from_source(db, src)?;
128 return Some(FieldAccess(field));
129 }
130 if let Some(_) = parent.parent().and_then(ast::ItemList::cast) {
131 let ast = ast::ImplItem::cast(parent.clone())?;
132 let src = hir::Source { file_id, ast };
133 let item = hir::AssocItem::from_source(db, src)?;
134 return Some(AssocItem(item));
135 }
136 if let Some(item) = ast::ModuleItem::cast(parent.clone()) {
137 let src = hir::Source { file_id, ast: item };
138 let def = hir::ModuleDef::from_source(db, src)?;
139 return Some(Def(def));
140 }
141 // FIXME: TYPE_PARAM, ALIAS, MACRO_CALL; Union
142
143 None
144}
145
146pub(crate) fn rename( 107pub(crate) fn rename(
147 db: &RootDatabase, 108 db: &RootDatabase,
148 position: FilePosition, 109 position: FilePosition,
diff --git a/crates/ra_ide_api/src/syntax_highlighting.rs b/crates/ra_ide_api/src/syntax_highlighting.rs
index 03104e348..98373a49c 100644
--- a/crates/ra_ide_api/src/syntax_highlighting.rs
+++ b/crates/ra_ide_api/src/syntax_highlighting.rs
@@ -14,7 +14,7 @@ use ra_syntax::{
14 14
15use crate::{ 15use crate::{
16 db::RootDatabase, 16 db::RootDatabase,
17 name_ref_kind::{classify_name_ref, NameKind::*}, 17 name_kind::{classify_name_ref, NameKind::*},
18 FileId, 18 FileId,
19}; 19};
20 20
@@ -104,7 +104,6 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
104 // FIXME: try to reuse the SourceAnalyzers 104 // FIXME: try to reuse the SourceAnalyzers
105 let analyzer = hir::SourceAnalyzer::new(db, file_id, name_ref.syntax(), None); 105 let analyzer = hir::SourceAnalyzer::new(db, file_id, name_ref.syntax(), None);
106 match classify_name_ref(db, &analyzer, &name_ref) { 106 match classify_name_ref(db, &analyzer, &name_ref) {
107 Some(Method(_)) => "function",
108 Some(Macro(_)) => "macro", 107 Some(Macro(_)) => "macro",
109 Some(FieldAccess(_)) => "field", 108 Some(FieldAccess(_)) => "field",
110 Some(AssocItem(hir::AssocItem::Function(_))) => "function", 109 Some(AssocItem(hir::AssocItem::Function(_))) => "function",