@@ -754,13 +754,13 @@ func (e *Editor) Symbol(ctx context.Context, query string) ([]SymbolInformation,
754
754
755
755
// OrganizeImports requests and performs the source.organizeImports codeAction.
756
756
func (e * Editor ) OrganizeImports (ctx context.Context , path string ) error {
757
- _ , err := e .codeAction (ctx , path , nil , nil , protocol .SourceOrganizeImports )
757
+ _ , err := e .applyCodeActions (ctx , path , nil , nil , protocol .SourceOrganizeImports )
758
758
return err
759
759
}
760
760
761
761
// RefactorRewrite requests and performs the source.refactorRewrite codeAction.
762
762
func (e * Editor ) RefactorRewrite (ctx context.Context , path string , rng * protocol.Range ) error {
763
- applied , err := e .codeAction (ctx , path , rng , nil , protocol .RefactorRewrite )
763
+ applied , err := e .applyCodeActions (ctx , path , rng , nil , protocol .RefactorRewrite )
764
764
if applied == 0 {
765
765
return errors .Errorf ("no refactorings were applied" )
766
766
}
@@ -769,19 +769,46 @@ func (e *Editor) RefactorRewrite(ctx context.Context, path string, rng *protocol
769
769
770
770
// ApplyQuickFixes requests and performs the quickfix codeAction.
771
771
func (e * Editor ) ApplyQuickFixes (ctx context.Context , path string , rng * protocol.Range , diagnostics []protocol.Diagnostic ) error {
772
- applied , err := e .codeAction (ctx , path , rng , diagnostics , protocol .QuickFix , protocol .SourceFixAll )
772
+ applied , err := e .applyCodeActions (ctx , path , rng , diagnostics , protocol .SourceFixAll , protocol .QuickFix )
773
773
if applied == 0 {
774
774
return errors .Errorf ("no quick fixes were applied" )
775
775
}
776
776
return err
777
777
}
778
778
779
+ // ApplyCodeAction applies the given code action.
780
+ func (e * Editor ) ApplyCodeAction (ctx context.Context , action protocol.CodeAction ) error {
781
+ for _ , change := range action .Edit .DocumentChanges {
782
+ path := e .sandbox .Workdir .URIToPath (change .TextDocument .URI )
783
+ if int32 (e .buffers [path ].version ) != change .TextDocument .Version {
784
+ // Skip edits for old versions.
785
+ continue
786
+ }
787
+ edits := convertEdits (change .Edits )
788
+ if err := e .EditBuffer (ctx , path , edits ); err != nil {
789
+ return errors .Errorf ("editing buffer %q: %w" , path , err )
790
+ }
791
+ }
792
+ // Execute any commands. The specification says that commands are
793
+ // executed after edits are applied.
794
+ if action .Command != nil {
795
+ if _ , err := e .ExecuteCommand (ctx , & protocol.ExecuteCommandParams {
796
+ Command : action .Command .Command ,
797
+ Arguments : action .Command .Arguments ,
798
+ }); err != nil {
799
+ return err
800
+ }
801
+ }
802
+ // Some commands may edit files on disk.
803
+ return e .sandbox .Workdir .CheckForFileChanges (ctx )
804
+ }
805
+
779
806
// GetQuickFixes returns the available quick fix code actions.
780
807
func (e * Editor ) GetQuickFixes (ctx context.Context , path string , rng * protocol.Range , diagnostics []protocol.Diagnostic ) ([]protocol.CodeAction , error ) {
781
808
return e .getCodeActions (ctx , path , rng , diagnostics , protocol .QuickFix , protocol .SourceFixAll )
782
809
}
783
810
784
- func (e * Editor ) codeAction (ctx context.Context , path string , rng * protocol.Range , diagnostics []protocol.Diagnostic , only ... protocol.CodeActionKind ) (int , error ) {
811
+ func (e * Editor ) applyCodeActions (ctx context.Context , path string , rng * protocol.Range , diagnostics []protocol.Diagnostic , only ... protocol.CodeActionKind ) (int , error ) {
785
812
actions , err := e .getCodeActions (ctx , path , rng , diagnostics , only ... )
786
813
if err != nil {
787
814
return 0 , err
@@ -802,29 +829,7 @@ func (e *Editor) codeAction(ctx context.Context, path string, rng *protocol.Rang
802
829
continue
803
830
}
804
831
applied ++
805
- for _ , change := range action .Edit .DocumentChanges {
806
- path := e .sandbox .Workdir .URIToPath (change .TextDocument .URI )
807
- if int32 (e .buffers [path ].version ) != change .TextDocument .Version {
808
- // Skip edits for old versions.
809
- continue
810
- }
811
- edits := convertEdits (change .Edits )
812
- if err := e .EditBuffer (ctx , path , edits ); err != nil {
813
- return 0 , errors .Errorf ("editing buffer %q: %w" , path , err )
814
- }
815
- }
816
- // Execute any commands. The specification says that commands are
817
- // executed after edits are applied.
818
- if action .Command != nil {
819
- if _ , err := e .ExecuteCommand (ctx , & protocol.ExecuteCommandParams {
820
- Command : action .Command .Command ,
821
- Arguments : action .Command .Arguments ,
822
- }); err != nil {
823
- return 0 , err
824
- }
825
- }
826
- // Some commands may edit files on disk.
827
- if err := e .sandbox .Workdir .CheckForFileChanges (ctx ); err != nil {
832
+ if err := e .ApplyCodeAction (ctx , action ); err != nil {
828
833
return 0 , err
829
834
}
830
835
}
0 commit comments