diff options
author | Aleksey Kladov <[email protected]> | 2019-09-26 10:18:26 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-09-26 13:29:28 +0100 |
commit | 183a38fb50f284de1ca02c05ed945e240f3c0274 (patch) | |
tree | 6f318218b3a97c9a2dba3e8311de4fddbce23b27 /crates/ra_assists/src/ast_editor.rs | |
parent | d6bbdfefa7ed4b2b567558e76d5adadda9d9b83f (diff) |
keep ast creation API simple
Diffstat (limited to 'crates/ra_assists/src/ast_editor.rs')
-rw-r--r-- | crates/ra_assists/src/ast_editor.rs | 52 |
1 files changed, 50 insertions, 2 deletions
diff --git a/crates/ra_assists/src/ast_editor.rs b/crates/ra_assists/src/ast_editor.rs index 4e253f0a4..2936c094b 100644 --- a/crates/ra_assists/src/ast_editor.rs +++ b/crates/ra_assists/src/ast_editor.rs | |||
@@ -13,8 +13,6 @@ use ra_syntax::{ | |||
13 | }; | 13 | }; |
14 | use ra_text_edit::TextEditBuilder; | 14 | use ra_text_edit::TextEditBuilder; |
15 | 15 | ||
16 | use crate::ast_builder::tokens; | ||
17 | |||
18 | pub struct AstEditor<N: AstNode> { | 16 | pub struct AstEditor<N: AstNode> { |
19 | original_ast: N, | 17 | original_ast: N, |
20 | ast: N, | 18 | ast: N, |
@@ -286,3 +284,53 @@ impl AstEditor<ast::TypeParam> { | |||
286 | self | 284 | self |
287 | } | 285 | } |
288 | } | 286 | } |
287 | |||
288 | mod tokens { | ||
289 | use once_cell::sync::Lazy; | ||
290 | use ra_syntax::{AstNode, Parse, SourceFile, SyntaxKind::*, SyntaxToken, T}; | ||
291 | |||
292 | static SOURCE_FILE: Lazy<Parse<SourceFile>> = Lazy::new(|| SourceFile::parse(",\n; ;")); | ||
293 | |||
294 | pub(crate) fn comma() -> SyntaxToken { | ||
295 | SOURCE_FILE | ||
296 | .tree() | ||
297 | .syntax() | ||
298 | .descendants_with_tokens() | ||
299 | .filter_map(|it| it.into_token()) | ||
300 | .find(|it| it.kind() == T![,]) | ||
301 | .unwrap() | ||
302 | } | ||
303 | |||
304 | pub(crate) fn single_space() -> SyntaxToken { | ||
305 | SOURCE_FILE | ||
306 | .tree() | ||
307 | .syntax() | ||
308 | .descendants_with_tokens() | ||
309 | .filter_map(|it| it.into_token()) | ||
310 | .find(|it| it.kind() == WHITESPACE && it.text().as_str() == " ") | ||
311 | .unwrap() | ||
312 | } | ||
313 | |||
314 | #[allow(unused)] | ||
315 | pub(crate) fn single_newline() -> SyntaxToken { | ||
316 | SOURCE_FILE | ||
317 | .tree() | ||
318 | .syntax() | ||
319 | .descendants_with_tokens() | ||
320 | .filter_map(|it| it.into_token()) | ||
321 | .find(|it| it.kind() == WHITESPACE && it.text().as_str() == "\n") | ||
322 | .unwrap() | ||
323 | } | ||
324 | |||
325 | pub(crate) struct WsBuilder(SourceFile); | ||
326 | |||
327 | impl WsBuilder { | ||
328 | pub(crate) fn new(text: &str) -> WsBuilder { | ||
329 | WsBuilder(SourceFile::parse(text).ok().unwrap()) | ||
330 | } | ||
331 | pub(crate) fn ws(&self) -> SyntaxToken { | ||
332 | self.0.syntax().first_child_or_token().unwrap().into_token().unwrap() | ||
333 | } | ||
334 | } | ||
335 | |||
336 | } | ||