aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/expand.rs
diff options
context:
space:
mode:
authorEdwin Cheng <[email protected]>2019-12-14 18:34:16 +0000
committerEdwin Cheng <[email protected]>2019-12-14 18:34:16 +0000
commit3ba4b3c554ee94cf96d62c57f9bb80eaff19beed (patch)
treebea320110c3d97a69e4fb9b4ef1ea451f9b5894f /crates/ra_ide/src/expand.rs
parentb53587c7bdd67c63bd33a745fdaeb22a847b6c2f (diff)
Use simpler logic on original_range
Diffstat (limited to 'crates/ra_ide/src/expand.rs')
-rw-r--r--crates/ra_ide/src/expand.rs83
1 files changed, 44 insertions, 39 deletions
diff --git a/crates/ra_ide/src/expand.rs b/crates/ra_ide/src/expand.rs
index 258478bc1..7a22bb0a4 100644
--- a/crates/ra_ide/src/expand.rs
+++ b/crates/ra_ide/src/expand.rs
@@ -7,55 +7,60 @@ use ra_syntax::{ast, AstNode, SyntaxNode, SyntaxToken, TextRange};
7 7
8use crate::{db::RootDatabase, FileRange}; 8use crate::{db::RootDatabase, FileRange};
9 9
10#[derive(Debug, PartialEq, Eq)] 10pub(crate) fn original_range(db: &RootDatabase, node: InFile<&SyntaxNode>) -> FileRange {
11pub(crate) enum OriginalRangeKind { 11 if let Some((range, Origin::Call)) = original_range_and_origin(db, node) {
12 /// Return range if any token is matched 12 return range;
13 #[allow(dead_code)] 13 }
14 Any, 14
15 /// Return range if token is inside macro_call 15 if let Some(expansion) = node.file_id.expansion_info(db) {
16 CallToken, 16 if let Some(call_node) = expansion.call_node() {
17 /// Return whole macro call range if matched 17 return FileRange {
18 WholeCall, 18 file_id: call_node.file_id.original_file(db),
19 range: call_node.value.text_range(),
20 };
21 }
22 }
23
24 FileRange { file_id: node.file_id.original_file(db), range: node.value.text_range() }
19} 25}
20 26
21pub(crate) fn original_range_by_kind( 27fn original_range_and_origin(
22 db: &RootDatabase, 28 db: &RootDatabase,
23 node: InFile<&SyntaxNode>, 29 node: InFile<&SyntaxNode>,
24 kind: OriginalRangeKind, 30) -> Option<(FileRange, Origin)> {
25) -> Option<FileRange> {
26 let expansion = node.file_id.expansion_info(db)?; 31 let expansion = node.file_id.expansion_info(db)?;
27 32
28 // the input node has only one token ? 33 // the input node has only one token ?
29 let single = node.value.first_token()? == node.value.last_token()?; 34 let single = node.value.first_token()? == node.value.last_token()?;
30 35
31 // FIXME: We should handle recurside macro expansions 36 // FIXME: We should handle recurside macro expansions
32 let range = match kind { 37 let (range, origin) = node.value.descendants().find_map(|it| {
33 OriginalRangeKind::WholeCall => expansion.call_node()?.map(|node| node.text_range()), 38 let first = it.first_token()?;
34 _ => node.value.descendants().find_map(|it| { 39 let last = it.last_token()?;
35 let first = it.first_token()?; 40
36 let last = it.last_token()?; 41 if !single && first == last {
37 42 return None;
38 if !single && first == last { 43 }
39 return None; 44
40 } 45 // Try to map first and last tokens of node, and, if success, return the union range of mapped tokens
41 46 let (first, first_origin) = expansion.map_token_up(node.with_value(&first))?;
42 // Try to map first and last tokens of node, and, if success, return the union range of mapped tokens 47 let (last, last_origin) = expansion.map_token_up(node.with_value(&last))?;
43 let (first, first_origin) = expansion.map_token_up(node.with_value(&first))?; 48
44 let (last, last_origin) = expansion.map_token_up(node.with_value(&last))?; 49 if first.file_id != last.file_id || first_origin != last_origin {
45 50 return None;
46 if first.file_id != last.file_id 51 }
47 || first_origin != last_origin 52
48 || (kind == OriginalRangeKind::CallToken && first_origin != Origin::Call) 53 // FIXME: Add union method in TextRange
49 { 54 Some((
50 return None; 55 first.with_value(union_range(first.value.text_range(), last.value.text_range())),
51 } 56 first_origin,
52 57 ))
53 // FIXME: Add union method in TextRange 58 })?;
54 Some(first.with_value(union_range(first.value.text_range(), last.value.text_range()))) 59
55 })?, 60 return Some((
56 }; 61 FileRange { file_id: range.file_id.original_file(db), range: range.value },
57 62 origin,
58 return Some(FileRange { file_id: range.file_id.original_file(db), range: range.value }); 63 ));
59 64
60 fn union_range(a: TextRange, b: TextRange) -> TextRange { 65 fn union_range(a: TextRange, b: TextRange) -> TextRange {
61 let start = a.start().min(b.start()); 66 let start = a.start().min(b.start());