internal/lsp: only linkify urls with http, https, and ftp schemes

Fixes golang/go#43990

Change-Id: I0ea26d521b2432238b05c26bfaccef6fc773dcf2
Reviewed-on: https://go-review.googlesource.com/c/tools/+/393854
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Peter Weinberger <pjw@google.com>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
This commit is contained in:
Robert Findley 2022-03-18 11:29:53 -04:00
parent 04fc2ba822
commit 4a3fc2182a
3 changed files with 18 additions and 1 deletions

View File

@ -15,6 +15,7 @@ import (
func TestHoverAndDocumentLink(t *testing.T) {
testenv.NeedsGo1Point(t, 13)
const program = `
-- go.mod --
module mod.test
@ -31,6 +32,8 @@ package main
import "import.test/pkg"
func main() {
// Issue 43990: this is not a link that most users can open from an LSP
// client: mongodb://not.a.link.com
println(pkg.Hello)
}`

View File

@ -185,6 +185,14 @@ func moduleAtVersion(target string, pkg source.Package) (string, string, bool) {
return modpath, version, true
}
// acceptedSchemes controls the schemes that URLs must have to be shown to the
// user. Other schemes can't be opened by LSP clients, so linkifying them is
// distracting. See golang/go#43990.
var acceptedSchemes = map[string]bool{
"http": true,
"https": true,
}
func findLinksInString(ctx context.Context, snapshot source.Snapshot, src string, pos token.Pos, m *protocol.ColumnMapper, fileKind source.FileKind) ([]protocol.DocumentLink, error) {
var links []protocol.DocumentLink
for _, index := range snapshot.View().Options().URLRegexp.FindAllIndex([]byte(src), -1) {
@ -205,6 +213,9 @@ func findLinksInString(ctx context.Context, snapshot source.Snapshot, src string
if linkURL.Scheme == "" {
linkURL.Scheme = "https"
}
if !acceptedSchemes[linkURL.Scheme] {
continue
}
l, err := toProtocolLink(snapshot, m, linkURL.String(), startPos, endPos, fileKind)
if err != nil {
return nil, err

View File

@ -475,7 +475,10 @@ type Hooks struct {
// ComputeEdits is used to compute edits between file versions.
ComputeEdits diff.ComputeEdits
// URLRegexp is used to find urls in comments and strings.
// URLRegexp is used to find potential URLs in comments/strings.
//
// Not all matches are shown to the user: if the matched URL is not detected
// as valid, it will be skipped.
URLRegexp *regexp.Regexp
// GofumptFormat allows the gopls module to wire-in a call to