internal: remove a remnant of old editing infra

This commit is contained in:
Aleksey Kladov
2021-08-14 18:24:42 +03:00
parent 90357a9090
commit dc17b35e62
2 changed files with 18 additions and 43 deletions

View File

@@ -209,21 +209,22 @@ pub(crate) fn invert_boolean_expression(expr: ast::Expr) -> ast::Expr {
fn invert_special_case(expr: &ast::Expr) -> Option<ast::Expr> { fn invert_special_case(expr: &ast::Expr) -> Option<ast::Expr> {
match expr { match expr {
ast::Expr::BinExpr(bin) => match bin.op_kind()? { ast::Expr::BinExpr(bin) => {
ast::BinaryOp::CmpOp(op) => { let bin = bin.clone_for_update();
let rev_op = match op { let op_token = bin.op_token()?;
ast::CmpOp::Eq { negated: false } => T![!=], let rev_token = match op_token.kind() {
ast::CmpOp::Eq { negated: true } => T![==], T![==] => T![!=],
ast::CmpOp::Ord { ordering: ast::Ordering::Less, strict: true } => T![>=], T![!=] => T![==],
ast::CmpOp::Ord { ordering: ast::Ordering::Less, strict: false } => T![>], T![<] => T![>=],
ast::CmpOp::Ord { ordering: ast::Ordering::Greater, strict: true } => T![<=], T![<=] => T![>],
ast::CmpOp::Ord { ordering: ast::Ordering::Greater, strict: false } => T![<], T![>] => T![<=],
}; T![>=] => T![<],
bin.replace_op(rev_op).map(ast::Expr::from)
}
// Parenthesize other expressions before prefixing `!` // Parenthesize other expressions before prefixing `!`
_ => Some(make::expr_prefix(T![!], make::expr_paren(expr.clone()))), _ => return Some(make::expr_prefix(T![!], make::expr_paren(expr.clone()))),
}, };
ted::replace(op_token, make::token(rev_token));
Some(bin.into())
}
ast::Expr::MethodCallExpr(mce) => { ast::Expr::MethodCallExpr(mce) => {
let receiver = mce.receiver()?; let receiver = mce.receiver()?;
let method = mce.name_ref()?; let method = mce.name_ref()?;

View File

@@ -1,28 +1,16 @@
//! This module contains functions for editing syntax trees. As the trees are //! This module contains functions for editing syntax trees. As the trees are
//! immutable, all function here return a fresh copy of the tree, instead of //! immutable, all function here return a fresh copy of the tree, instead of
//! doing an in-place modification. //! doing an in-place modification.
use std::{ use std::{fmt, iter, ops};
fmt, iter,
ops::{self, RangeInclusive},
};
use crate::{ use crate::{
algo, algo,
ast::{self, make, AstNode}, ast::{self, make, AstNode},
ted, AstToken, NodeOrToken, SyntaxElement, SyntaxKind, ted, AstToken, NodeOrToken, SyntaxElement,
SyntaxKind::{ATTR, COMMENT, WHITESPACE}, SyntaxKind::{ATTR, COMMENT, WHITESPACE},
SyntaxNode, SyntaxToken, SyntaxNode, SyntaxToken,
}; };
impl ast::BinExpr {
#[must_use]
pub fn replace_op(&self, op: SyntaxKind) -> Option<ast::BinExpr> {
let op_node: SyntaxElement = self.op_details()?.0.into();
let to_insert: Option<SyntaxElement> = Some(make::token(op).into());
Some(self.replace_children(single_node(op_node), to_insert))
}
}
impl ast::UseTree { impl ast::UseTree {
/// Splits off the given prefix, making it the path component of the use tree, appending the rest of the path to all UseTreeList items. /// Splits off the given prefix, making it the path component of the use tree, appending the rest of the path to all UseTreeList items.
#[must_use] #[must_use]
@@ -191,15 +179,6 @@ fn prev_tokens(token: SyntaxToken) -> impl Iterator<Item = SyntaxToken> {
} }
pub trait AstNodeEdit: AstNode + Clone + Sized { pub trait AstNodeEdit: AstNode + Clone + Sized {
#[must_use]
fn replace_children(
&self,
to_replace: RangeInclusive<SyntaxElement>,
to_insert: impl IntoIterator<Item = SyntaxElement>,
) -> Self {
let new_syntax = algo::replace_children(self.syntax(), to_replace, to_insert);
Self::cast(new_syntax).unwrap()
}
fn indent_level(&self) -> IndentLevel { fn indent_level(&self) -> IndentLevel {
IndentLevel::from_node(self.syntax()) IndentLevel::from_node(self.syntax())
} }
@@ -220,11 +199,6 @@ pub trait AstNodeEdit: AstNode + Clone + Sized {
impl<N: AstNode + Clone> AstNodeEdit for N {} impl<N: AstNode + Clone> AstNodeEdit for N {}
fn single_node(element: impl Into<SyntaxElement>) -> RangeInclusive<SyntaxElement> {
let element = element.into();
element.clone()..=element
}
#[test] #[test]
fn test_increase_indent() { fn test_increase_indent() {
let arm_list = { let arm_list = {