From 5d2ab192cf4ecce604cee4e8870ff7d5d3f47ed8 Mon Sep 17 00:00:00 2001 From: w0rp Date: Wed, 22 Nov 2017 15:31:39 +0000 Subject: [PATCH] Support fixer aliases, and make prettier-eslint and prettier-standard just work --- autoload/ale/fix/registry.vim | 60 +++++++++++++++++++++++++++-- doc/ale.txt | 8 +++- syntax/ale-fix-suggest.vim | 2 +- test/fix/test_ale_fix_aliases.vader | 5 +++ test/fix/test_ale_fix_suggest.vader | 4 +- 5 files changed, 71 insertions(+), 8 deletions(-) create mode 100644 test/fix/test_ale_fix_aliases.vader diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index 2e24e02..4fb229b 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -21,6 +21,7 @@ let s:default_registry = { \ 'function': 'ale#fixers#prettier_standard#Fix', \ 'suggested_filetypes': ['javascript'], \ 'description': 'Apply prettier-standard to a file.', +\ 'aliases': ['prettier-standard'], \ }, \ 'eslint': { \ 'function': 'ale#fixers#eslint#Fix', @@ -51,6 +52,7 @@ let s:default_registry = { \ 'function': 'ale#fixers#prettier_eslint#Fix', \ 'suggested_filetypes': ['javascript'], \ 'description': 'Apply prettier-eslint to a file.', +\ 'aliases': ['prettier-eslint'], \ }, \ 'puppetlint': { \ 'function': 'ale#fixers#puppetlint#Fix', @@ -147,6 +149,14 @@ let s:default_registry = { " Reset the function registry to the default entries. function! ale#fix#registry#ResetToDefaults() abort let s:entries = deepcopy(s:default_registry) + let s:aliases = {} + + " Set up aliases for fixers too. + for [l:key, l:entry] in items(s:entries) + for l:alias in get(l:entry, 'aliases', []) + let s:aliases[l:alias] = l:key + endfor + endfor endfunction " Set up entries now. @@ -155,10 +165,12 @@ call ale#fix#registry#ResetToDefaults() " Remove everything from the registry, useful for tests. function! ale#fix#registry#Clear() abort let s:entries = {} + let s:aliases = {} endfunction " Add a function for fixing problems to the registry. -function! ale#fix#registry#Add(name, func, filetypes, desc) abort +" (name, func, filetypes, desc, aliases) +function! ale#fix#registry#Add(name, func, filetypes, desc, ...) abort if type(a:name) != type('') throw '''name'' must be a String' endif @@ -181,16 +193,37 @@ function! ale#fix#registry#Add(name, func, filetypes, desc) abort throw '''desc'' must be a String' endif + let l:aliases = get(a:000, 0, []) + + if type(l:aliases) != type([]) + \|| !empty(filter(copy(l:aliases), 'type(v:val) != type('''')')) + throw '''aliases'' must be a List of String values' + endif + let s:entries[a:name] = { \ 'function': a:func, \ 'suggested_filetypes': a:filetypes, \ 'description': a:desc, \} + + " Set up aliases for the fixer. + if !empty(l:aliases) + let s:entries[a:name].aliases = l:aliases + + for l:alias in l:aliases + let s:aliases[l:alias] = a:name + endfor + endif endfunction " Get a function from the registry by its short name. function! ale#fix#registry#GetFunc(name) abort - return get(s:entries, a:name, {'function': ''}).function + " Use the exact name, or an alias. + let l:resolved_name = !has_key(s:entries, a:name) + \ ? get(s:aliases, a:name, a:name) + \ : a:name + + return get(s:entries, l:resolved_name, {'function': ''}).function endfunction function! s:ShouldSuggestForType(suggested_filetypes, type_list) abort @@ -203,6 +236,25 @@ function! s:ShouldSuggestForType(suggested_filetypes, type_list) abort return 0 endfunction +function! s:FormatEntry(key, entry) abort + let l:aliases_str = '' + + " Show aliases in :ALEFixSuggest if they are there. + if !empty(get(a:entry, 'aliases', [])) + let l:aliases_str = ', ' . join( + \ map(copy(a:entry.aliases), 'string(v:val)'), + \ ',' + \) + endif + + return printf( + \ '%s%s - %s', + \ string(a:key), + \ l:aliases_str, + \ a:entry.description, + \) +endfunction + " Suggest functions to use from the registry. function! ale#fix#registry#Suggest(filetype) abort let l:type_list = split(a:filetype, '\.') @@ -214,7 +266,7 @@ function! ale#fix#registry#Suggest(filetype) abort if s:ShouldSuggestForType(l:suggested_filetypes, l:type_list) call add( \ l:filetype_fixer_list, - \ printf('%s - %s', string(l:key), s:entries[l:key].description), + \ s:FormatEntry(l:key, s:entries[l:key]), \) endif endfor @@ -225,7 +277,7 @@ function! ale#fix#registry#Suggest(filetype) abort if empty(s:entries[l:key].suggested_filetypes) call add( \ l:generic_fixer_list, - \ printf('%s - %s', string(l:key), s:entries[l:key].description), + \ s:FormatEntry(l:key, s:entries[l:key]), \) endif endfor diff --git a/doc/ale.txt b/doc/ale.txt index ff8ac08..f7141cf 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -1780,7 +1780,8 @@ ale#engine#ManageDirectory(buffer, directory) *ale#engine#ManageDirectory()* files. -ale#fix#registry#Add(name, func, filetypes, desc) *ale#fix#registry#Add()* +ale#fix#registry#Add(name, func, filetypes, desc, [aliases]) + *ale#fix#registry#Add()* Given a |String| `name` for a name to add to the registry, a |String| `func` for a function name, a |List| `filetypes` for a list of filetypes to @@ -1790,6 +1791,11 @@ ale#fix#registry#Add(name, func, filetypes, desc) *ale#fix#registry#Add()* The `name` can then be used for |g:ale_fixers| in place of the function name, and suggested for fixing files. + An optional |List| of |String|s for aliases can be passed as the `aliases` + argument. These aliases can also be used for looking up a fixer function. + ALE will search for fixers in the registry first by `name`, then by their + `aliases`. + ale#linter#Define(filetype, linter) *ale#linter#Define()* diff --git a/syntax/ale-fix-suggest.vim b/syntax/ale-fix-suggest.vim index be3d45e..b112f5b 100644 --- a/syntax/ale-fix-suggest.vim +++ b/syntax/ale-fix-suggest.vim @@ -3,7 +3,7 @@ if exists('b:current_syntax') endif syn match aleFixerComment /^.*$/ -syn match aleFixerName /^'[^']*'/ +syn match aleFixerName /\(^\|, \)'[^']*'/ syn match aleFixerHelp /^See :help ale-fix-configuration/ hi def link aleFixerComment Comment diff --git a/test/fix/test_ale_fix_aliases.vader b/test/fix/test_ale_fix_aliases.vader new file mode 100644 index 0000000..d3c47b3 --- /dev/null +++ b/test/fix/test_ale_fix_aliases.vader @@ -0,0 +1,5 @@ +Execute(prettier-eslint should be aliased): + AssertEqual 'ale#fixers#prettier_eslint#Fix', ale#fix#registry#GetFunc('prettier-eslint') + +Execute(prettier-standard should be aliased): + AssertEqual 'ale#fixers#prettier_standard#Fix', ale#fix#registry#GetFunc('prettier-standard') diff --git a/test/fix/test_ale_fix_suggest.vader b/test/fix/test_ale_fix_suggest.vader index 97227b4..1100aee 100644 --- a/test/fix/test_ale_fix_suggest.vader +++ b/test/fix/test_ale_fix_suggest.vader @@ -80,7 +80,7 @@ Execute(ALEFixSuggest output should be correct for only filetype handlers): 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('zed', 'XYZ', ['testft2'], 'Zedify things.', ['foobar']) call ale#fix#registry#Add('alpha', 'XYZ', ['testft'], 'Alpha things.') call ale#fix#registry#Add('generic', 'XYZ', [], 'Generic things.') @@ -89,7 +89,7 @@ Execute(ALEFixSuggest should suggest filetype and generic handlers): \ 'Try the following fixers appropriate for the filetype:', \ '', \ '''alpha'' - Alpha things.', - \ '''zed'' - Zedify things.', + \ '''zed'', ''foobar'' - Zedify things.', \ '', \ 'Try the following generic fixers:', \ '',