Skip to content

Commit

Permalink
ClocOptions callbacks
Browse files Browse the repository at this point in the history
Signed-off-by: Máximo Cuadros <mcuadros@gmail.com>
  • Loading branch information
mcuadros committed May 23, 2019
1 parent b9c5a4a commit ecc2e24
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
24 changes: 24 additions & 0 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ func AnalyzeReader(filename string, language *Language, file io.Reader, opts *Cl

if len(strings.TrimSpace(line)) == 0 {
clocFile.Blanks++
if opts.OnBlank != nil {
opts.OnBlank(line)
}

if opts.Debug {
fmt.Printf("[BLNK,cd:%d,cm:%d,bk:%d,iscm:%v] %s\n",
clocFile.Code, clocFile.Comments, clocFile.Blanks, isInComments, lineOrg)
Expand All @@ -74,6 +78,10 @@ func AnalyzeReader(filename string, language *Language, file io.Reader, opts *Cl
// shebang line is 'code'
if isFirstLine && strings.HasPrefix(line, "#!") {
clocFile.Code++
if opts.OnCode != nil {
opts.OnCode(line)
}

isFirstLine = false
if opts.Debug {
fmt.Printf("[CODE,cd:%d,cm:%d,bk:%d,iscm:%v] %s\n",
Expand All @@ -90,6 +98,10 @@ func AnalyzeReader(filename string, language *Language, file io.Reader, opts *Cl
for _, singleComment := range language.lineComments {
if strings.HasPrefix(line, singleComment) {
clocFile.Comments++
if opts.OnComment != nil {
opts.OnComment(line)
}

isSingleComment = true
break
}
Expand Down Expand Up @@ -119,6 +131,10 @@ func AnalyzeReader(filename string, language *Language, file io.Reader, opts *Cl
if (multiLine != multiLineEnd) &&
(strings.HasSuffix(line, multiLine) || strings.HasPrefix(line, multiLineEnd)) {
clocFile.Code++
if opts.OnCode != nil {
opts.OnCode(line)
}

isCode = true
if opts.Debug {
fmt.Printf("[CODE,cd:%d,cm:%d,bk:%d,iscm:%v] %s\n",
Expand Down Expand Up @@ -155,6 +171,10 @@ func AnalyzeReader(filename string, language *Language, file io.Reader, opts *Cl
}
}
clocFile.Comments++
if opts.OnComment != nil {
opts.OnComment(line)
}

if opts.Debug {
fmt.Printf("[COMM,cd:%d,cm:%d,bk:%d,iscm:%v,iscms:%v] %s\n",
clocFile.Code, clocFile.Comments, clocFile.Blanks, isInComments, isInCommentsSame, lineOrg)
Expand All @@ -163,6 +183,10 @@ func AnalyzeReader(filename string, language *Language, file io.Reader, opts *Cl
}

clocFile.Code++
if opts.OnCode != nil {
opts.OnCode(line)
}

if opts.Debug {
fmt.Printf("[CODE,cd:%d,cm:%d,bk:%d,iscm:%v] %s\n",
clocFile.Code, clocFile.Comments, clocFile.Blanks, isInComments, lineOrg)
Expand Down
37 changes: 37 additions & 0 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,40 @@ class A:
t.Errorf("invalid logic. code=%v", clocFile.Code)
}
}

func TestAnalayzeReader_OnCallbacks(t *testing.T) {
buf := bytes.NewBuffer([]byte(`foo
"""bar
`))

var lines int
language := NewLanguage("Python", []string{"#"}, [][]string{{"\"\"\"", "\"\"\""}})
clocOpts := NewClocOptions()
clocOpts.OnCode = func(line string) {
if line != "foo" {
t.Errorf("invalid logic. code_line=%v", line)
}
lines++
}

clocOpts.OnBlank = func(line string) {
if line != "" {
t.Errorf("invalid logic. blank_line=%v", line)
}
lines++
}

clocOpts.OnComment = func(line string) {
if line != "\"\"\"bar" {
t.Errorf("invalid logic. comment_line=%v", line)
}
lines++
}

AnalyzeReader("test.py", language, buf, clocOpts)

if lines != 3 {
t.Errorf("invalid logic. lines=%v", lines)
}
}
7 changes: 7 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ type ClocOptions struct {
IncludeLangs map[string]struct{}
ReNotMatchDir *regexp.Regexp
ReMatchDir *regexp.Regexp

// OnCode is triggered for each line of code.
OnCode func(line string)
// OnBlack is triggered for each blank line.
OnBlank func(line string)
// OnComment is triggered for each line of comments.
OnComment func(line string)
}

func NewClocOptions() *ClocOptions {
Expand Down

0 comments on commit ecc2e24

Please sign in to comment.