Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 27 additions & 27 deletions gopls/internal/server/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import (
"go/ast"
"go/token"
"net/url"
"path"
"path/filepath"
"regexp"
"strings"
"sync"

"golang.org/x/mod/modfile"
"golang.org/x/mod/module"
"golang.org/x/tools/go/ast/edge"
"golang.org/x/tools/gopls/internal/cache"
"golang.org/x/tools/gopls/internal/cache/metadata"
"golang.org/x/tools/gopls/internal/cache/parsego"
Expand Down Expand Up @@ -201,27 +203,20 @@ func goLinks(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle) ([]p

urlRegexp := xurls.Relaxed()

// Gather links found in string literals.
var str []*ast.BasicLit
// Gather links found in string literals, ignoring import strings.
for curLit := range pgf.Cursor().Preorder((*ast.BasicLit)(nil)) {
lit := curLit.Node().(*ast.BasicLit)
if lit.Kind == token.STRING {
if _, ok := curLit.Parent().Node().(*ast.ImportSpec); ok {
continue // ignore import strings
if lit.Kind == token.STRING && curLit.ParentEdgeKind() != edge.ImportSpec_Path {
strOffset, err := safetoken.Offset(pgf.Tok, lit.Pos())
if err != nil {
return nil, err
}
str = append(str, lit)
}
}
for _, s := range str {
strOffset, err := safetoken.Offset(pgf.Tok, s.Pos())
if err != nil {
return nil, err
}
l, err := findLinksInString(urlRegexp, s.Value, strOffset, pgf.Mapper)
if err != nil {
return nil, err
l, err := findLinksInString(urlRegexp, lit.Value, strOffset, pgf.Mapper)
if err != nil {
return nil, err
}
links = append(links, l...)
}
links = append(links, l...)
}

// Gather links found in comments.
Expand Down Expand Up @@ -255,19 +250,16 @@ func findLinksInString(urlRegexp *regexp.Regexp, src string, srcOffset int, m *p
start, end := index[0], index[1]
link := src[start:end]
linkURL, err := url.Parse(link)
// Fallback: Linkify IP addresses as suggested in golang/go#18824.
if err != nil {
linkURL, err = url.Parse("//" + link)
// Not all potential links will be valid, so don't return this error.
if err != nil {
if err == nil && linkURL.Scheme == "" {
// Without a scheme, only link hosts with a common top-level domain,
// so that text such as "p.name" is left alone (golang/go#70033).
linkURL, err = url.Parse("https://" + link)
if err == nil && !hasCommonTLD(linkURL.Hostname()) {
continue
}
}
// If the URL has no scheme, use https.
if linkURL.Scheme == "" {
linkURL.Scheme = "https"
}
if !acceptedSchemes.Contains(linkURL.Scheme) {
// Not all potential links will be valid, so don't return this error.
if err != nil || !acceptedSchemes.Contains(linkURL.Scheme) {
continue
}

Expand Down Expand Up @@ -296,6 +288,14 @@ func findLinksInString(urlRegexp *regexp.Regexp, src string, srcOffset int, m *p
return links, nil
}

func hasCommonTLD(host string) bool {
switch path.Ext(host) {
case ".com", ".net", ".org", ".dev":
return true
}
return false
}

func getIssueRegexp() *regexp.Regexp {
once.Do(func() {
issueRegexp = regexp.MustCompile(`(\w+)/([\w-]+)#([0-9]+)`)
Expand Down
27 changes: 27 additions & 0 deletions gopls/internal/test/marker/testdata/links/schemeless.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
This test verifies that dotted text without a scheme is only linkified
when its host ends in a common top-level domain (golang/go#70033).

-- go.mod --
module example.com

go 1.18
-- a/a.go --
package a //@documentlink(links)

import _ "example.com/b"

var _ = []string{
"p.name is %q",
"go.dev is my favorite site!",
"https://p.name is not a real site!",
}

// See example.com/doc or https://go.dev/doc.
-- b/b.go --
package b
-- @links --
a/a.go:3:11-24 https://pkg.go.dev/example.com/b
a/a.go:7:3-9 https://go.dev
a/a.go:8:3-17 https://p.name
a/a.go:11:8-23 https://example.com/doc
a/a.go:11:27-45 https://go.dev/doc
Loading