aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/goto_definition.rs
diff options
context:
space:
mode:
authorSeivan Heidari <[email protected]>2019-11-18 19:53:40 +0000
committerSeivan Heidari <[email protected]>2019-11-18 19:53:40 +0000
commit4bdb6351ac557851607df9d142c9e573c0fb5e1d (patch)
tree48b349958afceeeebecccd63e55004d5a924baff /crates/ra_ide_api/src/goto_definition.rs
parentaceeb0b85ee8228503f970ea602af71ff22216a0 (diff)
parenta4f21801c54c65eafa337edc5e86de2c46b37544 (diff)
Merge branch 'master' of https://github.com/rust-analyzer/rust-analyzer into feature/themes
Diffstat (limited to 'crates/ra_ide_api/src/goto_definition.rs')
-rw-r--r--crates/ra_ide_api/src/goto_definition.rs60
1 files changed, 16 insertions, 44 deletions
diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs
index b693a4c31..3f16e9566 100644
--- a/crates/ra_ide_api/src/goto_definition.rs
+++ b/crates/ra_ide_api/src/goto_definition.rs
@@ -1,16 +1,15 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use std::iter::successors;
4
5use hir::{db::AstDatabase, Source}; 3use hir::{db::AstDatabase, Source};
6use ra_syntax::{ 4use ra_syntax::{
7 ast::{self, DocCommentsOwner}, 5 ast::{self, DocCommentsOwner},
8 match_ast, AstNode, SyntaxNode, SyntaxToken, 6 match_ast, AstNode, SyntaxNode,
9}; 7};
10 8
11use crate::{ 9use crate::{
12 db::RootDatabase, 10 db::RootDatabase,
13 display::{ShortLabel, ToNav}, 11 display::{ShortLabel, ToNav},
12 expand::descend_into_macros,
14 references::{classify_name_ref, NameKind::*}, 13 references::{classify_name_ref, NameKind::*},
15 FilePosition, NavigationTarget, RangeInfo, 14 FilePosition, NavigationTarget, RangeInfo,
16}; 15};
@@ -19,7 +18,9 @@ pub(crate) fn goto_definition(
19 db: &RootDatabase, 18 db: &RootDatabase,
20 position: FilePosition, 19 position: FilePosition,
21) -> Option<RangeInfo<Vec<NavigationTarget>>> { 20) -> Option<RangeInfo<Vec<NavigationTarget>>> {
22 let token = descend_into_macros(db, position)?; 21 let file = db.parse_or_expand(position.file_id.into())?;
22 let token = file.token_at_offset(position.offset).filter(|it| !it.kind().is_trivia()).next()?;
23 let token = descend_into_macros(db, position.file_id, token);
23 24
24 let res = match_ast! { 25 let res = match_ast! {
25 match (token.ast.parent()) { 26 match (token.ast.parent()) {
@@ -39,24 +40,6 @@ pub(crate) fn goto_definition(
39 Some(res) 40 Some(res)
40} 41}
41 42
42fn descend_into_macros(db: &RootDatabase, position: FilePosition) -> Option<Source<SyntaxToken>> {
43 let file = db.parse_or_expand(position.file_id.into())?;
44 let token = file.token_at_offset(position.offset).filter(|it| !it.kind().is_trivia()).next()?;
45
46 successors(Some(Source::new(position.file_id.into(), token)), |token| {
47 let macro_call = token.ast.ancestors().find_map(ast::MacroCall::cast)?;
48 let tt = macro_call.token_tree()?;
49 if !token.ast.text_range().is_subrange(&tt.syntax().text_range()) {
50 return None;
51 }
52 let source_analyzer =
53 hir::SourceAnalyzer::new(db, token.with_ast(token.ast.parent()).as_ref(), None);
54 let exp = source_analyzer.expand(db, &macro_call)?;
55 exp.map_token_down(db, token.as_ref())
56 })
57 .last()
58}
59
60#[derive(Debug)] 43#[derive(Debug)]
61pub(crate) enum ReferenceResult { 44pub(crate) enum ReferenceResult {
62 Exact(NavigationTarget), 45 Exact(NavigationTarget),
@@ -137,8 +120,7 @@ fn named_target(db: &RootDatabase, node: Source<&SyntaxNode>) -> Option<Navigati
137 ast::StructDef(it) => { 120 ast::StructDef(it) => {
138 Some(NavigationTarget::from_named( 121 Some(NavigationTarget::from_named(
139 db, 122 db,
140 node.file_id, 123 node.with_ast(&it),
141 &it,
142 it.doc_comment_text(), 124 it.doc_comment_text(),
143 it.short_label(), 125 it.short_label(),
144 )) 126 ))
@@ -146,8 +128,7 @@ fn named_target(db: &RootDatabase, node: Source<&SyntaxNode>) -> Option<Navigati
146 ast::EnumDef(it) => { 128 ast::EnumDef(it) => {
147 Some(NavigationTarget::from_named( 129 Some(NavigationTarget::from_named(
148 db, 130 db,
149 node.file_id, 131 node.with_ast(&it),
150 &it,
151 it.doc_comment_text(), 132 it.doc_comment_text(),
152 it.short_label(), 133 it.short_label(),
153 )) 134 ))
@@ -155,8 +136,7 @@ fn named_target(db: &RootDatabase, node: Source<&SyntaxNode>) -> Option<Navigati
155 ast::EnumVariant(it) => { 136 ast::EnumVariant(it) => {
156 Some(NavigationTarget::from_named( 137 Some(NavigationTarget::from_named(
157 db, 138 db,
158 node.file_id, 139 node.with_ast(&it),
159 &it,
160 it.doc_comment_text(), 140 it.doc_comment_text(),
161 it.short_label(), 141 it.short_label(),
162 )) 142 ))
@@ -164,8 +144,7 @@ fn named_target(db: &RootDatabase, node: Source<&SyntaxNode>) -> Option<Navigati
164 ast::FnDef(it) => { 144 ast::FnDef(it) => {
165 Some(NavigationTarget::from_named( 145 Some(NavigationTarget::from_named(
166 db, 146 db,
167 node.file_id, 147 node.with_ast(&it),
168 &it,
169 it.doc_comment_text(), 148 it.doc_comment_text(),
170 it.short_label(), 149 it.short_label(),
171 )) 150 ))
@@ -173,8 +152,7 @@ fn named_target(db: &RootDatabase, node: Source<&SyntaxNode>) -> Option<Navigati
173 ast::TypeAliasDef(it) => { 152 ast::TypeAliasDef(it) => {
174 Some(NavigationTarget::from_named( 153 Some(NavigationTarget::from_named(
175 db, 154 db,
176 node.file_id, 155 node.with_ast(&it),
177 &it,
178 it.doc_comment_text(), 156 it.doc_comment_text(),
179 it.short_label(), 157 it.short_label(),
180 )) 158 ))
@@ -182,8 +160,7 @@ fn named_target(db: &RootDatabase, node: Source<&SyntaxNode>) -> Option<Navigati
182 ast::ConstDef(it) => { 160 ast::ConstDef(it) => {
183 Some(NavigationTarget::from_named( 161 Some(NavigationTarget::from_named(
184 db, 162 db,
185 node.file_id, 163 node.with_ast(&it),
186 &it,
187 it.doc_comment_text(), 164 it.doc_comment_text(),
188 it.short_label(), 165 it.short_label(),
189 )) 166 ))
@@ -191,8 +168,7 @@ fn named_target(db: &RootDatabase, node: Source<&SyntaxNode>) -> Option<Navigati
191 ast::StaticDef(it) => { 168 ast::StaticDef(it) => {
192 Some(NavigationTarget::from_named( 169 Some(NavigationTarget::from_named(
193 db, 170 db,
194 node.file_id, 171 node.with_ast(&it),
195 &it,
196 it.doc_comment_text(), 172 it.doc_comment_text(),
197 it.short_label(), 173 it.short_label(),
198 )) 174 ))
@@ -200,8 +176,7 @@ fn named_target(db: &RootDatabase, node: Source<&SyntaxNode>) -> Option<Navigati
200 ast::TraitDef(it) => { 176 ast::TraitDef(it) => {
201 Some(NavigationTarget::from_named( 177 Some(NavigationTarget::from_named(
202 db, 178 db,
203 node.file_id, 179 node.with_ast(&it),
204 &it,
205 it.doc_comment_text(), 180 it.doc_comment_text(),
206 it.short_label(), 181 it.short_label(),
207 )) 182 ))
@@ -209,8 +184,7 @@ fn named_target(db: &RootDatabase, node: Source<&SyntaxNode>) -> Option<Navigati
209 ast::RecordFieldDef(it) => { 184 ast::RecordFieldDef(it) => {
210 Some(NavigationTarget::from_named( 185 Some(NavigationTarget::from_named(
211 db, 186 db,
212 node.file_id, 187 node.with_ast(&it),
213 &it,
214 it.doc_comment_text(), 188 it.doc_comment_text(),
215 it.short_label(), 189 it.short_label(),
216 )) 190 ))
@@ -218,8 +192,7 @@ fn named_target(db: &RootDatabase, node: Source<&SyntaxNode>) -> Option<Navigati
218 ast::Module(it) => { 192 ast::Module(it) => {
219 Some(NavigationTarget::from_named( 193 Some(NavigationTarget::from_named(
220 db, 194 db,
221 node.file_id, 195 node.with_ast(&it),
222 &it,
223 it.doc_comment_text(), 196 it.doc_comment_text(),
224 it.short_label(), 197 it.short_label(),
225 )) 198 ))
@@ -227,8 +200,7 @@ fn named_target(db: &RootDatabase, node: Source<&SyntaxNode>) -> Option<Navigati
227 ast::MacroCall(it) => { 200 ast::MacroCall(it) => {
228 Some(NavigationTarget::from_named( 201 Some(NavigationTarget::from_named(
229 db, 202 db,
230 node.file_id, 203 node.with_ast(&it),
231 &it,
232 it.doc_comment_text(), 204 it.doc_comment_text(),
233 None, 205 None,
234 )) 206 ))