Skip to content

Commit 6ce62c9

Browse files
committed
fmt: fancier attribute alignment
same rules as before, but now attributes are aligned in a more clear way
1 parent 662865f commit 6ce62c9

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/html/Ast.zig

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -927,9 +927,10 @@ pub fn render(ast: Ast, src: []const u8, w: *Writer) !void {
927927
.element_void,
928928
.element_self_closing,
929929
=> 1,
930-
else => 0,
930+
else => @intCast(name.len),
931931
};
932932

933+
var first = true;
933934
while (tt.next(src[0..current.open.end])) |maybe_attr| {
934935
log.debug("tt: {s}", .{@tagName(maybe_attr)});
935936
log.debug("tt: {any}", .{maybe_attr});
@@ -951,9 +952,14 @@ pub fn render(ast: Ast, src: []const u8, w: *Writer) !void {
951952
.tag => break,
952953
.attr => |attr| {
953954
if (vertical) {
954-
try w.print("\n", .{});
955-
for (0..indentation + extra) |_| {
956-
try w.print(" ", .{});
955+
if (first) {
956+
first = false;
957+
try w.print(" ", .{});
958+
} else {
959+
try w.print("\n", .{});
960+
for (0..(indentation * 2) + extra) |_| {
961+
try w.print(" ", .{});
962+
}
957963
}
958964
} else {
959965
try w.print(" ", .{});
@@ -978,7 +984,7 @@ pub fn render(ast: Ast, src: []const u8, w: *Writer) !void {
978984
}
979985
if (vertical) {
980986
try w.print("\n", .{});
981-
for (0..indentation + extra -| 1) |_| {
987+
for (0..indentation -| 1) |_| {
982988
try w.print(" ", .{});
983989
}
984990
}
@@ -1225,10 +1231,9 @@ test "formatting - attributes" {
12251231
\\ <body>
12261232
\\ <div>
12271233
\\ <link>
1228-
\\ <div
1229-
\\ id="foo"
1230-
\\ class="bar"
1231-
\\ style="tarstarstarstarstarstarstarst"
1234+
\\ <div id="foo"
1235+
\\ class="bar"
1236+
\\ style="tarstarstarstarstarstarstarst"
12321237
\\ ></div>
12331238
\\ </div>
12341239
\\ </body>

0 commit comments

Comments
 (0)