Support fixer aliases, and make prettier-eslint and prettier-standard just work

This commit is contained in:
w0rp 2017-11-22 15:31:39 +00:00
parent 5ed6f66f77
commit 5d2ab192cf
5 changed files with 71 additions and 8 deletions

View File

@ -21,6 +21,7 @@ let s:default_registry = {
\ 'function': 'ale#fixers#prettier_standard#Fix', \ 'function': 'ale#fixers#prettier_standard#Fix',
\ 'suggested_filetypes': ['javascript'], \ 'suggested_filetypes': ['javascript'],
\ 'description': 'Apply prettier-standard to a file.', \ 'description': 'Apply prettier-standard to a file.',
\ 'aliases': ['prettier-standard'],
\ }, \ },
\ 'eslint': { \ 'eslint': {
\ 'function': 'ale#fixers#eslint#Fix', \ 'function': 'ale#fixers#eslint#Fix',
@ -51,6 +52,7 @@ let s:default_registry = {
\ 'function': 'ale#fixers#prettier_eslint#Fix', \ 'function': 'ale#fixers#prettier_eslint#Fix',
\ 'suggested_filetypes': ['javascript'], \ 'suggested_filetypes': ['javascript'],
\ 'description': 'Apply prettier-eslint to a file.', \ 'description': 'Apply prettier-eslint to a file.',
\ 'aliases': ['prettier-eslint'],
\ }, \ },
\ 'puppetlint': { \ 'puppetlint': {
\ 'function': 'ale#fixers#puppetlint#Fix', \ 'function': 'ale#fixers#puppetlint#Fix',
@ -147,6 +149,14 @@ let s:default_registry = {
" Reset the function registry to the default entries. " Reset the function registry to the default entries.
function! ale#fix#registry#ResetToDefaults() abort function! ale#fix#registry#ResetToDefaults() abort
let s:entries = deepcopy(s:default_registry) 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 endfunction
" Set up entries now. " Set up entries now.
@ -155,10 +165,12 @@ call ale#fix#registry#ResetToDefaults()
" Remove everything from the registry, useful for tests. " Remove everything from the registry, useful for tests.
function! ale#fix#registry#Clear() abort function! ale#fix#registry#Clear() abort
let s:entries = {} let s:entries = {}
let s:aliases = {}
endfunction endfunction
" Add a function for fixing problems to the registry. " 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('') if type(a:name) != type('')
throw '''name'' must be a String' throw '''name'' must be a String'
endif endif
@ -181,16 +193,37 @@ function! ale#fix#registry#Add(name, func, filetypes, desc) abort
throw '''desc'' must be a String' throw '''desc'' must be a String'
endif 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] = { let s:entries[a:name] = {
\ 'function': a:func, \ 'function': a:func,
\ 'suggested_filetypes': a:filetypes, \ 'suggested_filetypes': a:filetypes,
\ 'description': a:desc, \ '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 endfunction
" Get a function from the registry by its short name. " Get a function from the registry by its short name.
function! ale#fix#registry#GetFunc(name) abort 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 endfunction
function! s:ShouldSuggestForType(suggested_filetypes, type_list) abort function! s:ShouldSuggestForType(suggested_filetypes, type_list) abort
@ -203,6 +236,25 @@ function! s:ShouldSuggestForType(suggested_filetypes, type_list) abort
return 0 return 0
endfunction 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. " Suggest functions to use from the registry.
function! ale#fix#registry#Suggest(filetype) abort function! ale#fix#registry#Suggest(filetype) abort
let l:type_list = split(a:filetype, '\.') 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) if s:ShouldSuggestForType(l:suggested_filetypes, l:type_list)
call add( call add(
\ l:filetype_fixer_list, \ l:filetype_fixer_list,
\ printf('%s - %s', string(l:key), s:entries[l:key].description), \ s:FormatEntry(l:key, s:entries[l:key]),
\) \)
endif endif
endfor endfor
@ -225,7 +277,7 @@ function! ale#fix#registry#Suggest(filetype) abort
if empty(s:entries[l:key].suggested_filetypes) if empty(s:entries[l:key].suggested_filetypes)
call add( call add(
\ l:generic_fixer_list, \ l:generic_fixer_list,
\ printf('%s - %s', string(l:key), s:entries[l:key].description), \ s:FormatEntry(l:key, s:entries[l:key]),
\) \)
endif endif
endfor endfor

View File

@ -1780,7 +1780,8 @@ ale#engine#ManageDirectory(buffer, directory) *ale#engine#ManageDirectory()*
files. 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` 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 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 The `name` can then be used for |g:ale_fixers| in place of the function
name, and suggested for fixing files. 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()* ale#linter#Define(filetype, linter) *ale#linter#Define()*

View File

@ -3,7 +3,7 @@ if exists('b:current_syntax')
endif endif
syn match aleFixerComment /^.*$/ syn match aleFixerComment /^.*$/
syn match aleFixerName /^'[^']*'/ syn match aleFixerName /\(^\|, \)'[^']*'/
syn match aleFixerHelp /^See :help ale-fix-configuration/ syn match aleFixerHelp /^See :help ale-fix-configuration/
hi def link aleFixerComment Comment hi def link aleFixerComment Comment

View File

@ -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')

View File

@ -80,7 +80,7 @@ Execute(ALEFixSuggest output should be correct for only filetype handlers):
Execute(ALEFixSuggest should suggest filetype and generic handlers): Execute(ALEFixSuggest should suggest filetype and generic handlers):
let &filetype = 'testft2.testft' 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('alpha', 'XYZ', ['testft'], 'Alpha things.')
call ale#fix#registry#Add('generic', 'XYZ', [], 'Generic 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:', \ 'Try the following fixers appropriate for the filetype:',
\ '', \ '',
\ '''alpha'' - Alpha things.', \ '''alpha'' - Alpha things.',
\ '''zed'' - Zedify things.', \ '''zed'', ''foobar'' - Zedify things.',
\ '', \ '',
\ 'Try the following generic fixers:', \ 'Try the following generic fixers:',
\ '', \ '',