diff options
author | Edwin Cheng <[email protected]> | 2019-12-13 15:55:51 +0000 |
---|---|---|
committer | Edwin Cheng <[email protected]> | 2019-12-18 03:20:22 +0000 |
commit | 320416d7561e9926ecbbc392cc2efd48740610e0 (patch) | |
tree | f20bbcfca5e079582ecd6ea87b2b4972b34fe7f2 /crates/ra_mbe/src | |
parent | 59295854f892b0a8f42a6fbc80b04d1f1c695828 (diff) |
Add TokenTextRange
Diffstat (limited to 'crates/ra_mbe/src')
-rw-r--r-- | crates/ra_mbe/src/syntax_bridge.rs | 41 |
1 files changed, 33 insertions, 8 deletions
diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index a85bb058b..44a51b7a5 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs | |||
@@ -12,11 +12,30 @@ use tt::buffer::{Cursor, TokenBuffer}; | |||
12 | use crate::subtree_source::SubtreeTokenSource; | 12 | use crate::subtree_source::SubtreeTokenSource; |
13 | use crate::ExpandError; | 13 | use crate::ExpandError; |
14 | 14 | ||
15 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] | ||
16 | pub enum TokenTextRange { | ||
17 | Token(TextRange), | ||
18 | Delimiter(TextRange, TextRange), | ||
19 | } | ||
20 | |||
21 | impl TokenTextRange { | ||
22 | pub fn range(self, kind: SyntaxKind) -> Option<TextRange> { | ||
23 | match self { | ||
24 | TokenTextRange::Token(it) => Some(it), | ||
25 | TokenTextRange::Delimiter(open, close) => match kind { | ||
26 | T!['{'] | T!['('] | T!['['] => Some(open), | ||
27 | T!['}'] | T![')'] | T![']'] => Some(close), | ||
28 | _ => None, | ||
29 | }, | ||
30 | } | ||
31 | } | ||
32 | } | ||
33 | |||
15 | /// Maps `tt::TokenId` to the relative range of the original token. | 34 | /// Maps `tt::TokenId` to the relative range of the original token. |
16 | #[derive(Debug, PartialEq, Eq, Default)] | 35 | #[derive(Debug, PartialEq, Eq, Default)] |
17 | pub struct TokenMap { | 36 | pub struct TokenMap { |
18 | /// Maps `tt::TokenId` to the *relative* source range. | 37 | /// Maps `tt::TokenId` to the *relative* source range. |
19 | entries: Vec<(tt::TokenId, TextRange)>, | 38 | entries: Vec<(tt::TokenId, TokenTextRange)>, |
20 | } | 39 | } |
21 | 40 | ||
22 | /// Convert the syntax tree (what user has written) to a `TokenTree` (what macro | 41 | /// Convert the syntax tree (what user has written) to a `TokenTree` (what macro |
@@ -72,26 +91,32 @@ pub fn token_tree_to_syntax_node( | |||
72 | 91 | ||
73 | impl TokenMap { | 92 | impl TokenMap { |
74 | pub fn token_by_range(&self, relative_range: TextRange) -> Option<tt::TokenId> { | 93 | pub fn token_by_range(&self, relative_range: TextRange) -> Option<tt::TokenId> { |
75 | let &(token_id, _) = self.entries.iter().find(|(_, range)| *range == relative_range)?; | 94 | let &(token_id, _) = self.entries.iter().find(|(_, range)| match range { |
95 | TokenTextRange::Token(it) => *it == relative_range, | ||
96 | TokenTextRange::Delimiter(open, close) => { | ||
97 | *open == relative_range || *close == relative_range | ||
98 | } | ||
99 | })?; | ||
76 | Some(token_id) | 100 | Some(token_id) |
77 | } | 101 | } |
78 | 102 | ||
79 | pub fn range_by_token(&self, token_id: tt::TokenId) -> Option<TextRange> { | 103 | pub fn range_by_token(&self, token_id: tt::TokenId) -> Option<TokenTextRange> { |
80 | let &(_, range) = self.entries.iter().find(|(tid, _)| *tid == token_id)?; | 104 | let &(_, range) = self.entries.iter().find(|(tid, _)| *tid == token_id)?; |
81 | Some(range) | 105 | Some(range) |
82 | } | 106 | } |
83 | 107 | ||
84 | fn insert(&mut self, token_id: tt::TokenId, relative_range: TextRange) { | 108 | fn insert(&mut self, token_id: tt::TokenId, relative_range: TextRange) { |
85 | self.entries.push((token_id, relative_range)); | 109 | self.entries.push((token_id, TokenTextRange::Token(relative_range))); |
86 | } | 110 | } |
87 | 111 | ||
88 | fn insert_delim( | 112 | fn insert_delim( |
89 | &mut self, | 113 | &mut self, |
90 | _token_id: tt::TokenId, | 114 | token_id: tt::TokenId, |
91 | _open_relative_range: TextRange, | 115 | open_relative_range: TextRange, |
92 | _close_relative_range: TextRange, | 116 | close_relative_range: TextRange, |
93 | ) { | 117 | ) { |
94 | // FIXME: Add entries for delimiter | 118 | self.entries |
119 | .push((token_id, TokenTextRange::Delimiter(open_relative_range, close_relative_range))); | ||
95 | } | 120 | } |
96 | } | 121 | } |
97 | 122 | ||