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 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
.
"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.
"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
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.
When you write new code, sensible completions will be offered. Use TAB to navigate between the suggestions and ENTER to accept them.
Look at your .vimrc
from the Video #1 to find more capabilities
and study the documentation of CoC and rust-analyzer
(links provided
below).
rust-analyzer
manual: https://rust-analyzer.github.io/manual.htmlrust-anaylzer
guide: https://github.com/rust-lang/rust-analyzer/blob/master/docs/dev/guide.mdrust-analyzer
home page: https://rust-analyzer.github.io/CoC
homepage: https://github.com/neoclide/coc.nvim- Video for setup: https://youtu.be/MQ-EMIEFdmg
- Video: https://youtu.be/kX3olkOwByk
- Overview: https://gist.github.com/max-itzpapalotl/18f7675a60f6f9603250367bcb63992e