syntastic/plugin/syntastic.vim

238 lines
7.7 KiB
VimL
Raw Normal View History

2009-07-10 21:39:39 -04:00
"============================================================================
"File: syntastic.vim
"Description: vim plugin for on the fly syntax checking
"Maintainer: Martin Grenfell <martin_grenfell at msn dot com>
"License: This program is free software. It comes without any warranty,
" to the extent permitted by applicable law. You can redistribute
" it and/or modify it under the terms of the Do What The Fuck You
" Want To Public License, Version 2, as published by Sam Hocevar.
" See http://sam.zoy.org/wtfpl/COPYING for more details.
"
"============================================================================
"
"Syntastic does the following:
"----------------------------------------------------------------------------
"
"1. Provides a statusline flag to notify you of errors in the buffer
"2. Uses the :sign interface to point out lines with syntax errors
2009-07-13 07:12:18 -04:00
"3. Defines :Errors, which opens the syntax errors in location list window
2009-07-10 21:39:39 -04:00
"
"To use the above functionality, a syntax checker plugin must be present for
"the filetype in question (more about that below).
"
2009-07-10 21:47:17 -04:00
"
"Using the statusline flag
"----------------------------------------------------------------------------
"
"To use the statusline flag, this must appear in your &statusline setting:
" %{SyntasticStatuslineFlag()}
"
"Something like this could be more useful:
"
" set statusline+=%#warningmsg#
" set statusline+=%{SyntasticStatuslineFlag()}
" set statusline+=%*
"
"
2009-07-10 21:39:39 -04:00
"Implementing syntax checker plugins:
"----------------------------------------------------------------------------
"
"A syntax checker plugin is really nothing more than a single function. You
"should define them in ~/.vim/syntax_checkers/<filetype>.vim. This is purely
"for convenience; Syntastic doesn't actually care where these functions are
"defined.
"
"A syntax checker plugin should define a function of the form:
"
2009-07-13 07:12:18 -04:00
" SyntaxCheckers_<filetype>_GetLocList()
2009-07-10 21:39:39 -04:00
"
2009-07-13 07:12:18 -04:00
"The output of this function should be of the same form as the getloclist()
"function. See :help getloclist() and :help getqflist() for details.
2009-07-10 21:39:39 -04:00
"
"Syntastic is designed so that the syntax checker plugins can be implemented
2009-07-13 07:12:18 -04:00
"using vims :lmake facility without screwing up the users current make
2009-07-10 21:39:39 -04:00
"settings. To this end, the following settings are saved and restored after
"the syntax checking function is called:
"
2009-07-13 07:12:18 -04:00
" * the users location list
2009-07-10 21:39:39 -04:00
" * &makeprg
" * &errorformat
"
"This way, a typical syntax checker function can look like this:
"
2009-07-13 07:12:18 -04:00
" function! SyntaxCheckers_ruby_GetLocList()
2009-07-10 23:57:03 -04:00
" set makeprg=ruby\ -c\ %
2009-07-11 00:48:37 -04:00
" set errorformat=%-GSyntax\ OK,%A%f:%l:\ syntax\ error\\,\ %m,%Z%p^,%-C%.%#
2009-07-13 07:12:18 -04:00
" silent lmake!
" return getloclist(0)
2009-07-10 21:39:39 -04:00
" endfunction
"
2009-07-13 07:12:18 -04:00
"After this function is called, makeprg, errorformat and the location list
2009-07-10 21:39:39 -04:00
"will be restored to their previous settings.
2009-07-10 21:47:17 -04:00
"
2009-07-13 07:12:18 -04:00
"NOTE: syntax checkers *can* piggy back off :lmake, but they dont *have* to. If
2009-07-10 23:57:03 -04:00
"&errorformat is too crazy for you then you can parse the syntax checker
2009-07-13 07:12:18 -04:00
"output yourself and compile it into the loclist style data structure.
2009-07-10 23:57:03 -04:00
"
2009-07-10 21:47:17 -04:00
"
"Options:
"----------------------------------------------------------------------------
"
"Use this option to tell syntastic to use the :sign interface to mark syntax
"errors
" let g:syntastic_enable_signs=1
"
"
2009-07-10 21:39:39 -04:00
"============================================================================
2009-07-10 19:09:52 -04:00
if exists("g:loaded_syntastic_plugin")
finish
endif
let g:loaded_syntastic_plugin = 1
let s:running_windows = has("win16") || has("win32") || has("win64")
2009-07-10 21:47:17 -04:00
if !exists("g:syntastic_enable_signs")
let g:syntastic_enable_signs = 0
endif
2009-07-13 07:12:18 -04:00
if !exists("g:syntastic_auto_loc_list")
let g:syntastic_auto_loc_list = 0
2009-07-13 05:38:50 -04:00
endif
2009-07-10 19:09:52 -04:00
"load all the syntax checkers
runtime! syntax_checkers/*.vim
"refresh and redraw all the error info for this buf when saving or reading
autocmd bufreadpost,bufwritepost * call s:UpdateErrors()
2009-07-10 19:09:52 -04:00
function! s:UpdateErrors()
call s:CacheErrors()
2009-07-10 21:47:17 -04:00
if g:syntastic_enable_signs
call s:RefreshSigns()
2009-07-10 21:47:17 -04:00
endif
2009-07-13 05:38:50 -04:00
2009-07-13 07:12:18 -04:00
if g:syntastic_auto_loc_list
if s:BufHasErrors()
call s:ShowLocList()
else
"TODO: this will close the loc list window if one was opened by
"something other than syntastic
2009-07-13 07:12:18 -04:00
lclose
endif
2009-07-13 05:38:50 -04:00
endif
2009-07-10 19:09:52 -04:00
endfunction
2009-07-10 19:55:51 -04:00
"detect and cache all syntax errors in this buffer
"
2009-07-13 07:12:18 -04:00
"depends on a function called SyntaxCheckers_{&ft}_GetLocList() existing
2009-07-10 19:55:51 -04:00
"elsewhere
"
"saves and restores some settings that the syntax checking function may wish
2009-07-13 07:12:18 -04:00
"to screw with if it uses :lmake!
2009-07-10 19:09:52 -04:00
function! s:CacheErrors()
2009-07-13 07:12:18 -04:00
let b:syntastic_loclist = []
2009-07-10 19:09:52 -04:00
for ft in split(&ft, '\.')
2009-07-13 07:12:18 -04:00
if exists("*SyntaxCheckers_". ft ."_GetLocList") && filereadable(expand("%"))
let oldlocixlist = getloclist(0)
let old_makeprg = &makeprg
let old_shellpipe = &shellpipe
let old_errorformat = &errorformat
2009-07-10 19:09:52 -04:00
if !s:running_windows
"this is a hack to stop the screen needing to be ':redraw'n when
2009-07-13 07:12:18 -04:00
"when :lmake is run. Otherwise the screen flickers annoyingly
let &shellpipe='&>'
endif
2009-07-10 19:09:52 -04:00
2009-07-13 07:12:18 -04:00
let b:syntastic_loclist = extend(b:syntastic_loclist, SyntaxCheckers_{ft}_GetLocList())
2009-07-10 19:09:52 -04:00
2009-07-13 07:12:18 -04:00
call setloclist(0, oldlocixlist)
let &makeprg = old_makeprg
let &errorformat = old_errorformat
let &shellpipe=old_shellpipe
endif
endfor
2009-07-10 19:09:52 -04:00
endfunction
2009-07-10 19:55:51 -04:00
"return true if there are cached errors for this buf
2009-07-10 19:09:52 -04:00
function! s:BufHasErrors()
2009-07-13 07:12:18 -04:00
return exists("b:syntastic_loclist") && !empty(b:syntastic_loclist)
2009-07-10 19:09:52 -04:00
endfunction
2009-07-10 19:55:51 -04:00
"use >> to display syntax errors in the sign column
2009-07-10 19:09:52 -04:00
sign define SyntaxError text=>> texthl=error
2009-07-10 19:55:51 -04:00
"start counting sign ids at 5000, start here to hopefully avoid conflicting
2009-07-13 07:13:22 -04:00
"with any other code that places signs (not sure if this precaution is
2009-07-10 19:55:51 -04:00
"actually needed)
2009-07-10 19:09:52 -04:00
let s:first_sign_id = 5000
let s:next_sign_id = s:first_sign_id
2009-07-10 19:55:51 -04:00
"place SyntaxError signs by all syntax errs in the buffer
2009-07-10 19:09:52 -04:00
function s:SignErrors()
if s:BufHasErrors()
2009-07-13 07:12:18 -04:00
for i in b:syntastic_loclist
2009-07-10 19:09:52 -04:00
exec "sign place ". s:next_sign_id ." line=". i['lnum'] ." name=SyntaxError file=". expand("%:p")
call add(s:BufSignIds(), s:next_sign_id)
let s:next_sign_id += 1
endfor
endif
endfunction
"remove the signs with the given ids from this buffer
function! s:RemoveSigns(ids)
for i in a:ids
2009-07-10 19:09:52 -04:00
exec "sign unplace " . i
call remove(s:BufSignIds(), index(s:BufSignIds(), i))
2009-07-10 19:09:52 -04:00
endfor
endfunction
2009-07-10 19:55:51 -04:00
"get all the ids of the SyntaxError signs in the buffer
2009-07-10 19:09:52 -04:00
function! s:BufSignIds()
if !exists("b:syntastic_sign_ids")
let b:syntastic_sign_ids = []
endif
return b:syntastic_sign_ids
endfunction
"update the error signs
function! s:RefreshSigns()
let old_signs = copy(s:BufSignIds())
call s:SignErrors()
call s:RemoveSigns(old_signs)
let s:first_sign_id = s:next_sign_id
endfunction
2009-07-13 07:12:18 -04:00
"display the cached errors for this buf in the location list
function! s:ShowLocList()
if exists("b:syntastic_loclist")
call setloclist(0, b:syntastic_loclist)
lopen
2009-07-10 19:09:52 -04:00
endif
endfunction
2009-07-13 07:12:18 -04:00
command Errors call s:ShowLocList()
2009-07-10 19:55:51 -04:00
"return [syntax:X(Y)] if syntax errors are detected in the buffer, where X is the
"line number of the first error and Y is the number of errors detected. (Y) is
"only displayed if > 1 errors are detected
"
"return '' if no errors are cached for the buffer
function! SyntasticStatuslineFlag()
2009-07-10 19:09:52 -04:00
if s:BufHasErrors()
2009-07-13 07:12:18 -04:00
let first_err_line = b:syntastic_loclist[0]['lnum']
2009-07-10 19:09:52 -04:00
let err_count = ""
2009-07-13 07:12:18 -04:00
if len(b:syntastic_loclist) > 1
let err_count = "(" . len(b:syntastic_loclist) . ")"
2009-07-10 19:09:52 -04:00
endif
return '[syntax:' . first_err_line . err_count . ']'
else
return ''
endif
endfunction
2009-07-10 19:55:51 -04:00
" vim: set et sts=4 sw=4: