See More

" File: javascriptLint.vim " Author: Joe Stelmach ([email protected]) " Version: 0.2 " Description: javascriptLint.vim allows the JavaScript Lint (jsl) program " from http://www.javascriptlint.com/ to be tightly integrated " with vim. The contents of a javascript file will be passed " through the jsl program after the file's buffer is saved. " Any lint warnings will be placed in the quickfix window. " JavaScript Lint must be installed on your system for this " plugin to work properly. This page should get you started: " http://www.javascriptlint.com/docs/index.htm " " Modified to use jslint (https://github.com/reid/node-jslint) " instead of jsl. " Last Modified: May 5, 2009 if !exists("jslint_command") " requires Node, install using: " npm install jslint -g " jslint version = 0.1.8 ( npm view jslint version ) let jslint_command = 'jslint' endif if !exists("jslint_command_options") let jslint_command_options = '--sloppy' endif if !exists("jslint_highlight_color") let jslint_highlight_color = 'DarkMagenta' endif " set up auto commands autocmd BufWritePost,FileWritePost *.js call JavascriptLint() autocmd BufWinLeave * call s:MaybeClearCursorLineColor() " Runs the current file through javascript lint and " opens a quickfix window with any warnings function JavascriptLint() " run javascript lint on the current file let current_file = shellescape(expand('%:p')) let cmd_output = system(g:jslint_command . ' ' . g:jslint_command_options . ' ' . current_file) " if some errors were found we process them if cmd_output !~ "is OK" " save current errorformat let save_efm = &efm " see http://stackoverflow.com/questions/3713015/vim-errorformat-and-jslint " set errorformat to match jslint ouput set efm=%-P%f,%A%>%\\s%\\?#%*\\d\ %m,%Z%.%#Line\ %l\\,\ Pos\ %c,%-G%f\ is\ OK.,%-Q " write quickfix errors to a temp file let quickfix_tmpfile_name = tempname() exe "redir! > " . quickfix_tmpfile_name silent echon cmd_output redir END " read in the errors temp file execute "silent! cfile " . quickfix_tmpfile_name " change the cursor line to something hard to miss call s:SetCursorLineColor() " open the quickfix window botright copen let s:qfix_buffer = bufnr("$") " delete the temp file call delete(quickfix_tmpfile_name) " restore the saved errorformat let &efm = save_efm " if no javascript warnings are found, we revert the cursorline color " and close the quick fix window else call s:ClearCursorLineColor() if(exists("s:qfix_buffer")) cclose unlet s:qfix_buffer endif endif endfunction " sets the cursor line highlight color to the error highlight color function s:SetCursorLineColor() " check for disabled cursor line if(!exists("g:jslint_highlight_color") || strlen(g:jslint_highlight_color) == 0) return endif call s:ClearCursorLineColor() let s:highlight_on = 1 " find the current cursor line highlight info redir => l:highlight_info silent highlight CursorLine redir END " find the guibg property within the highlight info (if it exists) let l:start_index = match(l:highlight_info, "guibg") if(l:start_index > 0) let s:previous_cursor_guibg = strpart(l:highlight_info, l:start_index) elseif(exists("s:previous_cursor_guibg")) unlet s:previous_cursor_guibg endif execute "highlight CursorLine guibg=" . g:jslint_highlight_color endfunction " Conditionally reverts the cursor line color based on the presence " of the quickfix window function s:MaybeClearCursorLineColor() if(exists("s:qfix_buffer") && s:qfix_buffer == bufnr("%")) call s:ClearCursorLineColor() endif endfunction " Reverts the cursor line color function s:ClearCursorLineColor() " only revert if our highlight is currently enabled if(exists("s:highlight_on") && s:highlight_on) let s:highlight_on = 0 " if a previous cursor guibg color was recorded, we use it if(exists("s:previous_cursor_guibg")) execute "highlight CursorLine " . s:previous_cursor_guibg unlet s:previous_cursor_guibg " otherwise, we clear the curor line highlight entirely else highlight clear CursorLine endif endif endfunction