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
17 changes: 16 additions & 1 deletion formatter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
// customized using formatter.Options.
package formatter

import "github.com/google/go-jsonnet/internal/formatter"
import (
"github.com/google/go-jsonnet/ast"
"github.com/google/go-jsonnet/internal/formatter"
"github.com/google/go-jsonnet/internal/parser"
)

// StringStyle controls how the reformatter rewrites string literals.
// Strings that contain a ' or a " use the optimal syntax to avoid escaping
Expand Down Expand Up @@ -46,3 +50,14 @@ func DefaultOptions() Options {
func Format(filename string, input string, options Options) (string, error) {
return formatter.Format(filename, input, options)
}

// FormatNode returns code that is equivalent to its input but better formatted
// according to the given options.
func FormatNode(node ast.Node, finalFodder ast.Fodder, options Options) (string, error) {
return formatter.FormatNode(node, finalFodder, options)
}

// SnippetToRawAST parses a snippet and returns the resulting AST.
func SnippetToRawAST(filename string, snippet string) (ast.Node, ast.Fodder, error) {
return parser.SnippetToRawAST(ast.DiagnosticFileName(filename), "", snippet)
}
6 changes: 6 additions & 0 deletions internal/formatter/jsonnetfmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ func Format(filename string, input string, options Options) (string, error) {
return "", err
}

return FormatNode(node, finalFodder, options)
}

// FormatNode returns code that is equivalent to its input but better formatted
// according to the given options.
func FormatNode(node ast.Node, finalFodder ast.Fodder, options Options) (string, error) {
// Passes to enforce style on the AST.
if options.SortImports {
SortImports(&node)
Expand Down