Skip to content

Commit bc22102

Browse files
authored
caddyfile: Fix caddy fmt nesting not decrementing (#4157)
* caddyfile(formatter): fix nesting not decrementing This is an extremely weird edge-case where if you had a environment variable {} on one line, a comment on the next line, and the closing of the block on the following line; the rest of the Caddyfile would be indented further than it should've been. ref; caddyserver/vscode-caddyfile#13 * run gofmt * fmt: better way of handling edge case
1 parent f5db41c commit bc22102

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

caddyconfig/caddyfile/formatter.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ func Format(input []byte) []byte {
7878
if comment {
7979
if ch == '\n' {
8080
comment = false
81+
space = true
8182
nextLine()
8283
continue
8384
} else {

caddyconfig/caddyfile/formatter_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,44 @@ baz`,
321321
foo
322322
}`,
323323
},
324+
{
325+
description: "matthewpi/vscode-caddyfile-support#13",
326+
input: `{
327+
email {$ACMEEMAIL}
328+
#debug
329+
}
330+
331+
block {
332+
}
333+
`,
334+
expect: `{
335+
email {$ACMEEMAIL}
336+
#debug
337+
}
338+
339+
block {
340+
}
341+
`,
342+
},
343+
{
344+
description: "matthewpi/vscode-caddyfile-support#13 - bad formatting",
345+
input: `{
346+
email {$ACMEEMAIL}
347+
#debug
348+
}
349+
350+
block {
351+
}
352+
`,
353+
expect: `{
354+
email {$ACMEEMAIL}
355+
#debug
356+
}
357+
358+
block {
359+
}
360+
`,
361+
},
324362
} {
325363
// the formatter should output a trailing newline,
326364
// even if the tests aren't written to expect that

modules/caddyhttp/replacer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ eqp31wM9il1n+guTNyxJd+FzVAH+hCZE5K+tCgVDdVFUlDEHHbS/wqb2PSIoouLV
173173
},
174174
{
175175
input: "{http.request.tls.client.certificate_pem}",
176-
expect: string(clientCert) + "\n", // returned value comes with a newline appended to it
176+
expect: string(clientCert) + "\n", // returned value comes with a newline appended to it
177177
},
178178
} {
179179
actual := repl.ReplaceAll(tc.input, "<empty>")

0 commit comments

Comments
 (0)