use ra_ap_syntax::{AstNode, AstToken, ast}; pub struct MethodCallStmt { pub indent: ast::Whitespace, pub expr_stmt: ast::ExprStmt, pub method_call_expr: ast::MethodCallExpr, } pub trait HasMethodCallExpr { fn method_call_expr(&self) -> &ast::MethodCallExpr; } impl HasMethodCallExpr for ast::MethodCallExpr { fn method_call_expr(&self) -> &ast::MethodCallExpr { self } } impl HasMethodCallExpr for MethodCallStmt { fn method_call_expr(&self) -> &ast::MethodCallExpr { &self.method_call_expr } } impl HasMethodCallExpr for &T { fn method_call_expr(&self) -> &ast::MethodCallExpr { (*self).method_call_expr() } } pub fn method_named( method_name: &'static str, ) -> impl Fn(&T) -> bool { move |method_call| { method_call .method_call_expr() .name_ref() .as_ref() .map(|name_ref| name_ref.text_non_mutable()) .unwrap_or_default() == method_name } } pub fn receiver_matches_name(method_call_expr: &ast::MethodCallExpr, name: &str) -> Option { let receiver = ast::PathExpr::cast(method_call_expr.receiver()?.syntax().clone())?; Some(receiver.path()?.as_single_name_ref()?.text_non_mutable() == name) } pub fn method_call_stmt(method_call_expr: &ast::MethodCallExpr) -> Option { let expr_stmt = ast::ExprStmt::cast(method_call_expr.syntax().parent()?)?; if expr_stmt.expr()?.syntax().text_range() != method_call_expr.syntax().text_range() { return None; } Some(MethodCallStmt { indent: ast::Whitespace::cast( expr_stmt .syntax() .prev_sibling_or_token()? .as_token()? .clone(), )?, expr_stmt, method_call_expr: method_call_expr.clone(), }) }