From a9c650cd05d5680a5e691eefb86e46bd6604ea1b Mon Sep 17 00:00:00 2001 From: DiscoViking Date: Wed, 25 Jan 2017 00:50:49 +0900 Subject: [PATCH] Add ALEInfo command to get list of available/enabled linters (#273) * Add ALEInfo command to get list of available/enabled linters for current filetype * Add Vader tests for ALEInfo command * Fix ALEInfo tests breaking CI by echoing too much output to screen * Speculative change to Makefile which seems to fix test hanging problem locally. * Fix Vader tests to not require a TTY --- autoload/ale/linter.vim | 54 ++++++++++++++++++------ plugin/ale.vim | 3 ++ test/test_ale_info.vader | 91 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 135 insertions(+), 13 deletions(-) create mode 100644 test/test_ale_info.vader diff --git a/autoload/ale/linter.vim b/autoload/ale/linter.vim index 8885e00b..a94e977b 100644 --- a/autoload/ale/linter.vim +++ b/autoload/ale/linter.vim @@ -103,7 +103,7 @@ function! ale#linter#Define(filetype, linter) abort call add(s:linters[a:filetype], l:new_linter) endfunction -function! s:LoadLinters(filetype) abort +function! ale#linter#GetAll(filetype) abort if a:filetype ==# '' " Empty filetype? Nothing to be done about that. return [] @@ -125,22 +125,28 @@ function! s:LoadLinters(filetype) abort return s:linters[a:filetype] endfunction +function! s:ResolveFiletype(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 + \ ) + \) + + return l:filetype +endfunction + function! ale#linter#Get(original_filetypes) abort let l:combined_linters = [] " Handle dot-seperated filetypes. for l:original_filetype in split(a:original_filetypes, '\.') - " 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, - \ l:original_filetype, - \ get( - \ s:default_ale_linter_aliases, - \ l:original_filetype, - \ l:original_filetype - \ ) - \) + let l:filetype = s:ResolveFiletype(l: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, @@ -155,7 +161,7 @@ function! ale#linter#Get(original_filetypes) abort \ ) \) - let l:all_linters = s:LoadLinters(l:filetype) + let l:all_linters = ale#linter#GetAll(l:filetype) let l:filetype_linters = [] if type(l:linter_names) == type('') && l:linter_names ==# 'all' @@ -174,3 +180,25 @@ function! ale#linter#Get(original_filetypes) abort return l:combined_linters endfunction + +function! ale#linter#Info() abort + let l:original_filetypes = &filetype + + " We get the list of enabled linters for free by the above function. + let l:enabled_linters = deepcopy(ale#linter#Get(l:original_filetypes)) + + " But have to build the list of available linters ourselves. + let l:all_linters = [] + for l:original_filetype in split(l:original_filetypes, '\.') + let l:filetype = s:ResolveFiletype(l:original_filetype) + let l:filetype_linters = ale#linter#GetAll(l:filetype) + call extend(l:all_linters, l:filetype_linters) + endfor + + let l:all_names = map(l:all_linters, 'v:val[''name'']') + let l:enabled_names = map(l:enabled_linters, 'v:val[''name'']') + + echom ' Current Filetype: ' . l:original_filetypes + echom 'Available Linters: ' . string(l:all_names) + echom ' Enabled Linters: ' . string(l:enabled_names) +endfunction diff --git a/plugin/ale.vim b/plugin/ale.vim index 28812be6..5c319870 100644 --- a/plugin/ale.vim +++ b/plugin/ale.vim @@ -138,6 +138,9 @@ command! ALEPreviousWrap :call ale#loclist_jumping#Jump('before', 1) command! ALENext :call ale#loclist_jumping#Jump('after', 0) command! ALENextWrap :call ale#loclist_jumping#Jump('after', 1) +" Define command to get information about current filetype. +command! ALEInfo :call ale#linter#Info() + " mappings for commands nnoremap (ale_previous) :ALEPrevious nnoremap (ale_previous_wrap) :ALEPreviousWrap diff --git a/test/test_ale_info.vader b/test/test_ale_info.vader new file mode 100644 index 00000000..f8172c0c --- /dev/null +++ b/test/test_ale_info.vader @@ -0,0 +1,91 @@ +Before: + let g:testlinter1 = {'name': 'testlinter1', 'executable': 'testlinter1', 'command': 'testlinter1', 'callback': 'testCB1', 'output_stream': 'stdout'} + let g:testlinter2 = {'name': 'testlinter2', 'executable': 'testlinter2', 'command': 'testlinter2', 'callback': 'testCB2', 'output_stream': 'stdout'} + + call ale#linter#Reset() + let g:ale_linters = {} + let g:ale_linter_aliases = {} + +After: + unlet! g:output + +Given nolintersft (Empty buffer with no linters): +Execute (ALEInfo with no linters should return the right output): + redir => g:output + silent ALEInfo + redir END + AssertEqual "\n + \ Current Filetype: nolintersft\n + \Available Linters: []\n + \ Enabled Linters: []", g:output + +Given (Empty buffer with no filetype): +Execute (ALEInfo with no filetype should return the right output): + redir => g:output + silent ALEInfo + redir END + AssertEqual "\n + \ Current Filetype: \n + \Available Linters: []\n + \ Enabled Linters: []", g:output + +Given testft (Empty buffer): +Execute (ALEInfo with a single linter should return the right output): + call ale#linter#Define('testft', g:testlinter1) + redir => g:output + silent ALEInfo + redir END + AssertEqual "\n + \ Current Filetype: testft\n + \Available Linters: ['testlinter1']\n + \ Enabled Linters: ['testlinter1']", g:output + +Given testft (Empty buffer): +Execute (ALEInfo with two linters should return the right output): + call ale#linter#Define('testft', g:testlinter1) + call ale#linter#Define('testft', g:testlinter2) + redir => g:output + silent ALEInfo + redir END + AssertEqual "\n + \ Current Filetype: testft\n + \Available Linters: ['testlinter1', 'testlinter2']\n + \ Enabled Linters: ['testlinter1', 'testlinter2']", g:output + +Given testft (Empty buffer): +Execute (ALEInfo should calculate enabled linters correctly): + call ale#linter#Define('testft', g:testlinter1) + call ale#linter#Define('testft', g:testlinter2) + let g:ale_linters = { 'testft': ['testlinter2'] } + redir => g:output + silent ALEInfo + redir END + AssertEqual "\n + \ Current Filetype: testft\n + \Available Linters: ['testlinter1', 'testlinter2']\n + \ Enabled Linters: ['testlinter2']", g:output + +Given testft (Empty buffer): +Execute (ALEInfo should only return linters for current filetype): + call ale#linter#Define('testft', g:testlinter1) + call ale#linter#Define('testft2', g:testlinter2) + redir => g:output + silent ALEInfo + redir END + AssertEqual "\n + \ Current Filetype: testft\n + \Available Linters: ['testlinter1']\n + \ Enabled Linters: ['testlinter1']", g:output + +Given testft.testft2 (Empty buffer with two filetypes): +Execute (ALEInfo with compound filetypes should return linters for both of them): + call ale#linter#Define('testft', g:testlinter1) + call ale#linter#Define('testft2', g:testlinter2) + redir => g:output + silent ALEInfo + redir END + AssertEqual "\n + \ Current Filetype: testft.testft2\n + \Available Linters: ['testlinter1', 'testlinter2']\n + \ Enabled Linters: ['testlinter1', 'testlinter2']", g:output +