Pre-cast impl nodes to ast::Impl in find_non_trait_impl

This commit is contained in:
Tiddo Langerak 2022-09-01 08:37:30 +03:00
parent 7402366877
commit d5e6aa39c1
No known key found for this signature in database
GPG Key ID: 7E7AEEE487C75300

View File

@ -1276,32 +1276,26 @@ fn node_to_insert_after(body: &FunctionBody, anchor: Anchor) -> Option<SyntaxNod
last_ancestor last_ancestor
} }
fn find_non_trait_impl(trait_impl: &SyntaxNode) -> Option<SyntaxNode> { fn find_non_trait_impl(trait_impl: &SyntaxNode) -> Option<ast::Impl> {
let impl_type = Some(impl_type_name(trait_impl)?); let as_impl = ast::Impl::cast(trait_impl.clone())?;
let impl_type = Some(impl_type_name(&as_impl)?);
let mut sibblings = trait_impl.parent()?.children(); let sibblings = trait_impl.parent()?.children();
sibblings.find(|s| impl_type_name(s) == impl_type && !is_trait_impl(s)) sibblings.filter_map(ast::Impl::cast)
.find(|s| impl_type_name(s) == impl_type && !is_trait_impl(s))
} }
fn last_impl_member(impl_node: &SyntaxNode) -> Option<SyntaxNode> { fn last_impl_member(impl_node: &ast::Impl) -> Option<SyntaxNode> {
impl_node.children().find(|c| c.kind() == SyntaxKind::ASSOC_ITEM_LIST)?.last_child() let last_child = impl_node.assoc_item_list()?.assoc_items().last()?;
Some(last_child.syntax().clone())
} }
fn is_trait_impl(node: &SyntaxNode) -> bool { fn is_trait_impl(node: &ast::Impl) -> bool {
if !ast::Impl::can_cast(node.kind()) { node.trait_().is_some()
return false;
}
match ast::Impl::cast(node.clone()) {
Some(c) => c.trait_().is_some(),
None => false,
}
} }
fn impl_type_name(impl_node: &SyntaxNode) -> Option<String> { fn impl_type_name(impl_node: &ast::Impl) -> Option<String> {
if !ast::Impl::can_cast(impl_node.kind()) { Some(impl_node.self_ty()?.to_string())
return None;
}
Some(ast::Impl::cast(impl_node.clone())?.self_ty()?.to_string())
} }
fn make_call(ctx: &AssistContext<'_>, fun: &Function, indent: IndentLevel) -> String { fn make_call(ctx: &AssistContext<'_>, fun: &Function, indent: IndentLevel) -> String {