aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_editor/src/code_actions.rs
diff options
context:
space:
mode:
authorJeremy A. Kolb <[email protected]>2018-10-15 22:44:23 +0100
committerJeremy A. Kolb <[email protected]>2018-10-16 14:41:10 +0100
commit61f3a438d3a729a6be941bca1ff4c6a97a33f221 (patch)
tree6551967cc8c6e921b66071453ad7888a9121d326 /crates/ra_editor/src/code_actions.rs
parent39cb6c6d3f78b193f5873c3492e530bbd24d5dd2 (diff)
Cargo Format
Run `cargo fmt` and ignore generated files
Diffstat (limited to 'crates/ra_editor/src/code_actions.rs')
-rw-r--r--crates/ra_editor/src/code_actions.rs49
1 files changed, 29 insertions, 20 deletions
diff --git a/crates/ra_editor/src/code_actions.rs b/crates/ra_editor/src/code_actions.rs
index 7b0a48c81..cadcd2720 100644
--- a/crates/ra_editor/src/code_actions.rs
+++ b/crates/ra_editor/src/code_actions.rs
@@ -1,17 +1,14 @@
1use join_to_string::join; 1use join_to_string::join;
2 2
3use ra_syntax::{ 3use ra_syntax::{
4 File, TextUnit, TextRange, Direction, 4 algo::{find_covering_node, find_leaf_at_offset},
5 ast::{self, AstNode, AttrsOwner, TypeParamsOwner, NameOwner}, 5 ast::{self, AstNode, AttrsOwner, NameOwner, TypeParamsOwner},
6 Direction, File,
6 SyntaxKind::{COMMA, WHITESPACE}, 7 SyntaxKind::{COMMA, WHITESPACE},
7 SyntaxNodeRef, 8 SyntaxNodeRef, TextRange, TextUnit,
8 algo::{
9 find_leaf_at_offset,
10 find_covering_node,
11 },
12}; 9};
13 10
14use crate::{EditBuilder, Edit, find_node_at_offset}; 11use crate::{find_node_at_offset, Edit, EditBuilder};
15 12
16#[derive(Debug)] 13#[derive(Debug)]
17pub struct LocalEdit { 14pub struct LocalEdit {
@@ -52,9 +49,7 @@ pub fn add_derive<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce()
52 edit.insert(node_start, "#[derive()]\n".to_string()); 49 edit.insert(node_start, "#[derive()]\n".to_string());
53 node_start + TextUnit::of_str("#[derive(") 50 node_start + TextUnit::of_str("#[derive(")
54 } 51 }
55 Some(tt) => { 52 Some(tt) => tt.syntax().range().end() - TextUnit::of_char(')'),
56 tt.syntax().range().end() - TextUnit::of_char(')')
57 }
58 }; 53 };
59 LocalEdit { 54 LocalEdit {
60 edit: edit.finish(), 55 edit: edit.finish(),
@@ -74,14 +69,19 @@ pub fn add_impl<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() ->
74 let mut buf = String::new(); 69 let mut buf = String::new();
75 buf.push_str("\n\nimpl"); 70 buf.push_str("\n\nimpl");
76 if let Some(type_params) = type_params { 71 if let Some(type_params) = type_params {
77 type_params.syntax().text() 72 type_params.syntax().text().push_to(&mut buf);
78 .push_to(&mut buf);
79 } 73 }
80 buf.push_str(" "); 74 buf.push_str(" ");
81 buf.push_str(name.text().as_str()); 75 buf.push_str(name.text().as_str());
82 if let Some(type_params) = type_params { 76 if let Some(type_params) = type_params {
83 let lifetime_params = type_params.lifetime_params().filter_map(|it| it.lifetime()).map(|it| it.text()); 77 let lifetime_params = type_params
84 let type_params = type_params.type_params().filter_map(|it| it.name()).map(|it| it.text()); 78 .lifetime_params()
79 .filter_map(|it| it.lifetime())
80 .map(|it| it.text());
81 let type_params = type_params
82 .type_params()
83 .filter_map(|it| it.name())
84 .map(|it| it.text());
85 join(lifetime_params.chain(type_params)) 85 join(lifetime_params.chain(type_params))
86 .surround_with("<", ">") 86 .surround_with("<", ">")
87 .to_buf(&mut buf); 87 .to_buf(&mut buf);
@@ -97,10 +97,17 @@ pub fn add_impl<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() ->
97 }) 97 })
98} 98}
99 99
100pub fn introduce_variable<'a>(file: &'a File, range: TextRange) -> Option<impl FnOnce() -> LocalEdit + 'a> { 100pub fn introduce_variable<'a>(
101 file: &'a File,
102 range: TextRange,
103) -> Option<impl FnOnce() -> LocalEdit + 'a> {
101 let node = find_covering_node(file.syntax(), range); 104 let node = find_covering_node(file.syntax(), range);
102 let expr = node.ancestors().filter_map(ast::Expr::cast).next()?; 105 let expr = node.ancestors().filter_map(ast::Expr::cast).next()?;
103 let anchor_stmt = expr.syntax().ancestors().filter_map(ast::Stmt::cast).next()?; 106 let anchor_stmt = expr
107 .syntax()
108 .ancestors()
109 .filter_map(ast::Stmt::cast)
110 .next()?;
104 let indent = anchor_stmt.syntax().prev_sibling()?; 111 let indent = anchor_stmt.syntax().prev_sibling()?;
105 if indent.kind() != WHITESPACE { 112 if indent.kind() != WHITESPACE {
106 return None; 113 return None;
@@ -191,7 +198,8 @@ mod tests {
191 " 198 "
192fn foo() { 199fn foo() {
193 foo(<|>1 + 1<|>); 200 foo(<|>1 + 1<|>);
194}", " 201}",
202 "
195fn foo() { 203fn foo() {
196 let <|>var_name = 1 + 1; 204 let <|>var_name = 1 + 1;
197 foo(var_name); 205 foo(var_name);
@@ -201,11 +209,12 @@ fn foo() {
201 } 209 }
202 #[test] 210 #[test]
203 fn test_intrdoduce_var_expr_stmt() { 211 fn test_intrdoduce_var_expr_stmt() {
204check_action_range( 212 check_action_range(
205 " 213 "
206fn foo() { 214fn foo() {
207 <|>1 + 1<|>; 215 <|>1 + 1<|>;
208}", " 216}",
217 "
209fn foo() { 218fn foo() {
210 let <|>var_name = 1 + 1; 219 let <|>var_name = 1 + 1;
211}", 220}",