imports

package
v0.3.8 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2023 License: BSD-3-Clause Imports: 29 Imported by: 0

Documentation

Overview

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

Index

Constants

View Source
const MaxRelevance = 7.0

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

Variables

This section is empty.

Functions

func GetAllCandidates

func GetAllCandidates(ctx context.Context, wrapped func(ImportFix), searchPrefix, filename, filePkg string, env *ProcessEnv) error

GetAllCandidates calls wrapped for each package whose name starts with searchPrefix, and can be imported from filename with the package name filePkg.

Beware that the wrapped function may be called multiple times concurrently. TODO(adonovan): encapsulate the concurrency.

func ImportPathToAssumedName

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 Process

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

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

func VendorlessPath

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".

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

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 ModuleResolver

type ModuleResolver struct {
	// 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.

	// LocalPrefix is a comma-separated string of import path prefixes, which, if
	// set, instructs Process to sort the import paths with the given prefixes
	// into another group after 3rd-party packages.
	LocalPrefix string

	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

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

A PackageExport is a package and its exports.

type ProcessEnv

type ProcessEnv struct {
	GocmdRunner *gocommand.Runner

	BuildFlags []string
	ModFlag    string
	ModFile    string

	// SkipPathInScan returns true if the path should be skipped from scans of
	// the RootCurrentModule root type. The function argument is a clean,
	// absolute path.
	SkipPathInScan func(string) bool

	// Env overrides the OS environment, and can be used to specify
	// GOPROXY, GO111MODULE, etc. PATH cannot be set here, because
	// exec.Command will not honor it.
	// Specifying all of RequiredGoEnvVars avoids a call to `go env`.
	Env map[string]string

	WorkingDir string

	// If Logf is non-nil, debug logging is enabled through this function.
	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

func (e *ProcessEnv) CopyConfig() *ProcessEnv

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

func (*ProcessEnv) GetResolver

func (e *ProcessEnv) GetResolver() (Resolver, error)

type Resolver

type Resolver interface {
	// contains filtered or unexported methods
}

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

Jump to

Keyboard shortcuts

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