Last active
March 19, 2024 07:20
-
-
Save Konfekt/127b006ecd63025a4235a92f17e42484 to your computer and use it in GitHub Desktop.
:Make target completion in Vim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" From https://github.com/mg979/tasks.vim/pull/15/files | |
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
" Command-line completion for ":Make" | |
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
command! -bang -nargs=* -complete=customlist,MakeComplete Make silent make<bang> <args> | silent redraw! | |
command! -bang -nargs=* -complete=customlist,MakeComplete LMake silent lmake<bang> <args> | silent redraw! | |
if executable('awk') | |
let s:MakeCompletionCmd = "make -qp | awk -F':' '/^[a-zA-Z0-9._-][^$#\/\t=]*:([^=]|$)/ {split($1, targets, / /); for (target in targets) if (targets[target] != \"Makefile\" && !seen[targets[target]]++) print targets[target]}'" | |
if has('win32') && executable('sh') | |
let s:MakeCompletionCmd = 'sh -c ' .. shellescape(s:MakeCompletionCmd) | |
else | |
unlet s:MakeCompletionCmd | |
endif | |
endif | |
""============================================================================= | |
" Function: MakeComplete | |
" Command-line completion for ":Make" | |
" @param ...: command-completion-custom arguments | |
" Returns: list of completion items | |
""============================================================================= | |
if exists('s:MakeCompletionCmd') | |
" From https://dev.to/pbnj/how-to-get-make-target-tab-completion-in-vim-4mj1 | |
function! MakeComplete(ArgLead, CmdLine, CursorPos) abort | |
if &makeprg == 'make' && exists('s:MakeCompletionCmd') | |
" test if 'make' can actually do anything here | |
call system('make -q') | |
if v:shell_error == 2 | |
return [] | |
endif | |
let targets = systemlist(s:MakeCompletionCmd) | |
return filter(targets, 'v:val =~ "^' .. a:1 .. '" && v:val != "makefile"') | |
endif | |
return [] | |
endfunction | |
else | |
" From https://github.com/jiangyinzuo/vimrc/commit/29a7f3f4686c4ea8246c2f149f698fd01d7cdba4 | |
function! MakeComplete(ArgLead, CmdLine, CursorPos) abort | |
if &makeprg !=? 'make' | |
return [] | |
endif | |
let makefiles = glob('[Mm]akefile', 1, 1) + glob('GNUmakefile', 1, 1) + | |
\ glob('*.mk', 1, 1) | |
if !empty(makefiles) && filereadable(makefiles[0]) | |
let makefile = makefiles[0] | |
else | |
return [] | |
endif | |
let lines = readfile(makefile) | |
let targets = [] | |
for line in lines | |
if line =~ '^[a-zA-Z0-9._-]\+:' | |
let target = matchstr(line, '^[a-zA-Z0-9._-]\+') | |
if target =~ '^' . a:ArgLead | |
call add(targets, target) | |
endif | |
endif | |
endfor | |
return uniq(targets) | |
endfunction | |
endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment