aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/ast
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-01-15 17:14:49 +0000
committerAleksey Kladov <[email protected]>2020-01-15 17:14:49 +0000
commitc84010e246c38f868baa777e1f1fda4172d21d53 (patch)
tree554d3e1dac1c770d98c75b820684002f19a46fed /crates/ra_syntax/src/ast
parent8296d3208d30b2b21c897d73e1c847d9549aef21 (diff)
Slightly more fluent API
Diffstat (limited to 'crates/ra_syntax/src/ast')
-rw-r--r--crates/ra_syntax/src/ast/edit.rs2
-rw-r--r--crates/ra_syntax/src/ast/make.rs26
2 files changed, 14 insertions, 14 deletions
diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs
index bd7f0aedc..3037f2715 100644
--- a/crates/ra_syntax/src/ast/edit.rs
+++ b/crates/ra_syntax/src/ast/edit.rs
@@ -22,7 +22,7 @@ impl ast::BinExpr {
22 #[must_use] 22 #[must_use]
23 pub fn replace_op(&self, op: SyntaxKind) -> Option<ast::BinExpr> { 23 pub fn replace_op(&self, op: SyntaxKind) -> Option<ast::BinExpr> {
24 let op_node: SyntaxElement = self.op_details()?.0.into(); 24 let op_node: SyntaxElement = self.op_details()?.0.into();
25 let to_insert: Option<SyntaxElement> = Some(tokens::op(op).into()); 25 let to_insert: Option<SyntaxElement> = Some(make::token(op).into());
26 Some(replace_children(self, single_node(op_node), to_insert.into_iter())) 26 Some(replace_children(self, single_node(op_node), to_insert.into_iter()))
27 } 27 }
28} 28}
diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs
index eef45090d..4a79d0dec 100644
--- a/crates/ra_syntax/src/ast/make.rs
+++ b/crates/ra_syntax/src/ast/make.rs
@@ -2,7 +2,7 @@
2//! of smaller pieces. 2//! of smaller pieces.
3use itertools::Itertools; 3use itertools::Itertools;
4 4
5use crate::{algo, ast, AstNode, SourceFile}; 5use crate::{algo, ast, AstNode, SourceFile, SyntaxKind, SyntaxToken};
6 6
7pub fn name(text: &str) -> ast::Name { 7pub fn name(text: &str) -> ast::Name {
8 ast_from_text(&format!("mod {};", text)) 8 ast_from_text(&format!("mod {};", text))
@@ -181,28 +181,28 @@ pub fn let_stmt(pattern: ast::Pat, initializer: Option<ast::Expr>) -> ast::LetSt
181 ast_from_text(&format!("fn f() {{ {} }}", text)) 181 ast_from_text(&format!("fn f() {{ {} }}", text))
182} 182}
183 183
184pub fn token(kind: SyntaxKind) -> SyntaxToken {
185 tokens::SOURCE_FILE
186 .tree()
187 .syntax()
188 .descendants_with_tokens()
189 .filter_map(|it| it.into_token())
190 .find(|it| it.kind() == kind)
191 .unwrap_or_else(|| panic!("unhandled token: {:?}", kind))
192}
193
184fn ast_from_text<N: AstNode>(text: &str) -> N { 194fn ast_from_text<N: AstNode>(text: &str) -> N {
185 let parse = SourceFile::parse(text); 195 let parse = SourceFile::parse(text);
186 parse.tree().syntax().descendants().find_map(N::cast).unwrap() 196 parse.tree().syntax().descendants().find_map(N::cast).unwrap()
187} 197}
188 198
189pub mod tokens { 199pub mod tokens {
190 use crate::{AstNode, Parse, SourceFile, SyntaxKind, SyntaxKind::*, SyntaxToken, T}; 200 use crate::{AstNode, Parse, SourceFile, SyntaxKind::*, SyntaxToken, T};
191 use once_cell::sync::Lazy; 201 use once_cell::sync::Lazy;
192 202
193 static SOURCE_FILE: Lazy<Parse<SourceFile>> = 203 pub(super) static SOURCE_FILE: Lazy<Parse<SourceFile>> =
194 Lazy::new(|| SourceFile::parse("const C: () = (1 != 1, 2 == 2)\n;")); 204 Lazy::new(|| SourceFile::parse("const C: () = (1 != 1, 2 == 2)\n;"));
195 205
196 pub fn op(op: SyntaxKind) -> SyntaxToken {
197 SOURCE_FILE
198 .tree()
199 .syntax()
200 .descendants_with_tokens()
201 .filter_map(|it| it.into_token())
202 .find(|it| it.kind() == op)
203 .unwrap()
204 }
205
206 pub fn comma() -> SyntaxToken { 206 pub fn comma() -> SyntaxToken {
207 SOURCE_FILE 207 SOURCE_FILE
208 .tree() 208 .tree()