From 3530180a73ec53c6c029926173c34e0d78a8ac70 Mon Sep 17 00:00:00 2001 From: w0rp Date: Sat, 20 May 2017 18:56:44 +0100 Subject: [PATCH] Suggest functions for fixing issues for ALEFix --- autoload/ale/fix.vim | 2 +- autoload/ale/fix/registry.vim | 60 ++++++++++++++++++++++++++ plugin/ale.vim | 2 + test/test_ale_fix.vader | 2 +- test/test_ale_fix_suggest.vader | 75 +++++++++++++++++++++++++++++++++ 5 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 test/test_ale_fix_suggest.vader diff --git a/autoload/ale/fix.vim b/autoload/ale/fix.vim index 4ff977b..e329693 100644 --- a/autoload/ale/fix.vim +++ b/autoload/ale/fix.vim @@ -249,7 +249,7 @@ function! s:GetCallbacks() abort endfor if empty(l:callback_list) - echoerr 'No fixers have been defined for filetype: ' . &filetype + echoerr 'No fixers have been defined. Try :ALEFixSuggest' return [] endif diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index e524e13..b85c5d7 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -37,6 +37,11 @@ endfunction " Set up entries now. call ale#fix#registry#ResetToDefaults() +" Remove everything from the registry, useful for tests. +function! ale#fix#registry#Clear() abort + let s:entries = {} +endfunction + " Add a function for fixing problems to the registry. function! ale#fix#registry#Add(name, func, filetypes, desc) abort if type(a:name) != type('') @@ -72,3 +77,58 @@ endfunction function! ale#fix#registry#GetFunc(name) abort return get(s:entries, a:name, {'function': ''}).function endfunction + +function! s:ShouldSuggestForType(suggested_filetypes, type_list) abort + for l:type in a:type_list + if index(a:suggested_filetypes, l:type) >= 0 + return 1 + endif + endfor + + return 0 +endfunction + +" Suggest functions to use from the registry. +function! ale#fix#registry#Suggest(filetype) abort + let l:type_list = split(a:filetype, '\.') + let l:first_for_filetype = 1 + let l:first_generic = 1 + + for l:key in sort(keys(s:entries)) + let l:suggested_filetypes = s:entries[l:key].suggested_filetypes + + if s:ShouldSuggestForType(l:suggested_filetypes, l:type_list) + if l:first_for_filetype + let l:first_for_filetype = 0 + echom 'Try the following fixers appropriate for the filetype:' + echom '' + endif + + echom printf('%s - %s', string(l:key), s:entries[l:key].description) + endif + endfor + + + for l:key in sort(keys(s:entries)) + if empty(s:entries[l:key].suggested_filetypes) + if l:first_generic + if !l:first_for_filetype + echom '' + endif + + let l:first_generic = 0 + echom 'Try the following generic fixers:' + echom '' + endif + + echom printf('%s - %s', string(l:key), s:entries[l:key].description) + endif + endfor + + if l:first_for_filetype && l:first_generic + echom 'There is nothing in the registry to suggest.' + else + echom '' + echom 'See :help ale-fix-configuration' + endif +endfunction diff --git a/plugin/ale.vim b/plugin/ale.vim index 28b8beb..a1a8666 100644 --- a/plugin/ale.vim +++ b/plugin/ale.vim @@ -281,6 +281,8 @@ command! -bar ALEInfoToClipboard :call ale#debugging#InfoToClipboard() " Fix problems in files. command! -bar ALEFix :call ale#fix#Fix() +" Suggest registered functions to use for fixing problems. +command! -bar ALEFixSuggest :call ale#fix#registry#Suggest(&filetype) " mappings for commands nnoremap (ale_previous) :ALEPrevious diff --git a/test/test_ale_fix.vader b/test/test_ale_fix.vader index 23c61f9..dfe7944 100644 --- a/test/test_ale_fix.vader +++ b/test/test_ale_fix.vader @@ -53,7 +53,7 @@ Given testft (A file with three lines): Execute(ALEFix should complain when there are no functions to call): AssertThrows ALEFix - AssertEqual 'Vim(echoerr):No fixers have been defined for filetype: testft', g:vader_exception + AssertEqual 'Vim(echoerr):No fixers have been defined. Try :ALEFixSuggest', g:vader_exception Execute(ALEFix should apply simple functions): let g:ale_fixers.testft = ['AddCarets'] diff --git a/test/test_ale_fix_suggest.vader b/test/test_ale_fix_suggest.vader new file mode 100644 index 0000000..9a7aecb --- /dev/null +++ b/test/test_ale_fix_suggest.vader @@ -0,0 +1,75 @@ +Before: + call ale#fix#registry#Clear() + + function GetSuggestions() + redir => l:output + silent ALEFixSuggest + redir END + + return split(l:output, "\n") + endfunction + +After: + call ale#fix#registry#ResetToDefaults() + delfunction GetSuggestions + +Execute(ALEFixSuggest should return something sensible with no suggestions): + AssertEqual + \ [ + \ 'There is nothing in the registry to suggest.', + \ ], + \ GetSuggestions() + +Execute(ALEFixSuggest output should be correct for only generic handlers): + call ale#fix#registry#Add('zed', 'XYZ', [], 'Zedify things.') + call ale#fix#registry#Add('alpha', 'XYZ', [], 'Alpha things.') + + AssertEqual + \ [ + \ 'Try the following generic fixers:', + \ '', + \ '''alpha'' - Alpha things.', + \ '''zed'' - Zedify things.', + \ '', + \ 'See :help ale-fix-configuration', + \ ], + \ GetSuggestions() + +Execute(ALEFixSuggest output should be correct for only filetype handlers): + let &filetype = 'testft2.testft' + + call ale#fix#registry#Add('zed', 'XYZ', ['testft2'], 'Zedify things.') + call ale#fix#registry#Add('alpha', 'XYZ', ['testft'], 'Alpha things.') + + AssertEqual + \ [ + \ 'Try the following fixers appropriate for the filetype:', + \ '', + \ '''alpha'' - Alpha things.', + \ '''zed'' - Zedify things.', + \ '', + \ 'See :help ale-fix-configuration', + \ ], + \ GetSuggestions() + +Execute(ALEFixSuggest should suggest filetype and generic handlers): + let &filetype = 'testft2.testft' + + call ale#fix#registry#Add('zed', 'XYZ', ['testft2'], 'Zedify things.') + call ale#fix#registry#Add('alpha', 'XYZ', ['testft'], 'Alpha things.') + call ale#fix#registry#Add('generic', 'XYZ', [], 'Generic things.') + + AssertEqual + \ [ + \ 'Try the following fixers appropriate for the filetype:', + \ '', + \ '''alpha'' - Alpha things.', + \ '''zed'' - Zedify things.', + \ '', + \ 'Try the following generic fixers:', + \ '', + \ '''generic'' - Generic things.', + \ '', + \ 'See :help ale-fix-configuration', + \ ], + \ GetSuggestions()