2019-04-24 09:33:45 -06:00
|
|
|
// Copyright 2018 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package lsp
|
|
|
|
|
|
|
|
import (
|
2020-02-18 13:47:38 -07:00
|
|
|
"bytes"
|
2019-04-24 09:33:45 -06:00
|
|
|
"context"
|
2019-11-28 00:00:44 -07:00
|
|
|
"fmt"
|
2019-07-07 16:25:19 -06:00
|
|
|
"go/ast"
|
|
|
|
"go/token"
|
2020-01-10 00:06:12 -07:00
|
|
|
"net/url"
|
2019-12-24 13:58:48 -07:00
|
|
|
"regexp"
|
2019-04-24 09:33:45 -06:00
|
|
|
"strconv"
|
2020-02-11 08:12:40 -07:00
|
|
|
"strings"
|
2019-12-24 13:58:48 -07:00
|
|
|
"sync"
|
2019-04-24 09:33:45 -06:00
|
|
|
|
2020-04-07 22:49:08 -06:00
|
|
|
"golang.org/x/mod/modfile"
|
2020-04-17 07:32:56 -06:00
|
|
|
"golang.org/x/tools/internal/event"
|
2020-04-07 22:49:08 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/debug/tag"
|
2019-04-24 09:33:45 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
2019-07-07 16:25:19 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
2019-04-24 09:33:45 -06:00
|
|
|
"golang.org/x/tools/internal/span"
|
|
|
|
)
|
|
|
|
|
2020-04-07 22:49:08 -06:00
|
|
|
func (s *Server) documentLink(ctx context.Context, params *protocol.DocumentLinkParams) (links []protocol.DocumentLink, err error) {
|
2020-07-02 16:34:10 -06:00
|
|
|
snapshot, fh, ok, release, err := s.beginFileRequest(ctx, params.TextDocument.URI, source.UnknownKind)
|
|
|
|
defer release()
|
2020-02-13 11:46:49 -07:00
|
|
|
if !ok {
|
|
|
|
return nil, err
|
2019-12-10 09:51:34 -07:00
|
|
|
}
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
switch fh.Kind() {
|
2020-02-18 13:47:38 -07:00
|
|
|
case source.Mod:
|
2020-04-07 22:49:08 -06:00
|
|
|
links, err = modLinks(ctx, snapshot, fh)
|
2020-02-18 13:47:38 -07:00
|
|
|
case source.Go:
|
2020-07-21 13:15:06 -06:00
|
|
|
links, err = goLinks(ctx, snapshot, fh)
|
2020-02-18 13:47:38 -07:00
|
|
|
}
|
2020-04-07 22:49:08 -06:00
|
|
|
// Don't return errors for document links.
|
|
|
|
if err != nil {
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
event.Error(ctx, "failed to compute document links", err, tag.URI.Of(fh.URI()))
|
2020-04-07 22:49:08 -06:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return links, nil
|
2020-02-18 13:47:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func modLinks(ctx context.Context, snapshot source.Snapshot, fh source.FileHandle) ([]protocol.DocumentLink, error) {
|
2020-02-13 11:46:49 -07:00
|
|
|
view := snapshot.View()
|
2020-02-18 13:47:38 -07:00
|
|
|
|
2020-07-24 11:39:58 -06:00
|
|
|
pm, err := snapshot.ParseMod(ctx, fh)
|
2020-02-18 13:47:38 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var links []protocol.DocumentLink
|
2020-07-24 11:39:58 -06:00
|
|
|
for _, req := range pm.File.Require {
|
2020-07-27 11:44:38 -06:00
|
|
|
if req.Syntax == nil {
|
|
|
|
continue
|
|
|
|
}
|
2020-06-15 20:21:06 -06:00
|
|
|
// See golang/go#36998: don't link to modules matching GOPRIVATE.
|
|
|
|
if snapshot.View().IsGoPrivatePath(req.Mod.Path) {
|
|
|
|
continue
|
|
|
|
}
|
2020-02-18 13:47:38 -07:00
|
|
|
dep := []byte(req.Mod.Path)
|
|
|
|
s, e := req.Syntax.Start.Byte, req.Syntax.End.Byte
|
2020-07-24 11:39:58 -06:00
|
|
|
i := bytes.Index(pm.Mapper.Content[s:e], dep)
|
2020-02-18 13:47:38 -07:00
|
|
|
if i == -1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Shift the start position to the location of the
|
|
|
|
// dependency within the require statement.
|
|
|
|
start, end := token.Pos(s+i), token.Pos(s+i+len(dep))
|
|
|
|
target := fmt.Sprintf("https://%s/mod/%s", view.Options().LinkTarget, req.Mod.String())
|
2020-07-24 11:39:58 -06:00
|
|
|
l, err := toProtocolLink(view, pm.Mapper, target, start, end, source.Mod)
|
2020-03-30 10:45:15 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-02-18 13:47:38 -07:00
|
|
|
}
|
2020-03-30 10:45:15 -06:00
|
|
|
links = append(links, l)
|
2020-02-18 13:47:38 -07:00
|
|
|
}
|
2020-03-30 10:45:15 -06:00
|
|
|
// TODO(ridersofrohan): handle links for replace and exclude directives.
|
2020-07-24 11:39:58 -06:00
|
|
|
if syntax := pm.File.Syntax; syntax == nil {
|
2020-02-18 13:47:38 -07:00
|
|
|
return links, nil
|
|
|
|
}
|
|
|
|
// Get all the links that are contained in the comments of the file.
|
2020-07-24 11:39:58 -06:00
|
|
|
for _, expr := range pm.File.Syntax.Stmt {
|
2020-02-18 13:47:38 -07:00
|
|
|
comments := expr.Comment()
|
|
|
|
if comments == nil {
|
|
|
|
continue
|
|
|
|
}
|
2020-04-07 22:49:08 -06:00
|
|
|
for _, section := range [][]modfile.Comment{comments.Before, comments.Suffix, comments.After} {
|
|
|
|
for _, comment := range section {
|
2020-07-24 11:39:58 -06:00
|
|
|
l, err := findLinksInString(ctx, view, comment.Token, token.Pos(comment.Start.Byte), pm.Mapper, source.Mod)
|
2020-04-07 22:49:08 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
links = append(links, l...)
|
|
|
|
}
|
2020-02-18 13:47:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return links, nil
|
|
|
|
}
|
|
|
|
|
2020-07-21 13:15:06 -06:00
|
|
|
func goLinks(ctx context.Context, snapshot source.Snapshot, fh source.FileHandle) ([]protocol.DocumentLink, error) {
|
|
|
|
view := snapshot.View()
|
2020-07-22 09:32:32 -06:00
|
|
|
pkgs, err := snapshot.PackagesForFile(ctx, fh.URI())
|
2020-02-11 08:12:40 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-22 09:32:32 -06:00
|
|
|
pkg, err := source.WidestPackage(pkgs)
|
2020-02-11 08:12:40 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-21 13:15:06 -06:00
|
|
|
pgf, err := snapshot.ParseGo(ctx, fh, source.ParseFull)
|
2019-09-17 09:19:11 -06:00
|
|
|
if err != nil {
|
2019-07-11 19:05:55 -06:00
|
|
|
return nil, err
|
2019-05-17 11:45:50 -06:00
|
|
|
}
|
2020-04-07 22:49:08 -06:00
|
|
|
var imports []*ast.ImportSpec
|
|
|
|
var str []*ast.BasicLit
|
2020-07-21 13:15:06 -06:00
|
|
|
ast.Inspect(pgf.File, func(node ast.Node) bool {
|
2019-07-07 16:25:19 -06:00
|
|
|
switch n := node.(type) {
|
|
|
|
case *ast.ImportSpec:
|
2020-04-07 22:49:08 -06:00
|
|
|
imports = append(imports, n)
|
2019-07-07 16:25:19 -06:00
|
|
|
return false
|
|
|
|
case *ast.BasicLit:
|
2020-01-10 00:06:12 -07:00
|
|
|
// Look for links in string literals.
|
|
|
|
if n.Kind == token.STRING {
|
2020-04-07 22:49:08 -06:00
|
|
|
str = append(str, n)
|
2019-07-07 16:25:19 -06:00
|
|
|
}
|
|
|
|
return false
|
2019-04-24 09:33:45 -06:00
|
|
|
}
|
2019-07-07 16:25:19 -06:00
|
|
|
return true
|
|
|
|
})
|
2020-04-07 22:49:08 -06:00
|
|
|
var links []protocol.DocumentLink
|
2020-07-13 21:18:12 -06:00
|
|
|
// For import specs, provide a link to a documentation website, like
|
|
|
|
// https://pkg.go.dev.
|
|
|
|
if view.Options().ImportShortcut.ShowLinks() {
|
|
|
|
for _, imp := range imports {
|
|
|
|
target, err := strconv.Unquote(imp.Path.Value)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// See golang/go#36998: don't link to modules matching GOPRIVATE.
|
|
|
|
if view.IsGoPrivatePath(target) {
|
|
|
|
continue
|
|
|
|
}
|
2020-07-22 09:32:32 -06:00
|
|
|
if mod, version, ok := moduleAtVersion(ctx, snapshot, target, pkg); ok && strings.ToLower(view.Options().LinkTarget) == "pkg.go.dev" {
|
2020-07-13 21:18:12 -06:00
|
|
|
target = strings.Replace(target, mod, mod+"@"+version, 1)
|
|
|
|
}
|
|
|
|
// Account for the quotation marks in the positions.
|
|
|
|
start := imp.Path.Pos() + 1
|
|
|
|
end := imp.Path.End() - 1
|
|
|
|
target = fmt.Sprintf("https://%s/%s", view.Options().LinkTarget, target)
|
2020-07-21 13:15:06 -06:00
|
|
|
l, err := toProtocolLink(view, pgf.Mapper, target, start, end, source.Go)
|
2020-07-13 21:18:12 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
links = append(links, l)
|
2020-04-07 22:49:08 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, s := range str {
|
2020-07-21 13:15:06 -06:00
|
|
|
l, err := findLinksInString(ctx, view, s.Value, s.Pos(), pgf.Mapper, source.Go)
|
2020-04-07 22:49:08 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
links = append(links, l...)
|
|
|
|
}
|
2020-07-21 13:15:06 -06:00
|
|
|
for _, commentGroup := range pgf.File.Comments {
|
2019-07-07 16:25:19 -06:00
|
|
|
for _, comment := range commentGroup.List {
|
2020-07-21 13:15:06 -06:00
|
|
|
l, err := findLinksInString(ctx, view, comment.Text, comment.Pos(), pgf.Mapper, source.Go)
|
2020-04-07 22:49:08 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
links = append(links, l...)
|
2019-04-24 09:33:45 -06:00
|
|
|
}
|
2019-07-07 16:25:19 -06:00
|
|
|
}
|
|
|
|
return links, nil
|
|
|
|
}
|
|
|
|
|
2020-07-22 09:32:32 -06:00
|
|
|
func moduleAtVersion(ctx context.Context, snapshot source.Snapshot, target string, pkg source.Package) (string, string, bool) {
|
2020-02-11 08:12:40 -07:00
|
|
|
impPkg, err := pkg.GetImport(target)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", false
|
|
|
|
}
|
|
|
|
if impPkg.Module() == nil {
|
|
|
|
return "", "", false
|
|
|
|
}
|
|
|
|
version, modpath := impPkg.Module().Version, impPkg.Module().Path
|
|
|
|
if modpath == "" || version == "" {
|
|
|
|
return "", "", false
|
|
|
|
}
|
|
|
|
return modpath, version, true
|
|
|
|
}
|
|
|
|
|
2020-04-07 22:49:08 -06:00
|
|
|
func findLinksInString(ctx context.Context, view source.View, src string, pos token.Pos, m *protocol.ColumnMapper, fileKind source.FileKind) ([]protocol.DocumentLink, error) {
|
2019-07-11 19:05:55 -06:00
|
|
|
var links []protocol.DocumentLink
|
2019-12-24 13:58:48 -07:00
|
|
|
for _, index := range view.Options().URLRegexp.FindAllIndex([]byte(src), -1) {
|
|
|
|
start, end := index[0], index[1]
|
|
|
|
startPos := token.Pos(int(pos) + start)
|
|
|
|
endPos := token.Pos(int(pos) + end)
|
2020-04-07 22:49:08 -06:00
|
|
|
link := src[start:end]
|
|
|
|
linkURL, err := url.Parse(link)
|
|
|
|
// Fallback: Linkify IP addresses as suggested in golang/go#18824.
|
2019-12-24 13:58:48 -07:00
|
|
|
if err != nil {
|
2020-04-07 22:49:08 -06:00
|
|
|
linkURL, err = url.Parse("//" + link)
|
|
|
|
// Not all potential links will be valid, so don't return this error.
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2020-01-10 00:06:12 -07:00
|
|
|
}
|
|
|
|
// If the URL has no scheme, use https.
|
2020-04-07 22:49:08 -06:00
|
|
|
if linkURL.Scheme == "" {
|
|
|
|
linkURL.Scheme = "https"
|
2020-01-10 00:06:12 -07:00
|
|
|
}
|
2020-04-07 22:49:08 -06:00
|
|
|
l, err := toProtocolLink(view, m, linkURL.String(), startPos, endPos, fileKind)
|
2020-01-10 00:06:12 -07:00
|
|
|
if err != nil {
|
2020-04-07 22:49:08 -06:00
|
|
|
return nil, err
|
2019-12-24 13:58:48 -07:00
|
|
|
}
|
|
|
|
links = append(links, l)
|
|
|
|
}
|
|
|
|
// Handle golang/go#1234-style links.
|
|
|
|
r := getIssueRegexp()
|
|
|
|
for _, index := range r.FindAllIndex([]byte(src), -1) {
|
|
|
|
start, end := index[0], index[1]
|
2019-07-11 19:05:55 -06:00
|
|
|
startPos := token.Pos(int(pos) + start)
|
|
|
|
endPos := token.Pos(int(pos) + end)
|
2019-12-24 13:58:48 -07:00
|
|
|
matches := r.FindStringSubmatch(src)
|
|
|
|
if len(matches) < 4 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
org, repo, number := matches[1], matches[2], matches[3]
|
|
|
|
target := fmt.Sprintf("https://github.com/%s/%s/issues/%s", org, repo, number)
|
2020-02-18 13:47:38 -07:00
|
|
|
l, err := toProtocolLink(view, m, target, startPos, endPos, fileKind)
|
2019-07-11 19:05:55 -06:00
|
|
|
if err != nil {
|
2020-04-07 22:49:08 -06:00
|
|
|
return nil, err
|
2019-07-11 19:05:55 -06:00
|
|
|
}
|
|
|
|
links = append(links, l)
|
|
|
|
}
|
2020-04-07 22:49:08 -06:00
|
|
|
return links, nil
|
2019-07-11 19:05:55 -06:00
|
|
|
}
|
|
|
|
|
2019-12-24 13:58:48 -07:00
|
|
|
func getIssueRegexp() *regexp.Regexp {
|
|
|
|
once.Do(func() {
|
|
|
|
issueRegexp = regexp.MustCompile(`(\w+)/([\w-]+)#([0-9]+)`)
|
|
|
|
})
|
|
|
|
return issueRegexp
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
once sync.Once
|
|
|
|
issueRegexp *regexp.Regexp
|
|
|
|
)
|
|
|
|
|
2020-02-18 13:47:38 -07:00
|
|
|
func toProtocolLink(view source.View, m *protocol.ColumnMapper, target string, start, end token.Pos, fileKind source.FileKind) (protocol.DocumentLink, error) {
|
|
|
|
var rng protocol.Range
|
|
|
|
switch fileKind {
|
|
|
|
case source.Go:
|
|
|
|
spn, err := span.NewRange(view.Session().Cache().FileSet(), start, end).Span()
|
|
|
|
if err != nil {
|
|
|
|
return protocol.DocumentLink{}, err
|
|
|
|
}
|
|
|
|
rng, err = m.Range(spn)
|
|
|
|
if err != nil {
|
|
|
|
return protocol.DocumentLink{}, err
|
|
|
|
}
|
|
|
|
case source.Mod:
|
|
|
|
s, e := int(start), int(end)
|
|
|
|
line, col, err := m.Converter.ToPosition(s)
|
|
|
|
if err != nil {
|
|
|
|
return protocol.DocumentLink{}, err
|
|
|
|
}
|
|
|
|
start := span.NewPoint(line, col, s)
|
|
|
|
line, col, err = m.Converter.ToPosition(e)
|
|
|
|
if err != nil {
|
|
|
|
return protocol.DocumentLink{}, err
|
|
|
|
}
|
|
|
|
end := span.NewPoint(line, col, e)
|
|
|
|
rng, err = m.Range(span.New(m.URI, start, end))
|
|
|
|
if err != nil {
|
|
|
|
return protocol.DocumentLink{}, err
|
|
|
|
}
|
2019-07-07 16:25:19 -06:00
|
|
|
}
|
2020-01-10 00:06:12 -07:00
|
|
|
return protocol.DocumentLink{
|
2019-07-07 16:25:19 -06:00
|
|
|
Range: rng,
|
|
|
|
Target: target,
|
2020-01-10 00:06:12 -07:00
|
|
|
}, nil
|
2019-07-07 16:25:19 -06:00
|
|
|
}
|