@@ -11,12 +11,11 @@ use lsp_server::ErrorCode;
11
11
use lsp_types:: {
12
12
CallHierarchyIncomingCall , CallHierarchyIncomingCallsParams , CallHierarchyItem ,
13
13
CallHierarchyOutgoingCall , CallHierarchyOutgoingCallsParams , CallHierarchyPrepareParams ,
14
- CodeAction , CodeActionResponse , CodeLens , Command , CompletionItem , Diagnostic ,
15
- DocumentFormattingParams , DocumentHighlight , DocumentSymbol , FoldingRange , FoldingRangeParams ,
16
- Hover , HoverContents , Location , MarkupContent , MarkupKind , Position , PrepareRenameResponse ,
17
- Range , RenameParams , SemanticTokensParams , SemanticTokensRangeParams ,
18
- SemanticTokensRangeResult , SemanticTokensResult , SymbolInformation , TextDocumentIdentifier ,
19
- TextEdit , Url , WorkspaceEdit ,
14
+ CodeLens , Command , CompletionItem , Diagnostic , DocumentFormattingParams , DocumentHighlight ,
15
+ DocumentSymbol , FoldingRange , FoldingRangeParams , Hover , HoverContents , Location ,
16
+ MarkupContent , MarkupKind , Position , PrepareRenameResponse , Range , RenameParams ,
17
+ SemanticTokensParams , SemanticTokensRangeParams , SemanticTokensRangeResult ,
18
+ SemanticTokensResult , SymbolInformation , TextDocumentIdentifier , TextEdit , Url , WorkspaceEdit ,
20
19
} ;
21
20
use ra_ide:: {
22
21
Assist , FileId , FilePosition , FileRange , Query , RangeInfo , Runnable , RunnableKind , SearchScope ,
@@ -585,9 +584,8 @@ pub fn handle_rename(world: WorldSnapshot, params: RenameParams) -> Result<Optio
585
584
None => return Ok ( None ) ,
586
585
Some ( it) => it. info ,
587
586
} ;
588
-
589
- let source_change = to_proto:: source_change ( & world, source_change) ?;
590
- Ok ( Some ( source_change. workspace_edit ) )
587
+ let workspace_edit = to_proto:: workspace_edit ( & world, source_change) ?;
588
+ Ok ( Some ( workspace_edit) )
591
589
}
592
590
593
591
pub fn handle_references (
@@ -696,14 +694,21 @@ pub fn handle_formatting(
696
694
pub fn handle_code_action (
697
695
world : WorldSnapshot ,
698
696
params : lsp_types:: CodeActionParams ,
699
- ) -> Result < Option < CodeActionResponse > > {
697
+ ) -> Result < Option < Vec < lsp_ext :: CodeAction > > > {
700
698
let _p = profile ( "handle_code_action" ) ;
699
+ // We intentionally don't support command-based actions, as those either
700
+ // requires custom client-code anyway, or requires server-initiated edits.
701
+ // Server initiated edits break causality, so we avoid those as well.
702
+ if !world. config . client_caps . code_action_literals {
703
+ return Ok ( None ) ;
704
+ }
705
+
701
706
let file_id = from_proto:: file_id ( & world, & params. text_document . uri ) ?;
702
707
let line_index = world. analysis ( ) . file_line_index ( file_id) ?;
703
708
let range = from_proto:: text_range ( & line_index, params. range ) ;
704
709
705
710
let diagnostics = world. analysis ( ) . diagnostics ( file_id) ?;
706
- let mut res = CodeActionResponse :: default ( ) ;
711
+ let mut res: Vec < lsp_ext :: CodeAction > = Vec :: new ( ) ;
707
712
708
713
let fixes_from_diagnostics = diagnostics
709
714
. into_iter ( )
@@ -713,22 +718,9 @@ pub fn handle_code_action(
713
718
714
719
for source_edit in fixes_from_diagnostics {
715
720
let title = source_edit. label . clone ( ) ;
716
- let edit = to_proto:: source_change ( & world, source_edit) ?;
717
-
718
- let command = Command {
719
- title,
720
- command : "rust-analyzer.applySourceChange" . to_string ( ) ,
721
- arguments : Some ( vec ! [ to_value( edit) . unwrap( ) ] ) ,
722
- } ;
723
- let action = CodeAction {
724
- title : command. title . clone ( ) ,
725
- kind : None ,
726
- diagnostics : None ,
727
- edit : None ,
728
- command : Some ( command) ,
729
- is_preferred : None ,
730
- } ;
731
- res. push ( action. into ( ) ) ;
721
+ let edit = to_proto:: snippet_workspace_edit ( & world, source_edit) ?;
722
+ let action = lsp_ext:: CodeAction { title, kind : None , edit : Some ( edit) , command : None } ;
723
+ res. push ( action) ;
732
724
}
733
725
734
726
for fix in world. check_fixes . get ( & file_id) . into_iter ( ) . flatten ( ) {
@@ -748,8 +740,13 @@ pub fn handle_code_action(
748
740
. entry ( label. to_owned ( ) )
749
741
. or_insert_with ( || {
750
742
let idx = res. len ( ) ;
751
- let dummy = Command :: new ( String :: new ( ) , String :: new ( ) , None ) ;
752
- res. push ( dummy. into ( ) ) ;
743
+ let dummy = lsp_ext:: CodeAction {
744
+ title : String :: new ( ) ,
745
+ kind : None ,
746
+ command : None ,
747
+ edit : None ,
748
+ } ;
749
+ res. push ( dummy) ;
753
750
( idx, Vec :: new ( ) )
754
751
} )
755
752
. 1
@@ -777,35 +774,10 @@ pub fn handle_code_action(
777
774
command : "rust-analyzer.selectAndApplySourceChange" . to_string ( ) ,
778
775
arguments : Some ( vec ! [ serde_json:: Value :: Array ( arguments) ] ) ,
779
776
} ) ;
780
- res[ idx] = CodeAction {
781
- title,
782
- kind : None ,
783
- diagnostics : None ,
784
- edit : None ,
785
- command,
786
- is_preferred : None ,
787
- }
788
- . into ( ) ;
777
+ res[ idx] = lsp_ext:: CodeAction { title, kind : None , edit : None , command } ;
789
778
}
790
779
}
791
780
792
- // If the client only supports commands then filter the list
793
- // and remove and actions that depend on edits.
794
- if !world. config . client_caps . code_action_literals {
795
- // FIXME: use drain_filter once it hits stable.
796
- res = res
797
- . into_iter ( )
798
- . filter_map ( |it| match it {
799
- cmd @ lsp_types:: CodeActionOrCommand :: Command ( _) => Some ( cmd) ,
800
- lsp_types:: CodeActionOrCommand :: CodeAction ( action) => match action. command {
801
- Some ( cmd) if action. edit . is_none ( ) => {
802
- Some ( lsp_types:: CodeActionOrCommand :: Command ( cmd) )
803
- }
804
- _ => None ,
805
- } ,
806
- } )
807
- . collect ( ) ;
808
- }
809
781
Ok ( Some ( res) )
810
782
}
811
783
0 commit comments