From 73f61514c9039e7e863da3544f251d3f8d7d1956 Mon Sep 17 00:00:00 2001 From: w0rp Date: Tue, 19 Dec 2017 18:10:29 +0000 Subject: [PATCH] Fix #1031 - Make the rust flags configurable --- ale_linters/rust/rustc.vim | 18 ++++++--- doc/ale-rust.txt | 13 +++++++ .../test_rustc_command_callback.vader | 37 +++++++++++++++++++ 3 files changed, 62 insertions(+), 6 deletions(-) create mode 100644 test/command_callback/test_rustc_command_callback.vader diff --git a/ale_linters/rust/rustc.vim b/ale_linters/rust/rustc.vim index e792faa7..3cd401b3 100644 --- a/ale_linters/rust/rustc.vim +++ b/ale_linters/rust/rustc.vim @@ -1,21 +1,27 @@ " Author: Daniel Schemala " Description: rustc for rust files -function! ale_linters#rust#rustc#RustcCommand(buffer_number) abort +call ale#Set('rust_rustc_options', '-Z no-trans') + +function! ale_linters#rust#rustc#RustcCommand(buffer) abort " Try to guess the library search path. If the project is managed by cargo, " it's usually /target/debug/deps/ or " /target/release/deps/ - let l:cargo_file = ale#path#FindNearestFile(a:buffer_number, 'Cargo.toml') + let l:cargo_file = ale#path#FindNearestFile(a:buffer, 'Cargo.toml') if l:cargo_file isnot# '' - let l:project_root = fnamemodify(l:cargo_file, ':h') - let l:dependencies = '-L ' . l:project_root . '/target/debug/deps -L ' . - \ l:project_root . '/target/release/deps' + let l:root = fnamemodify(l:cargo_file, ':h') + let l:dependencies = ' -L ' . ale#Escape(ale#path#GetAbsPath(l:root, 'target/debug/deps')) + \ . ' -L ' . ale#Escape(ale#path#GetAbsPath(l:root, 'target/release/deps')) else let l:dependencies = '' endif - return 'rustc --error-format=json -Z no-trans ' . l:dependencies . ' -' + let l:options = ale#Var(a:buffer, 'rust_rustc_options') + + return 'rustc --error-format=json' + \ . (!empty(l:options) ? ' ' . l:options : '') + \ . l:dependencies . ' -' endfunction call ale#linter#Define('rust', { diff --git a/doc/ale-rust.txt b/doc/ale-rust.txt index 64d5293c..535f21d6 100644 --- a/doc/ale-rust.txt +++ b/doc/ale-rust.txt @@ -84,6 +84,19 @@ g:ale_rust_rls_toolchain *g:ale_rust_rls_toolchain* =============================================================================== rustc *ale-rust-rustc* + +g:ale_rust_rustc_options *g:ale_rust_rustc_options* + *b:ale_rust_rustc_options* + Type: |String| + Default: `'-Z no-trans'` + + The variable can be used to change the options passed to `rustc`. + + `-Z no-trans` should only work with nightly builds of Rust. Be careful when + setting the options, as running `rustc` could execute code or generate + binary files. + + g:ale_rust_ignore_error_codes *g:ale_rust_ignore_error_codes* *b:ale_rust_ignore_error_codes* Type: |List| of |String|s diff --git a/test/command_callback/test_rustc_command_callback.vader b/test/command_callback/test_rustc_command_callback.vader new file mode 100644 index 00000000..fe46c9ad --- /dev/null +++ b/test/command_callback/test_rustc_command_callback.vader @@ -0,0 +1,37 @@ +Before: + Save g:ale_rust_rustc_options + + unlet! g:ale_rust_rustc_options + + runtime ale_linters/rust/rustc.vim + call ale#test#SetDirectory('/testplugin/test/command_callback') + +After: + Restore + + unlet! b:ale_rust_rustc_options + + call ale#test#RestoreDirectory() + call ale#linter#Reset() + +Execute(The default command should be correct): + AssertEqual + \ 'rustc --error-format=json -Z no-trans -', + \ ale_linters#rust#rustc#RustcCommand(bufnr('')) + +Execute(The options should be configurable): + let b:ale_rust_rustc_options = '--foo' + + AssertEqual + \ 'rustc --error-format=json --foo -', + \ ale_linters#rust#rustc#RustcCommand(bufnr('')) + +Execute(Some default paths should be included when the project is a Cargo project): + call ale#test#SetFilename('cargo_paths/test.rs') + + AssertEqual + \ 'rustc --error-format=json -Z no-trans' + \ . ' -L ' . ale#Escape(ale#path#GetAbsPath(g:dir, 'cargo_paths/target/debug/deps')) + \ . ' -L ' . ale#Escape(ale#path#GetAbsPath(g:dir, 'cargo_paths/target/release/deps')) + \ . ' -', + \ ale_linters#rust#rustc#RustcCommand(bufnr(''))