vim/autoload/build.vim
Kenneth Benzie (Benie) a0df92852a 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.
2021-01-05 21:45:43 +00:00

28 lines
918 B
VimL

function! build#dir(dir) abort
let l:cwd = getcwd()
let $BUILD_DIR = l:cwd.'/'.a:dir
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