diff options
author | w0rp <devw0rp@gmail.com> | 2017-05-05 23:03:19 +0100 |
---|---|---|
committer | w0rp <devw0rp@gmail.com> | 2017-05-05 23:03:19 +0100 |
commit | ab9afaa2bf446ed9db32f5ab43081e7f28cc1358 (patch) | |
tree | 56077678593f81b2b981fcaab81a373358f6fc60 | |
parent | 6b15c7c9fd56edf1d63bc92c81c86afb7f6d3b6e (diff) | |
download | ale-ab9afaa2bf446ed9db32f5ab43081e7f28cc1358.zip |
Add a has() wrapper we can override for tests, and add a function for generating paths up to the root directory to search through
-rw-r--r-- | autoload/ale.vim | 8 | ||||
-rw-r--r-- | autoload/ale/path.vim | 24 | ||||
-rw-r--r-- | autoload/ale/python.vim | 2 | ||||
-rw-r--r-- | test/test_path_upwards.vader | 46 |
4 files changed, 80 insertions, 0 deletions
diff --git a/autoload/ale.vim b/autoload/ale.vim index b9cd648f..c8fbfdfc 100644 --- a/autoload/ale.vim +++ b/autoload/ale.vim @@ -105,6 +105,14 @@ function! ale#ResetLintFileMarkers() abort let s:should_lint_file_for_buffer = {} endfunction +let g:ale_has_override = get(g:, 'ale_has_override', {}) + +" Call has(), but check a global Dictionary so we can force flags on or off +" for testing purposes. +function! ale#Has(feature) abort + return get(g:ale_has_override, a:feature, has(a:feature)) +endfunction + " Given a buffer number and a variable name, look for that variable in the " buffer scope, then in global scope. If the name does not exist in the global " scope, an exception will be thrown. diff --git a/autoload/ale/path.vim b/autoload/ale/path.vim index 28105b1e..0ea13351 100644 --- a/autoload/ale/path.vim +++ b/autoload/ale/path.vim @@ -92,3 +92,27 @@ function! ale#path#IsBufferPath(buffer, filename) abort return resolve(l:buffer_filename) ==# l:resolved_filename endfunction + +" Given a path, return every component of the path, moving upwards. +function! ale#path#Upwards(path) abort + let l:pattern = ale#Has('win32') ? '\v/+|\\+' : '\v/+' + let l:sep = ale#Has('win32') ? '\' : '/' + let l:parts = split(simplify(a:path), l:pattern) + let l:path_list = [] + + while !empty(l:parts) + call add(l:path_list, join(l:parts, l:sep)) + let l:parts = l:parts[:-2] + endwhile + + if ale#Has('win32') && a:path =~# '^[a-zA-z]:\' + " Add \ to C: for C:\, etc. + let l:path_list[-1] .= '\' + elseif a:path[0] ==# '/' + " If the path starts with /, even on Windows, add / and / to all paths. + call add(l:path_list, '') + call map(l:path_list, '''/'' . v:val') + endif + + return l:path_list +endfunction diff --git a/autoload/ale/python.vim b/autoload/ale/python.vim new file mode 100644 index 00000000..34b35ddb --- /dev/null +++ b/autoload/ale/python.vim @@ -0,0 +1,2 @@ +" Author: w0rp <devw0rp@gmail.com> +" Description: Functions for integrating with Python linters. diff --git a/test/test_path_upwards.vader b/test/test_path_upwards.vader new file mode 100644 index 00000000..2f7b2c0f --- /dev/null +++ b/test/test_path_upwards.vader @@ -0,0 +1,46 @@ +After: + let g:ale_has_override = {} + +Execute(ale#path#Upwards should return the correct path components for Unix): + " Absolute paths should include / on the end. + AssertEqual + \ ['/foo/bar/baz', '/foo/bar', '/foo', '/'], + \ ale#path#Upwards('/foo/bar/baz') + AssertEqual + \ ['/foo/bar/baz', '/foo/bar', '/foo', '/'], + \ ale#path#Upwards('/foo/bar/baz///') + " Relative paths do not. + AssertEqual + \ ['foo/bar/baz', 'foo/bar', 'foo'], + \ ale#path#Upwards('foo/bar/baz') + AssertEqual + \ ['foo2/bar', 'foo2'], + \ ale#path#Upwards('foo//..////foo2////bar') + " Expect an empty List for empty strings. + AssertEqual [], ale#path#Upwards('') + +Execute(ale#path#Upwards should return the correct path components for Windows): + let g:ale_has_override = {'win32': 1} + + AssertEqual + \ ['C:\foo\bar\baz', 'C:\foo\bar', 'C:\foo', 'C:\'], + \ ale#path#Upwards('C:\foo\bar\baz') + AssertEqual + \ ['C:\foo\bar\baz', 'C:\foo\bar', 'C:\foo', 'C:\'], + \ ale#path#Upwards('C:\foo\bar\baz\\\') + AssertEqual + \ ['/foo\bar\baz', '/foo\bar', '/foo', '/'], + \ ale#path#Upwards('/foo/bar/baz') + AssertEqual + \ ['foo\bar\baz', 'foo\bar', 'foo'], + \ ale#path#Upwards('foo/bar/baz') + AssertEqual + \ ['foo\bar\baz', 'foo\bar', 'foo'], + \ ale#path#Upwards('foo\bar\baz') + " simplify() is used internally, and should sort out \ paths when actually + " running Windows, which we can't test here. + AssertEqual + \ ['foo2\bar', 'foo2'], + \ ale#path#Upwards('foo//..///foo2////bar') + " Expect an empty List for empty strings. + AssertEqual [], ale#path#Upwards('') |