Skip to content

Commit d8af469

Browse files
committed
auto merge of #17459 : dradtke/rust/master, r=brson
This PR adds a new Vim compiler file specifically for use with Cargo. It passes all arguments through, so commands like `:make build`, `:make clean`, and `:make run` all work as expected. It also adds a quickfix autocommand for fixing the paths before populating the error list. `cargo build` reports errors with file paths that are relative to Cargo.toml, so if you're further down in the project tree, then trying to open the error will result in a blank buffer because Vim treats that path as relative to the working directory instead. With this fix, the paths work properly no matter where you are in the project.
2 parents 9cce2b7 + 59e750f commit d8af469

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

src/etc/vim/compiler/cargo.vim

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
" Vim compiler file
2+
" Compiler: Cargo Compiler
3+
" Maintainer: Damien Radtke <[email protected]>
4+
" Latest Revision: 2014 Sep 18
5+
6+
if exists("current_compiler")
7+
finish
8+
endif
9+
let current_compiler = "cargo"
10+
11+
if exists(":CompilerSet") != 2
12+
command -nargs=* CompilerSet setlocal <args>
13+
endif
14+
15+
CompilerSet errorformat&
16+
CompilerSet makeprg=cargo\ $*
17+
18+
" Allow a configurable global Cargo.toml name. This makes it easy to
19+
" support variations like 'cargo.toml'.
20+
if !exists('g:cargo_toml_name')
21+
let g:cargo_toml_name = 'Cargo.toml'
22+
endif
23+
24+
let s:toml_dir = fnamemodify(findfile(g:cargo_toml_name, '.;'), ':p:h').'/'
25+
26+
if s:toml_dir != ''
27+
augroup cargo
28+
au!
29+
au QuickfixCmdPost make call s:FixPaths()
30+
augroup END
31+
32+
" FixPaths() is run after Cargo, and is used to change the file paths
33+
" to be relative to the current directory instead of Cargo.toml.
34+
function! s:FixPaths()
35+
let qflist = getqflist()
36+
for qf in qflist
37+
if !qf['valid']
38+
continue
39+
endif
40+
let filename = bufname(qf['bufnr'])
41+
if stridx(filename, s:toml_dir) == -1
42+
let filename = s:toml_dir.filename
43+
endif
44+
let qf['filename'] = simplify(s:toml_dir.bufname(qf['bufnr']))
45+
call remove(qf, 'bufnr')
46+
endfor
47+
call setqflist(qflist, 'r')
48+
endfunction
49+
endif

0 commit comments

Comments
 (0)