@@ -23,20 +23,35 @@ function! s:ClangFormatHasAtLeastVersion(minimum_version) abort
23
23
return 0
24
24
endif
25
25
26
- let l: version_output =
27
- \ maktaba#syscall#Create ([l: executable , ' --version' ]).Call ().stdout
26
+ let l: syscall = maktaba#syscall#Create ([l: executable , ' --version' ])
27
+ " Call with throw_errors disabled because some versions of clang-format
28
+ " misbehave and return exit code 1 along with the successful version
29
+ " output (see https://github.com/google/vim-codefmt/issues/84).
30
+ let l: version_output = l: syscall .Call (0 ).stdout
28
31
let l: version_string = matchstr (l: version_output , ' \v\d+(.\d+)+' )
32
+ " If no version string was matched, cached version will be an empty list.
29
33
let s: clang_format_version = map (split (l: version_string , ' \.' ), ' v:val + 0' )
30
34
endif
31
- let l: length = min ([len (a: minimum_version ), len (s: clang_format_version )])
35
+ " Always fail check if version couldn't be fetched.
36
+ if empty (s: clang_format_version )
37
+ return 0
38
+ endif
39
+ " Compare each dotted version value in turn.
40
+ let l: length = max ([len (a: minimum_version ), len (s: clang_format_version )])
32
41
for i in range (l: length )
33
- if a: minimum_version [i ] < s: clang_format_version [i ]
42
+ " Consider missing version places as zero (e.g. 7 = 7.0 = 7.0.0).
43
+ let l: detected_value = get (s: clang_format_version , i , 0 )
44
+ let l: minimum_value = get (a: minimum_version , i , 0 )
45
+ " Any place value above or below than its minimum means entire version is
46
+ " above or below the minimum.
47
+ if l: detected_value > l: minimum_value
34
48
return 1
35
- elseif a: minimum_version [ i ] > s: clang_format_version [ i ]
49
+ elseif l: detected_value < l: minimum_value
36
50
return 0
37
51
endif
38
52
endfor
39
- return len (a: minimum_version ) <= len (s: clang_format_version )
53
+ " All version numbers were equal, so version was at least minimum.
54
+ return 1
40
55
endfunction
41
56
42
57
0 commit comments