From adac4fc2f21117486356063d82d79f8c3add084a Mon Sep 17 00:00:00 2001 From: bravomikekilo Date: Sun, 24 Nov 2019 13:14:57 +0800 Subject: do refact and fix some issue --- crates/ra_syntax/src/ast/edit.rs | 12 +++++++++++- crates/ra_syntax/src/ast/expr_extensions.rs | 2 +- crates/ra_syntax/src/ast/make.rs | 15 +++++++++++++-- 3 files changed, 25 insertions(+), 4 deletions(-) (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs index 6f005a2d8..95bf9db14 100644 --- a/crates/ra_syntax/src/ast/edit.rs +++ b/crates/ra_syntax/src/ast/edit.rs @@ -13,11 +13,21 @@ use crate::{ make::{self, tokens}, AstNode, TypeBoundsOwner, }, - AstToken, Direction, InsertPosition, SmolStr, SyntaxElement, + AstToken, Direction, InsertPosition, SmolStr, SyntaxElement, SyntaxKind, SyntaxKind::{ATTR, COMMENT, WHITESPACE}, SyntaxNode, SyntaxToken, T, }; +impl ast::BinExpr { + #[must_use] + pub fn replace_op(&self, op: SyntaxKind) -> Option { + let op_node: SyntaxElement = self.op_details()?.0.into(); + let to_insert: Option = Some(tokens::op(op).into()); + let replace_range = RangeInclusive::new(op_node.clone(), op_node); + Some(replace_children(self, replace_range, to_insert.into_iter())) + } +} + impl ast::FnDef { #[must_use] pub fn with_body(&self, body: ast::Block) -> ast::FnDef { diff --git a/crates/ra_syntax/src/ast/expr_extensions.rs b/crates/ra_syntax/src/ast/expr_extensions.rs index 7c53aa934..2fd039837 100644 --- a/crates/ra_syntax/src/ast/expr_extensions.rs +++ b/crates/ra_syntax/src/ast/expr_extensions.rs @@ -127,7 +127,7 @@ pub enum BinOp { } impl ast::BinExpr { - fn op_details(&self) -> Option<(SyntaxToken, BinOp)> { + pub fn op_details(&self) -> Option<(SyntaxToken, BinOp)> { self.syntax().children_with_tokens().filter_map(|it| it.into_token()).find_map(|c| { let bin_op = match c.kind() { T![||] => BinOp::BooleanOr, diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs index 9749327fa..40db570da 100644 --- a/crates/ra_syntax/src/ast/make.rs +++ b/crates/ra_syntax/src/ast/make.rs @@ -173,10 +173,21 @@ fn ast_from_text(text: &str) -> N { } pub mod tokens { - use crate::{AstNode, Parse, SourceFile, SyntaxKind::*, SyntaxToken, T}; + use crate::{AstNode, Parse, SourceFile, SyntaxKind, SyntaxKind::*, SyntaxToken, T}; use once_cell::sync::Lazy; - static SOURCE_FILE: Lazy> = Lazy::new(|| SourceFile::parse(",\n; ;")); + static SOURCE_FILE: Lazy> = + Lazy::new(|| SourceFile::parse("const C: () = (1 != 1, 2 == 2)\n;")); + + pub fn op(op: SyntaxKind) -> SyntaxToken { + SOURCE_FILE + .tree() + .syntax() + .descendants_with_tokens() + .filter_map(|it| it.into_token()) + .find(|it| it.kind() == op) + .unwrap() + } pub fn comma() -> SyntaxToken { SOURCE_FILE -- cgit v1.2.3