]> git.madduck.net Git - etc/vim.git/blob - autoload/ale/pattern_options.vim

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Squashed '.vim/bundle/ale/' content from commit 22185c4c
[etc/vim.git] / autoload / ale / pattern_options.vim
1 " Author: w0rp <devw0rp@gmail.com>
2 " Description: Set options in files based on regex patterns.
3
4 " These variables are used to cache the sorting of patterns below.
5 let s:last_pattern_options = {}
6 let s:sorted_items = []
7
8 function! s:CmpPatterns(left_item, right_item) abort
9     if a:left_item[0] < a:right_item[0]
10         return -1
11     endif
12
13     if a:left_item[0] > a:right_item[0]
14         return 1
15     endif
16
17     return 0
18 endfunction
19
20 function! ale#pattern_options#SetOptions(buffer) abort
21     let l:pattern_options = get(g:, 'ale_pattern_options', {})
22
23     if empty(l:pattern_options)
24         " Stop if no options are set.
25         return
26     endif
27
28     " The items will only be sorted whenever the patterns change.
29     if l:pattern_options != s:last_pattern_options
30         let s:last_pattern_options = deepcopy(l:pattern_options)
31         " The patterns are sorted, so they are applied consistently.
32         let s:sorted_items = sort(
33         \   items(l:pattern_options),
34         \   function('s:CmpPatterns')
35         \)
36     endif
37
38     let l:filename = expand('#' . a:buffer . ':p')
39
40     for [l:pattern, l:options] in s:sorted_items
41         if match(l:filename, l:pattern) >= 0
42             for [l:key, l:value] in items(l:options)
43                 call setbufvar(a:buffer, l:key, l:value)
44             endfor
45         endif
46     endfor
47 endfunction