1// Package imports implements a Go pretty-printer (like package "go/format")
2// that also adds or removes import statements as necessary.
3package imports // import "golang.org/x/tools/imports"
4
5import (
6	"go/build"
7
8	intimp "golang.org/x/tools/internal/imports"
9)
10
11// Options specifies options for processing files.
12type Options struct {
13	Fragment  bool // Accept fragment of a source file (no package statement)
14	AllErrors bool // Report all errors (not just the first 10 on different lines)
15
16	Comments  bool // Print comments (true if nil *Options provided)
17	TabIndent bool // Use tabs for indent (true if nil *Options provided)
18	TabWidth  int  // Tab width (8 if nil *Options provided)
19
20	FormatOnly bool // Disable the insertion and deletion of imports
21}
22
23// Debug controls verbose logging.
24var Debug = false
25
26// LocalPrefix is a comma-separated string of import path prefixes, which, if
27// set, instructs Process to sort the import paths with the given prefixes
28// into another group after 3rd-party packages.
29var LocalPrefix string
30
31// Process formats and adjusts imports for the provided file.
32// If opt is nil the defaults are used.
33//
34// Note that filename's directory influences which imports can be chosen,
35// so it is important that filename be accurate.
36// To process data ``as if'' it were in filename, pass the data as a non-nil src.
37func Process(filename string, src []byte, opt *Options) ([]byte, error) {
38	if opt == nil {
39		opt = &Options{Comments: true, TabIndent: true, TabWidth: 8}
40	}
41	intopt := &intimp.Options{
42		Env: &intimp.ProcessEnv{
43			GOPATH:      build.Default.GOPATH,
44			GOROOT:      build.Default.GOROOT,
45			Debug:       Debug,
46			LocalPrefix: LocalPrefix,
47		},
48		AllErrors:  opt.AllErrors,
49		Comments:   opt.Comments,
50		FormatOnly: opt.FormatOnly,
51		Fragment:   opt.Fragment,
52		TabIndent:  opt.TabIndent,
53		TabWidth:   opt.TabWidth,
54	}
55	return intimp.Process(filename, src, intopt)
56}
57
58// VendorlessPath returns the devendorized version of the import path ipath.
59// For example, VendorlessPath("foo/bar/vendor/a/b") returns "a/b".
60func VendorlessPath(ipath string) string {
61	return intimp.VendorlessPath(ipath)
62}
63