@@ -2,7 +2,6 @@ package processors
2
2
3
3
import (
4
4
"bytes"
5
- "fmt"
6
5
"os"
7
6
"slices"
8
7
@@ -12,7 +11,6 @@ import (
12
11
"github.com/golangci/golangci-lint/pkg/goformatters/gofmt"
13
12
"github.com/golangci/golangci-lint/pkg/goformatters/gofumpt"
14
13
"github.com/golangci/golangci-lint/pkg/goformatters/goimports"
15
- "github.com/golangci/golangci-lint/pkg/lint/linter"
16
14
"github.com/golangci/golangci-lint/pkg/logutils"
17
15
"github.com/golangci/golangci-lint/pkg/result"
18
16
)
@@ -22,38 +20,17 @@ import (
22
20
// - The code format is applied after the fixes to avoid changing positions.
23
21
// - The [Fixer] writes the files on the disk (so the file cache cannot be used as it contains the files before fixes).
24
22
type Formatter struct {
25
- log logutils.Log
26
- cfg * config.Config
27
- formatters [] goformatters.Formatter
23
+ log logutils.Log
24
+ cfg * config.Config
25
+ formatter * goformatters.MetaFormatter
28
26
}
29
27
30
28
// NewFormatter creates a new [Formatter].
31
- func NewFormatter (log logutils.Log , cfg * config.Config , enabledLinters map [ string ] * linter. Config ) (* Formatter , error ) {
29
+ func NewFormatter (log logutils.Log , cfg * config.Config , formatter * goformatters. MetaFormatter ) (* Formatter , error ) {
32
30
p := & Formatter {
33
- log : log ,
34
- cfg : cfg ,
35
- }
36
-
37
- if _ , ok := enabledLinters [gofmt .Name ]; ok {
38
- p .formatters = append (p .formatters , gofmt .New (cfg .LintersSettings .Gofmt ))
39
- }
40
-
41
- if _ , ok := enabledLinters [gofumpt .Name ]; ok {
42
- p .formatters = append (p .formatters , gofumpt .New (cfg .LintersSettings .Gofumpt , cfg .Run .Go ))
43
- }
44
-
45
- if _ , ok := enabledLinters [goimports .Name ]; ok {
46
- p .formatters = append (p .formatters , goimports .New ())
47
- }
48
-
49
- // gci is a last because the only goal of gci is to handle imports.
50
- if _ , ok := enabledLinters [gci .Name ]; ok {
51
- formatter , err := gci .New (cfg .LintersSettings .Gci )
52
- if err != nil {
53
- return nil , fmt .Errorf ("gci: creating formatter: %w" , err )
54
- }
55
-
56
- p .formatters = append (p .formatters , formatter )
31
+ log : log ,
32
+ cfg : cfg ,
33
+ formatter : formatter ,
57
34
}
58
35
59
36
return p , nil
@@ -68,10 +45,6 @@ func (p *Formatter) Process(issues []result.Issue) ([]result.Issue, error) {
68
45
return issues , nil
69
46
}
70
47
71
- if len (p .formatters ) == 0 {
72
- return issues , nil
73
- }
74
-
75
48
all := []string {gofumpt .Name , goimports .Name , gofmt .Name , gci .Name }
76
49
77
50
var notFixableIssues []result.Issue
@@ -95,7 +68,7 @@ func (p *Formatter) Process(issues []result.Issue) ([]result.Issue, error) {
95
68
continue
96
69
}
97
70
98
- formatted := p .format (target , content )
71
+ formatted := p .formatter . Format (target , content )
99
72
if bytes .Equal (content , formatted ) {
100
73
continue
101
74
}
@@ -109,20 +82,4 @@ func (p *Formatter) Process(issues []result.Issue) ([]result.Issue, error) {
109
82
return notFixableIssues , nil
110
83
}
111
84
112
- func (p * Formatter ) format (filename string , src []byte ) []byte {
113
- data := bytes .Clone (src )
114
-
115
- for _ , formatter := range p .formatters {
116
- formatted , err := formatter .Format (filename , data )
117
- if err != nil {
118
- p .log .Warnf ("(%s) formatting file %s: %v" , formatter .Name (), filename , err )
119
- continue
120
- }
121
-
122
- data = formatted
123
- }
124
-
125
- return data
126
- }
127
-
128
85
func (* Formatter ) Finish () {}
0 commit comments