Skip to content

Instantly share code, notes, and snippets.

@max-itzpapalotl
Last active January 14, 2024 12:58
Show Gist options
  • Save max-itzpapalotl/fa5ddcb3d0c1be1a87e15496f3cec8ee to your computer and use it in GitHub Desktop.
Save max-itzpapalotl/fa5ddcb3d0c1be1a87e15496f3cec8ee to your computer and use it in GitHub Desktop.
14. `rust-analyzer`

14. The language server rust-analyzer

This video is different from others. We look at a small project and see what the combination of rust-analyzer (language server) and vim with CoC can do for you.

The project

The Cargo.toml looks like this:

[package]
name = "project"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
uuid7 = "0.7.2"

The src/main.rs looks like this:

mod a;

use a::Person;

fn main() {
    a::f();
    let u = a::b::h();
    let p = Person::new("Lee".to_string(), "Bruce".to_string());
    p.show();
    println!("UUID: {}", u);
}

The file src/a.rs for the module a looks like this:

#[derive(Debug)]
pub struct Person {
    name : String,
    first_name : String,
}

impl Person {
    pub fn new(n: String, f: String) -> Self {
        Person{name: n, first_name: f}
    }
    pub fn show(&self) {
        println!("Person: name={}, fist name={}", self.name, self.first_name);
    }
}

fn g() -> Person {
    println!("g");
    Person{ name: "Miller".to_string(), first_name: "Frank".to_string() }
}

pub fn f() {
    println!("f");
    let p = g();
    println!("{}, {}", p.name, p.first_name);
    p.show();
}

pub mod b;

Finally, the file src/a/b.rs for the submodule b of a looks like this:

use uuid7::{uuid7, Uuid};

pub fn h() -> Uuid {
    println!("h");
    let u = uuid7();
    println!("UUID: {}", u);
    u
}

The program as such is non-sensical, it is just use to demonstrate the integrated development environment (IDE) consisting of vim, the CoC-plugin and the language server rust-analyzer.

Finding stuff

"K" is show definition in a pop-up. "gd" is "goto definition". "gy" is "goto type definition".

Use Ctrl-O to go back to where you were.

Finding all references

"gr" is "find all references".

Navigate the result with arrow keys up and down, hit ESC to go back to where you started or ENTER to visit the current place.

Use

  • SPACE j to go to the next reference
  • SPACE k to go to the previous reference
  • SPACE p to go back to the overview

Error display

Save the file to disk, wait a moment, and compile errors show up.

Hover over the errors to see the error messages.

You do not have to run the compiler explicitly so often.

Code completion

When you write new code, sensible completions will be offered. Use TAB to navigate between the suggestions and ENTER to accept them.

.vimrc

Look at your .vimrc from the Video #1 to find more capabilities and study the documentation of CoC and rust-analyzer (links provided below).

References:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment