From a591b191db819f2c78a852936bd9742a831f56cb Mon Sep 17 00:00:00 2001 From: fenuks Date: Thu, 12 Apr 2018 02:23:58 +0200 Subject: [PATCH] Make ALEFix support arguments --- autoload/ale/fix.vim | 9 ++++++--- autoload/ale/fix/registry.vim | 31 ++++++++++++++++++++++++++++++- plugin/ale.vim | 2 +- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/autoload/ale/fix.vim b/autoload/ale/fix.vim index 197a1862..066ae1af 100644 --- a/autoload/ale/fix.vim +++ b/autoload/ale/fix.vim @@ -356,8 +356,11 @@ function! s:RunFixer(options) abort call ale#fix#ApplyFixes(l:buffer, l:input) endfunction +function! s:GetCallbacks(linters) abort function! s:GetCallbacks(buffer) abort - if type(get(b:, 'ale_fixers')) is type([]) + if len(a:linters) + let l:callback_list = a:linters + elseif type(get(b:, 'ale_fixers')) is type([]) " Lists can be used for buffer-local variables only let l:callback_list = b:ale_fixers else @@ -422,13 +425,13 @@ endfunction " Accepts an optional argument for what to do when fixing. " " Returns 0 if no fixes can be applied, and 1 if fixing can be done. -function! ale#fix#Fix(buffer, fixing_flag) abort +function! ale#fix#Fix(buffer, fixing_flag, ...) abort if a:fixing_flag isnot# '' && a:fixing_flag isnot# 'save_file' throw "fixing_flag must be either '' or 'save_file'" endif try - let l:callback_list = s:GetCallbacks(a:buffer) + let l:callback_list = s:GetCallbacks(a:buffer, a:000) catch /E700\|BADNAME/ let l:function_name = join(split(split(v:exception, ':')[3])) let l:echo_message = printf( diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index 86a10908..d12cf0d4 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -277,6 +277,14 @@ function! s:ShouldSuggestForType(suggested_filetypes, type_list) abort return 0 endfunction +function! s:IsGenericFixer(suggested_filetypes) abort + if empty(a:suggested_filetypes) + return 1 + endif + + return 0 +endfunction + function! s:FormatEntry(key, entry) abort let l:aliases_str = '' @@ -296,6 +304,27 @@ function! s:FormatEntry(key, entry) abort \) endfunction +" Get list of applicable fixers for filetype, including generic fixers +function! ale#fix#registry#GetApplicableFixers(filetype) abort + let l:type_list = split(a:filetype, '\.') + let l:fixer_name_list = [] + + for l:key in sort(keys(s:entries)) + let l:suggested_filetypes = s:entries[l:key].suggested_filetypes + + if s:IsGenericFixer(l:suggested_filetypes) || s:ShouldSuggestForType(l:suggested_filetypes, l:type_list) + call add(l:fixer_name_list, l:key) + endif + endfor + + return l:fixer_name_list +endfunction + +" Function that returns autocomplete candidates for ALEFix command +function! ale#fix#registry#CompleteFixers(ArgLead, CmdLine, CursorPos) abort + return ale#fix#registry#GetApplicableFixers(&filetype) +endfunction + " Suggest functions to use from the registry. function! ale#fix#registry#Suggest(filetype) abort let l:type_list = split(a:filetype, '\.') @@ -315,7 +344,7 @@ function! ale#fix#registry#Suggest(filetype) abort let l:generic_fixer_list = [] for l:key in sort(keys(s:entries)) - if empty(s:entries[l:key].suggested_filetypes) + if s:IsGenericFixer(s:entries[l:key].suggested_filetypes) call add( \ l:generic_fixer_list, \ s:FormatEntry(l:key, s:entries[l:key]), diff --git a/plugin/ale.vim b/plugin/ale.vim index d64229b3..3f157bc0 100644 --- a/plugin/ale.vim +++ b/plugin/ale.vim @@ -263,7 +263,7 @@ command! -bar ALEInfoToClipboard :call ale#debugging#InfoToClipboard() command! -bar -nargs=1 ALEInfoToFile :call ale#debugging#InfoToFile() " Fix problems in files. -command! -bar ALEFix :call ale#fix#Fix(bufnr(''), '') +command! -bar -nargs=* -complete=customlist,ale#fix#registry#CompleteFixers ALEFix :call ale#fix#Fix(bufnr(''), '', ) " Suggest registered functions to use for fixing problems. command! -bar ALEFixSuggest :call ale#fix#registry#Suggest(&filetype)