Skip to content

Commit

Permalink
Use Inline instead of NewLine fix
Browse files Browse the repository at this point in the history
  • Loading branch information
bombsimon committed Aug 11, 2023
1 parent dd67e40 commit 9ab7274
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
13 changes: 6 additions & 7 deletions pkg/golinters/whitespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,13 @@ func NewWhitespace(settings *config.WhitespaceSettings) *goanalysis.Linter {
}

case whitespace.MessageTypeAdd:
position := pass.Fset.PositionFor(issue.Diagnostic, false)
bracketLine, err := lintCtx.LineCache.GetLine(position.Filename, position.Line)
if err != nil {
return nil, fmt.Errorf("failed to get line %s:%d: %w", position.Filename, position.Line, err)
}

report.Pos = pass.Fset.PositionFor(issue.FixStart, false)
report.Replacement = &result.Replacement{
NewLines: []string{bracketLine + "\n"},
Inline: &result.InlineFix{
StartCol: 0,
Length: 1,
NewString: "\n\t",
},
}

default:
Expand Down
11 changes: 11 additions & 0 deletions test/testdata/fix/in/whitespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,15 @@ func multiIfFunc() {
2 == 2 {
fmt.Println("Hello multi-line world")
}

if true {
if true {
if true {
if 1 == 1 &&
2 == 2 {
fmt.Println("Hello nested multi-line world")
}
}
}
}
}
12 changes: 12 additions & 0 deletions test/testdata/fix/out/whitespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,16 @@ func multiIfFunc() {

fmt.Println("Hello multi-line world")
}

if true {
if true {
if true {
if 1 == 1 &&
2 == 2 {

fmt.Println("Hello nested multi-line world")
}
}
}
}
}

0 comments on commit 9ab7274

Please sign in to comment.