mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-03 15:15:24 +00:00
Add vis matcher
This commit is contained in:
parent
c5983b85fc
commit
87ff908135
6 changed files with 33 additions and 1 deletions
|
@ -809,4 +809,16 @@ MACRO_ITEMS@[0; 40)
|
|||
);
|
||||
assert_expansion(&rules, r#"foo!(u8 0)"#, r#"const VALUE: u8 = 0;"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_vis() {
|
||||
let rules = create_rules(
|
||||
r#"
|
||||
macro_rules! foo {
|
||||
($ vis:vis $ name:ident) => { $ vis fn $ name() {}};
|
||||
}
|
||||
"#,
|
||||
);
|
||||
assert_expansion(&rules, r#"foo!(pub foo);"#, r#"pub fn foo() {}"#);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -193,6 +193,10 @@ fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Result<Bindings,
|
|||
Binding::Simple(tt::Leaf::from(literal).into()),
|
||||
);
|
||||
}
|
||||
"vis" => {
|
||||
let vis = input.eat_vis().ok_or(ExpandError::UnexpectedToken)?.clone();
|
||||
res.inner.insert(text.clone(), Binding::Simple(vis.into()));
|
||||
}
|
||||
|
||||
_ => return Err(ExpandError::UnexpectedToken),
|
||||
}
|
||||
|
|
|
@ -58,6 +58,10 @@ impl<'a> Parser<'a> {
|
|||
self.parse(ra_parser::parse_item)
|
||||
}
|
||||
|
||||
pub fn parse_vis(self) -> Option<tt::TokenTree> {
|
||||
self.parse(ra_parser::parse_vis)
|
||||
}
|
||||
|
||||
fn parse<F>(self, f: F) -> Option<tt::TokenTree>
|
||||
where
|
||||
F: FnOnce(&dyn TokenSource, &mut dyn TreeSink),
|
||||
|
|
|
@ -137,6 +137,11 @@ impl<'a> TtCursor<'a> {
|
|||
self.eat_ident().cloned().map(|ident| tt::Leaf::from(ident).into())
|
||||
}
|
||||
|
||||
pub(crate) fn eat_vis(&mut self) -> Option<tt::TokenTree> {
|
||||
let parser = Parser::new(&mut self.pos, self.subtree);
|
||||
parser.parse_vis()
|
||||
}
|
||||
|
||||
pub(crate) fn expect_char(&mut self, char: char) -> Result<(), ParseError> {
|
||||
if self.at_char(char) {
|
||||
self.bump();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue