@@ -754,13 +754,13 @@ func (e *Editor) Symbol(ctx context.Context, query string) ([]SymbolInformation,
754754
755755// OrganizeImports requests and performs the source.organizeImports codeAction.
756756func (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 )
758758 return err
759759}
760760
761761// RefactorRewrite requests and performs the source.refactorRewrite codeAction.
762762func (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 )
764764 if applied == 0 {
765765 return errors .Errorf ("no refactorings were applied" )
766766 }
@@ -769,19 +769,46 @@ func (e *Editor) RefactorRewrite(ctx context.Context, path string, rng *protocol
769769
770770// ApplyQuickFixes requests and performs the quickfix codeAction.
771771func (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 )
773773 if applied == 0 {
774774 return errors .Errorf ("no quick fixes were applied" )
775775 }
776776 return err
777777}
778778
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+
779806// GetQuickFixes returns the available quick fix code actions.
780807func (e * Editor ) GetQuickFixes (ctx context.Context , path string , rng * protocol.Range , diagnostics []protocol.Diagnostic ) ([]protocol.CodeAction , error ) {
781808 return e .getCodeActions (ctx , path , rng , diagnostics , protocol .QuickFix , protocol .SourceFixAll )
782809}
783810
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 ) {
785812 actions , err := e .getCodeActions (ctx , path , rng , diagnostics , only ... )
786813 if err != nil {
787814 return 0 , err
@@ -802,29 +829,7 @@ func (e *Editor) codeAction(ctx context.Context, path string, rng *protocol.Rang
802829 continue
803830 }
804831 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 {
828833 return 0 , err
829834 }
830835 }
0 commit comments