MCPcopy
hub / github.com/hashicorp/hcl / format

Function format

hclwrite/format.go:12–31  ·  view source on GitHub ↗

format rewrites tokens within the given sequence, in-place, to adjust the whitespace around their content to achieve canonical formatting.

(tokens Tokens)

Source from the content-addressed store, hash-verified

10// format rewrites tokens within the given sequence, in-place, to adjust the
11// whitespace around their content to achieve canonical formatting.
12func 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
33func formatIndent(lines []formatLine) {
34 // Our methodology for indents is to take the input one line at a time

Callers 15

TestFormatFunction · 0.85
WriteToMethod · 0.85
TestBodySetAttributeRawFunction · 0.85
TestBodyRemoveAttributeFunction · 0.85
TestBodyRenameAttributeFunction · 0.85
TestBodyAppendBlockFunction · 0.85
TestBodyRemoveBlockFunction · 0.85
FormatFunction · 0.85

Calls 4

linesForFormatFunction · 0.85
formatIndentFunction · 0.85
formatSpacesFunction · 0.85
formatCellsFunction · 0.85

Tested by 12

TestFormatFunction · 0.68
TestBodySetAttributeRawFunction · 0.68
TestBodyRemoveAttributeFunction · 0.68
TestBodyRenameAttributeFunction · 0.68
TestBodyAppendBlockFunction · 0.68
TestBodyRemoveBlockFunction · 0.68
TestBlockSetTypeFunction · 0.68
TestBlockSetLabelsFunction · 0.68