formatter

package
v0.3.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 26, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package formatter pretty-prints phpscript ASTs in a gofmt-like style: hard tabs, canonical `function` keywords, parentheses on control structures, semicolons, collapsed blank lines, and a trailing newline. Class, function, and control-structure opening braces stay on the declaration line.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Changed added in v0.3.0

func Changed(results []Result) []string

Changed returns the paths of the files that were rewritten.

func File

func File(path string) (bool, error)

File formats path in place. Reports whether the file contents changed, and returns a *SkipError for a file left as it is.

func Print

func Print(prog *model.Program, opts Options) string

Print renders prog as formatted PHP source. Any node the printer has no spelling for is rendered as a placeholder comment; use Source, which refuses to rewrite a file in that case.

func Source

func Source(src string) (string, error)

Source parses src and pretty-prints the AST.

Types

type Comment added in v0.3.0

type Comment struct {
	Text    string
	Line    int
	EndLine int
	OwnLine bool
}

Comment is one comment of the source, with the lines it occupied. The AST holds no comments, so they are read from the source separately and placed by line: a comment is written out before the first statement that starts below it. OwnLine records that nothing but whitespace preceded it, which is what separates a comment written above a statement from one written after it.

func CollectComments added in v0.3.0

func CollectComments(src string) []Comment

CollectComments returns every comment of src in source order, with the lines it occupied and whether it stood on a line of its own. The AST holds no comments, so this is what keeps them in a file the formatter rewrites.

type Options

type Options struct {
	// Comments is every comment of the source file, in source order. The AST
	// holds none of them, so the printer places them by line: a comment is
	// written out before the first statement that starts below it.
	Comments []Comment
}

Options controls AST pretty-printing.

type Result added in v0.3.0

type Result struct {
	Path    string
	Changed bool
	// Skipped is the reason the file was left as it is, and nil when the file
	// was formatted.
	Skipped error
}

Result is what the formatter did with one file. Formatting rewrites files in place, so a file is only rewritten when the formatter could read all of it and its output held up to the checks in File; every other file is reported as skipped and left as it is.

func NeedFormatting added in v0.3.0

func NeedFormatting(paths []string) ([]Result, error)

NeedFormatting reports what Paths would do, writing nothing.

func Paths

func Paths(paths []string) ([]Result, error)

Paths formats each path argument in place and reports what happened to every file it looked at. A file it cannot format is skipped rather than failing the run: a directory of PHP holds valid code phpscript does not support yet, and one such file should not stop the rest from being formatted. Only reading and writing errors are returned.

type SkipError added in v0.3.0

type SkipError struct {
	Path   string
	Reason error
}

SkipError reports a file the formatter left alone: source it cannot parse, a node it has no spelling for, or output that did not hold up to the checks in File. Formatting rewrites a file in place, so anything the formatter does not fully understand is safer left as it is than rewritten from a partial reading of it.

func (*SkipError) Error added in v0.3.0

func (e *SkipError) Error() string

func (*SkipError) Unwrap added in v0.3.0

func (e *SkipError) Unwrap() error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL