internal/lsp/source: export FindFileInpackage

And delete a copy of it.

Change-Id: Ice7b932327dbfe5e00f1d084fc6669f1e4059afe
Reviewed-on: https://go-review.googlesource.com/c/tools/+/218320
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 2020-02-06 15:43:39 -05:00
parent 8bb641bf89
commit 009580c43b
3 changed files with 8 additions and 28 deletions

View File

@ -177,7 +177,7 @@ func toSourceErrorKind(kind packages.ErrorKind) source.ErrorKind {
func typeErrorRange(ctx context.Context, fset *token.FileSet, pkg *pkg, pos token.Pos) (span.Span, error) {
posn := fset.Position(pos)
ph, _, err := findFileInPackage(pkg, span.FileURI(posn.Filename))
ph, _, err := source.FindFileInPackage(pkg, span.FileURI(posn.Filename))
if err != nil {
return span.Span{}, err
}
@ -213,7 +213,7 @@ func typeErrorRange(ctx context.Context, fset *token.FileSet, pkg *pkg, pos toke
}
func scannerErrorRange(ctx context.Context, fset *token.FileSet, pkg *pkg, posn token.Position) (span.Span, error) {
ph, _, err := findFileInPackage(pkg, span.FileURI(posn.Filename))
ph, _, err := source.FindFileInPackage(pkg, span.FileURI(posn.Filename))
if err != nil {
return span.Span{}, err
}
@ -232,7 +232,7 @@ func scannerErrorRange(ctx context.Context, fset *token.FileSet, pkg *pkg, posn
// spanToRange converts a span.Span to a protocol.Range,
// assuming that the span belongs to the package whose diagnostics are being computed.
func spanToRange(ctx context.Context, pkg *pkg, spn span.Span) (protocol.Range, error) {
ph, _, err := findFileInPackage(pkg, spn.URI())
ph, _, err := source.FindFileInPackage(pkg, spn.URI())
if err != nil {
return protocol.Range{}, err
}
@ -243,27 +243,6 @@ func spanToRange(ctx context.Context, pkg *pkg, spn span.Span) (protocol.Range,
return m.Range(spn)
}
func findFileInPackage(pkg source.Package, uri span.URI) (source.ParseGoHandle, source.Package, error) {
queue := []source.Package{pkg}
seen := make(map[string]bool)
for len(queue) > 0 {
pkg := queue[0]
queue = queue[1:]
seen[pkg.ID()] = true
if f, err := pkg.File(uri); err == nil {
return f, pkg, nil
}
for _, dep := range pkg.Imports() {
if !seen[dep.ID()] {
queue = append(queue, dep)
}
}
}
return nil, nil, errors.Errorf("no file for %s in package %s", uri, pkg.ID())
}
// parseGoListError attempts to parse a standard `go list` error message
// by stripping off the trailing error message.
//

View File

@ -89,7 +89,7 @@ func (i *IdentifierInfo) linkAndSymbolName() (string, string) {
// package's API). This is true if the request originated in a test package,
// and if the declaration is also found in the same test package.
if i.pkg != nil && obj.Pkg() != nil && i.pkg.ForTest() != "" {
if _, pkg, _ := findFileInPackage(i.pkg, i.Declaration.URI()); i.pkg == pkg {
if _, pkg, _ := FindFileInPackage(i.pkg, i.Declaration.URI()); i.pkg == pkg {
return "", ""
}
}

View File

@ -637,7 +637,7 @@ func findPosInPackage(v View, searchpkg Package, pos token.Pos) (*ast.File, Pack
if v.Ignore(uri) {
ph, err = findIgnoredFile(v, uri)
} else {
ph, pkg, err = findFileInPackage(searchpkg, uri)
ph, pkg, err = FindFileInPackage(searchpkg, uri)
}
if err != nil {
return nil, nil, err
@ -661,7 +661,7 @@ func findMapperInPackage(v View, searchpkg Package, uri span.URI) (*protocol.Col
if v.Ignore(uri) {
ph, err = findIgnoredFile(v, uri)
} else {
ph, _, err = findFileInPackage(searchpkg, uri)
ph, _, err = FindFileInPackage(searchpkg, uri)
}
if err != nil {
return nil, err
@ -681,7 +681,8 @@ func findIgnoredFile(v View, uri span.URI) (ParseGoHandle, error) {
return v.Session().Cache().ParseGoHandle(fh, ParseFull), nil
}
func findFileInPackage(pkg Package, uri span.URI) (ParseGoHandle, Package, error) {
// FindFileInPackage finds uri in pkg or its dependencies.
func FindFileInPackage(pkg Package, uri span.URI) (ParseGoHandle, Package, error) {
queue := []Package{pkg}
seen := make(map[string]bool)