11use futures_util:: FutureExt ;
22use helix_lsp:: {
33 block_on,
4- lsp:: {
5- self , CodeAction , CodeActionOrCommand , CodeActionTriggerKind , DiagnosticSeverity ,
6- NumberOrString ,
7- } ,
8- util:: { diagnostic_to_lsp_diagnostic, lsp_range_to_range, range_to_lsp_range} ,
4+ lsp:: { self , CodeAction , CodeActionOrCommand , DiagnosticSeverity , NumberOrString } ,
5+ util:: lsp_range_to_range,
96 OffsetEncoding ,
107} ;
118use tui:: {
@@ -544,31 +541,9 @@ fn action_fixes_diagnostics(action: &CodeActionOrCommand) -> bool {
544541pub fn code_action ( cx : & mut Context ) {
545542 let ( view, doc) = current ! ( cx. editor) ;
546543
547- let language_server = language_server ! ( cx. editor, doc) ;
548-
549544 let selection_range = doc. selection ( view. id ) . primary ( ) ;
550- let offset_encoding = language_server. offset_encoding ( ) ;
551545
552- let range = range_to_lsp_range ( doc. text ( ) , selection_range, offset_encoding) ;
553-
554- let future = match language_server. code_actions (
555- doc. identifier ( ) ,
556- range,
557- // Filter and convert overlapping diagnostics
558- lsp:: CodeActionContext {
559- diagnostics : doc
560- . diagnostics ( )
561- . iter ( )
562- . filter ( |& diag| {
563- selection_range
564- . overlaps ( & helix_core:: Range :: new ( diag. range . start , diag. range . end ) )
565- } )
566- . map ( |diag| diagnostic_to_lsp_diagnostic ( doc. text ( ) , diag, offset_encoding) )
567- . collect ( ) ,
568- only : None ,
569- trigger_kind : Some ( CodeActionTriggerKind :: INVOKED ) ,
570- } ,
571- ) {
546+ let future = match doc. code_actions ( selection_range) {
572547 Some ( future) => future,
573548 None => {
574549 cx. editor
@@ -642,25 +617,7 @@ pub fn code_action(cx: &mut Context) {
642617 // always present here
643618 let code_action = code_action. unwrap ( ) ;
644619
645- match code_action {
646- lsp:: CodeActionOrCommand :: Command ( command) => {
647- log:: debug!( "code action command: {:?}" , command) ;
648- execute_lsp_command ( editor, command. clone ( ) ) ;
649- }
650- lsp:: CodeActionOrCommand :: CodeAction ( code_action) => {
651- log:: debug!( "code action: {:?}" , code_action) ;
652- if let Some ( ref workspace_edit) = code_action. edit {
653- log:: debug!( "edit: {:?}" , workspace_edit) ;
654- let _ = apply_workspace_edit ( editor, offset_encoding, workspace_edit) ;
655- }
656-
657- // if code action provides both edit and command first the edit
658- // should be applied and then the command
659- if let Some ( command) = & code_action. command {
660- execute_lsp_command ( editor, command. clone ( ) ) ;
661- }
662- }
663- }
620+ apply_code_action ( editor, code_action) ;
664621 } ) ;
665622 picker. move_down ( ) ; // pre-select the first item
666623
@@ -670,6 +627,34 @@ pub fn code_action(cx: &mut Context) {
670627 )
671628}
672629
630+ pub fn apply_code_action ( editor : & mut Editor , code_action : & CodeActionOrCommand ) {
631+ let ( _view, doc) = current ! ( editor) ;
632+
633+ let language_server = language_server ! ( editor, doc) ;
634+
635+ let offset_encoding = language_server. offset_encoding ( ) ;
636+
637+ match code_action {
638+ lsp:: CodeActionOrCommand :: Command ( command) => {
639+ log:: debug!( "code action command: {:?}" , command) ;
640+ execute_lsp_command ( editor, command. clone ( ) ) ;
641+ }
642+ lsp:: CodeActionOrCommand :: CodeAction ( code_action) => {
643+ log:: debug!( "code action: {:?}" , code_action) ;
644+ if let Some ( ref workspace_edit) = code_action. edit {
645+ log:: debug!( "edit: {:?}" , workspace_edit) ;
646+ let _ = apply_workspace_edit ( editor, offset_encoding, workspace_edit) ;
647+ }
648+
649+ // if code action provides both edit and command first the edit
650+ // should be applied and then the command
651+ if let Some ( command) = & code_action. command {
652+ execute_lsp_command ( editor, command. clone ( ) ) ;
653+ }
654+ }
655+ }
656+ }
657+
673658impl ui:: menu:: Item for lsp:: Command {
674659 type Data = ( ) ;
675660 fn format ( & self , _data : & Self :: Data ) -> Row {
0 commit comments