wiki:CodingGuidelines/VIM

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

Add some rules that were in the python version but not here.

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 over 80 char
	call matchadd('Search', '^\s* \s*', -1)  " spaces instead of tabs
	call matchadd('Search', '\(for\|if\|select\|while\)(', -1)
		"missing space after control statement
	call matchadd('Search', '//\S', -1) " Missing space at comment start
	call matchadd('Search', '\w[,=>+\-*;]\w', -1)
		"operator without space around it (without false positive on
		"templated<type>)
	call matchadd('Search', '^[^#].*[^<]\zs\w*/\w', -1)
		"operator without space around it (without false positive on
		"#include <dir/file.h>)
	call matchadd('Search', '^[^/]\{2}.*\zs[^*][=/+\-< ]$', -1)
		"operator at end of line (without false positives on /* and */, nor
		"char*\nClass::method())
	call matchadd('Search', '^[^#].*\zs[^<]>$', -1)
		" > operator at end of line (without false positive on #include <file.h>)
	call matchadd('Search', '){', -1) " Missing space after method header
	call matchadd('Search', '}\n\s*else', -1) " Malformed else
	call matchadd('Search', '\s$', -1) "Spaces at end of line
	call matchadd('Search', ',\S', -1) " Missing space after comma
	call matchadd('Search', '^}\n\{1,2}\S', -1) " Less than 2 lines between functions
	call matchadd('Search', '^}\n\{4,}\S', -1) " More than 2 lines between functions
: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.