Skip to content

Commit 1e94930

Browse files
Pad code that holds a backtick or ends with a space, so render keeps it exact
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 58fd8d2 commit 1e94930

2 files changed

Lines changed: 8 additions & 8 deletions

File tree

‎liquidjava-verifier/src/main/java/liquidjava/diagnostics/Code.java‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ private Code() {
1616
}
1717

1818
/**
19-
* {@code code} marked as code, fenced by more backticks than any run it holds, padded if it starts or ends with one
19+
* {@code code} marked as code, fenced by more backticks than any run it holds, and padded with a space on each side
20+
* if it holds a backtick or starts or ends with a space, so {@link #render} can tell the padding from the code
2021
*/
2122
public static String of(String code) {
2223
String fence = "`";
2324
while (code.contains(fence))
2425
fence += "`";
25-
String pad = code.startsWith("`") || code.endsWith("`") ? " " : "";
26+
String pad = code.contains("`") || code.startsWith(" ") || code.endsWith(" ") ? " " : "";
2627
return fence + pad + code + pad + fence;
2728
}
2829

@@ -38,11 +39,9 @@ static String render(String message, boolean colour) {
3839
StringBuilder rendered = new StringBuilder();
3940
while (span.find()) {
4041
String code = span.group(2);
41-
// the padding of() adds around code that starts or ends with a backtick, not spaces of the code
42-
String inner = code.length() > 2 && code.startsWith(" ") && code.endsWith(" ")
43-
? code.substring(1, code.length() - 1) : code;
44-
if (inner.startsWith("`") || inner.endsWith("`"))
45-
code = inner;
42+
// the padding of() adds: code with a space at both ends is always padded
43+
if (code.length() >= 2 && code.startsWith(" ") && code.endsWith(" "))
44+
code = code.substring(1, code.length() - 1);
4645
span.appendReplacement(rendered, Matcher.quoteReplacement(Colors.BLUE + code + Colors.RESET));
4746
}
4847
span.appendTail(rendered);

‎liquidjava-verifier/src/test/java/liquidjava/diagnostics/CodeTest.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class CodeTest {
99
@Test
1010
void codeHoldingABacktickGetsALongerFence() {
1111
assertEquals("`x > 0`", Code.of("x > 0"));
12-
assertEquals("``c == '`'``", Code.of("c == '`'"));
12+
assertEquals("`` c == '`' ``", Code.of("c == '`'"));
1313
assertEquals("`` `a ``", Code.of("`a"));
1414
}
1515

@@ -23,5 +23,6 @@ void terminalShowsCodeInColourWithoutItsBackticks() {
2323
// spaces and line breaks of the code itself are kept
2424
assertEquals(Colors.BLUE + " a " + Colors.RESET, Code.render(Code.of(" a "), true));
2525
assertEquals(Colors.BLUE + "a\nb" + Colors.RESET, Code.render(Code.of("a\nb"), true));
26+
assertEquals(Colors.BLUE + " ` " + Colors.RESET, Code.render(Code.of(" ` "), true));
2627
}
2728
}

0 commit comments

Comments
 (0)