| ... | @@ -39,11 +39,12 @@ pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) (@typeOf( | ... | @@ -39,11 +39,12 @@ pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) (@typeOf( |
| 39 | } | 39 | } |
| 40 | | 40 | |
| 41 | fn renderExtraNewline(tree: &ast.Tree, stream: var, node: &ast.Node) !void { | 41 | fn renderExtraNewline(tree: &ast.Tree, stream: var, node: &ast.Node) !void { |
| 42 | var first_token = node.firstToken(); | 42 | const first_token = node.firstToken(); |
| 43 | while (tree.tokens.at(first_token - 1).id == Token.Id.DocComment) { | 43 | var prev_token = first_token; |
| 44 | first_token -= 1; | 44 | while (tree.tokens.at(prev_token - 1).id == Token.Id.DocComment) { |
| | 45 | prev_token -= 1; |
| 45 | } | 46 | } |
| 46 | const prev_token_end = tree.tokens.at(first_token - 1).end; | 47 | const prev_token_end = tree.tokens.at(prev_token - 1).end; |
| 47 | const loc = tree.tokenLocation(prev_token_end, first_token); | 48 | const loc = tree.tokenLocation(prev_token_end, first_token); |
| 48 | if (loc.line >= 2) { | 49 | if (loc.line >= 2) { |
| 49 | try stream.writeByte('\n'); | 50 | try stream.writeByte('\n'); |
| ... | @@ -1715,7 +1716,17 @@ fn renderToken(tree: &ast.Tree, stream: var, token_index: ast.TokenIndex, indent | ... | @@ -1715,7 +1716,17 @@ fn renderToken(tree: &ast.Tree, stream: var, token_index: ast.TokenIndex, indent |
| 1715 | else => {}, | 1716 | else => {}, |
| 1716 | } | 1717 | } |
| 1717 | | 1718 | |
| 1718 | if (next_token.id != Token.Id.LineComment) { | 1719 | // Skip over same line doc comments |
| | 1720 | var offset: usize = 1; |
| | 1721 | if (next_token.id == Token.Id.DocComment) { |
| | 1722 | const loc = tree.tokenLocationPtr(token.end, next_token); |
| | 1723 | if (loc.line == 0) { |
| | 1724 | offset += 1; |
| | 1725 | next_token = tree.tokens.at(token_index + offset); |
| | 1726 | } |
| | 1727 | } |
| | 1728 | |
| | 1729 | if (next_token.id != Token.Id.LineComment) blk: { |
| 1719 | switch (space) { | 1730 | switch (space) { |
| 1720 | Space.None, Space.NoNewline => return, | 1731 | Space.None, Space.NoNewline => return, |
| 1721 | Space.Newline => { | 1732 | Space.Newline => { |
| ... | @@ -1739,7 +1750,6 @@ fn renderToken(tree: &ast.Tree, stream: var, token_index: ast.TokenIndex, indent | ... | @@ -1739,7 +1750,6 @@ fn renderToken(tree: &ast.Tree, stream: var, token_index: ast.TokenIndex, indent |
| 1739 | } | 1750 | } |
| 1740 | | 1751 | |
| 1741 | var loc = tree.tokenLocationPtr(token.end, next_token); | 1752 | var loc = tree.tokenLocationPtr(token.end, next_token); |
| 1742 | var offset: usize = 1; | | |
| 1743 | if (loc.line == 0) { | 1753 | if (loc.line == 0) { |
| 1744 | try stream.print(" {}", mem.trimRight(u8, tree.tokenSlicePtr(next_token), " ")); | 1754 | try stream.print(" {}", mem.trimRight(u8, tree.tokenSlicePtr(next_token), " ")); |
| 1745 | offset = 2; | 1755 | offset = 2; |
| ... | @@ -1820,8 +1830,15 @@ fn renderToken(tree: &ast.Tree, stream: var, token_index: ast.TokenIndex, indent | ... | @@ -1820,8 +1830,15 @@ fn renderToken(tree: &ast.Tree, stream: var, token_index: ast.TokenIndex, indent |
| 1820 | fn renderDocComments(tree: &ast.Tree, stream: var, node: var, indent: usize) (@typeOf(stream).Child.Error || Error)!void { | 1830 | fn renderDocComments(tree: &ast.Tree, stream: var, node: var, indent: usize) (@typeOf(stream).Child.Error || Error)!void { |
| 1821 | const comment = node.doc_comments ?? return; | 1831 | const comment = node.doc_comments ?? return; |
| 1822 | var it = comment.lines.iterator(0); | 1832 | var it = comment.lines.iterator(0); |
| | 1833 | const first_token = node.firstToken(); |
| 1823 | while (it.next()) |line_token_index| { | 1834 | while (it.next()) |line_token_index| { |
| 1824 | try renderToken(tree, stream, line_token_index.*, indent, Space.Newline); | 1835 | if (line_token_index.* < first_token) { |
| 1825 | try stream.writeByteNTimes(' ', indent); | 1836 | try renderToken(tree, stream, line_token_index.*, indent, Space.Newline); |
| | 1837 | try stream.writeByteNTimes(' ', indent); |
| | 1838 | } else { |
| | 1839 | try renderToken(tree, stream, line_token_index.*, indent, Space.NoComment); |
| | 1840 | try stream.write("\n"); |
| | 1841 | try stream.writeByteNTimes(' ', indent); |
| | 1842 | } |
| 1826 | } | 1843 | } |
| 1827 | } | 1844 | } |