/
location.go
189 lines (155 loc) · 4.69 KB
/
location.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package internal
import (
"cmp"
"slices"
"sort"
"encr.dev/pkg/option"
)
type LocationType uint8
const (
LocError LocationType = iota // Renders in red
LocWarning // Renders in yellow
LocHelp // Renders in blue
)
type SrcLocation struct {
Type LocationType `json:"type"` // The type of this location
Text string `json:"text,omitempty"` // Optional text to render at this location
File *File `json:"file,omitempty"` // The file containing the error
Start Pos `json:"start"` // The position this location starts at
End Pos `json:"end"` // The position this location ends at
}
func (s *SrcLocation) Less(other *SrcLocation) bool {
// Order by type of location first (Err, then Warn, then Help)
// as we always want errors rendered above warnings and warnings above help
// if s.Type != other.Type {
// return s.Type < other.Type
// }
// Order by file first
if s.File.FullPath != other.File.FullPath {
return s.File.FullPath < other.File.FullPath
}
// And then by the line number of where they start
if s.Start.Line != other.Start.Line {
return s.Start.Line < other.Start.Line
}
// And then where they start on that line
if s.Start.Col != other.Start.Col {
return s.Start.Col < other.Start.Col
}
// And which line they end on
if s.End.Line != other.End.Line {
return s.End.Line < other.End.Line
}
// Then by ending column
if s.End.Col != other.End.Col {
return s.End.Col < other.End.Col
}
// Finally, by the text in the location
if s.Text != other.Text {
return s.Text < other.Text
}
if s.Type != other.Type {
return s.Type < other.Type
}
return false
}
type Pos struct {
Line int `json:"line"`
Col int `json:"col"`
}
type File struct {
RelPath string // The relative path within the project
FullPath string // The full path to the file
Contents []byte // The contents of the file
}
// SrcLocations represents a list of locations
// within the source code. It can be sorted and split
// up into separate lists using GroupByFile
type SrcLocations []*SrcLocation
var _ sort.Interface = SrcLocations{}
func (s SrcLocations) Len() int {
return len(s)
}
func (s SrcLocations) Less(i, j int) bool {
return s[i].Less(s[j])
}
func (s SrcLocations) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func NewSrcLocations(opts ...option.Option[*SrcLocation]) SrcLocations {
var rtn SrcLocations
for _, opt := range opts {
if loc, ok := opt.Get(); ok {
rtn = append(rtn, loc)
}
}
return rtn
}
// GroupByFile groups all locations by file and returns a new
// SrcLocations for each file.
//
// If a file has overlapping locations or two locations on the same line
// then more than one SrcLocations will be returned for that file. This
// is due to a limitation in the srcrender, and may be relaxed in the future.
func (s SrcLocations) GroupByFile() []SrcLocations {
type locationGroup struct {
fileName string
locations SrcLocations
}
var nonOverlappingLocations []*locationGroup
inlineOnSameLine := func(a, b *SrcLocation) bool {
return a.Start.Line == a.End.Line &&
b.Start.Line == b.End.Line &&
a.Start.Line == b.Start.Line &&
a.Text == "" && b.Text == "" && // Don't inline if there is text as we don't support rendering this yet
((a.Start.Col > b.End.Col) ||
(a.End.Col < b.Start.Col))
}
// Add locations to groups on the same file without overlaps
nextOriginalLoc:
for _, loc := range s {
// Attempt to match it into an existing group
for _, grp := range nonOverlappingLocations {
if grp.fileName == loc.File.FullPath {
for _, other := range grp.locations {
if other.Start.Line > loc.End.Line ||
other.End.Line < loc.Start.Line ||
inlineOnSameLine(other, loc) {
grp.locations = append(grp.locations, loc)
continue nextOriginalLoc
}
}
}
}
// if here we found no matching groups
nonOverlappingLocations = append(nonOverlappingLocations, &locationGroup{
fileName: loc.File.FullPath,
locations: SrcLocations{loc},
})
}
// Sort the locations in each group
rtn := make([]SrcLocations, len(nonOverlappingLocations))
for i, grp := range nonOverlappingLocations {
sort.Sort(grp.locations)
rtn[i] = grp.locations
}
// Now sort the groups by the lowest location hint
// this means that errors will be rendered first, then warnings, then help
// if they are in different files
slices.SortStableFunc(rtn, func(a, b SrcLocations) int {
lowestA := LocHelp
lowestB := LocHelp
for _, loc := range a {
if loc.Type < lowestA {
lowestA = loc.Type
}
}
for _, loc := range b {
if loc.Type < lowestB {
lowestB = loc.Type
}
}
return cmp.Compare(lowestA, lowestB)
})
return rtn
}