Skip to content
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

Add support for rust-analyzer #2837

Merged
merged 10 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from 9 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
24 changes: 24 additions & 0 deletions ale_linters/rust/analyzer.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
" Author: Jon Gjengset <jon@thesquareplanet.com>
" Description: The next generation language server for Rust

call ale#Set('rust_analyzer_executable', 'rust-analyzer')
call ale#Set('rust_analyzer_config', {})

function! ale_linters#rust#analyzer#GetCommand(buffer) abort
return '%e'
endfunction

function! ale_linters#rust#analyzer#GetProjectRoot(buffer) abort
let l:cargo_file = ale#path#FindNearestFile(a:buffer, 'Cargo.toml')

return !empty(l:cargo_file) ? fnamemodify(l:cargo_file, ':h') : ''
endfunction

call ale#linter#Define('rust', {
\ 'name': 'analyzer',
\ 'lsp': 'stdio',
\ 'lsp_config': {b -> ale#Var(b, 'rust_analyzer_config')},
\ 'executable': {b -> ale#Var(b, 'rust_analyzer_executable')},
\ 'command': function('ale_linters#rust#analyzer#GetCommand'),
\ 'project_root': function('ale_linters#rust#analyzer#GetProjectRoot'),
\})
28 changes: 26 additions & 2 deletions doc/ale-rust.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Integration Information
files for Rust distributed in Vim >=8.0.0501 or upstream:
https://github.com/rust-lang/rust.vim

Note that there are three possible linters for Rust files:
Note that there are several possible linters and fixers for Rust files:

1. rustc -- The Rust compiler is used to check the currently edited file.
So, if your project consists of multiple files, you will get some errors
Expand All @@ -23,7 +23,12 @@ Integration Information
over cargo. rls implements the Language Server Protocol for incremental
compilation of Rust code, and can check Rust files while you type. `rls`
requires Rust files to contained in Cargo projects.
4. rustfmt -- If you have `rustfmt` installed, you can use it as a fixer to
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 contained in Cargo projects.
5. rustfmt -- If you have `rustfmt` installed, you can use it as a fixer to
consistently reformat your Rust code.

Only cargo is enabled by default. To switch to using rustc instead of cargo,
Expand All @@ -36,6 +41,25 @@ Integration Information
Also note that rustc 1.12. or later is needed.


===============================================================================
analyzer *ale-rust-analyzer*

g:ale_rust_analyzer_executable *g:ale_rust_analyzer_executable*
*b:ale_rust_analyzer_executable*
Type: |String|
Default: `'ra_lsp_server'`

This variable can be modified to change the executable path for
`rust-analyzer`.


g:ale_rust_analyzer_config *g:ale_rust_analyzer_config*
*b:ale_rust_analyzer_config*
Type: |Dictionary|
Default: `{}`

Dictionary with configuration settings for rust-analyzer.

===============================================================================
cargo *ale-rust-cargo*

Expand Down
1 change: 1 addition & 0 deletions doc/ale-supported-languages-and-tools.txt
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ Notes:
* Rust
* `cargo`!!
* `rls`
* `rust-analyzer`
* `rustc` (see |ale-integration-rust|)
* `rustfmt`
* Sass
Expand Down
1 change: 1 addition & 0 deletions doc/ale.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2409,6 +2409,7 @@ documented in additional help files.
sorbet................................|ale-ruby-sorbet|
standardrb............................|ale-ruby-standardrb|
rust....................................|ale-rust-options|
analyzer..............................|ale-rust-analyzer|
cargo.................................|ale-rust-cargo|
rls...................................|ale-rust-rls|
rustc.................................|ale-rust-rustc|
Expand Down
1 change: 1 addition & 0 deletions supported-tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ formatting.
* Rust
* [cargo](https://github.com/rust-lang/cargo) :floppy_disk: (see `:help ale-integration-rust` for configuration instructions)
* [rls](https://github.com/rust-lang-nursery/rls) :warning:
* [rust-analyzer](https://github.com/rust-analyzer/rust-analyzer) :warning:
* [rustc](https://www.rust-lang.org/) :warning:
* [rustfmt](https://github.com/rust-lang-nursery/rustfmt)
* Sass
Expand Down
20 changes: 20 additions & 0 deletions test/command_callback/test_rust_analyzer_callbacks.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Before:
call ale#assert#SetUpLinterTest('rust', 'analyzer')

After:
call ale#assert#TearDownLinterTest()

Execute(The default executable path should be correct):
AssertLinter 'ra_lsp_server', ale#Escape('ra_lsp_server')
jonhoo marked this conversation as resolved.
Show resolved Hide resolved

Execute(The project root should be detected correctly):
AssertLSPProject ''

call ale#test#SetFilename('rust-rls-project/test.rs')

AssertLSPProject ale#path#Simplify(g:dir . '/rust-rls-project')

Execute(Should accept configuration settings):
AssertLSPConfig {}
let b:ale_rust_analyzer_config = {'rust': {'clippy_preference': 'on'}}
AssertLSPConfig {'rust': {'clippy_preference': 'on'}}