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_assists/src/lib.rs | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) (limited to 'crates/ra_assists/src/lib.rs') diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index 95a530821..a2983ae87 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -14,6 +14,7 @@ mod test_db; pub mod ast_transform; use hir::db::HirDatabase; +use itertools::Either; use ra_db::FileRange; use ra_syntax::{TextRange, TextUnit}; use ra_text_edit::TextEdit; @@ -41,6 +42,21 @@ pub struct AssistAction { pub target: Option, } +#[derive(Debug, Clone)] +pub struct ResolvedAssist { + pub label: AssistLabel, + pub action_data: Either>, +} + +impl ResolvedAssist { + pub fn get_first_action(&self) -> AssistAction { + match &self.action_data { + Either::Left(action) => action.clone(), + Either::Right(actions) => actions[0].clone(), + } + } +} + /// Return all the assists applicable at the given position. /// /// Assists are returned in the "unresolved" state, that is only labels are @@ -65,7 +81,7 @@ where /// /// Assists are returned in the "resolved" state, that is with edit fully /// computed. -pub fn assists(db: &H, range: FileRange) -> Vec<(AssistLabel, AssistAction, Vec)> +pub fn assists(db: &H, range: FileRange) -> Vec where H: HirDatabase + 'static, { @@ -76,13 +92,11 @@ where .iter() .filter_map(|f| f(ctx.clone())) .map(|a| match a { - Assist::Resolved { label, action, alternative_actions } => { - (label, action, alternative_actions) - } + Assist::Resolved { assist } => assist, Assist::Unresolved { .. } => unreachable!(), }) .collect::>(); - a.sort_by(|a, b| match (a.1.target, b.1.target) { + a.sort_by(|a, b| match (a.get_first_action().target, b.get_first_action().target) { (Some(a), Some(b)) => a.len().cmp(&b.len()), (Some(_), None) => Ordering::Less, (None, Some(_)) => Ordering::Greater, @@ -177,7 +191,7 @@ mod helpers { AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); let action = match assist { Assist::Unresolved { .. } => unreachable!(), - Assist::Resolved { action, .. } => action, + Assist::Resolved { assist } => assist.get_first_action(), }; let actual = action.edit.apply(&before); @@ -204,7 +218,7 @@ mod helpers { AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); let action = match assist { Assist::Unresolved { .. } => unreachable!(), - Assist::Resolved { action, .. } => action, + Assist::Resolved { assist } => assist.get_first_action(), }; let mut actual = action.edit.apply(&before); @@ -227,7 +241,7 @@ mod helpers { AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); let action = match assist { Assist::Unresolved { .. } => unreachable!(), - Assist::Resolved { action, .. } => action, + Assist::Resolved { assist } => assist.get_first_action(), }; let range = action.target.expect("expected target on action"); @@ -246,7 +260,7 @@ mod helpers { AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); let action = match assist { Assist::Unresolved { .. } => unreachable!(), - Assist::Resolved { action, .. } => action, + Assist::Resolved { assist } => assist.get_first_action(), }; let range = action.target.expect("expected target on action"); @@ -296,10 +310,10 @@ mod tests { let mut assists = assists.iter(); assert_eq!( - assists.next().expect("expected assist").0.label, + assists.next().expect("expected assist").label.label, "Change visibility to pub(crate)" ); - assert_eq!(assists.next().expect("expected assist").0.label, "Add `#[derive]`"); + assert_eq!(assists.next().expect("expected assist").label.label, "Add `#[derive]`"); } #[test] @@ -318,7 +332,7 @@ mod tests { let assists = super::assists(&db, frange); let mut assists = assists.iter(); - assert_eq!(assists.next().expect("expected assist").0.label, "Extract into variable"); - assert_eq!(assists.next().expect("expected assist").0.label, "Replace with match"); + assert_eq!(assists.next().expect("expected assist").label.label, "Extract into variable"); + assert_eq!(assists.next().expect("expected assist").label.label, "Replace with match"); } } -- cgit v1.2.3