add a makeprg builder

The problem
---

Some people want to change the syntax checker args and/or executable.
Currently they have to create their own checker to do this.

Solution
---

Create a standard API for building a makeprg that allows users to set
global variables to override the exe or args.

This API is in use in the coffee and python/flake8 checkers - as
proofs of concept.

So, if the user wanted to change the args that get passed to `flake8`
they can now set `let g:syntastic_python_flake8_args="--foo --bar"` in
their vimrc. Similarly they could set `let
g:syntastic_python_flake8_exe='python foo.py'`
This commit is contained in:
Martin Grenfell 2012-12-16 18:28:08 +00:00
parent 5fbe86db7b
commit 10c845d1f5
3 changed files with 72 additions and 2 deletions

View File

@ -0,0 +1,65 @@
if exists("g:loaded_syntastic_makeprg_autoload")
finish
endif
let g:loaded_syntastic_makeprg_autoload = 1
function! syntastic#makeprg#build(opts)
let opts = copy(a:opts)
if !has_key(opts, 'args')
let opts['args'] = ''
endif
if !has_key(opts, 'subchecker')
let opts['subchecker'] = ''
endif
let builder = s:MakeprgBuilder.New(opts['exe'], opts['args'], opts['subchecker'])
return builder.makeprg()
endfunction
let s:MakeprgBuilder = {}
function! s:MakeprgBuilder.New(exe, args, subchecker)
let newObj = copy(self)
let newObj._exe = a:exe
let newObj._args = a:args
let newObj._subchecker = a:subchecker
return newObj
endfunction
function! s:MakeprgBuilder.makeprg()
return join([self.exe(), self.args(), self.fname()])
endfunction
function! s:MakeprgBuilder.exe()
if self.optExists('exe')
return {self.optName('exe')}
endif
return self._exe
endfunction
function! s:MakeprgBuilder.args()
if exists('g:syntastic_' . &ft . '_args')
return g:syntastic_{&ft}_args
endif
return self._args
endfunction
function! s:MakeprgBuilder.fname()
return shellescape(expand("%"))
endfunction
function! s:MakeprgBuilder.optExists(name)
return exists(self.optName(a:name))
endfunction
function! s:MakeprgBuilder.optName(name)
let setting = "g:syntastic_" . &ft
if !empty(self._subchecker)
let setting .= '_' . self._subchecker
endif
return setting . '_' . a:name
endfunction

View File

@ -21,7 +21,9 @@ endif
function! SyntaxCheckers_coffee_GetLocList() function! SyntaxCheckers_coffee_GetLocList()
let makeprg = 'coffee -c -l -o /tmp '.shellescape(expand('%')) let makeprg = syntastic#makeprg#build({
\ 'exe': 'coffee',
\ 'args': '-c -l -o /tmp' })
let errorformat = 'Syntax%trror: In %f\, %m on line %l,%EError: In %f\, Parse error on line %l: %m,%EError: In %f\, %m on line %l,%W%f(%l): lint warning: %m,%-Z%p^,%W%f(%l): warning: %m,%-Z%p^,%E%f(%l): SyntaxError: %m,%-Z%p^,%-G%.%#' let errorformat = 'Syntax%trror: In %f\, %m on line %l,%EError: In %f\, Parse error on line %l: %m,%EError: In %f\, %m on line %l,%W%f(%l): lint warning: %m,%-Z%p^,%W%f(%l): warning: %m,%-Z%p^,%E%f(%l): SyntaxError: %m,%-Z%p^,%-G%.%#'
let coffee_results = SyntasticMake({ 'makeprg': makeprg, 'errorformat': errorformat }) let coffee_results = SyntasticMake({ 'makeprg': makeprg, 'errorformat': errorformat })

View File

@ -22,7 +22,10 @@ function! SyntaxCheckers_python_GetHighlightRegex(i)
endfunction endfunction
function! SyntaxCheckers_python_GetLocList() function! SyntaxCheckers_python_GetLocList()
let makeprg = 'flake8 '.g:syntastic_python_checker_args.' '.shellescape(expand('%')) let makeprg = syntastic#makeprg#build({
\ 'exe': 'flake8',
\ 'args': g:syntastic_python_checker_args,
\ 'subchecker': 'flake8' })
let errorformat = '%E%f:%l: could not compile,%-Z%p^,%E%f:%l:%c: %m,%W%f:%l: %m,%-G%.%#' let errorformat = '%E%f:%l: could not compile,%-Z%p^,%E%f:%l:%c: %m,%W%f:%l: %m,%-G%.%#'
return SyntasticMake({ 'makeprg': makeprg, 'errorformat': errorformat }) return SyntasticMake({ 'makeprg': makeprg, 'errorformat': errorformat })
endfunction endfunction