aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/macros.rs
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-01-03 11:54:10 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-01-03 11:54:10 +0000
commitd7ca7b8aacc931fe2efe6cbfefc73d30e80289a2 (patch)
tree8ecd92761eed9b6285c4009955deb06ceb483fe8 /crates/ra_hir/src/macros.rs
parentd29e98dd975c6d72a62f3d3e178fef1e2ae8fc6a (diff)
parent3e764f97eb4f07f6cf5ab76ecdd5a902c4645f43 (diff)
Merge #412
412: vec-macro r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir/src/macros.rs')
-rw-r--r--crates/ra_hir/src/macros.rs18
1 files changed, 18 insertions, 0 deletions
diff --git a/crates/ra_hir/src/macros.rs b/crates/ra_hir/src/macros.rs
index b7b75e702..1b378c977 100644
--- a/crates/ra_hir/src/macros.rs
+++ b/crates/ra_hir/src/macros.rs
@@ -21,6 +21,7 @@ use crate::{HirDatabase, MacroCallId};
21#[derive(Debug, Clone, PartialEq, Eq, Hash)] 21#[derive(Debug, Clone, PartialEq, Eq, Hash)]
22pub enum MacroDef { 22pub enum MacroDef {
23 CTry, 23 CTry,
24 Vec,
24 QueryGroup, 25 QueryGroup,
25} 26}
26 27
@@ -40,6 +41,8 @@ impl MacroDef {
40 let name_ref = path.segment()?.name_ref()?; 41 let name_ref = path.segment()?.name_ref()?;
41 if name_ref.text() == "ctry" { 42 if name_ref.text() == "ctry" {
42 MacroDef::CTry 43 MacroDef::CTry
44 } else if name_ref.text() == "vec" {
45 MacroDef::Vec
43 } else if name_ref.text() == "query_group" { 46 } else if name_ref.text() == "query_group" {
44 MacroDef::QueryGroup 47 MacroDef::QueryGroup
45 } else { 48 } else {
@@ -59,6 +62,7 @@ impl MacroDef {
59 fn expand(self, input: MacroInput) -> Option<MacroExpansion> { 62 fn expand(self, input: MacroInput) -> Option<MacroExpansion> {
60 match self { 63 match self {
61 MacroDef::CTry => self.expand_ctry(input), 64 MacroDef::CTry => self.expand_ctry(input),
65 MacroDef::Vec => self.expand_vec(input),
62 MacroDef::QueryGroup => self.expand_query_group(input), 66 MacroDef::QueryGroup => self.expand_query_group(input),
63 } 67 }
64 } 68 }
@@ -86,6 +90,20 @@ impl MacroDef {
86 }; 90 };
87 Some(res) 91 Some(res)
88 } 92 }
93 fn expand_vec(self, input: MacroInput) -> Option<MacroExpansion> {
94 let text = format!(r"fn dummy() {{ {}; }}", input.text);
95 let file = SourceFileNode::parse(&text);
96 let array_expr = file.syntax().descendants().find_map(ast::ArrayExpr::cast)?;
97 let ptr = LocalSyntaxPtr::new(array_expr.syntax());
98 let src_range = TextRange::offset_len(0.into(), TextUnit::of_str(&input.text));
99 let ranges_map = vec![(src_range, array_expr.syntax().range())];
100 let res = MacroExpansion {
101 text,
102 ranges_map,
103 ptr,
104 };
105 Some(res)
106 }
89 fn expand_query_group(self, input: MacroInput) -> Option<MacroExpansion> { 107 fn expand_query_group(self, input: MacroInput) -> Option<MacroExpansion> {
90 let anchor = "trait "; 108 let anchor = "trait ";
91 let pos = input.text.find(anchor)? + anchor.len(); 109 let pos = input.text.find(anchor)? + anchor.len();