aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src')
-rw-r--r--crates/ra_assists/src/add_derive.rs6
-rw-r--r--crates/ra_assists/src/add_impl.rs22
-rw-r--r--crates/ra_assists/src/assist_ctx.rs16
-rw-r--r--crates/ra_assists/src/change_visibility.rs42
-rw-r--r--crates/ra_assists/src/fill_match_arms.rs5
-rw-r--r--crates/ra_assists/src/introduce_variable.rs6
-rw-r--r--crates/ra_assists/src/lib.rs16
-rw-r--r--crates/ra_assists/src/replace_if_let_with_match.rs6
-rw-r--r--crates/ra_assists/src/split_import.rs4
9 files changed, 27 insertions, 96 deletions
diff --git a/crates/ra_assists/src/add_derive.rs b/crates/ra_assists/src/add_derive.rs
index 01a4079f6..caf21e079 100644
--- a/crates/ra_assists/src/add_derive.rs
+++ b/crates/ra_assists/src/add_derive.rs
@@ -30,10 +30,8 @@ pub(crate) fn add_derive(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
30 30
31// Insert `derive` after doc comments. 31// Insert `derive` after doc comments.
32fn derive_insertion_offset(nominal: &ast::NominalDef) -> Option<TextUnit> { 32fn derive_insertion_offset(nominal: &ast::NominalDef) -> Option<TextUnit> {
33 let non_ws_child = nominal 33 let non_ws_child =
34 .syntax() 34 nominal.syntax().children().find(|it| it.kind() != COMMENT && it.kind() != WHITESPACE)?;
35 .children()
36 .find(|it| it.kind() != COMMENT && it.kind() != WHITESPACE)?;
37 Some(non_ws_child.range().start()) 35 Some(non_ws_child.range().start())
38} 36}
39 37
diff --git a/crates/ra_assists/src/add_impl.rs b/crates/ra_assists/src/add_impl.rs
index 699508f91..f2360bc89 100644
--- a/crates/ra_assists/src/add_impl.rs
+++ b/crates/ra_assists/src/add_impl.rs
@@ -21,17 +21,11 @@ pub(crate) fn add_impl(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
21 buf.push_str(" "); 21 buf.push_str(" ");
22 buf.push_str(name.text().as_str()); 22 buf.push_str(name.text().as_str());
23 if let Some(type_params) = type_params { 23 if let Some(type_params) = type_params {
24 let lifetime_params = type_params 24 let lifetime_params =
25 .lifetime_params() 25 type_params.lifetime_params().filter_map(|it| it.lifetime()).map(|it| it.text());
26 .filter_map(|it| it.lifetime()) 26 let type_params =
27 .map(|it| it.text()); 27 type_params.type_params().filter_map(|it| it.name()).map(|it| it.text());
28 let type_params = type_params 28 join(lifetime_params.chain(type_params)).surround_with("<", ">").to_buf(&mut buf);
29 .type_params()
30 .filter_map(|it| it.name())
31 .map(|it| it.text());
32 join(lifetime_params.chain(type_params))
33 .surround_with("<", ">")
34 .to_buf(&mut buf);
35 } 29 }
36 buf.push_str(" {\n"); 30 buf.push_str(" {\n");
37 edit.set_cursor(start_offset + TextUnit::of_str(&buf)); 31 edit.set_cursor(start_offset + TextUnit::of_str(&buf));
@@ -47,11 +41,7 @@ mod tests {
47 41
48 #[test] 42 #[test]
49 fn test_add_impl() { 43 fn test_add_impl() {
50 check_assist( 44 check_assist(add_impl, "struct Foo {<|>}\n", "struct Foo {}\n\nimpl Foo {\n<|>\n}\n");
51 add_impl,
52 "struct Foo {<|>}\n",
53 "struct Foo {}\n\nimpl Foo {\n<|>\n}\n",
54 );
55 check_assist( 45 check_assist(
56 add_impl, 46 add_impl,
57 "struct Foo<T: Clone> {<|>}", 47 "struct Foo<T: Clone> {<|>}",
diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs
index 6d09bde52..0bf640241 100644
--- a/crates/ra_assists/src/assist_ctx.rs
+++ b/crates/ra_assists/src/assist_ctx.rs
@@ -69,12 +69,7 @@ impl<'a, DB: HirDatabase> AssistCtx<'a, DB> {
69 F: FnOnce(AssistCtx<DB>) -> T, 69 F: FnOnce(AssistCtx<DB>) -> T,
70 { 70 {
71 let source_file = &db.parse(frange.file_id); 71 let source_file = &db.parse(frange.file_id);
72 let ctx = AssistCtx { 72 let ctx = AssistCtx { db, frange, source_file, should_compute_edit };
73 db,
74 frange,
75 source_file,
76 should_compute_edit,
77 };
78 f(ctx) 73 f(ctx)
79 } 74 }
80 75
@@ -83,9 +78,7 @@ impl<'a, DB: HirDatabase> AssistCtx<'a, DB> {
83 label: impl Into<String>, 78 label: impl Into<String>,
84 f: impl FnOnce(&mut AssistBuilder), 79 f: impl FnOnce(&mut AssistBuilder),
85 ) -> Option<Assist> { 80 ) -> Option<Assist> {
86 let label = AssistLabel { 81 let label = AssistLabel { label: label.into() };
87 label: label.into(),
88 };
89 if !self.should_compute_edit { 82 if !self.should_compute_edit {
90 return Some(Assist::Unresolved(label)); 83 return Some(Assist::Unresolved(label));
91 } 84 }
@@ -146,9 +139,6 @@ impl AssistBuilder {
146 } 139 }
147 140
148 fn build(self) -> AssistAction { 141 fn build(self) -> AssistAction {
149 AssistAction { 142 AssistAction { edit: self.edit.finish(), cursor_position: self.cursor_position }
150 edit: self.edit.finish(),
151 cursor_position: self.cursor_position,
152 }
153 } 143 }
154} 144}
diff --git a/crates/ra_assists/src/change_visibility.rs b/crates/ra_assists/src/change_visibility.rs
index 4cd32985e..fa5f231c8 100644
--- a/crates/ra_assists/src/change_visibility.rs
+++ b/crates/ra_assists/src/change_visibility.rs
@@ -81,31 +81,11 @@ mod tests {
81 81
82 #[test] 82 #[test]
83 fn change_visibility_adds_pub_crate_to_items() { 83 fn change_visibility_adds_pub_crate_to_items() {
84 check_assist( 84 check_assist(change_visibility, "<|>fn foo() {}", "<|>pub(crate) fn foo() {}");
85 change_visibility, 85 check_assist(change_visibility, "f<|>n foo() {}", "<|>pub(crate) fn foo() {}");
86 "<|>fn foo() {}", 86 check_assist(change_visibility, "<|>struct Foo {}", "<|>pub(crate) struct Foo {}");
87 "<|>pub(crate) fn foo() {}", 87 check_assist(change_visibility, "<|>mod foo {}", "<|>pub(crate) mod foo {}");
88 ); 88 check_assist(change_visibility, "<|>trait Foo {}", "<|>pub(crate) trait Foo {}");
89 check_assist(
90 change_visibility,
91 "f<|>n foo() {}",
92 "<|>pub(crate) fn foo() {}",
93 );
94 check_assist(
95 change_visibility,
96 "<|>struct Foo {}",
97 "<|>pub(crate) struct Foo {}",
98 );
99 check_assist(
100 change_visibility,
101 "<|>mod foo {}",
102 "<|>pub(crate) mod foo {}",
103 );
104 check_assist(
105 change_visibility,
106 "<|>trait Foo {}",
107 "<|>pub(crate) trait Foo {}",
108 );
109 check_assist(change_visibility, "m<|>od {}", "<|>pub(crate) mod {}"); 89 check_assist(change_visibility, "m<|>od {}", "<|>pub(crate) mod {}");
110 check_assist( 90 check_assist(
111 change_visibility, 91 change_visibility,
@@ -125,20 +105,12 @@ mod tests {
125 105
126 #[test] 106 #[test]
127 fn change_visibility_pub_to_pub_crate() { 107 fn change_visibility_pub_to_pub_crate() {
128 check_assist( 108 check_assist(change_visibility, "<|>pub fn foo() {}", "<|>pub(crate) fn foo() {}")
129 change_visibility,
130 "<|>pub fn foo() {}",
131 "<|>pub(crate) fn foo() {}",
132 )
133 } 109 }
134 110
135 #[test] 111 #[test]
136 fn change_visibility_pub_crate_to_pub() { 112 fn change_visibility_pub_crate_to_pub() {
137 check_assist( 113 check_assist(change_visibility, "<|>pub(crate) fn foo() {}", "<|>pub fn foo() {}")
138 change_visibility,
139 "<|>pub(crate) fn foo() {}",
140 "<|>pub fn foo() {}",
141 )
142 } 114 }
143 115
144 #[test] 116 #[test]
diff --git a/crates/ra_assists/src/fill_match_arms.rs b/crates/ra_assists/src/fill_match_arms.rs
index 9aa37d94c..741f75e2a 100644
--- a/crates/ra_assists/src/fill_match_arms.rs
+++ b/crates/ra_assists/src/fill_match_arms.rs
@@ -27,10 +27,7 @@ pub(crate) fn fill_match_arms(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist
27 let node_expr = syntax_mapping.node_expr(expr)?; 27 let node_expr = syntax_mapping.node_expr(expr)?;
28 let match_expr_ty = infer_result[node_expr].clone(); 28 let match_expr_ty = infer_result[node_expr].clone();
29 let enum_def = match match_expr_ty { 29 let enum_def = match match_expr_ty {
30 Ty::Adt { 30 Ty::Adt { def_id: AdtDef::Enum(e), .. } => e,
31 def_id: AdtDef::Enum(e),
32 ..
33 } => e,
34 _ => return None, 31 _ => return None,
35 }; 32 };
36 let enum_name = enum_def.name(ctx.db)?; 33 let enum_name = enum_def.name(ctx.db)?;
diff --git a/crates/ra_assists/src/introduce_variable.rs b/crates/ra_assists/src/introduce_variable.rs
index f587b4fe6..4f7c9f3c2 100644
--- a/crates/ra_assists/src/introduce_variable.rs
+++ b/crates/ra_assists/src/introduce_variable.rs
@@ -81,11 +81,7 @@ fn anchor_stmt(expr: &ast::Expr) -> Option<(&SyntaxNode, bool)> {
81 return Some((node, false)); 81 return Some((node, false));
82 } 82 }
83 83
84 if let Some(expr) = node 84 if let Some(expr) = node.parent().and_then(ast::Block::cast).and_then(|it| it.expr()) {
85 .parent()
86 .and_then(ast::Block::cast)
87 .and_then(|it| it.expr())
88 {
89 if expr.syntax() == node { 85 if expr.syntax() == node {
90 return Some((node, false)); 86 return Some((node, false));
91 } 87 }
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs
index 555af51bc..881db6347 100644
--- a/crates/ra_assists/src/lib.rs
+++ b/crates/ra_assists/src/lib.rs
@@ -89,9 +89,7 @@ fn all_assists<DB: HirDatabase>() -> &'static [fn(AssistCtx<DB>) -> Option<Assis
89} 89}
90 90
91fn non_trivia_sibling(node: &SyntaxNode, direction: Direction) -> Option<&SyntaxNode> { 91fn non_trivia_sibling(node: &SyntaxNode, direction: Direction) -> Option<&SyntaxNode> {
92 node.siblings(direction) 92 node.siblings(direction).skip(1).find(|node| !node.kind().is_trivia())
93 .skip(1)
94 .find(|node| !node.kind().is_trivia())
95} 93}
96 94
97#[cfg(test)] 95#[cfg(test)]
@@ -110,10 +108,8 @@ mod helpers {
110 ) { 108 ) {
111 let (before_cursor_pos, before) = extract_offset(before); 109 let (before_cursor_pos, before) = extract_offset(before);
112 let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); 110 let (db, _source_root, file_id) = MockDatabase::with_single_file(&before);
113 let frange = FileRange { 111 let frange =
114 file_id, 112 FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
115 range: TextRange::offset_len(before_cursor_pos, 0.into()),
116 };
117 let assist = 113 let assist =
118 AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); 114 AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable");
119 let action = match assist { 115 let action = match assist {
@@ -161,10 +157,8 @@ mod helpers {
161 ) { 157 ) {
162 let (before_cursor_pos, before) = extract_offset(before); 158 let (before_cursor_pos, before) = extract_offset(before);
163 let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); 159 let (db, _source_root, file_id) = MockDatabase::with_single_file(&before);
164 let frange = FileRange { 160 let frange =
165 file_id, 161 FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
166 range: TextRange::offset_len(before_cursor_pos, 0.into()),
167 };
168 let assist = AssistCtx::with_ctx(&db, frange, true, assist); 162 let assist = AssistCtx::with_ctx(&db, frange, true, assist);
169 assert!(assist.is_none()); 163 assert!(assist.is_none());
170 } 164 }
diff --git a/crates/ra_assists/src/replace_if_let_with_match.rs b/crates/ra_assists/src/replace_if_let_with_match.rs
index f6af47ec9..683f0d119 100644
--- a/crates/ra_assists/src/replace_if_let_with_match.rs
+++ b/crates/ra_assists/src/replace_if_let_with_match.rs
@@ -30,11 +30,7 @@ fn build_match_expr(
30) -> String { 30) -> String {
31 let mut buf = String::new(); 31 let mut buf = String::new();
32 buf.push_str(&format!("match {} {{\n", expr.syntax().text())); 32 buf.push_str(&format!("match {} {{\n", expr.syntax().text()));
33 buf.push_str(&format!( 33 buf.push_str(&format!(" {} => {}\n", pat1.syntax().text(), format_arm(arm1)));
34 " {} => {}\n",
35 pat1.syntax().text(),
36 format_arm(arm1)
37 ));
38 buf.push_str(&format!(" _ => {}\n", format_arm(arm2))); 34 buf.push_str(&format!(" _ => {}\n", format_arm(arm2)));
39 buf.push_str("}"); 35 buf.push_str("}");
40 buf 36 buf
diff --git a/crates/ra_assists/src/split_import.rs b/crates/ra_assists/src/split_import.rs
index 7e34be087..fb69cef9c 100644
--- a/crates/ra_assists/src/split_import.rs
+++ b/crates/ra_assists/src/split_import.rs
@@ -8,9 +8,7 @@ use ra_syntax::{
8use crate::{AssistCtx, Assist}; 8use crate::{AssistCtx, Assist};
9 9
10pub(crate) fn split_import(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 10pub(crate) fn split_import(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
11 let colon_colon = ctx 11 let colon_colon = ctx.leaf_at_offset().find(|leaf| leaf.kind() == COLONCOLON)?;
12 .leaf_at_offset()
13 .find(|leaf| leaf.kind() == COLONCOLON)?;
14 let path = colon_colon.parent().and_then(ast::Path::cast)?; 12 let path = colon_colon.parent().and_then(ast::Path::cast)?;
15 let top_path = generate(Some(path), |it| it.parent_path()).last()?; 13 let top_path = generate(Some(path), |it| it.parent_path()).last()?;
16 14