diff options
Diffstat (limited to 'crates/ra_editor/src/code_actions.rs')
-rw-r--r-- | crates/ra_editor/src/code_actions.rs | 18 |
1 files changed, 8 insertions, 10 deletions
diff --git a/crates/ra_editor/src/code_actions.rs b/crates/ra_editor/src/code_actions.rs index 83f7956d2..216d592ff 100644 --- a/crates/ra_editor/src/code_actions.rs +++ b/crates/ra_editor/src/code_actions.rs | |||
@@ -1,15 +1,13 @@ | |||
1 | use join_to_string::join; | 1 | use join_to_string::join; |
2 | 2 | ||
3 | use ra_syntax::{ | 3 | use ra_syntax::{ |
4 | File, TextUnit, TextRange, | 4 | File, TextUnit, TextRange, Direction, |
5 | ast::{self, AstNode, AttrsOwner, TypeParamsOwner, NameOwner}, | 5 | ast::{self, AstNode, AttrsOwner, TypeParamsOwner, NameOwner}, |
6 | SyntaxKind::{COMMA, WHITESPACE}, | 6 | SyntaxKind::{COMMA, WHITESPACE}, |
7 | SyntaxNodeRef, | 7 | SyntaxNodeRef, |
8 | algo::{ | 8 | algo::{ |
9 | Direction, siblings, | ||
10 | find_leaf_at_offset, | 9 | find_leaf_at_offset, |
11 | find_covering_node, | 10 | find_covering_node, |
12 | ancestors, | ||
13 | }, | 11 | }, |
14 | }; | 12 | }; |
15 | 13 | ||
@@ -25,12 +23,12 @@ pub fn flip_comma<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() | |||
25 | let syntax = file.syntax(); | 23 | let syntax = file.syntax(); |
26 | 24 | ||
27 | let comma = find_leaf_at_offset(syntax, offset).find(|leaf| leaf.kind() == COMMA)?; | 25 | let comma = find_leaf_at_offset(syntax, offset).find(|leaf| leaf.kind() == COMMA)?; |
28 | let left = non_trivia_sibling(comma, Direction::Backward)?; | 26 | let prev = non_trivia_sibling(comma, Direction::Prev)?; |
29 | let right = non_trivia_sibling(comma, Direction::Forward)?; | 27 | let next = non_trivia_sibling(comma, Direction::Next)?; |
30 | Some(move || { | 28 | Some(move || { |
31 | let mut edit = EditBuilder::new(); | 29 | let mut edit = EditBuilder::new(); |
32 | edit.replace(left.range(), right.text().to_string()); | 30 | edit.replace(prev.range(), next.text().to_string()); |
33 | edit.replace(right.range(), left.text().to_string()); | 31 | edit.replace(next.range(), prev.text().to_string()); |
34 | LocalEdit { | 32 | LocalEdit { |
35 | edit: edit.finish(), | 33 | edit: edit.finish(), |
36 | cursor_position: None, | 34 | cursor_position: None, |
@@ -101,8 +99,8 @@ pub fn add_impl<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() -> | |||
101 | 99 | ||
102 | pub fn introduce_variable<'a>(file: &'a File, range: TextRange) -> Option<impl FnOnce() -> LocalEdit + 'a> { | 100 | pub fn introduce_variable<'a>(file: &'a File, range: TextRange) -> Option<impl FnOnce() -> LocalEdit + 'a> { |
103 | let node = find_covering_node(file.syntax(), range); | 101 | let node = find_covering_node(file.syntax(), range); |
104 | let expr = ancestors(node).filter_map(ast::Expr::cast).next()?; | 102 | let expr = node.ancestors().filter_map(ast::Expr::cast).next()?; |
105 | let anchor_stmt = ancestors(expr.syntax()).filter_map(ast::Stmt::cast).next()?; | 103 | let anchor_stmt = expr.syntax().ancestors().filter_map(ast::Stmt::cast).next()?; |
106 | let indent = anchor_stmt.syntax().prev_sibling()?; | 104 | let indent = anchor_stmt.syntax().prev_sibling()?; |
107 | if indent.kind() != WHITESPACE { | 105 | if indent.kind() != WHITESPACE { |
108 | return None; | 106 | return None; |
@@ -130,7 +128,7 @@ pub fn introduce_variable<'a>(file: &'a File, range: TextRange) -> Option<impl F | |||
130 | } | 128 | } |
131 | 129 | ||
132 | fn non_trivia_sibling(node: SyntaxNodeRef, direction: Direction) -> Option<SyntaxNodeRef> { | 130 | fn non_trivia_sibling(node: SyntaxNodeRef, direction: Direction) -> Option<SyntaxNodeRef> { |
133 | siblings(node, direction) | 131 | node.siblings(direction) |
134 | .skip(1) | 132 | .skip(1) |
135 | .find(|node| !node.kind().is_trivia()) | 133 | .find(|node| !node.kind().is_trivia()) |
136 | } | 134 | } |