Add extension to format on edit

This commit is contained in:
Claudio Ortolina
2026-05-04 21:41:01 +01:00
parent dc3b31378d
commit 65366738a7
3 changed files with 58 additions and 0 deletions
+53
View File
@@ -0,0 +1,53 @@
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
import { resolve } from "node:path";
const ELIXIR_EXTENSIONS = [".ex", ".exs", ".heex"];
function isPiTsFile(path: string, cwd: string): boolean {
if (!path.endsWith(".ts")) return false;
const piDir = resolve(cwd, ".pi");
return resolve(cwd, path).startsWith(piDir);
}
async function formatElixir(
pi: ExtensionAPI,
path: string,
signal: AbortSignal | undefined,
) {
await pi.exec("mix", ["format", path], { signal, timeout: 10_000 });
}
async function formatTypeScript(
pi: ExtensionAPI,
path: string,
signal: AbortSignal | undefined,
) {
await pi.exec("prettier", ["--write", path], { signal, timeout: 10_000 });
}
export default function (pi: ExtensionAPI) {
pi.on("tool_result", async (event, ctx) => {
// Only hook into file-modifying tools, and only on success
if (event.toolName !== "edit" && event.toolName !== "write") return;
if (event.isError) return;
// Get the file path from the tool input
const path = event.input?.path;
if (typeof path !== "string") return;
// Determine formatter based on file type
if (ELIXIR_EXTENSIONS.some((ext) => path.endsWith(ext))) {
try {
await formatElixir(pi, path, ctx.signal);
} catch {
/* ignore */
}
} else if (isPiTsFile(path, ctx.cwd)) {
try {
await formatTypeScript(pi, path, ctx.signal);
} catch {
/* ignore */
}
}
});
}
+4
View File
@@ -167,6 +167,10 @@ url = "https://nodejs.org/dist/v25.9.0/node-v25.9.0-win-x64.zip"
version = "1.45.0" version = "1.45.0"
backend = "npm:backlog.md" backend = "npm:backlog.md"
[[tools."npm:prettier"]]
version = "3.8.3"
backend = "npm:prettier"
[[tools.shellcheck]] [[tools.shellcheck]]
version = "0.11.0" version = "0.11.0"
backend = "aqua:koalaman/shellcheck" backend = "aqua:koalaman/shellcheck"
+1
View File
@@ -31,6 +31,7 @@ shellcheck = 'latest'
fd = 'latest' fd = 'latest'
litestream = 'latest' litestream = 'latest'
"npm:backlog.md" = 'latest' "npm:backlog.md" = 'latest'
"npm:prettier" = 'latest'
[task_config] [task_config]
includes = ["scripts"] includes = ["scripts"]