Move path functions to their own file
This commit is contained in:
57
autoload/ale/path.vim
Normal file
57
autoload/ale/path.vim
Normal 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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user