aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_hir/src/lib.rs3
-rw-r--r--crates/ra_hir_expand/src/lib.rs29
-rw-r--r--crates/ra_ide/src/display/navigation_target.rs19
-rw-r--r--crates/ra_ide/src/expand.rs79
-rw-r--r--crates/ra_ide/src/goto_definition.rs34
5 files changed, 109 insertions, 55 deletions
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs
index e7602ee30..2bf729b6d 100644
--- a/crates/ra_hir/src/lib.rs
+++ b/crates/ra_hir/src/lib.rs
@@ -58,6 +58,7 @@ pub use hir_def::{
58 type_ref::Mutability, 58 type_ref::Mutability,
59}; 59};
60pub use hir_expand::{ 60pub use hir_expand::{
61 name::Name, HirFileId, InFile, MacroCallId, MacroCallLoc, MacroDefId, MacroFile, 61 name::Name, ExpansionOrigin, HirFileId, InFile, MacroCallId, MacroCallLoc, MacroDefId,
62 MacroFile,
62}; 63};
63pub use hir_ty::{display::HirDisplay, CallableDef}; 64pub use hir_ty::{display::HirDisplay, CallableDef};
diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs
index 94e1e466a..d1a43fe6c 100644
--- a/crates/ra_hir_expand/src/lib.rs
+++ b/crates/ra_hir_expand/src/lib.rs
@@ -214,7 +214,17 @@ pub struct ExpansionInfo {
214 exp_map: Arc<mbe::TokenMap>, 214 exp_map: Arc<mbe::TokenMap>,
215} 215}
216 216
217#[derive(Debug, Clone, PartialEq, Eq)]
218pub enum ExpansionOrigin {
219 Call,
220 Def,
221}
222
217impl ExpansionInfo { 223impl ExpansionInfo {
224 pub fn call_node(&self) -> Option<InFile<SyntaxNode>> {
225 Some(self.arg.with_value(self.arg.value.parent()?))
226 }
227
218 pub fn map_token_down(&self, token: InFile<&SyntaxToken>) -> Option<InFile<SyntaxToken>> { 228 pub fn map_token_down(&self, token: InFile<&SyntaxToken>) -> Option<InFile<SyntaxToken>> {
219 assert_eq!(token.file_id, self.arg.file_id); 229 assert_eq!(token.file_id, self.arg.file_id);
220 let range = token.value.text_range().checked_sub(self.arg.value.text_range().start())?; 230 let range = token.value.text_range().checked_sub(self.arg.value.text_range().start())?;
@@ -228,21 +238,26 @@ impl ExpansionInfo {
228 Some(self.expanded.with_value(token)) 238 Some(self.expanded.with_value(token))
229 } 239 }
230 240
231 pub fn map_token_up(&self, token: InFile<&SyntaxToken>) -> Option<InFile<SyntaxToken>> { 241 pub fn map_token_up(
242 &self,
243 token: InFile<&SyntaxToken>,
244 ) -> Option<(InFile<SyntaxToken>, ExpansionOrigin)> {
232 let token_id = self.exp_map.token_by_range(token.value.text_range())?; 245 let token_id = self.exp_map.token_by_range(token.value.text_range())?;
233 246
234 let (token_id, origin) = self.macro_def.0.map_id_up(token_id); 247 let (token_id, origin) = self.macro_def.0.map_id_up(token_id);
235 let (token_map, tt) = match origin { 248 let (token_map, tt, origin) = match origin {
236 mbe::Origin::Call => (&self.macro_arg.1, self.arg.clone()), 249 mbe::Origin::Call => (&self.macro_arg.1, self.arg.clone(), ExpansionOrigin::Call),
237 mbe::Origin::Def => { 250 mbe::Origin::Def => (
238 (&self.macro_def.1, self.def.as_ref().map(|tt| tt.syntax().clone())) 251 &self.macro_def.1,
239 } 252 self.def.as_ref().map(|tt| tt.syntax().clone()),
253 ExpansionOrigin::Def,
254 ),
240 }; 255 };
241 256
242 let range = token_map.range_by_token(token_id)?; 257 let range = token_map.range_by_token(token_id)?;
243 let token = algo::find_covering_element(&tt.value, range + tt.value.text_range().start()) 258 let token = algo::find_covering_element(&tt.value, range + tt.value.text_range().start())
244 .into_token()?; 259 .into_token()?;
245 Some(tt.with_value(token)) 260 Some((tt.with_value(token), origin))
246 } 261 }
247} 262}
248 263
diff --git a/crates/ra_ide/src/display/navigation_target.rs b/crates/ra_ide/src/display/navigation_target.rs
index 6a6b49afd..6a2bf7273 100644
--- a/crates/ra_ide/src/display/navigation_target.rs
+++ b/crates/ra_ide/src/display/navigation_target.rs
@@ -7,10 +7,14 @@ use ra_syntax::{
7 ast::{self, DocCommentsOwner, NameOwner}, 7 ast::{self, DocCommentsOwner, NameOwner},
8 match_ast, AstNode, SmolStr, 8 match_ast, AstNode, SmolStr,
9 SyntaxKind::{self, BIND_PAT, TYPE_PARAM}, 9 SyntaxKind::{self, BIND_PAT, TYPE_PARAM},
10 TextRange, 10 SyntaxNode, TextRange,
11}; 11};
12 12
13use crate::{db::RootDatabase, expand::original_range, FileSymbol}; 13use crate::{
14 db::RootDatabase,
15 expand::{original_range_by_kind, OriginalRangeKind},
16 FileRange, FileSymbol,
17};
14 18
15use super::short_label::ShortLabel; 19use super::short_label::ShortLabel;
16 20
@@ -416,3 +420,14 @@ pub(crate) fn description_from_symbol(db: &RootDatabase, symbol: &FileSymbol) ->
416 } 420 }
417 } 421 }
418} 422}
423
424fn original_range(db: &RootDatabase, node: InFile<&SyntaxNode>) -> FileRange {
425 if let Some(range) = original_range_by_kind(db, node, OriginalRangeKind::CallToken) {
426 return range;
427 }
428 if let Some(range) = original_range_by_kind(db, node, OriginalRangeKind::WholeCall) {
429 return range;
430 }
431
432 FileRange { file_id: node.file_id.original_file(db), range: node.value.text_range() }
433}
diff --git a/crates/ra_ide/src/expand.rs b/crates/ra_ide/src/expand.rs
index 661628ae4..327393dbb 100644
--- a/crates/ra_ide/src/expand.rs
+++ b/crates/ra_ide/src/expand.rs
@@ -1,57 +1,62 @@
1//! Utilities to work with files, produced by macros. 1//! Utilities to work with files, produced by macros.
2use std::iter::successors; 2use std::iter::successors;
3 3
4use hir::InFile; 4use hir::{ExpansionOrigin, InFile};
5use ra_db::FileId; 5use ra_db::FileId;
6use ra_syntax::{ast, AstNode, SyntaxNode, SyntaxToken, TextRange}; 6use ra_syntax::{ast, AstNode, SyntaxNode, SyntaxToken, TextRange};
7 7
8use crate::{db::RootDatabase, FileRange}; 8use crate::{db::RootDatabase, FileRange};
9 9
10pub(crate) fn original_range(db: &RootDatabase, node: InFile<&SyntaxNode>) -> FileRange { 10#[derive(Debug, PartialEq, Eq)]
11 let expansion = match node.file_id.expansion_info(db) { 11pub(crate) enum OriginalRangeKind {
12 None => { 12 /// Return range if any token is matched
13 return FileRange { 13 #[allow(dead_code)]
14 file_id: node.file_id.original_file(db), 14 Any,
15 range: node.value.text_range(), 15 /// Return range if token is inside macro_call
16 } 16 CallToken,
17 } 17 /// Return whole macro call range if matched
18 Some(it) => it, 18 WholeCall,
19 }; 19}
20
21pub(crate) fn original_range_by_kind(
22 db: &RootDatabase,
23 node: InFile<&SyntaxNode>,
24 kind: OriginalRangeKind,
25) -> Option<FileRange> {
26 let expansion = node.file_id.expansion_info(db)?;
27
28 // the input node has only one token ?
29 let single = node.value.first_token()? == node.value.last_token()?;
30
20 // FIXME: We should handle recurside macro expansions 31 // FIXME: We should handle recurside macro expansions
32 let range = match kind {
33 OriginalRangeKind::WholeCall => expansion.call_node()?.map(|node| node.text_range()),
34 _ => node.value.descendants().find_map(|it| {
35 let first = it.first_token()?;
36 let last = it.last_token()?;
21 37
22 let range = node.value.descendants_with_tokens().find_map(|it| { 38 if !single && first == last {
23 match it.as_token() { 39 return None;
24 // FIXME: Remove this branch after all `tt::TokenTree`s have a proper `TokenId`,
25 // and return the range of the overall macro expansions if mapping first and last tokens fails.
26 Some(token) => {
27 let token = expansion.map_token_up(node.with_value(&token))?;
28 Some(token.with_value(token.value.text_range()))
29 } 40 }
30 None => {
31 // Try to map first and last tokens of node, and, if success, return the union range of mapped tokens
32 let n = it.into_node()?;
33 let first = expansion.map_token_up(node.with_value(&n.first_token()?))?;
34 let last = expansion.map_token_up(node.with_value(&n.last_token()?))?;
35 41
36 // FIXME: Is is possible ? 42 // Try to map first and last tokens of node, and, if success, return the union range of mapped tokens
37 if first.file_id != last.file_id { 43 let (first, first_origin) = expansion.map_token_up(node.with_value(&first))?;
38 return None; 44 let (last, last_origin) = expansion.map_token_up(node.with_value(&last))?;
39 }
40 45
41 // FIXME: Add union method in TextRange 46 if first.file_id != last.file_id
42 let range = union_range(first.value.text_range(), last.value.text_range()); 47 || first_origin != last_origin
43 Some(first.with_value(range)) 48 || (kind == OriginalRangeKind::CallToken && first_origin != ExpansionOrigin::Call)
49 {
50 return None;
44 } 51 }
45 }
46 });
47 52
48 return match range { 53 // FIXME: Add union method in TextRange
49 Some(it) => FileRange { file_id: it.file_id.original_file(db), range: it.value }, 54 Some(first.with_value(union_range(first.value.text_range(), last.value.text_range())))
50 None => { 55 })?,
51 FileRange { file_id: node.file_id.original_file(db), range: node.value.text_range() }
52 }
53 }; 56 };
54 57
58 return Some(FileRange { file_id: range.file_id.original_file(db), range: range.value });
59
55 fn union_range(a: TextRange, b: TextRange) -> TextRange { 60 fn union_range(a: TextRange, b: TextRange) -> TextRange {
56 let start = a.start().min(b.start()); 61 let start = a.start().min(b.start());
57 let end = a.end().max(b.end()); 62 let end = a.end().max(b.end());
diff --git a/crates/ra_ide/src/goto_definition.rs b/crates/ra_ide/src/goto_definition.rs
index cfe62037f..2c634990d 100644
--- a/crates/ra_ide/src/goto_definition.rs
+++ b/crates/ra_ide/src/goto_definition.rs
@@ -209,7 +209,7 @@ fn named_target(db: &RootDatabase, node: InFile<&SyntaxNode>) -> Option<Navigati
209 209
210#[cfg(test)] 210#[cfg(test)]
211mod tests { 211mod tests {
212 use test_utils::covers; 212 use test_utils::{assert_eq_text, covers};
213 213
214 use crate::mock_analysis::analysis_and_position; 214 use crate::mock_analysis::analysis_and_position;
215 215
@@ -222,6 +222,24 @@ mod tests {
222 nav.assert_match(expected); 222 nav.assert_match(expected);
223 } 223 }
224 224
225 fn check_goto_with_range_content(fixture: &str, expected: &str, expected_range: &str) {
226 let (analysis, pos) = analysis_and_position(fixture);
227
228 let mut navs = analysis.goto_definition(pos).unwrap().unwrap().info;
229 assert_eq!(navs.len(), 1);
230 let nav = navs.pop().unwrap();
231 let file_text = analysis.file_text(pos.file_id).unwrap();
232
233 let actual_full_range = &file_text[nav.full_range()];
234 let actual_range = &file_text[nav.range()];
235
236 test_utils::assert_eq_text!(
237 &format!("{}|{}", actual_full_range, actual_range),
238 expected_range
239 );
240 nav.assert_match(expected);
241 }
242
225 #[test] 243 #[test]
226 fn goto_definition_works_in_items() { 244 fn goto_definition_works_in_items() {
227 check_goto( 245 check_goto(
@@ -339,28 +357,27 @@ mod tests {
339 357
340 #[test] 358 #[test]
341 fn goto_definition_works_for_macro_defined_fn_with_arg() { 359 fn goto_definition_works_for_macro_defined_fn_with_arg() {
342 check_goto( 360 check_goto_with_range_content(
343 " 361 "
344 //- /lib.rs 362 //- /lib.rs
345 macro_rules! define_fn { 363 macro_rules! define_fn {
346 ($name:ident) => (fn $name() {}) 364 ($name:ident) => (fn $name() {})
347 } 365 }
348 366
349 define_fn!( 367 define_fn!(foo);
350 foo
351 )
352 368
353 fn bar() { 369 fn bar() {
354 <|>foo(); 370 <|>foo();
355 } 371 }
356 ", 372 ",
357 "foo FN_DEF FileId(1) [80; 83) [80; 83)", 373 "foo FN_DEF FileId(1) [64; 80) [75; 78)",
374 "define_fn!(foo);|foo",
358 ); 375 );
359 } 376 }
360 377
361 #[test] 378 #[test]
362 fn goto_definition_works_for_macro_defined_fn_no_arg() { 379 fn goto_definition_works_for_macro_defined_fn_no_arg() {
363 check_goto( 380 check_goto_with_range_content(
364 " 381 "
365 //- /lib.rs 382 //- /lib.rs
366 macro_rules! define_fn { 383 macro_rules! define_fn {
@@ -373,7 +390,8 @@ mod tests {
373 <|>foo(); 390 <|>foo();
374 } 391 }
375 ", 392 ",
376 "foo FN_DEF FileId(1) [39; 42) [39; 42)", 393 "foo FN_DEF FileId(1) [51; 64) [51; 64)",
394 "define_fn!();|define_fn!();",
377 ); 395 );
378 } 396 }
379 397