| ... | ... | @@ -26,7 +26,10 @@ pub fn renderTree(buffer: *std.ArrayList(u8), tree: ast.Tree) Error!void { |
| 26 | 26 | const ais = &auto_indenting_stream; |
| 27 | 27 | |
| 28 | 28 | // Render all the line comments at the beginning of the file. |
| 29 | | const comment_end_loc: usize = tree.tokens.items(.start)[0]; |
| 29 | const comment_end_loc = if (tree.tokens.items(.tag)[0] == .eof) |
| 30 | tree.source.len |
| 31 | else |
| 32 | tree.tokens.items(.start)[0]; |
| 30 | 33 | _ = try renderComments(ais, tree, 0, comment_end_loc); |
| 31 | 34 | |
| 32 | 35 | try renderMembers(ais, tree, tree.rootDecls()); |
| ... | ... | @@ -1995,11 +1998,20 @@ fn renderToken(ais: *Ais, tree: ast.Tree, token_index: ast.TokenIndex, space: Sp |
| 1995 | 1998 | /// that end is the last byte before the next token. |
| 1996 | 1999 | fn renderComments(ais: *Ais, tree: ast.Tree, start: usize, end: usize) Error!bool { |
| 1997 | 2000 | var index: usize = start; |
| 2001 | var rendered_empty_comments = false; |
| 1998 | 2002 | while (mem.indexOf(u8, tree.source[index..end], "//")) |offset| { |
| 1999 | 2003 | const comment_start = index + offset; |
| 2000 | | const newline = comment_start + |
| 2001 | | mem.indexOfScalar(u8, tree.source[comment_start..end], '\n').?; |
| 2002 | | |
| 2004 | const newline_index = mem.indexOfScalar(u8, tree.source[comment_start..end], '\n') orelse { |
| 2005 | // comment ends in EOF. |
| 2006 | const untrimmed_comment = tree.source[comment_start..]; |
| 2007 | const trimmed_comment = mem.trimRight(u8, untrimmed_comment, &std.ascii.spaces); |
| 2008 | if (trimmed_comment.len != 2) { |
| 2009 | try ais.writer().print("{s}\n", .{trimmed_comment}); |
| 2010 | index = end; |
| 2011 | } |
| 2012 | return index != start; |
| 2013 | }; |
| 2014 | const newline = comment_start + newline_index; |
| 2003 | 2015 | const untrimmed_comment = tree.source[comment_start..newline]; |
| 2004 | 2016 | const trimmed_comment = mem.trimRight(u8, untrimmed_comment, &std.ascii.spaces); |
| 2005 | 2017 | |
| ... | ... | @@ -2013,6 +2025,11 @@ fn renderComments(ais: *Ais, tree: ast.Tree, start: usize, end: usize) Error!boo |
| 2013 | 2025 | // Respect the newline directly before the comment. |
| 2014 | 2026 | // Note: This allows an empty line between comments |
| 2015 | 2027 | try ais.insertNewline(); |
| 2028 | } else if (trimmed_comment.len == 2) { |
| 2029 | if (!rendered_empty_comments) { |
| 2030 | try ais.writer().writeByte('\n'); |
| 2031 | rendered_empty_comments = true; |
| 2032 | } |
| 2016 | 2033 | } else if (index == start) { |
| 2017 | 2034 | // Otherwise if the first comment is on the same line as |
| 2018 | 2035 | // the token before it, prefix it with a single space. |
| ... | ... | @@ -2020,7 +2037,10 @@ fn renderComments(ais: *Ais, tree: ast.Tree, start: usize, end: usize) Error!boo |
| 2020 | 2037 | } |
| 2021 | 2038 | } |
| 2022 | 2039 | |
| 2023 | | try ais.writer().print("{s}\n", .{trimmed_comment}); |
| 2040 | if (trimmed_comment.len != 2) { |
| 2041 | try ais.writer().print("{s}\n", .{trimmed_comment}); |
| 2042 | rendered_empty_comments = false; |
| 2043 | } |
| 2024 | 2044 | index = newline + 1; |
| 2025 | 2045 | |
| 2026 | 2046 | if (ais.disabled_offset) |disabled_offset| { |