Fix #2421 - Use compile_commands.json in build dirs to find roots

This commit is contained in:
w0rp 2019-05-20 19:57:08 +01:00
parent 781bf1502f
commit 143af2b9fd
No known key found for this signature in database
GPG key ID: 0FC1ECAA8C81CD83
15 changed files with 78 additions and 78 deletions

View file

@ -4,12 +4,6 @@
call ale#Set('c_clangd_executable', 'clangd') call ale#Set('c_clangd_executable', 'clangd')
call ale#Set('c_clangd_options', '') 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 function! ale_linters#c#clangd#GetCommand(buffer) abort
return '%e' . ale#Pad(ale#Var(a:buffer, 'c_clangd_options')) return '%e' . ale#Pad(ale#Var(a:buffer, 'c_clangd_options'))
endfunction endfunction
@ -19,5 +13,5 @@ call ale#linter#Define('c', {
\ 'lsp': 'stdio', \ 'lsp': 'stdio',
\ 'executable': {b -> ale#Var(b, 'c_clangd_executable')}, \ 'executable': {b -> ale#Var(b, 'c_clangd_executable')},
\ 'command': function('ale_linters#c#clangd#GetCommand'), \ 'command': function('ale_linters#c#clangd#GetCommand'),
\ 'project_root': function('ale_linters#c#clangd#GetProjectRoot'), \ 'project_root': function('ale#c#FindProjectRoot'),
\}) \})

View file

@ -5,13 +5,15 @@ call ale#Set('c_cquery_executable', 'cquery')
call ale#Set('c_cquery_cache_directory', expand('~/.cache/cquery')) call ale#Set('c_cquery_cache_directory', expand('~/.cache/cquery'))
function! ale_linters#c#cquery#GetProjectRoot(buffer) abort 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) if !empty(l:config)
let l:project_root = ale#path#FindNearestFile(a:buffer, '.cquery') return fnamemodify(l:config, ':h')
endif 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 endfunction
function! ale_linters#c#cquery#GetInitializationOptions(buffer) abort function! ale_linters#c#cquery#GetInitializationOptions(buffer) abort

View file

@ -12,7 +12,8 @@ function! ale_linters#cpp#clangcheck#GetCommand(buffer) abort
let l:build_dir = ale#Var(a:buffer, 'c_build_dir') let l:build_dir = ale#Var(a:buffer, 'c_build_dir')
if empty(l: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 endif
" The extra arguments in the command are used to prevent .plist files from " The extra arguments in the command are used to prevent .plist files from

View file

@ -4,12 +4,6 @@
call ale#Set('cpp_clangd_executable', 'clangd') call ale#Set('cpp_clangd_executable', 'clangd')
call ale#Set('cpp_clangd_options', '') 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 function! ale_linters#cpp#clangd#GetCommand(buffer) abort
return '%e' . ale#Pad(ale#Var(a:buffer, 'cpp_clangd_options')) return '%e' . ale#Pad(ale#Var(a:buffer, 'cpp_clangd_options'))
endfunction endfunction
@ -19,5 +13,5 @@ call ale#linter#Define('cpp', {
\ 'lsp': 'stdio', \ 'lsp': 'stdio',
\ 'executable': {b -> ale#Var(b, 'cpp_clangd_executable')}, \ 'executable': {b -> ale#Var(b, 'cpp_clangd_executable')},
\ 'command': function('ale_linters#cpp#clangd#GetCommand'), \ 'command': function('ale_linters#cpp#clangd#GetCommand'),
\ 'project_root': function('ale_linters#cpp#clangd#GetProjectRoot'), \ 'project_root': function('ale#c#FindProjectRoot'),
\}) \})

View file

@ -5,13 +5,15 @@ call ale#Set('cpp_cquery_executable', 'cquery')
call ale#Set('cpp_cquery_cache_directory', expand('~/.cache/cquery')) call ale#Set('cpp_cquery_cache_directory', expand('~/.cache/cquery'))
function! ale_linters#cpp#cquery#GetProjectRoot(buffer) abort 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) if !empty(l:config)
let l:project_root = ale#path#FindNearestFile(a:buffer, '.cquery') return fnamemodify(l:config, ':h')
endif 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 endfunction
function! ale_linters#cpp#cquery#GetInitializationOptions(buffer) abort function! ale_linters#cpp#cquery#GetInitializationOptions(buffer) abort

View file

@ -4,12 +4,6 @@
call ale#Set('objc_clangd_executable', 'clangd') call ale#Set('objc_clangd_executable', 'clangd')
call ale#Set('objc_clangd_options', '') 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 function! ale_linters#objc#clangd#GetCommand(buffer) abort
return '%e' . ale#Pad(ale#Var(a:buffer, 'objc_clangd_options')) return '%e' . ale#Pad(ale#Var(a:buffer, 'objc_clangd_options'))
endfunction endfunction
@ -19,5 +13,5 @@ call ale#linter#Define('objc', {
\ 'lsp': 'stdio', \ 'lsp': 'stdio',
\ 'executable': {b -> ale#Var(b, 'objc_clangd_executable')}, \ 'executable': {b -> ale#Var(b, 'objc_clangd_executable')},
\ 'command': function('ale_linters#objc#clangd#GetCommand'), \ 'command': function('ale_linters#objc#clangd#GetCommand'),
\ 'project_root': function('ale_linters#objc#clangd#GetProjectRoot'), \ 'project_root': function('ale#c#FindProjectRoot'),
\}) \})

View file

@ -4,12 +4,6 @@
call ale#Set('objcpp_clangd_executable', 'clangd') call ale#Set('objcpp_clangd_executable', 'clangd')
call ale#Set('objcpp_clangd_options', '') 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 function! ale_linters#objcpp#clangd#GetCommand(buffer) abort
return '%e' . ale#Pad(ale#Var(a:buffer, 'objcpp_clangd_options')) return '%e' . ale#Pad(ale#Var(a:buffer, 'objcpp_clangd_options'))
endfunction endfunction
@ -19,5 +13,5 @@ call ale#linter#Define('objcpp', {
\ 'lsp': 'stdio', \ 'lsp': 'stdio',
\ 'executable': {b -> ale#Var(b, 'objcpp_clangd_executable')}, \ 'executable': {b -> ale#Var(b, 'objcpp_clangd_executable')},
\ 'command': function('ale_linters#objcpp#clangd#GetCommand'), \ 'command': function('ale_linters#objcpp#clangd#GetCommand'),
\ 'project_root': function('ale_linters#objcpp#clangd#GetProjectRoot'), \ 'project_root': function('ale#c#FindProjectRoot'),
\}) \})

View file

@ -23,27 +23,9 @@ function! ale#c#GetBuildDirectory(buffer) abort
return l:build_dir return l:build_dir
endif endif
return ale#path#Dirname(ale#c#FindCompileCommands(a:buffer)) let [l:root, l:json_file] = ale#c#FindCompileCommands(a:buffer)
endfunction
return ale#path#Dirname(l:json_file)
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 ''
endfunction endfunction
function! ale#c#AreSpecialCharsBalanced(option) abort function! ale#c#AreSpecialCharsBalanced(option) abort
@ -120,7 +102,7 @@ endfunction
function! ale#c#ParseCFlagsFromMakeOutput(buffer, make_output) abort function! ale#c#ParseCFlagsFromMakeOutput(buffer, make_output) abort
if !g:ale_c_parse_makefile if !g:ale_c_parse_makefile
return '' return v:null
endif endif
let l:buffer_filename = expand('#' . a:buffer . ':t') 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) return ale#c#ParseCFlags(l:makefile_dir, l:cflag_line)
endfunction endfunction
" Given a buffer number, find the build subdirectory with compile commands " Given a buffer number, find the project directory containing
" The subdirectory is returned without the trailing / " 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 function! ale#c#FindCompileCommands(buffer) abort
" Look above the current source file to find compile_commands.json " Look above the current source file to find compile_commands.json
let l:json_file = ale#path#FindNearestFile(a:buffer, 'compile_commands.json') let l:json_file = ale#path#FindNearestFile(a:buffer, 'compile_commands.json')
if !empty(l:json_file) if !empty(l:json_file)
return l:json_file return [fnamemodify(l:json_file, ':h'), l:json_file]
endif endif
" Search in build directories if we can't find it in the project. " 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' let l:json_file = l:c_build_dir . s:sep . 'compile_commands.json'
if filereadable(l:json_file) if filereadable(l:json_file)
return l:json_file return [l:path, l:json_file]
endif endif
endfor endfor
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 endfunction
" Cache compile_commands.json data in a Dictionary, so we don't need to read " 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:file_lookup = {}
let l:dir_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:basename = tolower(fnamemodify(l:entry.file, ':t'))
let l:file_lookup[l:basename] = get(l:file_lookup, l:basename, []) + [l:entry] 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 endfunction
function! ale#c#GetCFlags(buffer, output) abort 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) if ale#Var(a:buffer, 'c_parse_makefile') && !empty(a:output)
let l:cflags = ale#c#ParseCFlagsFromMakeOutput(a:buffer, a:output) let l:cflags = ale#c#ParseCFlagsFromMakeOutput(a:buffer, a:output)
endif endif
if ale#Var(a:buffer, 'c_parse_compile_commands') 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) if !empty(l:json_file)
let l:cflags = ale#c#FlagsFromCompileCommands(a:buffer, l:json_file) let l:cflags = ale#c#FlagsFromCompileCommands(a:buffer, l:json_file)
endif endif
endif endif
if l:cflags is# ' ' if l:cflags is v:null
let l:cflags = ale#c#IncludeOptions(ale#c#FindLocalHeaderPaths(a:buffer)) let l:cflags = ale#c#IncludeOptions(ale#c#FindLocalHeaderPaths(a:buffer))
endif endif
return l:cflags return l:cflags isnot v:null ? l:cflags : ''
endfunction endfunction
function! ale#c#GetMakeCommand(buffer) abort function! ale#c#GetMakeCommand(buffer) abort

View file

@ -3,15 +3,17 @@ scriptencoding utf-8
" Description: Utilities for ccls " Description: Utilities for ccls
function! ale#handlers#ccls#GetProjectRoot(buffer) abort 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) if empty(l:config)
let l:project_root = ale#path#FindNearestFile(a:buffer, 'compile_commands.json') let l:config = ale#path#FindNearestFile(a:buffer, '.ccls')
endif endif
if empty(l:project_root) if !empty(l:config)
let l:project_root = ale#path#FindNearestFile(a:buffer, '.ccls') return fnamemodify(l:config, ':h')
endif 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 endfunction

View file

@ -7,9 +7,9 @@ After:
Execute(The project root should be detected correctly using compile_commands.json file): Execute(The project root should be detected correctly using compile_commands.json file):
AssertLSPProject '' 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): Execute(The project root should be detected correctly using .cquery file):
AssertLSPProject '' AssertLSPProject ''

View file

@ -2,6 +2,7 @@ Before:
" Make sure the c.vim file is loaded first. " Make sure the c.vim file is loaded first.
call ale#c#FindProjectRoot(bufnr('')) call ale#c#FindProjectRoot(bufnr(''))
Save g:ale_c_parse_compile_commands
Save g:ale_c_parse_makefile Save g:ale_c_parse_makefile
Save g:__ale_c_project_filenames Save g:__ale_c_project_filenames
@ -14,6 +15,7 @@ Before:
\ 'v:val isnot# ''.git/HEAD''' \ 'v:val isnot# ''.git/HEAD'''
\) \)
let g:ale_c_parse_compile_commands = 0
let g:ale_c_parse_makefile = 0 let g:ale_c_parse_makefile = 0
After: After:

View file

@ -10,9 +10,9 @@ After:
Execute(The project root should be detected correctly using compile_commands.json file): Execute(The project root should be detected correctly using compile_commands.json file):
AssertLSPProject '' 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): Execute(The project root should be detected correctly using .cquery file):
AssertLSPProject '' AssertLSPProject ''