Prefer ale_root setting for project roots

This commit is contained in:
Andrew Wray
2025-06-25 20:50:15 +01:00
parent 9e49019a26
commit 0b0f8d91bc
10 changed files with 80 additions and 52 deletions

View File

@@ -216,7 +216,7 @@ endfunction
function! ale#assert#LSPProject(expected_root) abort
let l:buffer = bufnr('')
let l:linter = s:GetLinter()
let l:root = ale#lsp_linter#FindProjectRoot(l:buffer, l:linter)
let l:root = ale#linter#GetRoot(l:buffer, l:linter)
AssertEqual a:expected_root, l:root
endfunction

View File

@@ -447,3 +447,32 @@ function! ale#linter#GetAddress(buffer, linter) abort
return type(l:Address) is v:t_func ? l:Address(a:buffer) : l:Address
endfunction
" Get the project root for a linter.
" If |b:ale_root| or |g:ale_root| is set to either a String or a Dict mapping
" linter names to roots or callbacks, return that value immediately. When no
" value is available, fall back to the linter-specific configuration.
function! ale#linter#GetRoot(buffer, linter) abort
let l:buffer_ale_root = getbufvar(a:buffer, 'ale_root', {})
if type(l:buffer_ale_root) is v:t_string
return l:buffer_ale_root
endif
if has_key(l:buffer_ale_root, a:linter.name)
let l:Root = l:buffer_ale_root[a:linter.name]
return type(l:Root) is v:t_func ? l:Root(a:buffer) : l:Root
endif
if has_key(g:ale_root, a:linter.name)
let l:Root = g:ale_root[a:linter.name]
return type(l:Root) is v:t_func ? l:Root(a:buffer) : l:Root
endif
if has_key(a:linter, 'project_root')
let l:Root = a:linter.project_root
return type(l:Root) is v:t_func ? l:Root(a:buffer) : l:Root
endif
return ''
endfunction

View File

@@ -296,44 +296,6 @@ function! ale#lsp_linter#GetConfig(buffer, linter) abort
return {}
endfunction
function! ale#lsp_linter#FindProjectRoot(buffer, linter) abort
let l:buffer_ale_root = getbufvar(a:buffer, 'ale_root', {})
if type(l:buffer_ale_root) is v:t_string
return l:buffer_ale_root
endif
" Try to get a buffer-local setting for the root
if has_key(l:buffer_ale_root, a:linter.name)
let l:Root = l:buffer_ale_root[a:linter.name]
if type(l:Root) is v:t_func
return l:Root(a:buffer)
else
return l:Root
endif
endif
" Try to get a global setting for the root
if has_key(g:ale_root, a:linter.name)
let l:Root = g:ale_root[a:linter.name]
if type(l:Root) is v:t_func
return l:Root(a:buffer)
else
return l:Root
endif
endif
" Fall back to the linter-specific configuration
if has_key(a:linter, 'project_root')
let l:Root = a:linter.project_root
return type(l:Root) is v:t_func ? l:Root(a:buffer) : l:Root
endif
return ale#util#GetFunction(a:linter.project_root_callback)(a:buffer)
endfunction
" This function is accessible so tests can call it.
function! ale#lsp_linter#OnInit(linter, details, Callback) abort
@@ -504,7 +466,7 @@ endfunction
function! ale#lsp_linter#StartLSP(buffer, linter, Callback) abort
let l:command = ''
let l:address = ''
let l:root = ale#lsp_linter#FindProjectRoot(a:buffer, a:linter)
let l:root = ale#linter#GetRoot(a:buffer, a:linter)
if empty(l:root) && a:linter.lsp isnot# 'tsserver'
" If there's no project root, then we can't check files with LSP,

View File

@@ -61,6 +61,12 @@ endfunction
" through paths, including the current directory, until no __init__.py files
" is found.
function! ale#python#FindProjectRoot(buffer) abort
let l:root = ale#linter#GetRoot(a:buffer, {'name': 'python'})
if !empty(l:root)
return l:root
endif
let l:ini_root = ale#python#FindProjectRootIni(a:buffer)
if !empty(l:ini_root)