aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide')
-rw-r--r--crates/ra_ide/src/assists.rs44
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
3use ra_db::{FilePosition, FileRange}; 3use ra_db::{FilePosition, FileRange};
4 4
5use crate::{db::RootDatabase, SourceChange, SourceFileEdit}; 5use crate::{db::RootDatabase, FileId, SourceChange, SourceFileEdit};
6 6
7use either::Either;
7pub use ra_assists::AssistId; 8pub use ra_assists::AssistId;
9use ra_assists::{AssistAction, AssistLabel};
8 10
9#[derive(Debug)] 11#[derive(Debug)]
10pub struct Assist { 12pub 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
15pub(crate) fn assists(db: &RootDatabase, frange: FileRange) -> Vec<Assist> { 18pub(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
43fn 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}