]> git.madduck.net Git - etc/vim.git/blobdiff - .vim/bundle/ale/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:

Merge commit '76265755a1add77121c8f9dabb3e9bb70fe9a972' as '.vim/bundle/ale'
[etc/vim.git] / .vim / bundle / ale / autoload / ale / pattern_options.vim
diff --git a/.vim/bundle/ale/autoload/ale/pattern_options.vim b/.vim/bundle/ale/autoload/ale/pattern_options.vim
new file mode 100644 (file)
index 0000000..14e2142
--- /dev/null
@@ -0,0 +1,47 @@
+" 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
+    endif
+
+    if a:left_item[0] > a:right_item[0]
+        return 1
+    endif
+
+    return 0
+endfunction
+
+function! ale#pattern_options#SetOptions(buffer) abort
+    let l:pattern_options = get(g:, 'ale_pattern_options', {})
+
+    if empty(l:pattern_options)
+        " Stop if no options are set.
+        return
+    endif
+
+    " The items will only be sorted whenever the patterns change.
+    if l:pattern_options != s:last_pattern_options
+        let s:last_pattern_options = deepcopy(l:pattern_options)
+        " The patterns are sorted, so they are applied consistently.
+        let s:sorted_items = sort(
+        \   items(l: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)
+            endfor
+        endif
+    endfor
+endfunction