| author | |
| committer | |
| log | dfc3e11748615f10cf6958c61a934ef852b5aa94 |
| tree | fe9791d6534372eb0972f7a2d522ef007b7c05cf |
| parent | ca49b6f6b4ffa3ff4324a45501eef98848a2d141 |
3 files changed, 42 insertions(+), 1 deletions(-)
std/zig/parse.zig+7| ... | ... | @@ -41,6 +41,13 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree { |
| 41 | 41 | } |
| 42 | 42 | var tok_it = tree.tokens.iterator(0); |
| 43 | 43 | |
| 44 | // skip over line comments at the top of the file | |
| 45 | while (true) { | |
| 46 | const next_tok = tok_it.peek() ?? break; | |
| 47 | if (next_tok.id != Token.Id.LineComment) break; | |
| 48 | _ = tok_it.next(); | |
| 49 | } | |
| 50 | ||
| 44 | 51 | try stack.append(State.TopLevel); |
| 45 | 52 | |
| 46 | 53 | while (true) { |
std/zig/parser_test.zig+20| ... | ... | @@ -1,3 +1,23 @@ |
| 1 | test "zig fmt: first thing in file is line comment" { | |
| 2 | try testCanonical( | |
| 3 | \\// Introspection and determination of system libraries needed by zig. | |
| 4 | \\ | |
| 5 | \\// Introspection and determination of system libraries needed by zig. | |
| 6 | \\ | |
| 7 | \\const std = @import("std"); | |
| 8 | \\ | |
| 9 | ); | |
| 10 | } | |
| 11 | ||
| 12 | test "zig fmt: line comment after doc comment" { | |
| 13 | try testCanonical( | |
| 14 | \\/// doc comment | |
| 15 | \\// line comment | |
| 16 | \\fn foo() void {} | |
| 17 | \\ | |
| 18 | ); | |
| 19 | } | |
| 20 | ||
| 1 | 21 | test "zig fmt: float literal with exponent" { |
| 2 | 22 | try testCanonical( |
| 3 | 23 | \\test "bit field alignment" { |
std/zig/render.zig+15-1| ... | ... | @@ -15,6 +15,20 @@ pub const Error = error{ |
| 15 | 15 | pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) (@typeOf(stream).Child.Error || Error)!void { |
| 16 | 16 | comptime assert(@typeId(@typeOf(stream)) == builtin.TypeId.Pointer); |
| 17 | 17 | |
| 18 | // render all the line comments at the beginning of the file | |
| 19 | var tok_it = tree.tokens.iterator(0); | |
| 20 | while (tok_it.next()) |token| { | |
| 21 | if (token.id != Token.Id.LineComment) break; | |
| 22 | try stream.print("{}\n", tree.tokenSlicePtr(token)); | |
| 23 | if (tok_it.peek()) |next_token| { | |
| 24 | const loc = tree.tokenLocationPtr(token.end, next_token); | |
| 25 | if (loc.line >= 2) { | |
| 26 | try stream.writeByte('\n'); | |
| 27 | } | |
| 28 | } | |
| 29 | } | |
| 30 | ||
| 31 | ||
| 18 | 32 | var it = tree.root_node.decls.iterator(0); |
| 19 | 33 | while (it.next()) |decl| { |
| 20 | 34 | try renderTopLevelDecl(allocator, stream, tree, 0, decl.*); |
| ... | ... | @@ -1455,7 +1469,7 @@ fn renderDocComments(tree: &ast.Tree, stream: var, node: var, indent: usize) (@t |
| 1455 | 1469 | const comment = node.doc_comments ?? return; |
| 1456 | 1470 | var it = comment.lines.iterator(0); |
| 1457 | 1471 | while (it.next()) |line_token_index| { |
| 1458 | try stream.print("{}\n", tree.tokenSlice(line_token_index.*)); | |
| 1472 | try renderToken(tree, stream, line_token_index.*, indent, Space.Newline); | |
| 1459 | 1473 | try stream.writeByteNTimes(' ', indent); |
| 1460 | 1474 | } |
| 1461 | 1475 | } |