From 78bcf96e345c9aa31412ae06e1da2bbe6a568ff5 Mon Sep 17 00:00:00 2001 From: w0rp Date: Tue, 11 Oct 2016 23:11:45 +0100 Subject: [PATCH] Fix #87 - Allow linter filetypes to be aliased --- ale_linters/javascript/eslint.vim | 7 --- ale_linters/javascript/jscs.vim | 7 --- ale_linters/javascript/jshint.vim | 7 --- autoload/ale/linter.vim | 72 ++++++++++++++++++++++++++----- doc/ale.txt | 56 +++++++++++++++++++++--- plugin/ale.vim | 5 ++- 6 files changed, 116 insertions(+), 38 deletions(-) diff --git a/ale_linters/javascript/eslint.vim b/ale_linters/javascript/eslint.vim index 909c112..c5a2555 100644 --- a/ale_linters/javascript/eslint.vim +++ b/ale_linters/javascript/eslint.vim @@ -55,10 +55,3 @@ call ale#linter#Define('javascript', { \ 'command': g:ale_javascript_eslint_executable . ' -f unix --stdin --stdin-filename %s', \ 'callback': 'ale_linters#javascript#eslint#Handle', \}) - -call ale#linter#Define('javascript.jsx', { -\ 'name': 'eslint', -\ 'executable': g:ale_javascript_eslint_executable, -\ 'command': g:ale_javascript_eslint_executable . ' -f unix --stdin --stdin-filename %s', -\ 'callback': 'ale_linters#javascript#eslint#Handle', -\}) diff --git a/ale_linters/javascript/jscs.vim b/ale_linters/javascript/jscs.vim index 501a328..bddee24 100644 --- a/ale_linters/javascript/jscs.vim +++ b/ale_linters/javascript/jscs.vim @@ -13,10 +13,3 @@ call ale#linter#Define('javascript', { \ 'command': 'jscs -r unix -n -', \ 'callback': 'ale#handlers#HandleUnixFormatAsError', \}) - -call ale#linter#Define('javascript.jsx', { -\ 'name': 'jscs', -\ 'executable': 'jscs', -\ 'command': 'jscs -r unix -n -', -\ 'callback': 'ale#handlers#HandleUnixFormatAsError', -\}) diff --git a/ale_linters/javascript/jshint.vim b/ale_linters/javascript/jshint.vim index 571d05d..a2ef5ae 100644 --- a/ale_linters/javascript/jshint.vim +++ b/ale_linters/javascript/jshint.vim @@ -37,10 +37,3 @@ call ale#linter#Define('javascript', { \ 'command_callback': 'ale_linters#javascript#jshint#GetCommand', \ 'callback': 'ale#handlers#HandleUnixFormatAsError', \}) - -call ale#linter#Define('javascript.jsx', { -\ 'name': 'jshint', -\ 'executable': g:ale_javascript_jshint_executable, -\ 'command_callback': 'ale_linters#javascript#jshint#GetCommand', -\ 'callback': 'ale#handlers#HandleUnixFormatAsError', -\}) diff --git a/autoload/ale/linter.vim b/autoload/ale/linter.vim index 010256d..0cd3f9b 100644 --- a/autoload/ale/linter.vim +++ b/autoload/ale/linter.vim @@ -4,6 +4,21 @@ let s:linters = {} +" Default filetype aliaes. +" The user defined aliases will be merged with this Dictionary. +let s:default_ale_linter_aliases = { +\ 'javscript.jsx': 'javascript', +\ 'zsh': 'sh', +\ 'csh': 'sh', +\} + +" Default linters to run for particular filetypes. +" The user defined linter selections will be merged with this Dictionary. +let s:default_ale_linters = { +\ 'zsh': ['shell'], +\ 'csh': ['shell'], +\} + function! ale#linter#Define(filetype, linter) abort if !has_key(s:linters, a:filetype) let s:linters[a:filetype] = [] @@ -37,25 +52,19 @@ function! ale#linter#Define(filetype, linter) abort call add(s:linters[a:filetype], l:new_linter) endfunction -function! ale#linter#Get(filetype) abort +function! s:LoadLinters(filetype) abort if a:filetype ==# '' " Empty filetype? Nothing to be done about that. return [] endif if has_key(s:linters, a:filetype) - " We already loaded a linter, great! + " We already loaded the linter files for this filetype, so stop here. return s:linters[a:filetype] endif - if has_key(g:ale_linters, a:filetype) - " Filter loaded linters according to list of linters specified in option. - for l:linter in g:ale_linters[a:filetype] - execute 'runtime! ale_linters/' . a:filetype . '/' . l:linter . '.vim' - endfor - else - execute 'runtime! ale_linters/' . a:filetype . '/*.vim' - endif + " Load all linters for a given filetype. + execute 'runtime! ale_linters/' . a:filetype . '/*.vim' if !has_key(s:linters, a:filetype) " If we couldn't load any linters, let everyone know. @@ -64,3 +73,46 @@ function! ale#linter#Get(filetype) abort return s:linters[a:filetype] endfunction + +function! ale#linter#Get(original_filetype) abort + " Try and get an aliased file type either from the user's Dictionary, or + " our default Dictionary, otherwise use the filetype as-is. + let l:filetype = get( + \ g:ale_linter_aliases, + \ a:original_filetype, + \ get( + \ s:default_ale_linter_aliases, + \ a:original_filetype, + \ a:original_filetype + \ ) + \) + + " Try and get a list of linters to run, using the original file type, + " not the aliased filetype. We have some linters to limit by default, + " and users may define their own list of linters to run. + let l:linter_names = get( + \ g:ale_linters, + \ a:original_filetype, + \ get( + \ s:default_ale_linters, + \ a:original_filetype, + \ 'all' + \ ) + \) + + let l:all_linters = s:LoadLinters(l:filetype) + let l:combined_linters = [] + + if type(l:linter_names) == type('') && l:linter_names ==# 'all' + let l:combined_linters = l:all_linters + elseif type(l:linter_names) == type([]) + " Select only the linters we or the user has specified. + for l:linter in l:all_linters + if index(l:linter_names, l:linter.name) + call add(l:combined_linters, l:linter) + endif + endfor + endif + + return l:combined_linters +endfunction diff --git a/doc/ale.txt b/doc/ale.txt index 2b97513..2f4349e 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -80,22 +80,65 @@ The following languages and tools are supported. g:ale_linters *g:ale_linters* Type: |Dictionary| - Default: unset + Default: `{}` The |g:ale_linters| option sets a |Dictionary| mapping a filetype to a |List| of linter programs to be run when checking particular filetypes. - By default, this dictionary will not be set at all, and all possible - linter programs will be run for a given filetype, if the linter programs - are found to be |executable|. + Only the filetypes specified in the dictionary will be limited in terms + of which linters will be run. + This |Dictionary| will be merged with a default dictionary containing the + following values: > + + { + \ 'zsh': ['shell'], + \ 'csh': ['shell'], + \} +< This option can be used to enable only a particular set of linters for a file. For example, you can enable only 'eslint' for JavaScript files: > - let g:ale_linters = {'javascript': ['eslint']} + + let g:ale_linters = {'javascript': ['eslint']} < If you want to disable all linters for a particular filetype, you can pass an empty list of linters as the value: > - let g:ale_linters = {'javascript': []} + + let g:ale_linters = {'javascript': []} < + All linters available for a given filetype can be enabled by using the + string `'all'`: > + + let g:ale_linters = {'c': 'all'} +< + +g:ale_linter_aliases *g:ale_linter_aliases* + + Type: |Dictionary| + Default: `{}` + + The |g:ale_linter_aliases| option can be used to set aliases from one + filetype to another. A given filetype can be mapped to use the linters + run for another given filetype. + + This |Dictionary| will be merged with a default dictionary containing the + following values: > + + { + \ 'javscript.jsx': 'javascript', + \ 'zsh': 'sh', + \ 'csh': 'sh', + \} +< + For example, if you wish to map a new filetype `'foobar'` to run the `'php'` + linters, you could set the following: > + + let g:ale_linter_aliases = {'foobar': 'php'} +< + When combined with the |g:ale_linters| option, the original filetype + (`'foobar'`) will be used for determining which linters to run, + not the aliased type (`'php'`). This allows an aliased type to run a + different set of linters from the type it is being mapped to. + g:ale_buffer_loclist_map *g:ale_buffer_loclist_map* @@ -556,6 +599,7 @@ g:ale#util#stdin_wrapper *g:ale#util#stdin_wrapper* This variable names a wrapper script for sending stdin input to programs which cannot accept input via stdin. See |ale#linter#Define()| for more. + =============================================================================== 6. Special Thanks *ale-special-thanks* diff --git a/plugin/ale.vim b/plugin/ale.vim index 4f59f79..9b45cd7 100644 --- a/plugin/ale.vim +++ b/plugin/ale.vim @@ -30,9 +30,12 @@ let g:ale_buffer_sign_dummy_map = {} " User Configuration -" This list configures which linters are enabled for which languages. +" This Dictionary configures which linters are enabled for which languages. let g:ale_linters = get(g:, 'ale_linters', {}) +" This Dictionary allows users to set up filetype aliases for new filetypes. +let g:ale_linter_aliases = get(g:, 'ale_linter_aliases', {}) + " This flag can be set with a number of milliseconds for delaying the " execution of a linter when text is changed. The timeout will be set and " cleared each time text is changed, so repeated edits won't trigger the