.gitignore added
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / honnef.co / go / tools@v0.1.1 / lintcmd / format.go
1 package lintcmd
2
3 import (
4         "encoding/json"
5         "fmt"
6         "go/token"
7         "io"
8         "os"
9         "path/filepath"
10         "text/tabwriter"
11 )
12
13 func shortPath(path string) string {
14         cwd, err := os.Getwd()
15         if err != nil {
16                 return path
17         }
18         if rel, err := filepath.Rel(cwd, path); err == nil && len(rel) < len(path) {
19                 return rel
20         }
21         return path
22 }
23
24 func relativePositionString(pos token.Position) string {
25         s := shortPath(pos.Filename)
26         if pos.IsValid() {
27                 if s != "" {
28                         s += ":"
29                 }
30                 s += fmt.Sprintf("%d:%d", pos.Line, pos.Column)
31         }
32         if s == "" {
33                 s = "-"
34         }
35         return s
36 }
37
38 type statter interface {
39         Stats(total, errors, warnings, ignored int)
40 }
41
42 type formatter interface {
43         Format(p problem)
44 }
45
46 type textFormatter struct {
47         W io.Writer
48 }
49
50 func (o textFormatter) Format(p problem) {
51         fmt.Fprintf(o.W, "%s: %s\n", relativePositionString(p.Position), p.String())
52         for _, r := range p.Related {
53                 fmt.Fprintf(o.W, "\t%s: %s\n", relativePositionString(r.Position), r.Message)
54         }
55 }
56
57 type nullFormatter struct{}
58
59 func (nullFormatter) Format(problem) {}
60
61 type jsonFormatter struct {
62         W io.Writer
63 }
64
65 func (o jsonFormatter) Format(p problem) {
66         type location struct {
67                 File   string `json:"file"`
68                 Line   int    `json:"line"`
69                 Column int    `json:"column"`
70         }
71         type related struct {
72                 Location location `json:"location"`
73                 End      location `json:"end"`
74                 Message  string   `json:"message"`
75         }
76         jp := struct {
77                 Code     string    `json:"code"`
78                 Severity string    `json:"severity,omitempty"`
79                 Location location  `json:"location"`
80                 End      location  `json:"end"`
81                 Message  string    `json:"message"`
82                 Related  []related `json:"related,omitempty"`
83         }{
84                 Code:     p.Category,
85                 Severity: p.Severity.String(),
86                 Location: location{
87                         File:   p.Position.Filename,
88                         Line:   p.Position.Line,
89                         Column: p.Position.Column,
90                 },
91                 End: location{
92                         File:   p.End.Filename,
93                         Line:   p.End.Line,
94                         Column: p.End.Column,
95                 },
96                 Message: p.Message,
97         }
98         for _, r := range p.Related {
99                 jp.Related = append(jp.Related, related{
100                         Location: location{
101                                 File:   r.Position.Filename,
102                                 Line:   r.Position.Line,
103                                 Column: r.Position.Column,
104                         },
105                         End: location{
106                                 File:   r.End.Filename,
107                                 Line:   r.End.Line,
108                                 Column: r.End.Column,
109                         },
110                         Message: r.Message,
111                 })
112         }
113         _ = json.NewEncoder(o.W).Encode(jp)
114 }
115
116 type stylishFormatter struct {
117         W io.Writer
118
119         prevFile string
120         tw       *tabwriter.Writer
121 }
122
123 func (o *stylishFormatter) Format(p problem) {
124         pos := p.Position
125         if pos.Filename == "" {
126                 pos.Filename = "-"
127         }
128
129         if pos.Filename != o.prevFile {
130                 if o.prevFile != "" {
131                         o.tw.Flush()
132                         fmt.Fprintln(o.W)
133                 }
134                 fmt.Fprintln(o.W, pos.Filename)
135                 o.prevFile = pos.Filename
136                 o.tw = tabwriter.NewWriter(o.W, 0, 4, 2, ' ', 0)
137         }
138         fmt.Fprintf(o.tw, "  (%d, %d)\t%s\t%s\n", pos.Line, pos.Column, p.Category, p.Message)
139         for _, r := range p.Related {
140                 fmt.Fprintf(o.tw, "    (%d, %d)\t\t  %s\n", r.Position.Line, r.Position.Column, r.Message)
141         }
142 }
143
144 func (o *stylishFormatter) Stats(total, errors, warnings, ignored int) {
145         if o.tw != nil {
146                 o.tw.Flush()
147                 fmt.Fprintln(o.W)
148         }
149         fmt.Fprintf(o.W, " ✖ %d problems (%d errors, %d warnings, %d ignored)\n",
150                 total, errors, warnings, ignored)
151 }