Documentation ¶
Overview ¶
Package formatter provides API for producing pretty-printed source from AST.
Index ¶
- func Format(filename string, input string, options Options) (string, error)
- func SortImports(file *ast.Node)
- type AddPlusObject
- type CommentStyle
- type EnforceCommentStyle
- type EnforceMaxBlankLines
- type EnforceStringStyle
- type FixIndentation
- type FixNewlines
- func (c *FixNewlines) Arguments(p pass.ASTPass, l *ast.Fodder, args *ast.Arguments, r *ast.Fodder, ...)
- func (c *FixNewlines) Array(p pass.ASTPass, array *ast.Array, ctx pass.Context)
- func (c *FixNewlines) ArrayComp(p pass.ASTPass, arrayComp *ast.ArrayComp, ctx pass.Context)
- func (c *FixNewlines) Local(p pass.ASTPass, local *ast.Local, ctx pass.Context)
- func (c *FixNewlines) Object(p pass.ASTPass, object *ast.Object, ctx pass.Context)
- func (c *FixNewlines) ObjectComp(p pass.ASTPass, objectComp *ast.ObjectComp, ctx pass.Context)
- func (c *FixNewlines) Parameters(p pass.ASTPass, l *ast.Fodder, params *[]ast.Parameter, r *ast.Fodder, ...)
- func (c *FixNewlines) Parens(p pass.ASTPass, parens *ast.Parens, ctx pass.Context)
- type FixParens
- type FixTrailingCommas
- func (c *FixTrailingCommas) Array(p pass.ASTPass, node *ast.Array, ctx pass.Context)
- func (c *FixTrailingCommas) ArrayComp(p pass.ASTPass, node *ast.ArrayComp, ctx pass.Context)
- func (c *FixTrailingCommas) Object(p pass.ASTPass, node *ast.Object, ctx pass.Context)
- func (c *FixTrailingCommas) ObjectComp(p pass.ASTPass, node *ast.ObjectComp, ctx pass.Context)
- type NoRedundantSliceColon
- type Options
- type PrettyFieldNames
- type RemovePlusObject
- type StringStyle
- type StripAllButComments
- type StripComments
- type StripEverything
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Format ¶
Format returns code that is equivalent to its input but better formatted according to the given options.
func SortImports ¶
SortImports sorts imports at the top of the file into alphabetical order by path.
Top-level imports are `local x = import 'xxx.jsonnet` expressions that go before anything else in the file (more precisely all such imports that are either the root of AST or a direct child (body) of a top-level import. Top-level imports are therefore more top-level than top-level functions.
Grouping of imports is preserved. Groups of imports are separated by blank lines or lines containing comments.
Types ¶
type AddPlusObject ¶ added in v0.18.0
AddPlusObject is a formatter pass that replaces e {} with e + {}.
type CommentStyle ¶
type CommentStyle int
CommentStyle controls how the reformatter rewrites comments. Comments that look like a #! hashbang are always left alone.
const ( // CommentStyleHash means #. CommentStyleHash CommentStyle = iota // CommentStyleSlash means //. CommentStyleSlash // CommentStyleLeave means comments are left as they are found. CommentStyleLeave )
type EnforceCommentStyle ¶
type EnforceCommentStyle struct { pass.Base Options Options // contains filtered or unexported fields }
EnforceCommentStyle is a formatter pass that ensures the comments are styled according to the configuration in Options.
func (*EnforceCommentStyle) FodderElement ¶
func (c *EnforceCommentStyle) FodderElement(p pass.ASTPass, element *ast.FodderElement, ctx pass.Context)
FodderElement implements this pass.
type EnforceMaxBlankLines ¶
EnforceMaxBlankLines is a formatter pass that ensures there are not too many blank lines in the code.
func (*EnforceMaxBlankLines) FodderElement ¶
func (c *EnforceMaxBlankLines) FodderElement(p pass.ASTPass, element *ast.FodderElement, ctx pass.Context)
FodderElement implements this pass.
type EnforceStringStyle ¶
EnforceStringStyle is a formatter pass that manages string literals
func (*EnforceStringStyle) LiteralString ¶
func (c *EnforceStringStyle) LiteralString(p pass.ASTPass, lit *ast.LiteralString, ctx pass.Context)
LiteralString implements this pass.
type FixIndentation ¶
FixIndentation is a formatter pass that changes the indentation of new line fodder so that it follows the nested structure of the code.
type FixNewlines ¶
FixNewlines is a formatter pass that adds newlines inside complex structures (arrays, objects etc.).
The main principle is that a structure can either be: * expanded and contain newlines in all the designated places * unexpanded and contain newlines in none of the designated places
It only looks shallowly at the AST nodes, so there may be some newlines deeper that don't affect expanding. For example:
[{ 'a': 'b', 'c': 'd', }]
The outer array can stay unexpanded, because there are no newlines between the square brackets and the braces.
func (*FixNewlines) Arguments ¶
func (c *FixNewlines) Arguments(p pass.ASTPass, l *ast.Fodder, args *ast.Arguments, r *ast.Fodder, ctx pass.Context)
Arguments handles parameters Example2:
f(1, 2, 3)
Should be expanded to:
f(1, 2, 3)
And:
foo( 1, 2, 3)
Should be expanded to:
foo( 1, 2, 3 )
func (*FixNewlines) ObjectComp ¶
func (c *FixNewlines) ObjectComp(p pass.ASTPass, objectComp *ast.ObjectComp, ctx pass.Context)
ObjectComp handles this type of node
type FixTrailingCommas ¶
FixTrailingCommas is a formatter pass that ensures trailing commas are present when a list is split over several lines.
func (*FixTrailingCommas) ObjectComp ¶
func (c *FixTrailingCommas) ObjectComp(p pass.ASTPass, node *ast.ObjectComp, ctx pass.Context)
ObjectComp handles that type of node
type NoRedundantSliceColon ¶
NoRedundantSliceColon is a formatter pass that preserves fodder in the case of arr[1::] being formatted as arr[1:]
type Options ¶
type Options struct { // Indent is the number of spaces for each level of indenation. Indent int // MaxBlankLines is the max allowed number of consecutive blank lines. MaxBlankLines int StringStyle StringStyle CommentStyle CommentStyle // PrettyFieldNames causes fields to only be wrapped in ” when needed. PrettyFieldNames bool // PadArrays causes arrays to be written like [ this ] instead of [this]. PadArrays bool // PadObjects causes arrays to be written like { this } instead of {this}. PadObjects bool // SortImports causes imports at the top of the file to be sorted in groups // by filename. SortImports bool // UseImplicitPlus removes plus sign where it is not required. UseImplicitPlus bool StripEverything bool StripComments bool StripAllButComments bool }
Options is a set of parameters that control the reformatter's behaviour.
func DefaultOptions ¶
func DefaultOptions() Options
DefaultOptions returns the recommended formatter behaviour.
type PrettyFieldNames ¶
PrettyFieldNames forces minimal syntax with field lookups and definitions
func (*PrettyFieldNames) ObjectField ¶
func (c *PrettyFieldNames) ObjectField(p pass.ASTPass, field *ast.ObjectField, ctx pass.Context)
ObjectField prettifies the definitions.
type RemovePlusObject ¶ added in v0.18.0
RemovePlusObject is a formatter pass that replaces ((e)) with (e).
type StringStyle ¶
type StringStyle int
StringStyle controls how the reformatter rewrites string literals. Strings that contain a ' or a " use the optimal syntax to avoid escaping those characters.
const ( // StringStyleDouble means "this". StringStyleDouble StringStyle = iota // StringStyleSingle means 'this'. StringStyleSingle // StringStyleLeave means strings are left how they were found. StringStyleLeave )
type StripAllButComments ¶
StripAllButComments removes all comments and newlines
type StripComments ¶
StripComments removes all comments