Documentation ¶
Overview ¶
Package imports implements a Go pretty-printer (like package "go/format") that also adds or removes import statements as necessary.
Index ¶
- func ApplyFixes(fixes []*ImportFix, filename string, src []byte, opt *Options) (formatted []byte, err error)
- func Process(filename string, src []byte, opt *Options) (formatted []byte, err error)
- func VendorlessPath(ipath string) string
- type ImportFix
- type ImportFixType
- type ImportInfo
- type ModuleJSON
- type ModuleResolver
- type Options
- type PackageExport
- type ProcessEnv
- type Resolver
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ApplyFixes ¶
func ApplyFixes(fixes []*ImportFix, filename string, src []byte, opt *Options) (formatted []byte, err error)
ApplyFix will apply all of the fixes to the file and format it.
func VendorlessPath ¶
VendorlessPath returns the devendorized version of the import path ipath. For example, VendorlessPath("foo/bar/vendor/a/b") returns "a/b".
Types ¶
type ImportFix ¶
type ImportFix struct { // StmtInfo represents the import statement this fix will add, remove, or change. StmtInfo ImportInfo // IdentName is the identifier that this fix will add or remove. IdentName string // FixType is the type of fix this is (AddImport, DeleteImport, SetImportName). FixType ImportFixType }
func FixImports ¶
FixImports returns a list of fixes to the imports that, when applied, will leave the imports in the same state as Process.
Note that filename's directory influences which imports can be chosen, so it is important that filename be accurate.
type ImportFixType ¶
type ImportFixType int
const ( AddImport ImportFixType = iota DeleteImport SetImportName )
type ImportInfo ¶
type ImportInfo struct { ImportPath string // import path, e.g. "crypto/rand". Name string // import name, e.g. "crand", or "" if none. }
An ImportInfo represents a single import statement.
type ModuleJSON ¶
type ModuleJSON struct { Path string // module path Replace *ModuleJSON // replaced by this module Main bool // is this the main module? Dir string // directory holding files for this module, if any GoMod string // path to go.mod file for this module, if any GoVersion string // go version used in module }
type ModuleResolver ¶
type ModuleResolver struct { Initialized bool Main *ModuleJSON ModsByModPath []*ModuleJSON // All modules, ordered by # of path components in module Path... ModsByDir []*ModuleJSON // ...or Dir. // contains filtered or unexported fields }
ModuleResolver implements resolver for modules using the go command as little as feasible.
func (*ModuleResolver) ClearForNewMod ¶
func (r *ModuleResolver) ClearForNewMod()
func (*ModuleResolver) ClearForNewScan ¶
func (r *ModuleResolver) ClearForNewScan()
type Options ¶
type Options struct { Env *ProcessEnv // The environment to use. Note: this contains the cached module and filesystem state. Fragment bool // Accept fragment of a source file (no package statement) AllErrors bool // Report all errors (not just the first 10 on different lines) Comments bool // Print comments (true if nil *Options provided) TabIndent bool // Use tabs for indent (true if nil *Options provided) TabWidth int // Tab width (8 if nil *Options provided) SquashGroups bool // Squash existing groups before formatting Gofumpt bool // Use gofumpt instead of gofmt FormatOnly bool // Disable the insertion and deletion of imports }
Options is golang.org/x/tools/imports.Options with extra internal-only options.
type PackageExport ¶
A PackageExport is a package and its exports.
func GetPackageExports ¶
func GetPackageExports(pkg, filename string, opt *Options) (exports []PackageExport, err error)
GetPackageExports returns all known packages with name pkg and their exports.
type ProcessEnv ¶
type ProcessEnv struct { LocalPrefix string Debug bool // If non-empty, these will be used instead of the // process-wide values. GOPATH, GOROOT, GO111MODULE, GOPROXY, GOFLAGS, GOSUMDB string WorkingDir string // If true, use go/packages regardless of the environment. ForceGoPackages bool // Logf is the default logger for the ProcessEnv. Logf func(format string, args ...interface{}) // contains filtered or unexported fields }
ProcessEnv contains environment variables and settings that affect the use of the go command, the go/build package, etc.
func (*ProcessEnv) GetResolver ¶
func (e *ProcessEnv) GetResolver() Resolver