Use compdb to post-process compile_commands.json

When selecting a `:BuildDir` for the `clangd` LSP use `compdb` to
post-process the `compile_commands.json` to also include flags for
stand alone header files.
This commit is contained in:
Kenneth Benzie 2021-07-14 11:38:21 +01:00
parent eabec4e9b6
commit 0961c13316
2 changed files with 18 additions and 3 deletions

View File

@ -4,6 +4,7 @@
- vim-vint
- yamllint
- cmakelint
- compdb
- repo:
- remote: https://github.com/k-takata/minpac.git
location: ~/.vim/pack/minpac/opt/minpac

View File

@ -28,6 +28,7 @@ function! build#dir(...) abort
endfor
let l:index = inputlist(l:choices)
let l:dir = s:dirs[l:index - 1]
echomsg ' '.l:dir
endif
endif
else
@ -48,17 +49,30 @@ function! build#dir(...) abort
endif
endif
if exists('l:dir')
" Set build directory and restart YouCompleteMe.
let $BUILD_DIR = getcwd().'/'.substitute(l:dir, '\/$', '', '')
" Set build directory.
let l:cwd = substitute(getcwd(), '\\', '\/', 'g')
let $BUILD_DIR = l:cwd.'/'.substitute(l:dir, '\/$', '', '')
if executable('compdb')
" Post-process compile_commands.json with compdb, adds header files to
" missing compile_commands.json for more accurate diagnostics.
let l:database_dir = l:cwd.'/.vim'
let l:compile_commands = l:database_dir.'/compile_commands.json'
call systemlist('compdb -p '.$BUILD_DIR.' list > '.l:compile_commands)
else
let l:database_dir = $BUILD_DIR
endif
" Read/create .vim/coc-settings.json
let l:coc_settings = {}
if isdirectory('.vim')
let l:coc_settings = json_decode(join(readfile('.vim/coc-settings.json'), ''))
else
call mkdir('.vim')
endif
let l:coc_settings['clangd.compilationDatabasePath'] = $BUILD_DIR
" Update .vim/coc-settings.json with new build directory.
let l:coc_settings['clangd.compilationDatabasePath'] = l:database_dir
let l:coc_settings['cmake.lsp.buildDirectory'] = $BUILD_DIR
call writefile([json_encode(l:coc_settings)], '.vim/coc-settings.json')
" Finally restart coc.nvim with new config.
CocRestart
endif
endfunction