diff options
Diffstat (limited to 'crates/ra_syntax/src/ast/make.rs')
-rw-r--r-- | crates/ra_syntax/src/ast/make.rs | 42 |
1 files changed, 14 insertions, 28 deletions
diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs index eef45090d..36e648180 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. |
3 | use itertools::Itertools; | 3 | use itertools::Itertools; |
4 | 4 | ||
5 | use crate::{algo, ast, AstNode, SourceFile}; | 5 | use crate::{ast, AstNode, SourceFile, SyntaxKind, SyntaxToken}; |
6 | 6 | ||
7 | pub fn name(text: &str) -> ast::Name { | 7 | pub fn name(text: &str) -> ast::Name { |
8 | ast_from_text(&format!("mod {};", text)) | 8 | ast_from_text(&format!("mod {};", text)) |
@@ -21,20 +21,6 @@ pub fn path_qualified(qual: ast::Path, name_ref: ast::NameRef) -> ast::Path { | |||
21 | fn path_from_text(text: &str) -> ast::Path { | 21 | fn path_from_text(text: &str) -> ast::Path { |
22 | ast_from_text(text) | 22 | ast_from_text(text) |
23 | } | 23 | } |
24 | pub fn path_with_type_arg_list(path: ast::Path, args: Option<ast::TypeArgList>) -> ast::Path { | ||
25 | if let Some(args) = args { | ||
26 | let syntax = path.syntax(); | ||
27 | // FIXME: remove existing type args | ||
28 | let new_syntax = algo::insert_children( | ||
29 | syntax, | ||
30 | crate::algo::InsertPosition::Last, | ||
31 | &mut Some(args).into_iter().map(|n| n.syntax().clone().into()), | ||
32 | ); | ||
33 | ast::Path::cast(new_syntax).unwrap() | ||
34 | } else { | ||
35 | path | ||
36 | } | ||
37 | } | ||
38 | 24 | ||
39 | pub fn record_field(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::RecordField { | 25 | pub fn record_field(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::RecordField { |
40 | return match expr { | 26 | return match expr { |
@@ -181,27 +167,27 @@ pub fn let_stmt(pattern: ast::Pat, initializer: Option<ast::Expr>) -> ast::LetSt | |||
181 | ast_from_text(&format!("fn f() {{ {} }}", text)) | 167 | ast_from_text(&format!("fn f() {{ {} }}", text)) |
182 | } | 168 | } |
183 | 169 | ||
170 | pub fn token(kind: SyntaxKind) -> SyntaxToken { | ||
171 | tokens::SOURCE_FILE | ||
172 | .tree() | ||
173 | .syntax() | ||
174 | .descendants_with_tokens() | ||
175 | .filter_map(|it| it.into_token()) | ||
176 | .find(|it| it.kind() == kind) | ||
177 | .unwrap_or_else(|| panic!("unhandled token: {:?}", kind)) | ||
178 | } | ||
179 | |||
184 | fn ast_from_text<N: AstNode>(text: &str) -> N { | 180 | fn ast_from_text<N: AstNode>(text: &str) -> N { |
185 | let parse = SourceFile::parse(text); | 181 | let parse = SourceFile::parse(text); |
186 | parse.tree().syntax().descendants().find_map(N::cast).unwrap() | 182 | parse.tree().syntax().descendants().find_map(N::cast).unwrap() |
187 | } | 183 | } |
188 | 184 | ||
189 | pub mod tokens { | 185 | pub mod tokens { |
190 | use crate::{AstNode, Parse, SourceFile, SyntaxKind, SyntaxKind::*, SyntaxToken, T}; | 186 | use crate::{AstNode, Parse, SourceFile, SyntaxKind::*, SyntaxToken, T}; |
191 | use once_cell::sync::Lazy; | 187 | use once_cell::sync::Lazy; |
192 | 188 | ||
193 | static SOURCE_FILE: Lazy<Parse<SourceFile>> = | 189 | pub(super) static SOURCE_FILE: Lazy<Parse<SourceFile>> = |
194 | Lazy::new(|| SourceFile::parse("const C: () = (1 != 1, 2 == 2)\n;")); | 190 | Lazy::new(|| SourceFile::parse("const C: <()>::Item = (1 != 1, 2 == 2)\n;")); |
195 | |||
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 | 191 | ||
206 | pub fn comma() -> SyntaxToken { | 192 | pub fn comma() -> SyntaxToken { |
207 | SOURCE_FILE | 193 | SOURCE_FILE |