Skip to content

Commit

Permalink
hclwrite: Allow format to be called on fragment of tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
apparentlymart committed Jul 14, 2018
1 parent d6367b5 commit 314ea6f
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions hclwrite/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,6 @@ func spaceAfterToken(subject, before, after *Token) bool {

func linesForFormat(tokens Tokens) []formatLine {
if len(tokens) == 0 {
// should never happen, since we should always have EOF, but let's
// not crash anyway.
return make([]formatLine, 0)
}

Expand Down Expand Up @@ -331,6 +329,16 @@ func linesForFormat(tokens Tokens) []formatLine {
}
}

// If a set of tokens doesn't end in TokenEOF (e.g. because it's a
// fragment of tokens from the middle of a file) then we might fall
// out here with a line still pending.
if lineStart < len(tokens) {
lines[li].lead = tokens[lineStart:]
if lines[li].lead[len(lines[li].lead)-1].Type == hclsyntax.TokenEOF {
lines[li].lead = lines[li].lead[:len(lines[li].lead)-1]
}
}

// Now we'll pick off any trailing comments and attribute assignments
// to shuffle off into the "comment" and "assign" cells.
for i := range lines {
Expand Down

0 comments on commit 314ea6f

Please sign in to comment.