tools: golang.org/x/tools/internal/imports Index | Files

package imports

import "golang.org/x/tools/internal/imports"

Package imports implements a Go pretty-printer (like package "go/format") that also adds or removes import statements as necessary.

Index

Package Files

fix.go imports.go mod.go mod_cache.go sortimports.go zstdlib.go

Constants

const MaxRelevance = 7

Highest relevance, used for the standard library. Chosen arbitrarily to match pre-existing gopls code.

func ApplyFixes Uses

func ApplyFixes(fixes []*ImportFix, filename string, src []byte, opt *Options, extraMode parser.Mode) (formatted []byte, err error)

ApplyFixes applies all of the fixes to the file and formats it. extraMode is added in when parsing the file.

func GetAllCandidates Uses

func GetAllCandidates(ctx context.Context, callback func(ImportFix), searchPrefix, filename, filePkg string, opt *Options) error

GetAllCandidates gets all of the packages starting with prefix that can be imported by filename, sorted by import path.

func GetPackageExports Uses

func GetPackageExports(ctx context.Context, callback func(PackageExport), searchPkg, filename, filePkg string, opt *Options) error

GetPackageExports returns all known packages with name pkg and their exports.

func ImportPathToAssumedName Uses

func ImportPathToAssumedName(importPath string) string

ImportPathToAssumedName returns the assumed package name of an import path. It does this using only string parsing of the import path. It picks the last element of the path that does not look like a major version, and then picks the valid identifier off the start of that element. It is used to determine if a local rename should be added to an import for clarity. This function could be moved to a standard package and exported if we want for use in other tools.

func PrimeCache Uses

func PrimeCache(ctx context.Context, env *ProcessEnv) error

func Process Uses

func Process(filename string, src []byte, opt *Options) (formatted []byte, err error)

Process implements golang.org/x/tools/imports.Process with explicit context in env.

func ScoreImportPaths Uses

func ScoreImportPaths(ctx context.Context, env *ProcessEnv, paths []string) map[string]int

func VendorlessPath Uses

func VendorlessPath(ipath string) string

VendorlessPath returns the devendorized version of the import path ipath. For example, VendorlessPath("foo/bar/vendor/a/b") returns "a/b".

type ImportFix Uses

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
    Relevance int // see pkg
}

func FixImports Uses

func FixImports(filename string, src []byte, opt *Options) (fixes []*ImportFix, err error)

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 Uses

type ImportFixType int
const (
    AddImport ImportFixType = iota
    DeleteImport
    SetImportName
)

type ImportInfo Uses

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 Uses

type ModuleJSON struct {
    Path      string      // module path
    Replace   *ModuleJSON // replaced by this module
    Main      bool        // is this the main module?
    Indirect  bool        // is this module only an indirect dependency of 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 Uses

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

ModuleResolver implements resolver for modules using the go command as little as feasible.

func (*ModuleResolver) ClearForNewMod Uses

func (r *ModuleResolver) ClearForNewMod()

func (*ModuleResolver) ClearForNewScan Uses

func (r *ModuleResolver) ClearForNewScan()

type Options Uses

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)

    FormatOnly bool // Disable the insertion and deletion of imports
}

Options is golang.org/x/tools/imports.Options with extra internal-only options.

type PackageExport Uses

type PackageExport struct {
    Fix     *ImportFix
    Exports []string
}

A PackageExport is a package and its exports.

type ProcessEnv Uses

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

    // 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) CopyConfig Uses

func (e *ProcessEnv) CopyConfig() *ProcessEnv

CopyConfig copies the env's configuration into a new env.

func (*ProcessEnv) GetResolver Uses

func (e *ProcessEnv) GetResolver() Resolver

type Resolver Uses

type Resolver interface {
    ClearForNewScan()
    // contains filtered or unexported methods
}

A Resolver does the build-system-specific parts of goimports.

Package imports imports 31 packages (graph) and is imported by 11 packages. Updated 2020-01-19. Refresh now. Tools for package owners.