From 80dcd648d389965603246c2c5a4554e3e4aa184c Mon Sep 17 00:00:00 2001 From: Felix Maurer Date: Wed, 23 Mar 2022 02:56:29 +0100 Subject: [PATCH] rust-analyzer for non-Cargo projects (#4118) * rust-analyzer in non-cargo projects rust-analyzer can also be used in non-cargo projects. This requires a rust-project.json file in the project root [1]. Make the rust-analyzer linter search for a rust-project.json file if no Cargo.toml file could be found. [1]: https://rust-analyzer.github.io/manual.html#non-cargo-based-projects * Document rust-analyzer without cargo * Test rust-analyzer with non-cargo projects Change the other rust tests to match the new directory structure of the test files. --- ale_linters/rust/analyzer.vim | 14 +++++++++++++- doc/ale-rust.txt | 6 ++++-- test/linter/test_rust_analyzer.vader | 13 ++++++++++--- test/linter/test_rust_rls.vader | 4 ++-- test/linter/test_rustc.vader | 6 +++--- test/test-files/rust/{ => cargo}/Cargo.toml | 0 test/test-files/rust/{ => cargo}/testfile.rs | 0 .../test-files/rust/rust-project/rust-project.json | 0 test/test-files/rust/rust-project/testfile.rs | 0 9 files changed, 32 insertions(+), 11 deletions(-) rename test/test-files/rust/{ => cargo}/Cargo.toml (100%) rename test/test-files/rust/{ => cargo}/testfile.rs (100%) create mode 100644 test/test-files/rust/rust-project/rust-project.json create mode 100644 test/test-files/rust/rust-project/testfile.rs diff --git a/ale_linters/rust/analyzer.vim b/ale_linters/rust/analyzer.vim index 77d946f7..3ead3871 100644 --- a/ale_linters/rust/analyzer.vim +++ b/ale_linters/rust/analyzer.vim @@ -9,9 +9,21 @@ function! ale_linters#rust#analyzer#GetCommand(buffer) abort endfunction function! ale_linters#rust#analyzer#GetProjectRoot(buffer) abort + " Try to find nearest Cargo.toml for cargo projects let l:cargo_file = ale#path#FindNearestFile(a:buffer, 'Cargo.toml') - return !empty(l:cargo_file) ? fnamemodify(l:cargo_file, ':h') : '' + if !empty(l:cargo_file) + return fnamemodify(l:cargo_file, ':h') + endif + + " Try to find nearest rust-project.json for non-cargo projects + let l:rust_project = ale#path#FindNearestFile(a:buffer, 'rust-project.json') + + if !empty(l:rust_project) + return fnamemodify(l:rust_project, ':h') + endif + + return '' endfunction call ale#linter#Define('rust', { diff --git a/doc/ale-rust.txt b/doc/ale-rust.txt index 8e846844..c8ec9b13 100644 --- a/doc/ale-rust.txt +++ b/doc/ale-rust.txt @@ -26,8 +26,10 @@ Integration Information 4. analyzer -- If you have rust-analyzer installed, you might prefer using this linter over cargo and rls. rust-analyzer also implements the Language Server Protocol for incremental compilation of Rust code, and is - the next iteration of rls. rust-analyzer, like rls, requires Rust files - to be contained in Cargo projects. + the next iteration of rls. rust-analyzer either requires Rust files to be + contained in Cargo projects or requires the project to be described in + the rust-project.json format: + https://rust-analyzer.github.io/manual.html#non-cargo-based-projects 5. rustfmt -- If you have `rustfmt` installed, you can use it as a fixer to consistently reformat your Rust code. diff --git a/test/linter/test_rust_analyzer.vader b/test/linter/test_rust_analyzer.vader index 82a3adfb..2ee996c7 100644 --- a/test/linter/test_rust_analyzer.vader +++ b/test/linter/test_rust_analyzer.vader @@ -7,12 +7,19 @@ After: Execute(The default executable path should be correct): AssertLinter 'rust-analyzer', ale#Escape('rust-analyzer') -Execute(The project root should be detected correctly): +Execute(The project root should be detected correctly in cargo projects): AssertLSPProject '' - call ale#test#SetFilename('../test-files/rust/test.rs') + call ale#test#SetFilename('../test-files/rust/cargo/testfile.rs') - AssertLSPProject ale#path#Simplify(g:dir . '/../test-files/rust') + AssertLSPProject ale#path#Simplify(g:dir . '/../test-files/rust/cargo') + +Execute(The project root should be detected correctly in non-cargo projects): + AssertLSPProject '' + + call ale#test#SetFilename('../test-files/rust/rust-project/testfile.rs') + + AssertLSPProject ale#path#Simplify(g:dir . '/../test-files/rust/rust-project') Execute(Should accept configuration settings): AssertLSPConfig {} diff --git a/test/linter/test_rust_rls.vader b/test/linter/test_rust_rls.vader index 9ca25619..0b684c52 100644 --- a/test/linter/test_rust_rls.vader +++ b/test/linter/test_rust_rls.vader @@ -20,9 +20,9 @@ Execute(The toolchain should be ommitted if not given): Execute(The project root should be detected correctly): AssertLSPProject '' - call ale#test#SetFilename('../test-files/rust/test.rs') + call ale#test#SetFilename('../test-files/rust/cargo/testfile.rs') - AssertLSPProject ale#path#Simplify(g:dir . '/../test-files/rust') + AssertLSPProject ale#path#Simplify(g:dir . '/../test-files/rust/cargo') Execute(Should accept configuration settings): AssertLSPConfig {} diff --git a/test/linter/test_rustc.vader b/test/linter/test_rustc.vader index 41e3c735..37b7a8c2 100644 --- a/test/linter/test_rustc.vader +++ b/test/linter/test_rustc.vader @@ -13,9 +13,9 @@ Execute(The options should be configurable): AssertLinter 'rustc', 'rustc --error-format=json --foo -' Execute(Some default paths should be included when the project is a Cargo project): - call ale#test#SetFilename('../test-files/cargo/test.rs') + call ale#test#SetFilename('../test-files/rust/cargo/testfile.rs') AssertLinter 'rustc', 'rustc --error-format=json --emit=mir -o /dev/null' - \ . ' -L ' . ale#Escape(ale#path#GetAbsPath(g:dir, '../test-files/cargo/target/debug/deps')) - \ . ' -L ' . ale#Escape(ale#path#GetAbsPath(g:dir, '../test-files/cargo/target/release/deps')) + \ . ' -L ' . ale#Escape(ale#path#GetAbsPath(g:dir, '../test-files/rust/cargo/target/debug/deps')) + \ . ' -L ' . ale#Escape(ale#path#GetAbsPath(g:dir, '../test-files/rust/cargo/target/release/deps')) \ . ' -' diff --git a/test/test-files/rust/Cargo.toml b/test/test-files/rust/cargo/Cargo.toml similarity index 100% rename from test/test-files/rust/Cargo.toml rename to test/test-files/rust/cargo/Cargo.toml diff --git a/test/test-files/rust/testfile.rs b/test/test-files/rust/cargo/testfile.rs similarity index 100% rename from test/test-files/rust/testfile.rs rename to test/test-files/rust/cargo/testfile.rs diff --git a/test/test-files/rust/rust-project/rust-project.json b/test/test-files/rust/rust-project/rust-project.json new file mode 100644 index 00000000..e69de29b diff --git a/test/test-files/rust/rust-project/testfile.rs b/test/test-files/rust/rust-project/testfile.rs new file mode 100644 index 00000000..e69de29b