-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
range.go
executable file
·148 lines (131 loc) · 3.09 KB
/
range.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
package types
import (
"encoding/json"
"fmt"
"io/fs"
"path"
)
func NewRange(filename string, startLine, endLine int, sourcePrefix string, srcFS fs.FS) Range {
r := Range{
filename: filename,
startLine: startLine,
endLine: endLine,
fs: srcFS,
fsKey: CreateFSKey(srcFS),
sourcePrefix: sourcePrefix,
}
return r
}
func NewRangeWithLogicalSource(filename string, startLine int, endLine int, sourcePrefix string,
srcFS fs.FS) Range {
r := Range{
filename: filename,
startLine: startLine,
endLine: endLine,
fs: srcFS,
fsKey: CreateFSKey(srcFS),
sourcePrefix: sourcePrefix,
isLogicalSource: true,
}
return r
}
func NewRangeWithFSKey(filename string, startLine, endLine int, sourcePrefix, fsKey string, fsys fs.FS) Range {
r := Range{
filename: filename,
startLine: startLine,
endLine: endLine,
fs: fsys,
fsKey: fsKey,
sourcePrefix: sourcePrefix,
}
return r
}
type Range struct {
filename string
startLine int
endLine int
sourcePrefix string
isLogicalSource bool
fs fs.FS
fsKey string
}
func (r Range) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"filename": r.filename,
"startLine": r.startLine,
"endLine": r.endLine,
"sourcePrefix": r.sourcePrefix,
"fsKey": r.fsKey,
"isLogicalSource": r.isLogicalSource,
})
}
func (r *Range) UnmarshalJSON(data []byte) error {
var keys map[string]interface{}
if err := json.Unmarshal(data, &keys); err != nil {
return err
}
if keys["filename"] != nil {
r.filename = keys["filename"].(string)
}
if keys["startLine"] != nil {
r.startLine = int(keys["startLine"].(float64))
}
if keys["endLine"] != nil {
r.endLine = int(keys["endLine"].(float64))
}
if keys["sourcePrefix"] != nil {
r.sourcePrefix = keys["sourcePrefix"].(string)
}
if keys["fsKey"] != nil {
r.fsKey = keys["fsKey"].(string)
}
if keys["isLogicalSource"] != nil {
r.isLogicalSource = keys["isLogicalSource"].(bool)
}
return nil
}
func (r Range) GetFSKey() string {
return r.fsKey
}
func (r Range) LineCount() int {
if r.endLine == 0 {
return 0
}
return (r.endLine - r.startLine) + 1
}
func (r Range) GetFilename() string {
if r.sourcePrefix == "" {
return r.filename
}
if r.isLogicalSource {
return fmt.Sprintf("%s:%s", r.sourcePrefix, r.filename)
}
return path.Join(r.sourcePrefix, r.filename)
}
func (r Range) GetLocalFilename() string {
return r.filename
}
func (r Range) GetStartLine() int {
return r.startLine
}
func (r Range) GetEndLine() int {
return r.endLine
}
func (r Range) IsMultiLine() bool {
return r.startLine < r.endLine
}
func (r Range) String() string {
if r.startLine != r.endLine {
return fmt.Sprintf("%s:%d-%d", r.GetFilename(), r.startLine, r.endLine)
}
if r.startLine == 0 && r.endLine == 0 {
return r.GetFilename()
}
return fmt.Sprintf("%s:%d", r.GetFilename(), r.startLine)
}
func (r Range) GetFS() fs.FS {
return r.fs
}
func (r Range) GetSourcePrefix() string {
return r.sourcePrefix
}