Rest of corresponding change to formatter, see https://github.com/google/jsonnet/pull/937

This commit is contained in:
Dave Cunningham 2021-12-21 11:51:56 +00:00
parent f60b015405
commit 6fdbe3d728
2 changed files with 7 additions and 4 deletions

View File

@ -194,8 +194,10 @@ func Format(filename string, input string, options Options) (string, error) {
u := &unparser{options: options}
u.unparse(node, false)
u.fillFinal(finalFodder, true, false)
// Final whitespace is stripped at lexing time. Add a single new line
// as files ought to end with a new line.
u.write("\n")
if len(finalFodder) == 0 || finalFodder[len(finalFodder)-1].Kind == ast.FodderInterstitial {
// Final whitespace is stripped at lexing time. If we didn't just output a new line in fillFinal,
// then add a single new line to ensure Jsonnet files end with a new line.
u.write("\n")
}
return u.string(), nil
}

View File

@ -85,8 +85,9 @@ func (u *unparser) fodderFill(fodder ast.Fodder, crowded bool, separateToken boo
u.write(" ")
u.write(fod.Comment[0])
}
u.write("\n")
if !skipTrailing {
for i := 0; i <= fod.Blanks; i++ {
for i := 0; i < fod.Blanks; i++ {
u.write("\n")
}
for i := 0; i < fod.Indent; i++ {