SpaceVim

A community-driven vim distribution


Home | About | Quick start guide | Documentation | Development | Community | Sponsors


Blogs » Use Vim as a Rust IDE

This is a general guide for using SpaceVim as a Rust IDE, including layer configuration and usage. Each of the following sections will be covered:

Enable language layer

To add rust language support in SpaceVim, you need to enable the lang#rust layer. Press SPC f v d to open SpaceVim configuration file, and add the following snippet:

[[layers]]
  name = "lang#rust"

For more info, you can read the lang#rust layer documentation.

Code completion

Code completion is provided by autocomplete layer, the rust language completion is provided by lang#rust layer. We also recommended to use lsp layer for rust.

[[layers]]
  name = "lsp"

The lsp layer uses rls as the language server for rust, to install rls:

rustup component add rls rust-analysis rust-src

Add following snippet to SpaceVim config file:

[[layers]]
  name = "lsp"
  filetypes = [
    "rust"
  ]
  [layers.override_cmd]
    rust = ["rls"]

class outline viewer

By default we use tagbar as default outline viewer. To use tagbar with rust, you need to install universal-ctags.

image

Syntax linting

The checkers layer is enabled by default. This layer provides asynchronous syntax linting via neomake. The default lint is cargo. To use rustc as default lint, add following config to bootstrap function:

let g:neomake_rust_enabled_makers = ['rustc']

Jump to test file

SpaceVim use built-in plugin to manager the files in a project, you can add a .project_alt.json to the root of your project with the following content:

{
  "src/*.rs": { "alternate": "test/test_{}.rs" },
  "test/test_*.rs": { "alternate": "src/{}.rs" }
}

With this configuration, you can jump between the source code and test file via command :A

Code formatting

The format layer is also enabled by default. With this layer you can use key binding SPC b f to format current buffer. Before using this feature, please install rustfmt:

rustup component add rustfmt

running code

To run current script, you can press SPC l r, and a split window will be openen, the output of the script will be shown in this window. It is running asynchronously, and will not block your Vim.

rustide

REPL support

Start a evcxr inferior REPL process with SPC l s i. After the REPL process being started, you can send code to inferior process. All key bindings prefix with SPC l s, including sending line, sending selection or even send whole buffer.

rustrepl

Tasks manager

The tasks manager provides a function to register task provider. Adding following vim script into bootstrap function, then SpaceVim can detect the cargo tasks.

function! s:cargo_task() abort
    if filereadable('Cargo.toml')
        let commands = ['build', 'run', 'test']
        let conf = {}
        for cmd in commands
            call extend(conf, {
                        \ cmd : {
                        \ 'command': 'cargo',
                        \ 'args' : [cmd],
                        \ 'isDetected' : 1,
                        \ 'detectedName' : 'cargo:'
                        \ }
                        \ })
        endfor
        return conf
    else
        return {}
    endif
endfunction
call SpaceVim#plugins#tasks#reg_provider(funcref('s:cargo_task'))

Open SpaceVim with a rust file, after pressing SPC p t r, you will see the following tasks menu.

image

The task will run asynchronously, and the results will be shown in the runner buffer.

image

Powered by Jekyll, Help improve this page