]> git.madduck.net Git - etc/vim.git/blob - autoload/ale/semver.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 / semver.vim
1 let s:version_cache = {}
2
3 " Reset the version cache used for parsing the version.
4 function! ale#semver#ResetVersionCache() abort
5     let s:version_cache = {}
6 endfunction
7
8 function! ale#semver#ParseVersion(version_lines) abort
9     for l:line in a:version_lines
10         let l:match = matchlist(l:line, '\v(\d+)\.(\d+)(\.(\d+))?')
11
12         if !empty(l:match)
13             return [l:match[1] + 0, l:match[2] + 0, l:match[4] + 0]
14         endif
15     endfor
16
17     return []
18 endfunction
19
20 " Given an executable name and some lines of output, which can be empty,
21 " parse the version from the lines of output, or return the cached version
22 " triple [major, minor, patch]
23 "
24 " If the version cannot be found, an empty List will be returned instead.
25 function! s:GetVersion(executable, version_lines) abort
26     let l:version = get(s:version_cache, a:executable, [])
27     let l:parsed_version = ale#semver#ParseVersion(a:version_lines)
28
29     if !empty(l:parsed_version)
30         let l:version = l:parsed_version
31         let s:version_cache[a:executable] = l:version
32     endif
33
34     return l:version
35 endfunction
36
37 function! ale#semver#RunWithVersionCheck(buffer, executable, command, Callback) abort
38     if empty(a:executable)
39         return ''
40     endif
41
42     let l:cache = s:version_cache
43
44     if has_key(s:version_cache, a:executable)
45         return a:Callback(a:buffer, s:version_cache[a:executable])
46     endif
47
48     return ale#command#Run(
49     \   a:buffer,
50     \   a:command,
51     \   {_, output -> a:Callback(a:buffer, s:GetVersion(a:executable, output))},
52     \   {'output_stream': 'both', 'executable': a:executable}
53     \)
54 endfunction
55
56 " Given two triples of integers [major, minor, patch], compare the triples
57 " and return 1 if the LHS is greater than or equal to the RHS.
58 "
59 " Pairs of [major, minor] can also be used for either argument.
60 "
61 " 0 will be returned if the LHS is an empty List.
62 function! ale#semver#GTE(lhs, rhs) abort
63     if empty(a:lhs)
64         return 0
65     endif
66
67     if a:lhs[0] > a:rhs[0]
68         return 1
69     elseif a:lhs[0] == a:rhs[0]
70         if a:lhs[1] > a:rhs[1]
71             return 1
72         elseif a:lhs[1] == a:rhs[1]
73             return get(a:lhs, 2) >= get(a:rhs, 2)
74         endif
75     endif
76
77     return 0
78 endfunction