Skip to content

Commit f748550

Browse files
authored
feat(acton): save files on quick fix application (#303)
1 parent c25bcc1 commit f748550

2 files changed

Lines changed: 58 additions & 1 deletion

File tree

editors/code/src/acton/ActonLinter.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {consoleError} from "../client-log"
1010
import {Acton} from "./Acton"
1111
import {CheckCommand} from "./ActonCommand"
1212

13+
const APPLY_FIX_AND_SAVE_COMMAND = "ton.acton.applyFixAndSave"
14+
1315
interface 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

server/src/languages/tolk/psi/TolkFile.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ export class TolkFile extends File {
4949
if (!actonToml) return false
5050

5151
const relative = path.relative(actonToml.workingDir, this.path).replace(/\\/g, "/")
52-
return relative === "contracts" || relative.startsWith("contracts/")
52+
return (
53+
(relative === "contracts" || relative.startsWith("contracts/")) &&
54+
!relative.includes("/scripts")
55+
)
5356
}
5457

5558
public shouldHideActonImportCompletion(candidatePath: string): boolean {

0 commit comments

Comments
 (0)