forked from golangci/golangci-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
text.go
157 lines (133 loc) · 3.75 KB
/
text.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package printers
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"strings"
"time"
"github.com/fatih/color"
"github.com/golangci/golangci-lint/pkg/logutils"
"github.com/golangci/golangci-lint/pkg/result"
)
type linesCache [][]byte
type filesCache map[string]linesCache
type Text struct {
printIssuedLine bool
useColors bool
printLinterName bool
silent bool
cache filesCache
log logutils.Log
}
func NewText(printIssuedLine, useColors, printLinterName bool, silent bool, log logutils.Log) *Text {
return &Text{
printIssuedLine: printIssuedLine,
useColors: useColors,
printLinterName: printLinterName,
silent: silent,
cache: filesCache{},
log: log,
}
}
func (p Text) SprintfColored(ca color.Attribute, format string, args ...interface{}) string {
if !p.useColors {
return fmt.Sprintf(format, args...)
}
c := color.New(ca)
return c.Sprintf(format, args...)
}
func (p *Text) getFileLinesForIssue(i *result.Issue) (linesCache, error) {
fc := p.cache[i.FilePath()]
if fc != nil {
return fc, nil
}
// TODO: make more optimal algorithm: don't load all files into memory
fileBytes, err := ioutil.ReadFile(i.FilePath())
if err != nil {
return nil, fmt.Errorf("can't read file %s for printing issued line: %s", i.FilePath(), err)
}
lines := bytes.Split(fileBytes, []byte("\n")) // TODO: what about \r\n?
fc = lines
p.cache[i.FilePath()] = fc
return fc, nil
}
func (p *Text) Print(ctx context.Context, issues <-chan result.Issue) (bool, error) {
var issuedLineExtractingDuration time.Duration
defer func() {
p.log.Infof("Extracting issued lines took %s", issuedLineExtractingDuration)
}()
issuesN := 0
for i := range issues {
issuesN++
p.printIssue(&i)
if !p.printIssuedLine {
continue
}
startedAt := time.Now()
lines, err := p.getFileLinesForIssue(&i)
if err != nil {
return false, err
}
issuedLineExtractingDuration += time.Since(startedAt)
p.printIssuedLines(&i, lines)
if i.Line()-1 < len(lines) {
p.printUnderLinePointer(&i, string(lines[i.Line()-1]))
}
}
if issuesN != 0 {
p.log.Infof("Found %d issues", issuesN)
} else if ctx.Err() == nil { // don't print "congrats" if timeouted
if !p.silent {
outStr := p.SprintfColored(color.FgGreen, "Congrats! No issues were found.")
fmt.Fprintln(logutils.StdOut, outStr)
}
}
return issuesN != 0, nil
}
func (p Text) printIssue(i *result.Issue) {
text := p.SprintfColored(color.FgRed, "%s", i.Text)
if p.printLinterName {
text += fmt.Sprintf(" (%s)", i.FromLinter)
}
pos := p.SprintfColored(color.Bold, "%s:%d", i.FilePath(), i.Line())
if i.Pos.Column != 0 {
pos += fmt.Sprintf(":%d", i.Pos.Column)
}
fmt.Fprintf(logutils.StdOut, "%s: %s\n", pos, text)
}
func (p Text) printIssuedLines(i *result.Issue, lines linesCache) {
lineRange := i.GetLineRange()
var lineStr string
for line := lineRange.From; line <= lineRange.To; line++ {
if line == 0 { // some linters, e.g. gas can do it: it really means first line
line = 1
}
zeroIndexedLine := line - 1
if zeroIndexedLine >= len(lines) {
p.log.Warnf("No line %d in file %s", line, i.FilePath())
break
}
lineStr = string(bytes.Trim(lines[zeroIndexedLine], "\r"))
fmt.Fprintln(logutils.StdOut, lineStr)
}
}
func (p Text) printUnderLinePointer(i *result.Issue, line string) {
lineRange := i.GetLineRange()
if lineRange.From != lineRange.To || i.Pos.Column == 0 {
return
}
var j int
for ; j < len(line) && line[j] == '\t'; j++ {
}
tabsCount := j
spacesCount := i.Pos.Column - 1 - tabsCount
prefix := ""
if tabsCount != 0 {
prefix += strings.Repeat("\t", tabsCount)
}
if spacesCount != 0 {
prefix += strings.Repeat(" ", spacesCount)
}
fmt.Fprintf(logutils.StdOut, "%s%s\n", prefix, p.SprintfColored(color.FgYellow, "^"))
}