|
1 | 1 | package todos |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "bytes" |
5 | | - "io/ioutil" |
6 | | - "path/filepath" |
7 | 4 | "strings" |
8 | 5 |
|
9 | | - "github.com/augmentable-dev/lege" |
10 | | - "github.com/src-d/enry/v2" |
| 6 | + "github.com/augmentable-dev/tickgit/pkg/comments" |
11 | 7 | ) |
12 | 8 |
|
13 | | -// CStyleCommentOptions ... |
14 | | -var CStyleCommentOptions *lege.ParseOptions = &lege.ParseOptions{ |
15 | | - Boundaries: []lege.Boundary{ |
16 | | - lege.Boundary{ |
17 | | - Starts: []string{"//"}, |
18 | | - Ends: []string{"\n"}, |
19 | | - }, |
20 | | - lege.Boundary{ |
21 | | - Starts: []string{"/*"}, |
22 | | - Ends: []string{"*/"}, |
23 | | - }, |
24 | | - }, |
25 | | -} |
26 | | - |
27 | | -// HashStyleCommentOptions ... |
28 | | -var HashStyleCommentOptions *lege.ParseOptions = &lege.ParseOptions{ |
29 | | - Boundaries: []lege.Boundary{ |
30 | | - lege.Boundary{ |
31 | | - Starts: []string{"#"}, |
32 | | - Ends: []string{"\n"}, |
33 | | - }, |
34 | | - }, |
| 9 | +// ToDo represents a ToDo item |
| 10 | +type ToDo struct { |
| 11 | + comments.Comment |
| 12 | + String string |
35 | 13 | } |
36 | 14 |
|
37 | | -// Language is a source language (i.e. "Go") |
38 | | -type Language string |
| 15 | +// ToDos represents a list of ToDo items |
| 16 | +type ToDos []ToDo |
39 | 17 |
|
40 | | -// LanguageParseOptions keeps track of source languages and their corresponding comment options |
41 | | -var LanguageParseOptions map[Language]*lege.ParseOptions = map[Language]*lege.ParseOptions{ |
42 | | - "Go": CStyleCommentOptions, |
43 | | - "Java": CStyleCommentOptions, |
44 | | - "C": CStyleCommentOptions, |
45 | | - "C++": CStyleCommentOptions, |
46 | | - "C#": CStyleCommentOptions, |
47 | | - "JavaScript": CStyleCommentOptions, |
48 | | - "Python": HashStyleCommentOptions, |
49 | | - "Ruby": HashStyleCommentOptions, |
50 | | - "PHP": CStyleCommentOptions, |
51 | | -} |
52 | | - |
53 | | -// ToDo represents a ToDo item |
54 | | -type ToDo struct { |
55 | | - FilePath string |
56 | | - Line int |
57 | | - Position int |
58 | | - String string |
| 18 | +// Count returns the number of todos |
| 19 | +func (t ToDos) Count() int { |
| 20 | + return len(t) |
59 | 21 | } |
60 | 22 |
|
61 | | -// SearchFile searches a file for comments. It infers the language |
62 | | -func SearchFile(filePath string) ([]*ToDo, error) { |
63 | | - todos := make([]*ToDo, 0) |
64 | | - src, err := ioutil.ReadFile(filePath) |
65 | | - if err != nil { |
66 | | - return nil, err |
67 | | - } |
68 | | - lang := Language(enry.GetLanguage(filepath.Base(filePath), src)) |
69 | | - if enry.IsVendor(filePath) { |
70 | | - return nil, nil |
71 | | - } |
72 | | - options, ok := LanguageParseOptions[lang] |
73 | | - if !ok { |
74 | | - return nil, nil |
75 | | - } |
76 | | - commentParser, err := lege.NewParser(options) |
77 | | - if err != nil { |
78 | | - return nil, err |
79 | | - } |
80 | | - comments, err := commentParser.Parse(bytes.NewReader(src)) |
81 | | - if err != nil { |
82 | | - return nil, err |
| 23 | +// NewToDo produces a pointer to a ToDo from a comment |
| 24 | +func NewToDo(comment comments.Comment) *ToDo { |
| 25 | + s := comment.String() |
| 26 | + if !strings.Contains(s, "TODO") { |
| 27 | + return nil |
83 | 28 | } |
| 29 | + s = strings.Replace(comment.String(), "TODO", "", 1) |
| 30 | + s = strings.Trim(s, " ") |
84 | 31 |
|
| 32 | + todo := ToDo{Comment: comment, String: s} |
| 33 | + return &todo |
| 34 | +} |
| 35 | + |
| 36 | +// NewToDos produces a list of ToDos from a list of comments |
| 37 | +func NewToDos(comments comments.Comments) ToDos { |
| 38 | + todos := make(ToDos, 0) |
85 | 39 | for _, comment := range comments { |
86 | | - s := comment.String() |
87 | | - if !strings.Contains(s, "TODO") { |
88 | | - continue |
| 40 | + todo := NewToDo(comment) |
| 41 | + if todo != nil { |
| 42 | + todos = append(todos, *todo) |
89 | 43 | } |
90 | | - s = strings.Replace(comment.String(), "TODO", "", 1) |
91 | | - s = strings.Trim(s, " ") |
92 | | - // fmt.Printf("%q\n", s) |
93 | | - todo := &ToDo{ |
94 | | - FilePath: filePath, |
95 | | - Line: comment.StartLocation.Line, |
96 | | - Position: comment.StartLocation.Pos, |
97 | | - String: s, |
98 | | - } |
99 | | - todos = append(todos, todo) |
100 | 44 | } |
101 | | - |
102 | | - return todos, nil |
| 45 | + return todos |
103 | 46 | } |
0 commit comments