Skip to content

Commit d36a54b

Browse files
committed
internal/lsp: improve package search in a couple places
When we open a file in a package, independent of whether it is in the workspace, we type check in ParseFull mode. However, several other code paths don't find this better parse mode. We need a better abstraction, but for now improve a couple code paths specifically for the purpose of fixing Hover content. Updates golang/go#46158 Updates golang/go#46902 Change-Id: I34c0432fdba406d569ea963ab4366336068767a2 Reviewed-on: https://go-review.googlesource.com/c/tools/+/333689 Trust: Robert Findley <[email protected]> Run-TryBot: Robert Findley <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Rebecca Stambler <[email protected]>
1 parent 3844600 commit d36a54b

File tree

5 files changed

+31
-4
lines changed

5 files changed

+31
-4
lines changed

internal/lsp/cache/check.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,8 +539,10 @@ func doTypeCheck(ctx context.Context, snapshot *snapshot, m *metadata, mode sour
539539
for _, cgf := range pkg.compiledGoFiles {
540540
files = append(files, cgf.File)
541541
}
542+
542543
// Type checking errors are handled via the config, so ignore them here.
543544
_ = check.Files(files)
545+
544546
// If the context was cancelled, we may have returned a ton of transient
545547
// errors to the type checker. Swallow them.
546548
if ctx.Err() != nil {

internal/lsp/cache/pkg.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ func (p *pkg) PkgPath() string {
6161
return string(p.m.pkgPath)
6262
}
6363

64+
func (p *pkg) ParseMode() source.ParseMode {
65+
return p.mode
66+
}
67+
6468
func (p *pkg) CompiledGoFiles() []*source.ParsedGoFile {
6569
return p.compiledGoFiles
6670
}

internal/lsp/source/identifier.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ func Identifier(ctx context.Context, snapshot Snapshot, fh FileHandle, pos proto
8585
return nil, fmt.Errorf("no packages for file %v", fh.URI())
8686
}
8787
sort.Slice(pkgs, func(i, j int) bool {
88+
// Prefer packages with a more complete parse mode.
89+
if pkgs[i].ParseMode() != pkgs[j].ParseMode() {
90+
return pkgs[i].ParseMode() > pkgs[j].ParseMode()
91+
}
8892
return len(pkgs[i].CompiledGoFiles()) < len(pkgs[j].CompiledGoFiles())
8993
})
9094
var findErr error

internal/lsp/source/util.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,19 +274,35 @@ func CompareDiagnostic(a, b *Diagnostic) int {
274274
return 1
275275
}
276276

277-
// FindPackageFromPos finds the parsed file for a position in a given search
278-
// package.
277+
// FindPackageFromPos finds the first package containing pos in its
278+
// type-checked AST.
279279
func FindPackageFromPos(ctx context.Context, snapshot Snapshot, pos token.Pos) (Package, error) {
280280
tok := snapshot.FileSet().File(pos)
281281
if tok == nil {
282282
return nil, errors.Errorf("no file for pos %v", pos)
283283
}
284284
uri := span.URIFromPath(tok.Name())
285-
pkgs, err := snapshot.PackagesForFile(ctx, uri, TypecheckWorkspace)
285+
// Search all packages: some callers may be working with packages not
286+
// type-checked in workspace mode.
287+
pkgs, err := snapshot.PackagesForFile(ctx, uri, TypecheckAll)
286288
if err != nil {
287289
return nil, err
288290
}
289-
return pkgs[0], nil
291+
// Only return the package if it actually type-checked the given position.
292+
for _, pkg := range pkgs {
293+
parsed, err := pkg.File(uri)
294+
if err != nil {
295+
return nil, err
296+
}
297+
if parsed == nil {
298+
continue
299+
}
300+
if parsed.Tok.Base() != tok.Base() {
301+
continue
302+
}
303+
return pkg, nil
304+
}
305+
return nil, errors.Errorf("no package for given file position")
290306
}
291307

292308
// findFileInDeps finds uri in pkg or its dependencies.

internal/lsp/source/view.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,7 @@ type Package interface {
581581
Version() *module.Version
582582
HasListOrParseErrors() bool
583583
HasTypeErrors() bool
584+
ParseMode() ParseMode
584585
}
585586

586587
type CriticalError struct {

0 commit comments

Comments
 (0)