-
Notifications
You must be signed in to change notification settings - Fork 453
No format for tabular data #1216
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
base: main
Are you sure you want to change the base?
Conversation
Not in all cases formatting of build data / code is required. Code can be written in a way which looks beautiful and easy to spot problem when maintained in tabular format using certain spacing between columns. Current implementation of buildfier results in loosing well formatted code( manually maintained) which has been needed in tabular code.
|
This seems to change the whitespace of nearly every line. Did your editor save tabs as spaces? |
No such settings in my vim editor, which saves tabs as spaces. However our own internal parser works with precision of spaces and all such tabular code need to be separated with spaces. In other words, no formatting from buildifier. |
|
@aiuto ping for review request. |
|
FWIW, we are using an internal version of #985. That patch needs to be rebased and reworked. Importantly for us it supports sorting by a particular column. |
|
@pmbethe09 @larsrc-google @vladmos @aiuto I am curious to know if anything is pending? |
|
/gemini review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces a noformat directive to skip formatting for specific blocks of code, which is particularly useful for manually aligned tabular data. The implementation adds a new noFormat state to the printer and uses an advance function to preserve original spacing. While the feature is a valuable addition, I've found a few issues in the implementation. There's a significant bug in the handling of assignment expressions (AssignExpr) where the noformat logic is currently unreachable. Additionally, the logic for sequences (seq) seems to incorrectly disable noformat for multi-line lists, which contradicts the feature's purpose. I've also suggested a small improvement to the advance function to better preserve blank lines. Please see the detailed comments for suggestions.
| 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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The noformat logic for AssignExpr has two issues:
- The condition
if p.noFormat > 0 && falsewill always be false, making the code unreachable. The&& falseshould be removed. - Inside the block,
p.advance(from, start)is incorrect.fromis the end position of the left-hand side, but the printer cursor is already past the operator. The advance should be from the operator's position to the start of the right-hand side. It should bep.advance(v.OpPos, start).
| 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 { | |
| if p.noFormat > 0 { | |
| _, from := v.LHS.Span() | |
| p.advance(from, v.OpPos) | |
| p.printf("%s", v.Op) | |
| start, _ := v.RHS.Span() | |
| p.advance(v.OpPos, start) | |
| } else { |
| if p.noFormat > 0 { | ||
| noFormat = true | ||
| line := (*start).Line | ||
| for _, x := range *list { | ||
| xstart, _ := x.Span() | ||
| if xstart.Line != line { | ||
| noFormat = false | ||
| break | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic to enable noformat mode for sequences appears to be restricted to single-line sequences only. The loop checks if all elements are on the same line as the opening bracket, and disables noformat if they are not. This seems to contradict the feature's goal of preserving formatting for multi-line tabular data, as seen in the new test case. To support multi-line tables, this check should probably be removed.
if p.noFormat > 0 {
noFormat = true
}| if from.Line < to.Line { | ||
| p.printf("\n") | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation only inserts a single newline, which collapses multiple blank lines from the original source. To better preserve formatting within noformat blocks, consider preserving all newlines between tokens.
| if from.Line < to.Line { | |
| p.printf("\n") | |
| } | |
| for i := from.Line; i < to.Line; i++ { | |
| p.printf("\n") | |
| } |
|
Is there anyone in the bazelbuild org willing to move this forward. |
Not in all cases formatting of build data / code is required. Code can be written in a way which looks beautiful and easy to spot problem when maintained in tabular format using certain spacing between columns. Current implementation of buildfier results in loosing well formatted code( manually maintained) which has been needed in tabular code.