@@ -4,13 +4,16 @@ import (
44 "context"
55 "go/token"
66
7+ "github.com/pkg/errors"
8+
79 "github.com/golangci/golangci-lint/pkg/lint/linter"
810 "github.com/golangci/golangci-lint/pkg/result"
911
1012 "github.com/ultraware/whitespace"
1113)
1214
13- type Whitespace struct {}
15+ type Whitespace struct {
16+ }
1417
1518func (Whitespace ) Name () string {
1619 return "whitespace"
@@ -32,14 +35,40 @@ func (w Whitespace) Run(ctx context.Context, lintCtx *linter.Context) ([]result.
3235
3336 res := make ([]result.Issue , len (issues ))
3437 for k , i := range issues {
35- res [ k ] = result.Issue {
38+ issue : = result.Issue {
3639 Pos : token.Position {
3740 Filename : i .Pos .Filename ,
3841 Line : i .Pos .Line ,
3942 },
40- Text : i .Message ,
41- FromLinter : w .Name (),
43+ Text : i .Message ,
44+ FromLinter : w .Name (),
45+ Replacement : & result.Replacement {},
46+ }
47+
48+ // TODO(jirfag): return more information from Whitespace to get rid of string comparisons
49+ if i .Message == "unnecessary leading newline" {
50+ // cover two lines by the issue: opening bracket "{" (issue.Pos.Line) and following empty line
51+ issue .LineRange = & result.Range {From : issue .Pos .Line , To : issue .Pos .Line + 1 }
52+
53+ bracketLine , err := lintCtx .LineCache .GetLine (issue .Pos .Filename , issue .Pos .Line )
54+ if err != nil {
55+ return nil , errors .Wrapf (err , "failed to get line %s:%d" , issue .Pos .Filename , issue .Pos .Line )
56+ }
57+ issue .Replacement .NewLines = []string {bracketLine }
58+ } else {
59+ // cover two lines by the issue: closing bracket "}" (issue.Pos.Line) and preceding empty line
60+ issue .LineRange = & result.Range {From : issue .Pos .Line - 1 , To : issue .Pos .Line }
61+
62+ bracketLine , err := lintCtx .LineCache .GetLine (issue .Pos .Filename , issue .Pos .Line )
63+ if err != nil {
64+ return nil , errors .Wrapf (err , "failed to get line %s:%d" , issue .Pos .Filename , issue .Pos .Line )
65+ }
66+ issue .Replacement .NewLines = []string {bracketLine }
67+
68+ issue .Pos .Line -- // set in sync with LineRange.From to not break fixer and other code features
4269 }
70+
71+ res [k ] = issue
4372 }
4473
4574 return res , nil
0 commit comments