Merge #412
412: vec-macro r=matklad a=matklad Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
d7ca7b8aac
@ -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,
|
||||
)
|
||||
}
|
||||
|
@ -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<MacroExpansion> {
|
||||
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<MacroExpansion> {
|
||||
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<MacroExpansion> {
|
||||
let anchor = "trait ";
|
||||
let pos = input.text.find(anchor)? + anchor.len();
|
||||
|
Loading…
x
Reference in New Issue
Block a user