@@ -14,6 +14,7 @@ mod test_db;
1414pub mod ast_transform;
1515
1616use hir:: db:: HirDatabase ;
17+ use itertools:: Either ;
1718use ra_db:: FileRange ;
1819use ra_syntax:: { TextRange , TextUnit } ;
1920use ra_text_edit:: TextEdit ;
@@ -41,6 +42,21 @@ pub struct AssistAction {
4142 pub target : Option < TextRange > ,
4243}
4344
45+ #[ derive( Debug , Clone ) ]
46+ pub struct ResolvedAssist {
47+ pub label : AssistLabel ,
48+ pub action_data : Either < AssistAction , Vec < AssistAction > > ,
49+ }
50+
51+ impl 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+
4460/// Return all the assists applicable at the given position.
4561///
4662/// Assists are returned in the "unresolved" state, that is only labels are
6581///
6682/// Assists are returned in the "resolved" state, that is with edit fully
6783/// computed.
68- pub fn assists < H > ( db : & H , range : FileRange ) -> Vec < ( AssistLabel , AssistAction , Vec < AssistAction > ) >
84+ pub fn assists < H > ( db : & H , range : FileRange ) -> Vec < ResolvedAssist >
6985where
7086 H : HirDatabase + ' static ,
7187{
@@ -76,13 +92,11 @@ where
7692 . iter ( )
7793 . filter_map ( |f| f ( ctx. clone ( ) ) )
7894 . map ( |a| match a {
79- Assist :: Resolved { label, action, alternative_actions } => {
80- ( label, action, alternative_actions)
81- }
95+ Assist :: Resolved { assist } => assist,
8296 Assist :: Unresolved { .. } => unreachable ! ( ) ,
8397 } )
8498 . 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 ) {
86100 ( Some ( a) , Some ( b) ) => a. len ( ) . cmp ( & b. len ( ) ) ,
87101 ( Some ( _) , None ) => Ordering :: Less ,
88102 ( None , Some ( _) ) => Ordering :: Greater ,
@@ -177,7 +191,7 @@ mod helpers {
177191 AssistCtx :: with_ctx ( & db, frange, true , assist) . expect ( "code action is not applicable" ) ;
178192 let action = match assist {
179193 Assist :: Unresolved { .. } => unreachable ! ( ) ,
180- Assist :: Resolved { action , .. } => action ,
194+ Assist :: Resolved { assist } => assist . get_first_action ( ) ,
181195 } ;
182196
183197 let actual = action. edit . apply ( & before) ;
@@ -204,7 +218,7 @@ mod helpers {
204218 AssistCtx :: with_ctx ( & db, frange, true , assist) . expect ( "code action is not applicable" ) ;
205219 let action = match assist {
206220 Assist :: Unresolved { .. } => unreachable ! ( ) ,
207- Assist :: Resolved { action , .. } => action ,
221+ Assist :: Resolved { assist } => assist . get_first_action ( ) ,
208222 } ;
209223
210224 let mut actual = action. edit . apply ( & before) ;
@@ -227,7 +241,7 @@ mod helpers {
227241 AssistCtx :: with_ctx ( & db, frange, true , assist) . expect ( "code action is not applicable" ) ;
228242 let action = match assist {
229243 Assist :: Unresolved { .. } => unreachable ! ( ) ,
230- Assist :: Resolved { action , .. } => action ,
244+ Assist :: Resolved { assist } => assist . get_first_action ( ) ,
231245 } ;
232246
233247 let range = action. target . expect ( "expected target on action" ) ;
@@ -246,7 +260,7 @@ mod helpers {
246260 AssistCtx :: with_ctx ( & db, frange, true , assist) . expect ( "code action is not applicable" ) ;
247261 let action = match assist {
248262 Assist :: Unresolved { .. } => unreachable ! ( ) ,
249- Assist :: Resolved { action , .. } => action ,
263+ Assist :: Resolved { assist } => assist . get_first_action ( ) ,
250264 } ;
251265
252266 let range = action. target . expect ( "expected target on action" ) ;
@@ -295,8 +309,8 @@ mod tests {
295309 let assists = super :: assists ( & db, frange) ;
296310 let mut assists = assists. iter ( ) ;
297311
298- assert_eq ! ( assists. next( ) . expect( "expected assist" ) . 0 . label, "make pub(crate)" ) ;
299- assert_eq ! ( assists. next( ) . expect( "expected assist" ) . 0 . label, "add `#[derive]`" ) ;
312+ assert_eq ! ( assists. next( ) . expect( "expected assist" ) . label . label, "make pub(crate)" ) ;
313+ assert_eq ! ( assists. next( ) . expect( "expected assist" ) . label . label, "add `#[derive]`" ) ;
300314 }
301315
302316 #[ test]
@@ -315,7 +329,7 @@ mod tests {
315329 let assists = super :: assists ( & db, frange) ;
316330 let mut assists = assists. iter ( ) ;
317331
318- assert_eq ! ( assists. next( ) . expect( "expected assist" ) . 0 . label, "introduce variable" ) ;
319- assert_eq ! ( assists. next( ) . expect( "expected assist" ) . 0 . label, "replace with match" ) ;
332+ assert_eq ! ( assists. next( ) . expect( "expected assist" ) . label . label, "introduce variable" ) ;
333+ assert_eq ! ( assists. next( ) . expect( "expected assist" ) . label . label, "replace with match" ) ;
320334 }
321335}
0 commit comments