Add :Build command to invoke ninja or make

The `:Build [<target> ...]` command utilises the `$BUILD_DIR` variable set
by `:BuildDir {directory}` to invoke `ninja` or `make` based on the
existence of `$BUILD_DIR/build.ninja` or `$BUILD_DIR/Makefile`
respectively. The following commands are invoked in a new `:terminal`
window.

* `ninja -C $BUILD_DIR [<target> ...]` when `$BUILD_DIR/build.ninja` exists
* `make -C $BUILD_DIR [<target> ...]` when `$BUILD_DIR/Makefile` exists

In the case of `ninja`, completion for the targets supplied to the
`:Build` command is made available by the `build#targets()` function.
The list of targets returned by `ninja -C $BUILD_DIR -t targets` is
processed to generate the list of targets. Support for `make` target
completion is less straight forwards so has been omitted for now.
This commit is contained in:
Kenneth Benzie 2021-01-05 21:13:25 +00:00
parent 0cd93f1823
commit a0df92852a
2 changed files with 22 additions and 0 deletions

View File

@ -4,3 +4,24 @@ function! build#dir(dir) abort
let g:ycm_clangd_args = ['--compile-commands-dir='.$BUILD_DIR]
YcmRestartServer
endfunction
function! build#targets(ArgLead, CmdLine, CursorPos) abort
let l:targets = []
if filereadable($BUILD_DIR.'/build.ninja')
for l:target in split(system('ninja -C '.$BUILD_DIR.' -t targets'), '\n')
call add(l:targets, substitute(l:target, ':.*$', '', ''))
endfor
elseif filereadable($BUILD_DIR.'/Makefile')
" TODO: support make
endif
return join(l:targets, "\n")
endfunction
function! build#run(...) abort
let l:build_dir = substitute($BUILD_DIR, '\/$', '', '')
if filereadable($BUILD_DIR.'/build.ninja')
execute 'terminal ninja -C '.l:build_dir.' '.join(a:000, ' ')
elseif filereadable($BUILD_DIR.'/Makefile')
execute 'terminal make -C '.l:build_dir.' '.join(a:000, ' ')
endif
endfunction

View File

@ -32,3 +32,4 @@ command! TodoFile lvimgrep /todo/ %
" Change build directory
command! -nargs=1 -complete=dir BuildDir call build#dir(<f-args>)
command! -nargs=* -complete=custom,build#targets Build call build#run(<f-args>)