diff options
author | w0rp <devw0rp@gmail.com> | 2017-04-17 23:29:02 +0100 |
---|---|---|
committer | w0rp <devw0rp@gmail.com> | 2017-04-17 23:29:02 +0100 |
commit | e237add9fdec64c80ed57f383e2b73464fb4b43d (patch) | |
tree | c7a958526e3048c3457359de435fd8ee83ab07b6 | |
parent | 6c762237cea0dd29fc95d3cbbb9787213f581ef3 (diff) | |
download | ale-e237add9fdec64c80ed57f383e2b73464fb4b43d.zip |
Move path functions to their own file
26 files changed, 90 insertions, 89 deletions
diff --git a/ale_linters/coffee/coffee.vim b/ale_linters/coffee/coffee.vim index f263a163..f2539281 100644 --- a/ale_linters/coffee/coffee.vim +++ b/ale_linters/coffee/coffee.vim @@ -2,7 +2,7 @@ " Description: Coffee for checking coffee files function! ale_linters#coffee#coffee#GetExecutable(buffer) abort - return ale#util#ResolveLocalPath( + return ale#path#ResolveLocalPath( \ a:buffer, \ 'node_modules/.bin/coffee', \ 'coffee' diff --git a/ale_linters/coffee/coffeelint.vim b/ale_linters/coffee/coffeelint.vim index 614f45aa..bac86821 100644 --- a/ale_linters/coffee/coffeelint.vim +++ b/ale_linters/coffee/coffeelint.vim @@ -2,7 +2,7 @@ " Description: coffeelint linter for coffeescript files function! ale_linters#coffee#coffeelint#GetExecutable(buffer) abort - return ale#util#ResolveLocalPath( + return ale#path#ResolveLocalPath( \ a:buffer, \ 'node_modules/.bin/coffeelint', \ 'coffeelint' diff --git a/ale_linters/css/csslint.vim b/ale_linters/css/csslint.vim index 6b0956ee..641e9c3e 100644 --- a/ale_linters/css/csslint.vim +++ b/ale_linters/css/csslint.vim @@ -2,7 +2,7 @@ " Description: This file adds support for checking CSS code with csslint. function! ale_linters#css#csslint#GetCommand(buffer) abort - let l:csslintrc = ale#util#FindNearestFile(a:buffer, '.csslintrc') + let l:csslintrc = ale#path#FindNearestFile(a:buffer, '.csslintrc') let l:config_option = !empty(l:csslintrc) \ ? '--config=' . fnameescape(l:csslintrc) \ : '' diff --git a/ale_linters/css/stylelint.vim b/ale_linters/css/stylelint.vim index ffda0498..8c1c3be1 100644 --- a/ale_linters/css/stylelint.vim +++ b/ale_linters/css/stylelint.vim @@ -14,7 +14,7 @@ function! ale_linters#css#stylelint#GetExecutable(buffer) abort return ale#Var(a:buffer, 'css_stylelint_executable') endif - return ale#util#ResolveLocalPath( + return ale#path#ResolveLocalPath( \ a:buffer, \ 'node_modules/.bin/stylelint', \ ale#Var(a:buffer, 'css_stylelint_executable') diff --git a/ale_linters/d/dmd.vim b/ale_linters/d/dmd.vim index 45565d0c..c6f8f209 100644 --- a/ale_linters/d/dmd.vim +++ b/ale_linters/d/dmd.vim @@ -5,7 +5,7 @@ function! s:FindDUBConfig(buffer) abort " Find a DUB configuration file in ancestor paths. " The most DUB-specific names will be tried first. for l:possible_filename in ['dub.sdl', 'dub.json', 'package.json'] - let l:dub_file = ale#util#FindNearestFile(a:buffer, l:possible_filename) + let l:dub_file = ale#path#FindNearestFile(a:buffer, l:possible_filename) if !empty(l:dub_file) return l:dub_file diff --git a/ale_linters/elm/make.vim b/ale_linters/elm/make.vim index a8ae4b1a..32f824e0 100644 --- a/ale_linters/elm/make.vim +++ b/ale_linters/elm/make.vim @@ -38,7 +38,7 @@ endfunction " Return the command to execute the linter in the projects directory. " If it doesn't, then this will fail when imports are needed. function! ale_linters#elm#make#GetCommand(buffer) abort - let l:elm_package = ale#util#FindNearestFile(a:buffer, 'elm-package.json') + let l:elm_package = ale#path#FindNearestFile(a:buffer, 'elm-package.json') if empty(l:elm_package) let l:dir_set_cmd = '' else diff --git a/ale_linters/handlebars/embertemplatelint.vim b/ale_linters/handlebars/embertemplatelint.vim index bfec27aa..91dda70a 100644 --- a/ale_linters/handlebars/embertemplatelint.vim +++ b/ale_linters/handlebars/embertemplatelint.vim @@ -12,7 +12,7 @@ function! ale_linters#handlebars#embertemplatelint#GetExecutable(buffer) abort return ale#Var(a:buffer, 'handlebars_embertemplatelint_executable') endif - return ale#util#ResolveLocalPath( + return ale#path#ResolveLocalPath( \ a:buffer, \ 'node_modules/.bin/ember-template-lint', \ ale#Var(a:buffer, 'handlebars_embertemplatelint_executable') diff --git a/ale_linters/html/htmlhint.vim b/ale_linters/html/htmlhint.vim index b0172f68..2edae372 100644 --- a/ale_linters/html/htmlhint.vim +++ b/ale_linters/html/htmlhint.vim @@ -11,7 +11,7 @@ function! ale_linters#html#htmlhint#GetExecutable(buffer) abort return ale#Var(a:buffer, 'html_htmlhint_executable') endif - return ale#util#ResolveLocalPath( + return ale#path#ResolveLocalPath( \ a:buffer, \ 'node_modules/.bin/htmlhint', \ ale#Var(a:buffer, 'html_htmlhint_executable') diff --git a/ale_linters/javascript/eslint.vim b/ale_linters/javascript/eslint.vim index 2a65e301..459e4e5d 100644 --- a/ale_linters/javascript/eslint.vim +++ b/ale_linters/javascript/eslint.vim @@ -16,7 +16,7 @@ function! ale_linters#javascript#eslint#GetExecutable(buffer) abort endif " Look for the kinds of paths that create-react-app generates first. - let l:executable = ale#util#ResolveLocalPath( + let l:executable = ale#path#ResolveLocalPath( \ a:buffer, \ 'node_modules/eslint/bin/eslint.js', \ '' @@ -26,7 +26,7 @@ function! ale_linters#javascript#eslint#GetExecutable(buffer) abort return l:executable endif - return ale#util#ResolveLocalPath( + return ale#path#ResolveLocalPath( \ a:buffer, \ 'node_modules/.bin/eslint', \ ale#Var(a:buffer, 'javascript_eslint_executable') diff --git a/ale_linters/javascript/flow.vim b/ale_linters/javascript/flow.vim index 90d6f420..24c7bca5 100644 --- a/ale_linters/javascript/flow.vim +++ b/ale_linters/javascript/flow.vim @@ -12,7 +12,7 @@ function! ale_linters#javascript#flow#GetExecutable(buffer) abort return ale#Var(a:buffer, 'javascript_flow_executable') endif - return ale#util#ResolveLocalPath( + return ale#path#ResolveLocalPath( \ a:buffer, \ 'node_modules/.bin/flow', \ ale#Var(a:buffer, 'javascript_flow_executable') @@ -20,7 +20,7 @@ function! ale_linters#javascript#flow#GetExecutable(buffer) abort endfunction function! ale_linters#javascript#flow#GetCommand(buffer) abort - let l:flow_config = ale#util#FindNearestFile(a:buffer, '.flowconfig') + let l:flow_config = ale#path#FindNearestFile(a:buffer, '.flowconfig') if empty(l:flow_config) " Don't run Flow if we can't find a .flowconfig file. diff --git a/ale_linters/javascript/jshint.vim b/ale_linters/javascript/jshint.vim index 506b26da..6835eeb0 100644 --- a/ale_linters/javascript/jshint.vim +++ b/ale_linters/javascript/jshint.vim @@ -12,7 +12,7 @@ function! ale_linters#javascript#jshint#GetExecutable(buffer) abort return ale#Var(a:buffer, 'javascript_jshint_executable') endif - return ale#util#ResolveLocalPath( + return ale#path#ResolveLocalPath( \ a:buffer, \ 'node_modules/.bin/jshint', \ ale#Var(a:buffer, 'javascript_jshint_executable') @@ -21,7 +21,7 @@ endfunction function! ale_linters#javascript#jshint#GetCommand(buffer) abort " Search for a local JShint config locaation, and default to a global one. - let l:jshint_config = ale#util#ResolveLocalPath( + let l:jshint_config = ale#path#ResolveLocalPath( \ a:buffer, \ '.jshintrc', \ get(g:, 'ale_jshint_config_loc', '') diff --git a/ale_linters/javascript/standard.vim b/ale_linters/javascript/standard.vim index c43b83c5..8809b07e 100644 --- a/ale_linters/javascript/standard.vim +++ b/ale_linters/javascript/standard.vim @@ -15,7 +15,7 @@ function! ale_linters#javascript#standard#GetExecutable(buffer) abort return ale#Var(a:buffer, 'javascript_standard_executable') endif - return ale#util#ResolveLocalPath( + return ale#path#ResolveLocalPath( \ a:buffer, \ 'node_modules/.bin/standard', \ ale#Var(a:buffer, 'javascript_standard_executable') diff --git a/ale_linters/javascript/xo.vim b/ale_linters/javascript/xo.vim index e27f6f33..dc71f0d3 100644 --- a/ale_linters/javascript/xo.vim +++ b/ale_linters/javascript/xo.vim @@ -15,7 +15,7 @@ function! ale_linters#javascript#xo#GetExecutable(buffer) abort return g:ale_javascript_xo_executable endif - return ale#util#ResolveLocalPath( + return ale#path#ResolveLocalPath( \ a:buffer, \ 'node_modules/.bin/xo', \ g:ale_javascript_xo_executable diff --git a/ale_linters/python/mypy.vim b/ale_linters/python/mypy.vim index d0a891ea..257a1d39 100644 --- a/ale_linters/python/mypy.vim +++ b/ale_linters/python/mypy.vim @@ -4,7 +4,7 @@ let g:ale_python_mypy_options = get(g:, 'ale_python_mypy_options', '') function! ale_linters#python#mypy#GetCommand(buffer) abort - let l:automatic_stubs_dir = ale#util#FindNearestDirectory(a:buffer, 'stubs') + let l:automatic_stubs_dir = ale#path#FindNearestDirectory(a:buffer, 'stubs') " TODO: Add Windows support let l:automatic_stubs_command = (has('unix') && !empty(l:automatic_stubs_dir)) \ ? 'MYPYPATH=' . l:automatic_stubs_dir . ' ' diff --git a/ale_linters/rust/cargo.vim b/ale_linters/rust/cargo.vim index 8dbc0bd0..32c09a58 100644 --- a/ale_linters/rust/cargo.vim +++ b/ale_linters/rust/cargo.vim @@ -4,7 +4,7 @@ let g:ale_rust_cargo_use_check = get(g:, 'ale_rust_cargo_use_check', 0) function! ale_linters#rust#cargo#GetCargoExecutable(bufnr) abort - if ale#util#FindNearestFile(a:bufnr, 'Cargo.toml') !=# '' + if ale#path#FindNearestFile(a:bufnr, 'Cargo.toml') !=# '' return 'cargo' else " if there is no Cargo.toml file, we don't use cargo even if it exists, diff --git a/ale_linters/rust/rustc.vim b/ale_linters/rust/rustc.vim index 1d080b98..73c99cd9 100644 --- a/ale_linters/rust/rustc.vim +++ b/ale_linters/rust/rustc.vim @@ -5,7 +5,7 @@ function! ale_linters#rust#rustc#RustcCommand(buffer_number) abort " Try to guess the library search path. If the project is managed by cargo, " it's usually <project root>/target/debug/deps/ or " <project root>/target/release/deps/ - let l:cargo_file = ale#util#FindNearestFile(a:buffer_number, 'Cargo.toml') + let l:cargo_file = ale#path#FindNearestFile(a:buffer_number, 'Cargo.toml') if l:cargo_file !=# '' let l:project_root = fnamemodify(l:cargo_file, ':h') diff --git a/ale_linters/sass/stylelint.vim b/ale_linters/sass/stylelint.vim index a481f280..a148b879 100644 --- a/ale_linters/sass/stylelint.vim +++ b/ale_linters/sass/stylelint.vim @@ -11,7 +11,7 @@ function! ale_linters#sass#stylelint#GetExecutable(buffer) abort return ale#Var(a:buffer, 'sass_stylelint_executable') endif - return ale#util#ResolveLocalPath( + return ale#path#ResolveLocalPath( \ a:buffer, \ 'node_modules/.bin/stylelint', \ ale#Var(a:buffer, 'sass_stylelint_executable') diff --git a/ale_linters/scss/stylelint.vim b/ale_linters/scss/stylelint.vim index 2e5d0270..edb4abb5 100644 --- a/ale_linters/scss/stylelint.vim +++ b/ale_linters/scss/stylelint.vim @@ -11,7 +11,7 @@ function! ale_linters#scss#stylelint#GetExecutable(buffer) abort return ale#Var(a:buffer, 'scss_stylelint_executable') endif - return ale#util#ResolveLocalPath( + return ale#path#ResolveLocalPath( \ a:buffer, \ 'node_modules/.bin/stylelint', \ ale#Var(a:buffer, 'scss_stylelint_executable') diff --git a/ale_linters/tex/chktex.vim b/ale_linters/tex/chktex.vim index da5cc668..8ecb3773 100644 --- a/ale_linters/tex/chktex.vim +++ b/ale_linters/tex/chktex.vim @@ -9,7 +9,7 @@ let g:ale_tex_chktex_options = function! ale_linters#tex#chktex#GetCommand(buffer) abort " Check for optional .chktexrc - let l:chktex_config = ale#util#FindNearestFile( + let l:chktex_config = ale#path#FindNearestFile( \ a:buffer, \ '.chktexrc') diff --git a/ale_linters/typescript/tslint.vim b/ale_linters/typescript/tslint.vim index a77011e5..081ad42b 100644 --- a/ale_linters/typescript/tslint.vim +++ b/ale_linters/typescript/tslint.vim @@ -8,7 +8,7 @@ let g:ale_typescript_tslint_config_path = \ get(g:, 'ale_typescript_tslint_config_path', '') function! ale_linters#typescript#tslint#GetExecutable(buffer) abort - return ale#util#ResolveLocalPath( + return ale#path#ResolveLocalPath( \ a:buffer, \ 'node_modules/.bin/tslint', \ ale#Var(a:buffer, 'typescript_tslint_executable') @@ -50,7 +50,7 @@ function! ale_linters#typescript#tslint#Handle(buffer, lines) abort endfunction function! ale_linters#typescript#tslint#BuildLintCommand(buffer) abort - let l:tslint_config_path = ale#util#ResolveLocalPath( + let l:tslint_config_path = ale#path#ResolveLocalPath( \ a:buffer, \ 'tslint.json', \ ale#Var(a:buffer, 'typescript_tslint_config_path') diff --git a/autoload/ale/path.vim b/autoload/ale/path.vim new file mode 100644 index 00000000..cbd4d886 --- /dev/null +++ b/autoload/ale/path.vim @@ -0,0 +1,57 @@ +" Author: w0rp <devw0rp@gmail.com> +" Description: Functions for working with paths in the filesystem. + +" Given a buffer and a filename, find the nearest file by searching upwards +" through the paths relative to the given buffer. +function! ale#path#FindNearestFile(buffer, filename) abort + let l:buffer_filename = fnamemodify(bufname(a:buffer), ':p') + + let l:relative_path = findfile(a:filename, l:buffer_filename . ';') + + if !empty(l:relative_path) + return fnamemodify(l:relative_path, ':p') + endif + + return '' +endfunction + +" Given a buffer and a directory name, find the nearest directory by searching upwards +" through the paths relative to the given buffer. +function! ale#path#FindNearestDirectory(buffer, directory_name) abort + let l:buffer_filename = fnamemodify(bufname(a:buffer), ':p') + + let l:relative_path = finddir(a:directory_name, l:buffer_filename . ';') + + if !empty(l:relative_path) + return fnamemodify(l:relative_path, ':p') + endif + + return '' +endfunction + +" Given a buffer, a string to search for, an a global fallback for when +" the search fails, look for a file in parent paths, and if that fails, +" use the global fallback path instead. +function! ale#path#ResolveLocalPath(buffer, search_string, global_fallback) abort + " Search for a locally installed file first. + let l:path = ale#path#FindNearestFile(a:buffer, a:search_string) + + " If the serach fails, try the global executable instead. + if empty(l:path) + let l:path = a:global_fallback + endif + + return l:path +endfunction + +" Output 'cd <directory> && ' +" This function can be used changing the directory for a linter command. +function! ale#path#CdString(directory) abort + return 'cd ' . fnameescape(a:directory) . ' && ' +endfunction + +" Output 'cd <buffer_filename_directory> && ' +" This function can be used changing the directory for a linter command. +function! ale#path#BufferCdString(buffer) abort + return ale#path#CdString(fnamemodify(bufname(a:buffer), ':p:h')) +endfunction diff --git a/autoload/ale/util.vim b/autoload/ale/util.vim index 84ffe22e..37085432 100644 --- a/autoload/ale/util.vim +++ b/autoload/ale/util.vim @@ -13,49 +13,6 @@ function! ale#util#GetLineCount(buffer) abort return len(getbufline(a:buffer, 1, '$')) endfunction -" Given a buffer and a filename, find the nearest file by searching upwards -" through the paths relative to the given buffer. -function! ale#util#FindNearestFile(buffer, filename) abort - let l:buffer_filename = fnamemodify(bufname(a:buffer), ':p') - - let l:relative_path = findfile(a:filename, l:buffer_filename . ';') - - if !empty(l:relative_path) - return fnamemodify(l:relative_path, ':p') - endif - - return '' -endfunction - -" Given a buffer and a directory name, find the nearest directory by searching upwards -" through the paths relative to the given buffer. -function! ale#util#FindNearestDirectory(buffer, directory_name) abort - let l:buffer_filename = fnamemodify(bufname(a:buffer), ':p') - - let l:relative_path = finddir(a:directory_name, l:buffer_filename . ';') - - if !empty(l:relative_path) - return fnamemodify(l:relative_path, ':p') - endif - - return '' -endfunction - -" Given a buffer, a string to search for, an a global fallback for when -" the search fails, look for a file in parent paths, and if that fails, -" use the global fallback path instead. -function! ale#util#ResolveLocalPath(buffer, search_string, global_fallback) abort - " Search for a locally installed file first. - let l:path = ale#util#FindNearestFile(a:buffer, a:search_string) - - " If the serach fails, try the global executable instead. - if empty(l:path) - let l:path = a:global_fallback - endif - - return l:path -endfunction - function! ale#util#GetFunction(string_or_ref) abort if type(a:string_or_ref) == type('') return function(a:string_or_ref) @@ -144,16 +101,3 @@ endfunction function! ale#util#ClockMilliseconds() abort return float2nr(reltimefloat(reltime()) * 1000) endfunction - - -" Output 'cd <directory> && ' -" This function can be used changing the directory for a linter command. -function! ale#util#CdString(directory) abort - return 'cd ' . fnameescape(a:directory) . ' && ' -endfunction - -" Output 'cd <buffer_filename_directory> && ' -" This function can be used changing the directory for a linter command. -function! ale#util#BufferCdString(buffer) abort - return ale#util#CdString(fnamemodify(bufname(a:buffer), ':p:h')) -endfunction diff --git a/test/test_find_nearest_directory.vader b/test/test_find_nearest_directory.vader index 450826ed..ecfd1385 100644 --- a/test/test_find_nearest_directory.vader +++ b/test/test_find_nearest_directory.vader @@ -5,11 +5,11 @@ Execute(Open a file some directory down): Then(We should be able to find the right directory): AssertEqual \ expand('%:p:h:h:h:h') . '/top/ale-special-directory-name-dont-use-this-please/', - \ ale#util#FindNearestDirectory(bufnr('%'), 'ale-special-directory-name-dont-use-this-please') + \ ale#path#FindNearestDirectory(bufnr('%'), 'ale-special-directory-name-dont-use-this-please') Execute(Do nothing): Then(We shouldn't find anything for files which don't match): AssertEqual \ '', - \ ale#util#FindNearestDirectory(bufnr('%'), 'ale-this-should-never-match-anything') + \ ale#path#FindNearestDirectory(bufnr('%'), 'ale-this-should-never-match-anything') diff --git a/test/test_nearest_file_search.vader b/test/test_nearest_file_search.vader index c59bfce2..c2499ad2 100644 --- a/test/test_nearest_file_search.vader +++ b/test/test_nearest_file_search.vader @@ -3,9 +3,9 @@ Execute(Open a file some directory down): :e! top/middle/bottom/dummy.txt Then(We should be able to find a configuration file further up): - AssertEqual expand('%:p:h:h:h:h') . '/top/example.ini', ale#util#FindNearestFile(bufnr('%'), 'example.ini') + AssertEqual expand('%:p:h:h:h:h') . '/top/example.ini', ale#path#FindNearestFile(bufnr('%'), 'example.ini') Execute(Do nothing): Then(We shouldn't find anything for files which don't match): - AssertEqual '', ale#util#FindNearestFile(bufnr('%'), 'cantfindthis') + AssertEqual '', ale#path#FindNearestFile(bufnr('%'), 'cantfindthis') diff --git a/test/test_resolve_local_path.vader b/test/test_resolve_local_path.vader index e269221e..703eec3d 100644 --- a/test/test_resolve_local_path.vader +++ b/test/test_resolve_local_path.vader @@ -5,11 +5,11 @@ Execute(Open a file some directory down): Then(We should be able to find the local version of a file): AssertEqual \ expand('%:p:h:h:h:h') . '/top/example.ini', - \ ale#util#ResolveLocalPath(bufnr('%'), 'example.ini', '/global/config.ini') + \ ale#path#ResolveLocalPath(bufnr('%'), 'example.ini', '/global/config.ini') Execute(Do nothing): Then(We shouldn't find anything for files which don't match): AssertEqual \ '/global/config.ini', - \ ale#util#ResolveLocalPath(bufnr('%'), 'missing.ini', '/global/config.ini') + \ ale#path#ResolveLocalPath(bufnr('%'), 'missing.ini', '/global/config.ini') diff --git a/test/util/test_cd_string_commands.vader b/test/util/test_cd_string_commands.vader index 0fdc0e30..36212e3b 100644 --- a/test/util/test_cd_string_commands.vader +++ b/test/util/test_cd_string_commands.vader @@ -2,8 +2,8 @@ Before: silent! cd /testplugin/test/util Execute(CdString should output the correct command string): - AssertEqual 'cd /foo\ bar/baz && ', ale#util#CdString('/foo bar/baz') + AssertEqual 'cd /foo\ bar/baz && ', ale#path#CdString('/foo bar/baz') Execute(BufferCdString should output the correct command string): - Assert match(ale#util#BufferCdString(bufnr('')), '^cd .*test/util && $') >= 0, - \ 'String didn''t match regex: ' . ale#util#BufferCdString(bufnr('')) + Assert match(ale#path#BufferCdString(bufnr('')), '^cd .*test/util && $') >= 0, + \ 'String didn''t match regex: ' . ale#path#BufferCdString(bufnr('')) |