Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions ale_linters/toml/tombi.vim
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ function! ale_linters#toml#tombi#GetProjectRoot(buffer) abort
return fnamemodify(l:pyproject_file . '/', ':p:h:h')
endif

" Try to find nearest `git` directory
let l:gitdir = ale#path#FindNearestFile(a:buffer, '.git')
" Try to find the nearest VCS root.
let l:vcs_root = ale#path#FindVCSRoot(a:buffer)

if !empty(l:gitdir)
return fnamemodify(l:gitdir . '/', ':p:h:h')
if !empty(l:vcs_root)
return l:vcs_root
endif

return ''
Expand Down
44 changes: 44 additions & 0 deletions autoload/ale/path.vim
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@

let s:is_windows = has('win32') || has('win64') || has('win32unix')

let s:vcs_root_marker_map = {
\ 'git': {'directories': ['.git'], 'files': ['.git']},
\ 'got': {'directories': ['.got']},
\ 'hg': {'directories': ['.hg']},
\}

" simplify a path, and fix annoying issues with paths on Windows.
"
" Forward slashes are changed to back slashes so path equality works better
Expand Down Expand Up @@ -94,6 +100,44 @@ function! ale#path#FindNearestFileOrDirectory(buffer, filename) abort
return ''
endfunction

function! s:NearestVCSRoot(nearest_root, marker_path) abort
if empty(a:marker_path)
return a:nearest_root
endif

let l:root = ale#path#Simplify(ale#path#Dirname(a:marker_path))

return empty(a:nearest_root) || strlen(l:root) > strlen(a:nearest_root)
\ ? l:root
\ : a:nearest_root
endfunction

" Given a buffer, find the nearest root directory for selected version control
" systems by searching for VCS marker files and directories.
function! ale#path#FindVCSRoot(buffer) abort
let l:nearest_root = ''

for l:vcs in ale#Var(a:buffer, 'vcs_root')
let l:config = get(s:vcs_root_marker_map, l:vcs, {})

for l:directory in get(l:config, 'directories', [])
let l:nearest_root = s:NearestVCSRoot(
\ l:nearest_root,
\ ale#path#FindNearestDirectory(a:buffer, l:directory)
\)
endfor

for l:file in get(l:config, 'files', [])
let l:nearest_root = s:NearestVCSRoot(
\ l:nearest_root,
\ ale#path#FindNearestFile(a:buffer, l:file)
\)
endfor
endfor

return l:nearest_root
endfunction

" Given a buffer, a string to search for, and 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.
Expand Down
11 changes: 11 additions & 0 deletions doc/ale.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2355,6 +2355,17 @@ g:ale_root
detect a project root. If this, too, yields no result, and the linter is an
LSP linter, it will not run.

*ale-options.vcs_root*
*g:ale_vcs_root*
*b:ale_vcs_root*
vcs_root
g:ale_vcs_root
Type: |List|
Default: `['git', 'got', 'hg']`

This option configures which version control systems ALE will check when
looking for project roots. Supported values are `'git'`, `'got'`, and `'hg'`.

*ale-options.save_hidden*
*g:ale_save_hidden*
save_hidden
Expand Down
7 changes: 7 additions & 0 deletions plugin/ale.vim
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ let g:ale_filename_mappings = get(g:, 'ale_filename_mappings', {})
" This Dictionary configures the default project roots for various linters.
let g:ale_root = get(g:, 'ale_root', {})

" A List of VCS root markers to use for project root detection.
let g:ale_vcs_root = get(g:, 'ale_vcs_root', [
\ 'git',
\ 'got',
\ 'hg',
\])

" These flags dictates if ale uses the quickfix or the loclist (loclist is the
" default, quickfix overrides loclist).
let g:ale_set_loclist = get(g:, 'ale_set_loclist', v:true)
Expand Down
14 changes: 14 additions & 0 deletions test/linter/test_toml_tombi.vader
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ Before:
Save g:ale_toml_tombi_online
call ale#assert#SetUpLinterTest('toml', 'tombi')

let g:tombi_vcs_root_temp_dir = ale#path#Simplify(tempname())
call mkdir(g:tombi_vcs_root_temp_dir, 'p')

After:
call delete(g:tombi_vcs_root_temp_dir, 'rf')
unlet! g:tombi_vcs_root_temp_dir

call ale#assert#TearDownLinterTest()

Execute(The default executable path should be correct):
Expand All @@ -22,3 +28,11 @@ Execute(The project root should be detected correctly in Python projects):
call ale#test#SetFilename('../test-files/toml/tombi/pyprojecttoml/subdir/file.ext')

AssertLSPProject ale#path#Simplify(g:dir . '/../test-files/toml/tombi/pyprojecttoml')

Execute(The project root should be detected correctly with a VCS root):
call mkdir(g:tombi_vcs_root_temp_dir . '/project/subdir', 'p')
call mkdir(g:tombi_vcs_root_temp_dir . '/project/.git')
call writefile([], g:tombi_vcs_root_temp_dir . '/project/subdir/file.ext')
call ale#test#SetFilename(g:tombi_vcs_root_temp_dir . '/project/subdir/file.ext')

AssertLSPProject ale#path#Simplify(g:tombi_vcs_root_temp_dir . '/project')
87 changes: 87 additions & 0 deletions test/test_find_vcs_root.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
Before:
Save g:ale_vcs_root
Save b:ale_vcs_root

call ale#test#SetDirectory('/testplugin/test')

let g:vcs_root_temp_dir = ale#path#Simplify(tempname())
call mkdir(g:vcs_root_temp_dir, 'p')
let g:ale_vcs_root = ['git', 'got', 'hg']

After:
call ale#test#RestoreDirectory()
call delete(g:vcs_root_temp_dir, 'rf')
unlet! g:vcs_root_temp_dir

Restore

Execute(FindVCSRoot should find a simple git directory):
call mkdir(g:vcs_root_temp_dir . '/simple/project/src', 'p')
call mkdir(g:vcs_root_temp_dir . '/simple/project/.git')
call writefile([], g:vcs_root_temp_dir . '/simple/project/src/file.toml')
call ale#test#SetFilename(g:vcs_root_temp_dir . '/simple/project/src/file.toml')

AssertEqual
\ ale#path#Simplify(g:vcs_root_temp_dir . '/simple/project'),
\ ale#path#FindVCSRoot(bufnr(''))

Execute(FindVCSRoot should find git worktree files):
call mkdir(g:vcs_root_temp_dir . '/with-worktree/project/src', 'p')
call mkdir(g:vcs_root_temp_dir . '/with-worktree/project/.git')
call mkdir(g:vcs_root_temp_dir . '/with-worktree/project/wt/src', 'p')
call writefile([], g:vcs_root_temp_dir . '/with-worktree/project/wt/.git')
call writefile([], g:vcs_root_temp_dir . '/with-worktree/project/wt/src/file.toml')
call ale#test#SetFilename(g:vcs_root_temp_dir . '/with-worktree/project/wt/src/file.toml')

AssertEqual
\ ale#path#Simplify(g:vcs_root_temp_dir . '/with-worktree/project/wt'),
\ ale#path#FindVCSRoot(bufnr(''))

Execute(FindVCSRoot should choose the deepest root with the same marker):
call mkdir(g:vcs_root_temp_dir . '/nested_same/outer/inner/src', 'p')
call mkdir(g:vcs_root_temp_dir . '/nested_same/outer/.git')
call mkdir(g:vcs_root_temp_dir . '/nested_same/outer/inner/.git')
call writefile([], g:vcs_root_temp_dir . '/nested_same/outer/inner/src/file.toml')
call ale#test#SetFilename(g:vcs_root_temp_dir . '/nested_same/outer/inner/src/file.toml')

AssertEqual
\ ale#path#Simplify(g:vcs_root_temp_dir . '/nested_same/outer/inner'),
\ ale#path#FindVCSRoot(bufnr(''))

Execute(FindVCSRoot should choose the deepest root with different markers):
call mkdir(g:vcs_root_temp_dir . '/nested_different/project/module/src', 'p')
call mkdir(g:vcs_root_temp_dir . '/nested_different/project/.got')
call mkdir(g:vcs_root_temp_dir . '/nested_different/project/module/.hg')
call writefile([], g:vcs_root_temp_dir . '/nested_different/project/module/src/file.toml')
call ale#test#SetFilename(g:vcs_root_temp_dir . '/nested_different/project/module/src/file.toml')

AssertEqual
\ ale#path#Simplify(g:vcs_root_temp_dir . '/nested_different/project/module'),
\ ale#path#FindVCSRoot(bufnr(''))

Execute(FindVCSRoot should obey the global marker selector):
let g:ale_vcs_root = ['git']

call mkdir(g:vcs_root_temp_dir . '/global_selector/outer/inner/src', 'p')
call mkdir(g:vcs_root_temp_dir . '/global_selector/outer/.git')
call mkdir(g:vcs_root_temp_dir . '/global_selector/outer/inner/.hg')
call writefile([], g:vcs_root_temp_dir . '/global_selector/outer/inner/src/file.toml')
call ale#test#SetFilename(g:vcs_root_temp_dir . '/global_selector/outer/inner/src/file.toml')

AssertEqual
\ ale#path#Simplify(g:vcs_root_temp_dir . '/global_selector/outer'),
\ ale#path#FindVCSRoot(bufnr(''))

Execute(FindVCSRoot should let buffer marker selectors override globals):
let g:ale_vcs_root = ['git']
let b:ale_vcs_root = ['hg']

call mkdir(g:vcs_root_temp_dir . '/buffer_selector/outer/inner/src', 'p')
call mkdir(g:vcs_root_temp_dir . '/buffer_selector/outer/.git')
call mkdir(g:vcs_root_temp_dir . '/buffer_selector/outer/inner/.hg')
call writefile([], g:vcs_root_temp_dir . '/buffer_selector/outer/inner/src/file.toml')
call ale#test#SetFilename(g:vcs_root_temp_dir . '/buffer_selector/outer/inner/src/file.toml')

AssertEqual
\ ale#path#Simplify(g:vcs_root_temp_dir . '/buffer_selector/outer/inner'),
\ ale#path#FindVCSRoot(bufnr(''))
Loading