Fix #2421 - Use compile_commands.json in build dirs to find roots
This commit is contained in:
parent
781bf1502f
commit
143af2b9fd
15 changed files with 78 additions and 78 deletions
|
@ -4,12 +4,6 @@
|
|||
call ale#Set('c_clangd_executable', 'clangd')
|
||||
call ale#Set('c_clangd_options', '')
|
||||
|
||||
function! ale_linters#c#clangd#GetProjectRoot(buffer) abort
|
||||
let l:project_root = ale#path#FindNearestFile(a:buffer, 'compile_commands.json')
|
||||
|
||||
return !empty(l:project_root) ? fnamemodify(l:project_root, ':h') : ''
|
||||
endfunction
|
||||
|
||||
function! ale_linters#c#clangd#GetCommand(buffer) abort
|
||||
return '%e' . ale#Pad(ale#Var(a:buffer, 'c_clangd_options'))
|
||||
endfunction
|
||||
|
@ -19,5 +13,5 @@ call ale#linter#Define('c', {
|
|||
\ 'lsp': 'stdio',
|
||||
\ 'executable': {b -> ale#Var(b, 'c_clangd_executable')},
|
||||
\ 'command': function('ale_linters#c#clangd#GetCommand'),
|
||||
\ 'project_root': function('ale_linters#c#clangd#GetProjectRoot'),
|
||||
\ 'project_root': function('ale#c#FindProjectRoot'),
|
||||
\})
|
||||
|
|
|
@ -5,13 +5,15 @@ call ale#Set('c_cquery_executable', 'cquery')
|
|||
call ale#Set('c_cquery_cache_directory', expand('~/.cache/cquery'))
|
||||
|
||||
function! ale_linters#c#cquery#GetProjectRoot(buffer) abort
|
||||
let l:project_root = ale#path#FindNearestFile(a:buffer, 'compile_commands.json')
|
||||
" Try to find cquery configuration files first.
|
||||
let l:config = ale#path#FindNearestFile(a:buffer, '.cquery')
|
||||
|
||||
if empty(l:project_root)
|
||||
let l:project_root = ale#path#FindNearestFile(a:buffer, '.cquery')
|
||||
if !empty(l:config)
|
||||
return fnamemodify(l:config, ':h')
|
||||
endif
|
||||
|
||||
return !empty(l:project_root) ? fnamemodify(l:project_root, ':h') : ''
|
||||
" Fall back on default project root detection.
|
||||
return ale#c#FindProjectRoot(a:buffer)
|
||||
endfunction
|
||||
|
||||
function! ale_linters#c#cquery#GetInitializationOptions(buffer) abort
|
||||
|
|
|
@ -12,7 +12,8 @@ function! ale_linters#cpp#clangcheck#GetCommand(buffer) abort
|
|||
let l:build_dir = ale#Var(a:buffer, 'c_build_dir')
|
||||
|
||||
if empty(l:build_dir)
|
||||
let l:build_dir = ale#path#Dirname(ale#c#FindCompileCommands(a:buffer))
|
||||
let [l:root, l:json_file] = ale#c#FindCompileCommands(a:buffer)
|
||||
let l:build_dir = ale#path#Dirname(l:json_file)
|
||||
endif
|
||||
|
||||
" The extra arguments in the command are used to prevent .plist files from
|
||||
|
|
|
@ -4,12 +4,6 @@
|
|||
call ale#Set('cpp_clangd_executable', 'clangd')
|
||||
call ale#Set('cpp_clangd_options', '')
|
||||
|
||||
function! ale_linters#cpp#clangd#GetProjectRoot(buffer) abort
|
||||
let l:project_root = ale#path#FindNearestFile(a:buffer, 'compile_commands.json')
|
||||
|
||||
return !empty(l:project_root) ? fnamemodify(l:project_root, ':h') : ''
|
||||
endfunction
|
||||
|
||||
function! ale_linters#cpp#clangd#GetCommand(buffer) abort
|
||||
return '%e' . ale#Pad(ale#Var(a:buffer, 'cpp_clangd_options'))
|
||||
endfunction
|
||||
|
@ -19,5 +13,5 @@ call ale#linter#Define('cpp', {
|
|||
\ 'lsp': 'stdio',
|
||||
\ 'executable': {b -> ale#Var(b, 'cpp_clangd_executable')},
|
||||
\ 'command': function('ale_linters#cpp#clangd#GetCommand'),
|
||||
\ 'project_root': function('ale_linters#cpp#clangd#GetProjectRoot'),
|
||||
\ 'project_root': function('ale#c#FindProjectRoot'),
|
||||
\})
|
||||
|
|
|
@ -5,13 +5,15 @@ call ale#Set('cpp_cquery_executable', 'cquery')
|
|||
call ale#Set('cpp_cquery_cache_directory', expand('~/.cache/cquery'))
|
||||
|
||||
function! ale_linters#cpp#cquery#GetProjectRoot(buffer) abort
|
||||
let l:project_root = ale#path#FindNearestFile(a:buffer, 'compile_commands.json')
|
||||
" Try to find cquery configuration files first.
|
||||
let l:config = ale#path#FindNearestFile(a:buffer, '.cquery')
|
||||
|
||||
if empty(l:project_root)
|
||||
let l:project_root = ale#path#FindNearestFile(a:buffer, '.cquery')
|
||||
if !empty(l:config)
|
||||
return fnamemodify(l:config, ':h')
|
||||
endif
|
||||
|
||||
return !empty(l:project_root) ? fnamemodify(l:project_root, ':h') : ''
|
||||
" Fall back on default project root detection.
|
||||
return ale#c#FindProjectRoot(a:buffer)
|
||||
endfunction
|
||||
|
||||
function! ale_linters#cpp#cquery#GetInitializationOptions(buffer) abort
|
||||
|
|
|
@ -4,12 +4,6 @@
|
|||
call ale#Set('objc_clangd_executable', 'clangd')
|
||||
call ale#Set('objc_clangd_options', '')
|
||||
|
||||
function! ale_linters#objc#clangd#GetProjectRoot(buffer) abort
|
||||
let l:project_root = ale#path#FindNearestFile(a:buffer, 'compile_commands.json')
|
||||
|
||||
return !empty(l:project_root) ? fnamemodify(l:project_root, ':h') : ''
|
||||
endfunction
|
||||
|
||||
function! ale_linters#objc#clangd#GetCommand(buffer) abort
|
||||
return '%e' . ale#Pad(ale#Var(a:buffer, 'objc_clangd_options'))
|
||||
endfunction
|
||||
|
@ -19,5 +13,5 @@ call ale#linter#Define('objc', {
|
|||
\ 'lsp': 'stdio',
|
||||
\ 'executable': {b -> ale#Var(b, 'objc_clangd_executable')},
|
||||
\ 'command': function('ale_linters#objc#clangd#GetCommand'),
|
||||
\ 'project_root': function('ale_linters#objc#clangd#GetProjectRoot'),
|
||||
\ 'project_root': function('ale#c#FindProjectRoot'),
|
||||
\})
|
||||
|
|
|
@ -4,12 +4,6 @@
|
|||
call ale#Set('objcpp_clangd_executable', 'clangd')
|
||||
call ale#Set('objcpp_clangd_options', '')
|
||||
|
||||
function! ale_linters#objcpp#clangd#GetProjectRoot(buffer) abort
|
||||
let l:project_root = ale#path#FindNearestFile(a:buffer, 'compile_commands.json')
|
||||
|
||||
return !empty(l:project_root) ? fnamemodify(l:project_root, ':h') : ''
|
||||
endfunction
|
||||
|
||||
function! ale_linters#objcpp#clangd#GetCommand(buffer) abort
|
||||
return '%e' . ale#Pad(ale#Var(a:buffer, 'objcpp_clangd_options'))
|
||||
endfunction
|
||||
|
@ -19,5 +13,5 @@ call ale#linter#Define('objcpp', {
|
|||
\ 'lsp': 'stdio',
|
||||
\ 'executable': {b -> ale#Var(b, 'objcpp_clangd_executable')},
|
||||
\ 'command': function('ale_linters#objcpp#clangd#GetCommand'),
|
||||
\ 'project_root': function('ale_linters#objcpp#clangd#GetProjectRoot'),
|
||||
\ 'project_root': function('ale#c#FindProjectRoot'),
|
||||
\})
|
||||
|
|
|
@ -23,27 +23,9 @@ function! ale#c#GetBuildDirectory(buffer) abort
|
|||
return l:build_dir
|
||||
endif
|
||||
|
||||
return ale#path#Dirname(ale#c#FindCompileCommands(a:buffer))
|
||||
endfunction
|
||||
let [l:root, l:json_file] = ale#c#FindCompileCommands(a:buffer)
|
||||
|
||||
|
||||
function! ale#c#FindProjectRoot(buffer) abort
|
||||
for l:project_filename in g:__ale_c_project_filenames
|
||||
let l:full_path = ale#path#FindNearestFile(a:buffer, l:project_filename)
|
||||
|
||||
if !empty(l:full_path)
|
||||
let l:path = fnamemodify(l:full_path, ':h')
|
||||
|
||||
" Correct .git path detection.
|
||||
if fnamemodify(l:path, ':t') is# '.git'
|
||||
let l:path = fnamemodify(l:path, ':h')
|
||||
endif
|
||||
|
||||
return l:path
|
||||
endif
|
||||
endfor
|
||||
|
||||
return ''
|
||||
return ale#path#Dirname(l:json_file)
|
||||
endfunction
|
||||
|
||||
function! ale#c#AreSpecialCharsBalanced(option) abort
|
||||
|
@ -120,7 +102,7 @@ endfunction
|
|||
|
||||
function! ale#c#ParseCFlagsFromMakeOutput(buffer, make_output) abort
|
||||
if !g:ale_c_parse_makefile
|
||||
return ''
|
||||
return v:null
|
||||
endif
|
||||
|
||||
let l:buffer_filename = expand('#' . a:buffer . ':t')
|
||||
|
@ -140,14 +122,17 @@ function! ale#c#ParseCFlagsFromMakeOutput(buffer, make_output) abort
|
|||
return ale#c#ParseCFlags(l:makefile_dir, l:cflag_line)
|
||||
endfunction
|
||||
|
||||
" Given a buffer number, find the build subdirectory with compile commands
|
||||
" The subdirectory is returned without the trailing /
|
||||
" Given a buffer number, find the project directory containing
|
||||
" compile_commands.json, and the path to the compile_commands.json file.
|
||||
"
|
||||
" If compile_commands.json cannot be found, two empty strings will be
|
||||
" returned.
|
||||
function! ale#c#FindCompileCommands(buffer) abort
|
||||
" Look above the current source file to find compile_commands.json
|
||||
let l:json_file = ale#path#FindNearestFile(a:buffer, 'compile_commands.json')
|
||||
|
||||
if !empty(l:json_file)
|
||||
return l:json_file
|
||||
return [fnamemodify(l:json_file, ':h'), l:json_file]
|
||||
endif
|
||||
|
||||
" Search in build directories if we can't find it in the project.
|
||||
|
@ -157,12 +142,42 @@ function! ale#c#FindCompileCommands(buffer) abort
|
|||
let l:json_file = l:c_build_dir . s:sep . 'compile_commands.json'
|
||||
|
||||
if filereadable(l:json_file)
|
||||
return l:json_file
|
||||
return [l:path, l:json_file]
|
||||
endif
|
||||
endfor
|
||||
endfor
|
||||
|
||||
return ''
|
||||
return ['', '']
|
||||
endfunction
|
||||
|
||||
" Find the project root for C/C++ projects.
|
||||
"
|
||||
" The location of compile_commands.json will be used to find project roots.
|
||||
"
|
||||
" If compile_commands.json cannot be found, other common configuration files
|
||||
" will be used to detect the project root.
|
||||
function! ale#c#FindProjectRoot(buffer) abort
|
||||
let [l:root, l:json_file] = ale#c#FindCompileCommands(a:buffer)
|
||||
|
||||
" Fall back on detecting the project root based on other filenames.
|
||||
if empty(l:root)
|
||||
for l:project_filename in g:__ale_c_project_filenames
|
||||
let l:full_path = ale#path#FindNearestFile(a:buffer, l:project_filename)
|
||||
|
||||
if !empty(l:full_path)
|
||||
let l:path = fnamemodify(l:full_path, ':h')
|
||||
|
||||
" Correct .git path detection.
|
||||
if fnamemodify(l:path, ':t') is# '.git'
|
||||
let l:path = fnamemodify(l:path, ':h')
|
||||
endif
|
||||
|
||||
return l:path
|
||||
endif
|
||||
endfor
|
||||
endif
|
||||
|
||||
return l:root
|
||||
endfunction
|
||||
|
||||
" Cache compile_commands.json data in a Dictionary, so we don't need to read
|
||||
|
@ -201,7 +216,7 @@ function! s:GetLookupFromCompileCommandsFile(compile_commands_file) abort
|
|||
let l:file_lookup = {}
|
||||
let l:dir_lookup = {}
|
||||
|
||||
for l:entry in l:raw_data
|
||||
for l:entry in (type(l:raw_data) is v:t_list ? l:raw_data : [])
|
||||
let l:basename = tolower(fnamemodify(l:entry.file, ':t'))
|
||||
let l:file_lookup[l:basename] = get(l:file_lookup, l:basename, []) + [l:entry]
|
||||
|
||||
|
@ -278,25 +293,25 @@ function! ale#c#FlagsFromCompileCommands(buffer, compile_commands_file) abort
|
|||
endfunction
|
||||
|
||||
function! ale#c#GetCFlags(buffer, output) abort
|
||||
let l:cflags = ' '
|
||||
let l:cflags = v:null
|
||||
|
||||
if ale#Var(a:buffer, 'c_parse_makefile') && !empty(a:output)
|
||||
let l:cflags = ale#c#ParseCFlagsFromMakeOutput(a:buffer, a:output)
|
||||
endif
|
||||
|
||||
if ale#Var(a:buffer, 'c_parse_compile_commands')
|
||||
let l:json_file = ale#c#FindCompileCommands(a:buffer)
|
||||
let [l:root, l:json_file] = ale#c#FindCompileCommands(a:buffer)
|
||||
|
||||
if !empty(l:json_file)
|
||||
let l:cflags = ale#c#FlagsFromCompileCommands(a:buffer, l:json_file)
|
||||
endif
|
||||
endif
|
||||
|
||||
if l:cflags is# ' '
|
||||
if l:cflags is v:null
|
||||
let l:cflags = ale#c#IncludeOptions(ale#c#FindLocalHeaderPaths(a:buffer))
|
||||
endif
|
||||
|
||||
return l:cflags
|
||||
return l:cflags isnot v:null ? l:cflags : ''
|
||||
endfunction
|
||||
|
||||
function! ale#c#GetMakeCommand(buffer) abort
|
||||
|
|
|
@ -3,15 +3,17 @@ scriptencoding utf-8
|
|||
" Description: Utilities for ccls
|
||||
|
||||
function! ale#handlers#ccls#GetProjectRoot(buffer) abort
|
||||
let l:project_root = ale#path#FindNearestFile(a:buffer, '.ccls-root')
|
||||
" Try to find ccls configuration files first.
|
||||
let l:config = ale#path#FindNearestFile(a:buffer, '.ccls-root')
|
||||
|
||||
if empty(l:project_root)
|
||||
let l:project_root = ale#path#FindNearestFile(a:buffer, 'compile_commands.json')
|
||||
if empty(l:config)
|
||||
let l:config = ale#path#FindNearestFile(a:buffer, '.ccls')
|
||||
endif
|
||||
|
||||
if empty(l:project_root)
|
||||
let l:project_root = ale#path#FindNearestFile(a:buffer, '.ccls')
|
||||
if !empty(l:config)
|
||||
return fnamemodify(l:config, ':h')
|
||||
endif
|
||||
|
||||
return !empty(l:project_root) ? fnamemodify(l:project_root, ':h') : ''
|
||||
" Fall back on default project root detection.
|
||||
return ale#c#FindProjectRoot(a:buffer)
|
||||
endfunction
|
||||
|
|
|
@ -7,9 +7,9 @@ After:
|
|||
Execute(The project root should be detected correctly using compile_commands.json file):
|
||||
AssertLSPProject ''
|
||||
|
||||
call ale#test#SetFilename('cquery_paths/with_compile_commands_json/dummy.c')
|
||||
call ale#test#SetFilename('cquery_paths/dummy.c')
|
||||
|
||||
AssertLSPProject ale#path#Simplify(g:dir . '/cquery_paths/with_compile_commands_json')
|
||||
AssertLSPProject ale#path#Simplify(g:dir . '/cquery_paths')
|
||||
|
||||
Execute(The project root should be detected correctly using .cquery file):
|
||||
AssertLSPProject ''
|
||||
|
|
|
@ -2,6 +2,7 @@ Before:
|
|||
" Make sure the c.vim file is loaded first.
|
||||
call ale#c#FindProjectRoot(bufnr(''))
|
||||
|
||||
Save g:ale_c_parse_compile_commands
|
||||
Save g:ale_c_parse_makefile
|
||||
Save g:__ale_c_project_filenames
|
||||
|
||||
|
@ -14,6 +15,7 @@ Before:
|
|||
\ 'v:val isnot# ''.git/HEAD'''
|
||||
\)
|
||||
|
||||
let g:ale_c_parse_compile_commands = 0
|
||||
let g:ale_c_parse_makefile = 0
|
||||
|
||||
After:
|
||||
|
|
|
@ -10,9 +10,9 @@ After:
|
|||
Execute(The project root should be detected correctly using compile_commands.json file):
|
||||
AssertLSPProject ''
|
||||
|
||||
call ale#test#SetFilename('cquery_paths/with_compile_commands_json/dummy.cpp')
|
||||
call ale#test#SetFilename('cquery_paths/dummy.cpp')
|
||||
|
||||
AssertLSPProject ale#path#Simplify(g:dir . '/cquery_paths/with_compile_commands_json')
|
||||
AssertLSPProject ale#path#Simplify(g:dir . '/cquery_paths')
|
||||
|
||||
Execute(The project root should be detected correctly using .cquery file):
|
||||
AssertLSPProject ''
|
||||
|
|
Reference in a new issue