From 972166600614b8d0a7d76b43836b76f6637c70d6 Mon Sep 17 00:00:00 2001 From: Sander van Harmelen Date: Fri, 27 Nov 2015 16:37:08 +0100 Subject: [PATCH] Add metalinter options MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Add the option to run the metalinter on save 2. Add the option to limit the output to the currently active buffer There is a outstanding PR with the `gometalinter` repo to add some needed logic for this: https://github.com/alecthomas/gometalinter/pull/71 So I assume this PR should not be merged before that one is merged. Also this the first vimscript code I’ve ever written, so I guess this could be done a little cleaner. Especially the implementation of the `go#lint#GometaAutoSave` func doesn’t feel all that clean to me, but I’m not sure how to do this in a different/better way. So if you have --- autoload/go/lint.vim | 24 +++++++++++++++++------- doc/vim-go.txt | 13 +++++++++++++ ftplugin/go/commands.vim | 2 +- ftplugin/go/mappings.vim | 2 +- plugin/go.vim | 5 +++++ 5 files changed, 37 insertions(+), 9 deletions(-) diff --git a/autoload/go/lint.vim b/autoload/go/lint.vim index 64c8ca8862..9676576485 100644 --- a/autoload/go/lint.vim +++ b/autoload/go/lint.vim @@ -2,6 +2,10 @@ if !exists("g:go_metalinter_command") let g:go_metalinter_command = "" endif +if !exists("g:go_metalinter_autosave_enabled") + let g:go_metalinter_autosave_enabled = ['vet', 'golint'] +endif + if !exists("g:go_metalinter_enabled") let g:go_metalinter_enabled = ['vet', 'golint', 'errcheck'] endif @@ -18,7 +22,7 @@ if !exists("g:go_errcheck_bin") let g:go_errcheck_bin = "errcheck" endif -function! go#lint#Gometa(...) abort +function! go#lint#Gometa(autosave, ...) abort if a:0 == 0 let goargs = expand('%:p:h') else @@ -26,24 +30,27 @@ function! go#lint#Gometa(...) abort endif let meta_command = "gometalinter --disable-all" - if empty(g:go_metalinter_command) + if a:autosave || empty(g:go_metalinter_command) let bin_path = go#path#CheckBinPath("gometalinter") if empty(bin_path) return endif - if empty(g:go_metalinter_enabled) - echohl Error | echomsg "vim-go: please enable linters with the setting g:go_metalinter_enabled" | echohl None - return + if a:autosave + " include only messages for the active buffer + let meta_command .= " --include='^" . expand('%:p') . ".*$'" endif - for linter in g:go_metalinter_enabled + " linters + let linters = a:autosave ? g:go_metalinter_autosave_enabled : g:go_metalinter_enabled + for linter in linters let meta_command .= " --enable=".linter endfor " deadline let meta_command .= " --deadline=" . g:go_metalinter_deadline + " path let meta_command .= " " . goargs else " the user wants something else, let us use it. @@ -73,7 +80,10 @@ function! go#lint#Gometa(...) abort let errors = go#list#Get() call go#list#Window(len(errors)) - call go#list#JumpToFirst() + + if !a:autosave + call go#list#JumpToFirst() + endif endif endfunction diff --git a/doc/vim-go.txt b/doc/vim-go.txt index 6d5370971f..2786208c8d 100755 --- a/doc/vim-go.txt +++ b/doc/vim-go.txt @@ -864,7 +864,20 @@ Adds custom text objects. By default it's enabled. > let g:go_textobj_enabled = 1 < + *'g:go_metalinter_autosave'* +Use this option to auto |:GoMetaLinter| on save. Only linter messages for +the active buffer will be shown. By default it's disabled > + + let g:go_metalinter_autosave = 0 +< + *'g:go_metalinter_autosave_enabled'* + +Specifies the enabled linters for auto |GoMetaLinter| on save. By +default it's using `vet` and `golint` +> + let g:go_metalinter_autosave_enabled = ['vet', 'golint'] +< *'g:go_metalinter_enabled'* Specifies the currently enabled linters for the |GoMetaLinter| command. By diff --git a/ftplugin/go/commands.vim b/ftplugin/go/commands.vim index ec15d38781..a821e92918 100755 --- a/ftplugin/go/commands.vim +++ b/ftplugin/go/commands.vim @@ -48,7 +48,7 @@ command! -nargs=1 -bang -complete=customlist,go#package#Complete GoImport call g command! -nargs=* -bang -complete=customlist,go#package#Complete GoImportAs call go#import#SwitchImport(1, , '') " -- linters -command! -nargs=* GoMetaLinter call go#lint#Gometa() +command! -nargs=* GoMetaLinter call go#lint#Gometa(0, ) command! -nargs=* GoLint call go#lint#Golint() command! -nargs=* -bang GoVet call go#lint#Vet(0, ) command! -nargs=* -complete=customlist,go#package#Complete GoErrCheck call go#lint#Errcheck() diff --git a/ftplugin/go/mappings.vim b/ftplugin/go/mappings.vim index eca0d0bbc5..2b11c4c8e9 100644 --- a/ftplugin/go/mappings.vim +++ b/ftplugin/go/mappings.vim @@ -46,7 +46,7 @@ nnoremap (go-doc-vertical) :call go#doc#Open("vnew", "vsplit nnoremap (go-doc-split) :call go#doc#Open("new", "split") nnoremap (go-doc-browser) :call go#doc#OpenBrowser() -nnoremap (go-metalinter) :call go#lint#Gometa('') +nnoremap (go-metalinter) :call go#lint#Gometa(0) nnoremap (go-vet) :call go#lint#Vet(!g:go_jump_to_error) diff --git a/plugin/go.vim b/plugin/go.vim index c3e1493a16..3337b59dcc 100644 --- a/plugin/go.vim +++ b/plugin/go.vim @@ -133,6 +133,11 @@ augroup vim-go autocmd BufWritePre *.go call go#fmt#Format(-1) endif + " run gometalinter on save + if get(g:, "go_metalinter_autosave", 0) + autocmd BufWritePost *.go call go#lint#Gometa(1) + endif + augroup END