blob: d2547c7433822575e310d227c1433540642c6caf [file] [log] [blame]
Rebecca Stambler92778472021-01-05 23:05:35 -05001// 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 Kreinick757ca712019-05-06 15:37:46 -04005// Package imports implements a Go pretty-printer (like package "go/format")
6// that also adds or removes import statements as necessary.
7package imports // import "golang.org/x/tools/imports"
8
9import (
Heschi Kreinick416e8f42020-07-06 15:18:50 -040010 "io/ioutil"
Heschi Kreinick5bcca832020-02-28 17:03:27 -050011 "log"
Heschi Kreinick757ca712019-05-06 15:37:46 -040012
Heschi Kreinick416e8f42020-07-06 15:18:50 -040013 "golang.org/x/tools/internal/gocommand"
Heschi Kreinick757ca712019-05-06 15:37:46 -040014 intimp "golang.org/x/tools/internal/imports"
15)
16
17// Options specifies options for processing files.
18type 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.
30var 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.
35var LocalPrefix string
36
37// Process formats and adjusts imports for the provided file.
Heschi Kreinick416e8f42020-07-06 15:18:50 -040038// If opt is nil the defaults are used, and if src is nil the source
39// is read from the filesystem.
Heschi Kreinick757ca712019-05-06 15:37:46 -040040//
41// Note that filename's directory influences which imports can be chosen,
42// so it is important that filename be accurate.
Russ Coxd5f48fc2022-04-11 23:03:04 -040043// To process data “as if” it were in filename, pass the data as a non-nil src.
Heschi Kreinick757ca712019-05-06 15:37:46 -040044func Process(filename string, src []byte, opt *Options) ([]byte, error) {
Heschi Kreinick416e8f42020-07-06 15:18:50 -040045 var err error
46 if src == nil {
47 src, err = ioutil.ReadFile(filename)
48 if err != nil {
49 return nil, err
50 }
51 }
Heschi Kreinick26647e32019-05-20 18:00:48 -040052 if opt == nil {
53 opt = &Options{Comments: true, TabIndent: true, TabWidth: 8}
54 }
Heschi Kreinick757ca712019-05-06 15:37:46 -040055 intopt := &intimp.Options{
56 Env: &intimp.ProcessEnv{
Heschi Kreinick416e8f42020-07-06 15:18:50 -040057 GocmdRunner: &gocommand.Runner{},
Heschi Kreinick757ca712019-05-06 15:37:46 -040058 },
Heschi Kreinick416e8f42020-07-06 15:18:50 -040059 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 Kreinick757ca712019-05-06 15:37:46 -040066 }
Heschi Kreinick5bcca832020-02-28 17:03:27 -050067 if Debug {
68 intopt.Env.Logf = log.Printf
69 }
Heschi Kreinick757ca712019-05-06 15:37:46 -040070 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".
75func VendorlessPath(ipath string) string {
76 return intimp.VendorlessPath(ipath)
77}