mirror of
https://github.com/google/go-jsonnet.git
synced 2025-09-28 17:01:02 +02:00
Corresponding change to formatter, see https://github.com/google/jsonnet/pull/937
This commit is contained in:
parent
68af6e05bb
commit
f60b015405
@ -134,6 +134,12 @@ func removeInitialNewlines(node ast.Node) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func removeExtraTrailingNewlines(finalFodder ast.Fodder) {
|
||||||
|
if len(finalFodder) > 0 {
|
||||||
|
finalFodder[len(finalFodder)-1].Blanks = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func visitFile(p pass.ASTPass, node *ast.Node, finalFodder *ast.Fodder) {
|
func visitFile(p pass.ASTPass, node *ast.Node, finalFodder *ast.Fodder) {
|
||||||
p.File(p, node, finalFodder)
|
p.File(p, node, finalFodder)
|
||||||
}
|
}
|
||||||
@ -183,10 +189,11 @@ func Format(filename string, input string, options Options) (string, error) {
|
|||||||
visitor := FixIndentation{Options: options}
|
visitor := FixIndentation{Options: options}
|
||||||
visitor.VisitFile(node, finalFodder)
|
visitor.VisitFile(node, finalFodder)
|
||||||
}
|
}
|
||||||
|
removeExtraTrailingNewlines(finalFodder)
|
||||||
|
|
||||||
u := &unparser{options: options}
|
u := &unparser{options: options}
|
||||||
u.unparse(node, false)
|
u.unparse(node, false)
|
||||||
u.fill(finalFodder, true, false)
|
u.fillFinal(finalFodder, true, false)
|
||||||
// Final whitespace is stripped at lexing time. Add a single new line
|
// Final whitespace is stripped at lexing time. Add a single new line
|
||||||
// as files ought to end with a new line.
|
// as files ought to end with a new line.
|
||||||
u.write("\n")
|
u.write("\n")
|
||||||
|
@ -50,9 +50,10 @@ func (u *unparser) write(str string) {
|
|||||||
// creates a crowded situation where there was not one before).
|
// creates a crowded situation where there was not one before).
|
||||||
// If crowded is false and separateToken is false then no space is printed
|
// If crowded is false and separateToken is false then no space is printed
|
||||||
// after or before the fodder, even if the last fodder was an interstitial.
|
// after or before the fodder, even if the last fodder was an interstitial.
|
||||||
func (u *unparser) fill(fodder ast.Fodder, crowded bool, separateToken bool) {
|
func (u *unparser) fodderFill(fodder ast.Fodder, crowded bool, separateToken bool, final bool) {
|
||||||
var lastIndent int
|
var lastIndent int
|
||||||
for _, fod := range fodder {
|
for i, fod := range fodder {
|
||||||
|
skipTrailing := final && (i == (len(fodder) - 1))
|
||||||
switch fod.Kind {
|
switch fod.Kind {
|
||||||
case ast.FodderParagraph:
|
case ast.FodderParagraph:
|
||||||
for i, l := range fod.Comment {
|
for i, l := range fod.Comment {
|
||||||
@ -68,11 +69,13 @@ func (u *unparser) fill(fodder ast.Fodder, crowded bool, separateToken bool) {
|
|||||||
}
|
}
|
||||||
u.write("\n")
|
u.write("\n")
|
||||||
}
|
}
|
||||||
for i := 0; i < fod.Blanks; i++ {
|
if !skipTrailing {
|
||||||
u.write("\n")
|
for i := 0; i < fod.Blanks; i++ {
|
||||||
}
|
u.write("\n")
|
||||||
for i := 0; i < fod.Indent; i++ {
|
}
|
||||||
u.write(" ")
|
for i := 0; i < fod.Indent; i++ {
|
||||||
|
u.write(" ")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
lastIndent = fod.Indent
|
lastIndent = fod.Indent
|
||||||
crowded = false
|
crowded = false
|
||||||
@ -82,11 +85,13 @@ func (u *unparser) fill(fodder ast.Fodder, crowded bool, separateToken bool) {
|
|||||||
u.write(" ")
|
u.write(" ")
|
||||||
u.write(fod.Comment[0])
|
u.write(fod.Comment[0])
|
||||||
}
|
}
|
||||||
for i := 0; i <= fod.Blanks; i++ {
|
if !skipTrailing {
|
||||||
u.write("\n")
|
for i := 0; i <= fod.Blanks; i++ {
|
||||||
}
|
u.write("\n")
|
||||||
for i := 0; i < fod.Indent; i++ {
|
}
|
||||||
u.write(" ")
|
for i := 0; i < fod.Indent; i++ {
|
||||||
|
u.write(" ")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
lastIndent = fod.Indent
|
lastIndent = fod.Indent
|
||||||
crowded = false
|
crowded = false
|
||||||
@ -104,6 +109,14 @@ func (u *unparser) fill(fodder ast.Fodder, crowded bool, separateToken bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *unparser) fill(fodder ast.Fodder, crowded bool, separateToken bool) {
|
||||||
|
u.fodderFill(fodder, crowded, separateToken, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *unparser) fillFinal(fodder ast.Fodder, crowded bool, separateToken bool) {
|
||||||
|
u.fodderFill(fodder, crowded, separateToken, true)
|
||||||
|
}
|
||||||
|
|
||||||
func (u *unparser) unparseSpecs(spec *ast.ForSpec) {
|
func (u *unparser) unparseSpecs(spec *ast.ForSpec) {
|
||||||
if spec.Outer != nil {
|
if spec.Outer != nil {
|
||||||
u.unparseSpecs(spec.Outer)
|
u.unparseSpecs(spec.Outer)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user