Skip to content

Instantly share code, notes, and snippets.

@bstaint
Last active November 28, 2019 05:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bstaint/7834e0efedc4d1467f8e8d2dd8760a19 to your computer and use it in GitHub Desktop.
Save bstaint/7834e0efedc4d1467f8e8d2dd8760a19 to your computer and use it in GitHub Desktop.
" Depends
" asyncrun.vim
" vim-ingo-library
"
" example .vimproject
" let $VIRTUAL_ENV = ingo#fs#path#Combine(expand("%:p:h"), '.venv')
" function RunnerMain()
" " python3 -m unittest discover -v -s tests -p "*_test.py"
" return 'python app.py'
" endfunction
if exists("loaded_runner_plugin")
finish
endif
let loaded_runner_plugin = 1
function! RunnerPathAppend(path)
if ingo#str#find#StartIndex($PATH, a:path) == -1
let $PATH = a:path.';'.$PATH
endif
endfunction
" Load vimproject
function! s:RunnerLoad()
if filereadable(".vimproject")
source .vimproject
" echom "Load vimproject"
if exists("$VIRTUAL_ENV")
call RunnerPathAppend(ingo#fs#path#Combine($VIRTUAL_ENV, 'Scripts'))
endif
endif
endfunction
function! s:RunnerInput()
let cmdlist = RunnerMain()
if type(cmdlist) == type([])
let idx = inputlist(["select run command:"] +
\ map(copy(cmdlist), {k,v -> (k+1).'. '.v})) - 1
if idx < 0 || idx >= len(cmdlist)
throw 'UnknownIndex'
endif
let command = get(cmdlist, idx)
return command
elseif type(cmdlist) == type("")
return cmdlist
else
throw 'UnknownType'
endif
endfunction
" Run script
function! s:Runner()
let command = 'AsyncRun -raw -program=make %'
if exists("*RunnerMain")
try
let command = s:RunnerInput()
if command !=# ""
let command = 'AsyncRun -raw '.command
endif
catch /UnknownType/
call ingo#msg#ErrorMsg("Unknown RunnerMain type.")
catch /UnknownIndex/
endtry
endif
let status = !exists('g:asyncrun_status')
\ || g:asyncrun_status != 'running'
try
if status
silent exe command
silent 8copen
else
silent AsyncStop
endif
catch /E499/
" Ignore missing scripts
endtry
endfunction
call s:RunnerLoad()
command! -nargs=0 Runner call s:Runner()
command! -nargs=0 RunnerLoad call s:RunnerLoad()
nmap <silent> <Plug>Runner :call <SID>Runner()<CR>
if !hasmapto('<Plug>Runner', 'n')
nmap <F6> <Plug>Runner
endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment