format rewrites tokens within the given sequence, in-place, to adjust the whitespace around their content to achieve canonical formatting.
(tokens Tokens)
| 10 | // format rewrites tokens within the given sequence, in-place, to adjust the |
| 11 | // whitespace around their content to achieve canonical formatting. |
| 12 | func format(tokens Tokens) { |
| 13 | // Formatting is a multi-pass process. More details on the passes below, |
| 14 | // but this is the overview: |
| 15 | // - adjust the leading space on each line to create appropriate |
| 16 | // indentation |
| 17 | // - adjust spaces between tokens in a single cell using a set of rules |
| 18 | // - adjust the leading space in the "assign" and "comment" cells on each |
| 19 | // line to vertically align with neighboring lines. |
| 20 | // All of these steps operate in-place on the given tokens, so a caller |
| 21 | // may collect a flat sequence of all of the tokens underlying an AST |
| 22 | // and pass it here and we will then indirectly modify the AST itself. |
| 23 | // Formatting must change only whitespace. Specifically, that means |
| 24 | // changing the SpacesBefore attribute on a token while leaving the |
| 25 | // other token attributes unchanged. |
| 26 | |
| 27 | lines := linesForFormat(tokens) |
| 28 | formatIndent(lines) |
| 29 | formatSpaces(lines) |
| 30 | formatCells(lines) |
| 31 | } |
| 32 | |
| 33 | func formatIndent(lines []formatLine) { |
| 34 | // Our methodology for indents is to take the input one line at a time |