From 4a3fc2182a4a10d066a3c3662eb4c1fddc03ed5b Mon Sep 17 00:00:00 2001 From: Robert Findley Date: Fri, 18 Mar 2022 11:29:53 -0400 Subject: [PATCH] internal/lsp: only linkify urls with http, https, and ftp schemes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes golang/go#43990 Change-Id: I0ea26d521b2432238b05c26bfaccef6fc773dcf2 Reviewed-on: https://go-review.googlesource.com/c/tools/+/393854 TryBot-Result: Gopher Robot Reviewed-by: Peter Weinberger Reviewed-by: Daniel Martí Run-TryBot: Robert Findley gopls-CI: kokoro --- gopls/internal/regtest/misc/link_test.go | 3 +++ internal/lsp/link.go | 11 +++++++++++ internal/lsp/source/options.go | 5 ++++- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/gopls/internal/regtest/misc/link_test.go b/gopls/internal/regtest/misc/link_test.go index daea74250f..e84f6377ee 100644 --- a/gopls/internal/regtest/misc/link_test.go +++ b/gopls/internal/regtest/misc/link_test.go @@ -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) }` diff --git a/internal/lsp/link.go b/internal/lsp/link.go index 2f9f006664..dcb9217e32 100644 --- a/internal/lsp/link.go +++ b/internal/lsp/link.go @@ -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 diff --git a/internal/lsp/source/options.go b/internal/lsp/source/options.go index 79628ee263..123bbe120c 100644 --- a/internal/lsp/source/options.go +++ b/internal/lsp/source/options.go @@ -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