Allow @def body to start on the next continuation line

Previously, a backslash immediately after the macro name (with no
body on the first line) produced a leading newline. Now an empty
prefix is handled so the body begins cleanly on the continuation line.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Chris Wanstrath 2026-03-19 13:50:01 -07:00
parent 9943641c02
commit 724f40c25d
4 changed files with 12 additions and 3 deletions

View File

@ -110,7 +110,7 @@ $ greet
hello world hello world
``` ```
Use backslash `\` for multi-line bodies: Use backslash `\` for multi-line bodies. The body can start on the same line or on the next continuation line:
``` ```
@def serve \ @def serve \

View File

@ -204,6 +204,14 @@ describe("parse", () => {
expect(result.commands).toHaveLength(1) expect(result.commands).toHaveLength(1)
}) })
test("@def with body starting on continuation line", () => {
const content = "@def serve \\\n python3 -m http.server\n$ serve\n"
const result = parse("test.shout", content)
expect(result.directives).toEqual([
{ type: "def", name: "serve", body: "python3 -m http.server", line: 1 },
])
})
test("@def multiple macros", () => { test("@def multiple macros", () => {
const content = "@def foo echo foo\n@def bar echo bar\n$ foo\nfoo\n" const content = "@def foo echo foo\n@def bar echo bar\n$ foo\nfoo\n"
const result = parse("test.shout", content) const result = parse("test.shout", content)

View File

@ -91,7 +91,8 @@ function parseDefDirective(
if (next.startsWith("$") || next.startsWith("@") || next.trim() === "" || next.startsWith("#")) { if (next.startsWith("$") || next.startsWith("@") || next.trim() === "" || next.startsWith("#")) {
throw new Error(`${path}:${i + extra + 2}: @def continuation consumed a command or directive line`) throw new Error(`${path}:${i + extra + 2}: @def continuation consumed a command or directive line`)
} }
body = body.slice(0, -1).trimEnd() + "\n" + next.trim() const prefix = body.slice(0, -1).trimEnd()
body = prefix ? prefix + "\n" + next.trim() : next.trim()
extra++ extra++
} }

View File

@ -225,7 +225,7 @@
<span class="prompt">$</span> <span class="cmd">greet</span> <span class="prompt">$</span> <span class="cmd">greet</span>
<span class="output">hello world</span></code></pre> <span class="output">hello world</span></code></pre>
<p>If a command matches a macro name exactly, the body is substituted. Use <code>\</code> for multi-line bodies. Macros from setup files are inherited; user-file macros override them.</p> <p>If a command matches a macro name exactly, the body is substituted. Use <code>\</code> for multi-line bodies — the body can start on the same line or on the next continuation line. Macros from setup files are inherited; user-file macros override them.</p>
</section> </section>
<section> <section>