@@ -10,6 +10,8 @@ import {consoleError} from "../client-log"
1010import { Acton } from "./Acton"
1111import { CheckCommand } from "./ActonCommand"
1212
13+ const APPLY_FIX_AND_SAVE_COMMAND = "ton.acton.applyFixAndSave"
14+
1315interface ActonRange {
1416 readonly start : { readonly line : number ; readonly character : number }
1517 readonly end : { readonly line : number ; readonly character : number }
@@ -63,6 +65,12 @@ export class ActonLinter implements vscode.CodeActionProvider {
6365 this . diagnosticCollection = vscode . languages . createDiagnosticCollection ( "acton" )
6466
6567 this . disposables . push (
68+ vscode . commands . registerCommand (
69+ APPLY_FIX_AND_SAVE_COMMAND ,
70+ async ( filePaths : readonly string [ ] | undefined ) => {
71+ await this . saveEditedFiles ( filePaths )
72+ } ,
73+ ) ,
6674 vscode . languages . registerCodeActionsProvider ( "tolk" , this , {
6775 providedCodeActionKinds : [ vscode . CodeActionKind . QuickFix ] ,
6876 } ) ,
@@ -327,6 +335,13 @@ export class ActonLinter implements vscode.CodeActionProvider {
327335 action . isPreferred = true
328336 }
329337
338+ const editedFiles = [ ...new Set ( fix . edits . map ( edit => edit . file ) ) ]
339+ action . command = {
340+ title : "Save Acton quick fix files" ,
341+ command : APPLY_FIX_AND_SAVE_COMMAND ,
342+ arguments : [ editedFiles ] ,
343+ }
344+
330345 actions . push ( action )
331346 }
332347 }
@@ -335,6 +350,45 @@ export class ActonLinter implements vscode.CodeActionProvider {
335350 return actions
336351 }
337352
353+ private async saveEditedFiles ( filePaths : readonly string [ ] | undefined ) : Promise < void > {
354+ if ( ! filePaths || filePaths . length === 0 ) {
355+ return
356+ }
357+
358+ const editedUris = new Set ( filePaths . map ( filePath => vscode . Uri . file ( filePath ) . toString ( ) ) )
359+
360+ for ( const filePath of filePaths ) {
361+ const uri = vscode . Uri . file ( filePath )
362+
363+ try {
364+ const document =
365+ vscode . workspace . textDocuments . find (
366+ doc => doc . uri . toString ( ) === uri . toString ( ) ,
367+ ) ?? ( await vscode . workspace . openTextDocument ( uri ) )
368+
369+ if ( ! document . isDirty ) {
370+ continue
371+ }
372+
373+ const saved = await document . save ( )
374+ if ( ! saved ) {
375+ consoleError ( "Failed to save Acton quick fix file" , filePath )
376+ }
377+ } catch ( error ) {
378+ consoleError ( "Failed to save Acton quick fix file" , filePath , error )
379+ }
380+ }
381+
382+ const activeDocument = vscode . window . activeTextEditor ?. document
383+ if (
384+ activeDocument &&
385+ editedUris . has ( activeDocument . uri . toString ( ) ) &&
386+ ! activeDocument . isDirty
387+ ) {
388+ this . triggerCheck ( activeDocument )
389+ }
390+ }
391+
338392 private processDiagnostics ( output : ActonCheckOutput ) : void {
339393 const diagnosticsByFile : Map < string , vscode . Diagnostic [ ] > = new Map ( )
340394
0 commit comments