aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/macros.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/macros.rs')
-rw-r--r--crates/ra_hir/src/macros.rs21
1 files changed, 10 insertions, 11 deletions
diff --git a/crates/ra_hir/src/macros.rs b/crates/ra_hir/src/macros.rs
index 1b378c977..eb1c86091 100644
--- a/crates/ra_hir/src/macros.rs
+++ b/crates/ra_hir/src/macros.rs
@@ -11,7 +11,7 @@ use std::sync::Arc;
11 11
12use ra_db::LocalSyntaxPtr; 12use ra_db::LocalSyntaxPtr;
13use ra_syntax::{ 13use ra_syntax::{
14 TextRange, TextUnit, SourceFileNode, AstNode, SyntaxNode, 14 TextRange, TextUnit, SourceFile, AstNode, SyntaxNode, TreePtr,
15 ast::{self, NameOwner}, 15 ast::{self, NameOwner},
16}; 16};
17 17
@@ -28,14 +28,14 @@ pub enum MacroDef {
28impl MacroDef { 28impl MacroDef {
29 /// Expands macro call, returning the expansion and offset to be used to 29 /// Expands macro call, returning the expansion and offset to be used to
30 /// convert ranges between expansion and original source. 30 /// convert ranges between expansion and original source.
31 pub fn ast_expand(macro_call: ast::MacroCall) -> Option<(TextUnit, MacroExpansion)> { 31 pub fn ast_expand(macro_call: &ast::MacroCall) -> Option<(TextUnit, MacroExpansion)> {
32 let (def, input) = MacroDef::from_call(macro_call)?; 32 let (def, input) = MacroDef::from_call(macro_call)?;
33 let exp = def.expand(input)?; 33 let exp = def.expand(input)?;
34 let off = macro_call.token_tree()?.syntax().range().start(); 34 let off = macro_call.token_tree()?.syntax().range().start();
35 Some((off, exp)) 35 Some((off, exp))
36 } 36 }
37 37
38 fn from_call(macro_call: ast::MacroCall) -> Option<(MacroDef, MacroInput)> { 38 fn from_call(macro_call: &ast::MacroCall) -> Option<(MacroDef, MacroInput)> {
39 let def = { 39 let def = {
40 let path = macro_call.path()?; 40 let path = macro_call.path()?;
41 let name_ref = path.segment()?.name_ref()?; 41 let name_ref = path.segment()?.name_ref()?;
@@ -77,7 +77,7 @@ impl MacroDef {
77 }}", 77 }}",
78 input.text 78 input.text
79 ); 79 );
80 let file = SourceFileNode::parse(&text); 80 let file = SourceFile::parse(&text);
81 let match_expr = file.syntax().descendants().find_map(ast::MatchExpr::cast)?; 81 let match_expr = file.syntax().descendants().find_map(ast::MatchExpr::cast)?;
82 let match_arg = match_expr.expr()?; 82 let match_arg = match_expr.expr()?;
83 let ptr = LocalSyntaxPtr::new(match_arg.syntax()); 83 let ptr = LocalSyntaxPtr::new(match_arg.syntax());
@@ -92,7 +92,7 @@ impl MacroDef {
92 } 92 }
93 fn expand_vec(self, input: MacroInput) -> Option<MacroExpansion> { 93 fn expand_vec(self, input: MacroInput) -> Option<MacroExpansion> {
94 let text = format!(r"fn dummy() {{ {}; }}", input.text); 94 let text = format!(r"fn dummy() {{ {}; }}", input.text);
95 let file = SourceFileNode::parse(&text); 95 let file = SourceFile::parse(&text);
96 let array_expr = file.syntax().descendants().find_map(ast::ArrayExpr::cast)?; 96 let array_expr = file.syntax().descendants().find_map(ast::ArrayExpr::cast)?;
97 let ptr = LocalSyntaxPtr::new(array_expr.syntax()); 97 let ptr = LocalSyntaxPtr::new(array_expr.syntax());
98 let src_range = TextRange::offset_len(0.into(), TextUnit::of_str(&input.text)); 98 let src_range = TextRange::offset_len(0.into(), TextUnit::of_str(&input.text));
@@ -116,7 +116,7 @@ impl MacroDef {
116 } 116 }
117 let src_range = TextRange::offset_len((pos as u32).into(), TextUnit::of_str(&trait_name)); 117 let src_range = TextRange::offset_len((pos as u32).into(), TextUnit::of_str(&trait_name));
118 let text = format!(r"trait {} {{ }}", trait_name); 118 let text = format!(r"trait {} {{ }}", trait_name);
119 let file = SourceFileNode::parse(&text); 119 let file = SourceFile::parse(&text);
120 let trait_def = file.syntax().descendants().find_map(ast::TraitDef::cast)?; 120 let trait_def = file.syntax().descendants().find_map(ast::TraitDef::cast)?;
121 let name = trait_def.name()?; 121 let name = trait_def.name()?;
122 let ptr = LocalSyntaxPtr::new(trait_def.syntax()); 122 let ptr = LocalSyntaxPtr::new(trait_def.syntax());
@@ -152,11 +152,11 @@ pub struct MacroExpansion {
152impl MacroExpansion { 152impl MacroExpansion {
153 // FIXME: does not really make sense, macro expansion is not neccessary a 153 // FIXME: does not really make sense, macro expansion is not neccessary a
154 // whole file. See `MacroExpansion::ptr` as well. 154 // whole file. See `MacroExpansion::ptr` as well.
155 pub(crate) fn file(&self) -> SourceFileNode { 155 pub(crate) fn file(&self) -> TreePtr<SourceFile> {
156 SourceFileNode::parse(&self.text) 156 SourceFile::parse(&self.text)
157 } 157 }
158 158
159 pub fn syntax(&self) -> SyntaxNode { 159 pub fn syntax(&self) -> TreePtr<SyntaxNode> {
160 self.ptr.resolve(&self.file()) 160 self.ptr.resolve(&self.file())
161 } 161 }
162 /// Maps range in the source code to the range in the expanded code. 162 /// Maps range in the source code to the range in the expanded code.
@@ -191,8 +191,7 @@ pub(crate) fn expand_macro_invocation(
191) -> Option<Arc<MacroExpansion>> { 191) -> Option<Arc<MacroExpansion>> {
192 let loc = invoc.loc(db); 192 let loc = invoc.loc(db);
193 let syntax = db.file_item(loc.source_item_id); 193 let syntax = db.file_item(loc.source_item_id);
194 let syntax = syntax.borrowed(); 194 let macro_call = ast::MacroCall::cast(&syntax).unwrap();
195 let macro_call = ast::MacroCall::cast(syntax).unwrap();
196 195
197 let (def, input) = MacroDef::from_call(macro_call)?; 196 let (def, input) = MacroDef::from_call(macro_call)?;
198 def.expand(input).map(Arc::new) 197 def.expand(input).map(Arc::new)