Skip to content
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
2 changes: 2 additions & 0 deletions cmd/txtpbfmt/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var (
dryRun = flag.Bool("dry_run", false, "Enable dry run mode.")
expandAllChildren = flag.Bool("expand_all_children", false, "Expand all children irrespective of initial state.")
skipAllColons = flag.Bool("skip_all_colons", false, "Skip colons whenever possible.")
setAllColons = flag.Bool("set_all_colons", false, "Force colons to be present.")
sortFieldsByFieldName = flag.Bool("sort_fields_by_field_name", false, "Sort fields by field name.")
sortFieldsByFieldNumber = flag.Bool("sort_fields_by_field_number", false, "Sort fields by field number from proto definition.")
protoDescriptor = flag.String("proto_descriptor", "", "Path to protobuf descriptor file (.desc)")
Expand Down Expand Up @@ -90,6 +91,7 @@ func processPath(path string) error {
newContent, err := parser.FormatWithConfig(content, config.Config{
ExpandAllChildren: *expandAllChildren,
SkipAllColons: *skipAllColons,
SetAllColons: *setAllColons,
SortFieldsByFieldName: *sortFieldsByFieldName,
SortFieldsByFieldNumber: *sortFieldsByFieldNumber,
ProtoDescriptor: *protoDescriptor,
Expand Down
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ type Config struct {
// Skip colons whenever possible.
SkipAllColons bool

// Force colons to be present.
SetAllColons bool

// Allow unnamed nodes everywhere.
// Default is to allow only top-level nodes to be unnamed.
AllowUnnamedNodesEverywhere bool
Expand Down
4 changes: 4 additions & 0 deletions impl/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ func addToConfig(metaComment string, c *config.Config) error {
c.RequireFieldSortOrderToMatchAllFieldsInNode = true
case "skip_all_colons":
c.SkipAllColons = true
case "set_all_colons":
c.SetAllColons = true
case "smartquotes":
c.SmartQuotes = true
case "sort_fields_by_field_name":
Expand Down Expand Up @@ -668,6 +670,8 @@ func (p *parser) parseFieldName(nd *ast.Node, isRoot bool) error {
func (p *parser) parseMessage(nd *ast.Node, desc protoreflect.MessageDescriptor) error {
if p.config.SkipAllColons {
nd.SkipColon = true
} else if p.config.SetAllColons {
nd.SkipColon = false
}
nd.ChildrenSameLine = p.bracketSameLine[p.index-1]
nd.IsAngleBracket = p.config.PreserveAngleBrackets && p.in[p.index-1] == '<'
Expand Down
11 changes: 10 additions & 1 deletion parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,16 @@ presubmit: { check_presubmit_service: { address: "address" failure_status: WARNI
`,
out: `# txtpbfmt: skip_all_colons
presubmit { check_presubmit_service { address: "address" failure_status: WARNING options: "options" } }
`}, {
`,
}, {
name: "set_all_colons",
in: `# txtpbfmt: set_all_colons
presubmit { check_presubmit_service { address: "address" failure_status: WARNING options: "options" } }
`,
out: `# txtpbfmt: set_all_colons
presubmit: { check_presubmit_service: { address: "address" failure_status: WARNING options: "options" } }
`,
}, {
name: "separate_directives",
in: `# txtpbfmt: expand_all_children
# txtpbfmt: skip_all_colons
Expand Down
Loading