diff --git a/plugin/jslint/jslint.vim b/plugin/jslint/jslint.vim index ba6bc74..e797f20 100644 --- a/plugin/jslint/jslint.vim +++ b/plugin/jslint/jslint.vim @@ -1,16 +1,27 @@ -function! s:JSLint() +function! s:JSLint() range + cclose " Close quickfix window + cexpr [] " Create empty quickfix list + + " Detect range + if a:firstline == a:lastline + let b:firstline = 1 + let b:lastline = '$' + else + let b:firstline = a:firstline + let b:lastline = a:lastline + endif + + " Delete previous matches if exists('b:errors') for error in b:errors call matchdelete(error) endfor endif - let b:errors = [] - " Set up command and parameters let s:plugin_path = '"' . expand("~/") . '"' if has("win32") - let s:cmd = 'cscript' + let s:cmd = 'cscript /NoLogo ' let s:plugin_path = s:plugin_path . "vimfiles" let s:runjslint_ext = 'wsf' else @@ -20,22 +31,33 @@ function! s:JSLint() endif let s:plugin_path = s:plugin_path . "/plugin/jslint/" let s:cmd = "cd " . s:plugin_path . " && " . s:cmd . " " . s:plugin_path - \ . "runjslint." . s:runjslint_ext + \ . "runjslint." . s:runjslint_ext + let b:jslint_output = system(s:cmd, join(getline(b:firstline, b:lastline), + \ "\n") . "\n") - let b:jslint_output = system(s:cmd, join(getline(1, '$'), "\n") . "\n") + let b:errors = [] + let b:has_errors = 0 for error in split(b:jslint_output, "\n") - let b:parts = matchlist(error, "line\\s\\+\\(\\d\\+\\)\\s\\+") + " Match {line}:{char}:{message} + let b:parts = matchlist(error, "\\(\\d\\+\\):\\(\\d\\+\\):\\(.*\\)") if !empty(b:parts) - call add(b:errors, matchadd('Error', '\%'.b:parts[1].'l')) - if b:parts[1] == line('.') - echo(error) - endif - elseif error ==? "All Good." - echo "JSLint: " . error + let b:has_errors = 1 + let l:line = b:parts[1] + (b:firstline - 1) " Get line relative to selection + " Add line to match list + call add(b:errors, matchadd('Error', '\%' . l:line . 'l')) + " Add to quickfix + caddexpr expand("%") . ":" . l:line . ":" . b:parts[2] . ":" . b:parts[3] endif endfor + + " Open the quickfix window if errors are present + if b:has_errors == 1 + copen + else " Or not + echo "JSLint: All good." + endif endfunction -command! JSLint :call s:JSLint() +command! -range JSLint ,call s:JSLint() diff --git a/plugin/jslint/runjslint.js b/plugin/jslint/runjslint.js index db33f72..845a50e 100644 --- a/plugin/jslint/runjslint.js +++ b/plugin/jslint/runjslint.js @@ -24,17 +24,18 @@ var readSTDIN = function() { }; var body = readSTDIN() || arguments[0], - result = JSLINT(body), + ok = JSLINT(body), i, - error; + error, + errorCount; -if (result) { - print('All good.'); -} else { - print('Error:'); - for (i = 0; i < JSLINT.errors.length; i++) { +if (!ok) { + errorCount = JSLINT.errors.length; + for (i = 0; i < errorCount; i += 1) { error = JSLINT.errors[i]; - print('Problem at line ' + error.line + ' character ' + error.character + ': ' + error.reason); - print(error.evidence); + if (error && error.reason && error.reason.match(/^Stopping/) === null) { + print([error.line, error.character, error.reason].join(":")); + } } } + diff --git a/plugin/jslint/runjslint.wsf b/plugin/jslint/runjslint.wsf index 18fdcfe..b181d82 100644 --- a/plugin/jslint/runjslint.wsf +++ b/plugin/jslint/runjslint.wsf @@ -21,21 +21,20 @@ var readSTDIN = function() { }; var body = readSTDIN() || arguments[0], - result = JSLINT(body), + ok = JSLINT(body), i, - error; - -if (result) { - WScript.echo('All good.'); -} else { - WScript.echo('Error:'); - for (i = 0; i < JSLINT.errors.length; i++) { + error, + errorCount; + +if (!ok) { + errorCount = JSLINT.errors.length; + for (i = 0; i < errorCount; i += 1) { error = JSLINT.errors[i]; - if (error) { - WScript.echo('Problem at line ' + error.line + ' character ' + error.character + ': ' + error.reason); - WScript.echo(error.evidence); + if (error && error.reason && error.reason.match(/^Stopping/) === null) { + WScript.echo([error.line, error.character, error.reason].join(":")); } } } +