Skip to content

Commit f20b8b4

Browse files
committed
Detect standalone rs vs a crate
1 parent 7ade8c3 commit f20b8b4

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

syntax_checkers/rust/rustc.vim

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,20 @@ let s:save_cpo = &cpo
1414
set cpo&vim
1515

1616
function! SyntaxCheckers_rust_rustc_GetLocList() dict
17-
let makeprg = self.makeprgBuild({
18-
\ 'args': 'rustc -Zno-trans',
19-
\ 'fname': '' })
17+
let cwd = '.' " Don't change cwd as default
18+
let cargo_toml_path = findfile('Cargo.toml', '.;')
19+
if empty(cargo_toml_path) " Plain rs file, not a crate
20+
let makeprg = self.makeprgBuild({
21+
\ 'exe': 'rustc',
22+
\ 'args': '-Zno-trans' })
23+
else " We are inside a crate
24+
let makeprg = self.makeprgBuild({
25+
\ 'exe': 'cargo',
26+
\ 'args': 'rustc -Zno-trans',
27+
\ 'fname': '' })
28+
" Change cwd to the root of the crate
29+
let cwd = fnamemodify( cargo_toml_path, ':p:h')
30+
endif
2031

2132
let errorformat =
2233
\ '%E%f:%l:%c: %\d%#:%\d%# %.%\{-}error:%.%\{-} %m,' .
@@ -26,13 +37,13 @@ function! SyntaxCheckers_rust_rustc_GetLocList() dict
2637

2738
return SyntasticMake({
2839
\ 'makeprg': makeprg,
29-
\ 'errorformat': errorformat })
40+
\ 'errorformat': errorformat,
41+
\ 'cwd': cwd })
3042
endfunction
3143

3244
call g:SyntasticRegistry.CreateAndRegisterChecker({
33-
\ 'exec': 'cargo',
3445
\ 'filetype': 'rust',
35-
\ 'name': 'rustc'})
46+
\ 'name': 'rustc' })
3647

3748
let &cpo = s:save_cpo
3849
unlet s:save_cpo

0 commit comments

Comments
 (0)