aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-11-16 13:50:34 +0000
committerGitHub <[email protected]>2019-11-16 13:50:34 +0000
commit786cae520ad62c9a0a13f5ab18e5bd7e5b0c9825 (patch)
treea812e1c6c64944d4f24abf6b3c9f3dbb9add55b4 /crates/ra_ide_api
parent5d9bce6e88626ec5f36b562803686c848fdf7b66 (diff)
parent3b7cf9226d9cbadb6a45bc2a95617e35db11e742 (diff)
Merge #2276
2276: Source-ify name_definition r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_ide_api')
-rw-r--r--crates/ra_ide_api/src/goto_definition.rs50
-rw-r--r--crates/ra_ide_api/src/hover.rs5
-rw-r--r--crates/ra_ide_api/src/references.rs5
-rw-r--r--crates/ra_ide_api/src/references/classify.rs17
-rw-r--r--crates/ra_ide_api/src/syntax_highlighting.rs5
5 files changed, 41 insertions, 41 deletions
diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs
index 6c8387f6c..821796e5f 100644
--- a/crates/ra_ide_api/src/goto_definition.rs
+++ b/crates/ra_ide_api/src/goto_definition.rs
@@ -1,6 +1,7 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use ra_db::{FileId, SourceDatabase}; 3use hir::Source;
4use ra_db::SourceDatabase;
4use ra_syntax::{ 5use ra_syntax::{
5 algo::find_node_at_offset, 6 algo::find_node_at_offset,
6 ast::{self, DocCommentsOwner}, 7 ast::{self, DocCommentsOwner},
@@ -21,11 +22,12 @@ pub(crate) fn goto_definition(
21 let parse = db.parse(position.file_id); 22 let parse = db.parse(position.file_id);
22 let syntax = parse.tree().syntax().clone(); 23 let syntax = parse.tree().syntax().clone();
23 if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(&syntax, position.offset) { 24 if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(&syntax, position.offset) {
24 let navs = reference_definition(db, position.file_id, &name_ref).to_vec(); 25 let navs =
26 reference_definition(db, Source::new(position.file_id.into(), &name_ref)).to_vec();
25 return Some(RangeInfo::new(name_ref.syntax().text_range(), navs.to_vec())); 27 return Some(RangeInfo::new(name_ref.syntax().text_range(), navs.to_vec()));
26 } 28 }
27 if let Some(name) = find_node_at_offset::<ast::Name>(&syntax, position.offset) { 29 if let Some(name) = find_node_at_offset::<ast::Name>(&syntax, position.offset) {
28 let navs = name_definition(db, position.file_id, &name)?; 30 let navs = name_definition(db, Source::new(position.file_id.into(), &name))?;
29 return Some(RangeInfo::new(name.syntax().text_range(), navs)); 31 return Some(RangeInfo::new(name.syntax().text_range(), navs));
30 } 32 }
31 None 33 None
@@ -49,12 +51,11 @@ impl ReferenceResult {
49 51
50pub(crate) fn reference_definition( 52pub(crate) fn reference_definition(
51 db: &RootDatabase, 53 db: &RootDatabase,
52 file_id: FileId, 54 name_ref: Source<&ast::NameRef>,
53 name_ref: &ast::NameRef,
54) -> ReferenceResult { 55) -> ReferenceResult {
55 use self::ReferenceResult::*; 56 use self::ReferenceResult::*;
56 57
57 let name_kind = classify_name_ref(db, file_id, &name_ref).map(|d| d.kind); 58 let name_kind = classify_name_ref(db, name_ref).map(|d| d.kind);
58 match name_kind { 59 match name_kind {
59 Some(Macro(mac)) => return Exact(mac.to_nav(db)), 60 Some(Macro(mac)) => return Exact(mac.to_nav(db)),
60 Some(Field(field)) => return Exact(field.to_nav(db)), 61 Some(Field(field)) => return Exact(field.to_nav(db)),
@@ -76,7 +77,7 @@ pub(crate) fn reference_definition(
76 }; 77 };
77 78
78 // Fallback index based approach: 79 // Fallback index based approach:
79 let navs = crate::symbol_index::index_resolve(db, name_ref) 80 let navs = crate::symbol_index::index_resolve(db, name_ref.ast)
80 .into_iter() 81 .into_iter()
81 .map(|s| s.to_nav(db)) 82 .map(|s| s.to_nav(db))
82 .collect(); 83 .collect();
@@ -85,14 +86,13 @@ pub(crate) fn reference_definition(
85 86
86pub(crate) fn name_definition( 87pub(crate) fn name_definition(
87 db: &RootDatabase, 88 db: &RootDatabase,
88 file_id: FileId, 89 name: Source<&ast::Name>,
89 name: &ast::Name,
90) -> Option<Vec<NavigationTarget>> { 90) -> Option<Vec<NavigationTarget>> {
91 let parent = name.syntax().parent()?; 91 let parent = name.ast.syntax().parent()?;
92 92
93 if let Some(module) = ast::Module::cast(parent.clone()) { 93 if let Some(module) = ast::Module::cast(parent.clone()) {
94 if module.has_semi() { 94 if module.has_semi() {
95 let src = hir::Source { file_id: file_id.into(), ast: module }; 95 let src = name.with_ast(module);
96 if let Some(child_module) = hir::Module::from_declaration(db, src) { 96 if let Some(child_module) = hir::Module::from_declaration(db, src) {
97 let nav = child_module.to_nav(db); 97 let nav = child_module.to_nav(db);
98 return Some(vec![nav]); 98 return Some(vec![nav]);
@@ -100,20 +100,20 @@ pub(crate) fn name_definition(
100 } 100 }
101 } 101 }
102 102
103 if let Some(nav) = named_target(db, file_id, &parent) { 103 if let Some(nav) = named_target(db, name.with_ast(&parent)) {
104 return Some(vec![nav]); 104 return Some(vec![nav]);
105 } 105 }
106 106
107 None 107 None
108} 108}
109 109
110fn named_target(db: &RootDatabase, file_id: FileId, node: &SyntaxNode) -> Option<NavigationTarget> { 110fn named_target(db: &RootDatabase, node: Source<&SyntaxNode>) -> Option<NavigationTarget> {
111 match_ast! { 111 match_ast! {
112 match node { 112 match (node.ast) {
113 ast::StructDef(it) => { 113 ast::StructDef(it) => {
114 Some(NavigationTarget::from_named( 114 Some(NavigationTarget::from_named(
115 db, 115 db,
116 file_id.into(), 116 node.file_id,
117 &it, 117 &it,
118 it.doc_comment_text(), 118 it.doc_comment_text(),
119 it.short_label(), 119 it.short_label(),
@@ -122,7 +122,7 @@ fn named_target(db: &RootDatabase, file_id: FileId, node: &SyntaxNode) -> Option
122 ast::EnumDef(it) => { 122 ast::EnumDef(it) => {
123 Some(NavigationTarget::from_named( 123 Some(NavigationTarget::from_named(
124 db, 124 db,
125 file_id.into(), 125 node.file_id,
126 &it, 126 &it,
127 it.doc_comment_text(), 127 it.doc_comment_text(),
128 it.short_label(), 128 it.short_label(),
@@ -131,7 +131,7 @@ fn named_target(db: &RootDatabase, file_id: FileId, node: &SyntaxNode) -> Option
131 ast::EnumVariant(it) => { 131 ast::EnumVariant(it) => {
132 Some(NavigationTarget::from_named( 132 Some(NavigationTarget::from_named(
133 db, 133 db,
134 file_id.into(), 134 node.file_id,
135 &it, 135 &it,
136 it.doc_comment_text(), 136 it.doc_comment_text(),
137 it.short_label(), 137 it.short_label(),
@@ -140,7 +140,7 @@ fn named_target(db: &RootDatabase, file_id: FileId, node: &SyntaxNode) -> Option
140 ast::FnDef(it) => { 140 ast::FnDef(it) => {
141 Some(NavigationTarget::from_named( 141 Some(NavigationTarget::from_named(
142 db, 142 db,
143 file_id.into(), 143 node.file_id,
144 &it, 144 &it,
145 it.doc_comment_text(), 145 it.doc_comment_text(),
146 it.short_label(), 146 it.short_label(),
@@ -149,7 +149,7 @@ fn named_target(db: &RootDatabase, file_id: FileId, node: &SyntaxNode) -> Option
149 ast::TypeAliasDef(it) => { 149 ast::TypeAliasDef(it) => {
150 Some(NavigationTarget::from_named( 150 Some(NavigationTarget::from_named(
151 db, 151 db,
152 file_id.into(), 152 node.file_id,
153 &it, 153 &it,
154 it.doc_comment_text(), 154 it.doc_comment_text(),
155 it.short_label(), 155 it.short_label(),
@@ -158,7 +158,7 @@ fn named_target(db: &RootDatabase, file_id: FileId, node: &SyntaxNode) -> Option
158 ast::ConstDef(it) => { 158 ast::ConstDef(it) => {
159 Some(NavigationTarget::from_named( 159 Some(NavigationTarget::from_named(
160 db, 160 db,
161 file_id.into(), 161 node.file_id,
162 &it, 162 &it,
163 it.doc_comment_text(), 163 it.doc_comment_text(),
164 it.short_label(), 164 it.short_label(),
@@ -167,7 +167,7 @@ fn named_target(db: &RootDatabase, file_id: FileId, node: &SyntaxNode) -> Option
167 ast::StaticDef(it) => { 167 ast::StaticDef(it) => {
168 Some(NavigationTarget::from_named( 168 Some(NavigationTarget::from_named(
169 db, 169 db,
170 file_id.into(), 170 node.file_id,
171 &it, 171 &it,
172 it.doc_comment_text(), 172 it.doc_comment_text(),
173 it.short_label(), 173 it.short_label(),
@@ -176,7 +176,7 @@ fn named_target(db: &RootDatabase, file_id: FileId, node: &SyntaxNode) -> Option
176 ast::TraitDef(it) => { 176 ast::TraitDef(it) => {
177 Some(NavigationTarget::from_named( 177 Some(NavigationTarget::from_named(
178 db, 178 db,
179 file_id.into(), 179 node.file_id,
180 &it, 180 &it,
181 it.doc_comment_text(), 181 it.doc_comment_text(),
182 it.short_label(), 182 it.short_label(),
@@ -185,7 +185,7 @@ fn named_target(db: &RootDatabase, file_id: FileId, node: &SyntaxNode) -> Option
185 ast::RecordFieldDef(it) => { 185 ast::RecordFieldDef(it) => {
186 Some(NavigationTarget::from_named( 186 Some(NavigationTarget::from_named(
187 db, 187 db,
188 file_id.into(), 188 node.file_id,
189 &it, 189 &it,
190 it.doc_comment_text(), 190 it.doc_comment_text(),
191 it.short_label(), 191 it.short_label(),
@@ -194,7 +194,7 @@ fn named_target(db: &RootDatabase, file_id: FileId, node: &SyntaxNode) -> Option
194 ast::Module(it) => { 194 ast::Module(it) => {
195 Some(NavigationTarget::from_named( 195 Some(NavigationTarget::from_named(
196 db, 196 db,
197 file_id.into(), 197 node.file_id,
198 &it, 198 &it,
199 it.doc_comment_text(), 199 it.doc_comment_text(),
200 it.short_label(), 200 it.short_label(),
@@ -203,7 +203,7 @@ fn named_target(db: &RootDatabase, file_id: FileId, node: &SyntaxNode) -> Option
203 ast::MacroCall(it) => { 203 ast::MacroCall(it) => {
204 Some(NavigationTarget::from_named( 204 Some(NavigationTarget::from_named(
205 db, 205 db,
206 file_id.into(), 206 node.file_id,
207 &it, 207 &it,
208 it.doc_comment_text(), 208 it.doc_comment_text(),
209 None, 209 None,
diff --git a/crates/ra_ide_api/src/hover.rs b/crates/ra_ide_api/src/hover.rs
index 92b4b1f79..cc25f4c37 100644
--- a/crates/ra_ide_api/src/hover.rs
+++ b/crates/ra_ide_api/src/hover.rs
@@ -1,6 +1,6 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use hir::{Adt, HasSource, HirDisplay}; 3use hir::{Adt, HasSource, HirDisplay, Source};
4use ra_db::SourceDatabase; 4use ra_db::SourceDatabase;
5use ra_syntax::{ 5use ra_syntax::{
6 algo::{ancestors_at_offset, find_covering_element, find_node_at_offset}, 6 algo::{ancestors_at_offset, find_covering_element, find_node_at_offset},
@@ -171,7 +171,8 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
171 find_node_at_offset::<ast::NameRef>(file.syntax(), position.offset) 171 find_node_at_offset::<ast::NameRef>(file.syntax(), position.offset)
172 { 172 {
173 let mut no_fallback = false; 173 let mut no_fallback = false;
174 if let Some(name_kind) = classify_name_ref(db, position.file_id, &name_ref).map(|d| d.kind) 174 if let Some(name_kind) =
175 classify_name_ref(db, Source::new(position.file_id.into(), &name_ref)).map(|d| d.kind)
175 { 176 {
176 res.extend(hover_text_from_name_kind(db, name_kind, &mut no_fallback)) 177 res.extend(hover_text_from_name_kind(db, name_kind, &mut no_fallback))
177 } 178 }
diff --git a/crates/ra_ide_api/src/references.rs b/crates/ra_ide_api/src/references.rs
index faa88d988..1af7e8a9f 100644
--- a/crates/ra_ide_api/src/references.rs
+++ b/crates/ra_ide_api/src/references.rs
@@ -14,6 +14,7 @@ mod name_definition;
14mod rename; 14mod rename;
15mod search_scope; 15mod search_scope;
16 16
17use hir::Source;
17use once_cell::unsync::Lazy; 18use once_cell::unsync::Lazy;
18use ra_db::{SourceDatabase, SourceDatabaseExt}; 19use ra_db::{SourceDatabase, SourceDatabaseExt};
19use ra_prof::profile; 20use ra_prof::profile;
@@ -114,7 +115,7 @@ fn find_name<'a>(
114 return Some(RangeInfo::new(range, (name.text().to_string(), def))); 115 return Some(RangeInfo::new(range, (name.text().to_string(), def)));
115 } 116 }
116 let name_ref = find_node_at_offset::<ast::NameRef>(&syntax, position.offset)?; 117 let name_ref = find_node_at_offset::<ast::NameRef>(&syntax, position.offset)?;
117 let def = classify_name_ref(db, position.file_id, &name_ref)?; 118 let def = classify_name_ref(db, Source::new(position.file_id.into(), &name_ref))?;
118 let range = name_ref.syntax().text_range(); 119 let range = name_ref.syntax().text_range();
119 Some(RangeInfo::new(range, (name_ref.text().to_string(), def))) 120 Some(RangeInfo::new(range, (name_ref.text().to_string(), def)))
120} 121}
@@ -146,7 +147,7 @@ fn process_definition(
146 continue; 147 continue;
147 } 148 }
148 } 149 }
149 if let Some(d) = classify_name_ref(db, file_id, &name_ref) { 150 if let Some(d) = classify_name_ref(db, Source::new(file_id.into(), &name_ref)) {
150 if d == def { 151 if d == def {
151 refs.push(FileRange { file_id, range }); 152 refs.push(FileRange { file_id, range });
152 } 153 }
diff --git a/crates/ra_ide_api/src/references/classify.rs b/crates/ra_ide_api/src/references/classify.rs
index f12b58cb9..5ca9da15e 100644
--- a/crates/ra_ide_api/src/references/classify.rs
+++ b/crates/ra_ide_api/src/references/classify.rs
@@ -123,14 +123,12 @@ pub(crate) fn classify_name(
123 123
124pub(crate) fn classify_name_ref( 124pub(crate) fn classify_name_ref(
125 db: &RootDatabase, 125 db: &RootDatabase,
126 file_id: FileId, 126 name_ref: Source<&ast::NameRef>,
127 name_ref: &ast::NameRef,
128) -> Option<NameDefinition> { 127) -> Option<NameDefinition> {
129 let _p = profile("classify_name_ref"); 128 let _p = profile("classify_name_ref");
130 129
131 let parent = name_ref.syntax().parent()?; 130 let parent = name_ref.ast.syntax().parent()?;
132 let analyzer = 131 let analyzer = SourceAnalyzer::new(db, name_ref.map(|it| it.syntax()), None);
133 SourceAnalyzer::new(db, hir::Source::new(file_id.into(), name_ref.syntax()), None);
134 132
135 if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) { 133 if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) {
136 tested_by!(goto_definition_works_for_methods); 134 tested_by!(goto_definition_works_for_methods);
@@ -150,17 +148,16 @@ pub(crate) fn classify_name_ref(
150 tested_by!(goto_definition_works_for_record_fields); 148 tested_by!(goto_definition_works_for_record_fields);
151 if let Some(record_lit) = record_field.syntax().ancestors().find_map(ast::RecordLit::cast) { 149 if let Some(record_lit) = record_field.syntax().ancestors().find_map(ast::RecordLit::cast) {
152 let variant_def = analyzer.resolve_record_literal(&record_lit)?; 150 let variant_def = analyzer.resolve_record_literal(&record_lit)?;
153 let hir_path = Path::from_name_ref(name_ref); 151 let hir_path = Path::from_name_ref(name_ref.ast);
154 let hir_name = hir_path.as_ident()?; 152 let hir_name = hir_path.as_ident()?;
155 let field = variant_def.field(db, hir_name)?; 153 let field = variant_def.field(db, hir_name)?;
156 return Some(from_struct_field(db, field)); 154 return Some(from_struct_field(db, field));
157 } 155 }
158 } 156 }
159 157
160 let file_id = file_id.into(); 158 let ast = ModuleSource::from_child_node(db, name_ref.with_ast(&parent));
161 let ast = ModuleSource::from_child_node(db, Source::new(file_id, &parent));
162 // FIXME: find correct container and visibility for each case 159 // FIXME: find correct container and visibility for each case
163 let container = Module::from_definition(db, Source { file_id, ast })?; 160 let container = Module::from_definition(db, name_ref.with_ast(ast))?;
164 let visibility = None; 161 let visibility = None;
165 162
166 if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) { 163 if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) {
@@ -171,7 +168,7 @@ pub(crate) fn classify_name_ref(
171 } 168 }
172 } 169 }
173 170
174 let path = name_ref.syntax().ancestors().find_map(ast::Path::cast)?; 171 let path = name_ref.ast.syntax().ancestors().find_map(ast::Path::cast)?;
175 let resolved = analyzer.resolve_path(db, &path)?; 172 let resolved = analyzer.resolve_path(db, &path)?;
176 match resolved { 173 match resolved {
177 PathResolution::Def(def) => Some(from_module_def(db, def, Some(container))), 174 PathResolution::Def(def) => Some(from_module_def(db, def, Some(container))),
diff --git a/crates/ra_ide_api/src/syntax_highlighting.rs b/crates/ra_ide_api/src/syntax_highlighting.rs
index d53a759ee..584657ca2 100644
--- a/crates/ra_ide_api/src/syntax_highlighting.rs
+++ b/crates/ra_ide_api/src/syntax_highlighting.rs
@@ -2,7 +2,7 @@
2 2
3use rustc_hash::{FxHashMap, FxHashSet}; 3use rustc_hash::{FxHashMap, FxHashSet};
4 4
5use hir::{Mutability, Name}; 5use hir::{Mutability, Name, Source};
6use ra_db::SourceDatabase; 6use ra_db::SourceDatabase;
7use ra_prof::profile; 7use ra_prof::profile;
8use ra_syntax::{ast, AstNode, Direction, SyntaxElement, SyntaxKind, SyntaxKind::*, TextRange, T}; 8use ra_syntax::{ast, AstNode, Direction, SyntaxElement, SyntaxKind, SyntaxKind::*, TextRange, T};
@@ -80,7 +80,8 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
80 } 80 }
81 81
82 let name_ref = node.as_node().cloned().and_then(ast::NameRef::cast).unwrap(); 82 let name_ref = node.as_node().cloned().and_then(ast::NameRef::cast).unwrap();
83 let name_kind = classify_name_ref(db, file_id, &name_ref).map(|d| d.kind); 83 let name_kind =
84 classify_name_ref(db, Source::new(file_id.into(), &name_ref)).map(|d| d.kind);
84 85
85 if let Some(Local(local)) = &name_kind { 86 if let Some(Local(local)) = &name_kind {
86 if let Some(name) = local.name(db) { 87 if let Some(name) = local.name(db) {