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