This repository has been archived by the owner on May 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
ref.go
139 lines (117 loc) · 3.46 KB
/
ref.go
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
package graph
import (
"strconv"
"sourcegraph.com/sourcegraph/srclib/repo"
)
type RefDefKey struct {
DefRepo repo.URI `db:"def_repo" json:",omitempty"`
DefUnitType string `db:"def_unit_type" json:",omitempty"`
DefUnit string `db:"def_unit" json:",omitempty"`
DefPath DefPath `db:"def_path" json:",omitempty"`
}
type RefKey struct {
DefRepo repo.URI `db:"def_repo" json:",omitempty"`
DefUnitType string `db:"def_unit_type" json:",omitempty"`
DefUnit string `db:"def_unit" json:",omitempty"`
DefPath DefPath `db:"def_path" json:",omitempty"`
Def bool `json:",omitempty"`
Repo repo.URI `json:",omitempty"`
UnitType string `db:"unit_type" json:",omitempty"`
Unit string `json:",omitempty"`
File string `json:",omitempty"`
CommitID string `db:"commit_id" json:",omitempty"`
Start int `json:",omitempty"`
End int `json:",omitempty"`
}
func (r *RefKey) RefDefKey() RefDefKey {
return RefDefKey{
DefRepo: r.DefRepo,
DefUnitType: r.DefUnitType,
DefUnit: r.DefUnit,
DefPath: r.DefPath,
}
}
// START Ref OMIT
// Ref represents a reference from source code to a def.
type Ref struct {
// The definition that this reference points to
DefRepo repo.URI `db:"def_repo"`
DefUnitType string `db:"def_unit_type"`
DefUnit string `db:"def_unit"`
DefPath DefPath `db:"def_path"`
// Def is true if this ref is the original definition or a redefinition
Def bool
Repo repo.URI
// CommitID is the immutable commit ID (not the branch name) of the VCS
// revision that this ref was found in.
CommitID string `db:"commit_id" json:",omitempty"`
UnitType string `db:"unit_type" json:",omitempty"`
Unit string `json:",omitempty"`
File string
Start int
End int
}
// END Ref OMIT
func (r *Ref) RefKey() RefKey {
return RefKey{
DefRepo: r.DefRepo,
DefUnitType: r.DefUnitType,
DefUnit: r.DefUnit,
DefPath: r.DefPath,
Def: r.Def,
Repo: r.Repo,
UnitType: r.UnitType,
Unit: r.Unit,
File: r.File,
Start: r.Start,
End: r.End,
}
}
func (r *Ref) RefDefKey() RefDefKey {
return RefDefKey{
DefRepo: r.DefRepo,
DefUnitType: r.DefUnitType,
DefUnit: r.DefUnit,
DefPath: r.DefPath,
}
}
func (r *Ref) DefKey() DefKey {
return DefKey{
Repo: r.DefRepo,
UnitType: r.DefUnitType,
Unit: r.DefUnit,
Path: r.DefPath,
}
}
func (r *Ref) SetFromDefKey(k DefKey) {
r.DefPath = k.Path
r.DefUnitType = k.UnitType
r.DefUnit = k.Unit
r.DefRepo = k.Repo
}
// Sorting
type Refs []*Ref
func (r *Ref) sortKey() string {
return string(r.DefPath) + string(r.DefRepo) + r.DefUnitType + r.DefUnit + string(r.Repo) + r.UnitType + r.Unit + r.File + strconv.Itoa(r.Start) + strconv.Itoa(r.End)
}
func (vs Refs) Len() int { return len(vs) }
func (vs Refs) Swap(i, j int) { vs[i], vs[j] = vs[j], vs[i] }
func (vs Refs) Less(i, j int) bool { return vs[i].sortKey() < vs[j].sortKey() }
// RefSet is a set of Refs. It can used to determine whether a grapher emits
// duplicate refs.
type RefSet struct {
refs map[Ref]struct{}
}
func NewRefSet() *RefSet {
return &RefSet{make(map[Ref]struct{})}
}
// AddAndCheckUnique adds ref to the set of seen refs, and returns whether the
// ref already existed in the set.
func (c *RefSet) AddAndCheckUnique(ref Ref) (duplicate bool) {
_, present := c.refs[ref]
if present {
return true
}
c.refs[ref] = struct{}{}
return false
}