diff --git a/formatter/formatter.go b/formatter/formatter.go index 55d0625..b4f0ef0 100644 --- a/formatter/formatter.go +++ b/formatter/formatter.go @@ -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 @@ -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) +} diff --git a/internal/formatter/jsonnetfmt.go b/internal/formatter/jsonnetfmt.go index 21e741d..574d520 100644 --- a/internal/formatter/jsonnetfmt.go +++ b/internal/formatter/jsonnetfmt.go @@ -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)