aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/lib.rs
diff options
context:
space:
mode:
authorKirill Bulatov <[email protected]>2020-01-11 22:40:36 +0000
committerKirill Bulatov <[email protected]>2020-01-15 18:17:17 +0000
commit78a21253b494e573885ac8336bff6e93b401046f (patch)
tree3ec31f8481f9a930b0b3afb6cc0ce4c97cf648f2 /crates/ra_assists/src/lib.rs
parent73dc8b6f06b49f4728a50e83781c361e9a8b3100 (diff)
Apply the api design suggestions
Diffstat (limited to 'crates/ra_assists/src/lib.rs')
-rw-r--r--crates/ra_assists/src/lib.rs40
1 files changed, 27 insertions, 13 deletions
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;
14pub mod ast_transform; 14pub mod ast_transform;
15 15
16use hir::db::HirDatabase; 16use hir::db::HirDatabase;
17use itertools::Either;
17use ra_db::FileRange; 18use ra_db::FileRange;
18use ra_syntax::{TextRange, TextUnit}; 19use ra_syntax::{TextRange, TextUnit};
19use ra_text_edit::TextEdit; 20use ra_text_edit::TextEdit;
@@ -41,6 +42,21 @@ pub struct AssistAction {
41 pub target: Option<TextRange>, 42 pub target: Option<TextRange>,
42} 43}
43 44
45#[derive(Debug, Clone)]
46pub struct ResolvedAssist {
47 pub label: AssistLabel,
48 pub action_data: Either<AssistAction, Vec<AssistAction>>,
49}
50
51impl ResolvedAssist {
52 pub fn get_first_action(&self) -> AssistAction {
53 match &self.action_data {
54 Either::Left(action) => action.clone(),
55 Either::Right(actions) => actions[0].clone(),
56 }
57 }
58}
59
44/// Return all the assists applicable at the given position. 60/// Return all the assists applicable at the given position.
45/// 61///
46/// Assists are returned in the "unresolved" state, that is only labels are 62/// Assists are returned in the "unresolved" state, that is only labels are
@@ -65,7 +81,7 @@ where
65/// 81///
66/// Assists are returned in the "resolved" state, that is with edit fully 82/// Assists are returned in the "resolved" state, that is with edit fully
67/// computed. 83/// computed.
68pub fn assists<H>(db: &H, range: FileRange) -> Vec<(AssistLabel, AssistAction, Vec<AssistAction>)> 84pub fn assists<H>(db: &H, range: FileRange) -> Vec<ResolvedAssist>
69where 85where
70 H: HirDatabase + 'static, 86 H: HirDatabase + 'static,
71{ 87{
@@ -76,13 +92,11 @@ where
76 .iter() 92 .iter()
77 .filter_map(|f| f(ctx.clone())) 93 .filter_map(|f| f(ctx.clone()))
78 .map(|a| match a { 94 .map(|a| match a {
79 Assist::Resolved { label, action, alternative_actions } => { 95 Assist::Resolved { assist } => assist,
80 (label, action, alternative_actions)
81 }
82 Assist::Unresolved { .. } => unreachable!(), 96 Assist::Unresolved { .. } => unreachable!(),
83 }) 97 })
84 .collect::<Vec<_>>(); 98 .collect::<Vec<_>>();
85 a.sort_by(|a, b| match (a.1.target, b.1.target) { 99 a.sort_by(|a, b| match (a.get_first_action().target, b.get_first_action().target) {
86 (Some(a), Some(b)) => a.len().cmp(&b.len()), 100 (Some(a), Some(b)) => a.len().cmp(&b.len()),
87 (Some(_), None) => Ordering::Less, 101 (Some(_), None) => Ordering::Less,
88 (None, Some(_)) => Ordering::Greater, 102 (None, Some(_)) => Ordering::Greater,
@@ -177,7 +191,7 @@ mod helpers {
177 AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); 191 AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable");
178 let action = match assist { 192 let action = match assist {
179 Assist::Unresolved { .. } => unreachable!(), 193 Assist::Unresolved { .. } => unreachable!(),
180 Assist::Resolved { action, .. } => action, 194 Assist::Resolved { assist } => assist.get_first_action(),
181 }; 195 };
182 196
183 let actual = action.edit.apply(&before); 197 let actual = action.edit.apply(&before);
@@ -204,7 +218,7 @@ mod helpers {
204 AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); 218 AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable");
205 let action = match assist { 219 let action = match assist {
206 Assist::Unresolved { .. } => unreachable!(), 220 Assist::Unresolved { .. } => unreachable!(),
207 Assist::Resolved { action, .. } => action, 221 Assist::Resolved { assist } => assist.get_first_action(),
208 }; 222 };
209 223
210 let mut actual = action.edit.apply(&before); 224 let mut actual = action.edit.apply(&before);
@@ -227,7 +241,7 @@ mod helpers {
227 AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); 241 AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable");
228 let action = match assist { 242 let action = match assist {
229 Assist::Unresolved { .. } => unreachable!(), 243 Assist::Unresolved { .. } => unreachable!(),
230 Assist::Resolved { action, .. } => action, 244 Assist::Resolved { assist } => assist.get_first_action(),
231 }; 245 };
232 246
233 let range = action.target.expect("expected target on action"); 247 let range = action.target.expect("expected target on action");
@@ -246,7 +260,7 @@ mod helpers {
246 AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); 260 AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable");
247 let action = match assist { 261 let action = match assist {
248 Assist::Unresolved { .. } => unreachable!(), 262 Assist::Unresolved { .. } => unreachable!(),
249 Assist::Resolved { action, .. } => action, 263 Assist::Resolved { assist } => assist.get_first_action(),
250 }; 264 };
251 265
252 let range = action.target.expect("expected target on action"); 266 let range = action.target.expect("expected target on action");
@@ -296,10 +310,10 @@ mod tests {
296 let mut assists = assists.iter(); 310 let mut assists = assists.iter();
297 311
298 assert_eq!( 312 assert_eq!(
299 assists.next().expect("expected assist").0.label, 313 assists.next().expect("expected assist").label.label,
300 "Change visibility to pub(crate)" 314 "Change visibility to pub(crate)"
301 ); 315 );
302 assert_eq!(assists.next().expect("expected assist").0.label, "Add `#[derive]`"); 316 assert_eq!(assists.next().expect("expected assist").label.label, "Add `#[derive]`");
303 } 317 }
304 318
305 #[test] 319 #[test]
@@ -318,7 +332,7 @@ mod tests {
318 let assists = super::assists(&db, frange); 332 let assists = super::assists(&db, frange);
319 let mut assists = assists.iter(); 333 let mut assists = assists.iter();
320 334
321 assert_eq!(assists.next().expect("expected assist").0.label, "Extract into variable"); 335 assert_eq!(assists.next().expect("expected assist").label.label, "Extract into variable");
322 assert_eq!(assists.next().expect("expected assist").0.label, "Replace with match"); 336 assert_eq!(assists.next().expect("expected assist").label.label, "Replace with match");
323 } 337 }
324} 338}