Skip to content
Merged
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
15 changes: 15 additions & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,21 @@ func ByFieldNumber(_, ni, nj *Node, isWholeSlice bool) bool {
return ni.Name < nj.Name
}

// Formatter is a function that can format nodes in the AST.
type Formatter func([]*Node) error

var extraFormatters []Formatter

// RegisterFormatter registers an extra formatter that will be called after parsing.
func RegisterFormatter(f Formatter) {
extraFormatters = append(extraFormatters, f)
}

// GetFormatters returns all registered formatters.
func GetFormatters() []Formatter {
return extraFormatters
}

// getChildValue returns the Value of the child with the given field name,
// or nil if no single such child exists.
func (n *Node) getChildValue(field string) *Value {
Expand Down
7 changes: 6 additions & 1 deletion impl/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ func ParseWithMetaCommentConfig(in []byte, c config.Config) ([]*ast.Node, error)
if p.index < p.length {
return nil, fmt.Errorf("parser didn't consume all input. Stopped at %s", p.errorContext())
}
for _, f := range ast.GetFormatters() {
if err := f(nodes); err != nil {
return nil, err
}
}
if err := wrap.Strings(nodes, 0, c); err != nil {
return nil, err
}
Expand All @@ -201,7 +206,7 @@ func ParseWithMetaCommentConfig(in []byte, c config.Config) ([]*ast.Node, error)
// have the equal sign. Currently there are only two MetaComments that are in the former format:
//
// "sort_repeated_fields_by_subfield": If this appears multiple times, then they will all be added
// to the config and the order is perserved.
// to the config and the order is preserved.
// "wrap_strings_at_column": The <val> is expected to be an integer. If it is not, then it will be
// ignored. If this appears multiple times, only the last one saved.
func addToConfig(metaComment string, c *config.Config) error {
Expand Down
Loading