Skip to content

Commit

Permalink
work in progress: option/extension/field completion; formatting bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kralicky committed Nov 27, 2023
1 parent adb66f2 commit d87290c
Show file tree
Hide file tree
Showing 7 changed files with 334 additions and 103 deletions.
15 changes: 13 additions & 2 deletions pkg/format/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (v *dumpVisitor) VisitMapTypeNode(node *ast.MapTypeNode) error {
}

func (v *dumpVisitor) VisitMessageFieldNode(node *ast.MessageFieldNode) error {
v.buf.WriteString(fmt.Sprintf("(name=%s) (val=%T)\n", maybe(node.Name).Name.Value(), node.Val.Value()))
v.buf.WriteString(fmt.Sprintf("(name=%s) (val=%T)\n", maybe(node.Name).Name.Value(), maybeValue(node.Val)))
return nil
}

Expand Down Expand Up @@ -188,7 +188,7 @@ func (v *dumpVisitor) VisitOptionNameNode(node *ast.OptionNameNode) error {
}

func (v *dumpVisitor) VisitOptionNode(node *ast.OptionNode) error {
v.buf.WriteString(fmt.Sprintf("(name=%s) (val=%T)\n", StringForOptionName(node.Name), node.Val.Value()))
v.buf.WriteString(fmt.Sprintf("(name=%s) (val=%T)\n", StringForOptionName(node.Name), maybeValue(node.Val)))
return nil
}

Expand Down Expand Up @@ -257,3 +257,14 @@ func (v *dumpVisitor) VisitUintLiteralNode(node *ast.UintLiteralNode) error {
}

var _ ast.Visitor = (*dumpVisitor)(nil)

func maybeValue(val ast.ValueNode) any {
if _, ok := val.(ast.NoSourceNode); ok {
return "(invalid)"
}
vi := val.Value()
if vi == nil {
return "(nil)"
}
return vi
}
60 changes: 44 additions & 16 deletions pkg/format/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,9 @@ func (f *formatter) writeOptionName(optionNameNode *ast.OptionNameNode) {
}
if fieldReferenceNode.Close != nil {
f.writeInline(fieldReferenceNode.Close)
} else if fieldReferenceNode.Open != nil {
// (extended syntax rule) fill in missing close paren automatically
f.writeInline(&ast.RuneNode{Rune: ')'})
}
continue
}
Expand Down Expand Up @@ -815,6 +818,9 @@ GROUPS:
}
if elem.Name.Close != nil {
fclone.writeInline(elem.Name.Close)
} else if elem.Name.Open != nil {
// (extended syntax rule) fill in missing close paren automatically
fclone.writeInline(&ast.RuneNode{Rune: ')'})
}
if elem.Sep != nil {
fclone.writeInline(elem.Sep)
Expand Down Expand Up @@ -1071,8 +1077,12 @@ func messageLiteralHasNestedMessageOrArray(messageLiteralNode *ast.MessageLitera
}
func messageLiteralHasNestedArray(messageLiteralNode *ast.MessageLiteralNode) bool {
for _, elem := range messageLiteralNode.Elements {
switch elem.Val.(type) {
switch v := elem.Val.(type) {
case *ast.ArrayLiteralNode:
if len(v.Elements) == 0 {
// (extended syntax rule) empty array literals don't need to be expanded
continue
}
return true
}
}
Expand Down Expand Up @@ -1179,6 +1189,10 @@ func (f *formatter) writeMessageFieldPrefix(messageFieldNode *ast.MessageFieldNo
}
if fieldReferenceNode.Close != nil {
f.writeInline(fieldReferenceNode.Close)
} else if fieldReferenceNode.Open != nil {
// (extended syntax rule) fill in missing close paren automatically
f.writeInline(&ast.RuneNode{Rune: ')'})

}
if messageFieldNode.Sep != nil {
f.writeInline(messageFieldNode.Sep)
Expand Down Expand Up @@ -1314,6 +1328,9 @@ func (f *formatter) writeFieldReference(fieldReferenceNode *ast.FieldReferenceNo
f.writeInline(fieldReferenceNode.Name)
if fieldReferenceNode.Close != nil {
f.writeInline(fieldReferenceNode.Close)
} else if fieldReferenceNode.Open != nil {
// (extended syntax rule) fill in missing close paren automatically
f.writeInline(&ast.RuneNode{Rune: ')'})
}
}

Expand Down Expand Up @@ -1633,23 +1650,31 @@ func (f *formatter) writeCompactOptions(compactOptionsNode *ast.CompactOptionsNo
// deprecated = true
// ]
//
f.writeInline(compactOptionsNode.OpenBracket)
if len(compactOptionsNode.Options) != 0 {
optionNode := compactOptionsNode.Options[0]
f.writeInline(optionNode.Name)
f.Space()
f.writeInline(optionNode.Equals)
if node, ok := optionNode.Val.(*ast.CompoundStringLiteralNode); ok {
// If there's only a single compact option, the value needs to
// write its comments (if any) in a way that preserves the closing ']'.
f.writeCompoundStringLiteralNoIndentEndInline(node)
f.writeInline(compactOptionsNode.CloseBracket)
return
if len(compactOptionsNode.Options) > 0 {
f.writeInline(compactOptionsNode.OpenBracket)
for i, optionNode := range compactOptionsNode.Options {
f.writeInline(optionNode.Name)
f.Space()
if optionNode.Equals != nil {
f.writeInline(optionNode.Equals)
}
if node, ok := optionNode.Val.(*ast.CompoundStringLiteralNode); ok {
// If there's only a single compact option, the value needs to
// write its comments (if any) in a way that preserves the closing ']'.
f.writeCompoundStringLiteralNoIndentEndInline(node)
f.writeInline(compactOptionsNode.CloseBracket)
return
}
f.Space()
f.writeInline(optionNode.Val)

if i < len(compactOptionsNode.Options)-1 && i < len(compactOptionsNode.Commas) {
f.writeInline(compactOptionsNode.Commas[i])
f.Space()
}
}
f.Space()
f.writeInline(optionNode.Val)
f.writeInline(compactOptionsNode.CloseBracket)
}
f.writeInline(compactOptionsNode.CloseBracket)
return
}
var elementWriterFunc func()
Expand Down Expand Up @@ -2791,6 +2816,9 @@ func StringForFieldReference(fieldReference *ast.FieldReferenceNode) string {
result += string(fieldReference.Name.AsIdentifier())
if fieldReference.Close != nil {
result += ")"
} else if fieldReference.Open != nil {
// (extended syntax rule) fill in missing close paren automatically
result += ")"
}
return result
}
Expand Down
Loading

0 comments on commit d87290c

Please sign in to comment.