|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +// Copyright © 2026 TON Core |
| 3 | + |
| 4 | +import * as fs from "node:fs/promises" |
| 5 | +import * as os from "node:os" |
| 6 | +import * as path from "node:path" |
| 7 | + |
| 8 | +import * as vscode from "vscode" |
| 9 | + |
| 10 | +import {consoleError} from "../client-log" |
| 11 | + |
| 12 | +import {Acton} from "./Acton" |
| 13 | +import {FormatCommand} from "./ActonCommand" |
| 14 | + |
| 15 | +function isActonUnavailableError(error: unknown): error is NodeJS.ErrnoException { |
| 16 | + return error instanceof Error && "code" in error && error.code === "ENOENT" |
| 17 | +} |
| 18 | + |
| 19 | +function fullDocumentRange(document: vscode.TextDocument): vscode.Range { |
| 20 | + const lastLine = document.lineCount - 1 |
| 21 | + const end = document.lineAt(lastLine).range.end |
| 22 | + return new vscode.Range(new vscode.Position(0, 0), end) |
| 23 | +} |
| 24 | + |
| 25 | +async function resolveWorkingDirectory(document: vscode.TextDocument): Promise<string | undefined> { |
| 26 | + if (document.uri.scheme === "file") { |
| 27 | + const actonToml = await Acton.getInstance().findActonToml(document.uri) |
| 28 | + if (actonToml) { |
| 29 | + return path.dirname(actonToml.fsPath) |
| 30 | + } |
| 31 | + |
| 32 | + const workspaceFolder = vscode.workspace.getWorkspaceFolder(document.uri) |
| 33 | + if (workspaceFolder) { |
| 34 | + return workspaceFolder.uri.fsPath |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + return vscode.workspace.workspaceFolders?.[0]?.uri.fsPath |
| 39 | +} |
| 40 | + |
| 41 | +export async function formatTolkDocumentWithActon( |
| 42 | + document: vscode.TextDocument, |
| 43 | +): Promise<vscode.TextEdit[] | null> { |
| 44 | + const formatterEnabled = vscode.workspace |
| 45 | + .getConfiguration("ton", document.uri) |
| 46 | + .get<boolean>("tolk.formatter.useFormatter", true) |
| 47 | + if (!formatterEnabled) { |
| 48 | + return [] |
| 49 | + } |
| 50 | + |
| 51 | + const source = document.getText() |
| 52 | + const workingDirectory = await resolveWorkingDirectory(document) |
| 53 | + const tempDirectory = await fs.mkdtemp(path.join(os.tmpdir(), "acton-fmt-")) |
| 54 | + |
| 55 | + try { |
| 56 | + const originalFileName = |
| 57 | + document.uri.scheme === "file" ? path.basename(document.uri.fsPath) : "untitled.tolk" |
| 58 | + const tempFilePath = path.join(tempDirectory, originalFileName) |
| 59 | + await fs.writeFile(tempFilePath, source, "utf8") |
| 60 | + |
| 61 | + const command = new FormatCommand([tempFilePath]) |
| 62 | + const {exitCode, stderr, stdout} = await Acton.getInstance().spawn( |
| 63 | + command, |
| 64 | + workingDirectory, |
| 65 | + ) |
| 66 | + |
| 67 | + if (exitCode !== 0) { |
| 68 | + const details = (stderr.trim() || stdout.trim() || `exit code ${exitCode}`).split( |
| 69 | + "\n", |
| 70 | + )[0] |
| 71 | + void vscode.window.showErrorMessage(`Failed to format with acton fmt: ${details}`) |
| 72 | + return [] |
| 73 | + } |
| 74 | + |
| 75 | + const formatted = await fs.readFile(tempFilePath, "utf8") |
| 76 | + if (formatted === source) { |
| 77 | + return [] |
| 78 | + } |
| 79 | + |
| 80 | + return [vscode.TextEdit.replace(fullDocumentRange(document), formatted)] |
| 81 | + } catch (error) { |
| 82 | + if (isActonUnavailableError(error)) { |
| 83 | + return null |
| 84 | + } |
| 85 | + |
| 86 | + consoleError("Failed to format with acton fmt", error) |
| 87 | + void vscode.window.showErrorMessage("Failed to format with acton fmt") |
| 88 | + return [] |
| 89 | + } finally { |
| 90 | + await fs.rm(tempDirectory, {recursive: true, force: true}).catch(() => { |
| 91 | + // ignore cleanup errors for temporary files |
| 92 | + }) |
| 93 | + } |
| 94 | +} |
0 commit comments