|
| 1 | +" lint381.vim --- style check your code according to EECS 381 style guidelines |
| 2 | + |
| 3 | +" This checker filters your code through the lint381 application and displays |
| 4 | +" the violations of the EECS 381 style guide right within your vim editor. |
| 5 | +" It requires that the lint381 program lives in your path, and that you have |
| 6 | +" the Syntastic plugin for vim installed. |
| 7 | + |
| 8 | +" The lint381 program: https://github.com/arxanas/lint381 |
| 9 | +" See also the EECS 381 style guides: |
| 10 | + " http://www.umich.edu/~eecs381/handouts/C_Coding_Standards.pdf |
| 11 | + " http://www.umich.edu/~eecs381/handouts/C++_Coding_Standards.pdf |
| 12 | + |
| 13 | +" To use this: |
| 14 | + " 1) Ensure that you have Syntastic installed: https://github.com/vim-syntastic/syntastic |
| 15 | + " 2) Place this file in ~/.vim/bundle/syntastic/syntax_checkers/cpp |
| 16 | + " 3) Place the c/lint381.vim file in ~/.vim/bundle/syntastic/syntax_checkers/c |
| 17 | + " 4) Add these lines to your vimrc: |
| 18 | + " let g:syntastic_cpp_checkers = ['lint381'] |
| 19 | + " let g:syntastic_c_checkers = ['lint381'] |
| 20 | + " Of course, precede 'lint381' with any other linters you may wish to use, e.g.: |
| 21 | + " let g:syntastic_cpp_checkers = ['cppcheck', 'lint381'] |
| 22 | + |
| 23 | + " Note that if you have multiple checkers enabled, the default behavior of Syntastic |
| 24 | + " is to run them in sequence, continuing with the next checker ONLY once the previous |
| 25 | + " checker returns no errors. |
| 26 | + " To tell Syntastic to always run all checkers, add this to your vimrc: |
| 27 | + " let g:syntastic_aggregate_errors = 1 |
| 28 | + |
| 29 | +if exists('g:loaded_syntastic_cpp_lint381_checker') |
| 30 | + finish |
| 31 | +endif |
| 32 | +let g:loaded_syntastic_cpp_lint381_checker = 1 |
| 33 | + |
| 34 | +if !exists('g:syntastic_cpp_lint381_sort') |
| 35 | + let g:syntastic_cpp_lint381_sort = 1 |
| 36 | +endif |
| 37 | + |
| 38 | +let s:save_cpo = &cpo |
| 39 | +set cpo&vim |
| 40 | + |
| 41 | +" Since lint381 only prints the basename of the file being edited, |
| 42 | +" if the user edits a file that is not in the current working directory, |
| 43 | +" syntastic ends up telling vim that the identified errors are in a file |
| 44 | +" in the cwd with the printed name. |
| 45 | +" To fix this, we convert the basenames to absolute paths. |
| 46 | +function! Basenames_to_absolute_paths(errors) abort |
| 47 | + let out = [] |
| 48 | + for err in a:errors |
| 49 | + let str = expand('%:h') . '/' . err |
| 50 | + echom str |
| 51 | + call add(out, str) |
| 52 | + endfor |
| 53 | + return out |
| 54 | +endfunction |
| 55 | + |
| 56 | +let s:default_args = { |
| 57 | + \ 'c': '--lang=c', |
| 58 | + \ 'cpp': '--lang=cpp'} |
| 59 | + |
| 60 | +function! SyntaxCheckers_cpp_lint381_IsAvailable() dict |
| 61 | + return executable(self.getExec()) |
| 62 | +endfunction |
| 63 | + |
| 64 | +function! SyntaxCheckers_cpp_lint381_GetLocList() dict |
| 65 | + let makeprg = self.makeprgBuild({'args': get(s:default_args, self.getFiletype(), '')}) |
| 66 | + |
| 67 | + " See :help errorformat and :help errorformat-multi-line |
| 68 | + let errorformat = '%E%f:%l:%c: ' " Matches the file name, line number and column number |
| 69 | + let errorformat .= 'error:\ %m' " Matches the error message |
| 70 | + let errorformat .= ',%C%p^,%Z' " Matches the ^^^^ that points to the error |
| 71 | + |
| 72 | + " Note that we mark the errors as 'Style' errors; Syntastic can be configured to ignore these. |
| 73 | + return SyntasticMake({ 'makeprg': makeprg, |
| 74 | + \'errorformat': errorformat, |
| 75 | + \'subtype': 'Style', |
| 76 | + \'Preprocess': 'Basenames_to_absolute_paths' }) |
| 77 | +endfunction |
| 78 | + |
| 79 | +call g:SyntasticRegistry.CreateAndRegisterChecker({ 'filetype': 'cpp', 'name': 'lint381' }) |
| 80 | + |
| 81 | +let &cpo = s:save_cpo |
| 82 | +unlet s:save_cpo |
0 commit comments