internal/lsp: use import path literal for documentLink range

Instead of using the entire import node as the range for the
link, use only the link text in the path node itself. This looks
better when using a _ or named import, as well as constraining
the link to inside the quotes.

Fixes golang/go#35565

Change-Id: Ie93d9df993fbd8e0106ca6c3b40e0885355be66b
Reviewed-on: https://go-review.googlesource.com/c/tools/+/207137
Reviewed-by: Heschi Kreinick <heschi@google.com>
Run-TryBot: Heschi Kreinick <heschi@google.com>
This commit is contained in:
zikaeroh 2019-11-13 20:53:00 -08:00 committed by Heschi Kreinick
parent faa69481e7
commit a764947cb5
3 changed files with 13 additions and 4 deletions

View File

@ -41,8 +41,11 @@ func (s *Server) documentLink(ctx context.Context, params *protocol.DocumentLink
log.Error(ctx, "cannot unquote import path", err, tag.Of("Path", n.Path.Value))
return false
}
if target == "" {
return false
}
target = "https://godoc.org/" + target
l, err := toProtocolLink(view, m, target, n.Pos(), n.End())
l, err := toProtocolLink(view, m, target, n.Path.Pos()+1, n.Path.End()-1)
if err != nil {
log.Error(ctx, "cannot initialize DocumentLink", err, tag.Of("Path", n.Path.Value))
return false

View File

@ -1,19 +1,25 @@
package links
import (
"fmt" //@link(re`".*"`,"https://godoc.org/fmt")
"fmt" //@link(`fmt`,"https://godoc.org/fmt")
"golang.org/x/tools/internal/lsp/foo" //@link(re`".*"`,`https://godoc.org/golang.org/x/tools/internal/lsp/foo`)
"golang.org/x/tools/internal/lsp/foo" //@link(`golang.org/x/tools/internal/lsp/foo`,`https://godoc.org/golang.org/x/tools/internal/lsp/foo`)
_ "database/sql" //@link(`database/sql`, `https://godoc.org/database/sql`)
errors "golang.org/x/xerrors" //@link(`golang.org/x/xerrors`, `https://godoc.org/golang.org/x/xerrors`)
)
var (
_ fmt.Formatter
_ foo.StructFoo
_ errors.Formatter
)
// Foo function
func Foo() string {
/*https://example.com/comment */ //@link("https://example.com/comment","https://example.com/comment")
url := "https://example.com/string_literal" //@link("https://example.com/string_literal","https://example.com/string_literal")
return url
}

View File

@ -19,5 +19,5 @@ RenamesCount = 20
PrepareRenamesCount = 8
SymbolsCount = 1
SignaturesCount = 21
LinksCount = 4
LinksCount = 6