1
0
mirror of https://github.com/golang/go synced 2024-11-18 21:44:45 -07:00

internal/lsp/source: always use default goimports options

No point in constructing the defaults in three places.

Change-Id: I2b0776910a933a7250245bd82dc27e63c34df18a
Reviewed-on: https://go-review.googlesource.com/c/tools/+/212632
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Heschi Kreinick 2019-12-27 14:07:33 -05:00
parent 50c778fb86
commit 0a57c09236
3 changed files with 17 additions and 36 deletions

View File

@ -171,7 +171,7 @@ func (v *view) Config(ctx context.Context) *packages.Config {
} }
} }
func (v *view) RunProcessEnvFunc(ctx context.Context, fn func(*imports.Options) error, opts *imports.Options) error { func (v *view) RunProcessEnvFunc(ctx context.Context, fn func(*imports.Options) error) error {
v.mu.Lock() v.mu.Lock()
defer v.mu.Unlock() defer v.mu.Unlock()
if v.processEnv == nil { if v.processEnv == nil {
@ -187,7 +187,17 @@ func (v *view) RunProcessEnvFunc(ctx context.Context, fn func(*imports.Options)
} }
// Run the user function. // Run the user function.
opts.Env = v.processEnv opts := &imports.Options{
// Defaults.
AllErrors: true,
Comments: true,
Fragment: true,
FormatOnly: false,
TabIndent: true,
TabWidth: 8,
Env: v.processEnv,
}
if err := fn(opts); err != nil { if err := fn(opts); err != nil {
return err return err
} }

View File

@ -98,19 +98,10 @@ func AllImportsFixes(ctx context.Context, snapshot Snapshot, fh FileHandle) (all
if hasListErrors(pkg) { if hasListErrors(pkg) {
return nil, nil, errors.Errorf("%s has list errors, not running goimports", fh.Identity().URI) return nil, nil, errors.Errorf("%s has list errors, not running goimports", fh.Identity().URI)
} }
options := &imports.Options{
// Defaults.
AllErrors: true,
Comments: true,
Fragment: true,
FormatOnly: false,
TabIndent: true,
TabWidth: 8,
}
err = snapshot.View().RunProcessEnvFunc(ctx, func(opts *imports.Options) error { err = snapshot.View().RunProcessEnvFunc(ctx, func(opts *imports.Options) error {
allFixEdits, editsPerFix, err = computeImportEdits(ctx, snapshot.View(), pgh, opts) allFixEdits, editsPerFix, err = computeImportEdits(ctx, snapshot.View(), pgh, opts)
return err return err
}, options) })
if err != nil { if err != nil {
return nil, nil, errors.Errorf("computing fix edits: %v", err) return nil, nil, errors.Errorf("computing fix edits: %v", err)
} }
@ -317,23 +308,13 @@ func CandidateImports(ctx context.Context, prefix string, view View, filename st
ctx, done := trace.StartSpan(ctx, "source.CandidateImports") ctx, done := trace.StartSpan(ctx, "source.CandidateImports")
defer done() defer done()
options := &imports.Options{
// Defaults.
AllErrors: true,
Comments: true,
Fragment: true,
FormatOnly: false,
TabIndent: true,
TabWidth: 8,
}
var imps []imports.ImportFix var imps []imports.ImportFix
importFn := func(opts *imports.Options) error { importFn := func(opts *imports.Options) error {
var err error var err error
imps, err = imports.GetAllCandidates(ctx, prefix, filename, opts) imps, err = imports.GetAllCandidates(ctx, prefix, filename, opts)
return err return err
} }
err := view.RunProcessEnvFunc(ctx, importFn, options) err := view.RunProcessEnvFunc(ctx, importFn)
return imps, err return imps, err
} }
@ -343,23 +324,13 @@ func PackageExports(ctx context.Context, view View, pkg, filename string) ([]imp
ctx, done := trace.StartSpan(ctx, "source.PackageExports") ctx, done := trace.StartSpan(ctx, "source.PackageExports")
defer done() defer done()
options := &imports.Options{
// Defaults.
AllErrors: true,
Comments: true,
Fragment: true,
FormatOnly: false,
TabIndent: true,
TabWidth: 8,
}
var pkgs []imports.PackageExport var pkgs []imports.PackageExport
importFn := func(opts *imports.Options) error { importFn := func(opts *imports.Options) error {
var err error var err error
pkgs, err = imports.GetPackageExports(ctx, pkg, filename, opts) pkgs, err = imports.GetPackageExports(ctx, pkg, filename, opts)
return err return err
} }
err := view.RunProcessEnvFunc(ctx, importFn, options) err := view.RunProcessEnvFunc(ctx, importFn)
return pkgs, err return pkgs, err
} }

View File

@ -110,9 +110,9 @@ type View interface {
// Config returns the configuration for the view. // Config returns the configuration for the view.
Config(ctx context.Context) *packages.Config Config(ctx context.Context) *packages.Config
// RunProcessEnvFunc runs fn with the process env for this view inserted into opts. // RunProcessEnvFunc runs fn with the process env for this view.
// Note: the process env contains cached module and filesystem state. // Note: the process env contains cached module and filesystem state.
RunProcessEnvFunc(ctx context.Context, fn func(*imports.Options) error, opts *imports.Options) error RunProcessEnvFunc(ctx context.Context, fn func(*imports.Options) error) error
// Options returns a copy of the Options for this view. // Options returns a copy of the Options for this view.
Options() Options Options() Options