Skip to content

Commit a764947

Browse files
zikaerohheschi
authored andcommitted
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 #35565 Change-Id: Ie93d9df993fbd8e0106ca6c3b40e0885355be66b Reviewed-on: https://go-review.googlesource.com/c/tools/+/207137 Reviewed-by: Heschi Kreinick <[email protected]> Run-TryBot: Heschi Kreinick <[email protected]>
1 parent faa6948 commit a764947

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

internal/lsp/link.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ func (s *Server) documentLink(ctx context.Context, params *protocol.DocumentLink
4141
log.Error(ctx, "cannot unquote import path", err, tag.Of("Path", n.Path.Value))
4242
return false
4343
}
44+
if target == "" {
45+
return false
46+
}
4447
target = "https://godoc.org/" + target
45-
l, err := toProtocolLink(view, m, target, n.Pos(), n.End())
48+
l, err := toProtocolLink(view, m, target, n.Path.Pos()+1, n.Path.End()-1)
4649
if err != nil {
4750
log.Error(ctx, "cannot initialize DocumentLink", err, tag.Of("Path", n.Path.Value))
4851
return false

internal/lsp/testdata/links/links.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
package links
22

33
import (
4-
"fmt" //@link(re`".*"`,"https://godoc.org/fmt")
4+
"fmt" //@link(`fmt`,"https://godoc.org/fmt")
55

6-
"golang.org/x/tools/internal/lsp/foo" //@link(re`".*"`,`https://godoc.org/golang.org/x/tools/internal/lsp/foo`)
6+
"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`)
7+
8+
_ "database/sql" //@link(`database/sql`, `https://godoc.org/database/sql`)
9+
10+
errors "golang.org/x/xerrors" //@link(`golang.org/x/xerrors`, `https://godoc.org/golang.org/x/xerrors`)
711
)
812

913
var (
1014
_ fmt.Formatter
1115
_ foo.StructFoo
16+
_ errors.Formatter
1217
)
1318

1419
// Foo function
1520
func Foo() string {
1621
/*https://example.com/comment */ //@link("https://example.com/comment","https://example.com/comment")
22+
1723
url := "https://example.com/string_literal" //@link("https://example.com/string_literal","https://example.com/string_literal")
1824
return url
1925
}

internal/lsp/testdata/summary.txt.golden

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ RenamesCount = 20
1919
PrepareRenamesCount = 8
2020
SymbolsCount = 1
2121
SignaturesCount = 21
22-
LinksCount = 4
22+
LinksCount = 6
2323

0 commit comments

Comments
 (0)