.gitignore added
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / mvdan.cc / gofumpt@v0.1.0 / README.md
1 # gofumpt
2
3         GO111MODULE=on go get mvdan.cc/gofumpt
4
5 Enforce a stricter format than `gofmt`, while being backwards compatible. That
6 is, `gofumpt` is happy with a subset of the formats that `gofmt` is happy with.
7
8 The tool is a modified fork of `gofmt`, so it can be used as a drop-in
9 replacement. Running `gofmt` after `gofumpt` should be a no-op.
10
11 Most of the Go source files in this repository belong to the Go project.
12 The added formatting rules are in the `format` package.
13
14 ### Added rules
15
16 No empty lines at the beginning or end of a function
17
18 <details><summary><i>example</i></summary>
19
20 ```
21 func foo() {
22         println("bar")
23
24 }
25 ```
26
27 ```
28 func foo() {
29         println("bar")
30 }
31 ```
32
33 </details>
34
35 No empty lines around a lone statement (or comment) in a block
36
37 <details><summary><i>example</i></summary>
38
39 ```
40 if err != nil {
41
42         return err
43 }
44 ```
45
46 ```
47 if err != nil {
48         return err
49 }
50 ```
51
52 </details>
53
54 No empty lines before a simple error check
55
56 <details><summary><i>example</i></summary>
57
58 ```
59 foo, err := processFoo()
60
61 if err != nil {
62         return err
63 }
64 ```
65
66 ```
67 foo, err := processFoo()
68 if err != nil {
69         return err
70 }
71 ```
72
73 </details>
74
75 Composite literals should use newlines consistently
76
77 <details><summary><i>example</i></summary>
78
79 ```
80 // A newline before or after an element requires newlines for the opening and
81 // closing braces.
82 var ints = []int{1, 2,
83         3, 4}
84
85 // A newline between consecutive elements requires a newline between all
86 // elements.
87 var matrix = [][]int{
88         {1},
89         {2}, {
90                 3,
91         },
92 }
93 ```
94
95 ```
96 var ints = []int{
97         1, 2,
98         3, 4,
99 }
100
101 var matrix = [][]int{
102         {1},
103         {2},
104         {
105                 3,
106         },
107 }
108 ```
109
110 </details>
111
112 Empty field lists should use a single line
113
114 <details><summary><i>example</i></summary>
115
116 ```
117 var V interface {
118 } = 3
119
120 type T struct {
121 }
122
123 func F(
124 )
125 ```
126
127 ```
128 var V interface{} = 3
129
130 type T struct{}
131
132 func F()
133 ```
134
135 ```
136 var ints = []int{
137         1, 2,
138         3, 4,
139 }
140
141 var matrix = [][]int{
142         {1},
143         {2},
144         {
145                 3,
146         },
147 }
148 ```
149
150 </details>
151
152 `std` imports must be in a separate group at the top
153
154 <details><summary><i>example</i></summary>
155
156 ```
157 import (
158         "foo.com/bar"
159
160         "io"
161
162         "io/ioutil"
163 )
164 ```
165
166 ```
167 import (
168         "io"
169         "io/ioutil"
170
171         "foo.com/bar"
172 )
173 ```
174
175 </details>
176
177 Short case clauses should take a single line
178
179 <details><summary><i>example</i></summary>
180
181 ```
182 switch c {
183 case 'a', 'b',
184         'c', 'd':
185 }
186 ```
187
188 ```
189 switch c {
190 case 'a', 'b', 'c', 'd':
191 }
192 ```
193
194 </details>
195
196 Multiline top-level declarations must be separated by empty lines
197
198 <details><summary><i>example</i></summary>
199
200 ```
201 func foo() {
202         println("multiline foo")
203 }
204 func bar() {
205         println("multiline bar")
206 }
207 ```
208
209 ```
210 func foo() {
211         println("multiline foo")
212 }
213
214 func bar() {
215         println("multiline bar")
216 }
217 ```
218
219 </details>
220
221 Single var declarations should not be grouped with parentheses
222
223 <details><summary><i>example</i></summary>
224
225 ```
226 var (
227         foo = "bar"
228 )
229 ```
230
231 ```
232 var foo = "bar"
233 ```
234
235 </details>
236
237 Contiguous top-level declarations should be grouped together
238
239 <details><summary><i>example</i></summary>
240
241 ```
242 var nicer = "x"
243 var with = "y"
244 var alignment = "z"
245 ```
246
247 ```
248 var (
249         nicer     = "x"
250         with      = "y"
251         alignment = "z"
252 )
253 ```
254
255 </details>
256
257
258 Simple var-declaration statements should use short assignments
259
260 <details><summary><i>example</i></summary>
261
262 ```
263 var s = "somestring"
264 ```
265
266 ```
267 s := "somestring"
268 ```
269
270 </details>
271
272
273 The `-s` code simplification flag is enabled by default
274
275 <details><summary><i>example</i></summary>
276
277 ```
278 var _ = [][]int{[]int{1}}
279 ```
280
281 ```
282 var _ = [][]int{{1}}
283 ```
284
285 </details>
286
287
288 Octal integer literals should use the `0o` prefix on modules using Go 1.13 and later
289
290 <details><summary><i>example</i></summary>
291
292 ```
293 const perm = 0755
294 ```
295
296 ```
297 const perm = 0o755
298 ```
299
300 </details>
301
302 Comments which aren't Go directives should start with a whitespace
303
304 <details><summary><i>example</i></summary>
305
306 ```
307 //go:noinline
308
309 //Foo is awesome.
310 func Foo() {}
311 ```
312
313 ```
314 //go:noinline
315
316 // Foo is awesome.
317 func Foo() {}
318 ```
319
320 </details>
321
322 #### Extra rules behind `-extra`
323
324 Adjacent parameters with the same type should be grouped together
325
326 <details><summary><i>example</i></summary>
327
328 ```
329 func Foo(bar string, baz string) {}
330 ```
331
332 ```
333 func Foo(bar, baz string) {}
334 ```
335
336 </details>
337
338 ### Installation
339
340 `gofumpt` is a replacement for `gofmt`, so you can simply `go get` it as
341 described at the top of this README and use it.
342
343 When using an IDE or editor with Go integrations, it's best to use `gofumpt` as
344 part of `gopls`. The instructions below show how to do that for some of the
345 major editors out there.
346
347 #### Visual Studio Code
348
349 Enable the language server following [the official docs](https://github.com/golang/tools/blob/master/gopls/doc/vscode.md),
350 and then enable gopls's `gofumpt` option. Note that VS Code will complain about
351 the `gopls` settings, but they will still work.
352
353 ```json
354 "go.useLanguageServer": true,
355 "gopls": {
356         "gofumpt": true,
357 },
358 ```
359
360 #### Goland
361
362 Once `gofumpt` is installed, follow the steps below:
363
364 - Open **Settings** (File > Settings)
365 - Open the **Tools** section
366 - Find the *File Watchers* sub-section
367 - Click on the `+` on the right side to add a new file watcher
368 - Choose *Custom Template*
369
370 When a window asks for settings, you can enter the following:
371
372 * File Types: Select all .go files
373 * Scope: Project Files
374 * Program: Select your `gofumpt` executable
375 * Arguments: `-w $FilePath$`
376 * Output path to refresh: `$FilePath$`
377 * Working directory: `$ProjectFileDir$`
378 * Environment variables: `GOROOT=$GOROOT$;GOPATH=$GOPATH$;PATH=$GoBinDirs$`
379
380 To avoid unecessary runs, you should disable all checkboxes in the *Advanced* section.
381
382 #### Vim-go
383
384 Ensure you are at least running version
385 [v1.24](https://github.com/fatih/vim-go/blob/master/CHANGELOG.md#v124---september-15-2020),
386 and set up `gopls` for formatting code with `gofumpt`:
387
388 ```vim
389 let g:go_fmt_command="gopls"
390 let g:go_gopls_gofumpt=1
391 ```
392
393 #### Govim
394
395 With a [new enough version of govim](https://github.com/govim/govim/pull/1005),
396 simply configure `gopls` to use `gofumpt`:
397
398 ```vim
399 call govim#config#Set("Gofumpt", 1)
400 ```
401
402 ### Roadmap
403
404 This tool is a place to experiment. In the long term, the features that work
405 well might be proposed for `gofmt` itself.
406
407 The tool is also compatible with `gofmt` and is aimed to be stable, so you can
408 rely on it for your code as long as you pin a version of it.
409
410 ### License
411
412 Note that much of the code is copied from Go's `gofmt` and `goimports` commands.
413 You can tell which files originate from the Go repository from their copyright
414 headers. Their license file is `LICENSE.google`.
415
416 `gofumpt`'s original source files are also under the 3-clause BSD license, with
417 the separate file `LICENSE`.