More refactor to move state to be handled by dedicated entities

This commit is contained in:
Samuel Hawksby-Robinson 2021-01-06 16:47:34 +00:00
parent ce7a0f5a38
commit b397566aaf
No known key found for this signature in database
GPG Key ID: 64CF99D4A64A1205
3 changed files with 43 additions and 44 deletions

36
main.go
View File

@ -1,9 +1,7 @@
package main package main
import ( import (
"fmt"
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
"unicode"
) )
// TODO implement dynamic comment token selection, could maybe work similar to entityTracker{} // TODO implement dynamic comment token selection, could maybe work similar to entityTracker{}
@ -15,10 +13,6 @@ const (
ignore = statusDir + "/vendor" ignore = statusDir + "/vendor"
) )
var (
keywords = []string{"todo", "fixme"}
)
func main() { func main() {
tf := NewTodoFinder() tf := NewTodoFinder()
@ -29,33 +23,3 @@ func main() {
spew.Dump(tf.foundTree) spew.Dump(tf.foundTree)
} }
func isGoFile(name string) bool {
if len(name) < 3 {
return false
}
last := name[len(name)-3:]
return last == ".go"
}
func buildRegexPattern(keywords []string) string {
kwp := makeRegexKeywords(keywords)
return fmt.Sprintf("//.*((%s)(.*))", kwp)
}
func makeRegexKeywords(keywords []string) (out string) {
for i, kw := range keywords {
for _, r := range kw {
lower := unicode.ToLower(r)
upper := unicode.ToUpper(r)
out += fmt.Sprintf("[%s,%s]", string(lower), string(upper))
}
if i+1 < len(keywords) {
out += "|"
}
}
return
}

11
todo.go
View File

@ -16,8 +16,17 @@ func (t *todo) Path() []string {
} }
sp := strings.Split(t.Filepath, "/") sp := strings.Split(t.Filepath, "/")
sp = trimPath(sp) sp = t.trimPath(sp)
t.filePathSlice = sp t.filePathSlice = sp
return t.filePathSlice return t.filePathSlice
} }
func (t *todo) trimPath(path []string) []string {
ignoreList := []string{"..", "status-go"}
if path[0] == ignoreList[0] && path[1] == ignoreList[1] {
path = path[2:]
}
return path
}

View File

@ -1,9 +1,11 @@
package main package main
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"regexp" "regexp"
"strings" "strings"
"unicode"
) )
type node struct { type node struct {
@ -19,12 +21,15 @@ type TodoFinder struct {
// openTodo tracks if subsequent comment lines should be included in the last to-do's description // openTodo tracks if subsequent comment lines should be included in the last to-do's description
openTodo *todo openTodo *todo
keywords []string
} }
func NewTodoFinder() TodoFinder { func NewTodoFinder() TodoFinder {
return TodoFinder{ return TodoFinder{
FoundTable: []*todo{}, FoundTable: []*todo{},
foundTree: &node{Name: "root", Type: "dir"}, foundTree: &node{Name: "root", Type: "dir"},
keywords: []string{"todo", "fixme"},
} }
} }
@ -34,7 +39,7 @@ func (tf *TodoFinder) AddTodo(t *todo) {
} }
func (tf *TodoFinder) FindInDir(dir string) error { func (tf *TodoFinder) FindInDir(dir string) error {
r, err := regexp.Compile(buildRegexPattern(keywords)) r, err := regexp.Compile(tf.buildRegexPattern())
if err != nil { if err != nil {
return err return err
} }
@ -68,7 +73,7 @@ func (tf *TodoFinder) FindInDir(dir string) error {
} }
} }
if !isGoFile(f.Name()) { if !tf.isGoFile(f.Name()) {
continue continue
} }
@ -145,11 +150,32 @@ func (n node) getTypeFromPath(path []string) string {
return "dir" return "dir"
} }
func trimPath(path []string) []string { func (tf TodoFinder) isGoFile(name string) bool {
ignoreList := []string{"..", "status-go"} if len(name) < 3 {
if path[0] == ignoreList[0] && path[1] == ignoreList[1] { return false
path = path[2:] }
last := name[len(name)-3:]
return last == ".go"
}
func (tf TodoFinder) buildRegexPattern() string {
kwp := tf.makeRegexKeywords()
return fmt.Sprintf("//.*((%s)(.*))", kwp)
}
func (tf TodoFinder) makeRegexKeywords() (out string) {
for i, kw := range tf.keywords {
for _, r := range kw {
lower := unicode.ToLower(r)
upper := unicode.ToUpper(r)
out += fmt.Sprintf("[%s,%s]", string(lower), string(upper))
}
if i+1 < len(tf.keywords) {
out += "|"
}
} }
return path return
} }