Skip to content

Commit d9f0d95

Browse files
Paul Woolcockda-x
Paul Woolcock
authored andcommitted
Add cargo#guessrun function & Cgrun command
This command attempts to guess which target you want to run, based on the current buffer. If it is a bin target, it will run `cargo run --bin <name> a:args`, an example target would be `cargo run --example <name> a:args`, and if it is neither of these, or the function can't be sure what it is, it falls back to just `cargo run a:args`
1 parent 039b7c7 commit d9f0d95

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

autoload/cargo.vim

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,32 @@ function! cargo#bench(args)
8484
call cargo#cmd("bench " . a:args)
8585
endfunction
8686

87+
function! cargo#runtarget(args)
88+
let l:filename = expand('%:p')
89+
let l:read_manifest = system('cargo read-manifest')
90+
let l:metadata = json_decode(l:read_manifest)
91+
let l:targets = get(l:metadata, 'targets', [])
92+
let l:did_run = 0
93+
for l:target in l:targets
94+
let l:src_path = get(l:target, 'src_path', '')
95+
let l:kinds = get(l:target, 'kind', [])
96+
let l:name = get(l:target, 'name', '')
97+
if l:src_path == l:filename
98+
if index(l:kinds, 'example') != -1
99+
let l:did_run = 1
100+
call cargo#run("--example " . shellescape(l:name) . " " . a:args)
101+
return
102+
elseif index(l:kinds, 'bin') != -1
103+
let l:did_run = 1
104+
call cargo#run("--bin " . shellescape(l:name) . " " . a:args)
105+
return
106+
endif
107+
endif
108+
endfor
109+
if l:did_run != 1
110+
call cargo#run(a:args)
111+
return
112+
endif
113+
endfunction
114+
87115
" vim: set et sw=4 sts=4 ts=8:

doc/rust.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,9 @@ This plug defines very simple shortcuts for invoking Cargo from with Vim.
307307
:Cinstall <args> *:Cinstall*
308308
Shortcut for 'cargo install`.
309309

310+
:Cruntarget <args> *:Cruntarget*
311+
Shortcut for 'cargo run --target' or 'cargo run --example',
312+
depending on the currently open buffer.
310313

311314
Formatting
312315
----------

plugin/cargo.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ command! -nargs=* Cupdate call cargo#update(<q-args>)
1818
command! -nargs=* Csearch call cargo#search(<q-args>)
1919
command! -nargs=* Cpublish call cargo#publish(<q-args>)
2020
command! -nargs=* Cinstall call cargo#install(<q-args>)
21+
command! -nargs=* Cruntarget call cargo#runtarget(<q-args>)
2122

2223
let &cpoptions = s:save_cpo
2324
unlet s:save_cpo

0 commit comments

Comments
 (0)