forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.go
More file actions
31 lines (25 loc) · 874 Bytes
/
util.go
File metadata and controls
31 lines (25 loc) · 874 Bytes
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
package opencodegraph
import (
"go/token"
"github.com/grafana/regexp"
"github.com/sourcegraph/sourcegraph/schema"
)
func firstSubmatchNamesAndRanges(pattern *regexp.Regexp, content string) (names []string, ranges []schema.OpenCodeGraphRange) {
fset := token.NewFileSet()
f := fset.AddFile("x", 1, len(content))
f.SetLinesForContent([]byte(content))
ms := pattern.FindAllStringSubmatchIndex(content, -1)
for _, m := range ms {
mstart := m[2]
mend := m[3]
start := f.Position(f.Pos(mstart))
end := f.Position(f.Pos(mend))
name := string(content[mstart:mend])
names = append(names, name)
ranges = append(ranges, schema.OpenCodeGraphRange{
Start: schema.OpenCodeGraphPosition{Line: start.Line - 1, Character: start.Column - 1},
End: schema.OpenCodeGraphPosition{Line: end.Line - 1, Character: end.Column - 1},
})
}
return names, ranges
}