Skip to content

Commit db047d7

Browse files
eduvimstamblerre
authored andcommitted
internal/links: improve links parser, no protocol specification
The existing implementation has no consideration of links with no protocol specification, so "example.com/comments" now it's trated as "https://example.com/comments" "Fixes golang/go#33505" Corrects the regexp definition Change-Id: I587d611f26a3f3c5ea89eda7b2c3ccf369e8bb2f GitHub-Last-Rev: 740ffca GitHub-Pull-Request: #154 Reviewed-on: https://go-review.googlesource.com/c/tools/+/194661 Reviewed-by: Rebecca Stambler <[email protected]>
1 parent 8db9634 commit db047d7

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

internal/lsp/link.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,15 @@ func findLinksInString(src string, pos token.Pos, view source.View, mapper *prot
9191
if err != nil {
9292
return nil, errors.Errorf("cannot create regexp for links: %s", err.Error())
9393
}
94-
for _, urlIndex := range re.FindAllIndex([]byte(src), -1) {
94+
indexUrl := re.FindAllIndex([]byte(src), -1)
95+
for _, urlIndex := range indexUrl {
96+
var target string
9597
start := urlIndex[0]
9698
end := urlIndex[1]
9799
startPos := token.Pos(int(pos) + start)
98100
endPos := token.Pos(int(pos) + end)
99-
target := src[start:end]
100-
l, err := toProtocolLink(view, mapper, target, startPos, endPos)
101+
target = src[start:end]
102+
l, err := toProtocolLink(view, mapper, target, startPos, endPos)
101103
if err != nil {
102104
return nil, err
103105
}
@@ -106,7 +108,7 @@ func findLinksInString(src string, pos token.Pos, view source.View, mapper *prot
106108
return links, nil
107109
}
108110

109-
const urlRegexpString = "(http|ftp|https)://([\\w_-]+(?:(?:\\.[\\w_-]+)+))([\\w.,@?^=%&:/~+#-]*[\\w@?^=%&/~+#-])?"
111+
const urlRegexpString = "((http|ftp|https)://)?([\\w_-]+(?:(?:\\.[\\w_-]+)+))([\\w.,@?^=%&:/~+#-]*[\\w@?^=%&/~+#-])?"
110112

111113
var (
112114
urlRegexp *regexp.Regexp

0 commit comments

Comments
 (0)