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