Move path functions to their own file

This commit is contained in:
w0rp 2017-04-17 23:29:02 +01:00
parent 6c762237ce
commit e237add9fd
26 changed files with 90 additions and 89 deletions

View File

@ -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'

View File

@ -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'

View File

@ -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)
\ : ''

View File

@ -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')

View File

@ -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

View File

@ -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

View File

@ -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')

View File

@ -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')

View File

@ -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')

View File

@ -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.

View File

@ -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', '')

View File

@ -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')

View File

@ -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

View File

@ -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 . ' '

View File

@ -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,

View File

@ -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')

View File

@ -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')

View File

@ -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')

View File

@ -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')

View File

@ -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')

57
autoload/ale/path.vim Normal file
View File

@ -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

View File

@ -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

View File

@ -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')

View File

@ -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')

View File

@ -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')

View File

@ -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(''))