Skip to content

Commit c6ffafc

Browse files
committed
Set proper current directory for quickfix with 'cargo' compiler
The buffer-local current directory of the quickfix window should match the location of the Cargo.toml file. Fixes rust-lang#230.
1 parent ce8e0f7 commit c6ffafc

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

autoload/cargo/quickfix.vim

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function! cargo#quickfix#CmdPre() abort
2+
if &filetype ==# 'rust' && get(b:, 'current_compiler', '') ==# 'cargo'
3+
" Preserve the current directory, and 'lcd' to the nearest Cargo file.
4+
let b:rust_compiler_cargo_qf_has_lcd = haslocaldir()
5+
let b:rust_compiler_cargo_qf_prev_cd = getcwd()
6+
let b:rust_compiler_cargo_qf_prev_cd_saved = 1
7+
let l:nearest = fnamemodify(cargo#nearestRootCargo(0), ':h')
8+
execute 'lchdir! '.l:nearest
9+
else
10+
let b:rust_compiler_cargo_qf_prev_cd_saved = 0
11+
endif
12+
endfunction
13+
14+
function! cargo#quickfix#CmdPost() abort
15+
if b:rust_compiler_cargo_qf_prev_cd_saved
16+
" Restore the current directory.
17+
if b:rust_compiler_cargo_qf_has_lcd
18+
execute 'lchdir! '.b:rust_compiler_cargo_qf_prev_cd
19+
else
20+
execute 'chdir! '.b:rust_compiler_cargo_qf_prev_cd
21+
endif
22+
let b:rust_compiler_cargo_qf_prev_cd_saved = 0
23+
endif
24+
endfunction
25+
26+
" vim: set et sw=4 sts=4 ts=8:

compiler/cargo.vim

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ else
2323
CompilerSet makeprg=cargo\ $*
2424
endif
2525

26+
augroup RustCargoQuickFixHooks
27+
autocmd!
28+
autocmd QuickFixCmdPre make call cargo#quickfix#CmdPre()
29+
autocmd QuickFixCmdPost make call cargo#quickfix#CmdPost()
30+
augroup END
31+
2632
" Ignore general cargo progress messages
2733
CompilerSet errorformat+=
2834
\%-G%\\s%#Downloading%.%#,

0 commit comments

Comments
 (0)