aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/ast_editor.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/ast_editor.rs')
-rw-r--r--crates/ra_assists/src/ast_editor.rs52
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};
14use ra_text_edit::TextEditBuilder; 14use ra_text_edit::TextEditBuilder;
15 15
16use crate::ast_builder::tokens;
17
18pub struct AstEditor<N: AstNode> { 16pub 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
288mod 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}