Skip to content

Latest commit

 

History

History
1168 lines (887 loc) · 20.3 KB

generated_assists.adoc

File metadata and controls

1168 lines (887 loc) · 20.3 KB

add_custom_impl

Adds impl block for derived trait.

Before
#[derive(Deb┃ug, Display)]
struct S;
After
#[derive(Display)]
struct S;

impl Debug for S {
    $0
}

add_explicit_type

Specify type for a let binding.

Before
fn main() {
    let x┃ = 92;
}
After
fn main() {
    let x: i32 = 92;
}

add_hash

Source: raw_string.rs

Adds a hash to a raw string literal.

Before
fn main() {
    r#"Hello,┃ World!"#;
}
After
fn main() {
    r##"Hello, World!"##;
}

add_impl_default_members

Adds scaffold for overriding default impl members.

Before
trait Trait {
    Type X;
    fn foo(&self);
    fn bar(&self) {}
}

impl Trait for () {
    Type X = ();
    fn foo(&self) {}}
After
trait Trait {
    Type X;
    fn foo(&self);
    fn bar(&self) {}
}

impl Trait for () {
    Type X = ();
    fn foo(&self) {}

    $0fn bar(&self) {}
}

add_impl_missing_members

Adds scaffold for required impl members.

Before
trait Trait<T> {
    Type X;
    fn foo(&self) -> T;
    fn bar(&self) {}
}

impl Trait<u32> for () {}
After
trait Trait<T> {
    Type X;
    fn foo(&self) -> T;
    fn bar(&self) {}
}

impl Trait<u32> for () {
    fn foo(&self) -> u32 {
        ${0:todo!()}
    }
}

add_turbo_fish

Adds ::<_> to a call of a generic method or function.

Before
fn make<T>() -> T { todo!() }
fn main() {
    let x = make();
}
After
fn make<T>() -> T { todo!() }
fn main() {
    let x = make::<${0:_}>();
}

apply_demorgan

Apply De Morgan’s law. This transforms expressions of the form !l || !r into !(l && r). This also works with &&. This assist can only be applied with the cursor on either || or &&, with both operands being a negation of some kind. This means something of the form !x or x != y.

Before
fn main() {
    if x != 4 ||┃ !y {}
}
After
fn main() {
    if !(x == 4 && y) {}
}

auto_import

Source: auto_import.rs

If the name is unresolved, provides all possible imports for it.

Before
fn main() {
    let map = HashMap::new();
}
After
use std::collections::HashMap;

fn main() {
    let map = HashMap::new();
}

change_return_type_to_result

Change the function’s return type to Result.

Before
fn foo() -> i32{ 42i32 }
After
fn foo() -> Result<i32, ${0:_}> { Ok(42i32) }

change_visibility

Adds or changes existing visibility specifier.

Before
fn frobnicate() {}
After
pub(crate) fn frobnicate() {}

convert_integer_literal

Converts the base of integer literals to other bases.

Before
const _: i32 = 10;
After
const _: i32 = 0b1010;

convert_to_guarded_return

Source: early_return.rs

Replace a large conditional with a guarded return.

Before
fn main() {if cond {
        foo();
        bar();
    }
}
After
fn main() {
    if !cond {
        return;
    }
    foo();
    bar();
}

expand_glob_import

Expands glob imports.

Before
mod foo {
    pub struct Bar;
    pub struct Baz;
}

use foo::*;

fn qux(bar: Bar, baz: Baz) {}
After
mod foo {
    pub struct Bar;
    pub struct Baz;
}

use foo::{Baz, Bar};

fn qux(bar: Bar, baz: Baz) {}

extract_struct_from_enum_variant

Extracts a struct from enum variant.

Before
enum A {One(u32, u32) }
After
struct One(pub u32, pub u32);

enum A { One(One) }

extract_variable

Extracts subexpression into a variable.

Before
fn main() {(1 + 2)* 4;
}
After
fn main() {
    let $0var_name = (1 + 2);
    var_name * 4;
}

fill_match_arms

Adds missing clauses to a match expression.

Before
enum Action { Move { distance: u32 }, Stop }

fn handle(action: Action) {
    match action {}
}
After
enum Action { Move { distance: u32 }, Stop }

fn handle(action: Action) {
    match action {
        $0Action::Move { distance } => {}
        Action::Stop => {}
    }
}

fix_visibility

Makes inaccessible item public.

Before
mod m {
    fn frobnicate() {}
}
fn main() {
    m::frobnicate() {}
}
After
mod m {
    $0pub(crate) fn frobnicate() {}
}
fn main() {
    m::frobnicate() {}
}

flip_binexpr

Source: flip_binexpr.rs

Flips operands of a binary expression.

Before
fn main() {
    let _ = 90 +┃ 2;
}
After
fn main() {
    let _ = 2 + 90;
}

flip_comma

Source: flip_comma.rs

Flips two comma-separated items.

Before
fn main() {
    ((1, 2),(3, 4));
}
After
fn main() {
    ((3, 4), (1, 2));
}

flip_trait_bound

Flips two trait bounds.

Before
fn foo<T: Clone +┃ Copy>() { }
After
fn foo<T: Copy + Clone>() { }

generate_derive

Adds a new #[derive()] clause to a struct or enum.

Before
struct Point {
    x: u32,
    y: u32,}
After
#[derive($0)]
struct Point {
    x: u32,
    y: u32,
}

generate_from_impl_for_enum

Adds a From impl for an enum variant with one tuple field.

Before
enum A {One(u32) }
After
enum A { One(u32) }

impl From<u32> for A {
    fn from(v: u32) -> Self {
        A::One(v)
    }
}

generate_function

Adds a stub function with a signature matching the function under the cursor.

Before
struct Baz;
fn baz() -> Baz { Baz }
fn foo() {
    bar("", baz());
}
After
struct Baz;
fn baz() -> Baz { Baz }
fn foo() {
    bar("", baz());
}

fn bar(arg: &str, baz: Baz) ${0:-> ()} {
    todo!()
}

generate_impl

Adds a new inherent impl for a type.

Before
struct Ctx<T: Clone> {
    data: T,}
After
struct Ctx<T: Clone> {
    data: T,
}

impl<T: Clone> Ctx<T> {
    $0
}

generate_new

Source: generate_new.rs

Adds a new inherent impl for a type.

Before
struct Ctx<T: Clone> {
     data: T,}
After
struct Ctx<T: Clone> {
     data: T,
}

impl<T: Clone> Ctx<T> {
    fn $0new(data: T) -> Self { Self { data } }
}

inline_local_variable

Inlines local variable.

Before
fn main() {
    let x┃ = 1 + 2;
    x * 4;
}
After
fn main() {
    (1 + 2) * 4;
}

introduce_named_lifetime

Change an anonymous lifetime to a named lifetime.

Before
impl Cursor<'_┃> {
    fn node(self) -> &SyntaxNode {
        match self {
            Cursor::Replace(node) | Cursor::Before(node) => node,
        }
    }
}
After
impl<'a> Cursor<'a> {
    fn node(self) -> &SyntaxNode {
        match self {
            Cursor::Replace(node) | Cursor::Before(node) => node,
        }
    }
}

invert_if

Source: invert_if.rs

Apply invert_if This transforms if expressions of the form if !x {A} else {B} into if x {B} else {A} This also works with !=. This assist can only be applied with the cursor on if.

Before
fn main() {
    if┃ !y { A } else { B }
}
After
fn main() {
    if y { B } else { A }
}

make_raw_string

Source: raw_string.rs

Adds r# to a plain string literal.

Before
fn main() {
    "Hello,┃ World!";
}
After
fn main() {
    r#"Hello, World!"#;
}

make_usual_string

Source: raw_string.rs

Turns a raw string into a plain string.

Before
fn main() {
    r#"Hello,┃ "World!""#;
}
After
fn main() {
    "Hello, \"World!\"";
}

merge_imports

Merges two imports with a common prefix.

Before
use std::┃fmt::Formatter;
use std::io;
After
use std::{fmt::Formatter, io};

merge_match_arms

Merges identical match arms.

Before
enum Action { Move { distance: u32 }, Stop }

fn handle(action: Action) {
    match action {Action::Move(..) => foo(),
        Action::Stop => foo(),
    }
}
After
enum Action { Move { distance: u32 }, Stop }

fn handle(action: Action) {
    match action {
        Action::Move(..) | Action::Stop => foo(),
    }
}

move_arm_cond_to_match_guard

Source: move_guard.rs

Moves if expression from match arm body into a guard.

Before
enum Action { Move { distance: u32 }, Stop }

fn handle(action: Action) {
    match action {
        Action::Move { distance } => ┃if distance > 10 { foo() },
        _ => (),
    }
}
After
enum Action { Move { distance: u32 }, Stop }

fn handle(action: Action) {
    match action {
        Action::Move { distance } if distance > 10 => foo(),
        _ => (),
    }
}

move_bounds_to_where_clause

Source: move_bounds.rs

Moves inline type bounds to a where clause.

Before
fn apply<T, U,F: FnOnce(T) -> U>(f: F, x: T) -> U {
    f(x)
}
After
fn apply<T, U, F>(f: F, x: T) -> U where F: FnOnce(T) -> U {
    f(x)
}

move_guard_to_arm_body

Source: move_guard.rs

Moves match guard into match arm body.

Before
enum Action { Move { distance: u32 }, Stop }

fn handle(action: Action) {
    match action {
        Action::Move { distance }if distance > 10 => foo(),
        _ => (),
    }
}
After
enum Action { Move { distance: u32 }, Stop }

fn handle(action: Action) {
    match action {
        Action::Move { distance } => if distance > 10 {
            foo()
        },
        _ => (),
    }
}

qualify_path

Source: qualify_path.rs

If the name is unresolved, provides all possible qualified paths for it.

Before
fn main() {
    let map = HashMap::new();
}
After
fn main() {
    let map = std::collections::HashMap::new();
}

remove_dbg

Source: remove_dbg.rs

Removes dbg!() macro call.

Before
fn main() {dbg!(92);
}
After
fn main() {
    92;
}

remove_hash

Source: raw_string.rs

Removes a hash from a raw string literal.

Before
fn main() {
    r#"Hello,┃ World!"#;
}
After
fn main() {
    r"Hello, World!";
}

remove_mut

Source: remove_mut.rs

Removes the mut keyword.

Before
impl Walrus {
    fn feed(&mutself, amount: u32) {}
}
After
impl Walrus {
    fn feed(&self, amount: u32) {}
}

remove_unused_param

Removes unused function parameter.

Before
fn frobnicate(x: i32) {}

fn main() {
    frobnicate(92);
}
After
fn frobnicate() {}

fn main() {
    frobnicate();
}

reorder_fields

Reorder the fields of record literals and record patterns in the same order as in the definition.

Before
struct Foo {foo: i32, bar: i32};
const test: Foo = ┃Foo {bar: 0, foo: 1}
After
struct Foo {foo: i32, bar: i32};
const test: Foo = Foo {foo: 1, bar: 0}

replace_if_let_with_match

Replaces if let with an else branch with a match expression.

Before
enum Action { Move { distance: u32 }, Stop }

fn handle(action: Action) {if let Action::Move { distance } = action {
        foo(distance)
    } else {
        bar()
    }
}
After
enum Action { Move { distance: u32 }, Stop }

fn handle(action: Action) {
    match action {
        Action::Move { distance } => foo(distance),
        _ => bar(),
    }
}

replace_impl_trait_with_generic

Replaces impl Trait function argument with the named generic.

Before
fn foo(bar:impl Bar) {}
After
fn foo<B: Bar>(bar: B) {}

replace_let_with_if_let

Replaces let with an if-let.

Before
fn main(action: Action) {let x = compute();
}

fn compute() -> Option<i32> { None }
After
fn main(action: Action) {
    if let Some(x) = compute() {
    }
}

fn compute() -> Option<i32> { None }

replace_qualified_name_with_use

Adds a use statement for a given fully-qualified name.

Before
fn process(map: std::collections::HashMap<String, String>) {}
After
use std::collections::HashMap;

fn process(map: HashMap<String, String>) {}

replace_string_with_char

Replace string with char.

Before
fn main() {
    find("{┃");
}
After
fn main() {
    find('{');
}

replace_unwrap_with_match

Replaces unwrap a match expression. Works for Result and Option.

Before
enum Result<T, E> { Ok(T), Err(E) }
fn main() {
    let x: Result<i32, i32> = Result::Ok(92);
    let y = x.unwrap();
}
After
enum Result<T, E> { Ok(T), Err(E) }
fn main() {
    let x: Result<i32, i32> = Result::Ok(92);
    let y = match x {
        Ok(a) => a,
        $0_ => unreachable!(),
    };
}

split_import

Source: split_import.rs

Wraps the tail of import into braces.

Before
use std::┃collections::HashMap;
After
use std::{collections::HashMap};

unwrap_block

Source: unwrap_block.rs

This assist removes if…​else, for, while and loop control statements to just keep the body.

Before
fn foo() {
    if true {println!("foo");
    }
}
After
fn foo() {
    println!("foo");
}