Add derive handles cursor

This commit is contained in:
Aleksey Kladov 2018-08-16 13:11:20 +03:00
parent 7094291573
commit a5515d9d6f
5 changed files with 51 additions and 20 deletions

View file

@ -339,6 +339,7 @@ impl<R: TreeRoot> AstNode<R> for NominalDef<R> {
}
}
impl<R: TreeRoot> ast::AttrsOwner<R> for NominalDef<R> {}
impl<R: TreeRoot> NominalDef<R> {}
// ParenType

View file

@ -52,19 +52,31 @@ impl<R: TreeRoot> File<R> {
impl<R: TreeRoot> FnDef<R> {
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<R: TreeRoot>(tt: TokenTree<R>) -> Option<SmolStr> {
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<R: TreeRoot> Attr<R> {
pub fn as_atom(&self) -> Option<SmolStr> {
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<R>)> {
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
}
}
}

View file

@ -272,6 +272,9 @@ Grammar(
"DynTraitType",
]),
"NominalDef": ( enum: ["StructDef", "EnumDef"]),
"NominalDef": (
enum: ["StructDef", "EnumDef"],
traits: [ "AttrsOwner" ],
),
},
)