aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax')
-rw-r--r--crates/ra_syntax/src/ast/edit.rs12
-rw-r--r--crates/ra_syntax/src/ast/expr_extensions.rs2
-rw-r--r--crates/ra_syntax/src/ast/make.rs15
3 files changed, 25 insertions, 4 deletions
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::{
13 make::{self, tokens}, 13 make::{self, tokens},
14 AstNode, TypeBoundsOwner, 14 AstNode, TypeBoundsOwner,
15 }, 15 },
16 AstToken, Direction, InsertPosition, SmolStr, SyntaxElement, 16 AstToken, Direction, InsertPosition, SmolStr, SyntaxElement, SyntaxKind,
17 SyntaxKind::{ATTR, COMMENT, WHITESPACE}, 17 SyntaxKind::{ATTR, COMMENT, WHITESPACE},
18 SyntaxNode, SyntaxToken, T, 18 SyntaxNode, SyntaxToken, T,
19}; 19};
20 20
21impl ast::BinExpr {
22 #[must_use]
23 pub fn replace_op(&self, op: SyntaxKind) -> Option<ast::BinExpr> {
24 let op_node: SyntaxElement = self.op_details()?.0.into();
25 let to_insert: Option<SyntaxElement> = Some(tokens::op(op).into());
26 let replace_range = RangeInclusive::new(op_node.clone(), op_node);
27 Some(replace_children(self, replace_range, to_insert.into_iter()))
28 }
29}
30
21impl ast::FnDef { 31impl ast::FnDef {
22 #[must_use] 32 #[must_use]
23 pub fn with_body(&self, body: ast::Block) -> ast::FnDef { 33 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 {
127} 127}
128 128
129impl ast::BinExpr { 129impl ast::BinExpr {
130 fn op_details(&self) -> Option<(SyntaxToken, BinOp)> { 130 pub fn op_details(&self) -> Option<(SyntaxToken, BinOp)> {
131 self.syntax().children_with_tokens().filter_map(|it| it.into_token()).find_map(|c| { 131 self.syntax().children_with_tokens().filter_map(|it| it.into_token()).find_map(|c| {
132 let bin_op = match c.kind() { 132 let bin_op = match c.kind() {
133 T![||] => BinOp::BooleanOr, 133 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<N: AstNode>(text: &str) -> N {
173} 173}
174 174
175pub mod tokens { 175pub mod tokens {
176 use crate::{AstNode, Parse, SourceFile, SyntaxKind::*, SyntaxToken, T}; 176 use crate::{AstNode, Parse, SourceFile, SyntaxKind, SyntaxKind::*, SyntaxToken, T};
177 use once_cell::sync::Lazy; 177 use once_cell::sync::Lazy;
178 178
179 static SOURCE_FILE: Lazy<Parse<SourceFile>> = Lazy::new(|| SourceFile::parse(",\n; ;")); 179 static SOURCE_FILE: Lazy<Parse<SourceFile>> =
180 Lazy::new(|| SourceFile::parse("const C: () = (1 != 1, 2 == 2)\n;"));
181
182 pub fn op(op: SyntaxKind) -> SyntaxToken {
183 SOURCE_FILE
184 .tree()
185 .syntax()
186 .descendants_with_tokens()
187 .filter_map(|it| it.into_token())
188 .find(|it| it.kind() == op)
189 .unwrap()
190 }
180 191
181 pub fn comma() -> SyntaxToken { 192 pub fn comma() -> SyntaxToken {
182 SOURCE_FILE 193 SOURCE_FILE