Skip to content

Commit d188494

Browse files
author
Jonathan Gautheron
committed
Friendlier output for IDEs
1 parent 163c92e commit d188494

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

cmd/goconst/main.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"log"
88
"os"
9+
"strings"
910

1011
"github.com/jgautheron/goconst"
1112
)
@@ -91,11 +92,18 @@ func printOutput(strs goconst.Strings, consts goconst.Constants, output string,
9192
})
9293
case "text":
9394
for str, item := range strs {
94-
fmt.Printf(`%d occurrences of "%s" found:`, len(item), str)
9595
for _, xpos := range item {
96-
fmt.Printf("\n\t%s", xpos.String())
96+
fmt.Printf(
97+
`%s:%d:%d:%d other occurrence(s) of "%s" found in: %s`,
98+
xpos.Filename,
99+
xpos.Line,
100+
xpos.Column,
101+
len(item)-1,
102+
str,
103+
occurrences(item, xpos),
104+
)
105+
fmt.Print("\n")
97106
}
98-
fmt.Print("\n")
99107

100108
if len(consts) == 0 {
101109
continue
@@ -110,3 +118,16 @@ func printOutput(strs goconst.Strings, consts goconst.Constants, output string,
110118
fmt.Printf(`Unsupported output format: %s`, output)
111119
}
112120
}
121+
122+
func occurrences(item []goconst.ExtendedPos, current goconst.ExtendedPos) string {
123+
occurrences := []string{}
124+
for _, xpos := range item {
125+
if xpos == current {
126+
continue
127+
}
128+
occurrences = append(occurrences, fmt.Sprintf(
129+
"%s:%d:%d", xpos.Filename, xpos.Line, xpos.Column,
130+
))
131+
}
132+
return strings.Join(occurrences, " ")
133+
}

parser.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
// Package goconst finds repeated strings that could be replaced by a constant.
2+
//
3+
// There are obvious benefits to using constants instead of repeating strings,
4+
// mostly to ease maintenance. Cannot argue against changing a single constant versus many strings.
5+
// While this could be considered a beginner mistake, across time,
6+
// multiple packages and large codebases, some repetition could have slipped in.
17
package goconst
28

39
import (
@@ -25,6 +31,8 @@ type Parser struct {
2531
consts Constants
2632
}
2733

34+
// New creates a new instance of the parser.
35+
// This is your entry point if you'd like to use goconst as an API.
2836
func New(path, ignore string, ignoreTests, matchConstant bool) *Parser {
2937
return &Parser{
3038
path: path,
@@ -38,6 +46,8 @@ func New(path, ignore string, ignoreTests, matchConstant bool) *Parser {
3846
}
3947
}
4048

49+
// ParseTree will search the given path for occurrences that could be moved into constants.
50+
// If "..." is appended, the search will be recursive.
4151
func (p *Parser) ParseTree() (Strings, Constants, error) {
4252
pathLen := len(p.path)
4353
// Parse recursively the given path if the recursive notation is found

0 commit comments

Comments
 (0)