From c84010e246c38f868baa777e1f1fda4172d21d53 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 15 Jan 2020 18:14:49 +0100 Subject: Slightly more fluent API --- crates/ra_syntax/src/ast/make.rs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'crates/ra_syntax/src/ast/make.rs') 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 @@ //! of smaller pieces. use itertools::Itertools; -use crate::{algo, ast, AstNode, SourceFile}; +use crate::{algo, ast, AstNode, SourceFile, SyntaxKind, SyntaxToken}; pub fn name(text: &str) -> ast::Name { ast_from_text(&format!("mod {};", text)) @@ -181,28 +181,28 @@ pub fn let_stmt(pattern: ast::Pat, initializer: Option) -> ast::LetSt ast_from_text(&format!("fn f() {{ {} }}", text)) } +pub fn token(kind: SyntaxKind) -> SyntaxToken { + tokens::SOURCE_FILE + .tree() + .syntax() + .descendants_with_tokens() + .filter_map(|it| it.into_token()) + .find(|it| it.kind() == kind) + .unwrap_or_else(|| panic!("unhandled token: {:?}", kind)) +} + fn ast_from_text(text: &str) -> N { let parse = SourceFile::parse(text); parse.tree().syntax().descendants().find_map(N::cast).unwrap() } pub mod tokens { - use crate::{AstNode, Parse, SourceFile, SyntaxKind, SyntaxKind::*, SyntaxToken, T}; + use crate::{AstNode, Parse, SourceFile, SyntaxKind::*, SyntaxToken, T}; use once_cell::sync::Lazy; - static SOURCE_FILE: Lazy> = + pub(super) 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 .tree() -- cgit v1.2.3