Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No format for tabular data #1216

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 72 additions & 5 deletions build/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ type printer struct {
depth int // nesting depth inside ( ) [ ] { }
level int // nesting level of def-, if-else- and for-blocks
needsNewLine bool // true if the next statement needs a new line before it
noFormat int // are we in no-format mode
}

// Emits spaces and newlines to move from one logical position to another.
func (p *printer) advance(from Position, to Position) {
if from.Line < to.Line {
p.printf("\n")
}
for p.indent() < to.LineRune-1 {
p.printf(" ")
}
}

// formattingMode returns the current file formatting mode.
Expand Down Expand Up @@ -549,6 +560,7 @@ func (p *printer) expr(v Expr, outerPrec int) {
// recent \n not following a \ line.
p.newlineIfNeeded()

noFormat := false
if before := v.Comment().Before; len(before) > 0 {
// Want to print a line comment.
// Line comments must be at the current margin.
Expand All @@ -562,6 +574,12 @@ func (p *printer) expr(v Expr, outerPrec int) {
for _, com := range before {
p.printf("%s", strings.TrimSpace(com.Token))
p.newline()
if strings.Contains(strings.ToLower(com.Token), "buildifier: noformat") {
noFormat = true
}
}
if noFormat {
p.noFormat += 1
}
}

Expand Down Expand Up @@ -659,7 +677,15 @@ func (p *printer) expr(v Expr, outerPrec int) {

case *KeyValueExpr:
p.expr(v.Key, precLow)
p.printf(": ")
if p.noFormat > 0 {
_, from := v.Key.Span()
p.advance(from, v.Colon)
p.printf(":")
to, _ := v.Value.Span()
p.advance(v.Colon, to)
} else {
p.printf(": ")
}
p.expr(v.Value, precLow)

case *SliceExpr:
Expand Down Expand Up @@ -748,11 +774,19 @@ func (p *printer) expr(v Expr, outerPrec int) {
}

p.expr(v.LHS, precAssign)
p.printf(" %s", v.Op)
if v.LineBreak {
p.breakline()
if p.noFormat > 0 && false {
_, from := v.LHS.Span()
p.advance(from, v.OpPos)
p.printf("%s", v.Op)
start, _ := v.RHS.Span()
p.advance(from, start)
} else {
p.printf(" ")
p.printf(" %s", v.Op)
if v.LineBreak {
p.breakline()
} else {
p.printf(" ")
}
}
p.expr(v.RHS, precAssign+1)
p.margin = m
Expand Down Expand Up @@ -919,6 +953,10 @@ func (p *printer) expr(v Expr, outerPrec int) {
// Queue end-of-line comments for printing when we
// reach the end of the line.
p.comment = append(p.comment, v.Comment().Suffix...)

if noFormat {
p.noFormat -= 1
}
}

// A seqMode describes a formatting mode for a sequence of values,
Expand Down Expand Up @@ -1027,6 +1065,35 @@ func (p *printer) seq(brack string, start *Position, list *[]Expr, end *End, mod
}
}()


noFormat := false
if p.noFormat > 0 {
noFormat = true
line := (*start).Line
for _, x := range *list {
xstart, _ := x.Span()
if xstart.Line != line {
noFormat = false
break
}
}
}

if noFormat {
lastEnd := *start
for i, x := range *list {
if i > 0 {
p.printf(",")
}
exprStart, exprEnd := x.Span()
p.advance(lastEnd, exprStart)
p.expr(x, precLow)
lastEnd = exprEnd
}
p.advance(lastEnd, end.Pos)
return
}

if p.useCompactMode(start, args, end, mode, forceCompact, forceMultiLine) {
for i, x := range *args {
if i > 0 {
Expand Down
6 changes: 6 additions & 0 deletions build/testdata/071.build.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# buildifier: noformat
my_list = [
("", "", ""),
("ab", "bc", "cd"),
("a", "b", "c"),
]
6 changes: 6 additions & 0 deletions build/testdata/071.bzl.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# buildifier: noformat
my_list = [
("", "", ""),
("ab", "bc", "cd"),
("a", "b", "c"),
]
13 changes: 6 additions & 7 deletions build/testdata/071.in
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
foo()

# buildifier: disable=load-on-top

load(":foo", "bar")

bar()
# buildifier: noformat
my_list = [
("", "", ""),
("ab", "bc", "cd"),
("a", "b", "c"),
]