Skip to content

Commit 4d4e9d6

Browse files
Merge pull request #44 from augmentable-dev/more-match-phrases
Add support for more matching phrases
2 parents fa7055a + 184d42a commit 4d4e9d6

File tree

4 files changed

+68
-14
lines changed

4 files changed

+68
-14
lines changed

pkg/comments/comments.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func SearchFile(filePath string, reader io.Reader, cb func(*Comment)) error {
4343
return nil
4444
}
4545
options, ok := LanguageParseOptions[lang]
46-
if !ok { // TODO provide a default parse option?
46+
if !ok { // TODO provide a default parse option for when we don't know how to handle a language? I.e. default to CStyle comments say
4747
return nil
4848
}
4949
commentParser, err := lege.NewParser(options)

pkg/todos/report.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package todos
22

33
import (
44
"io"
5+
"strings"
56
"text/template"
67
)
78

8-
// DefaultTemplate is the default report template
9-
const DefaultTemplate = `
9+
const defaultTemplate = `
1010
{{- range $index, $todo := . }}
11-
{{ print "\u001b[33m" }}TODO{{ print "\u001b[0m" }}: {{ .String }}
11+
{{ .String }}
1212
=> {{ .Comment.FilePath }}:{{ .Comment.StartLocation.Line }}:{{ .Comment.StartLocation.Pos }}
1313
{{- if .Blame }}
1414
=> added {{ .TimeAgo }} by {{ .Blame.Author }} in {{ .Blame.SHA }}
@@ -22,11 +22,17 @@ no todos 🎉
2222
// WriteTodos renders a report of todos
2323
func WriteTodos(todos ToDos, writer io.Writer) error {
2424

25-
t, err := template.New("todos").Parse(DefaultTemplate)
25+
t, err := template.New("todos").Parse(defaultTemplate)
2626
if err != nil {
2727
return err
2828
}
2929

30+
// replace the phrase in the todo string with a "highlighted" version for console output
31+
// TODO eventually make this configurable, for NO_COLOR output (or customization of color?)
32+
for _, todo := range todos {
33+
todo.String = strings.Replace(todo.String, todo.Phrase, "\u001b[33m"+todo.Phrase+"\u001b[0m", 1)
34+
}
35+
3036
err = t.Execute(writer, todos)
3137
if err != nil {
3238
return err

pkg/todos/todos.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package todos
33
import (
44
"bufio"
55
"context"
6-
"regexp"
76
"strings"
87

98
"github.com/augmentable-dev/tickgit/pkg/blame"
@@ -16,6 +15,7 @@ import (
1615
type ToDo struct {
1716
comments.Comment
1817
String string
18+
Phrase string
1919
Blame *blame.Blame
2020
}
2121

@@ -32,16 +32,28 @@ func (t *ToDo) TimeAgo() string {
3232

3333
// NewToDo produces a pointer to a ToDo from a comment
3434
func NewToDo(comment comments.Comment) *ToDo {
35-
s := comment.String()
36-
if !strings.Contains(s, "TODO") {
37-
return nil
35+
// FIXME this should be configurable and probably NOT hardcoded here
36+
// in fact, this list might be too expansive for a sensible default
37+
startingMatchPhrases := []string{"TODO", "FIXME", "OPTIMIZE", "HACK", "XXX", "WTF", "LEGACY"}
38+
var matchPhrases []string
39+
for _, phrase := range startingMatchPhrases {
40+
// populates matchPhrases with the contents of startingMatchPhrases plus the @+lowerCase version of each phrase
41+
matchPhrases = append(matchPhrases, phrase, "@"+strings.ToLower(phrase))
3842
}
39-
re := regexp.MustCompile(`TODO(:|,)?`)
40-
s = re.ReplaceAllLiteralString(comment.String(), "")
41-
s = strings.Trim(s, " ")
4243

43-
todo := ToDo{Comment: comment, String: s}
44-
return &todo
44+
for _, phrase := range matchPhrases {
45+
s := comment.String()
46+
if strings.Contains(s, phrase) {
47+
todo := ToDo{
48+
Comment: comment,
49+
String: strings.Trim(s, " "),
50+
Phrase: phrase,
51+
}
52+
return &todo
53+
}
54+
}
55+
56+
return nil
4557
}
4658

4759
// NewToDos produces a list of ToDos from a list of comments

pkg/todos/todos_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package todos
2+
3+
import (
4+
"testing"
5+
6+
"github.com/augmentable-dev/lege"
7+
"github.com/augmentable-dev/tickgit/pkg/comments"
8+
)
9+
10+
func TestNewToDoNil(t *testing.T) {
11+
collection := lege.NewCollection(lege.Location{}, lege.Location{}, lege.Boundary{}, "Hello World")
12+
comment := comments.Comment{
13+
Collection: *collection,
14+
}
15+
todo := NewToDo(comment)
16+
17+
if todo != nil {
18+
t.Fatalf("did not expect a TODO, got: %v", todo)
19+
}
20+
}
21+
22+
func TestNewToDo(t *testing.T) {
23+
collection := lege.NewCollection(lege.Location{}, lege.Location{}, lege.Boundary{}, "TODO Hello World")
24+
comment := comments.Comment{
25+
Collection: *collection,
26+
}
27+
todo := NewToDo(comment)
28+
29+
if todo == nil {
30+
t.Fatalf("expected a TODO, got: %v", todo)
31+
}
32+
33+
if todo.Phrase != "TODO" {
34+
t.Fatalf("expected matched phrase to be TODO, got: %s", todo.Phrase)
35+
}
36+
}

0 commit comments

Comments
 (0)