codefmt

package
v0.0.0-...-a992680 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DisambiguateName

func DisambiguateName(name string) iter.Seq[string]

DisambiguateName offers an alternative unique names.

func Errorf

func Errorf(pkger Pkger, poser Poser, format string, args ...any) error

func FormatExpr

func FormatExpr(pkger Pkger, expr ast.Expr) string

FormatExpr is a shorthand for Formatter.Expr.

func FormatObj

func FormatObj(pkger Pkger, obj types.Object) string

FormatObj is a shorthand for Formatter.Obj.

func FormatPos

func FormatPos(pkger Pkger, pos token.Pos) string

FormatPos is a shorthand for Formatter.Pos.

func FormatPosition

func FormatPosition(pos token.Position) string

func FormatSig

func FormatSig(pkger Pkger, sig *types.Signature) string

FormatSig is a shorthand for Formatter.Sig.

func FormatType

func FormatType(pkger Pkger, typ types.Type) string

FormatType is a shorthand for Formatter.Type.

func FormatTypeParen

func FormatTypeParen(pkger Pkger, typ types.Type) string

FormatTypeParen is a shorthand for Formatter.TypeParen.

func Fprintf

func Fprintf(pkger Pkger, w io.Writer, format string, args ...any) (int, error)

func NormalizeName

func NormalizeName(name string) string

func RewriteImports

func RewriteImports[T ast.Node](w *Writer, node T) T

RewriteImports modifies the given AST node to rewrite imported package names to ensure there is no name conflict.

func Sprintf

func Sprintf(pkger Pkger, format string, args ...any) string

Types

type CodeError

type CodeError struct {
	// contains filtered or unexported fields
}

CodeError indicates where the error occurred in user's source code.

func (CodeError) End

func (e CodeError) End() token.Pos

End returns the end position of the error. It may be invalid.

func (CodeError) Error

func (e CodeError) Error() string

Error implements the error interface. If pos is valid, the position is prepended to the error message.

func (CodeError) Pos

func (e CodeError) Pos() token.Pos

Pos returns the position where the error occurred. It may be invalid.

func (CodeError) Unwrap

func (e CodeError) Unwrap() error

Unwrap returns the underlying error.

type Ender

type Ender interface{ End() token.Pos }

type Exprer

type Exprer interface{ Expr() ast.Expr }

type Formatter

type Formatter struct {
	PkgPath   string
	Fset      *token.FileSet
	TypesInfo *types.Info
}

Formatter formats types, objects, expressions, and positions.

func New

func New(pkg *packages.Package) Formatter

func (Formatter) Errorf

func (f Formatter) Errorf(poser Poser, format string, args ...any) error

Errorf formats an error message. The error will indicate the position in the source code if the position is valid.

func (Formatter) Expr

func (f Formatter) Expr(expr ast.Expr) string

Expr returns a Go source code representation of the given ast.Expr.

func (Formatter) Fprintf

func (f Formatter) Fprintf(w io.Writer, format string, args ...any) (int, error)

func (Formatter) Obj

func (f Formatter) Obj(obj types.Object) string

Obj returns a code string to refer the given object.

e.g., f.Obj([types.Object for strconv.Atoi]) => "strconv.Atoi"

func (Formatter) Pos

func (f Formatter) Pos(pos token.Pos) string

func (Formatter) Sig

func (f Formatter) Sig(sig *types.Signature) string

Sig returns a compact string representation of the given function signature without "func" keyword, receiver, and parameter names.

e.g., f.Sig([*types.Signature of strconv.Atoi) => "(string) (int, error)"

func (Formatter) Sprintf

func (f Formatter) Sprintf(format string, args ...any) string

func (Formatter) Type

func (f Formatter) Type(typ types.Type) string

Type returns a string representation of the given type.

e.g., f.Type([types.Type for bytes.Buffer]) => "bytes.Buffer"

func (Formatter) TypeParen

func (f Formatter) TypeParen(typ types.Type) string

TypeParen returns a string representation of the given type. It wraps the string with parentheses if the type is a pointer.

type Import

type Import struct {
	// The package to import.
	*types.Package

	// HasAlias indicates that the import has an alias.
	HasAlias bool
}

type NS

type NS map[string]struct{}

NS manages unique names in a namespace.

func NewNS

func NewNS(scope *types.Scope) NS

NewNS creates a new namespace which reserves all names in the given scope.

func (NS) Name

func (ns NS) Name(name string) string

Name returns a unique name in its namespace. Once a name is used, it is reserved in the namespace to avoid conflicts. If conflicts occur, a numbering suffix is added.

Panics if the name is empty.

func (NS) Reserve

func (ns NS) Reserve(name string) bool

Reserve marks a name as used in the namespace. If the name is already used, it returns false.

type Objecter

type Objecter interface{ Object() types.Object }

type Pkger

type Pkger interface{ Pkg() *packages.Package }

func Pkg

func Pkg(pkg *packages.Package) Pkger

type Poser

type Poser interface{ Pos() token.Pos }

func Pos

func Pos(pos token.Pos) Poser

type TypeInfoTyper

type TypeInfoTyper interface{ Type() typeinfo.Type }

type TypeInfoer

type TypeInfoer interface{ TypeInfo() typeinfo.Type }

type Typer

type Typer interface{ Type() types.Type }

func Type

func Type(typ types.Type) Typer

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer is a writer for generated code.

func NewWriter

func NewWriter(w io.Writer, pkg *packages.Package) *Writer

NewWriter creates a new Writer. It does not initialize the namespace. To specify a namespace, use [SetNamespace].

func (*Writer) Import

func (w *Writer) Import(path, name string) string

Import adds an import for the package with the given path and alias. It returns the name of the imported package. The name might be different if it has tried to resolve name conflicts.

// fmtName can be used to refer to the "fmt" package without any name conflict.
fmtName := w.Import("fmt", "fmt")
w.Printf("%s.Println(\"Hello, World!\")", fmtName)

When calling it, the package to import is recorded. Call [Imports] to retrieve them.

func (*Writer) Imports

func (w *Writer) Imports() map[string]Import

Imports returns the collected imports. Imports are collected by [Ref] and Type.

func (*Writer) Name

func (w *Writer) Name(name string) string

Name returns a unique name in the namespace of the writer.

func (*Writer) Printf

func (w *Writer) Printf(format string, args ...any) (int, error)

Printf writes a formatted string to the underlying writer using [CodeFormatter.Fprintf].

func (*Writer) Reserve

func (w *Writer) Reserve(name string) bool

Reserve marks a name as used in the namespace of the writer.

func (*Writer) Sprintf

func (w *Writer) Sprintf(format string, args ...any) string

Sprintf creates a formatted string using [CodeFormatter.Sprintf].

func (*Writer) WithBuf

func (w *Writer) WithBuf(buf io.Writer) *Writer

WithBuf copies the writer and sets a new write buffer.

func (*Writer) WithNS

func (w *Writer) WithNS(ns NS) *Writer

WithNS copies the writer and sets a new namespace.

func (*Writer) Write

func (w *Writer) Write(p []byte) (int, error)

Write implements io.Writer.

Jump to

Keyboard shortcuts

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