mirror of https://github.com/dense-analysis/ale
#371 Allow buffer variables to be set based on patterns
This commit is contained in:
parent
7fe1119cf1
commit
c77cf0e518
|
@ -0,0 +1,18 @@
|
|||
" Author: w0rp <devw0rp@gmail.com>
|
||||
" Description: Set options in files based on regex patterns.
|
||||
|
||||
function! ale#pattern_options#SetOptions() abort
|
||||
let l:filename = expand('%:p')
|
||||
let l:options = {}
|
||||
|
||||
for l:pattern in keys(g:ale_pattern_options)
|
||||
if match(l:filename, l:pattern) >= 0
|
||||
let l:options = g:ale_pattern_options[l:pattern]
|
||||
break
|
||||
endif
|
||||
endfor
|
||||
|
||||
for l:key in keys(l:options)
|
||||
let b:[l:key] = l:options[l:key]
|
||||
endfor
|
||||
endfunction
|
35
doc/ale.txt
35
doc/ale.txt
|
@ -517,6 +517,41 @@ g:ale_open_list *g:ale_open_list*
|
|||
to `1`, in which case the window will be kept open until closed manually.
|
||||
|
||||
|
||||
g:ale_pattern_options *g:ale_pattern_options*
|
||||
|
||||
Type: |Dictionary|
|
||||
Default: `{}`
|
||||
|
||||
This option maps regular expression patterns to |Dictionary| values for
|
||||
buffer variables. This option can be set to automatically configure
|
||||
different settings for different files. For example: >
|
||||
|
||||
let g:ale_pattern_options = {
|
||||
\ '\.foo\.js$': {
|
||||
\ 'ale_linters: {'javascript': ['eslint']},
|
||||
\ },
|
||||
\}
|
||||
<
|
||||
The above example will match any filename ending in `.foo.js`, and use
|
||||
only `eslint` for checking those files by setting `b:ale_linters`.
|
||||
|
||||
Filenames are matched with |match()|, and patterns depend on the |magic|
|
||||
setting, unless prefixed with the special escape sequences like `'\v'`, etc.
|
||||
|
||||
The patterns can match any part of a filename. The absolute path of the
|
||||
filename will be used for matching, taken from `expand('%:p')`.
|
||||
|
||||
|
||||
g:ale_pattern_options_enabled *g:ale_pattern_options_enabled*
|
||||
|
||||
Type: |Number|
|
||||
Default: `!empty(g:ale_pattern_options)`
|
||||
|
||||
This option can be used for turning the behaviour of setting
|
||||
|g:ale_pattern_options| on or off. By default, setting a single key
|
||||
for |g:ale_pattern_options| will turn this option on.
|
||||
|
||||
|
||||
g:ale_set_highlights *g:ale_set_highlights*
|
||||
|
||||
Type: |Number|
|
||||
|
|
|
@ -161,10 +161,23 @@ let g:ale_history_enabled = get(g:, 'ale_history_enabled', 1)
|
|||
" A flag for storing the full output of commands in the history.
|
||||
let g:ale_history_log_output = get(g:, 'ale_history_log_output', 0)
|
||||
|
||||
" A dictionary mapping regular expression patterns to arbitrary buffer
|
||||
" variables to be set. Useful for configuration ALE based on filename
|
||||
" patterns.
|
||||
call ale#Set('pattern_options', {})
|
||||
call ale#Set('pattern_options_enabled', !empty(g:ale_pattern_options))
|
||||
|
||||
function! ALEInitAuGroups() abort
|
||||
" This value used to be a Boolean as a Number, and is now a String.
|
||||
let l:text_changed = '' . g:ale_lint_on_text_changed
|
||||
|
||||
augroup ALEPatternOptionsGroup
|
||||
autocmd!
|
||||
if g:ale_enabled && g:ale_pattern_options_enabled
|
||||
autocmd BufEnter,BufRead * call ale#pattern_options#SetOptions()
|
||||
endif
|
||||
augroup END
|
||||
|
||||
augroup ALERunOnTextChangedGroup
|
||||
autocmd!
|
||||
if g:ale_enabled
|
||||
|
@ -226,6 +239,7 @@ function! ALEInitAuGroups() abort
|
|||
augroup END
|
||||
|
||||
if !g:ale_enabled
|
||||
augroup! ALEPatternOptionsGroup
|
||||
augroup! ALERunOnTextChangedGroup
|
||||
augroup! ALERunOnEnterGroup
|
||||
augroup! ALERunOnSaveGroup
|
||||
|
@ -238,6 +252,11 @@ function! s:ALEToggle() abort
|
|||
let g:ale_enabled = !get(g:, 'ale_enabled')
|
||||
|
||||
if g:ale_enabled
|
||||
" Set pattern options again, if enabled.
|
||||
if g:ale_pattern_options_enabled
|
||||
call ale#pattern_options#SetOptions()
|
||||
endif
|
||||
|
||||
" Lint immediately, including running linters against the file.
|
||||
call ale#Queue(0, 'lint_file')
|
||||
else
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
Before:
|
||||
Save g:ale_pattern_options, g:ale_pattern_options_enabled
|
||||
|
||||
After:
|
||||
Restore
|
||||
|
||||
unlet! b:ale_enabled
|
||||
unlet! b:some_option
|
||||
|
||||
Execute(Buffer variables should be set when filename patterns match):
|
||||
let g:ale_pattern_options = {'baz.*\.js': {
|
||||
\ 'ale_enabled': 1,
|
||||
\ 'some_option': 347,
|
||||
\}}
|
||||
|
||||
silent! file foobar.js
|
||||
|
||||
call ale#pattern_options#SetOptions()
|
||||
|
||||
Assert !exists('b:ale_enabled')
|
||||
Assert !exists('b:some_option')
|
||||
|
||||
silent! file bazboz.js
|
||||
|
||||
call ale#pattern_options#SetOptions()
|
||||
|
||||
AssertEqual 1, b:ale_enabled
|
||||
AssertEqual 347, b:some_option
|
Loading…
Reference in New Issue