diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-01-15 18:43:23 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-01-15 18:43:23 +0000 |
commit | aa2e13b37f4508168fb064a79d0190fa705d8a47 (patch) | |
tree | 15d4b618885813c2c9efadd2ea0d25a7173807c8 /crates/ra_ide/src | |
parent | 01422cc31d1917aaef4b1f402eda05abfff1e75f (diff) | |
parent | 79b77403b65877e4d20bbbac6dd853a3beead445 (diff) |
Merge #2716
2716: Allow assists with multiple selectable actions r=SomeoneToIgnore a=SomeoneToIgnore
This PR prepares an infra for https://github.com/rust-analyzer/rust-analyzer/issues/2180 task by adding a possibility to specify multiple actions in one assist as multiple edit parameters to the `applySourceChange` command.
When this is done, the command opens a selection dialog, allowing the user to pick the edit to be applied.
I have no working example to test in this PR, but here's a demo of an auto import feature (a separate PR coming later for that one) using this functionality:
![out](https://user-images.githubusercontent.com/2690773/71633614-f8ea4d80-2c1d-11ea-9b15-0e13611a7aa4.gif)
The PR is not that massive as it may seem: all the assist files' changes are very generic and similar.
Co-authored-by: Kirill Bulatov <[email protected]>
Diffstat (limited to 'crates/ra_ide/src')
-rw-r--r-- | crates/ra_ide/src/assists.rs | 44 |
1 files changed, 35 insertions, 9 deletions
diff --git a/crates/ra_ide/src/assists.rs b/crates/ra_ide/src/assists.rs index e00589733..a936900da 100644 --- a/crates/ra_ide/src/assists.rs +++ b/crates/ra_ide/src/assists.rs | |||
@@ -2,27 +2,53 @@ | |||
2 | 2 | ||
3 | use ra_db::{FilePosition, FileRange}; | 3 | use ra_db::{FilePosition, FileRange}; |
4 | 4 | ||
5 | use crate::{db::RootDatabase, SourceChange, SourceFileEdit}; | 5 | use crate::{db::RootDatabase, FileId, SourceChange, SourceFileEdit}; |
6 | 6 | ||
7 | use either::Either; | ||
7 | pub use ra_assists::AssistId; | 8 | pub use ra_assists::AssistId; |
9 | use ra_assists::{AssistAction, AssistLabel}; | ||
8 | 10 | ||
9 | #[derive(Debug)] | 11 | #[derive(Debug)] |
10 | pub struct Assist { | 12 | pub struct Assist { |
11 | pub id: AssistId, | 13 | pub id: AssistId, |
12 | pub change: SourceChange, | 14 | pub label: String, |
15 | pub change_data: Either<SourceChange, Vec<SourceChange>>, | ||
13 | } | 16 | } |
14 | 17 | ||
15 | pub(crate) fn assists(db: &RootDatabase, frange: FileRange) -> Vec<Assist> { | 18 | pub(crate) fn assists(db: &RootDatabase, frange: FileRange) -> Vec<Assist> { |
16 | ra_assists::assists(db, frange) | 19 | ra_assists::assists(db, frange) |
17 | .into_iter() | 20 | .into_iter() |
18 | .map(|(label, action)| { | 21 | .map(|assist| { |
19 | let file_id = frange.file_id; | 22 | let file_id = frange.file_id; |
20 | let file_edit = SourceFileEdit { file_id, edit: action.edit }; | 23 | let assist_label = &assist.label; |
21 | let id = label.id; | 24 | Assist { |
22 | let change = SourceChange::source_file_edit(label.label, file_edit).with_cursor_opt( | 25 | id: assist_label.id, |
23 | action.cursor_position.map(|offset| FilePosition { offset, file_id }), | 26 | label: assist_label.label.clone(), |
24 | ); | 27 | change_data: match assist.action_data { |
25 | Assist { id, change } | 28 | Either::Left(action) => { |
29 | Either::Left(action_to_edit(action, file_id, assist_label)) | ||
30 | } | ||
31 | Either::Right(actions) => Either::Right( | ||
32 | actions | ||
33 | .into_iter() | ||
34 | .map(|action| action_to_edit(action, file_id, assist_label)) | ||
35 | .collect(), | ||
36 | ), | ||
37 | }, | ||
38 | } | ||
26 | }) | 39 | }) |
27 | .collect() | 40 | .collect() |
28 | } | 41 | } |
42 | |||
43 | fn action_to_edit( | ||
44 | action: AssistAction, | ||
45 | file_id: FileId, | ||
46 | assist_label: &AssistLabel, | ||
47 | ) -> SourceChange { | ||
48 | let file_edit = SourceFileEdit { file_id, edit: action.edit }; | ||
49 | SourceChange::source_file_edit( | ||
50 | action.label.unwrap_or_else(|| assist_label.label.clone()), | ||
51 | file_edit, | ||
52 | ) | ||
53 | .with_cursor_opt(action.cursor_position.map(|offset| FilePosition { offset, file_id })) | ||
54 | } | ||