Use match_ast

This commit is contained in:
Aleksey Kladov 2020-03-18 20:51:47 +01:00
parent b28d411866
commit 0bf903411c
2 changed files with 15 additions and 12 deletions

View file

@ -1,6 +1,6 @@
use ra_syntax::{ use ra_syntax::{
ast::{self, edit::AstNodeEdit, make, AstNode, NameOwner, TypeBoundsOwner}, ast::{self, edit::AstNodeEdit, make, AstNode, NameOwner, TypeBoundsOwner},
SyntaxElement, match_ast,
SyntaxKind::*, SyntaxKind::*,
}; };
@ -34,15 +34,18 @@ pub(crate) fn move_bounds_to_where_clause(ctx: AssistCtx) -> Option<Assist> {
return None; return None;
} }
let anchor: SyntaxElement = match parent.kind() { let anchor = match_ast! {
FN_DEF => ast::FnDef::cast(parent)?.body()?.syntax().clone().into(), match parent {
TRAIT_DEF => ast::TraitDef::cast(parent)?.item_list()?.syntax().clone().into(), ast::FnDef(it) => it.body()?.syntax().clone().into(),
IMPL_DEF => ast::ImplDef::cast(parent)?.item_list()?.syntax().clone().into(), ast::TraitDef(it) => it.item_list()?.syntax().clone().into(),
ENUM_DEF => ast::EnumDef::cast(parent)?.variant_list()?.syntax().clone().into(), ast::ImplDef(it) => it.item_list()?.syntax().clone().into(),
STRUCT_DEF => parent ast::EnumDef(it) => it.variant_list()?.syntax().clone().into(),
.children_with_tokens() ast::StructDef(it) => {
.find(|it| it.kind() == RECORD_FIELD_DEF_LIST || it.kind() == SEMI)?, it.syntax().children_with_tokens()
_ => return None, .find(|it| it.kind() == RECORD_FIELD_DEF_LIST || it.kind() == SEMI)?
},
_ => return None
}
}; };
ctx.add_assist(AssistId("move_bounds_to_where_clause"), "Move to where clause", |edit| { ctx.add_assist(AssistId("move_bounds_to_where_clause"), "Move to where clause", |edit| {

View file

@ -179,10 +179,10 @@ macro_rules! match_ast {
(match $node:ident { $($tt:tt)* }) => { match_ast!(match ($node) { $($tt)* }) }; (match $node:ident { $($tt:tt)* }) => { match_ast!(match ($node) { $($tt)* }) };
(match ($node:expr) { (match ($node:expr) {
$( ast::$ast:ident($it:ident) => $res:block, )* $( ast::$ast:ident($it:ident) => $res:expr, )*
_ => $catch_all:expr $(,)? _ => $catch_all:expr $(,)?
}) => {{ }) => {{
$( if let Some($it) = ast::$ast::cast($node.clone()) $res else )* $( if let Some($it) = ast::$ast::cast($node.clone()) { $res } else )*
{ $catch_all } { $catch_all }
}}; }};
} }