From a764947cb58b2343ecb97a8c5382d8179d95de90 Mon Sep 17 00:00:00 2001 From: zikaeroh Date: Wed, 13 Nov 2019 20:53:00 -0800 Subject: [PATCH] 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 Run-TryBot: Heschi Kreinick --- internal/lsp/link.go | 5 ++++- internal/lsp/testdata/links/links.go | 10 ++++++++-- internal/lsp/testdata/summary.txt.golden | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/internal/lsp/link.go b/internal/lsp/link.go index 4c79bdc707..92777fbc95 100644 --- a/internal/lsp/link.go +++ b/internal/lsp/link.go @@ -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 diff --git a/internal/lsp/testdata/links/links.go b/internal/lsp/testdata/links/links.go index 44053b3d13..f726e38cbf 100644 --- a/internal/lsp/testdata/links/links.go +++ b/internal/lsp/testdata/links/links.go @@ -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 } diff --git a/internal/lsp/testdata/summary.txt.golden b/internal/lsp/testdata/summary.txt.golden index aacef0be0b..baa0d82892 100644 --- a/internal/lsp/testdata/summary.txt.golden +++ b/internal/lsp/testdata/summary.txt.golden @@ -19,5 +19,5 @@ RenamesCount = 20 PrepareRenamesCount = 8 SymbolsCount = 1 SignaturesCount = 21 -LinksCount = 4 +LinksCount = 6