Skip to content

Commit

Permalink
format: break groups between option and non-option message elements
Browse files Browse the repository at this point in the history
  • Loading branch information
kralicky committed Nov 1, 2023
1 parent 9c7a7b5 commit 8667094
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions pkg/format/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,12 +579,21 @@ func (f *formatter) writeMessage(messageNode *ast.MessageNode) {
)
}

func groupableNodeType(t ast.Node) bool {
type elemKind int

const (
nonOptionKind elemKind = iota + 1
optionKind
)

func groupableNodeType(t ast.Node) (elemKind, bool) {
switch t.(type) {
case *ast.FieldNode, *ast.MapFieldNode, *ast.EnumValueNode, *ast.MessageFieldNode, *ast.OptionNode:
return true
case *ast.FieldNode, *ast.MapFieldNode, *ast.EnumValueNode, *ast.MessageFieldNode:
return nonOptionKind, true
case *ast.OptionNode:
return optionKind, true
}
return false
return 0, false
}

type elementsContainer[T ast.Node] interface {
Expand All @@ -601,9 +610,21 @@ func columnFormatElements[T ast.Node, C elementsContainer[T]](f *formatter, ctr
}
currentGroup = []T{}
}
var kind elemKind
var isGroupable bool
for i := 0; i < len(elems); i++ {
e := elems[i]
if groupableNodeType(e) {
if i > 0 {
k, g := groupableNodeType(e)
if k != kind || g != isGroupable {
startNewGroup()
}
kind = k
isGroupable = g
} else {
kind, isGroupable = groupableNodeType(e)
}
if isGroupable {
fieldInfo := f.fileNode.NodeInfo(e)
if len(currentGroup) == 0 {
currentGroup = append(currentGroup, e)
Expand Down

0 comments on commit 8667094

Please sign in to comment.