hclwrite: Adjust token spacing automatically when writing

Although our underlying parse tree retains all of the token content, it
doesn't necessarily retain all of the spacing information under editing,
and so formatting on save ensures that we'll produce a canonical result
even if some edits have been applied that have changed the expected
alignment of objects, etc.
This commit is contained in:
Martin Atkins 2018-08-14 07:55:47 -07:00
parent ed8144cda1
commit d754d5a269
4 changed files with 7 additions and 9 deletions

View File

@ -19,8 +19,12 @@ func (f *File) Body() *Body {
}
// WriteTo writes the tokens underlying the receiving file to the given writer.
//
// The tokens first have a simple formatting pass applied that adjusts only
// the spaces between them.
func (f *File) WriteTo(wr io.Writer) (int, error) {
tokens := f.inTree.children.BuildTokens(nil)
format(tokens)
return tokens.WriteTo(wr)
}

View File

@ -10,10 +10,6 @@ type Body struct {
inTree
items nodeSet
// indentLevel is the number of spaces that should appear at the start
// of lines added within this body.
indentLevel int
}
func (b *Body) appendItem(c nodeContent) *node {

View File

@ -185,9 +185,7 @@ func parseBody(nativeBody *hclsyntax.Body, from inputTokens) (inputTokens, *node
body := &Body{
inTree: newInTree(),
indentLevel: 0, // TODO: deal with this
items: newNodeSet(),
items: newNodeSet(),
}
remain := within

View File

@ -10,8 +10,8 @@ import (
// added t it.
func NewFile() *File {
body := &Body{
inTree: newInTree(),
indentLevel: 0,
inTree: newInTree(),
items: newNodeSet(),
}
file := &File{
inTree: newInTree(),