Skip to content

Commit

Permalink
Completion of Design 1:
Browse files Browse the repository at this point in the history
- Till now only the Table's root node was detected and marked by our logic.
- Rows were printed based on whether "tabWriter" is ON/OFF.
- This makes the logic brittle.
- Add code to detect the children of Table root which act as the rows of the table.
- Marking them gives us a flexible design - that can be extended to [ [ ] ]/( [] ) etc
  • Loading branch information
SKashyap committed Jun 22, 2021
1 parent d3334bc commit 84a1b7d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
8 changes: 4 additions & 4 deletions build/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ func (p *printer) expr(v Expr, outerPrec int) {
p.seq("()", &v.Load, &args, &v.Rparen, modeLoad, v.ForceCompact, false, false)

case *ListExpr:
p.seq("[]", &v.Start, &v.List, &v.End, modeList, false, v.ForceMultiLine, v.ForceTabular)
p.seq("[]", &v.Start, &v.List, &v.End, modeList, false, v.ForceMultiLine, v.FormatAsTable)

case *SetExpr:
p.seq("{}", &v.Start, &v.List, &v.End, modeList, false, v.ForceMultiLine, false)
Expand All @@ -629,10 +629,10 @@ func (p *printer) expr(v Expr, outerPrec int) {
mode = modeSeq
}

if !p.tabWriterOn {
p.seq("()", &v.Start, &v.List, &v.End, mode, v.ForceCompact, v.ForceMultiLine, false)
} else {
if v.FormatAsTableRow {
p.tabbedSeq("()", &v.Start, &v.List, &v.End)
} else {
p.seq("()", &v.Start, &v.List, &v.End, mode, v.ForceCompact, v.ForceMultiLine, false)
}

case *DictExpr:
Expand Down
18 changes: 15 additions & 3 deletions build/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,16 +405,28 @@ func (x namedArgs) Less(i, j int) bool {
return p.index < q.index
}

// Detects if a data is marked as tablular based on the tag `buildifier: table`
// and sets the correct flags on the expression to indentify those nodes in the AST.
// Detects if a data is marked as tablular based on the tag
// `buildifier: table` and sets the correct flags on the expression to
// identify those nodes in the AST.
func formatTables(f *File) {

// Function that sets a bit on the child nodes of type (list) to indicate
// that they are a part of a row
markTableRowNodes := func(x Expr, stk2 []Expr) Expr {
if x, ok := x.(*TupleExpr); ok {
x.FormatAsTableRow = true
}
return x
}

markTableNodes := func(v Expr, stk []Expr) {
switch v := v.(type) {
// Tabular formatting is currently supported only for lists
case *ListExpr:
// Handle when "#buildifier: table" tag is set
if len(v.List) > 0 && tableFormat(v.List[0]) {
v.ForceTabular = true
v.FormatAsTable = true
EditChildren(v, markTableRowNodes)
sortTableRows(v)
}
}
Expand Down
7 changes: 4 additions & 3 deletions build/syntax.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ type ListExpr struct {
List []Expr
End
ForceMultiLine bool // force multiline form when printing
ForceTabular bool // force tabular formatting when printing
FormatAsTable bool // flag to indicate that this list needs to print tabular data.
}

// Span returns the start and end positions of the node
Expand Down Expand Up @@ -486,8 +486,9 @@ type TupleExpr struct {
Start Position
List []Expr
End
ForceCompact bool // force compact (non-multiline) form when printing
ForceMultiLine bool // force multiline form when printing
ForceCompact bool // force compact (non-multiline) form when printing
ForceMultiLine bool // force multiline form when printing
FormatAsTableRow bool // flag that indicates that this tuple will be printed as a row of a table
}

// Span returns the start and end positions of the node
Expand Down

0 comments on commit 84a1b7d

Please sign in to comment.