From 0961c1331693a25056e9b236d8a0ccefd323fb5c Mon Sep 17 00:00:00 2001 From: "Kenneth Benzie (Benie)" Date: Wed, 14 Jul 2021 11:38:21 +0100 Subject: [PATCH] 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. --- .conduit.yaml | 1 + autoload/build.vim | 20 +++++++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/.conduit.yaml b/.conduit.yaml index aa6bf1a..8504548 100644 --- a/.conduit.yaml +++ b/.conduit.yaml @@ -4,6 +4,7 @@ - vim-vint - yamllint - cmakelint + - compdb - repo: - remote: https://github.com/k-takata/minpac.git location: ~/.vim/pack/minpac/opt/minpac diff --git a/autoload/build.vim b/autoload/build.vim index 10f5e20..769f1ea 100644 --- a/autoload/build.vim +++ b/autoload/build.vim @@ -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