Skip to content

Commit

Permalink
deps: upgrade to gopls and x/tools 5e752206 (#584)
Browse files Browse the repository at this point in the history
Includes changes to govim to:

* handle type changes in code generated LSP API types
* define "CompletionBudget" config value for use in integration tests
* remove skip from scenario_default/format_on_save_new_file script
* add scenario_default/format_on_save_new_file_existing_package to
  verify the addition of a new file to an existing package
* make changes to various scripts to handle the fact that gopls now only
  sends diagnostics for a file when they have changed

gopls and x/tools changes:

* internal/lsp: don't clear file contents on save 5e752206
* internal/lsp/cache: detach context before invalidation 145a1e40
* internal/lsp/cache: remove errors from dependencies 041a08a5
* internal/lsp/cache: consolidate function to update overlays 85a33566
* internal/imports: consider direct mod deps more relevant 62b96743
* internal/lsp: eliminate source.File type and move GetFile to snapshot 2208e167
* internal/lsp: put verbose go/packages output behind verboseOutput flag 56b0b28a
* internal/lsp: fix highlighting of inexact matches when highlighting from the right bdebc07e
* internal/lps/protocol: bring code.ts up to date with latest changes. a98e7fcf
* telemetry/log: sample reference for benchmarking harness 2ad5dca7
* internal/lsp: add gopls setting to disable use of -modfile flag behavior 3aa5a364
* go/packages: change import cycle errorkind from UnknownError to ListError 4f29fbcc
* internal/lsp/cache: fix excessive recursion in (*snapshot).clone() 979b82bf
* internal/lsp/cache: don't forget files just because they change 84f0c7cf
* all: burn all the evidence ac281547
* internal/lsp: return snapshots from text modifications ca0407e6
* internal/lsp/source: fix completion budget type 41c101f3
* internal/lsp: show comments for grouped variable declarations 61483d10
* blog: fix swallowed error 61f5e7d2
* internal/lsp/cache: move overlay and debug handling into separate files d270ebf9
* internal/lsp/cache: don't invalidate dependents' metadata bc4a8d39
* internal/lsp/debug: print the debug port 6f9e13bb
* internal/lsp: use the -modfile flag to update a different go.mod file 62a96288
* cmd/fiximports: fix 'go list' error formatting 210e553f
* internal/lsp: fix links for interface methods 38570b76
* internal/lsp: kill some dead code 64005949
* internal/lsp: change completionBudget to milliseconds f2a421a9
* internal/imports: set the Dir field on the build.Context (instead of WorkingDir) if present 01c78d57
* fix: using section links to IDE specific doc 0b436227
* gopls: add status and roadmap to the main readme. bbbf87ae
* internal/lsp: fix link anchors for struct fields 42268a1f
* internal/lsp: expose option to disable timeouts for completion c39ce214
* imports: pass more of the environment 621d4eef
* internal/lsp/source: fix crash on conflict markers in imports 846828e8
* internal/lsp: change CompletionItem.{Command,TextEdit} to pointers 1d943b09
* internal/lsp/cache: consolidate snapshot cloning 4981f6b3
* internal/gopathwalk/walk: add missing function descriptions and renaming 99b39703
* benchmark/parse: add unit test for String function bcb65d52
* internal/lsp: fix find-implementation for promoted methods a6aac22f
* go/analysis: gofmt snippets in package documentation 53d48bfc
* internal/lsp, internal/tool: clean up command line of gopls a0e659d5
* internal/lsp: remove useless signature cast completion candidate 49a3e744
* internal/lsp: skip return highlighting if cursor is in arglist of func 04c2e8ef
* internal/lsp: create parseModHandle for storing go.mod data 56463cc1
* internal/lsp: add diagnostic on import causing import cycle 7ebc6af0
* internal/lsp/source: fix error message for multiple GOPATHs 74e303f8
* internal/lsp/tests: add error handling for @item() 27b81bef
* go/packages: correct logging of driver stderr 7093a17b
* internal/lsp: offer basic type conversion candidates 0f69de23
* internal/lsp: improve link in documentation on hover cfcbc779
* go/packages: disable -mod=readonly behavior in overlays 8facea2e
* internal/lsp: downrank "nil" completion candidate 825cb062
* internal/lsp: don't resend diagnostics if they are unchanged 56eb7d2c
* internal/lsp: move DidModifyFile into internal/lsp/cache 4403f798
* internal/lsp: fix find-references to search from all package variants dfebd280
* internal/lsp: add handling for go.mod files in internal/lsp functions ad473c03
* internal/lsp: invalidate metadata and type info more selectively 4da4485a
* internal/lsp/source: fix all types in resolveInvalid ec14b296
* internal/lsp: invalidate workspace packages when go.mod file changes ac2db28e
* internal/lsp: check all package variants in find-implementations a27fdba2
* internal/lsp: fix circular import errors to account for import stack 37853707
* go/packages: add import stack to error messages when there's an import cycle 0d087302
* internal/lsp: don't invalidate dependents' metadata 912f50ad
* internal/lsp: add comment completions for exported vars 98df1237
* internal/lsp: suggest "fallthrough" only inside switches fe93f4a7
* internal/lsp: Make Text in DidSave at *string rather than a string 1bcf67c9
* internal/lsp: make Text in DidSave request a pointer 0bd90eac
* internal/lsp/cache: invalidate metadata even without Create 22774f7d
* internal/lsp: fix control flow highlighting taking precedence 115af5e8
* internal/lsp: trim down implementations code 9a30a9a9
* internal/lsp: tweak implementation tests 259af5ff
* internal/lsp: fix error suppression in (*session).createView bc369361
* internal/lsp/source: optimize computeFixEdits 6d582d50
* internal/lsp/source: cap number of unimported completions 330b9f13
* internal/lsp: respect References.IncludeDeclaration setting 786dcba0
* internal/lsp: add error handling for self imports cec95805
* internal/lsp: fix concurrent map write in file invalidation db903f39
* internal/lsp: propagate and handle context cancellation errors 3393d29b
* internal/lsp/cache: clean up dead code after CL 209737 7b8c8591
* internal/lsp: fix golden generation for import tests addffd16
* internal/lsp: expose godoc or pkg.go.dev link on hover 69111344
* internal/memoize: fix race on read of handle.function 61fa4dff
* go/packages: remove obsolete comment about LoadMode in doc.go e140590b
* internal/lsp: refactor and clean up text synchronization d1f10d1c
* internal/lsp: return snapshot when creating a view a5887330
* internal/lsp: fix AST bookkeeping as we repair nodes 73c7173a
* internal/lsp/cmd: use x/tools/gopls as the gopls bug prefix e84277c2
* internal/lsp: propagate errors through source.DocumentSymbols 7a2a8a04
* internal/lsp: run packages.Load only if imports are added or changed ac417207
* go/packages: revert "handle invalid files in overlays" 427c522c
* internal/lsp: always ParseFull in-workspace dependencies d79e56da
* go/analysis/internal/checker: format files modified by -fix 0d967eff
* internal/lsp/source: extract helper, improve error messages 660eba4d
* internal/lsp/cache: fix load race, refactor 9611592c
* gopls/integration: remove commented-out debug code b1451cf3
* gopls/integration: add the replay command to replay LSP logs c197fd4b
* internal/links: improve links parser, no protocol specification db047d72
* playground/socket: handle multi-file present play snippets 8db96347
* internal/lsp: improve completion after accidental keywords 5ae4576c
* internal/lsp: add nil check for control flow highlighting 2b6af5f9
* internal/imports: make ApplyFixes work despite syntax errors 5a103c92
* internal/lsp: add control flow highlighting for functions aa29eadb
* internal/lsp/protocol: replace code for generating LSP types and stubs 713d4893
* internal/lsp: suppress all errors when a view is loaded and checked ffc413ea
* internal/lsp: rename CheckPackageHandle to PackageHandle a51b8faf
* internal/lsp: link to the new pkg.go.dev instead of godoc.org 6e064ea0
* internal/lsp: make sure CodeAction.Command is a pointer ecd32218
* lsp/protocol: make sure RenameProvider is an interface{} c1736c0f
* internal/telemetry: lift the tests up to the request level 99399511
* gopls/doc: add documentation for gopls with Atom 724660f1
* internal/telemtry: changed to a simpler threading model for stats 7360bd5c
* internal/lsp: build the wire.Node lazily 9fe613bd
* internal/lsp: add some minimal validation for client capabilities 69a79c76
* internal/lsp: fixes premature return in find implementations e13f15d1
* internal/lsp: remove duplicate implementations when finding implementations af857703
* tools/gopls: add cmd support for highlight 20805546
* cmd/gopls/integration: remove old VS Code extension for debugging b832835c
* internal/telemetry: delay the conversion of metrics fc82735a
* godoc/static: re-add "or" function call to package.html template b0650ceb
* internal/lsp: don't run analyses on the entire view 73cd2cc3
* internal/lsp/source: fix crash on short file 9f1eb440
* internal/lsp/tests: skip cgo tests when not supported efa86633
* internal/lsp: do not return interface itself when finding implementations 73db347c
* internal/lsp/source: give more imports names 66af5afb
* tools/gopls: add cmd support for implementation 59b2cd78
* internal/lsp: fix return variables for FindPosInPackage b99b2d09
* internal/lsp: set version correctly after textDocument/didOpen d7101b74
* internal/lsp: add tests for cgo package users 05c67e95
* internal/lsp: track and parse non-compiled go files ef6787d3
* internal/span: support line directives 761dbfd6
* internal/lsp: add documentation for package-level vars acc15743
* internal/lsp: fix literal completions in variadic params a99e43fc
* internal/lsp: only search for references in reverse dependencies a911d900
* internal/telemetry: make the metrics tests external 89d49d94
* internal/lsp: use AST to construct placeholders cc15fab3
* internal/telemetry: merge the metrics tests f5828fc9
* internal/telemetry: compare the compact JSON 2a6ccf25
* internal/lsp: handle first change behavior on the server side c02aa52d
* internal/lsp/cache: disable analysis on dependencies (temporarily) 2189885d
* internal/lsp: improve completion ordering workaround 035a8167
* internal/lsp: fix a race in the command line tests 0ae87fff
* internal/telemetry/export/ocagent: update usages of BucketOptions to use pointers f774e2e2
* internal/telemetry/export/ocagent: add tutorial for exporting metrics df8e87c2
* internal/telemetry/export/ocagent: correctly JSON marshal BucketOptionsExplicit 2d3bb8ce
* internal/lsp: use snapshot to get reverse dependencies f191eec9
* internal/lsp: delete the source.Diagnostic.File field 004141db
* cmd/godoc: delete unused autocert hooks f51c1a7c
* internal/lsp: add control flow highlighting inside for loops 66c5a5ad
* cmd/godoc: don't execute go list -m all when GOMOD is /dev/null 91381dc0
* internal/lsp: use version numbers in diagnostic messages eaeb3832
* internal/lsp: don't clear diagnostics on file close cb050682
* internal/telemetry/export/ocagent: correctly marshal points to JSON 96ad48e4
* internal/lsp: fix diagnostics on didChange 82924fac
* internal/lsp: make the ocagent test external 947d4aa8
* internal/telemetry: delay the conversion to wire format 9c44060b
* internal/lsp: reorganize and refactor code 35ba81b9
* internal/lsp: rename Files to CompiledGoFiles 8fd45951
* internal: avoid use of (*token.File).Name 328c41bf
* internal/lsp: fix and re-enable godef tests 4bf2f406
* txtar/archive: add unit test function for Format 17847a84
* cmd/godoc, godoc/static: remove remnants of golang.org website 688c506a
* internal/lsp: make Range a pointer in Change events cefbc64f
* internal/lsp: improve highlighting functionality for fields and methods 0a33398b
* cmd/digraph: omit unnecessary empty lines in focus b3340bdf
* cmd/digraph: add focus e97fc272
* internal/lsp: run diagnostics on the entire workspace ad01d599
* internal/lsp: handle breakage caused by CL 207598 7f7817c0
* txtar: promote from internal 298f0cb1
* internal/lsp: only construct a cache when we need to caaa49c6
* go/analysis/passes/lostcancel: fix typo 4054736f
* all: fix typo ("identifer" → "identifier") dc59f20e
* internal/lsp/cmd: don't create .orig files for rename by default 6d480e29
* internal/lsp: fix the return type of CodeAction() 11e13f1c
* internal/lsp: fix infinite recursion in CancelRequest 07fc4c7f
* go/packages: add String methods for LoadMode 3d1ebf6f
* internal/lsp/protocol: fix type of WorkspaceEdit.Changes in tsprotocol.go 13f8be5f
* go/analysis/passes: ensure diagnostic-reporting analyses have exported Doc 1ef994f2
* internal/lsp: don't search other packages for unexported references faf0e61b
* internal/lsp: reorganize the generated Go code for the lsp protocol 5091d647
* internal/lsp: fix variadic interface completion ranking 5a76f03b
* internal/lsp: fix panic in bestView 80313e1b
* internal/lsp: fix panic when logging errors in snapshot.KnownPackages 3a792d9c
* cmd/godoc: don't execute go mod download when GOMOD is /dev/null 1c71899d
* internal/lsp/testadata/rename: add a test case for renames across packages 4191b8cb
* internal/lsp: ignore AST errors when generating import edits 634482ed
* internal/lsp/source: add support for references in the same workspace caa0b0f7
* internal/lsp: use import path literal for documentLink range a764947c
* internal/lsp/cache: add finer-grained control of file changes faa69481
* cmd/benchcmp: add unit test for formatNs function bd32a3e7
* internal/lsp: use the versions provided by the client e2727e81
  • Loading branch information
myitcv committed Dec 20, 2019
1 parent 3376e91 commit 29ba8dc
Show file tree
Hide file tree
Showing 175 changed files with 8,022 additions and 5,247 deletions.
5 changes: 5 additions & 0 deletions autoload/govim/config.vim
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ function! s:validGoImportsLocalPrefix(v)
return s:validString(a:v)
endfunction

function! s:validCompletionBudget(v)
return s:validString(a:v)
endfunction

function! s:validExperimentalMouseTriggeredHoverPopupOptions(v)
if has_key(a:v, "line")
if type(a:v["line"]) != 0
Expand Down Expand Up @@ -116,6 +120,7 @@ let s:validators = {
\ "CompletionCaseSensitive": function("s:validCompletionCaseSensitive"),
\ "CompleteUnimported": function("s:validCompleteUnimported"),
\ "GoImportsLocalPrefix": function("s:validGoImportsLocalPrefix"),
\ "CompletionBudget": function("s:validCompletionBudget"),
\ "ExperimentalMouseTriggeredHoverPopupOptions": function("s:validExperimentalMouseTriggeredHoverPopupOptions"),
\ "ExperimentalCursorTriggeredHoverPopupOptions": function("s:validExperimentalCursorTriggeredHoverPopupOptions"),
\ }
1 change: 0 additions & 1 deletion cmd/govim/buffer_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ func (v *vimstate) bufWritePost(args ...json.RawMessage) error {
TextDocumentIdentifier: cb.ToTextDocumentIdentifier(),
Version: float64(cb.Version),
},
Text: string(cb.Contents()),
}
if err := v.server.DidSave(context.Background(), params); err != nil {
return fmt.Errorf("failed to call gopls.DidSave on %v: %v", cb.Name, err)
Expand Down
7 changes: 7 additions & 0 deletions cmd/govim/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ type Config struct {
// comma-separated list
GoImportsLocalPrefix *string `json:",omitempty"`

// CompletionBudget is the soft latency string-format time.Duration goal for
// gopls completion requests. Most requests finish in a couple milliseconds,
// but in some cases deep completions can take much longer. As we use up our
// budget we dynamically reduce the search scope to ensure we return timely
// results. Zero seconds means unlimited. Examples values: "0s", "100ms"
CompletionBudget *string `json:",omitempty"`

// ExperimentalMouseTriggeredHoverPopupOptions is a map of options to apply
// when creating hover-based popup windows triggered by the mouse hovering
// over an identifier. It corresponds to the second argument to popup_create
Expand Down
3 changes: 3 additions & 0 deletions cmd/govim/config/gen_applygen.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ func (r *Config) Apply(v *Config) {
if v.GoImportsLocalPrefix != nil {
r.GoImportsLocalPrefix = v.GoImportsLocalPrefix
}
if v.CompletionBudget != nil {
r.CompletionBudget = v.CompletionBudget
}
if v.ExperimentalMouseTriggeredHoverPopupOptions != nil {
r.ExperimentalMouseTriggeredHoverPopupOptions = v.ExperimentalMouseTriggeredHoverPopupOptions
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/govim/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,9 @@ func (v *vimstate) formatBufferRange(b *types.Buffer, mode config.FormatOnSave,
v.Logf("gopls.CodeAction returned an error; nothing to do")
return nil
}

var organizeImports []protocol.CodeAction
// We might get other kinds in the response, like QuickFix for example.
// They will be handled via issue #510 (add/enable support for suggested fixes)
var organizeImports []protocol.CodeAction
for _, action := range actions {
if action.Kind == protocol.SourceOrganizeImports {
organizeImports = append(organizeImports, action)
Expand Down
7 changes: 6 additions & 1 deletion cmd/govim/gopls_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
goplsCaseSensitiveCompletion = "caseSensitiveCompletion"
goplsCompleteUnimported = "completeUnimported"
goplsGoImportsLocalPrefix = "local"
goplsCompletionBudget = "completionBudget"
)

var _ protocol.Client = (*govimplugin)(nil)
Expand Down Expand Up @@ -87,7 +88,8 @@ func (g *govimplugin) WorkspaceFolders(context.Context) ([]protocol.WorkspaceFol
defer absorbShutdownErr()
panic("not implemented yet")
}
func (g *govimplugin) Configuration(ctxt context.Context, params *protocol.ParamConfig) ([]interface{}, error) {

func (g *govimplugin) Configuration(ctxt context.Context, params *protocol.ParamConfiguration) ([]interface{}, error) {
defer absorbShutdownErr()

// TODO this is a rather fragile workaround for https://github.com/golang/go/issues/35817
Expand Down Expand Up @@ -144,6 +146,9 @@ func (g *govimplugin) Configuration(ctxt context.Context, params *protocol.Param
if g.vimstate.config.GoImportsLocalPrefix != nil {
conf[goplsGoImportsLocalPrefix] = *g.vimstate.config.GoImportsLocalPrefix
}
if g.vimstate.config.CompletionBudget != nil {
conf[goplsCompletionBudget] = *g.vimstate.config.CompletionBudget
}
res[0] = conf

g.logGoplsClientf("Configuration response: %v", pretty.Sprint(res))
Expand Down
6 changes: 3 additions & 3 deletions cmd/govim/gopls_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (l loggingGoplsServer) Logf(format string, args ...interface{}) {
l.g.Logf("gopls server start =======================\n"+format+"gopls server end =======================\n", args...)
}

func (l loggingGoplsServer) Initialize(ctxt context.Context, params *protocol.ParamInitia) (*protocol.InitializeResult, error) {
func (l loggingGoplsServer) Initialize(ctxt context.Context, params *protocol.ParamInitialize) (*protocol.InitializeResult, error) {
l.Logf("gopls.Initialize() call; params:\n%v", pretty.Sprint(params))
res, err := l.u.Initialize(ctxt, params)
l.Logf("gopls.Initialize() return; err: %v; res:\n%v", err, pretty.Sprint(res))
Expand Down Expand Up @@ -280,7 +280,7 @@ func (l loggingGoplsServer) FoldingRange(ctxt context.Context, params *protocol.
return res, err
}

func (l loggingGoplsServer) Declaration(ctxt context.Context, params *protocol.DeclarationParams) ([]protocol.DeclarationLink, error) {
func (l loggingGoplsServer) Declaration(ctxt context.Context, params *protocol.DeclarationParams) (protocol.Declaration, error) {
l.Logf("gopls.Declaration() call; params:\n%v", pretty.Sprint(params))
res, err := l.u.Declaration(ctxt, params)
l.Logf("gopls.Declaration() return; err: %v; res\n%v%v", err, pretty.Sprint(res))
Expand All @@ -294,7 +294,7 @@ func (l loggingGoplsServer) LogTraceNotification(ctxt context.Context, params *p
return err
}

func (l loggingGoplsServer) PrepareRename(ctxt context.Context, params *protocol.PrepareRenameParams) (*protocol.Range, error) {
func (l loggingGoplsServer) PrepareRename(ctxt context.Context, params *protocol.PrepareRenameParams) (interface{}, error) {
l.Logf("gopls.PrepareRename() call; params:\n%v", pretty.Sprint(params))
res, err := l.u.PrepareRename(ctxt, params)
l.Logf("gopls.PrepareRename() return; err: %v; res\n%v", err, pretty.Sprint(res))
Expand Down
5 changes: 4 additions & 1 deletion cmd/govim/internal/golang_org_x_tools/gopathwalk/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func WalkSkip(roots []Root, add func(root Root, dir string), skip func(root Root
}
}

// walkDir creates a walker and starts fastwalk with this walker.
func walkDir(root Root, add func(Root, string), skip func(root Root, dir string) bool, opts Options) {
if _, err := os.Stat(root.Path); os.IsNotExist(err) {
if opts.Debug {
Expand Down Expand Up @@ -114,7 +115,7 @@ type walker struct {
ignoredDirs []os.FileInfo // The ignored directories, loaded from .goimportsignore files.
}

// init initializes the walker based on its Options.
// init initializes the walker based on its Options
func (w *walker) init() {
var ignoredPaths []string
if w.root.Type == RootModuleCache {
Expand Down Expand Up @@ -167,6 +168,7 @@ func (w *walker) getIgnoredDirs(path string) []string {
return ignoredDirs
}

// shouldSkipDir reports whether the file should be skipped or not.
func (w *walker) shouldSkipDir(fi os.FileInfo, dir string) bool {
for _, ignoredDir := range w.ignoredDirs {
if os.SameFile(fi, ignoredDir) {
Expand All @@ -180,6 +182,7 @@ func (w *walker) shouldSkipDir(fi os.FileInfo, dir string) bool {
return false
}

// walk walks through the given path.
func (w *walker) walk(path string, typ os.FileMode) error {
dir := filepath.Dir(path)
if typ.IsRegular() {
Expand Down
26 changes: 17 additions & 9 deletions cmd/govim/internal/golang_org_x_tools/imports/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ func (p *pass) importIdentifier(imp *ImportInfo) string {
if known != nil && known.name != "" {
return known.name
}
return importPathToAssumedName(imp.ImportPath)
return ImportPathToAssumedName(imp.ImportPath)
}

// load reads in everything necessary to run a pass, and reports whether the
Expand Down Expand Up @@ -435,7 +435,7 @@ func (p *pass) importSpecName(imp *ImportInfo) string {
}

ident := p.importIdentifier(imp)
if ident == importPathToAssumedName(imp.ImportPath) {
if ident == ImportPathToAssumedName(imp.ImportPath) {
return "" // ident not needed since the assumed and real names are the same.
}
return ident
Expand Down Expand Up @@ -644,7 +644,7 @@ func getCandidatePkgs(pkgName, filename string, env *ProcessEnv) ([]*pkg, error)
}

func candidateImportName(pkg *pkg) string {
if importPathToAssumedName(pkg.importPathShort) != pkg.packageName {
if ImportPathToAssumedName(pkg.importPathShort) != pkg.packageName {
return pkg.packageName
}
return ""
Expand Down Expand Up @@ -784,12 +784,20 @@ func (e *ProcessEnv) buildContext() *build.Context {
ctx.GOROOT = e.GOROOT
ctx.GOPATH = e.GOPATH

// As of Go 1.14, build.Context has a WorkingDir field
// As of Go 1.14, build.Context has a Dir field
// (see golang.org/issue/34860).
// Populate it only if present.
if wd := reflect.ValueOf(&ctx).Elem().FieldByName("WorkingDir"); wd.IsValid() && wd.Kind() == reflect.String {
wd.SetString(e.WorkingDir)
rc := reflect.ValueOf(&ctx).Elem()
dir := rc.FieldByName("Dir")
if !dir.IsValid() {
// Working drafts of Go 1.14 named the field "WorkingDir" instead.
// TODO(bcmills): Remove this case after the Go 1.14 beta has been released.
dir = rc.FieldByName("WorkingDir")
}
if dir.IsValid() && dir.Kind() == reflect.String {
dir.SetString(e.WorkingDir)
}

return &ctx
}

Expand Down Expand Up @@ -884,7 +892,7 @@ func (r *goPackagesResolver) loadPackageNames(importPaths []string, srcDir strin
if _, ok := names[path]; ok {
continue
}
names[path] = importPathToAssumedName(path)
names[path] = ImportPathToAssumedName(path)
}
return names, nil

Expand Down Expand Up @@ -1006,15 +1014,15 @@ func notIdentifier(ch rune) bool {
ch >= utf8.RuneSelf && (unicode.IsLetter(ch) || unicode.IsDigit(ch)))
}

// importPathToAssumedName returns the assumed package name of an import path.
// ImportPathToAssumedName returns the assumed package name of an import path.
// It does this using only string parsing of the import path.
// It picks the last element of the path that does not look like a major
// version, and then picks the valid identifier off the start of that element.
// It is used to determine if a local rename should be added to an import for
// clarity.
// This function could be moved to a standard package and exported if we want
// for use in other tools.
func importPathToAssumedName(importPath string) string {
func ImportPathToAssumedName(importPath string) string {
base := path.Base(importPath)
if strings.HasPrefix(base, "v") {
if _, err := strconv.Atoi(base[1:]); err == nil {
Expand Down
31 changes: 24 additions & 7 deletions cmd/govim/internal/golang_org_x_tools/imports/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io"
"io/ioutil"
"log"
"os"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -83,23 +84,35 @@ func FixImports(filename string, src []byte, opt *Options) (fixes []*ImportFix,
return getFixes(fileSet, file, filename, opt.Env)
}

// 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) {
// ApplyFixes applies all of the fixes to the file and formats it. extraMode
// is added in when parsing the file.
func ApplyFixes(fixes []*ImportFix, filename string, src []byte, opt *Options, extraMode parser.Mode) (formatted []byte, err error) {
src, opt, err = initialize(filename, src, opt)
if err != nil {
return nil, err
}

// Don't use parse() -- we don't care about fragments or statement lists
// here, and we need to work with unparseable files.
fileSet := token.NewFileSet()
file, adjust, err := parse(fileSet, filename, src, opt)
if err != nil {
parserMode := parser.Mode(0)
if opt.Comments {
parserMode |= parser.ParseComments
}
if opt.AllErrors {
parserMode |= parser.AllErrors
}
parserMode |= extraMode

file, err := parser.ParseFile(fileSet, filename, src, parserMode)
if file == nil {
return nil, err
}

// Apply the fixes to the file.
apply(fileSet, file, fixes)

return formatFile(fileSet, file, src, adjust, opt)
return formatFile(fileSet, file, src, nil, opt)
}

// GetAllCandidates gets all of the standard library candidate packages to import in
Expand Down Expand Up @@ -133,8 +146,12 @@ func initialize(filename string, src []byte, opt *Options) ([]byte, *Options, er
// Set the env if the user has not provided it.
if opt.Env == nil {
opt.Env = &ProcessEnv{
GOPATH: build.Default.GOPATH,
GOROOT: build.Default.GOROOT,
GOPATH: build.Default.GOPATH,
GOROOT: build.Default.GOROOT,
GOFLAGS: os.Getenv("GOFLAGS"),
GO111MODULE: os.Getenv("GO111MODULE"),
GOPROXY: os.Getenv("GOPROXY"),
GOSUMDB: os.Getenv("GOSUMDB"),
}
}

Expand Down
9 changes: 7 additions & 2 deletions cmd/govim/internal/golang_org_x_tools/imports/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type ModuleJSON struct {
Path string // module path
Replace *ModuleJSON // replaced by this module
Main bool // is this the main module?
Indirect bool // is this module only an indirect dependency of main module?
Dir string // directory holding files for this module, if any
GoMod string // path to go.mod file for this module, if any
GoVersion string // go version used in module
Expand Down Expand Up @@ -433,10 +434,14 @@ func (r *ModuleResolver) canonicalize(info directoryPackageInfo) (*pkg, error) {
}

importPath := info.nonCanonicalImportPath
relevance := 2
relevance := 3
// Check if the directory is underneath a module that's in scope.
if mod := r.findModuleByDir(info.dir); mod != nil {
relevance = 1
if mod.Indirect {
relevance = 2
} else {
relevance = 1
}
// It is. If dir is the target of a replace directive,
// our guessed import path is wrong. Use the real one.
if mod.Dir == info.dir {
Expand Down
Loading

0 comments on commit 29ba8dc

Please sign in to comment.