Skip to content

Commit ce4af06

Browse files
committed
Fix review comments
* Fix auto toggle command * Add guard agains Go versions lower than 1.11 * Add documentation * Fixed setting correct filetype * Fixed parsing errors * Changed variable names to be consistent
1 parent 3d725eb commit ce4af06

File tree

7 files changed

+68
-23
lines changed

7 files changed

+68
-23
lines changed

autoload/go/config.vim

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,12 @@ function! go#config#SetAsmfmtAutosave(value) abort
278278
let g:go_asmfmt_autosave = a:value
279279
endfunction
280280

281-
function! go#config#ModfmtAutosave() abort
282-
return get(g:, "go_modfmt_autosave", 1)
281+
function! go#config#ModFmtAutosave() abort
282+
return get(g:, "go_mod_fmt_autosave", 1)
283283
endfunction
284284

285-
function! go#config#SetModfmtAutosave(value) abort
286-
let g:go_modfmt_autosave = a:value
285+
function! go#config#SetModFmtAutosave(value) abort
286+
let g:go_mod_fmt_autosave = a:value
287287
endfunction
288288

289289
function! go#config#DocMaxHeight() abort

autoload/go/mod.vim

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
1+
let s:go_major_version = ""
2+
13
function! go#mod#Format() abort
4+
" go mod only exists in `v1.11`
5+
if empty(s:go_major_version)
6+
let tokens = matchlist(go#util#System("go version"), '\d\+.\(\d\+\) ')
7+
let s:go_major_version = str2nr(tokens[1])
8+
endif
9+
10+
if s:go_major_version < "11"
11+
call go#util#EchoError("Go v1.11 is required to format go.mod file")
12+
return
13+
endif
14+
215
let fname = fnamemodify(expand("%"), ':p:gs?\\?/?')
316

417
" Save cursor position and many other things.
@@ -88,13 +101,12 @@ function! s:parse_errors(filename, content) abort
88101
" list of errors to be put into location list
89102
let errors = []
90103
for line in splitted
91-
let tokens = matchlist(line, '^\(.\{-}\):\(\d\+\):\(\d\+\)\s*\(.*\)')
104+
let tokens = matchlist(line, '^\(.\{-}\):\(\d\+\):\s*\(.*\)')
92105
if !empty(tokens)
93106
call add(errors,{
94107
\"filename": a:filename,
95108
\"lnum": tokens[2],
96-
\"col": tokens[3],
97-
\"text": tokens[4],
109+
\"text": tokens[3],
98110
\ })
99111
endif
100112
endfor
@@ -108,21 +120,21 @@ function! s:show_errors(errors) abort
108120
let l:listtype = go#list#Type("GoModFmt")
109121
if !empty(a:errors)
110122
call go#list#Populate(l:listtype, a:errors, 'Format')
111-
echohl Error | echomsg "GoModFmt returned error" | echohl None
123+
call go#util#EchoError("GoModFmt returned error")
112124
endif
113125

114126
" this closes the window if there are no errors or it opens
115127
" it if there is any
116128
call go#list#Window(l:listtype, len(a:errors))
117129
endfunction
118130

119-
function! go#mod#ToggleModfmtAutoSave() abort
120-
if go#config#ModfmtAutosave()
121-
call go#config#SetModfmtAutosave(0)
131+
function! go#mod#ToggleModFmtAutoSave() abort
132+
if go#config#ModFmtAutosave()
133+
call go#config#SetModFmtAutosave(0)
122134
call go#util#EchoProgress("auto mod fmt disabled")
123135
return
124136
end
125137

126-
call go#config#SetModfmtAutosave(1)
127-
call go#util#EchoProgress("auto fmt enabled")
138+
call go#config#SetModFmtAutosave(1)
139+
call go#util#EchoProgress("auto mod fmt enabled")
128140
endfunction

doc/vim-go.txt

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,11 @@ CTRL-t
806806

807807
Toggles |'g:go_fmt_autosave'|.
808808

809+
*:GoModFmtAutoSaveToggle*
810+
:GoModFmtAutoSaveToggle
811+
812+
Toggles |'g:go_mod_fmt_autosave'|.
813+
809814
*:GoAsmFmtAutoSaveToggle*
810815
:GoAsmFmtAutoSaveToggle
811816

@@ -880,6 +885,13 @@ CTRL-t
880885
}
881886
}
882887
<
888+
*:GoModFmt*
889+
:GoModFmt
890+
891+
Filter the current go.mod buffer through "go mod edit -fmt" command. It
892+
tries to preserve cursor position and avoids replacing the buffer with
893+
stderr output.
894+
883895
==============================================================================
884896
MAPPINGS *go-mappings*
885897

@@ -1097,6 +1109,10 @@ Calls `:GoImport` for the current package
10971109
Generate if err != nil { return ... } automatically which infer the type of
10981110
return values and the numbers.
10991111

1112+
*(go-mod-fmt)*
1113+
1114+
Calls |:GoModFmt| for the current buffer
1115+
11001116
==============================================================================
11011117
TEXT OBJECTS *go-text-objects*
11021118

@@ -1287,7 +1303,15 @@ doesn't break. However it's slows (creates/deletes a file for every save) and
12871303
it's causing problems on some Vim versions. By default it's disabled. >
12881304
12891305
let g:go_fmt_experimental = 0
1306+
12901307
<
1308+
*'g:go_mod_fmt_autosave'*
1309+
1310+
Use this option to auto |:GoModFmt| on save. By default it's enabled >
1311+
1312+
let g:go_mod_fmt_autosave = 1
1313+
<
1314+
12911315
*'g:go_doc_keywordprg_enabled'*
12921316

12931317
Use this option to run `godoc` on words under the cursor with |K|; this will
@@ -1497,10 +1521,10 @@ that was called. Supported values are "", "quickfix", and "locationlist".
14971521
Specifies the type of list to use for command outputs (such as errors from
14981522
builds, results from static analysis commands, etc...). When an expected key
14991523
is not present in the dictionary, |'g:go_list_type'| will be used instead.
1500-
Supported keys are "GoBuild", "GoErrCheck", "GoFmt", "GoInstall", "GoLint",
1501-
"GoMetaLinter", "GoMetaLinterAutoSave", "GoModifyTags" (used for both
1502-
:GoAddTags and :GoRemoveTags), "GoRename", "GoRun", and "GoTest". Supported
1503-
values for each command are "quickfix" and "locationlist".
1524+
Supported keys are "GoBuild", "GoErrCheck", "GoFmt", "GoModFmt", "GoInstall",
1525+
"GoLint", "GoMetaLinter", "GoMetaLinterAutoSave", "GoModifyTags" (used for
1526+
both :GoAddTags and :GoRemoveTags), "GoRename", "GoRun", and "GoTest".
1527+
Supported values for each command are "quickfix" and "locationlist".
15041528
>
15051529
let g:go_list_type_commands = {}
15061530
<
@@ -1874,6 +1898,12 @@ filetype.
18741898
The `gohtmltmpl` filetype is automatically set for `*.tmpl` files; the
18751899
`gotexttmpl` is never automatically set and needs to be set manually.
18761900

1901+
==============================================================================
1902+
*gomod* *ft-gomod-syntax*
1903+
go.mod file syntax~
1904+
1905+
The `gomod` 'filetype' provides syntax highlighting for Go's module file `go.mod`
1906+
18771907

18781908
==============================================================================
18791909
DEBUGGER *go-debug*

ftdetect/gofiletype.vim

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ au BufReadPost *.s call s:gofiletype_post()
3131

3232
au BufRead,BufNewFile *.tmpl set filetype=gohtmltmpl
3333

34-
au BufNewFile *.mod setfiletype gomod | setlocal fileencoding=utf-8 fileformat=unix
35-
au BufRead *.mod call s:gofiletype_pre("gomod")
36-
au BufReadPost *.mod call s:gofiletype_post()
34+
" make sure we explicitly look for a `go.mod` and the `module` starts from the
35+
" beginning
36+
au BufNewFile,BufRead go.mod
37+
\ if getline(1) =~ '^module.*' | set filetype=gomod | endif
3738

3839
" vim: sw=2 ts=2 et

ftplugin/gomod/commands.vim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
command! -nargs=0 -range GoModFmt call go#mod#Format()
2+
3+
command! -nargs=0 GoModFmtAutoSaveToggle call go#mod#ToggleModFmtAutoSave()

plugin/go.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ endfunction
228228

229229
function! s:modfmt_autosave()
230230
" go.mod code formatting on save
231-
if get(g:, "go_modfmt_autosave", 1)
231+
if get(g:, "go_mod_fmt_autosave", 1)
232232
call go#mod#Format()
233233
endif
234234
endfunction

syntax/gomod.vim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ highlight default link gomodReplaceOperator Operator
3838

3939

4040
" highlight semver, note that this is very simple. But it works for now
41-
syntax match gomodVersion "v\d\.\d\.\d"
42-
syntax match gomodVersion "v\d\.\d\.\d-.*"
41+
syntax match gomodVersion "v\d\+\.\d\+\.\d\+"
42+
syntax match gomodVersion "v\d\+\.\d\+\.\d\+-.*"
4343
highlight default link gomodVersion Identifier
4444

4545
let b:current_syntax = "gomod"

0 commit comments

Comments
 (0)