From 602a263f5534897aa8ffaf8895bd9c6a39b26074 Mon Sep 17 00:00:00 2001 From: Samuel Hawksby-Robinson Date: Wed, 6 Jan 2021 14:41:25 +0000 Subject: [PATCH] BROKEN - This commit breaks the original functionality, but introduces an effective method of representing found todos in both a table and tree format. --- main.go | 31 +++++++++++++++------ todo_finder.go | 67 +++++++++++++++++++++++++++++++++++++++++++++ todo_finder_test.go | 20 ++++++++++++++ 3 files changed, 110 insertions(+), 8 deletions(-) create mode 100644 todo_finder.go create mode 100644 todo_finder_test.go diff --git a/main.go b/main.go index a54e06c..db11219 100644 --- a/main.go +++ b/main.go @@ -6,8 +6,6 @@ import ( "regexp" "strings" "unicode" - - "github.com/davecgh/go-spew/spew" ) // TODO implement dynamic comment token selection, could maybe work similar to entityTracker{} @@ -20,8 +18,7 @@ const ( ) var ( - keywords = []string{"todo", "fixme"} - found []todo + keywords = []string{"todo", "fixme"} // todoMode tracks if subsequent comment lines should be included in the last to-do's description todoMode = false @@ -32,6 +29,19 @@ type todo struct { Description string LineNumber int RelatedFuncOrType string + filePathSlice []string +} + +func (t *todo) Path() []string { + if t.filePathSlice != nil { + return t.filePathSlice + } + + sp := strings.Split(t.Filepath, "/") + sp = trimPath(sp) + t.filePathSlice = sp + + return t.filePathSlice } func main() { @@ -39,8 +49,6 @@ func main() { if err != nil { panic(err) } - - spew.Dump(found) } func processFilesInDir(dir string) error { @@ -100,7 +108,7 @@ func processFilesInDir(dir string) error { if todoMode { l = strings.TrimSpace(l) if l[:2] == "//" { - found[len(found)-1].Description += "\n" + l[2:] + //found[len(found)-1].Description += "\n" + l[2:] } else { todoMode = false } @@ -108,7 +116,14 @@ func processFilesInDir(dir string) error { continue } - found = append(found, todo{filepath, string(results[1]), i + 1, et.Current()}) + td := &todo{ + Filepath: filepath, + Description: string(results[1]), + LineNumber: i + 1, + RelatedFuncOrType: et.Current(), + } + println(td) + //found = append(found, todo{filepath, string(results[1]), i + 1, et.Current()}) todoMode = true } } diff --git a/todo_finder.go b/todo_finder.go new file mode 100644 index 0000000..f131cb1 --- /dev/null +++ b/todo_finder.go @@ -0,0 +1,67 @@ +package main + +type node struct { + Name string + Type string + Nodes []*node + Todos []*todo +} + +type todoFinder struct { + foundTable map[string]*todo + foundTree *node +} + +func NewTodoFinder() *todoFinder { + return &todoFinder{ + foundTable: map[string]*todo{}, + foundTree: &node{Name: "root", Type: "dir"}, + } +} + +func (tf *todoFinder) AddTodo(t *todo) { + tf.foundTable[t.Filepath] = t + tf.foundTree.AddToTree(t.Path(), t) +} + +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 trimPath(path []string) []string { + ignoreList := []string{"..", "status-go"} + if path[0] == ignoreList[0] && path[1] == ignoreList[1] { + path = path[2:] + } + + return path +} diff --git a/todo_finder_test.go b/todo_finder_test.go new file mode 100644 index 0000000..5f49427 --- /dev/null +++ b/todo_finder_test.go @@ -0,0 +1,20 @@ +package main + +import ( + "github.com/davecgh/go-spew/spew" + "testing" +) + +func TestNode_AddToTree(t *testing.T) { + n := &node{Name: "root", Type: "dir"} + td := &todo{ + Filepath: "../status-go/admin/sales/donkeys/pokemon/gif.go", + Description: "this looks borken", + LineNumber: 19224, + RelatedFuncOrType: "", + } + + n.AddToTree(td.Path(), td) + + spew.Dump(n) +}