aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/lib.rs')
-rw-r--r--crates/ra_assists/src/lib.rs39
1 files changed, 28 insertions, 11 deletions
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs
index 150b34ac7..d45b58966 100644
--- a/crates/ra_assists/src/lib.rs
+++ b/crates/ra_assists/src/lib.rs
@@ -13,6 +13,7 @@ mod doc_tests;
13mod test_db; 13mod test_db;
14pub mod ast_transform; 14pub mod ast_transform;
15 15
16use either::Either;
16use hir::db::HirDatabase; 17use hir::db::HirDatabase;
17use ra_db::FileRange; 18use ra_db::FileRange;
18use ra_syntax::{TextRange, TextUnit}; 19use ra_syntax::{TextRange, TextUnit};
@@ -35,11 +36,27 @@ pub struct AssistLabel {
35 36
36#[derive(Debug, Clone)] 37#[derive(Debug, Clone)]
37pub struct AssistAction { 38pub struct AssistAction {
39 pub label: Option<String>,
38 pub edit: TextEdit, 40 pub edit: TextEdit,
39 pub cursor_position: Option<TextUnit>, 41 pub cursor_position: Option<TextUnit>,
40 pub target: Option<TextRange>, 42 pub target: Option<TextRange>,
41} 43}
42 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
43/// Return all the assists applicable at the given position. 60/// Return all the assists applicable at the given position.
44/// 61///
45/// 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
@@ -64,7 +81,7 @@ where
64/// 81///
65/// 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
66/// computed. 83/// computed.
67pub fn assists<H>(db: &H, range: FileRange) -> Vec<(AssistLabel, AssistAction)> 84pub fn assists<H>(db: &H, range: FileRange) -> Vec<ResolvedAssist>
68where 85where
69 H: HirDatabase + 'static, 86 H: HirDatabase + 'static,
70{ 87{
@@ -75,11 +92,11 @@ where
75 .iter() 92 .iter()
76 .filter_map(|f| f(ctx.clone())) 93 .filter_map(|f| f(ctx.clone()))
77 .map(|a| match a { 94 .map(|a| match a {
78 Assist::Resolved { label, action } => (label, action), 95 Assist::Resolved { assist } => assist,
79 Assist::Unresolved { .. } => unreachable!(), 96 Assist::Unresolved { .. } => unreachable!(),
80 }) 97 })
81 .collect::<Vec<_>>(); 98 .collect::<Vec<_>>();
82 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) {
83 (Some(a), Some(b)) => a.len().cmp(&b.len()), 100 (Some(a), Some(b)) => a.len().cmp(&b.len()),
84 (Some(_), None) => Ordering::Less, 101 (Some(_), None) => Ordering::Less,
85 (None, Some(_)) => Ordering::Greater, 102 (None, Some(_)) => Ordering::Greater,
@@ -174,7 +191,7 @@ mod helpers {
174 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");
175 let action = match assist { 192 let action = match assist {
176 Assist::Unresolved { .. } => unreachable!(), 193 Assist::Unresolved { .. } => unreachable!(),
177 Assist::Resolved { action, .. } => action, 194 Assist::Resolved { assist } => assist.get_first_action(),
178 }; 195 };
179 196
180 let actual = action.edit.apply(&before); 197 let actual = action.edit.apply(&before);
@@ -201,7 +218,7 @@ mod helpers {
201 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");
202 let action = match assist { 219 let action = match assist {
203 Assist::Unresolved { .. } => unreachable!(), 220 Assist::Unresolved { .. } => unreachable!(),
204 Assist::Resolved { action, .. } => action, 221 Assist::Resolved { assist } => assist.get_first_action(),
205 }; 222 };
206 223
207 let mut actual = action.edit.apply(&before); 224 let mut actual = action.edit.apply(&before);
@@ -224,7 +241,7 @@ mod helpers {
224 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");
225 let action = match assist { 242 let action = match assist {
226 Assist::Unresolved { .. } => unreachable!(), 243 Assist::Unresolved { .. } => unreachable!(),
227 Assist::Resolved { action, .. } => action, 244 Assist::Resolved { assist } => assist.get_first_action(),
228 }; 245 };
229 246
230 let range = action.target.expect("expected target on action"); 247 let range = action.target.expect("expected target on action");
@@ -243,7 +260,7 @@ mod helpers {
243 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");
244 let action = match assist { 261 let action = match assist {
245 Assist::Unresolved { .. } => unreachable!(), 262 Assist::Unresolved { .. } => unreachable!(),
246 Assist::Resolved { action, .. } => action, 263 Assist::Resolved { assist } => assist.get_first_action(),
247 }; 264 };
248 265
249 let range = action.target.expect("expected target on action"); 266 let range = action.target.expect("expected target on action");
@@ -293,10 +310,10 @@ mod tests {
293 let mut assists = assists.iter(); 310 let mut assists = assists.iter();
294 311
295 assert_eq!( 312 assert_eq!(
296 assists.next().expect("expected assist").0.label, 313 assists.next().expect("expected assist").label.label,
297 "Change visibility to pub(crate)" 314 "Change visibility to pub(crate)"
298 ); 315 );
299 assert_eq!(assists.next().expect("expected assist").0.label, "Add `#[derive]`"); 316 assert_eq!(assists.next().expect("expected assist").label.label, "Add `#[derive]`");
300 } 317 }
301 318
302 #[test] 319 #[test]
@@ -315,7 +332,7 @@ mod tests {
315 let assists = super::assists(&db, frange); 332 let assists = super::assists(&db, frange);
316 let mut assists = assists.iter(); 333 let mut assists = assists.iter();
317 334
318 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");
319 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");
320 } 337 }
321} 338}