Skip to content

Commit e17b954

Browse files
jfrabautejirfag
authored andcommitted
Add tab-width option to lll linter
1 parent 6ccd0c5 commit e17b954

File tree

6 files changed

+20
-7
lines changed

6 files changed

+20
-7
lines changed

.golangci.example.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,11 @@ linters-settings:
107107
# Setting locale to US will correct the British spelling of 'colour' to 'color'.
108108
locale: US
109109
lll:
110-
# max line length, lines longer will be reported. Default is 120. '\t' is counted as 1 character.
110+
# max line length, lines longer will be reported. Default is 120.
111+
# '\t' is counted as 1 character by default, and can be changed with the tab-width option
111112
line-length: 120
113+
# tab width in spaces. Default to 1.
114+
tab-width: 1
112115
unused:
113116
# treat code as a program (not a library) and report unused exported identifiers; default is false.
114117
# XXX: if you enable this setting, unused will report a lot of false-positives in text editors:

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -520,8 +520,11 @@ linters-settings:
520520
# Setting locale to US will correct the British spelling of 'colour' to 'color'.
521521
locale: US
522522
lll:
523-
# max line length, lines longer will be reported. Default is 120. '\t' is counted as 1 character.
523+
# max line length, lines longer will be reported. Default is 120.
524+
# '\t' is counted as 1 character by default, and can be changed with the tab-width option
524525
line-length: 120
526+
# tab width in spaces. Default to 1.
527+
tab-width: 1
525528
unused:
526529
# treat code as a program (not a library) and report unused exported identifiers; default is false.
527530
# XXX: if you enable this setting, unused will report a lot of false-positives in text editors:

pkg/commands/run.go

+4
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config) {
124124
"Depguard: check list against standard lib")
125125
hideFlag("depguard.include-go-root")
126126

127+
fs.IntVar(&lsc.Lll.TabWidth, "lll.tab-width", 1,
128+
"Lll: tab width in spaces")
129+
hideFlag("lll.tab-width")
130+
127131
// Linters config
128132
lc := &cfg.Linters
129133
fs.StringSliceVarP(&lc.Enable, "enable", "E", nil, wh("Enable specific linter"))

pkg/config/config.go

+2
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ type LintersSettings struct {
168168

169169
type LllSettings struct {
170170
LineLength int `mapstructure:"line-length"`
171+
TabWidth int `mapstructure:"tab-width"`
171172
}
172173

173174
type UnparamSettings struct {
@@ -188,6 +189,7 @@ type PreallocSettings struct {
188189
var defaultLintersSettings = LintersSettings{
189190
Lll: LllSettings{
190191
LineLength: 120,
192+
TabWidth: 1,
191193
},
192194
Unparam: UnparamSettings{
193195
Algo: "cha",

pkg/golinters/lll.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func (Lll) Desc() string {
2323
return "Reports long lines"
2424
}
2525

26-
func (lint Lll) getIssuesForFile(filename string, maxLineLen int) ([]result.Issue, error) {
26+
func (lint Lll) getIssuesForFile(filename string, maxLineLen int, tabSpaces string) ([]result.Issue, error) {
2727
var res []result.Issue
2828

2929
f, err := os.Open(filename)
@@ -36,7 +36,7 @@ func (lint Lll) getIssuesForFile(filename string, maxLineLen int) ([]result.Issu
3636
scanner := bufio.NewScanner(f)
3737
for scanner.Scan() {
3838
line := scanner.Text()
39-
line = strings.Replace(line, "\t", " ", -1)
39+
line = strings.Replace(line, "\t", tabSpaces, -1)
4040
lineLen := utf8.RuneCountInString(line)
4141
if lineLen > maxLineLen {
4242
res = append(res, result.Issue{
@@ -61,8 +61,9 @@ func (lint Lll) getIssuesForFile(filename string, maxLineLen int) ([]result.Issu
6161

6262
func (lint Lll) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) {
6363
var res []result.Issue
64+
spaces := strings.Repeat(" ", lintCtx.Settings().Lll.TabWidth)
6465
for _, f := range lintCtx.PkgProgram.Files(lintCtx.Cfg.Run.AnalyzeTests) {
65-
issues, err := lint.getIssuesForFile(f, lintCtx.Settings().Lll.LineLength)
66+
issues, err := lint.getIssuesForFile(f, lintCtx.Settings().Lll.LineLength, spaces)
6667
if err != nil {
6768
return nil, err
6869
}

test/testdata/lll.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// args: -Elll
1+
// args: -Elll --lll.tab-width 4
22
package testdata
33

44
func Lll() {
5-
// In my experience, long lines are the lines with comments, not the code. So this is a long comment // ERROR "line is 135 characters"
5+
// In my experience, long lines are the lines with comments, not the code. So this is a long comment // ERROR "line is 138 characters"
66
}

0 commit comments

Comments
 (0)