2020-04-25 21:56:16 -04:00
|
|
|
function! cargo#Load()
|
2019-11-16 12:43:18 -05:00
|
|
|
" Utility call to get this script loaded, for debugging
|
|
|
|
endfunction
|
|
|
|
|
2020-04-25 21:56:16 -04:00
|
|
|
function! cargo#cmd(args) abort
|
2019-11-30 07:06:56 -05:00
|
|
|
" Trim trailing spaces. This is necessary since :terminal command parses
|
|
|
|
" trailing spaces as an empty argument.
|
|
|
|
let args = substitute(a:args, '\s\+$', '', '')
|
2020-04-25 21:56:16 -04:00
|
|
|
if exists('g:cargo_shell_command_runner')
|
|
|
|
let cmd = g:cargo_shell_command_runner
|
|
|
|
elseif has('terminal')
|
2019-11-30 07:06:56 -05:00
|
|
|
let cmd = 'terminal'
|
|
|
|
elseif has('nvim')
|
|
|
|
let cmd = 'noautocmd new | terminal'
|
|
|
|
else
|
|
|
|
let cmd = '!'
|
|
|
|
endif
|
|
|
|
execute cmd 'cargo' args
|
2019-11-16 12:43:18 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:nearest_cargo(...) abort
|
|
|
|
" If the second argument is not specified, the first argument determines
|
|
|
|
" whether we will start from the current directory or the directory of the
|
|
|
|
" current buffer, otherwise, we start with the provided path on the
|
|
|
|
" second argument.
|
|
|
|
|
|
|
|
let l:is_getcwd = get(a:, 1, 0)
|
|
|
|
if l:is_getcwd
|
|
|
|
let l:starting_path = get(a:, 2, getcwd())
|
2019-11-16 11:14:34 -05:00
|
|
|
else
|
2019-11-16 12:43:18 -05:00
|
|
|
let l:starting_path = get(a:, 2, expand('%:p:h'))
|
2019-11-16 11:14:34 -05:00
|
|
|
endif
|
2019-11-16 12:43:18 -05:00
|
|
|
|
|
|
|
return findfile('Cargo.toml', l:starting_path . ';')
|
2019-11-16 11:14:34 -05:00
|
|
|
endfunction
|
|
|
|
|
2019-11-16 12:43:18 -05:00
|
|
|
function! cargo#nearestCargo(is_getcwd) abort
|
|
|
|
return s:nearest_cargo(a:is_getcwd)
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! cargo#nearestWorkspaceCargo(is_getcwd) abort
|
|
|
|
let l:nearest = s:nearest_cargo(a:is_getcwd)
|
|
|
|
while l:nearest !=# ''
|
|
|
|
for l:line in readfile(l:nearest, '', 0x100)
|
|
|
|
if l:line =~# '\V[workspace]'
|
|
|
|
return l:nearest
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
let l:next = fnamemodify(l:nearest, ':p:h:h')
|
|
|
|
let l:nearest = s:nearest_cargo(0, l:next)
|
|
|
|
endwhile
|
|
|
|
return ''
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! cargo#nearestRootCargo(is_getcwd) abort
|
|
|
|
" Try to find a workspace Cargo.toml, and if not found, take the nearest
|
|
|
|
" regular Cargo.toml
|
|
|
|
let l:workspace_cargo = cargo#nearestWorkspaceCargo(a:is_getcwd)
|
|
|
|
if l:workspace_cargo !=# ''
|
|
|
|
return l:workspace_cargo
|
2019-11-16 11:14:34 -05:00
|
|
|
endif
|
2019-11-16 12:43:18 -05:00
|
|
|
return s:nearest_cargo(a:is_getcwd)
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
function! cargo#build(args)
|
|
|
|
call cargo#cmd("build " . a:args)
|
2019-11-16 11:14:34 -05:00
|
|
|
endfunction
|
|
|
|
|
2020-01-28 21:07:36 -05:00
|
|
|
function! cargo#check(args)
|
|
|
|
call cargo#cmd("check " . a:args)
|
|
|
|
endfunction
|
|
|
|
|
2019-11-16 11:14:34 -05:00
|
|
|
function! cargo#clean(args)
|
2019-11-16 12:43:18 -05:00
|
|
|
call cargo#cmd("clean " . a:args)
|
2019-11-16 11:14:34 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! cargo#doc(args)
|
2019-11-16 12:43:18 -05:00
|
|
|
call cargo#cmd("doc " . a:args)
|
2019-11-16 11:14:34 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! cargo#new(args)
|
2019-11-16 12:43:18 -05:00
|
|
|
call cargo#cmd("new " . a:args)
|
|
|
|
cd `=a:args`
|
2019-11-16 11:14:34 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! cargo#init(args)
|
2019-11-16 12:43:18 -05:00
|
|
|
call cargo#cmd("init " . a:args)
|
2019-11-16 11:14:34 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! cargo#run(args)
|
2019-11-16 12:43:18 -05:00
|
|
|
call cargo#cmd("run " . a:args)
|
2019-11-16 11:14:34 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! cargo#test(args)
|
2019-11-16 12:43:18 -05:00
|
|
|
call cargo#cmd("test " . a:args)
|
2019-11-16 11:14:34 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! cargo#bench(args)
|
2019-11-16 12:43:18 -05:00
|
|
|
call cargo#cmd("bench " . a:args)
|
|
|
|
endfunction
|
|
|
|
|
2020-04-25 21:56:16 -04:00
|
|
|
function! cargo#update(args)
|
|
|
|
call cargo#cmd("update " . a:args)
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! cargo#search(args)
|
|
|
|
call cargo#cmd("search " . a:args)
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! cargo#publish(args)
|
|
|
|
call cargo#cmd("publish " . a:args)
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! cargo#install(args)
|
|
|
|
call cargo#cmd("install " . a:args)
|
|
|
|
endfunction
|
|
|
|
|
2019-11-16 12:43:18 -05:00
|
|
|
function! cargo#runtarget(args)
|
|
|
|
let l:filename = expand('%:p')
|
|
|
|
let l:read_manifest = system('cargo read-manifest')
|
|
|
|
let l:metadata = json_decode(l:read_manifest)
|
|
|
|
let l:targets = get(l:metadata, 'targets', [])
|
|
|
|
let l:did_run = 0
|
|
|
|
for l:target in l:targets
|
|
|
|
let l:src_path = get(l:target, 'src_path', '')
|
|
|
|
let l:kinds = get(l:target, 'kind', [])
|
|
|
|
let l:name = get(l:target, 'name', '')
|
|
|
|
if l:src_path == l:filename
|
|
|
|
if index(l:kinds, 'example') != -1
|
|
|
|
let l:did_run = 1
|
|
|
|
call cargo#run("--example " . shellescape(l:name) . " " . a:args)
|
|
|
|
return
|
|
|
|
elseif index(l:kinds, 'bin') != -1
|
|
|
|
let l:did_run = 1
|
|
|
|
call cargo#run("--bin " . shellescape(l:name) . " " . a:args)
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
if l:did_run != 1
|
|
|
|
call cargo#run(a:args)
|
|
|
|
return
|
2019-11-16 11:14:34 -05:00
|
|
|
endif
|
|
|
|
endfunction
|
2019-11-16 12:43:18 -05:00
|
|
|
|
|
|
|
" vim: set et sw=4 sts=4 ts=8:
|