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.rs34
1 files changed, 3 insertions, 31 deletions
diff --git a/crates/ra_hir/src/macros.rs b/crates/ra_hir/src/macros.rs
index 7ca34d434..cb8a9312e 100644
--- a/crates/ra_hir/src/macros.rs
+++ b/crates/ra_hir/src/macros.rs
@@ -14,12 +14,11 @@ use ra_syntax::{
14 ast::{self, NameOwner}, 14 ast::{self, NameOwner},
15}; 15};
16 16
17use crate::{HirDatabase, MacroCallId}; 17use crate::{MacroCallId, PersistentHirDatabase};
18 18
19// Hard-coded defs for now :-( 19// Hard-coded defs for now :-(
20#[derive(Debug, Clone, PartialEq, Eq, Hash)] 20#[derive(Debug, Clone, PartialEq, Eq, Hash)]
21pub enum MacroDef { 21pub enum MacroDef {
22 CTry,
23 Vec, 22 Vec,
24 QueryGroup, 23 QueryGroup,
25} 24}
@@ -38,9 +37,7 @@ impl MacroDef {
38 let def = { 37 let def = {
39 let path = macro_call.path()?; 38 let path = macro_call.path()?;
40 let name_ref = path.segment()?.name_ref()?; 39 let name_ref = path.segment()?.name_ref()?;
41 if name_ref.text() == "ctry" { 40 if name_ref.text() == "vec" {
42 MacroDef::CTry
43 } else if name_ref.text() == "vec" {
44 MacroDef::Vec 41 MacroDef::Vec
45 } else if name_ref.text() == "query_group" { 42 } else if name_ref.text() == "query_group" {
46 MacroDef::QueryGroup 43 MacroDef::QueryGroup
@@ -60,35 +57,10 @@ impl MacroDef {
60 57
61 fn expand(self, input: MacroInput) -> Option<MacroExpansion> { 58 fn expand(self, input: MacroInput) -> Option<MacroExpansion> {
62 match self { 59 match self {
63 MacroDef::CTry => self.expand_ctry(input),
64 MacroDef::Vec => self.expand_vec(input), 60 MacroDef::Vec => self.expand_vec(input),
65 MacroDef::QueryGroup => self.expand_query_group(input), 61 MacroDef::QueryGroup => self.expand_query_group(input),
66 } 62 }
67 } 63 }
68 fn expand_ctry(self, input: MacroInput) -> Option<MacroExpansion> {
69 let text = format!(
70 r"
71 fn dummy() {{
72 match {} {{
73 None => return Ok(None),
74 Some(it) => it,
75 }}
76 }}",
77 input.text
78 );
79 let file = SourceFile::parse(&text);
80 let match_expr = file.syntax().descendants().find_map(ast::MatchExpr::cast)?;
81 let match_arg = match_expr.expr()?;
82 let ptr = SyntaxNodePtr::new(match_arg.syntax());
83 let src_range = TextRange::offset_len(0.into(), TextUnit::of_str(&input.text));
84 let ranges_map = vec![(src_range, match_arg.syntax().range())];
85 let res = MacroExpansion {
86 text,
87 ranges_map,
88 ptr,
89 };
90 Some(res)
91 }
92 fn expand_vec(self, input: MacroInput) -> Option<MacroExpansion> { 64 fn expand_vec(self, input: MacroInput) -> Option<MacroExpansion> {
93 let text = format!(r"fn dummy() {{ {}; }}", input.text); 65 let text = format!(r"fn dummy() {{ {}; }}", input.text);
94 let file = SourceFile::parse(&text); 66 let file = SourceFile::parse(&text);
@@ -185,7 +157,7 @@ impl MacroExpansion {
185} 157}
186 158
187pub(crate) fn expand_macro_invocation( 159pub(crate) fn expand_macro_invocation(
188 db: &impl HirDatabase, 160 db: &impl PersistentHirDatabase,
189 invoc: MacroCallId, 161 invoc: MacroCallId,
190) -> Option<Arc<MacroExpansion>> { 162) -> Option<Arc<MacroExpansion>> {
191 let loc = invoc.loc(db); 163 let loc = invoc.loc(db);