Skip to content

Improved syntastic linting #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions doc/rust.txt
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,31 @@ g:rust_shortener_url~
let g:rust_shortener_url = 'https://is.gd/'
<

*g:rustc_syntax_only*
g:rustc_syntax_only~
Set this option to control the syntastic linter.
You can choose between a simple syntax parser or a fuller compilation.

The syntax parser is very fast, but won't catch any compilation
errors. This is the default.
We recommend you enable syntastic's active mode with this setting
(that's the default too). >
let g:rustc_syntax_only = 1
<

The fuller compilation performs a compilation of the entire project.
This makes it really slow on large projects.
If VIM becomes unusable, set syntastic passive mode for Rust. This
will make it so that the lint will only run on |:SyntasticCheck| (see
|syntastic_mode_map|).
>
let g:rustc_syntax_only = 0
" Syntastic passive mode
let g:syntastic_mode_map = {
\ "mode": "active",
\ "active_filetypes": [],
\ "passive_filetypes": ["rust"] }
<

==============================================================================
COMMANDS *rust-commands*
Expand Down
25 changes: 22 additions & 3 deletions syntax_checkers/rust/rustc.vim
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,29 @@ if exists("g:loaded_syntastic_rust_rustc_checker")
endif
let g:loaded_syntastic_rust_rustc_checker = 1

if !exists('g:rustc_syntax_only')
let g:rustc_syntax_only = 1 "Keep the fast behaviour by default
endif

let s:save_cpo = &cpo
set cpo&vim

function! SyntaxCheckers_rust_rustc_GetLocList() dict
let makeprg = self.makeprgBuild({ 'args': '-Zparse-only' })
let compiler_params = g:rustc_syntax_only ? '-Zparse-only' : '-Zno-trans'
let cwd = '.' " Don't change cwd as default
let cargo_toml_path = findfile('Cargo.toml', '.;')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this search in parent directories or only in current dir and subdirs? Thanks.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the current and parent directories until it finds the file.
See :help findfile()

if empty(cargo_toml_path) " Plain rs file, not a crate
let makeprg = self.makeprgBuild({
\ 'exe': 'rustc',
\ 'args': compiler_params})
else " We are inside a crate
let makeprg = self.makeprgBuild({
\ 'exe': 'cargo',
\ 'args': 'rustc ' . compiler_params,
\ 'fname': '' })
" Change cwd to the root of the crate
let cwd = fnamemodify( cargo_toml_path, ':p:h')
Copy link

@ghost ghost Jan 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line currently doesn't work inside a workspace, for it needs to find workspace's Cargo.toml (which is ../ relative to what it currently finds), so reported filenames in the errors are like:
--> recompile_self/build.rs:32:3
but cwd is
/home/xftroxgpx/build/2nonpkgs/rust.stuff/rustlearnage/recompile_self
when it should be
/home/xftroxgpx/build/2nonpkgs/rust.stuff/rustlearnage
for vim/syntastic to actually show the errors upon save.

I don't know of a way to detect if we're inside a workspace now...

EDIT: I opted to patch cargo to pass absolute path names to rustc so that they will show up as such in the error messages, and thus not need any cwd changes!

endif

let errorformat =
\ '%E%f:%l:%c: %\d%#:%\d%# %.%\{-}error:%.%\{-} %m,' .
Expand All @@ -24,12 +42,13 @@ function! SyntaxCheckers_rust_rustc_GetLocList() dict

return SyntasticMake({
\ 'makeprg': makeprg,
\ 'errorformat': errorformat })
\ 'errorformat': errorformat,
\ 'cwd': cwd })
endfunction

call g:SyntasticRegistry.CreateAndRegisterChecker({
\ 'filetype': 'rust',
\ 'name': 'rustc'})
\ 'name': 'rustc' })

let &cpo = s:save_cpo
unlet s:save_cpo