gnofmt

package
v0.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2026 License: UNKNOWN not legal advice Imports: 0 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrPackageConflict = errors.New("package name conflict")

ErrPackageConflict is returned when a directory contains non-test .gno files with different package names (e.g. filetest directories).

Functions

func ReadWalkPackage

func ReadWalkPackage(pkg Package, fn PackageReadWalkFunc) error

Types

type FSResolver

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

func NewFSResolver

func NewFSResolver() *FSResolver

func (*FSResolver) LoadPackages

func (r *FSResolver) LoadPackages(root string, pkgHandler PackageHandler) error

LoadPackages lists all packages in the directory (excluding those which can't be processed).

func (*FSResolver) ResolveName

func (r *FSResolver) ResolveName(pkgname string) []Package

func (*FSResolver) ResolvePath

func (r *FSResolver) ResolvePath(pkgpath string) Package

type Package

type Package interface {
	// Should return the package path
	Path() string
	// Should return the name of the package as defined at the top level of each file
	Name() string
	// Should return all gno filenames inside the package
	Files() []string
	// Should return a content reader for the given filename within the package
	Read(filename string) (io.ReadCloser, error)
}

func ParsePackage

func ParsePackage(fset *token.FileSet, root string, dir string) (Package, error)

ParsePackage parses package from the given directory. It will return a nil package if no gno files are found. If a gnomod.toml is found, it will be used to determine the pkg path. If root is specified, it will be trimmed from the actual given dir to create the pkgpath if no gnomod.toml is found.

type PackageHandler

type PackageHandler func(path string, err error) error

PackageHandler is a callback passed to the resolver during package loading. PackageHandler will be called on each package. If no error is passed, that means that the package has been fully loaded. If any handled error is returned from the handler, the package process will immediately stop.

type PackageReadWalkFunc

type PackageReadWalkFunc func(filename string, r io.Reader, err error) error

type Processor

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

func NewProcessor

func NewProcessor(r Resolver) *Processor

func (*Processor) FormatFile

func (p *Processor) FormatFile(file string) ([]byte, error)

FormatFile processes a single Gno file from the given file path, resolving its imports against the other .gno files in the same directory (parsed as a single package).

If those files declare conflicting package names — as filetest directories such as gnovm/tests/files/ do by design — they cannot be parsed as one package and FormatFile returns ErrPackageConflict (wrapped). Callers that format such independent files must detect them up front, by path or suffix, and call FormatImportFromSource (or FormatSource, to leave imports untouched) directly instead (see cmd/gno/fmt.go).

Known limitation: directories whose files share a consistent package name but are semantically independent (e.g. a directory of `package main` filetests, each with its own func main) parse cleanly as a single package and so are not caught here. Import resolution then pools top-level declarations across every file: a symbol used in file A but declared in (unrelated) file B is treated as already resolved, so the import A needs may be dropped (and imports A already had may be pruned). Layout is still formatted correctly per file; only the import list can be wrong. Such directories must be routed to per-file formatting by the caller too.

func (*Processor) FormatImportFromSource

func (p *Processor) FormatImportFromSource(filename string, src any) ([]byte, error)

FormatImportFromSource parse and format the source from src. The type of the argument for the src parameter must be string, []byte, or io.Reader.

func (*Processor) FormatPackageFile

func (p *Processor) FormatPackageFile(pkg Package, filename string) ([]byte, error)

FormatPackageFile processes a single Gno file from the given Package and filename.

func (*Processor) FormatSource

func (p *Processor) FormatSource(filename string, src any) ([]byte, error)

FormatSource parses and formats the source from src for layout only: it reformats the code and sorts existing import groups, but never resolves, adds, or prunes imports. The type of the argument for the src parameter must be string, []byte, or io.Reader.

Use this when a file's imports are intentional and must be preserved verbatim — e.g. a filetest that expects a compile error and deliberately leaves a symbol unimported. FormatImportFromSource, by contrast, resolves imports against the resolver.

type Resolver

type Resolver interface {
	// ResolveName should resolve the given package name by returning a list
	// of packages matching the given name
	ResolveName(pkgname string) []Package
	// ResolvePath should resolve the given package path by returning a
	// single package
	ResolvePath(pkgpath string) Package
}

Jump to

Keyboard shortcuts

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