From 8c1d6eda81d408de767916c00d20139ddf6fc9c6 Mon Sep 17 00:00:00 2001 From: w0rp Date: Sat, 11 Nov 2017 13:44:05 +0000 Subject: #1095 Apply all patterns for g:ale_pattern_options, instead of just the first match --- autoload/ale/pattern_options.vim | 40 ++++++++++++++++++++++++++-------------- autoload/ale/toggle.vim | 5 +---- 2 files changed, 27 insertions(+), 18 deletions(-) (limited to 'autoload') diff --git a/autoload/ale/pattern_options.vim b/autoload/ale/pattern_options.vim index a603c980..a55a27f6 100644 --- a/autoload/ale/pattern_options.vim +++ b/autoload/ale/pattern_options.vim @@ -1,22 +1,34 @@ " Author: w0rp " Description: Set options in files based on regex patterns. -function! ale#pattern_options#SetOptions() abort - let l:filename = expand('%:p') " no-custom-checks - let l:options = {} +function! s:CmpPatterns(left_item, right_item) abort + if a:left_item[0] < a:right_item[0] + return -1 + endif - 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 + if a:left_item[0] > a:right_item[0] + return 1 + endif + + return 0 +endfunction + +function! ale#pattern_options#SetOptions(buffer) abort + if !g:ale_pattern_options_enabled || empty(g:ale_pattern_options) + return + endif - for l:key in keys(l:options) - if l:key[:0] is# '&' - call setbufvar(bufnr(''), l:key, l:options[l:key]) - else - let b:[l:key] = l:options[l:key] + let l:filename = expand('#' . a:buffer . ':p') + + " The patterns are sorted, so they are applied consistently. + for [l:pattern, l:options] in sort( + \ items(g:ale_pattern_options), + \ function('s:CmpPatterns') + \) + if match(l:filename, l:pattern) >= 0 + for [l:key, l:value] in items(l:options) + call setbufvar(a:buffer, l:key, l:value) + endfor endif endfor endfunction diff --git a/autoload/ale/toggle.vim b/autoload/ale/toggle.vim index 6809edd1..aa6d113b 100644 --- a/autoload/ale/toggle.vim +++ b/autoload/ale/toggle.vim @@ -4,9 +4,7 @@ function! ale#toggle#InitAuGroups() abort augroup ALEPatternOptionsGroup autocmd! - if g:ale_enabled && g:ale_pattern_options_enabled - autocmd BufEnter,BufRead * call ale#pattern_options#SetOptions() - endif + autocmd BufEnter,BufRead * call ale#pattern_options#SetOptions(str2nr(expand(''))) augroup END augroup ALERunOnTextChangedGroup @@ -71,7 +69,6 @@ function! ale#toggle#InitAuGroups() abort augroup END if !g:ale_enabled - augroup! ALEPatternOptionsGroup augroup! ALERunOnTextChangedGroup augroup! ALERunOnEnterGroup augroup! ALERunOnInsertLeave -- cgit v1.2.3