Browse Source

post on rust in neovim

master
Vincent Truchseß 4 months ago
parent
commit
74f75ec4b0
1 changed files with 151 additions and 0 deletions
  1. 151
    0
      posts/2019-04-28-rust-environment.md

+ 151
- 0
posts/2019-04-28-rust-environment.md View File

@@ -0,0 +1,151 @@
---
title: HowTo - A working Rust Development Environment with NeoVIM
author: VI
---

This is a short writeup on how to setup a Rust Environment in **neovim** that
provides:

* Autocompletion
* Live-linting
* Autoformatting
* Syntax-highlighting

Most of the documentation I used referred to **vim8** as well but I haven't
tested that.

## Installing the rust toolchain(s)

First, we need to install the rust installer **rustup**.
On Arch-Linux, rustup is packaged within the standard repositories. For other
Operating Systems just follow the rustup [installation
guide](https://www.rust-lang.org/tools/install).

Don't forget add ```~/.cargo/bin``` to your ```PATH``` variable.

Having the installer ready, we need to install the toolchains, sources and docs.
I want to use the ```stable``` toolchain to write code, but some of the
development tools are only available for the ```nightly``` toolchain so we're
simply going to install them both.

```bash
rustup toolchain install stable
rustup toolchain install nightly
rustup default stable
rustup component add rust-src
```

## Installing the Rust Language Server

**[RLS](https://github.com/rust-lang/rls)** provides a backend for IDEs/editors
to support their features for Rust. It is advised by it's documentation to
install it from the ```nightly``` toolchain. We'll use rustup to install it via

```bash
rustup component add rls --toolchain nightly
```

In my case this command failed with a message telling me that rls is not part of
the current ```nightly``` distribution. It also pointed me to have a look at
[this site](https://rust-lang.github.io/rustup-components-history/) to find the last
nightly build containing it.
There I found out which ```nightly-YYYY-MM-DD``` toolchain I need to use, so I
simply installed it.

```bash
rustup toolchain install nightly-YYYY-MM-DD
```

Now I could install rls via:

```bash
rustup component add rls --toolchain nightly-YYYY-MM-DD
```

## Installing racer

**[Racer](https://github.com/racer-rust/racer)** is a tool providing
autocompletion for rust's standard library.
We can simply install it via:

```bash
cargo +nightly install racer
```

Afterwards we have to set the ```RUST_SRC_PATH``` variable to
```$HOME/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src```.

## Installing rustfmt

**[rustfmt](https://github.com/rust-lang/rustfmt)** performs autoformatting on
rust-code. At first I installed just ```rustfmt``` via cargo. While using it, it
was showing me lots of deprication-warnings so I installed ```rustfmt-nightly```
via:

```bash
cargo install rustfmt-nightly
```

## Installing NeoVIM plugins

There are some ```vim```/```neovim``` plugins needed to integrate all those
tools into neovim. I won't go into details on how to install plugins on your
neovim-setup since everyone has one's own preferences about this. Instead I'll
just list them here:

* [w0rp/ale](https://github.com/w0rp/ale)
* [Shougo/deoplete.nvim](https://github.com/Shougo/deoplete)
* [racer-rust/vim-racer](https://github.com/racer-rust)

### Setting up ALE

**[ALE](https://github.com/w0rp/ale)** is a linting-engin for neovim that
implements a language-server client. To comfigure it to use the **rls**-server
we previously installed, put the following lines into your ```init.vim```:

```vim
let g:ale_fixers = {
\ 'rust': ['rustfmt'],
\}

let g:ale_linters = {
\'rust': ['rls'],
\}

let g:ale_rust_rls_toolchain = 'nightly-YYYY-MM-DD'

let g:ale_fix_on_save = 1
let g:airline#extensions#ale#enabled = 1
nmap <silent> <C-k> <Plug>(ale_previous_wrap)
nmap <silent> <C-j> <Plug>(ale_next_wrap)
```

Where ```g:ale_rust_rls_toolchain``` needs to correspond to the ```nightly```
build you used to unstall rls.
This sets up ALE to use rls for linting as well as rustfmt for autoformatting.

### Setting up deoplete

**[deoplete](https://github.com/Shougo/deoplete)** provides live-autocompletion
in neovim. We have two sources for autocompletion installed right now; ALE and
racer, so we must configure deoplete to use those two when editing rust-files.

```vim
let g:deoplete#enable_at_startup = 1
let g:deoplete#sources = {'rust': ['ale', 'racer']}
```

## Additional Plugins

In addition to this particular rust-setup I also use some more plugins that help
me writing code in neovim that might be worth a look.

* [nerdtree](https://github.com/scrooloose/nerdtree) - File tree in a sidebar
* [tagbar](https://github.com/majutsushi/tagbar) - Code Outline in a sidebar
* [vim-fugitive](https://github.com/tpope/vim-fugitive) - Git integration

I won't go too much into details about how to set them up, just read the
documentation and you're good to go.


[Discussion](https://social.fyber.space/notice/9iHJmSZBMD42vPTQdU)

Loading…
Cancel
Save