-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaths.go
More file actions
168 lines (146 loc) · 2.94 KB
/
paths.go
File metadata and controls
168 lines (146 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"bufio"
"io"
"iter"
"regexp"
"strings"
"unicode"
)
type Span struct {
Line int // 0-based line index
Start int // byte offset in line (inclusive)
End int // byte offset in line (exclusive)
Text string // cleaned match text
}
var (
lineColSuffix = regexp.MustCompile(`^(.+?)(:\d+(?::\d+)?)$`)
fileExtRe = regexp.MustCompile(`\w\.[a-zA-Z]{1,10}$`)
dotfileRe = regexp.MustCompile(`^\.[a-zA-Z0-9_-]{3,}`)
specialFileRe = regexp.MustCompile(`^[A-Z][a-zA-Z]{2,}file$`)
)
func looksLikePath(s string) bool {
if strings.Contains(s, "/") && s != "/" {
return true
}
if fileExtRe.MatchString(s) {
return true
}
if dotfileRe.MatchString(s) {
return true
}
return specialFileRe.MatchString(s)
}
func scanLines(r io.Reader) iter.Seq[string] {
return func(yield func(string) bool) {
scanner := bufio.NewScanner(r)
for scanner.Scan() {
if !yield(scanner.Text()) {
return
}
}
}
}
func unwrap(lines iter.Seq[string], width int) iter.Seq[string] {
return func(yield func(string) bool) {
if width <= 0 {
for line := range lines {
if !yield(line) {
return
}
}
return
}
var buf string
for line := range lines {
buf += line
if len(line) == width {
continue
}
if !yield(buf) {
return
}
buf = ""
}
if buf != "" {
yield(buf)
}
}
}
func findPaths(lines []string) []Span {
var spans []Span
for i, line := range lines {
spans = append(spans, matchPaths(i, line)...)
}
return spans
}
func matchPaths(lineIdx int, line string) []Span {
var spans []Span
off := 0
for off < len(line) {
if line[off] == ' ' || line[off] == '\t' {
off++
continue
}
end := off
for end < len(line) && line[end] != ' ' && line[end] != '\t' {
end++
}
tok := line[off:end]
tokStart := off
off = end
if strings.Contains(tok, "://") {
continue
}
tok, tokStart, tokEnd := stripWrapping(tok, tokStart)
if tok == "" {
continue
}
cleaned := tok
if m := lineColSuffix.FindStringSubmatch(tok); m != nil {
cleaned = m[1]
}
if (strings.HasPrefix(cleaned, "a/") || strings.HasPrefix(cleaned, "b/")) && len(cleaned) > 2 {
cleaned = cleaned[2:]
}
if !looksLikePath(cleaned) {
continue
}
spans = append(spans, Span{
Line: lineIdx,
Start: tokStart,
End: tokEnd,
Text: cleaned,
})
}
return spans
}
var wrapPairs = map[byte]byte{
'"': '"',
'\'': '\'',
'(': ')',
'[': ']',
'<': '>',
}
func stripWrapping(tok string, start int) (string, int, int) {
for len(tok) >= 2 {
if close, ok := wrapPairs[tok[0]]; ok && tok[len(tok)-1] == close {
tok = tok[1 : len(tok)-1]
start++
} else {
break
}
}
for len(tok) > 0 {
last := tok[len(tok)-1]
if last == ',' || last == ';' {
tok = tok[:len(tok)-1]
} else {
break
}
}
end := start + len(tok)
// Strip leading/trailing whitespace (shouldn't happen, but be safe).
tok = strings.TrimFunc(tok, unicode.IsSpace)
return tok, start, end
}