Skip to content

Commit

Permalink
Fix review comments
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
fatih committed Aug 31, 2018
1 parent 3d725eb commit 3b04072
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 24 deletions.
8 changes: 4 additions & 4 deletions autoload/go/config.vim
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,12 @@ function! go#config#SetAsmfmtAutosave(value) abort
let g:go_asmfmt_autosave = a:value
endfunction

function! go#config#ModfmtAutosave() abort
return get(g:, "go_modfmt_autosave", 1)
function! go#config#ModFmtAutosave() abort
return get(g:, "go_mod_fmt_autosave", 1)
endfunction

function! go#config#SetModfmtAutosave(value) abort
let g:go_modfmt_autosave = a:value
function! go#config#SetModFmtAutosave(value) abort
let g:go_mod_fmt_autosave = a:value
endfunction

function! go#config#DocMaxHeight() abort
Expand Down
32 changes: 22 additions & 10 deletions autoload/go/mod.vim
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
let s:go_major_version = ""

function! go#mod#Format() abort
" go mod only exists in `v1.11`
if empty(s:go_major_version)
let tokens = matchlist(go#util#System("go version"), '\d\+.\(\d\+\) ')
let s:go_major_version = str2nr(tokens[1])
endif

if s:go_major_version < "11"
call go#util#EchoError("Go v1.11 is required to format go.mod file")
return
endif

let fname = fnamemodify(expand("%"), ':p:gs?\\?/?')

" Save cursor position and many other things.
let l:curw = winsaveview()

" Write current unsaved buffer to a temp file
let l:tmpname = tempname() . '.go'
let l:tmpname = tempname() . '.mod'
call writefile(go#util#GetLines(), l:tmpname)
if go#util#IsWin()
let l:tmpname = tr(l:tmpname, '\', '/')
Expand Down Expand Up @@ -88,13 +101,12 @@ function! s:parse_errors(filename, content) abort
" list of errors to be put into location list
let errors = []
for line in splitted
let tokens = matchlist(line, '^\(.\{-}\):\(\d\+\):\(\d\+\)\s*\(.*\)')
let tokens = matchlist(line, '^\(.\{-}\):\(\d\+\):\s*\(.*\)')
if !empty(tokens)
call add(errors,{
\"filename": a:filename,
\"lnum": tokens[2],
\"col": tokens[3],
\"text": tokens[4],
\"text": tokens[3],
\ })
endif
endfor
Expand All @@ -108,21 +120,21 @@ function! s:show_errors(errors) abort
let l:listtype = go#list#Type("GoModFmt")
if !empty(a:errors)
call go#list#Populate(l:listtype, a:errors, 'Format')
echohl Error | echomsg "GoModFmt returned error" | echohl None
call go#util#EchoError("GoModFmt returned error")
endif

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

function! go#mod#ToggleModfmtAutoSave() abort
if go#config#ModfmtAutosave()
call go#config#SetModfmtAutosave(0)
function! go#mod#ToggleModFmtAutoSave() abort
if go#config#ModFmtAutosave()
call go#config#SetModFmtAutosave(0)
call go#util#EchoProgress("auto mod fmt disabled")
return
end

call go#config#SetModfmtAutosave(1)
call go#util#EchoProgress("auto fmt enabled")
call go#config#SetModFmtAutosave(1)
call go#util#EchoProgress("auto mod fmt enabled")
endfunction
39 changes: 35 additions & 4 deletions doc/vim-go.txt
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,11 @@ CTRL-t

Toggles |'g:go_fmt_autosave'|.

*:GoModFmtAutoSaveToggle*
:GoModFmtAutoSaveToggle

Toggles |'g:go_mod_fmt_autosave'|.

*:GoAsmFmtAutoSaveToggle*
:GoAsmFmtAutoSaveToggle

Expand Down Expand Up @@ -880,6 +885,13 @@ CTRL-t
}
}
<
*:GoModFmt*
:GoModFmt

Filter the current go.mod buffer through "go mod edit -fmt" command. It
tries to preserve cursor position and avoids replacing the buffer with
stderr output.

==============================================================================
MAPPINGS *go-mappings*

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

*(go-mod-fmt)*

Calls |:GoModFmt| for the current buffer

==============================================================================
TEXT OBJECTS *go-text-objects*

Expand Down Expand Up @@ -1287,7 +1303,15 @@ doesn't break. However it's slows (creates/deletes a file for every save) and
it's causing problems on some Vim versions. By default it's disabled. >
let g:go_fmt_experimental = 0
<
*'g:go_mod_fmt_autosave'*

Use this option to auto |:GoModFmt| on save. By default it's enabled >
let g:go_mod_fmt_autosave = 1
<

*'g:go_doc_keywordprg_enabled'*

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

==============================================================================
*gomod* *ft-gomod-syntax*
go.mod file syntax~

The `gomod` 'filetype' provides syntax highlighting for Go's module file
`go.mod`


==============================================================================
DEBUGGER *go-debug*
Expand Down
7 changes: 4 additions & 3 deletions ftdetect/gofiletype.vim
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ au BufReadPost *.s call s:gofiletype_post()

au BufRead,BufNewFile *.tmpl set filetype=gohtmltmpl

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

" vim: sw=2 ts=2 et
2 changes: 2 additions & 0 deletions ftplugin/gomod/commands.vim
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
command! -nargs=0 -range GoModFmt call go#mod#Format()

command! -nargs=0 GoModFmtAutoSaveToggle call go#mod#ToggleModFmtAutoSave()
2 changes: 1 addition & 1 deletion plugin/go.vim
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ endfunction

function! s:modfmt_autosave()
" go.mod code formatting on save
if get(g:, "go_modfmt_autosave", 1)
if get(g:, "go_mod_fmt_autosave", 1)
call go#mod#Format()
endif
endfunction
Expand Down
4 changes: 2 additions & 2 deletions syntax/gomod.vim
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ highlight default link gomodReplaceOperator Operator


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

let b:current_syntax = "gomod"

0 comments on commit 3b04072

Please sign in to comment.