Skip to content

Commit

Permalink
Add tab-width option to lll linter
Browse files Browse the repository at this point in the history
  • Loading branch information
jfrabaute authored and jirfag committed Jul 25, 2018
1 parent 6ccd0c5 commit e17b954
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 7 deletions.
5 changes: 4 additions & 1 deletion .golangci.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,11 @@ linters-settings:
# Setting locale to US will correct the British spelling of 'colour' to 'color'.
locale: US
lll:
# max line length, lines longer will be reported. Default is 120. '\t' is counted as 1 character.
# max line length, lines longer will be reported. Default is 120.
# '\t' is counted as 1 character by default, and can be changed with the tab-width option
line-length: 120
# tab width in spaces. Default to 1.
tab-width: 1
unused:
# treat code as a program (not a library) and report unused exported identifiers; default is false.
# XXX: if you enable this setting, unused will report a lot of false-positives in text editors:
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,11 @@ linters-settings:
# Setting locale to US will correct the British spelling of 'colour' to 'color'.
locale: US
lll:
# max line length, lines longer will be reported. Default is 120. '\t' is counted as 1 character.
# max line length, lines longer will be reported. Default is 120.
# '\t' is counted as 1 character by default, and can be changed with the tab-width option
line-length: 120
# tab width in spaces. Default to 1.
tab-width: 1
unused:
# treat code as a program (not a library) and report unused exported identifiers; default is false.
# XXX: if you enable this setting, unused will report a lot of false-positives in text editors:
Expand Down
4 changes: 4 additions & 0 deletions pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config) {
"Depguard: check list against standard lib")
hideFlag("depguard.include-go-root")

fs.IntVar(&lsc.Lll.TabWidth, "lll.tab-width", 1,
"Lll: tab width in spaces")
hideFlag("lll.tab-width")

// Linters config
lc := &cfg.Linters
fs.StringSliceVarP(&lc.Enable, "enable", "E", nil, wh("Enable specific linter"))
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ type LintersSettings struct {

type LllSettings struct {
LineLength int `mapstructure:"line-length"`
TabWidth int `mapstructure:"tab-width"`
}

type UnparamSettings struct {
Expand All @@ -188,6 +189,7 @@ type PreallocSettings struct {
var defaultLintersSettings = LintersSettings{
Lll: LllSettings{
LineLength: 120,
TabWidth: 1,
},
Unparam: UnparamSettings{
Algo: "cha",
Expand Down
7 changes: 4 additions & 3 deletions pkg/golinters/lll.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (Lll) Desc() string {
return "Reports long lines"
}

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

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

func (lint Lll) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) {
var res []result.Issue
spaces := strings.Repeat(" ", lintCtx.Settings().Lll.TabWidth)
for _, f := range lintCtx.PkgProgram.Files(lintCtx.Cfg.Run.AnalyzeTests) {
issues, err := lint.getIssuesForFile(f, lintCtx.Settings().Lll.LineLength)
issues, err := lint.getIssuesForFile(f, lintCtx.Settings().Lll.LineLength, spaces)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions test/testdata/lll.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// args: -Elll
// args: -Elll --lll.tab-width 4
package testdata

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

0 comments on commit e17b954

Please sign in to comment.