mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 21:05:02 +00:00
hardcode vec macro
This commit is contained in:
parent
d29e98dd97
commit
3e764f97eb
2 changed files with 27 additions and 1 deletions
|
@ -43,6 +43,7 @@ mod tests {
|
||||||
"
|
"
|
||||||
fn main() {
|
fn main() {
|
||||||
ctry!({ let x = 92; x});
|
ctry!({ let x = 92; x});
|
||||||
|
vec![{ let x = 92; x}];
|
||||||
}
|
}
|
||||||
",
|
",
|
||||||
);
|
);
|
||||||
|
@ -53,10 +54,17 @@ mod tests {
|
||||||
HighlightedRange { range: [41; 46), tag: "macro" },
|
HighlightedRange { range: [41; 46), tag: "macro" },
|
||||||
HighlightedRange { range: [49; 52), tag: "keyword" },
|
HighlightedRange { range: [49; 52), tag: "keyword" },
|
||||||
HighlightedRange { range: [57; 59), tag: "literal" },
|
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: [49; 52), tag: "keyword" },
|
||||||
HighlightedRange { range: [53; 54), tag: "function" },
|
HighlightedRange { range: [53; 54), tag: "function" },
|
||||||
HighlightedRange { range: [57; 59), tag: "literal" },
|
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,
|
&highlights,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,6 +21,7 @@ use crate::{HirDatabase, MacroCallId};
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
pub enum MacroDef {
|
pub enum MacroDef {
|
||||||
CTry,
|
CTry,
|
||||||
|
Vec,
|
||||||
QueryGroup,
|
QueryGroup,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,6 +41,8 @@ impl MacroDef {
|
||||||
let name_ref = path.segment()?.name_ref()?;
|
let name_ref = path.segment()?.name_ref()?;
|
||||||
if name_ref.text() == "ctry" {
|
if name_ref.text() == "ctry" {
|
||||||
MacroDef::CTry
|
MacroDef::CTry
|
||||||
|
} else if name_ref.text() == "vec" {
|
||||||
|
MacroDef::Vec
|
||||||
} else if name_ref.text() == "query_group" {
|
} else if name_ref.text() == "query_group" {
|
||||||
MacroDef::QueryGroup
|
MacroDef::QueryGroup
|
||||||
} else {
|
} else {
|
||||||
|
@ -59,6 +62,7 @@ impl MacroDef {
|
||||||
fn expand(self, input: MacroInput) -> Option<MacroExpansion> {
|
fn expand(self, input: MacroInput) -> Option<MacroExpansion> {
|
||||||
match self {
|
match self {
|
||||||
MacroDef::CTry => self.expand_ctry(input),
|
MacroDef::CTry => self.expand_ctry(input),
|
||||||
|
MacroDef::Vec => self.expand_vec(input),
|
||||||
MacroDef::QueryGroup => self.expand_query_group(input),
|
MacroDef::QueryGroup => self.expand_query_group(input),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -86,6 +90,20 @@ impl MacroDef {
|
||||||
};
|
};
|
||||||
Some(res)
|
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> {
|
fn expand_query_group(self, input: MacroInput) -> Option<MacroExpansion> {
|
||||||
let anchor = "trait ";
|
let anchor = "trait ";
|
||||||
let pos = input.text.find(anchor)? + anchor.len();
|
let pos = input.text.find(anchor)? + anchor.len();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue