From fb215dc192005160c70d5026bea7bfd752033292 Mon Sep 17 00:00:00 2001 From: Mikhail Modin Date: Sun, 20 Oct 2019 19:00:09 +0100 Subject: Adds "replace with guarded return" assist --- crates/ra_syntax/src/ast/edit.rs | 28 ++++++++++++++++++++++++++++ crates/ra_syntax/src/ast/make.rs | 8 ++++++++ 2 files changed, 36 insertions(+) (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs index ea92284b8..47bdbb81a 100644 --- a/crates/ra_syntax/src/ast/edit.rs +++ b/crates/ra_syntax/src/ast/edit.rs @@ -284,6 +284,34 @@ impl IndentLevel { .collect(); algo::replace_descendants(&node, &replacements) } + + pub fn decrease_indent(self, node: N) -> N { + N::cast(self._decrease_indent(node.syntax().clone())).unwrap() + } + + fn _decrease_indent(self, node: SyntaxNode) -> SyntaxNode { + let replacements: FxHashMap = node + .descendants_with_tokens() + .filter_map(|el| el.into_token()) + .filter_map(ast::Whitespace::cast) + .filter(|ws| { + let text = ws.syntax().text(); + text.contains('\n') + }) + .map(|ws| { + ( + ws.syntax().clone().into(), + make::tokens::whitespace( + &ws.syntax() + .text() + .replace(&format!("\n{:1$}", "", self.0 as usize * 4), "\n"), + ) + .into(), + ) + }) + .collect(); + algo::replace_descendants(&node, &replacements) + } } // FIXME: replace usages with IndentLevel above diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs index 143835172..00422ea91 100644 --- a/crates/ra_syntax/src/ast/make.rs +++ b/crates/ra_syntax/src/ast/make.rs @@ -128,6 +128,14 @@ pub fn where_clause(preds: impl Iterator) -> ast::WhereCl } } +pub fn if_expression(condition: &ast::Expr, statement: &str) -> ast::IfExpr { + return ast_from_text(&format!( + "fn f() {{ if !{} {{\n {}\n}}\n}}", + condition.syntax().text(), + statement + )); +} + fn ast_from_text(text: &str) -> N { let parse = SourceFile::parse(text); let res = parse.tree().syntax().descendants().find_map(N::cast).unwrap(); -- cgit v1.2.3