From a5515d9d6f215da4351b482d839aab5212fa0e6f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 16 Aug 2018 13:11:20 +0300 Subject: Add derive handles cursor --- crates/libsyntax2/src/ast/mod.rs | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) (limited to 'crates/libsyntax2/src/ast/mod.rs') diff --git a/crates/libsyntax2/src/ast/mod.rs b/crates/libsyntax2/src/ast/mod.rs index fe8f91d15..d53b12ab8 100644 --- a/crates/libsyntax2/src/ast/mod.rs +++ b/crates/libsyntax2/src/ast/mod.rs @@ -52,19 +52,31 @@ impl File { impl FnDef { pub fn has_atom_attr(&self, atom: &str) -> bool { self.attrs() - .filter_map(|x| x.value()) - .filter_map(|x| as_atom(x)) + .filter_map(|x| x.as_atom()) .any(|x| x == atom) } } -fn as_atom(tt: TokenTree) -> Option { - let syntax = tt.syntax_ref(); - let (_bra, attr, _ket) = syntax.children().collect_tuple()?; - if attr.kind() == IDENT { - Some(attr.leaf_text().unwrap()) - } else { - None +impl Attr { + pub fn as_atom(&self) -> Option { + let tt = self.value()?; + let (_bra, attr, _ket) = tt.syntax().children().collect_tuple()?; + if attr.kind() == IDENT { + Some(attr.leaf_text().unwrap()) + } else { + None + } + } + + pub fn as_call(&self) -> Option<(SmolStr, TokenTree)> { + let tt = self.value()?; + let (_bra, attr, args, _ket) = tt.syntax().children().collect_tuple()?; + let args = TokenTree::cast(args)?; + if attr.kind() == IDENT { + Some((attr.leaf_text().unwrap(), args)) + } else { + None + } } } -- cgit v1.2.3