wiki:CodingGuidelines/VIM

Version 13 (modified by pulkomandy, 14 years ago) ( diff )

--

Here is a small script that will detect some guideline violations for vim. I put it in my .vimrc and it highlight all the errors it can find in all opened files. I'd like to know if you made improvements on it.


:fu FuncHaikuCheck()
		call matchadd('Search', '\%>80v.\+', -1) " line with more than 80 chars
	call matchadd('Search', '0\s* \s*', -1)        " probably wrong indenting
	call matchadd('Search', '\(for\|if\|select\|while\)(', -1)
		"keyword without space after it
	call matchadd('Search', '[a-zA-Z0-9][,=>+\-*;][a-zA-Z0-9]', -1)
		"operator without space around it (without false positive on
		"templated<type>)
	call matchadd('Search', '^[^#].*[^<][a-zA-Z0-9]*/[a-zA-Z0-9]', -1)
		"operator without space around it (without false positive on
		"#include <dir/file.h>)
	call matchadd('Search', '^[^/]\{2}.*[^*][=/+\- ]$', -1)
		"operator at end of line (without false positives on /* and */, nor
		"char*\nClass::method())
	call matchadd('Search', '\s$', -1) "Spaces at end of line
	call matchadd('Search', ',\S', -1) " Missing space after comma
:endfu

Calling the function

The obvious way to enable the script is to do it by hand :

:call FuncHaikuCheck()

It is also possible to make it run on every opened file (this is a bit annoying when using vim for something else than Haiku or C++ files)

" autocmd that will set up the w:created variable (vim tip 1598)
" so we can run the check only once per window
autocmd VimEnter * autocmd WinEnter * let w:created=1

" call the function on all opened files
autocmd WinEnter * if !exists('w:created') | call FuncHaikuCheck() | endif
autocmd BufWinEnter * call FuncHaikuCheck()

The last option is to define a keyboard shortcut to enable it (control+F3 in this example) :

map <C-F3> :call FuncHaikuCheck()<CR>

These rules have been ported to a standalone python program see the Style Checker Tools paragraph in the ​Haiku coding guidelines and ​src/tools/checkstyle/

Note: See TracWiki for help on using the wiki.