Skip to content

Commit 25cd11d

Browse files
Internal change.
PiperOrigin-RevId: 869641916
1 parent fcb97cc commit 25cd11d

3 files changed

Lines changed: 24 additions & 2 deletions

File tree

ast/ast.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,21 @@ func ByFieldNumber(_, ni, nj *Node, isWholeSlice bool) bool {
295295
return ni.Name < nj.Name
296296
}
297297

298+
// Formatter is a function that can format nodes in the AST.
299+
type Formatter func([]*Node) error
300+
301+
var extraFormatters []Formatter
302+
303+
// RegisterFormatter registers an extra formatter that will be called after parsing.
304+
func RegisterFormatter(f Formatter) {
305+
extraFormatters = append(extraFormatters, f)
306+
}
307+
308+
// GetFormatters returns all registered formatters.
309+
func GetFormatters() []Formatter {
310+
return extraFormatters
311+
}
312+
298313
// getChildValue returns the Value of the child with the given field name,
299314
// or nil if no single such child exists.
300315
func (n *Node) getChildValue(field string) *Value {

impl/impl.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,11 @@ func ParseWithMetaCommentConfig(in []byte, c config.Config) ([]*ast.Node, error)
188188
if p.index < p.length {
189189
return nil, fmt.Errorf("parser didn't consume all input. Stopped at %s", p.errorContext())
190190
}
191+
for _, f := range ast.GetFormatters() {
192+
if err := f(nodes); err != nil {
193+
return nil, err
194+
}
195+
}
191196
if err := wrap.Strings(nodes, 0, c); err != nil {
192197
return nil, err
193198
}
@@ -201,7 +206,7 @@ func ParseWithMetaCommentConfig(in []byte, c config.Config) ([]*ast.Node, error)
201206
// have the equal sign. Currently there are only two MetaComments that are in the former format:
202207
//
203208
// "sort_repeated_fields_by_subfield": If this appears multiple times, then they will all be added
204-
// to the config and the order is perserved.
209+
// to the config and the order is preserved.
205210
// "wrap_strings_at_column": The <val> is expected to be an integer. If it is not, then it will be
206211
// ignored. If this appears multiple times, only the last one saved.
207212
func addToConfig(metaComment string, c *config.Config) error {

wrap/wrap.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import (
1414

1515
var tagRegex = regexp.MustCompile(`<.*>`)
1616

17-
const indentSpaces = " "
17+
const (
18+
indentSpaces = " "
19+
)
1820

1921
// Strings wraps the strings in the given nodes.
2022
func Strings(nodes []*ast.Node, depth int, c config.Config) error {

0 commit comments

Comments
 (0)