-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathparse_func.go
68 lines (63 loc) · 1.59 KB
/
parse_func.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
package main
import (
"log"
"regexp"
"strings"
)
// ParseFunc ...
func ParseFunc(text string) []*NeedCommentLine {
// func (r *ResenderMsg) IsSendCtx() context.Context {
// func (s *DefaultPBConverter[T, U]) Convert(
re, err := regexp.Compile(
`^func\s+([A-Z][a-zA-Z0-9_-]+).*\(|^func\s*\([a-zA-Z0-9]+\s+[ *a-zA-Z0-9,\[\]\.]+\)\s+([A-Z][a-zA-Z0-9_-]+)`)
if err != nil {
log.Println(err)
return []*NeedCommentLine{}
}
lines := strings.Split(text, "\n")
willComments := make([]*NeedCommentLine, 0)
for i, line := range lines {
ret := re.FindAllStringSubmatch(line, -1)
if len(ret) >= 1 {
matchArr := ret[0]
if len(matchArr) >= 3 {
var funcName string
if matchArr[1] != "" {
funcName = matchArr[1]
} else {
funcName = matchArr[2]
}
// log.Printf("func name:%v\n", funcName)
tp := new(NeedCommentLine)
tp.Name = funcName
tp.OriginLineNo = i
willComments = append(willComments, tp)
}
}
}
return willComments
}
func filterRegex(re *regexp.Regexp, lines []string, prefix string) []*NeedCommentLine {
willComments := make([]*NeedCommentLine, 0)
for i, line := range lines {
ret := re.FindAllStringSubmatch(line, -1)
if len(ret) >= 1 {
matchArr := ret[0]
var funcName string
if len(matchArr) >= 2 {
for _, v := range matchArr[1:] {
if strings.Trim(v, "\t\r\n ") != "" {
funcName = v
break
}
}
// log.Printf("func name:%v\n", funcName)
tp := new(NeedCommentLine)
tp.Name = prefix + funcName
tp.OriginLineNo = i
willComments = append(willComments, tp)
}
}
}
return willComments
}