@@ -13,6 +13,7 @@ mod doc_tests;
1313mod test_db;
1414pub mod ast_transform;
1515
16+ use either:: Either ;
1617use hir:: db:: HirDatabase ;
1718use ra_db:: FileRange ;
1819use ra_syntax:: { TextRange , TextUnit } ;
@@ -35,11 +36,27 @@ pub struct AssistLabel {
3536
3637#[ derive( Debug , Clone ) ]
3738pub struct AssistAction {
39+ pub label : Option < String > ,
3840 pub edit : TextEdit ,
3941 pub cursor_position : Option < TextUnit > ,
4042 pub target : Option < TextRange > ,
4143}
4244
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+
4360/// Return all the assists applicable at the given position.
4461///
4562/// Assists are returned in the "unresolved" state, that is only labels are
6481///
6582/// Assists are returned in the "resolved" state, that is with edit fully
6683/// computed.
67- pub fn assists < H > ( db : & H , range : FileRange ) -> Vec < ( AssistLabel , AssistAction ) >
84+ pub fn assists < H > ( db : & H , range : FileRange ) -> Vec < ResolvedAssist >
6885where
6986 H : HirDatabase + ' static ,
7087{
@@ -75,11 +92,11 @@ where
7592 . iter ( )
7693 . filter_map ( |f| f ( ctx. clone ( ) ) )
7794 . map ( |a| match a {
78- Assist :: Resolved { label , action } => ( label , action ) ,
95+ Assist :: Resolved { assist } => assist ,
7996 Assist :: Unresolved { .. } => unreachable ! ( ) ,
8097 } )
8198 . 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 ) {
83100 ( Some ( a) , Some ( b) ) => a. len ( ) . cmp ( & b. len ( ) ) ,
84101 ( Some ( _) , None ) => Ordering :: Less ,
85102 ( None , Some ( _) ) => Ordering :: Greater ,
@@ -174,7 +191,7 @@ mod helpers {
174191 AssistCtx :: with_ctx ( & db, frange, true , assist) . expect ( "code action is not applicable" ) ;
175192 let action = match assist {
176193 Assist :: Unresolved { .. } => unreachable ! ( ) ,
177- Assist :: Resolved { action , .. } => action ,
194+ Assist :: Resolved { assist } => assist . get_first_action ( ) ,
178195 } ;
179196
180197 let actual = action. edit . apply ( & before) ;
@@ -201,7 +218,7 @@ mod helpers {
201218 AssistCtx :: with_ctx ( & db, frange, true , assist) . expect ( "code action is not applicable" ) ;
202219 let action = match assist {
203220 Assist :: Unresolved { .. } => unreachable ! ( ) ,
204- Assist :: Resolved { action , .. } => action ,
221+ Assist :: Resolved { assist } => assist . get_first_action ( ) ,
205222 } ;
206223
207224 let mut actual = action. edit . apply ( & before) ;
@@ -224,7 +241,7 @@ mod helpers {
224241 AssistCtx :: with_ctx ( & db, frange, true , assist) . expect ( "code action is not applicable" ) ;
225242 let action = match assist {
226243 Assist :: Unresolved { .. } => unreachable ! ( ) ,
227- Assist :: Resolved { action , .. } => action ,
244+ Assist :: Resolved { assist } => assist . get_first_action ( ) ,
228245 } ;
229246
230247 let range = action. target . expect ( "expected target on action" ) ;
@@ -243,7 +260,7 @@ mod helpers {
243260 AssistCtx :: with_ctx ( & db, frange, true , assist) . expect ( "code action is not applicable" ) ;
244261 let action = match assist {
245262 Assist :: Unresolved { .. } => unreachable ! ( ) ,
246- Assist :: Resolved { action , .. } => action ,
263+ Assist :: Resolved { assist } => assist . get_first_action ( ) ,
247264 } ;
248265
249266 let range = action. target . expect ( "expected target on action" ) ;
@@ -293,10 +310,10 @@ mod tests {
293310 let mut assists = assists. iter ( ) ;
294311
295312 assert_eq ! (
296- assists. next( ) . expect( "expected assist" ) . 0 . label,
313+ assists. next( ) . expect( "expected assist" ) . label . label,
297314 "Change visibility to pub(crate)"
298315 ) ;
299- assert_eq ! ( assists. next( ) . expect( "expected assist" ) . 0 . label, "Add `#[derive]`" ) ;
316+ assert_eq ! ( assists. next( ) . expect( "expected assist" ) . label . label, "Add `#[derive]`" ) ;
300317 }
301318
302319 #[ test]
@@ -315,7 +332,7 @@ mod tests {
315332 let assists = super :: assists ( & db, frange) ;
316333 let mut assists = assists. iter ( ) ;
317334
318- assert_eq ! ( assists. next( ) . expect( "expected assist" ) . 0 . label, "Extract into variable" ) ;
319- assert_eq ! ( assists. next( ) . expect( "expected assist" ) . 0 . label, "Replace with match" ) ;
335+ assert_eq ! ( assists. next( ) . expect( "expected assist" ) . label . label, "Extract into variable" ) ;
336+ assert_eq ! ( assists. next( ) . expect( "expected assist" ) . label . label, "Replace with match" ) ;
320337 }
321338}
0 commit comments