#1095 Cache the sorting of patterns for g:ale_pattern_options

This commit is contained in:
w0rp 2017-11-11 14:26:54 +00:00
parent 8cd1ccff84
commit 555743a2ba
2 changed files with 31 additions and 5 deletions

View File

@ -1,6 +1,10 @@
" Author: w0rp <devw0rp@gmail.com>
" Description: Set options in files based on regex patterns.
" These variables are used to cache the sorting of patterns below.
let s:last_pattern_options = {}
let s:sorted_items = []
function! s:CmpPatterns(left_item, right_item) abort
if a:left_item[0] < a:right_item[0]
return -1
@ -18,13 +22,19 @@ function! ale#pattern_options#SetOptions(buffer) abort
return
endif
let l:filename = expand('#' . a:buffer . ':p')
" The items will only be sorted whenever the patterns change.
if g:ale_pattern_options != s:last_pattern_options
let s:last_pattern_options = deepcopy(g:ale_pattern_options)
" The patterns are sorted, so they are applied consistently.
for [l:pattern, l:options] in sort(
let s:sorted_items = sort(
\ items(g:ale_pattern_options),
\ function('s:CmpPatterns')
\)
endif
let l:filename = expand('#' . a:buffer . ':p')
for [l:pattern, l:options] in s:sorted_items
if match(l:filename, l:pattern) >= 0
for [l:key, l:value] in items(l:options)
call setbufvar(a:buffer, l:key, l:value)

View File

@ -74,3 +74,19 @@ Execute(Patterns should not be applied when the setting is disabled):
call ale#pattern_options#SetOptions(bufnr(''))
AssertEqual 0, b:some_option
" This test is important for making sure we update the sorted items.
Execute(Patterns should be applied after the Dictionary changes):
call ale#test#SetFilename('foobar.js')
let g:ale_pattern_options = {}
call ale#pattern_options#SetOptions(bufnr(''))
AssertEqual 0, b:some_option
let g:ale_pattern_options['foo'] = {'some_option': 666}
call ale#pattern_options#SetOptions(bufnr(''))
AssertEqual 666, b:some_option