diff options
author | Aleksey Kladov <[email protected]> | 2020-08-13 10:41:20 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-08-13 10:41:39 +0100 |
commit | f0a9128761e8dd42f0dd40610a1d8734a52f3d9c (patch) | |
tree | 147c5fc40413d63e29fe68f6de1c6732ef028e54 /crates/ra_assists/src/utils.rs | |
parent | b0f03db51dc84aac56c807ecf4da8151c01e3c04 (diff) |
Minor
Diffstat (limited to 'crates/ra_assists/src/utils.rs')
-rw-r--r-- | crates/ra_assists/src/utils.rs | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/crates/ra_assists/src/utils.rs b/crates/ra_assists/src/utils.rs index 6d85661c4..a20453dd8 100644 --- a/crates/ra_assists/src/utils.rs +++ b/crates/ra_assists/src/utils.rs | |||
@@ -4,6 +4,7 @@ pub(crate) mod insert_use; | |||
4 | use std::{iter, ops}; | 4 | use std::{iter, ops}; |
5 | 5 | ||
6 | use hir::{Adt, Crate, Enum, ScopeDef, Semantics, Trait, Type}; | 6 | use hir::{Adt, Crate, Enum, ScopeDef, Semantics, Trait, Type}; |
7 | use itertools::Itertools; | ||
7 | use ra_ide_db::RootDatabase; | 8 | use ra_ide_db::RootDatabase; |
8 | use rustc_hash::FxHashSet; | 9 | use rustc_hash::FxHashSet; |
9 | use syntax::{ | 10 | use syntax::{ |
@@ -17,6 +18,43 @@ use crate::assist_config::SnippetCap; | |||
17 | 18 | ||
18 | pub(crate) use insert_use::{find_insert_use_container, insert_use_statement}; | 19 | pub(crate) use insert_use::{find_insert_use_container, insert_use_statement}; |
19 | 20 | ||
21 | pub(crate) fn unwrap_trivial_block(block: ast::BlockExpr) -> ast::Expr { | ||
22 | extract_trivial_expression(&block) | ||
23 | .filter(|expr| !expr.syntax().text().contains_char('\n')) | ||
24 | .unwrap_or_else(|| block.into()) | ||
25 | } | ||
26 | |||
27 | pub fn extract_trivial_expression(block: &ast::BlockExpr) -> Option<ast::Expr> { | ||
28 | let has_anything_else = |thing: &SyntaxNode| -> bool { | ||
29 | let mut non_trivial_children = | ||
30 | block.syntax().children_with_tokens().filter(|it| match it.kind() { | ||
31 | WHITESPACE | T!['{'] | T!['}'] => false, | ||
32 | _ => it.as_node() != Some(thing), | ||
33 | }); | ||
34 | non_trivial_children.next().is_some() | ||
35 | }; | ||
36 | |||
37 | if let Some(expr) = block.expr() { | ||
38 | if has_anything_else(expr.syntax()) { | ||
39 | return None; | ||
40 | } | ||
41 | return Some(expr); | ||
42 | } | ||
43 | // Unwrap `{ continue; }` | ||
44 | let (stmt,) = block.statements().next_tuple()?; | ||
45 | if let ast::Stmt::ExprStmt(expr_stmt) = stmt { | ||
46 | if has_anything_else(expr_stmt.syntax()) { | ||
47 | return None; | ||
48 | } | ||
49 | let expr = expr_stmt.expr()?; | ||
50 | match expr.syntax().kind() { | ||
51 | CONTINUE_EXPR | BREAK_EXPR | RETURN_EXPR => return Some(expr), | ||
52 | _ => (), | ||
53 | } | ||
54 | } | ||
55 | None | ||
56 | } | ||
57 | |||
20 | #[derive(Clone, Copy, Debug)] | 58 | #[derive(Clone, Copy, Debug)] |
21 | pub(crate) enum Cursor<'a> { | 59 | pub(crate) enum Cursor<'a> { |
22 | Replace(&'a SyntaxNode), | 60 | Replace(&'a SyntaxNode), |