Repository navigation
Expand file tree
/
Copy pathlocation.go
More file actions
65 lines (62 loc) · 1.92 KB
/
Copy pathlocation.go
File metadata and controls
65 lines (62 loc) · 1.92 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
package contracttrace
import (
"fmt"
"path/filepath"
"sort"
"strconv"
"strings"
)
func declarationKind(kind string) bool {
return kind == "global" || kind == "record_field" || kind == "event_discriminator_field"
}
func (ix *index) locationSeeds(locations []string) ([]string, error) {
symbols := []string{}
for _, location := range locations {
colon := strings.LastIndex(location, ":")
if colon < 0 {
return nil, fmt.Errorf("location must be file:line: %q", location)
}
line, err := strconv.Atoi(location[colon+1:])
if err != nil || line < 1 {
return nil, fmt.Errorf("invalid location line: %q", location)
}
path := filepath.FromSlash(location[:colon])
if !filepath.IsAbs(path) {
path = filepath.Join(ix.root, path)
}
file, inside := relative(ix.root, filepath.Clean(path))
if !inside {
return nil, fmt.Errorf("location is outside target root: %q", location)
}
matches := []string{}
for id, f := range ix.funcs {
if f.node.Evidence.File != file {
continue
}
first, last := 0, 0
if f.decl != nil {
first, last = ix.fset.Position(f.decl.Pos()).Line, ix.fset.Position(f.decl.End()).Line
} else if span, ok := ix.ranges[id]; ok {
first, last = span.First, span.Last
} else if span, ok := ix.declarationRanges[id]; ok {
first, last = span.First, span.Last
} else if declarationKind(f.node.Kind) {
first, last = f.node.Evidence.Line, f.node.Evidence.Line
} else {
continue
}
if line >= first && line <= last {
matches = append(matches, id)
}
}
sort.Strings(matches)
if len(matches) == 0 {
return nil, fmt.Errorf("no indexed function, global or record-field declaration contains location %q; unindexed package-level initializers are not supported", location)
}
if len(matches) > 1 {
return nil, fmt.Errorf("ambiguous location %q: %s", location, strings.Join(matches, ", "))
}
symbols = append(symbols, matches[0])
}
return symbols, nil
}