@@ -865,10 +865,52 @@ pub(crate) fn handle_formatting(
865865 }
866866}
867867
868- fn handle_fixes (
868+ pub ( crate ) fn handle_code_action (
869+ mut snap : GlobalStateSnapshot ,
870+ params : lsp_types:: CodeActionParams ,
871+ ) -> Result < Option < Vec < lsp_ext:: CodeAction > > > {
872+ let _p = profile:: span ( "handle_code_action" ) ;
873+ // We intentionally don't support command-based actions, as those either
874+ // requires custom client-code anyway, or requires server-initiated edits.
875+ // Server initiated edits break causality, so we avoid those as well.
876+ if !snap. config . client_caps . code_action_literals {
877+ return Ok ( None ) ;
878+ }
879+
880+ let file_id = from_proto:: file_id ( & snap, & params. text_document . uri ) ?;
881+ let line_index = snap. analysis . file_line_index ( file_id) ?;
882+ let range = from_proto:: text_range ( & line_index, params. range ) ;
883+ let frange = FileRange { file_id, range } ;
884+
885+ snap. config . assist . allowed = params
886+ . clone ( )
887+ . context
888+ . only
889+ . map ( |it| it. into_iter ( ) . filter_map ( from_proto:: assist_kind) . collect ( ) ) ;
890+
891+ let mut res: Vec < lsp_ext:: CodeAction > = Vec :: new ( ) ;
892+
893+ add_quick_fixes ( & snap, & params, & mut res) ?;
894+
895+ if snap. config . client_caps . code_action_resolve {
896+ for ( index, assist) in
897+ snap. analysis . unresolved_assists ( & snap. config . assist , frange) ?. into_iter ( ) . enumerate ( )
898+ {
899+ res. push ( to_proto:: unresolved_code_action ( & snap, params. clone ( ) , assist, index) ?) ;
900+ }
901+ } else {
902+ for assist in snap. analysis . resolved_assists ( & snap. config . assist , frange) ?. into_iter ( ) {
903+ res. push ( to_proto:: resolved_code_action ( & snap, assist) ?) ;
904+ }
905+ }
906+
907+ Ok ( Some ( res) )
908+ }
909+
910+ fn add_quick_fixes (
869911 snap : & GlobalStateSnapshot ,
870912 params : & lsp_types:: CodeActionParams ,
871- res : & mut Vec < lsp_ext:: CodeAction > ,
913+ acc : & mut Vec < lsp_ext:: CodeAction > ,
872914) -> Result < ( ) > {
873915 let file_id = from_proto:: file_id ( & snap, & params. text_document . uri ) ?;
874916 let line_index = snap. analysis . file_line_index ( file_id) ?;
@@ -902,61 +944,19 @@ fn handle_fixes(
902944 is_preferred : Some ( false ) ,
903945 data : None ,
904946 } ;
905- res . push ( action) ;
947+ acc . push ( action) ;
906948 }
907949
908950 for fix in snap. check_fixes . get ( & file_id) . into_iter ( ) . flatten ( ) {
909951 let fix_range = from_proto:: text_range ( & line_index, fix. range ) ;
910952 if fix_range. intersect ( range) . is_none ( ) {
911953 continue ;
912954 }
913- res . push ( fix. action . clone ( ) ) ;
955+ acc . push ( fix. action . clone ( ) ) ;
914956 }
915957 Ok ( ( ) )
916958}
917959
918- pub ( crate ) fn handle_code_action (
919- mut snap : GlobalStateSnapshot ,
920- params : lsp_types:: CodeActionParams ,
921- ) -> Result < Option < Vec < lsp_ext:: CodeAction > > > {
922- let _p = profile:: span ( "handle_code_action" ) ;
923- // We intentionally don't support command-based actions, as those either
924- // requires custom client-code anyway, or requires server-initiated edits.
925- // Server initiated edits break causality, so we avoid those as well.
926- if !snap. config . client_caps . code_action_literals {
927- return Ok ( None ) ;
928- }
929-
930- let file_id = from_proto:: file_id ( & snap, & params. text_document . uri ) ?;
931- let line_index = snap. analysis . file_line_index ( file_id) ?;
932- let range = from_proto:: text_range ( & line_index, params. range ) ;
933- let frange = FileRange { file_id, range } ;
934-
935- snap. config . assist . allowed = params
936- . clone ( )
937- . context
938- . only
939- . map ( |it| it. into_iter ( ) . filter_map ( from_proto:: assist_kind) . collect ( ) ) ;
940-
941- let mut res: Vec < lsp_ext:: CodeAction > = Vec :: new ( ) ;
942-
943- handle_fixes ( & snap, & params, & mut res) ?;
944-
945- if snap. config . client_caps . code_action_resolve {
946- for ( index, assist) in
947- snap. analysis . unresolved_assists ( & snap. config . assist , frange) ?. into_iter ( ) . enumerate ( )
948- {
949- res. push ( to_proto:: unresolved_code_action ( & snap, params. clone ( ) , assist, index) ?) ;
950- }
951- } else {
952- for assist in snap. analysis . resolved_assists ( & snap. config . assist , frange) ?. into_iter ( ) {
953- res. push ( to_proto:: resolved_code_action ( & snap, assist) ?) ;
954- }
955- }
956-
957- Ok ( Some ( res) )
958- }
959-
960960pub ( crate ) fn handle_code_action_resolve (
961961 mut snap : GlobalStateSnapshot ,
962962 mut code_action : lsp_ext:: CodeAction ,
0 commit comments