From c89c4fcef9379ec44ed5e0ec74c9f384ad4f001d Mon Sep 17 00:00:00 2001 From: w0rp Date: Mon, 3 Oct 2016 13:18:27 +0100 Subject: [PATCH] Add support for shellcheck linting. --- README.md | 2 +- ale_linters/c/gcc.vim | 31 +-------------------------- ale_linters/sh/shellcheck.vim | 32 ++++++++++++++++++++++++++++ plugin/ale/handlers.vim | 40 +++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 31 deletions(-) create mode 100644 ale_linters/sh/shellcheck.vim create mode 100644 plugin/ale/handlers.vim diff --git a/README.md b/README.md index 934998b..56ae89b 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ name. That seems to be the fairest way to arrange this table. | Language | Tools | | -------- | ----- | | Bash | [-n flag](https://www.gnu.org/software/bash/manual/bash.html#index-set) | -| Bourne Shell | [-n flag](http://linux.die.net/man/1/sh) | +| Bourne Shell | [-n flag](http://linux.die.net/man/1/sh), [shellcheck](https://www.shellcheck.net/) | | C | [gcc](https://gcc.gnu.org/) | | CoffeeScript | [coffeelint](https://www.npmjs.com/package/coffeelint) | | D | [dmd](https://dlang.org/dmd-linux.html)^ | diff --git a/ale_linters/c/gcc.vim b/ale_linters/c/gcc.vim index 6bd335d..083c318 100644 --- a/ale_linters/c/gcc.vim +++ b/ale_linters/c/gcc.vim @@ -9,35 +9,6 @@ if !exists('g:ale_c_gcc_options') let g:ale_c_gcc_options = '-Wall' endif -function! ale_linters#c#gcc#Handle(buffer, lines) - " Look for lines like the following. - " - " :8:5: warning: conversion lacks type at end of format [-Wformat=] - " :10:27: error: invalid operands to binary - (have ‘int’ and ‘char *’) - let pattern = '^:\(\d\+\):\(\d\+\): \(warning\|error\): \(.\+\)$' - let output = [] - - for line in a:lines - let l:match = matchlist(line, pattern) - - if len(l:match) == 0 - continue - endif - - call add(output, { - \ 'bufnr': a:buffer, - \ 'lnum': l:match[1] + 0, - \ 'vcol': 0, - \ 'col': l:match[2] + 0, - \ 'text': l:match[4], - \ 'type': l:match[3] ==# 'warning' ? 'W' : 'E', - \ 'nr': -1, - \}) - endfor - - return output -endfunction - call ALEAddLinter('c', { \ 'name': 'gcc', \ 'output_stream': 'stderr', @@ -45,5 +16,5 @@ call ALEAddLinter('c', { \ 'command': 'gcc -S -x c -fsyntax-only ' \ . g:ale_c_gcc_options \ . ' -', -\ 'callback': 'ale_linters#c#gcc#Handle', +\ 'callback': 'ale#handlers#HandleGCCFormat', \}) diff --git a/ale_linters/sh/shellcheck.vim b/ale_linters/sh/shellcheck.vim new file mode 100644 index 0000000..6a96d12 --- /dev/null +++ b/ale_linters/sh/shellcheck.vim @@ -0,0 +1,32 @@ +" Author: w0rp +" Description: This file adds support for using the shellcheck linter with +" shell scripts. + +if exists('g:loaded_ale_linters_sh_shellcheck') + finish +endif + +let g:loaded_ale_linters_sh_shellcheck = 1 + +" This global variable can be set with a string of comma-seperated error +" codes to exclude from shellcheck. For example: +" +" let g:ale_linters_sh_shellcheck_exclusions = 'SC2002,SC2004' +if !exists('g:ale_linters_sh_shellcheck_exclusions') + let g:ale_linters_sh_shellcheck_exclusions = '' +endif + +if g:ale_linters_sh_shellcheck_exclusions != '' + let s:exclude_option = '-e ' . g:ale_linters_sh_shellcheck_exclusions +else + let s:exclude_option = '' +endif + +call ALEAddLinter('sh', { +\ 'name': 'shellcheck', +\ 'executable': 'shellcheck', +\ 'command': 'shellcheck ' . s:exclude_option . ' -f gcc -', +\ 'callback': 'ale#handlers#HandleGCCFormat', +\}) + +echo 'shellcheck' . s:exclude_option . '-f gcc -' diff --git a/plugin/ale/handlers.vim b/plugin/ale/handlers.vim new file mode 100644 index 0000000..6777ac4 --- /dev/null +++ b/plugin/ale/handlers.vim @@ -0,0 +1,40 @@ +" Author: w0rp +" Description: This file defines some standard error format handlers. Any +" linter which outputs warnings and errors in a format accepted by one of +" these functions can simply use one of these pre-defined error handlers. + +if exists('g:loaded_ale_handlers') + finish +endif + +let g:loaded_ale_handlers = 1 + +function! ale#handlers#HandleGCCFormat(buffer, lines) + " Look for lines like the following. + " + " :8:5: warning: conversion lacks type at end of format [-Wformat=] + " :10:27: error: invalid operands to binary - (have ‘int’ and ‘char *’) + " -:189:7: note: $/${} is unnecessary on arithmetic variables. [SC2004] + let pattern = '^[^:]\+:\(\d\+\):\(\d\+\): \([^:]\+\): \(.\+\)$' + let output = [] + + for line in a:lines + let l:match = matchlist(line, pattern) + + if len(l:match) == 0 + continue + endif + + call add(output, { + \ 'bufnr': a:buffer, + \ 'lnum': l:match[1] + 0, + \ 'vcol': 0, + \ 'col': l:match[2] + 0, + \ 'text': l:match[4], + \ 'type': l:match[3] ==# 'error' ? 'E' : 'W', + \ 'nr': -1, + \}) + endfor + + return output +endfunction