From 73dc8b6f06b49f4728a50e83781c361e9a8b3100 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Thu, 2 Jan 2020 01:39:01 +0200 Subject: Another attempt to add multiple edits --- crates/ra_ide/src/assists.rs | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) (limited to 'crates/ra_ide/src') diff --git a/crates/ra_ide/src/assists.rs b/crates/ra_ide/src/assists.rs index e00589733..db6e4e8b7 100644 --- a/crates/ra_ide/src/assists.rs +++ b/crates/ra_ide/src/assists.rs @@ -2,27 +2,46 @@ use ra_db::{FilePosition, FileRange}; -use crate::{db::RootDatabase, SourceChange, SourceFileEdit}; +use crate::{db::RootDatabase, FileId, SourceChange, SourceFileEdit}; pub use ra_assists::AssistId; +use ra_assists::{AssistAction, AssistLabel}; #[derive(Debug)] pub struct Assist { pub id: AssistId, pub change: SourceChange, + pub label: String, + pub alternative_changes: Vec, } pub(crate) fn assists(db: &RootDatabase, frange: FileRange) -> Vec { ra_assists::assists(db, frange) .into_iter() - .map(|(label, action)| { + .map(|(assist_label, action, alternative_actions)| { let file_id = frange.file_id; - let file_edit = SourceFileEdit { file_id, edit: action.edit }; - let id = label.id; - let change = SourceChange::source_file_edit(label.label, file_edit).with_cursor_opt( - action.cursor_position.map(|offset| FilePosition { offset, file_id }), - ); - Assist { id, change } + Assist { + id: assist_label.id, + label: assist_label.label.clone(), + change: action_to_edit(action, file_id, &assist_label), + alternative_changes: alternative_actions + .into_iter() + .map(|action| action_to_edit(action, file_id, &assist_label)) + .collect(), + } }) .collect() } + +fn action_to_edit( + action: AssistAction, + file_id: FileId, + assist_label: &AssistLabel, +) -> SourceChange { + let file_edit = SourceFileEdit { file_id, edit: action.edit }; + SourceChange::source_file_edit( + action.label.unwrap_or_else(|| assist_label.label.clone()), + file_edit, + ) + .with_cursor_opt(action.cursor_position.map(|offset| FilePosition { offset, file_id })) +} -- cgit v1.2.3 From 78a21253b494e573885ac8336bff6e93b401046f Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Sun, 12 Jan 2020 00:40:36 +0200 Subject: Apply the api design suggestions --- crates/ra_ide/src/assists.rs | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'crates/ra_ide/src') diff --git a/crates/ra_ide/src/assists.rs b/crates/ra_ide/src/assists.rs index db6e4e8b7..e30eee5f4 100644 --- a/crates/ra_ide/src/assists.rs +++ b/crates/ra_ide/src/assists.rs @@ -4,30 +4,37 @@ use ra_db::{FilePosition, FileRange}; use crate::{db::RootDatabase, FileId, SourceChange, SourceFileEdit}; +use itertools::Either; pub use ra_assists::AssistId; use ra_assists::{AssistAction, AssistLabel}; #[derive(Debug)] pub struct Assist { pub id: AssistId, - pub change: SourceChange, pub label: String, - pub alternative_changes: Vec, + pub change_data: Either>, } pub(crate) fn assists(db: &RootDatabase, frange: FileRange) -> Vec { ra_assists::assists(db, frange) .into_iter() - .map(|(assist_label, action, alternative_actions)| { + .map(|assist| { let file_id = frange.file_id; + let assist_label = &assist.label; Assist { id: assist_label.id, label: assist_label.label.clone(), - change: action_to_edit(action, file_id, &assist_label), - alternative_changes: alternative_actions - .into_iter() - .map(|action| action_to_edit(action, file_id, &assist_label)) - .collect(), + change_data: match assist.action_data { + Either::Left(action) => { + Either::Left(action_to_edit(action, file_id, assist_label)) + } + Either::Right(actions) => Either::Right( + actions + .into_iter() + .map(|action| action_to_edit(action, file_id, assist_label)) + .collect(), + ), + }, } }) .collect() -- cgit v1.2.3 From d51cf7794d110b064fd0e8d53726b4608ec50d1a Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Wed, 15 Jan 2020 20:20:20 +0200 Subject: itertools::Either -> either::Either --- crates/ra_ide/src/assists.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_ide/src') diff --git a/crates/ra_ide/src/assists.rs b/crates/ra_ide/src/assists.rs index e30eee5f4..a936900da 100644 --- a/crates/ra_ide/src/assists.rs +++ b/crates/ra_ide/src/assists.rs @@ -4,7 +4,7 @@ use ra_db::{FilePosition, FileRange}; use crate::{db::RootDatabase, FileId, SourceChange, SourceFileEdit}; -use itertools::Either; +use either::Either; pub use ra_assists::AssistId; use ra_assists::{AssistAction, AssistLabel}; -- cgit v1.2.3