summaryrefslogtreecommitdiff
path: root/autoload/ale/semver.vim
diff options
context:
space:
mode:
Diffstat (limited to 'autoload/ale/semver.vim')
-rw-r--r--autoload/ale/semver.vim49
1 files changed, 35 insertions, 14 deletions
diff --git a/autoload/ale/semver.vim b/autoload/ale/semver.vim
index 5f1b46fc..e3eb49c0 100644
--- a/autoload/ale/semver.vim
+++ b/autoload/ale/semver.vim
@@ -5,31 +5,52 @@ function! ale#semver#ResetVersionCache() abort
let s:version_cache = {}
endfunction
+function! ale#semver#ParseVersion(version_lines) abort
+ for l:line in a:version_lines
+ let l:match = matchlist(l:line, '\v(\d+)\.(\d+)(\.(\d+))?')
+
+ if !empty(l:match)
+ return [l:match[1] + 0, l:match[2] + 0, l:match[4] + 0]
+ endif
+ endfor
+
+ return []
+endfunction
+
" Given an executable name and some lines of output, which can be empty,
" parse the version from the lines of output, or return the cached version
" triple [major, minor, patch]
"
" If the version cannot be found, an empty List will be returned instead.
-function! ale#semver#GetVersion(executable, version_lines) abort
+function! s:GetVersion(executable, version_lines) abort
let l:version = get(s:version_cache, a:executable, [])
+ let l:parsed_version = ale#semver#ParseVersion(a:version_lines)
- for l:line in a:version_lines
- let l:match = matchlist(l:line, '\v(\d+)\.(\d+)(\.(\d+))?')
-
- if !empty(l:match)
- let l:version = [l:match[1] + 0, l:match[2] + 0, l:match[4] + 0]
- let s:version_cache[a:executable] = l:version
-
- break
- endif
- endfor
+ if !empty(l:parsed_version)
+ let l:version = l:parsed_version
+ let s:version_cache[a:executable] = l:version
+ endif
return l:version
endfunction
-" Return 1 if the semver version has been cached for a given executable.
-function! ale#semver#HasVersion(executable) abort
- return has_key(s:version_cache, a:executable)
+function! ale#semver#RunWithVersionCheck(buffer, executable, command, Callback) abort
+ if empty(a:executable)
+ return ''
+ endif
+
+ let l:cache = s:version_cache
+
+ if has_key(s:version_cache, a:executable)
+ return a:Callback(a:buffer, s:version_cache[a:executable])
+ endif
+
+ return ale#command#Run(
+ \ a:buffer,
+ \ a:command,
+ \ {_, output -> a:Callback(a:buffer, s:GetVersion(a:executable, output))},
+ \ {'output_stream': 'both', 'executable': a:executable}
+ \)
endfunction
" Given two triples of integers [major, minor, patch], compare the triples