aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_mbe/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_mbe/src')
-rw-r--r--crates/ra_mbe/src/syntax_bridge.rs8
1 files changed, 8 insertions, 0 deletions
diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs
index 798f9d8fa..848c785f8 100644
--- a/crates/ra_mbe/src/syntax_bridge.rs
+++ b/crates/ra_mbe/src/syntax_bridge.rs
@@ -3,12 +3,15 @@ use ra_syntax::{
3 ast, SyntaxKind::*, TextUnit 3 ast, SyntaxKind::*, TextUnit
4}; 4};
5 5
6/// Maps `tt::TokenId` to the relative range of the original token.
6#[derive(Default)] 7#[derive(Default)]
7pub struct TokenMap { 8pub struct TokenMap {
8 /// Maps `tt::TokenId` to the *relative* source range. 9 /// Maps `tt::TokenId` to the *relative* source range.
9 toknes: Vec<TextRange>, 10 toknes: Vec<TextRange>,
10} 11}
11 12
13/// Convert the syntax tree (what user has written) to a `TokenTree` (what macro
14/// will consume).
12pub fn ast_to_token_tree(ast: &ast::TokenTree) -> Option<(tt::Subtree, TokenMap)> { 15pub fn ast_to_token_tree(ast: &ast::TokenTree) -> Option<(tt::Subtree, TokenMap)> {
13 let mut token_map = TokenMap::default(); 16 let mut token_map = TokenMap::default();
14 let node = ast.syntax(); 17 let node = ast.syntax();
@@ -17,6 +20,11 @@ pub fn ast_to_token_tree(ast: &ast::TokenTree) -> Option<(tt::Subtree, TokenMap)
17} 20}
18 21
19impl TokenMap { 22impl TokenMap {
23 pub fn relative_range_of(&self, tt: tt::TokenId) -> Option<TextRange> {
24 let idx = tt.0 as usize;
25 self.toknes.get(idx).map(|&it| it)
26 }
27
20 fn alloc(&mut self, relative_range: TextRange) -> tt::TokenId { 28 fn alloc(&mut self, relative_range: TextRange) -> tt::TokenId {
21 let id = self.toknes.len(); 29 let id = self.toknes.len();
22 self.toknes.push(relative_range); 30 self.toknes.push(relative_range);