diff options
Diffstat (limited to 'crates/ra_assists/src/lib.rs')
-rw-r--r-- | crates/ra_assists/src/lib.rs | 34 |
1 files changed, 17 insertions, 17 deletions
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index 5cec10088..0f94f5ee8 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs | |||
@@ -23,7 +23,7 @@ use ra_ide_db::RootDatabase; | |||
23 | use ra_syntax::{TextRange, TextSize}; | 23 | use ra_syntax::{TextRange, TextSize}; |
24 | use ra_text_edit::TextEdit; | 24 | use ra_text_edit::TextEdit; |
25 | 25 | ||
26 | pub(crate) use crate::assist_ctx::{Assist, AssistCtx, AssistHandler}; | 26 | pub(crate) use crate::assist_ctx::{Assist, AssistCtx}; |
27 | 27 | ||
28 | /// Unique identifier of the assist, should not be shown to the user | 28 | /// Unique identifier of the assist, should not be shown to the user |
29 | /// directly. | 29 | /// directly. |
@@ -109,7 +109,9 @@ pub fn resolved_assists(db: &RootDatabase, range: FileRange) -> Vec<ResolvedAssi | |||
109 | } | 109 | } |
110 | 110 | ||
111 | mod handlers { | 111 | mod handlers { |
112 | use crate::AssistHandler; | 112 | use crate::{Assist, AssistCtx}; |
113 | |||
114 | pub(crate) type Handler = fn(AssistCtx) -> Option<Assist>; | ||
113 | 115 | ||
114 | mod add_custom_impl; | 116 | mod add_custom_impl; |
115 | mod add_derive; | 117 | mod add_derive; |
@@ -145,12 +147,13 @@ mod handlers { | |||
145 | mod reorder_fields; | 147 | mod reorder_fields; |
146 | mod unwrap_block; | 148 | mod unwrap_block; |
147 | 149 | ||
148 | pub(crate) fn all() -> &'static [AssistHandler] { | 150 | pub(crate) fn all() -> &'static [Handler] { |
149 | &[ | 151 | &[ |
150 | // These are alphabetic for the foolish consistency | 152 | // These are alphabetic for the foolish consistency |
151 | add_custom_impl::add_custom_impl, | 153 | add_custom_impl::add_custom_impl, |
152 | add_derive::add_derive, | 154 | add_derive::add_derive, |
153 | add_explicit_type::add_explicit_type, | 155 | add_explicit_type::add_explicit_type, |
156 | add_from_impl_for_enum::add_from_impl_for_enum, | ||
154 | add_function::add_function, | 157 | add_function::add_function, |
155 | add_impl::add_impl, | 158 | add_impl::add_impl, |
156 | add_new::add_new, | 159 | add_new::add_new, |
@@ -176,17 +179,18 @@ mod handlers { | |||
176 | raw_string::remove_hash, | 179 | raw_string::remove_hash, |
177 | remove_dbg::remove_dbg, | 180 | remove_dbg::remove_dbg, |
178 | remove_mut::remove_mut, | 181 | remove_mut::remove_mut, |
182 | reorder_fields::reorder_fields, | ||
179 | replace_if_let_with_match::replace_if_let_with_match, | 183 | replace_if_let_with_match::replace_if_let_with_match, |
180 | replace_let_with_if_let::replace_let_with_if_let, | 184 | replace_let_with_if_let::replace_let_with_if_let, |
181 | replace_qualified_name_with_use::replace_qualified_name_with_use, | 185 | replace_qualified_name_with_use::replace_qualified_name_with_use, |
182 | replace_unwrap_with_match::replace_unwrap_with_match, | 186 | replace_unwrap_with_match::replace_unwrap_with_match, |
183 | split_import::split_import, | 187 | split_import::split_import, |
184 | add_from_impl_for_enum::add_from_impl_for_enum, | ||
185 | unwrap_block::unwrap_block, | 188 | unwrap_block::unwrap_block, |
186 | // These are manually sorted for better priorities | 189 | // These are manually sorted for better priorities |
187 | add_missing_impl_members::add_missing_impl_members, | 190 | add_missing_impl_members::add_missing_impl_members, |
188 | add_missing_impl_members::add_missing_default_members, | 191 | add_missing_impl_members::add_missing_default_members, |
189 | reorder_fields::reorder_fields, | 192 | // Are you sure you want to add new assist here, and not to the |
193 | // sorted list above? | ||
190 | ] | 194 | ] |
191 | } | 195 | } |
192 | } | 196 | } |
@@ -195,12 +199,12 @@ mod handlers { | |||
195 | mod helpers { | 199 | mod helpers { |
196 | use std::sync::Arc; | 200 | use std::sync::Arc; |
197 | 201 | ||
202 | use hir::Semantics; | ||
198 | use ra_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt}; | 203 | use ra_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt}; |
199 | use ra_ide_db::{symbol_index::SymbolsDatabase, RootDatabase}; | 204 | use ra_ide_db::{symbol_index::SymbolsDatabase, RootDatabase}; |
200 | use test_utils::{add_cursor, assert_eq_text, extract_range_or_offset, RangeOrOffset}; | 205 | use test_utils::{add_cursor, assert_eq_text, extract_range_or_offset, RangeOrOffset}; |
201 | 206 | ||
202 | use crate::{AssistCtx, AssistFile, AssistHandler}; | 207 | use crate::{handlers::Handler, AssistCtx, AssistFile}; |
203 | use hir::Semantics; | ||
204 | 208 | ||
205 | pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) { | 209 | pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) { |
206 | let (mut db, file_id) = RootDatabase::with_single_file(text); | 210 | let (mut db, file_id) = RootDatabase::with_single_file(text); |
@@ -210,22 +214,18 @@ mod helpers { | |||
210 | (db, file_id) | 214 | (db, file_id) |
211 | } | 215 | } |
212 | 216 | ||
213 | pub(crate) fn check_assist( | 217 | pub(crate) fn check_assist(assist: Handler, ra_fixture_before: &str, ra_fixture_after: &str) { |
214 | assist: AssistHandler, | ||
215 | ra_fixture_before: &str, | ||
216 | ra_fixture_after: &str, | ||
217 | ) { | ||
218 | check(assist, ra_fixture_before, ExpectedResult::After(ra_fixture_after)); | 218 | check(assist, ra_fixture_before, ExpectedResult::After(ra_fixture_after)); |
219 | } | 219 | } |
220 | 220 | ||
221 | // FIXME: instead of having a separate function here, maybe use | 221 | // FIXME: instead of having a separate function here, maybe use |
222 | // `extract_ranges` and mark the target as `<target> </target>` in the | 222 | // `extract_ranges` and mark the target as `<target> </target>` in the |
223 | // fixuture? | 223 | // fixuture? |
224 | pub(crate) fn check_assist_target(assist: AssistHandler, ra_fixture: &str, target: &str) { | 224 | pub(crate) fn check_assist_target(assist: Handler, ra_fixture: &str, target: &str) { |
225 | check(assist, ra_fixture, ExpectedResult::Target(target)); | 225 | check(assist, ra_fixture, ExpectedResult::Target(target)); |
226 | } | 226 | } |
227 | 227 | ||
228 | pub(crate) fn check_assist_not_applicable(assist: AssistHandler, ra_fixture: &str) { | 228 | pub(crate) fn check_assist_not_applicable(assist: Handler, ra_fixture: &str) { |
229 | check(assist, ra_fixture, ExpectedResult::NotApplicable); | 229 | check(assist, ra_fixture, ExpectedResult::NotApplicable); |
230 | } | 230 | } |
231 | 231 | ||
@@ -235,7 +235,7 @@ mod helpers { | |||
235 | Target(&'a str), | 235 | Target(&'a str), |
236 | } | 236 | } |
237 | 237 | ||
238 | fn check(assist: AssistHandler, before: &str, expected: ExpectedResult) { | 238 | fn check(assist: Handler, before: &str, expected: ExpectedResult) { |
239 | let (text_without_caret, file_with_caret_id, range_or_offset, db) = | 239 | let (text_without_caret, file_with_caret_id, range_or_offset, db) = |
240 | if before.contains("//-") { | 240 | if before.contains("//-") { |
241 | let (mut db, position) = RootDatabase::with_position(before); | 241 | let (mut db, position) = RootDatabase::with_position(before); |
@@ -261,13 +261,13 @@ mod helpers { | |||
261 | (Some(assist), ExpectedResult::After(after)) => { | 261 | (Some(assist), ExpectedResult::After(after)) => { |
262 | let action = assist.0[0].action.clone().unwrap(); | 262 | let action = assist.0[0].action.clone().unwrap(); |
263 | 263 | ||
264 | let assisted_file_text = if let AssistFile::TargetFile(file_id) = action.file { | 264 | let mut actual = if let AssistFile::TargetFile(file_id) = action.file { |
265 | db.file_text(file_id).as_ref().to_owned() | 265 | db.file_text(file_id).as_ref().to_owned() |
266 | } else { | 266 | } else { |
267 | text_without_caret | 267 | text_without_caret |
268 | }; | 268 | }; |
269 | action.edit.apply(&mut actual); | ||
269 | 270 | ||
270 | let mut actual = action.edit.apply(&assisted_file_text); | ||
271 | match action.cursor_position { | 271 | match action.cursor_position { |
272 | None => { | 272 | None => { |
273 | if let RangeOrOffset::Offset(before_cursor_pos) = range_or_offset { | 273 | if let RangeOrOffset::Offset(before_cursor_pos) = range_or_offset { |