Skip to content

Commit 0fb95b5

Browse files
committed
fmt: respect single empty lines
Allow users to place empty lines between sibling elements. Multiple empty lines in a row will be collapsed to a single empty line. This allows for visual separation of content "blocks" while still offering a normalized render of the document.
1 parent d07ca9b commit 0fb95b5

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

src/html/Ast.zig

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,14 @@ pub fn render(ast: Ast, src: []const u8, w: *Writer) !void {
744744

745745
if (vertical) {
746746
log.debug("adding a newline", .{});
747-
try w.writeAll("\n");
747+
const lines = std.mem.count(u8, maybe_ws, "\n");
748+
749+
if (lines >= 2) {
750+
try w.writeAll("\n\n");
751+
} else {
752+
try w.writeAll("\n");
753+
}
754+
748755
for (0..indentation) |_| {
749756
try w.writeAll(" ");
750757
}
@@ -1296,6 +1303,7 @@ test "what" {
12961303
\\ </a>
12971304
\\ </body>
12981305
\\</html>
1306+
\\
12991307
\\<a href="#">foo</a>
13001308
\\
13011309
;
@@ -1370,6 +1378,7 @@ test "self-closing tag complex example" {
13701378
;
13711379
const expected =
13721380
\\extend template="base.html"/>
1381+
\\
13731382
\\<div id="content">
13741383
\\ <svg viewBox="0 0 24 24">
13751384
\\ <path d="M14.4,6H20V16H13L12.6,14H7V21H5V4H14L14.4,6M14,14H16V12H18V10H16V8H14V10L13,8V6H11V8H9V6H7V8H9V10H7V12H9V10H11V12H13V10L14,12V14M11,10V8H13V10H11M14,10H16V12H14V10Z"/>
@@ -1383,6 +1392,58 @@ test "self-closing tag complex example" {
13831392
try std.testing.expectFmt(expected, "{f}", .{ast.formatter(case)});
13841393
}
13851394

1395+
test "respect empty lines" {
1396+
const case =
1397+
\\<div> a
1398+
\\</div>
1399+
\\
1400+
\\<div></div>
1401+
\\
1402+
\\<div></div>
1403+
\\<div></div>
1404+
\\
1405+
\\
1406+
\\<div></div>
1407+
\\
1408+
\\
1409+
\\
1410+
\\<div></div>
1411+
\\<div> a
1412+
\\</div>
1413+
\\
1414+
\\
1415+
\\
1416+
\\<div> a
1417+
\\</div>
1418+
;
1419+
const expected =
1420+
\\<div>
1421+
\\ a
1422+
\\</div>
1423+
\\
1424+
\\<div></div>
1425+
\\
1426+
\\<div></div>
1427+
\\<div></div>
1428+
\\
1429+
\\<div></div>
1430+
\\
1431+
\\<div></div>
1432+
\\<div>
1433+
\\ a
1434+
\\</div>
1435+
\\
1436+
\\<div>
1437+
\\ a
1438+
\\</div>
1439+
\\
1440+
;
1441+
const ast = try Ast.init(std.testing.allocator, case, .html, true);
1442+
defer ast.deinit(std.testing.allocator);
1443+
1444+
try std.testing.expectFmt(expected, "{f}", .{ast.formatter(case)});
1445+
}
1446+
13861447
pub const Cursor = struct {
13871448
ast: Ast,
13881449
idx: u32,

0 commit comments

Comments
 (0)