-
Notifications
You must be signed in to change notification settings - Fork 9
/
annotated.go
64 lines (53 loc) · 1.21 KB
/
annotated.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
package main
import (
"errors"
"os"
"strings"
)
type AnnotatedFile struct {
Path string `json:"path"`
AbsPath string `json:"abspath"`
Lines []Line `json:"lines"`
}
type Line struct {
Source string `json:"source"`
Notes []LineNote `json:"notes"`
}
type LineNote struct {
Column int `json:"column"`
Message string `json:"message"`
}
func (index *Index) LoadAnnotatedFile(path string) (*AnnotatedFile, error) {
info, ok := index.Files[path]
if !ok {
return nil, errors.New("not found")
}
data, err := os.ReadFile(info.AbsPath)
if err != nil {
return nil, err
}
file := &AnnotatedFile{}
file.Path = info.Path
file.AbsPath = info.AbsPath
noteidx := 0
sourceLines := strings.Split(string(data), "\n")
for i, sourceLine := range sourceLines {
line := Line{}
line.Source = sourceLine
line.Notes = []LineNote{}
for noteidx < len(info.Notes) && i > info.Notes[noteidx].Line {
noteidx++
}
for noteidx < len(info.Notes) && i == info.Notes[noteidx].Line {
x := info.Notes[noteidx]
note := LineNote{
Column: x.Column,
Message: string(x.Message),
}
line.Notes = append(line.Notes, note)
noteidx++
}
file.Lines = append(file.Lines, line)
}
return file, nil
}