internal/imports: return initialized options
Whent the pointer is nil, changing its value does not fix the returned
options. Return the new pointer so it can be used.
Change-Id: Ie17fe5c47b48b4a77ffb17b974a5c90e3b44df5e
Reviewed-on: https://go-review.googlesource.com/c/tools/+/189998
Run-TryBot: Suzy Mueller <suzmue@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
diff --git a/internal/imports/imports.go b/internal/imports/imports.go
index e466dfb..82d02f0 100644
--- a/internal/imports/imports.go
+++ b/internal/imports/imports.go
@@ -44,7 +44,7 @@
// Process implements golang.org/x/tools/imports.Process with explicit context in env.
func Process(filename string, src []byte, opt *Options) (formatted []byte, err error) {
- src, err = initialize(filename, src, opt)
+ src, opt, err = initialize(filename, src, opt)
if err != nil {
return nil, err
}
@@ -69,7 +69,7 @@
// Note that filename's directory influences which imports can be chosen,
// so it is important that filename be accurate.
func FixImports(filename string, src []byte, opt *Options) (fixes []*ImportFix, err error) {
- src, err = initialize(filename, src, opt)
+ src, opt, err = initialize(filename, src, opt)
if err != nil {
return nil, err
}
@@ -85,7 +85,7 @@
// ApplyFix will apply all of the fixes to the file and format it.
func ApplyFixes(fixes []*ImportFix, filename string, src []byte, opt *Options) (formatted []byte, err error) {
- src, err = initialize(filename, src, opt)
+ src, opt, err = initialize(filename, src, opt)
if err != nil {
return nil, err
}
@@ -105,7 +105,7 @@
// initialize sets the values for opt and src.
// If they are provided, they are not changed. Otherwise opt is set to the
// default values and src is read from the file system.
-func initialize(filename string, src []byte, opt *Options) ([]byte, error) {
+func initialize(filename string, src []byte, opt *Options) ([]byte, *Options, error) {
// Use defaults if opt is nil.
if opt == nil {
opt = &Options{Comments: true, TabIndent: true, TabWidth: 8}
@@ -127,12 +127,12 @@
if src == nil {
b, err := ioutil.ReadFile(filename)
if err != nil {
- return nil, err
+ return nil, nil, err
}
src = b
}
- return src, nil
+ return src, opt, nil
}
func formatFile(fileSet *token.FileSet, file *ast.File, src []byte, adjust func(orig []byte, src []byte) []byte, opt *Options) ([]byte, error) {