ludic fmt for editors: --lint --json, and a buffer on stdin (R9)

ludic-fmt --lint --json prints the violations --lint reports as one JSON array on stdout,
[{file, line, col, rule, message}] ordered by file, line and column (col where the rule knows it),
the summary on stderr, --lint's exit status, and never rewrites the baseline. ludic-fmt - refuses a
buffer that does not read as Ludic (a string/template/key literal left open, a bracket never closed
or closed by the wrong one) with exit 2 and name:line:col on stderr; - --lint judges a buffer as the
file --stdin-name names (--stdin-rel: that path relative to the project), against its baseline and
lint paths. ludic fmt --lint and ludic fmt - run it from the nearest package.ludic upwards (from
--stdin-name's directory when given). Hooks read nothing and write to stderr under --json or -.
Regression cases added to test-tools and ludic-dev test (fmt_editor_cases), not run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Orkun ÇAKILKAYA 2026-09-30 00:24:43 +03:00
parent c804c4b0ee
commit 047bdf4189
13 changed files with 405 additions and 17 deletions

View file

@ -32,6 +32,17 @@ function script_env(event: pointer, phase: pointer) -> pointer {
return env
}
# a command whose stdout is for a program (`--json`) or whose stdin is a buffer (`ludic fmt -`): its
# hooks read nothing and say everything on stderr, so neither is spoilt
function machine_output() -> bool {
var ai = 2
while ai < arg_count() {
if arg(ai) == "--json" or arg(ai) == "-" { return true }
ai += 1
}
return false
}
# run every `hook <phase> <event>` in order; the first failure's exit code, or 0
function run_hooks(phase: pointer, event: pointer) -> int {
if not file_exists("package.ludic") { return 0 }
@ -40,8 +51,14 @@ function run_hooks(phase: pointer, event: pointer) -> int {
while i + 2 < len(m.hooks) {
if m.hooks[i] == phase and m.hooks[i + 1] == event {
let cmd = m.hooks[i + 2]
say(`> {phase} {event}: {cmd}`)
let rc = sh(`{script_env(event, phase)}{cmd}`)
var rc = 0
if machine_output() {
err(`> {phase} {event}: {cmd}\n`)
rc = sh(`{script_env(event, phase)}( {cmd} ) < /dev/null 1>&2`)
} else {
say(`> {phase} {event}: {cmd}`)
rc = sh(`{script_env(event, phase)}{cmd}`)
}
if rc != 0 {
err(`ludic: the {phase} {event} hook failed (exit {string(rc)}): {cmd}\n`)
return rc