-
Notifications
You must be signed in to change notification settings - Fork 110
/
astitodo.go
74 lines (61 loc) · 1.65 KB
/
astitodo.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
package astitodo
import (
"github.com/antlr/antlr4/runtime/Go/antlr/v4"
"regexp"
"strings"
)
var todoIdentifiers = []string{"TODO", "FIXME"}
type TODO struct {
Assignee string
Filename string
Line int
Message string
}
var (
assignRegStr = "^\\([\\w \\._\\+\\-@]+\\)"
regexpAssignee = regexp.MustCompile(assignRegStr)
)
func ParseComment(token antlr.Token, filename string) *TODO {
comment := token.GetText()
var t = strings.TrimSpace(comment)
// todo: add todo list
if strings.HasPrefix(t, "//") || strings.HasPrefix(t, "/*") || strings.HasPrefix(t, "*/") || strings.HasPrefix(t, "#") {
t = strings.TrimSpace(t[2:])
}
if length, isTodo := IsTodoIdentifier(t); isTodo {
todo := &TODO{Filename: filename, Line: token.GetLine()}
t = strings.TrimSpace(t[length:])
if strings.HasPrefix(t, ":") {
t = strings.TrimLeft(t, ":")
t = strings.TrimSpace(t)
}
// Look for assignee
if todo.Assignee = regexpAssignee.FindString(t); todo.Assignee != "" {
t = strings.TrimSpace(t[len(todo.Assignee):])
if strings.HasPrefix(t, ":") {
t = strings.TrimLeft(t, ":")
t = strings.TrimSpace(t)
}
todo.Assignee = todo.Assignee[1 : len(todo.Assignee)-1]
}
// Append text
todo.Message = handleForMultipleLine(t)
return todo
}
return nil
}
// todo: handle for letter
func handleForMultipleLine(t string) string {
t = strings.ReplaceAll(t, "*/", " ")
t = strings.ReplaceAll(t, "*", " ")
t = strings.ReplaceAll(t, "\n", " ")
return t
}
func IsTodoIdentifier(s string) (int, bool) {
for _, indent := range todoIdentifiers {
if strings.HasPrefix(strings.ToUpper(s), indent) {
return len(indent), true
}
}
return 0, false
}