Something went wrong. Try again.
AST-based search and replace to refactor to new `Playground` impl
Something went wrong. Try again.
1.9 kB · 67 lines
Rust
at main
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768use 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<T: HasMethodCallExpr + ?Sized> HasMethodCallExpr for &T { fn method_call_expr(&self) -> &ast::MethodCallExpr { (*self).method_call_expr() }}
pub fn method_named<T: HasMethodCallExpr + ?Sized>( 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<bool> { 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<MethodCallStmt> { 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(), })}