Moved node code into dedicated node file

This commit is contained in:
Samuel Hawksby-Robinson 2021-01-06 17:08:15 +00:00
parent 9792ef33d2
commit 4723f2f47d
No known key found for this signature in database
GPG Key ID: 64CF99D4A64A1205
3 changed files with 44 additions and 41 deletions

View File

@ -25,5 +25,5 @@ func main() {
panic(err)
}
spew.Dump(tf.foundTree)
spew.Dump(tf.FoundTable)
}

41
node.go Normal file
View File

@ -0,0 +1,41 @@
package main
type node struct {
Name string
Type string
Nodes []*node
Todos []*todo
}
func (n *node) AddToTree(path []string, t *todo) {
if len(path) == 0 {
n.Todos = append(n.Todos, t)
return
}
var nn *node
for _, cn := range n.Nodes {
if cn.Name == path[0] {
nn = cn
}
}
if nn == nil {
nn = &node{
Name: path[0],
Type: n.getTypeFromPath(path),
}
n.Nodes = append(n.Nodes, nn)
}
nn.AddToTree(path[1:], t)
}
func (n node) getTypeFromPath(path []string) string {
if len(path) == 1 {
return "file"
}
return "dir"
}

View File

@ -8,18 +8,13 @@ import (
"unicode"
)
type node struct {
Name string
Type string
Nodes []*node
Todos []*todo
}
type TodoFinder struct {
// regex related fields
entityTracker *entityTracker
todoRegex *regexp.Regexp
lineRegex *regexp.Regexp
// results store
FoundTable []*todo
foundTree *node
@ -125,39 +120,6 @@ func (tf *TodoFinder) FindInDir(dir string) error {
return nil
}
func (n *node) AddToTree(path []string, t *todo) {
if len(path) == 0 {
n.Todos = append(n.Todos, t)
return
}
var nn *node
for _, cn := range n.Nodes {
if cn.Name == path[0] {
nn = cn
}
}
if nn == nil {
nn = &node{
Name: path[0],
Type: n.getTypeFromPath(path),
}
n.Nodes = append(n.Nodes, nn)
}
nn.AddToTree(path[1:], t)
}
func (n node) getTypeFromPath(path []string) string {
if len(path) == 1 {
return "file"
}
return "dir"
}
func (tf TodoFinder) isGoFile(name string) bool {
if len(name) < 3 {
return false