Vscode uses trust analyzer

Time:2021-1-20

preface

The official plug-in experience of rust’s vscode is often unsatisfactory. When I visited the community today, I found RLS 2.0 – trust analyzer,
After the experience, I feel that although there are still many flaws in trust analyzer, it is at least better than RLS 1.0. I hope the rust Working Group
Put in more energy to improve the editor experience.

install

Need nodejs 10 + and NPM, you can refer tonode.js and npmIt will not be repeated.
You may also need to install the trust standard library

rustup component add rust-src

ensurecodeThe order is inPATHEnvironment variables, and uninstall the rust plug-in to avoid conflicts, execute the following named installation of rust analyzer

git clone https://github.com/rust-analyzer/rust-analyzer.git --depth 1
cd rust-analyzer
cargo install-ra

If there is no error in the installation, this will install the RLS and vscode plug-ins on the machine.

If you want to update the trust analyzer, pull the latest code and run againcargo install-raThat’s it.

to configure

Open vscode, [[Ctrl +,]] search for trust to see all the configuration of the trust analyzer.
Common configurations are:

{
"rust- analyzer.enableCargoWatchOnStartup ":" true "// auto open cargo watch when opening project
"rust- analyzer.highlightingOn ": true, // override built-in syntax
"rust- analyzer.lruCapacity ": 1000, // analyzer maximum cache depth
}

More configuration referencesettings

characteristic

Workspace symbol search [[Ctrl + T]]

In addition to vscode’s own fuzzy search, trust analyzer also uses#and*Enhance fuzzy search function. Use the shortcut key [[Ctrl + T]] to open the symbol search, enter the following content in the search box, and ignore the beginning#Symbol

  • FooSearch the current workspace for theFooTypes of
  • foo#Search the current workspace forfooFunction of
  • Foo*At allrely on, includingstdlibThe search name in isFooTypes of
  • foo#**Search for all dependencies namedfooFunction of

Current document symbol search [[Ctrl + Shift + O]]

Provides a complete symbol search capabilities, detailed use, please refer to vscode documentation. PS: one of my favorite features is in@Add after:All symbols can be classified, neat and beautiful.

Input assist

Although the document says that trust analyzer has the following features, it doesn’t seem to work in my vscode
Trust analyzer provides assistance in entering specific characters:

  • inputlet =If=Then a known expression will try to add it automatically;
  • When you type in a comment, if you change the line, you will automatically add a comment symbol at the beginning of the line
  • Input in chain call.It indents automatically

Code assist

Trust analyzer adds several useful code tips or AIDS.

Structure code assist

If you move the cursor over the structure name, usectrl + .You can see it’s coming upadd #[derive]andadd implTwo tips, as the name suggestsadd #[derive]Is to add
deriveandadd implWill be added automaticallyimplStatement block.

Auto add missingtraitMember method

Add a trait and structure

trait Foo {
    fn foo(&self);
    fn bar(&self);
    fn baz(&self);
}

struct S;

If the structure wants to implement the trait, you can enter theimpl Foo for S {}To see the auxiliary prompt on the left, press [[Ctrl +.]] to see itadd missing impl members, enter and confirm
The following code will be automatically filled in

impl Foo for s {
    fn foo(&self) { unimplemented!() }
    fn bar(&self) { unimplemented!() }
    fn baz(&self) { unimplemented!() }
}

I think this function is very convenient, especially when I realize some unfamiliar traits, I don’t need to look through the definition of trait.

Path import

Suppose there’s a piece of code like this

impl std::fmt::Debug for Foo {
}

Move the cursor toDebugAfter using code assist will help you refactor code as follows

use std::fmt::Debug;

impl Debug for Foo {
}

Changing function visibility

When the cursor moves to the private function name, you can quickly change the function topuborpub (crate)

Fill pattern matching branch

Suppose there is an enumerator

enum A {
    As,
    Bs,
    Cs(String),
    Ds(String, String),
    Es{x: usize, y: usize}
}

fn main() {
    let a = A::As;
    match a {}
}

In the inputmatch a {}Post code assist will automatically help you to expand the matching, such as:

fn main() {
    let a = A::As;
    match <|>a {
        A::As => (),
        A::Bs => (),
        A::Cs(_) => (),
        A::Ds(_, _) => (),
        A::Es{x, y} => (),
    }
}

It is also a very practical function

In addition to the above auxiliary features, trust analyzer has more code auxiliary features, which you can refer to if you are interestedCode Actions(Assists)

Magic fill

This function gives me the feeling of using IPython%commandMagic command, cool and practical.
Suppose there is a function as follows:

foo() -> bool { true }

Follow after calling function.ifThen press [[tab]] to expand toif foo() {}. All expandable expressions are as follows:

  • expr.if->if expr {}
  • expr.match->match expr {}
  • expr.while->while expr {}
  • expr.ref->&expr
  • expr.refm->&mut expr
  • expr.not->!expr
  • expr.dbg->dbg!(expr)

In addition, there are the following snippets in the expression

  • pd->println!("{:?}")
  • ppd->println!("{:#?}")

In the module, snippets are provided with test methods

  • tfn->#[test] fn f(){}

Conclusion:

The new RLS provides more intimate functions than the original RLS, but some basic functions are not as good as the original RLS, which may be due to the lack of snippets,
I’m not used to it at first, but I’m familiar with it as long as I feel good.


This article refers to the document of trust analyzerrust-analyzer-Github

This work adoptsCC agreementReprint must indicate the author and the link of this article

How many things, never urgent. Heaven and earth turn, time is pressing.

Recommended Today

DK7 switch’s support for string

Before JDK7, switch can only support byte, short, char, int or their corresponding encapsulation classes and enum types. After JDK7, switch supports string type. In the switch statement, the value of the expression cannot be null, otherwise NullPointerException will be thrown at runtime. Null cannot be used in the case clause, otherwise compilation errors will […]