Skip to content

Commit 7b9f5fd

Browse files
Expose "unparse", FormatNode and RawAST (#710)
* Export FormatNode so external users can pretty print an AST Signed-off-by: Tom Wilkie <[email protected]> * Expose RawAST function. Signed-off-by: Tom Wilkie <[email protected]> * Add comment Signed-off-by: Tom Wilkie <[email protected]> --------- Signed-off-by: Tom Wilkie <[email protected]> Co-authored-by: Tom Wilkie <[email protected]>
1 parent 9639773 commit 7b9f5fd

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

formatter/formatter.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
// customized using formatter.Options.
55
package formatter
66

7-
import "github.com/google/go-jsonnet/internal/formatter"
7+
import (
8+
"github.com/google/go-jsonnet/ast"
9+
"github.com/google/go-jsonnet/internal/formatter"
10+
"github.com/google/go-jsonnet/internal/parser"
11+
)
812

913
// StringStyle controls how the reformatter rewrites string literals.
1014
// Strings that contain a ' or a " use the optimal syntax to avoid escaping
@@ -46,3 +50,14 @@ func DefaultOptions() Options {
4650
func Format(filename string, input string, options Options) (string, error) {
4751
return formatter.Format(filename, input, options)
4852
}
53+
54+
// FormatNode returns code that is equivalent to its input but better formatted
55+
// according to the given options.
56+
func FormatNode(node ast.Node, finalFodder ast.Fodder, options Options) (string, error) {
57+
return formatter.FormatNode(node, finalFodder, options)
58+
}
59+
60+
// SnippetToRawAST parses a snippet and returns the resulting AST.
61+
func SnippetToRawAST(filename string, snippet string) (ast.Node, ast.Fodder, error) {
62+
return parser.SnippetToRawAST(ast.DiagnosticFileName(filename), "", snippet)
63+
}

internal/formatter/jsonnetfmt.go

+6
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ func Format(filename string, input string, options Options) (string, error) {
152152
return "", err
153153
}
154154

155+
return FormatNode(node, finalFodder, options)
156+
}
157+
158+
// FormatNode returns code that is equivalent to its input but better formatted
159+
// according to the given options.
160+
func FormatNode(node ast.Node, finalFodder ast.Fodder, options Options) (string, error) {
155161
// Passes to enforce style on the AST.
156162
if options.SortImports {
157163
SortImports(&node)

0 commit comments

Comments
 (0)