aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_editor/src/code_actions.rs
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2018-10-02 16:51:39 +0100
committerbors[bot] <bors[bot]@users.noreply.github.com>2018-10-02 16:51:39 +0100
commit248ee0c3fe333f9180f8c9d9dfd4efcc6886b5bc (patch)
treef94bb3a8bd47e3612ef29e815532c07d7871fa0f /crates/ra_editor/src/code_actions.rs
parent7ffc114dab6d1e25ead195a5937cd4f9ca51ef2c (diff)
parent1a2a8dec14ec04ea8eeccae99fd885e7a280e45b (diff)
Merge #90
90: Inherent traversal r=matklad a=matklad bors r+ Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_editor/src/code_actions.rs')
-rw-r--r--crates/ra_editor/src/code_actions.rs18
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 @@
1use join_to_string::join; 1use join_to_string::join;
2 2
3use ra_syntax::{ 3use 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
102pub fn introduce_variable<'a>(file: &'a File, range: TextRange) -> Option<impl FnOnce() -> LocalEdit + 'a> { 100pub 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
132fn non_trivia_sibling(node: SyntaxNodeRef, direction: Direction) -> Option<SyntaxNodeRef> { 130fn 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}