diff options
Diffstat (limited to 'crates/ra_ide/src')
-rw-r--r-- | crates/ra_ide/src/expand.rs | 84 | ||||
-rw-r--r-- | crates/ra_ide/src/goto_definition.rs | 34 | ||||
-rw-r--r-- | crates/ra_ide/src/snapshots/highlighting.html | 2 | ||||
-rw-r--r-- | crates/ra_ide/src/snapshots/rainbow_highlighting.html | 2 | ||||
-rw-r--r-- | crates/ra_ide/src/syntax_highlighting.rs | 2 |
5 files changed, 76 insertions, 48 deletions
diff --git a/crates/ra_ide/src/expand.rs b/crates/ra_ide/src/expand.rs index 661628ae4..7a22bb0a4 100644 --- a/crates/ra_ide/src/expand.rs +++ b/crates/ra_ide/src/expand.rs | |||
@@ -1,56 +1,66 @@ | |||
1 | //! Utilities to work with files, produced by macros. | 1 | //! Utilities to work with files, produced by macros. |
2 | use std::iter::successors; | 2 | use std::iter::successors; |
3 | 3 | ||
4 | use hir::InFile; | 4 | use hir::{InFile, Origin}; |
5 | use ra_db::FileId; | 5 | use ra_db::FileId; |
6 | use ra_syntax::{ast, AstNode, SyntaxNode, SyntaxToken, TextRange}; | 6 | use ra_syntax::{ast, AstNode, SyntaxNode, SyntaxToken, TextRange}; |
7 | 7 | ||
8 | use crate::{db::RootDatabase, FileRange}; | 8 | use crate::{db::RootDatabase, FileRange}; |
9 | 9 | ||
10 | pub(crate) fn original_range(db: &RootDatabase, node: InFile<&SyntaxNode>) -> FileRange { | 10 | pub(crate) fn original_range(db: &RootDatabase, node: InFile<&SyntaxNode>) -> FileRange { |
11 | let expansion = match node.file_id.expansion_info(db) { | 11 | if let Some((range, Origin::Call)) = original_range_and_origin(db, node) { |
12 | None => { | 12 | return range; |
13 | } | ||
14 | |||
15 | if let Some(expansion) = node.file_id.expansion_info(db) { | ||
16 | if let Some(call_node) = expansion.call_node() { | ||
13 | return FileRange { | 17 | return FileRange { |
14 | file_id: node.file_id.original_file(db), | 18 | file_id: call_node.file_id.original_file(db), |
15 | range: node.value.text_range(), | 19 | range: call_node.value.text_range(), |
16 | } | 20 | }; |
17 | } | 21 | } |
18 | Some(it) => it, | 22 | } |
19 | }; | 23 | |
24 | FileRange { file_id: node.file_id.original_file(db), range: node.value.text_range() } | ||
25 | } | ||
26 | |||
27 | fn original_range_and_origin( | ||
28 | db: &RootDatabase, | ||
29 | node: InFile<&SyntaxNode>, | ||
30 | ) -> Option<(FileRange, Origin)> { | ||
31 | let expansion = node.file_id.expansion_info(db)?; | ||
32 | |||
33 | // the input node has only one token ? | ||
34 | let single = node.value.first_token()? == node.value.last_token()?; | ||
35 | |||
20 | // FIXME: We should handle recurside macro expansions | 36 | // FIXME: We should handle recurside macro expansions |
37 | let (range, origin) = node.value.descendants().find_map(|it| { | ||
38 | let first = it.first_token()?; | ||
39 | let last = it.last_token()?; | ||
21 | 40 | ||
22 | let range = node.value.descendants_with_tokens().find_map(|it| { | 41 | if !single && first == last { |
23 | match it.as_token() { | 42 | 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 | } | ||
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 | |||
36 | // FIXME: Is is possible ? | ||
37 | if first.file_id != last.file_id { | ||
38 | return None; | ||
39 | } | ||
40 | |||
41 | // FIXME: Add union method in TextRange | ||
42 | let range = union_range(first.value.text_range(), last.value.text_range()); | ||
43 | Some(first.with_value(range)) | ||
44 | } | ||
45 | } | 43 | } |
46 | }); | ||
47 | 44 | ||
48 | return match range { | 45 | // Try to map first and last tokens of node, and, if success, return the union range of mapped tokens |
49 | Some(it) => FileRange { file_id: it.file_id.original_file(db), range: it.value }, | 46 | let (first, first_origin) = expansion.map_token_up(node.with_value(&first))?; |
50 | None => { | 47 | let (last, last_origin) = expansion.map_token_up(node.with_value(&last))?; |
51 | FileRange { file_id: node.file_id.original_file(db), range: node.value.text_range() } | 48 | |
49 | if first.file_id != last.file_id || first_origin != last_origin { | ||
50 | return None; | ||
52 | } | 51 | } |
53 | }; | 52 | |
53 | // FIXME: Add union method in TextRange | ||
54 | Some(( | ||
55 | first.with_value(union_range(first.value.text_range(), last.value.text_range())), | ||
56 | first_origin, | ||
57 | )) | ||
58 | })?; | ||
59 | |||
60 | return Some(( | ||
61 | FileRange { file_id: range.file_id.original_file(db), range: range.value }, | ||
62 | origin, | ||
63 | )); | ||
54 | 64 | ||
55 | fn union_range(a: TextRange, b: TextRange) -> TextRange { | 65 | fn union_range(a: TextRange, b: TextRange) -> TextRange { |
56 | let start = a.start().min(b.start()); | 66 | let start = a.start().min(b.start()); |
diff --git a/crates/ra_ide/src/goto_definition.rs b/crates/ra_ide/src/goto_definition.rs index 27052d72b..bee8e9df2 100644 --- a/crates/ra_ide/src/goto_definition.rs +++ b/crates/ra_ide/src/goto_definition.rs | |||
@@ -221,7 +221,7 @@ fn named_target(db: &RootDatabase, node: InFile<&SyntaxNode>) -> Option<Navigati | |||
221 | 221 | ||
222 | #[cfg(test)] | 222 | #[cfg(test)] |
223 | mod tests { | 223 | mod tests { |
224 | use test_utils::covers; | 224 | use test_utils::{assert_eq_text, covers}; |
225 | 225 | ||
226 | use crate::mock_analysis::analysis_and_position; | 226 | use crate::mock_analysis::analysis_and_position; |
227 | 227 | ||
@@ -234,6 +234,24 @@ mod tests { | |||
234 | nav.assert_match(expected); | 234 | nav.assert_match(expected); |
235 | } | 235 | } |
236 | 236 | ||
237 | fn check_goto_with_range_content(fixture: &str, expected: &str, expected_range: &str) { | ||
238 | let (analysis, pos) = analysis_and_position(fixture); | ||
239 | |||
240 | let mut navs = analysis.goto_definition(pos).unwrap().unwrap().info; | ||
241 | assert_eq!(navs.len(), 1); | ||
242 | let nav = navs.pop().unwrap(); | ||
243 | let file_text = analysis.file_text(pos.file_id).unwrap(); | ||
244 | |||
245 | let actual_full_range = &file_text[nav.full_range()]; | ||
246 | let actual_range = &file_text[nav.range()]; | ||
247 | |||
248 | test_utils::assert_eq_text!( | ||
249 | &format!("{}|{}", actual_full_range, actual_range), | ||
250 | expected_range | ||
251 | ); | ||
252 | nav.assert_match(expected); | ||
253 | } | ||
254 | |||
237 | #[test] | 255 | #[test] |
238 | fn goto_definition_works_in_items() { | 256 | fn goto_definition_works_in_items() { |
239 | check_goto( | 257 | check_goto( |
@@ -363,28 +381,27 @@ mod tests { | |||
363 | 381 | ||
364 | #[test] | 382 | #[test] |
365 | fn goto_definition_works_for_macro_defined_fn_with_arg() { | 383 | fn goto_definition_works_for_macro_defined_fn_with_arg() { |
366 | check_goto( | 384 | check_goto_with_range_content( |
367 | " | 385 | " |
368 | //- /lib.rs | 386 | //- /lib.rs |
369 | macro_rules! define_fn { | 387 | macro_rules! define_fn { |
370 | ($name:ident) => (fn $name() {}) | 388 | ($name:ident) => (fn $name() {}) |
371 | } | 389 | } |
372 | 390 | ||
373 | define_fn!( | 391 | define_fn!(foo); |
374 | foo | ||
375 | ) | ||
376 | 392 | ||
377 | fn bar() { | 393 | fn bar() { |
378 | <|>foo(); | 394 | <|>foo(); |
379 | } | 395 | } |
380 | ", | 396 | ", |
381 | "foo FN_DEF FileId(1) [80; 83) [80; 83)", | 397 | "foo FN_DEF FileId(1) [64; 80) [75; 78)", |
398 | "define_fn!(foo);|foo", | ||
382 | ); | 399 | ); |
383 | } | 400 | } |
384 | 401 | ||
385 | #[test] | 402 | #[test] |
386 | fn goto_definition_works_for_macro_defined_fn_no_arg() { | 403 | fn goto_definition_works_for_macro_defined_fn_no_arg() { |
387 | check_goto( | 404 | check_goto_with_range_content( |
388 | " | 405 | " |
389 | //- /lib.rs | 406 | //- /lib.rs |
390 | macro_rules! define_fn { | 407 | macro_rules! define_fn { |
@@ -397,7 +414,8 @@ mod tests { | |||
397 | <|>foo(); | 414 | <|>foo(); |
398 | } | 415 | } |
399 | ", | 416 | ", |
400 | "foo FN_DEF FileId(1) [39; 42) [39; 42)", | 417 | "foo FN_DEF FileId(1) [51; 64) [51; 64)", |
418 | "define_fn!();|define_fn!();", | ||
401 | ); | 419 | ); |
402 | } | 420 | } |
403 | 421 | ||
diff --git a/crates/ra_ide/src/snapshots/highlighting.html b/crates/ra_ide/src/snapshots/highlighting.html index 40605d9ef..2157139f6 100644 --- a/crates/ra_ide/src/snapshots/highlighting.html +++ b/crates/ra_ide/src/snapshots/highlighting.html | |||
@@ -7,9 +7,9 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd | |||
7 | .string { color: #CC9393; } | 7 | .string { color: #CC9393; } |
8 | .function { color: #93E0E3; } | 8 | .function { color: #93E0E3; } |
9 | .parameter { color: #94BFF3; } | 9 | .parameter { color: #94BFF3; } |
10 | .builtin { color: #DD6718; } | ||
11 | .text { color: #DCDCCC; } | 10 | .text { color: #DCDCCC; } |
12 | .type { color: #7CB8BB; } | 11 | .type { color: #7CB8BB; } |
12 | .type\.builtin { color: #8CD0D3; } | ||
13 | .type\.param { color: #20999D; } | 13 | .type\.param { color: #20999D; } |
14 | .attribute { color: #94BFF3; } | 14 | .attribute { color: #94BFF3; } |
15 | .literal { color: #BFEBBF; } | 15 | .literal { color: #BFEBBF; } |
diff --git a/crates/ra_ide/src/snapshots/rainbow_highlighting.html b/crates/ra_ide/src/snapshots/rainbow_highlighting.html index ecf26c708..871a52cf6 100644 --- a/crates/ra_ide/src/snapshots/rainbow_highlighting.html +++ b/crates/ra_ide/src/snapshots/rainbow_highlighting.html | |||
@@ -7,9 +7,9 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd | |||
7 | .string { color: #CC9393; } | 7 | .string { color: #CC9393; } |
8 | .function { color: #93E0E3; } | 8 | .function { color: #93E0E3; } |
9 | .parameter { color: #94BFF3; } | 9 | .parameter { color: #94BFF3; } |
10 | .builtin { color: #DD6718; } | ||
11 | .text { color: #DCDCCC; } | 10 | .text { color: #DCDCCC; } |
12 | .type { color: #7CB8BB; } | 11 | .type { color: #7CB8BB; } |
12 | .type\.builtin { color: #8CD0D3; } | ||
13 | .type\.param { color: #20999D; } | 13 | .type\.param { color: #20999D; } |
14 | .attribute { color: #94BFF3; } | 14 | .attribute { color: #94BFF3; } |
15 | .literal { color: #BFEBBF; } | 15 | .literal { color: #BFEBBF; } |
diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index eb3dd1779..15e75709c 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs | |||
@@ -284,9 +284,9 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd | |||
284 | .string { color: #CC9393; } | 284 | .string { color: #CC9393; } |
285 | .function { color: #93E0E3; } | 285 | .function { color: #93E0E3; } |
286 | .parameter { color: #94BFF3; } | 286 | .parameter { color: #94BFF3; } |
287 | .builtin { color: #DD6718; } | ||
288 | .text { color: #DCDCCC; } | 287 | .text { color: #DCDCCC; } |
289 | .type { color: #7CB8BB; } | 288 | .type { color: #7CB8BB; } |
289 | .type\\.builtin { color: #8CD0D3; } | ||
290 | .type\\.param { color: #20999D; } | 290 | .type\\.param { color: #20999D; } |
291 | .attribute { color: #94BFF3; } | 291 | .attribute { color: #94BFF3; } |
292 | .literal { color: #BFEBBF; } | 292 | .literal { color: #BFEBBF; } |