feat: Migrate flip_binexpr
assist to SyntaxEditor
This commit is contained in:
parent
0eed197014
commit
ff6b020512
1 changed files with 28 additions and 25 deletions
|
@ -1,4 +1,7 @@
|
|||
use syntax::ast::{self, AstNode, BinExpr};
|
||||
use syntax::{
|
||||
ast::{self, syntax_factory::SyntaxFactory, AstNode, BinExpr},
|
||||
SyntaxKind, T,
|
||||
};
|
||||
|
||||
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
||||
|
||||
|
@ -19,22 +22,17 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
|
|||
// ```
|
||||
pub(crate) fn flip_binexpr(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
||||
let expr = ctx.find_node_at_offset::<BinExpr>()?;
|
||||
let rhs = expr.rhs()?.syntax().clone();
|
||||
let lhs = expr.lhs()?.syntax().clone();
|
||||
let lhs = expr.lhs()?;
|
||||
let rhs = expr.rhs()?;
|
||||
|
||||
let lhs = if let Some(bin_expr) = BinExpr::cast(lhs.clone()) {
|
||||
if bin_expr.op_kind() == expr.op_kind() {
|
||||
bin_expr.rhs()?.syntax().clone()
|
||||
} else {
|
||||
lhs
|
||||
}
|
||||
} else {
|
||||
lhs
|
||||
let lhs = match &lhs {
|
||||
ast::Expr::BinExpr(bin_expr) if bin_expr.op_kind() == expr.op_kind() => bin_expr.rhs()?,
|
||||
_ => lhs,
|
||||
};
|
||||
|
||||
let op_range = expr.op_token()?.text_range();
|
||||
let op_token = expr.op_token()?;
|
||||
// The assist should be applied only if the cursor is on the operator
|
||||
let cursor_in_range = op_range.contains_range(ctx.selection_trimmed());
|
||||
let cursor_in_range = op_token.text_range().contains_range(ctx.selection_trimmed());
|
||||
if !cursor_in_range {
|
||||
return None;
|
||||
}
|
||||
|
@ -47,13 +45,18 @@ pub(crate) fn flip_binexpr(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option
|
|||
acc.add(
|
||||
AssistId("flip_binexpr", AssistKind::RefactorRewrite),
|
||||
"Flip binary expression",
|
||||
op_range,
|
||||
|edit| {
|
||||
if let FlipAction::FlipAndReplaceOp(new_op) = action {
|
||||
edit.replace(op_range, new_op);
|
||||
}
|
||||
edit.replace(lhs.text_range(), rhs.text());
|
||||
edit.replace(rhs.text_range(), lhs.text());
|
||||
op_token.text_range(),
|
||||
|builder| {
|
||||
let mut editor = builder.make_editor(&expr.syntax().parent().unwrap());
|
||||
let make = SyntaxFactory::new();
|
||||
if let FlipAction::FlipAndReplaceOp(binary_op) = action {
|
||||
editor.replace(op_token, make.token(binary_op))
|
||||
};
|
||||
// FIXME: remove `clone_for_update` when `SyntaxEditor` handles it for us
|
||||
editor.replace(lhs.syntax(), rhs.syntax().clone_for_update());
|
||||
editor.replace(rhs.syntax(), lhs.syntax().clone_for_update());
|
||||
editor.add_mappings(make.finish_with_mappings());
|
||||
builder.add_file_edits(ctx.file_id(), editor);
|
||||
},
|
||||
)
|
||||
}
|
||||
|
@ -62,7 +65,7 @@ enum FlipAction {
|
|||
// Flip the expression
|
||||
Flip,
|
||||
// Flip the expression and replace the operator with this string
|
||||
FlipAndReplaceOp(&'static str),
|
||||
FlipAndReplaceOp(SyntaxKind),
|
||||
// Do not flip the expression
|
||||
DontFlip,
|
||||
}
|
||||
|
@ -73,10 +76,10 @@ impl From<ast::BinaryOp> for FlipAction {
|
|||
ast::BinaryOp::Assignment { .. } => FlipAction::DontFlip,
|
||||
ast::BinaryOp::CmpOp(ast::CmpOp::Ord { ordering, strict }) => {
|
||||
let rev_op = match (ordering, strict) {
|
||||
(ast::Ordering::Less, true) => ">",
|
||||
(ast::Ordering::Less, false) => ">=",
|
||||
(ast::Ordering::Greater, true) => "<",
|
||||
(ast::Ordering::Greater, false) => "<=",
|
||||
(ast::Ordering::Less, true) => T![>],
|
||||
(ast::Ordering::Less, false) => T![>=],
|
||||
(ast::Ordering::Greater, true) => T![<],
|
||||
(ast::Ordering::Greater, false) => T![<=],
|
||||
};
|
||||
FlipAction::FlipAndReplaceOp(rev_op)
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue