diff options
author | Benjamin Coenen <[email protected]> | 2020-05-06 08:57:00 +0100 |
---|---|---|
committer | Benjamin Coenen <[email protected]> | 2020-05-06 08:57:00 +0100 |
commit | c4d128e454448191c4b21d8e151c673e4c42376e (patch) | |
tree | 17cf5bbf429642c52708cd0d3c1d8885b63543f0 /crates/ra_assists/src/lib.rs | |
parent | 0bf02f5ccac99c91f10ef46bb06ff2ea316c382c (diff) | |
parent | 30eb458b4fa8adcecd8cbf731bd1cfa9a7a8b88b (diff) |
Merge branch 'master' of github.com:rust-analyzer/rust-analyzer into fix_4311
Diffstat (limited to 'crates/ra_assists/src/lib.rs')
-rw-r--r-- | crates/ra_assists/src/lib.rs | 44 |
1 files changed, 22 insertions, 22 deletions
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index c5df86600..0f94f5ee8 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs | |||
@@ -17,13 +17,13 @@ mod doc_tests; | |||
17 | pub mod utils; | 17 | pub mod utils; |
18 | pub mod ast_transform; | 18 | pub mod ast_transform; |
19 | 19 | ||
20 | use hir::Semantics; | ||
20 | use ra_db::{FileId, FileRange}; | 21 | use ra_db::{FileId, FileRange}; |
21 | use ra_ide_db::RootDatabase; | 22 | use ra_ide_db::RootDatabase; |
22 | use ra_syntax::{TextRange, TextSize}; | 23 | use ra_syntax::{TextRange, TextSize}; |
23 | use ra_text_edit::TextEdit; | 24 | use ra_text_edit::TextEdit; |
24 | 25 | ||
25 | pub(crate) use crate::assist_ctx::{Assist, AssistCtx, AssistHandler}; | 26 | pub(crate) use crate::assist_ctx::{Assist, AssistCtx}; |
26 | use hir::Semantics; | ||
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. |
@@ -32,19 +32,20 @@ pub struct AssistId(pub &'static str); | |||
32 | 32 | ||
33 | #[derive(Debug, Clone)] | 33 | #[derive(Debug, Clone)] |
34 | pub struct AssistLabel { | 34 | pub struct AssistLabel { |
35 | pub id: AssistId, | ||
35 | /// Short description of the assist, as shown in the UI. | 36 | /// Short description of the assist, as shown in the UI. |
36 | pub label: String, | 37 | pub label: String, |
37 | pub id: AssistId, | 38 | pub group: Option<GroupLabel>, |
38 | } | 39 | } |
39 | 40 | ||
40 | #[derive(Clone, Debug)] | 41 | #[derive(Clone, Debug)] |
41 | pub struct GroupLabel(pub String); | 42 | pub struct GroupLabel(pub String); |
42 | 43 | ||
43 | impl AssistLabel { | 44 | impl AssistLabel { |
44 | pub(crate) fn new(label: String, id: AssistId) -> AssistLabel { | 45 | pub(crate) fn new(id: AssistId, label: String, group: Option<GroupLabel>) -> AssistLabel { |
45 | // FIXME: make fields private, so that this invariant can't be broken | 46 | // FIXME: make fields private, so that this invariant can't be broken |
46 | assert!(label.starts_with(|c: char| c.is_uppercase())); | 47 | assert!(label.starts_with(|c: char| c.is_uppercase())); |
47 | AssistLabel { label, id } | 48 | AssistLabel { id, label, group } |
48 | } | 49 | } |
49 | } | 50 | } |
50 | 51 | ||
@@ -60,7 +61,6 @@ pub struct AssistAction { | |||
60 | #[derive(Debug, Clone)] | 61 | #[derive(Debug, Clone)] |
61 | pub struct ResolvedAssist { | 62 | pub struct ResolvedAssist { |
62 | pub label: AssistLabel, | 63 | pub label: AssistLabel, |
63 | pub group_label: Option<GroupLabel>, | ||
64 | pub action: AssistAction, | 64 | pub action: AssistAction, |
65 | } | 65 | } |
66 | 66 | ||
@@ -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 { |