From 3e764f97eb4f07f6cf5ab76ecdd5a902c4645f43 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 3 Jan 2019 14:44:31 +0300 Subject: hardcode vec macro --- crates/ra_analysis/src/syntax_highlighting.rs | 10 +++++++++- crates/ra_hir/src/macros.rs | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/crates/ra_analysis/src/syntax_highlighting.rs b/crates/ra_analysis/src/syntax_highlighting.rs index ccea4aee3..35e153ca0 100644 --- a/crates/ra_analysis/src/syntax_highlighting.rs +++ b/crates/ra_analysis/src/syntax_highlighting.rs @@ -43,6 +43,7 @@ mod tests { " fn main() { ctry!({ let x = 92; x}); + vec![{ let x = 92; x}]; } ", ); @@ -53,10 +54,17 @@ mod tests { HighlightedRange { range: [41; 46), tag: "macro" }, HighlightedRange { range: [49; 52), tag: "keyword" }, HighlightedRange { range: [57; 59), tag: "literal" }, + HighlightedRange { range: [82; 86), tag: "macro" }, + HighlightedRange { range: [89; 92), tag: "keyword" }, + HighlightedRange { range: [97; 99), tag: "literal" }, HighlightedRange { range: [49; 52), tag: "keyword" }, HighlightedRange { range: [53; 54), tag: "function" }, HighlightedRange { range: [57; 59), tag: "literal" }, - HighlightedRange { range: [61; 62), tag: "text" }]"#, + HighlightedRange { range: [61; 62), tag: "text" }, + HighlightedRange { range: [89; 92), tag: "keyword" }, + HighlightedRange { range: [93; 94), tag: "function" }, + HighlightedRange { range: [97; 99), tag: "literal" }, + HighlightedRange { range: [101; 102), tag: "text" }]"#, &highlights, ) } 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}; #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum MacroDef { CTry, + Vec, QueryGroup, } @@ -40,6 +41,8 @@ impl MacroDef { let name_ref = path.segment()?.name_ref()?; if name_ref.text() == "ctry" { MacroDef::CTry + } else if name_ref.text() == "vec" { + MacroDef::Vec } else if name_ref.text() == "query_group" { MacroDef::QueryGroup } else { @@ -59,6 +62,7 @@ impl MacroDef { fn expand(self, input: MacroInput) -> Option { match self { MacroDef::CTry => self.expand_ctry(input), + MacroDef::Vec => self.expand_vec(input), MacroDef::QueryGroup => self.expand_query_group(input), } } @@ -86,6 +90,20 @@ impl MacroDef { }; Some(res) } + fn expand_vec(self, input: MacroInput) -> Option { + let text = format!(r"fn dummy() {{ {}; }}", input.text); + let file = SourceFileNode::parse(&text); + let array_expr = file.syntax().descendants().find_map(ast::ArrayExpr::cast)?; + let ptr = LocalSyntaxPtr::new(array_expr.syntax()); + let src_range = TextRange::offset_len(0.into(), TextUnit::of_str(&input.text)); + let ranges_map = vec![(src_range, array_expr.syntax().range())]; + let res = MacroExpansion { + text, + ranges_map, + ptr, + }; + Some(res) + } fn expand_query_group(self, input: MacroInput) -> Option { let anchor = "trait "; let pos = input.text.find(anchor)? + anchor.len(); -- cgit v1.2.3