diff --git a/crates/hir-def/src/import_map.rs b/crates/hir-def/src/import_map.rs index 6137bd34d64a..d43776b8a66a 100644 --- a/crates/hir-def/src/import_map.rs +++ b/crates/hir-def/src/import_map.rs @@ -320,7 +320,7 @@ impl SearchMode { }; match m { Some((index, _)) => { - name = &name[index + 1..]; + name = name[index..].strip_prefix(|_: char| true).unwrap_or_default(); true } None => false, @@ -1039,4 +1039,22 @@ pub mod fmt { "#]], ); } + + #[test] + fn unicode_fn_name() { + let ra_fixture = r#" + //- /main.rs crate:main deps:dep + //- /dep.rs crate:dep + pub fn あい() {} + "#; + + check_search( + ra_fixture, + "main", + Query::new("あ".to_owned()).fuzzy(), + expect![[r#" + dep::あい (f) + "#]], + ); + } } diff --git a/docs/book/src/assists_generated.md b/docs/book/src/assists_generated.md new file mode 100644 index 000000000000..3617badeef55 --- /dev/null +++ b/docs/book/src/assists_generated.md @@ -0,0 +1,3820 @@ +//! Generated by `cargo xtask codegen assists-doc-tests`, do not edit by hand. + +### `add_braces` +**Source:** [add_braces.rs](crates/ide-assists/src/handlers/add_braces.rs#8) + +Adds braces to lambda and match arm expressions. + +#### Before +```rust +fn foo(n: i32) -> i32 { + match n { + 1 =>┃ n + 1, + _ => 0 + } +} +``` + +#### After +```rust +fn foo(n: i32) -> i32 { + match n { + 1 => { + n + 1 + }, + _ => 0 + } +} +``` + + +### `add_explicit_type` +**Source:** [add_explicit_type.rs](crates/ide-assists/src/handlers/add_explicit_type.rs#7) + +Specify type for a let binding. + +#### Before +```rust +fn main() { + let x┃ = 92; +} +``` + +#### After +```rust +fn main() { + let x: i32 = 92; +} +``` + + +### `add_hash` +**Source:** [raw_string.rs](crates/ide-assists/src/handlers/raw_string.rs#89) + +Adds a hash to a raw string literal. + +#### Before +```rust +fn main() { + r#"Hello,┃ World!"#; +} +``` + +#### After +```rust +fn main() { + r##"Hello, World!"##; +} +``` + + +### `add_impl_default_members` +**Source:** [add_missing_impl_members.rs](crates/ide-assists/src/handlers/add_missing_impl_members.rs#58) + +Adds scaffold for overriding default impl members. + +#### Before +```rust +trait Trait { + type X; + fn foo(&self); + fn bar(&self) {} +} + +impl Trait for () { + type X = (); + fn foo(&self) {}┃ +} +``` + +#### After +```rust +trait Trait { + type X; + fn foo(&self); + fn bar(&self) {} +} + +impl Trait for () { + type X = (); + fn foo(&self) {} + + ┃fn bar(&self) {} +} +``` + + +### `add_impl_missing_members` +**Source:** [add_missing_impl_members.rs](crates/ide-assists/src/handlers/add_missing_impl_members.rs#16) + +Adds scaffold for required impl members. + +#### Before +```rust +trait Trait { + type X; + fn foo(&self) -> T; + fn bar(&self) {} +} + +impl Trait for () {┃ + +} +``` + +#### After +```rust +trait Trait { + type X; + fn foo(&self) -> T; + fn bar(&self) {} +} + +impl Trait for () { + ┃type X; + + fn foo(&self) -> u32 { + todo!() + } +} +``` + + +### `add_label_to_loop` +**Source:** [add_label_to_loop.rs](crates/ide-assists/src/handlers/add_label_to_loop.rs#9) + +Adds a label to a loop. + +#### Before +```rust +fn main() { + loop┃ { + break; + continue; + } +} +``` + +#### After +```rust +fn main() { + 'l: loop { + break 'l; + continue 'l; + } +} +``` + + +### `add_lifetime_to_type` +**Source:** [add_lifetime_to_type.rs](crates/ide-assists/src/handlers/add_lifetime_to_type.rs#5) + +Adds a new lifetime to a struct, enum or union. + +#### Before +```rust +struct Point { + x: &┃u32, + y: u32, +} +``` + +#### After +```rust +struct Point<'a> { + x: &'a u32, + y: u32, +} +``` + + +### `add_missing_match_arms` +**Source:** [add_missing_match_arms.rs](crates/ide-assists/src/handlers/add_missing_match_arms.rs#14) + +Adds missing clauses to a `match` expression. + +#### Before +```rust +enum Action { Move { distance: u32 }, Stop } + +fn handle(action: Action) { + match action { + ┃ + } +} +``` + +#### After +```rust +enum Action { Move { distance: u32 }, Stop } + +fn handle(action: Action) { + match action { + Action::Move { distance } => ${1:todo!()}, + Action::Stop => ${2:todo!()},┃ + } +} +``` + + +### `add_return_type` +**Source:** [add_return_type.rs](crates/ide-assists/src/handlers/add_return_type.rs#6) + +Adds the return type to a function or closure inferred from its tail expression if it doesn't have a return +type specified. This assists is useable in a functions or closures tail expression or return type position. + +#### Before +```rust +fn foo() { 4┃2i32 } +``` + +#### After +```rust +fn foo() -> i32 { 42i32 } +``` + + +### `add_turbo_fish` +**Source:** [add_turbo_fish.rs](crates/ide-assists/src/handlers/add_turbo_fish.rs#14) + +Adds `::<_>` to a call of a generic method or function. + +#### Before +```rust +fn make() -> T { todo!() } +fn main() { + let x = make┃(); +} +``` + +#### After +```rust +fn make() -> T { todo!() } +fn main() { + let x = make::<${0:_}>(); +} +``` + + +### `apply_demorgan` +**Source:** [apply_demorgan.rs](crates/ide-assists/src/handlers/apply_demorgan.rs#16) + +Apply [De Morgan's law](https://en.wikipedia.org/wiki/De_Morgan%27s_laws). +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 `&&`. + +#### Before +```rust +fn main() { + if x != 4 ||┃ y < 3.14 {} +} +``` + +#### After +```rust +fn main() { + if !(x == 4 && y >= 3.14) {} +} +``` + + +### `apply_demorgan_iterator` +**Source:** [apply_demorgan.rs](crates/ide-assists/src/handlers/apply_demorgan.rs#132) + +Apply [De Morgan's law](https://en.wikipedia.org/wiki/De_Morgan%27s_laws) to +`Iterator::all` and `Iterator::any`. + +This transforms expressions of the form `!iter.any(|x| predicate(x))` into +`iter.all(|x| !predicate(x))` and vice versa. This also works the other way for +`Iterator::all` into `Iterator::any`. + +#### Before +```rust +fn main() { + let arr = [1, 2, 3]; + if !arr.into_iter().┃any(|num| num == 4) { + println!("foo"); + } +} +``` + +#### After +```rust +fn main() { + let arr = [1, 2, 3]; + if arr.into_iter().all(|num| num != 4) { + println!("foo"); + } +} +``` + + +### `auto_import` +**Source:** [auto_import.rs](crates/ide-assists/src/handlers/auto_import.rs#73) + +If the name is unresolved, provides all possible imports for it. + +#### Before +```rust +fn main() { + let map = HashMap┃::new(); +} +``` + +#### After +```rust +use std::collections::HashMap; + +fn main() { + let map = HashMap::new(); +} +``` + + +### `bind_unused_param` +**Source:** [bind_unused_param.rs](crates/ide-assists/src/handlers/bind_unused_param.rs#12) + +Binds unused function parameter to an underscore. + +#### Before +```rust +fn some_function(x: i32┃) {} +``` + +#### After +```rust +fn some_function(x: i32) { + let _ = x; +} +``` + + +### `bool_to_enum` +**Source:** [bool_to_enum.rs](crates/ide-assists/src/handlers/bool_to_enum.rs#29) + +This converts boolean local variables, fields, constants, and statics into a new +enum with two variants `Bool::True` and `Bool::False`, as well as replacing +all assignments with the variants and replacing all usages with `== Bool::True` or +`== Bool::False`. + +#### Before +```rust +fn main() { + let ┃bool = true; + + if bool { + println!("foo"); + } +} +``` + +#### After +```rust +#[derive(PartialEq, Eq)] +enum Bool { True, False } + +fn main() { + let bool = Bool::True; + + if bool == Bool::True { + println!("foo"); + } +} +``` + + +### `change_visibility` +**Source:** [change_visibility.rs](crates/ide-assists/src/handlers/change_visibility.rs#13) + +Adds or changes existing visibility specifier. + +#### Before +```rust +┃fn frobnicate() {} +``` + +#### After +```rust +pub(crate) fn frobnicate() {} +``` + + +### `comment_to_doc` +**Source:** [convert_comment_from_or_to_doc.rs](crates/ide-assists/src/handlers/convert_comment_from_or_to_doc.rs#9) + +Converts comments to documentation. + +#### Before +```rust +// Wow what ┃a nice module +// I sure hope this shows up when I hover over it +``` + +#### After +```rust +//! Wow what a nice module +//! I sure hope this shows up when I hover over it +``` + + +### `convert_bool_then_to_if` +**Source:** [convert_bool_then.rs](crates/ide-assists/src/handlers/convert_bool_then.rs#131) + +Converts a `bool::then` method call to an equivalent if expression. + +#### Before +```rust +fn main() { + (0 == 0).then┃(|| val) +} +``` + +#### After +```rust +fn main() { + if 0 == 0 { + Some(val) + } else { + None + } +} +``` + + +### `convert_closure_to_fn` +**Source:** [convert_closure_to_fn.rs](crates/ide-assists/src/handlers/convert_closure_to_fn.rs#25) + +This converts a closure to a freestanding function, changing all captures to parameters. + +#### Before +```rust +fn main() { + let mut s = String::new(); + let closure = |┃a| s.push_str(a); + closure("abc"); +} +``` + +#### After +```rust +fn main() { + let mut s = String::new(); + fn closure(a: &str, s: &mut String) { + s.push_str(a) + } + closure("abc", &mut s); +} +``` + + +### `convert_for_loop_with_for_each` +**Source:** [convert_iter_for_each_to_for.rs](crates/ide-assists/src/handlers/convert_iter_for_each_to_for.rs#76) + +Converts a for loop into a for_each loop on the Iterator. + +#### Before +```rust +fn main() { + let x = vec![1, 2, 3]; + for┃ v in x { + let y = v * 2; + } +} +``` + +#### After +```rust +fn main() { + let x = vec![1, 2, 3]; + x.into_iter().for_each(|v| { + let y = v * 2; + }); +} +``` + + +### `convert_from_to_tryfrom` +**Source:** [convert_from_to_tryfrom.rs](crates/ide-assists/src/handlers/convert_from_to_tryfrom.rs#10) + +Converts a From impl to a TryFrom impl, wrapping returns in `Ok`. + +#### Before +```rust +impl ┃From for Thing { + fn from(val: usize) -> Self { + Thing { + b: val.to_string(), + a: val + } + } +} +``` + +#### After +```rust +impl TryFrom for Thing { + type Error = ${0:()}; + + fn try_from(val: usize) -> Result { + Ok(Thing { + b: val.to_string(), + a: val + }) + } +} +``` + + +### `convert_if_to_bool_then` +**Source:** [convert_bool_then.rs](crates/ide-assists/src/handlers/convert_bool_then.rs#20) + +Converts an if expression into a corresponding `bool::then` call. + +#### Before +```rust +fn main() { + if┃ cond { + Some(val) + } else { + None + } +} +``` + +#### After +```rust +fn main() { + cond.then(|| val) +} +``` + + +### `convert_integer_literal` +**Source:** [convert_integer_literal.rs](crates/ide-assists/src/handlers/convert_integer_literal.rs#5) + +Converts the base of integer literals to other bases. + +#### Before +```rust +const _: i32 = 10┃; +``` + +#### After +```rust +const _: i32 = 0b1010; +``` + + +### `convert_into_to_from` +**Source:** [convert_into_to_from.rs](crates/ide-assists/src/handlers/convert_into_to_from.rs#8) + +Converts an Into impl to an equivalent From impl. + +#### Before +```rust +impl ┃Into for usize { + fn into(self) -> Thing { + Thing { + b: self.to_string(), + a: self + } + } +} +``` + +#### After +```rust +impl From for Thing { + fn from(val: usize) -> Self { + Thing { + b: val.to_string(), + a: val + } + } +} +``` + + +### `convert_iter_for_each_to_for` +**Source:** [convert_iter_for_each_to_for.rs](crates/ide-assists/src/handlers/convert_iter_for_each_to_for.rs#11) + +Converts an Iterator::for_each function into a for loop. + +#### Before +```rust +fn main() { + let iter = iter::repeat((9, 2)); + iter.for_each┃(|(x, y)| { + println!("x: {}, y: {}", x, y); + }); +} +``` + +#### After +```rust +fn main() { + let iter = iter::repeat((9, 2)); + for (x, y) in iter { + println!("x: {}, y: {}", x, y); + } +} +``` + + +### `convert_let_else_to_match` +**Source:** [convert_let_else_to_match.rs](crates/ide-assists/src/handlers/convert_let_else_to_match.rs#9) + +Converts let-else statement to let statement and match expression. + +#### Before +```rust +fn main() { + let Ok(mut x) = f() else┃ { return }; +} +``` + +#### After +```rust +fn main() { + let mut x = match f() { + Ok(x) => x, + _ => return, + }; +} +``` + + +### `convert_match_to_let_else` +**Source:** [convert_match_to_let_else.rs](crates/ide-assists/src/handlers/convert_match_to_let_else.rs#12) + +Converts let statement with match initializer to let-else statement. + +#### Before +```rust +fn foo(opt: Option<()>) { + let val┃ = match opt { + Some(it) => it, + None => return, + }; +} +``` + +#### After +```rust +fn foo(opt: Option<()>) { + let Some(val) = opt else { return }; +} +``` + + +### `convert_named_struct_to_tuple_struct` +**Source:** [convert_named_struct_to_tuple_struct.rs](crates/ide-assists/src/handlers/convert_named_struct_to_tuple_struct.rs#11) + +Converts struct with named fields to tuple struct, and analogously for enum variants with named +fields. + +#### Before +```rust +struct Point┃ { x: f32, y: f32 } + +impl Point { + pub fn new(x: f32, y: f32) -> Self { + Point { x, y } + } + + pub fn x(&self) -> f32 { + self.x + } + + pub fn y(&self) -> f32 { + self.y + } +} +``` + +#### After +```rust +struct Point(f32, f32); + +impl Point { + pub fn new(x: f32, y: f32) -> Self { + Point(x, y) + } + + pub fn x(&self) -> f32 { + self.0 + } + + pub fn y(&self) -> f32 { + self.1 + } +} +``` + + +### `convert_nested_function_to_closure` +**Source:** [convert_nested_function_to_closure.rs](crates/ide-assists/src/handlers/convert_nested_function_to_closure.rs#7) + +Converts a function that is defined within the body of another function into a closure. + +#### Before +```rust +fn main() { + fn fo┃o(label: &str, number: u64) { + println!("{}: {}", label, number); + } + + foo("Bar", 100); +} +``` + +#### After +```rust +fn main() { + let foo = |label: &str, number: u64| { + println!("{}: {}", label, number); + }; + + foo("Bar", 100); +} +``` + + +### `convert_to_guarded_return` +**Source:** [convert_to_guarded_return.rs](crates/ide-assists/src/handlers/convert_to_guarded_return.rs#24) + +Replace a large conditional with a guarded return. + +#### Before +```rust +fn main() { + ┃if cond { + foo(); + bar(); + } +} +``` + +#### After +```rust +fn main() { + if !cond { + return; + } + foo(); + bar(); +} +``` + + +### `convert_tuple_return_type_to_struct` +**Source:** [convert_tuple_return_type_to_struct.rs](crates/ide-assists/src/handlers/convert_tuple_return_type_to_struct.rs#20) + +This converts the return type of a function from a tuple type +into a tuple struct and updates the body accordingly. + +#### Before +```rust +fn bar() { + let (a, b, c) = foo(); +} + +fn foo() -> (┃u32, u32, u32) { + (1, 2, 3) +} +``` + +#### After +```rust +fn bar() { + let FooResult(a, b, c) = foo(); +} + +struct FooResult(u32, u32, u32); + +fn foo() -> FooResult { + FooResult(1, 2, 3) +} +``` + + +### `convert_tuple_struct_to_named_struct` +**Source:** [convert_tuple_struct_to_named_struct.rs](crates/ide-assists/src/handlers/convert_tuple_struct_to_named_struct.rs#10) + +Converts tuple struct to struct with named fields, and analogously for tuple enum variants. + +#### Before +```rust +struct Point┃(f32, f32); + +impl Point { + pub fn new(x: f32, y: f32) -> Self { + Point(x, y) + } + + pub fn x(&self) -> f32 { + self.0 + } + + pub fn y(&self) -> f32 { + self.1 + } +} +``` + +#### After +```rust +struct Point { field1: f32, field2: f32 } + +impl Point { + pub fn new(x: f32, y: f32) -> Self { + Point { field1: x, field2: y } + } + + pub fn x(&self) -> f32 { + self.field1 + } + + pub fn y(&self) -> f32 { + self.field2 + } +} +``` + + +### `convert_two_arm_bool_match_to_matches_macro` +**Source:** [convert_two_arm_bool_match_to_matches_macro.rs](crates/ide-assists/src/handlers/convert_two_arm_bool_match_to_matches_macro.rs#8) + +Convert 2-arm match that evaluates to a boolean into the equivalent matches! invocation. + +#### Before +```rust +fn main() { + match scrutinee┃ { + Some(val) if val.cond() => true, + _ => false, + } +} +``` + +#### After +```rust +fn main() { + matches!(scrutinee, Some(val) if val.cond()) +} +``` + + +### `convert_while_to_loop` +**Source:** [convert_while_to_loop.rs](crates/ide-assists/src/handlers/convert_while_to_loop.rs#20) + +Replace a while with a loop. + +#### Before +```rust +fn main() { + ┃while cond { + foo(); + } +} +``` + +#### After +```rust +fn main() { + loop { + if !cond { + break; + } + foo(); + } +} +``` + + +### `destructure_struct_binding` +**Source:** [destructure_struct_binding.rs](crates/ide-assists/src/handlers/destructure_struct_binding.rs#18) + +Destructures a struct binding in place. + +#### Before +```rust +struct Foo { + bar: i32, + baz: i32, +} +fn main() { + let ┃foo = Foo { bar: 1, baz: 2 }; + let bar2 = foo.bar; + let baz2 = &foo.baz; +} +``` + +#### After +```rust +struct Foo { + bar: i32, + baz: i32, +} +fn main() { + let Foo { bar, baz } = Foo { bar: 1, baz: 2 }; + let bar2 = bar; + let baz2 = &baz; +} +``` + + +### `destructure_tuple_binding` +**Source:** [destructure_tuple_binding.rs](crates/ide-assists/src/handlers/destructure_tuple_binding.rs#19) + +Destructures a tuple binding in place. + +#### Before +```rust +fn main() { + let ┃t = (1,2); + let v = t.0; +} +``` + +#### After +```rust +fn main() { + let (┃_0, _1) = (1,2); + let v = _0; +} +``` + + +### `desugar_async_into_impl_future` +**Source:** [toggle_async_sugar.rs](crates/ide-assists/src/handlers/toggle_async_sugar.rs#103) + +Rewrites asynchronous function from `async fn` into `-> impl Future`. +This action does not touch the function body and therefore `0` +block does not transform to `async { 0 }`. + +#### Before +```rust +pub as┃ync fn foo() -> usize { + 0 +} +``` + +#### After +```rust +pub fn foo() -> impl core::future::Future { + 0 +} +``` + + +### `desugar_doc_comment` +**Source:** [desugar_doc_comment.rs](crates/ide-assists/src/handlers/desugar_doc_comment.rs#14) + +Desugars doc-comments to the attribute form. + +#### Before +```rust +/// Multi-line┃ +/// comment +``` + +#### After +```rust +#[doc = r"Multi-line +comment"] +``` + + +### `expand_glob_import` +**Source:** [expand_glob_import.rs](crates/ide-assists/src/handlers/expand_glob_import.rs#18) + +Expands glob imports. + +#### Before +```rust +mod foo { + pub struct Bar; + pub struct Baz; +} + +use foo::*┃; + +fn qux(bar: Bar, baz: Baz) {} +``` + +#### After +```rust +mod foo { + pub struct Bar; + pub struct Baz; +} + +use foo::{Bar, Baz}; + +fn qux(bar: Bar, baz: Baz) {} +``` + + +### `explicit_enum_discriminant` +**Source:** [explicit_enum_discriminant.rs](crates/ide-assists/src/handlers/explicit_enum_discriminant.rs#11) + +Adds explicit discriminant to all enum variants. + +#### Before +```rust +enum TheEnum┃ { + Foo, + Bar, + Baz = 42, + Quux, +} +``` + +#### After +```rust +enum TheEnum { + Foo = 0, + Bar = 1, + Baz = 42, + Quux = 43, +} +``` + + +### `extract_constant` +**Source:** [extract_variable.rs](crates/ide-assists/src/handlers/extract_variable.rs#35) + +Extracts subexpression into a constant. + +#### Before +```rust +fn main() { + ┃(1 + 2)┃ * 4; +} +``` + +#### After +```rust +fn main() { + const ┃VAR_NAME: i32 = 1 + 2; + VAR_NAME * 4; +} +``` + + +### `extract_expressions_from_format_string` +**Source:** [extract_expressions_from_format_string.rs](crates/ide-assists/src/handlers/extract_expressions_from_format_string.rs#14) + +Move an expression out of a format string. + +#### Before +```rust +fn main() { + print!("{var} {x + 1}┃"); +} +``` + +#### After +```rust +fn main() { + print!("{var} {}"┃, x + 1); +} +``` + + +### `extract_function` +**Source:** [extract_function.rs](crates/ide-assists/src/handlers/extract_function.rs#39) + +Extracts selected statements and comments into new function. + +#### Before +```rust +fn main() { + let n = 1; + ┃let m = n + 2; + // calculate + let k = m + n;┃ + let g = 3; +} +``` + +#### After +```rust +fn main() { + let n = 1; + fun_name(n); + let g = 3; +} + +fn ┃fun_name(n: i32) { + let m = n + 2; + // calculate + let k = m + n; +} +``` + + +### `extract_module` +**Source:** [extract_module.rs](crates/ide-assists/src/handlers/extract_module.rs#29) + +Extracts a selected region as separate module. All the references, visibility and imports are +resolved. + +#### Before +```rust +┃fn foo(name: i32) -> i32 { + name + 1 +}┃ + +fn bar(name: i32) -> i32 { + name + 2 +} +``` + +#### After +```rust +mod modname { + pub(crate) fn foo(name: i32) -> i32 { + name + 1 + } +} + +fn bar(name: i32) -> i32 { + name + 2 +} +``` + + +### `extract_static` +**Source:** [extract_variable.rs](crates/ide-assists/src/handlers/extract_variable.rs#52) + +Extracts subexpression into a static. + +#### Before +```rust +fn main() { + ┃(1 + 2)┃ * 4; +} +``` + +#### After +```rust +fn main() { + static ┃VAR_NAME: i32 = 1 + 2; + VAR_NAME * 4; +} +``` + + +### `extract_struct_from_enum_variant` +**Source:** [extract_struct_from_enum_variant.rs](crates/ide-assists/src/handlers/extract_struct_from_enum_variant.rs#26) + +Extracts a struct from enum variant. + +#### Before +```rust +enum A { ┃One(u32, u32) } +``` + +#### After +```rust +struct One(u32, u32); + +enum A { One(One) } +``` + + +### `extract_type_alias` +**Source:** [extract_type_alias.rs](crates/ide-assists/src/handlers/extract_type_alias.rs#10) + +Extracts the selected type as a type alias. + +#### Before +```rust +struct S { + field: ┃(u8, u8, u8)┃, +} +``` + +#### After +```rust +type ┃Type = (u8, u8, u8); + +struct S { + field: Type, +} +``` + + +### `extract_variable` +**Source:** [extract_variable.rs](crates/ide-assists/src/handlers/extract_variable.rs#18) + +Extracts subexpression into a variable. + +#### Before +```rust +fn main() { + ┃(1 + 2)┃ * 4; +} +``` + +#### After +```rust +fn main() { + let ┃var_name = 1 + 2; + var_name * 4; +} +``` + + +### `fill_record_pattern_fields` +**Source:** [fill_record_pattern_fields.rs](crates/ide-assists/src/handlers/fill_record_pattern_fields.rs#8) + +Fills fields by replacing rest pattern in record patterns. + +#### Before +```rust +struct Bar { y: Y, z: Z } + +fn foo(bar: Bar) { + let Bar { ..┃ } = bar; +} +``` + +#### After +```rust +struct Bar { y: Y, z: Z } + +fn foo(bar: Bar) { + let Bar { y, z } = bar; +} +``` + + +### `fix_visibility` +**Source:** [fix_visibility.rs](crates/ide-assists/src/handlers/fix_visibility.rs#14) + +Makes inaccessible item public. + +#### Before +```rust +mod m { + fn frobnicate() {} +} +fn main() { + m::frobnicate┃(); +} +``` + +#### After +```rust +mod m { + ┃pub(crate) fn frobnicate() {} +} +fn main() { + m::frobnicate(); +} +``` + + +### `flip_binexpr` +**Source:** [flip_binexpr.rs](crates/ide-assists/src/handlers/flip_binexpr.rs#8) + +Flips operands of a binary expression. + +#### Before +```rust +fn main() { + let _ = 90 +┃ 2; +} +``` + +#### After +```rust +fn main() { + let _ = 2 + 90; +} +``` + + +### `flip_comma` +**Source:** [flip_comma.rs](crates/ide-assists/src/handlers/flip_comma.rs#10) + +Flips two comma-separated items. + +#### Before +```rust +fn main() { + ((1, 2),┃ (3, 4)); +} +``` + +#### After +```rust +fn main() { + ((3, 4), (1, 2)); +} +``` + + +### `flip_trait_bound` +**Source:** [flip_trait_bound.rs](crates/ide-assists/src/handlers/flip_trait_bound.rs#9) + +Flips two trait bounds. + +#### Before +```rust +fn foo() { } +``` + +#### After +```rust +fn foo() { } +``` + + +### `generate_constant` +**Source:** [generate_constant.rs](crates/ide-assists/src/handlers/generate_constant.rs#14) + +Generate a named constant. + +#### Before +```rust +struct S { i: usize } +impl S { pub fn new(n: usize) {} } +fn main() { + let v = S::new(CAPA┃CITY); +} +``` + +#### After +```rust +struct S { i: usize } +impl S { pub fn new(n: usize) {} } +fn main() { + const CAPACITY: usize = ┃; + let v = S::new(CAPACITY); +} +``` + + +### `generate_default_from_enum_variant` +**Source:** [generate_default_from_enum_variant.rs](crates/ide-assists/src/handlers/generate_default_from_enum_variant.rs#6) + +Adds a Default impl for an enum using a variant. + +#### Before +```rust +enum Version { + Undefined, + Minor┃, + Major, +} +``` + +#### After +```rust +enum Version { + Undefined, + Minor, + Major, +} + +impl Default for Version { + fn default() -> Self { + Self::Minor + } +} +``` + + +### `generate_default_from_new` +**Source:** [generate_default_from_new.rs](crates/ide-assists/src/handlers/generate_default_from_new.rs#13) + +Generates default implementation from new method. + +#### Before +```rust +struct Example { _inner: () } + +impl Example { + pub fn n┃ew() -> Self { + Self { _inner: () } + } +} +``` + +#### After +```rust +struct Example { _inner: () } + +impl Example { + pub fn new() -> Self { + Self { _inner: () } + } +} + +impl Default for Example { + fn default() -> Self { + Self::new() + } +} +``` + + +### `generate_delegate_methods` +**Source:** [generate_delegate_methods.rs](crates/ide-assists/src/handlers/generate_delegate_methods.rs#15) + +Generate delegate methods. + +#### Before +```rust +struct Age(u8); +impl Age { + fn age(&self) -> u8 { + self.0 + } +} + +struct Person { + ag┃e: Age, +} +``` + +#### After +```rust +struct Age(u8); +impl Age { + fn age(&self) -> u8 { + self.0 + } +} + +struct Person { + age: Age, +} + +impl Person { + ┃fn age(&self) -> u8 { + self.age.age() + } +} +``` + + +### `generate_delegate_trait` +**Source:** [generate_delegate_trait.rs](crates/ide-assists/src/handlers/generate_delegate_trait.rs#29) + +Generate delegate trait implementation for `StructField`s. + +#### Before +```rust +trait SomeTrait { + type T; + fn fn_(arg: u32) -> u32; + fn method_(&mut self) -> bool; +} +struct A; +impl SomeTrait for A { + type T = u32; + + fn fn_(arg: u32) -> u32 { + 42 + } + + fn method_(&mut self) -> bool { + false + } +} +struct B { + a┃: A, +} +``` + +#### After +```rust +trait SomeTrait { + type T; + fn fn_(arg: u32) -> u32; + fn method_(&mut self) -> bool; +} +struct A; +impl SomeTrait for A { + type T = u32; + + fn fn_(arg: u32) -> u32 { + 42 + } + + fn method_(&mut self) -> bool { + false + } +} +struct B { + a: A, +} + +impl SomeTrait for B { + type T = ::T; + + fn fn_(arg: u32) -> u32 { + ::fn_(arg) + } + + fn method_(&mut self) -> bool { + ::method_(&mut self.a) + } +} +``` + + +### `generate_deref` +**Source:** [generate_deref.rs](crates/ide-assists/src/handlers/generate_deref.rs#16) + +Generate `Deref` impl using the given struct field. + +#### Before +```rust +struct A; +struct B { + ┃a: A +} +``` + +#### After +```rust +struct A; +struct B { + a: A +} + +impl core::ops::Deref for B { + type Target = A; + + fn deref(&self) -> &Self::Target { + &self.a + } +} +``` + + +### `generate_derive` +**Source:** [generate_derive.rs](crates/ide-assists/src/handlers/generate_derive.rs#8) + +Adds a new `#[derive()]` clause to a struct or enum. + +#### Before +```rust +struct Point { + x: u32, + y: u32,┃ +} +``` + +#### After +```rust +#[derive(┃)] +struct Point { + x: u32, + y: u32, +} +``` + + +### `generate_doc_example` +**Source:** [generate_documentation_template.rs](crates/ide-assists/src/handlers/generate_documentation_template.rs#76) + +Generates a rustdoc example when editing an item's documentation. + +#### Before +```rust +/// Adds two numbers.┃ +pub fn add(a: i32, b: i32) -> i32 { a + b } +``` + +#### After +```rust +/// Adds two numbers. +/// +/// # Examples +/// +/// ``` +/// use ra_test_fixture::add; +/// +/// assert_eq!(add(a, b), ); +/// ``` +pub fn add(a: i32, b: i32) -> i32 { a + b } +``` + + +### `generate_documentation_template` +**Source:** [generate_documentation_template.rs](crates/ide-assists/src/handlers/generate_documentation_template.rs#13) + +Adds a documentation template above a function definition / declaration. + +#### Before +```rust +pub struct S; +impl S { + pub unsafe fn set_len┃(&mut self, len: usize) -> Result<(), std::io::Error> { + /* ... */ + } +} +``` + +#### After +```rust +pub struct S; +impl S { + /// Sets the length of this [`S`]. + /// + /// # Errors + /// + /// This function will return an error if . + /// + /// # Safety + /// + /// . + pub unsafe fn set_len(&mut self, len: usize) -> Result<(), std::io::Error> { + /* ... */ + } +} +``` + + +### `generate_enum_as_method` +**Source:** [generate_enum_projection_method.rs](crates/ide-assists/src/handlers/generate_enum_projection_method.rs#59) + +Generate an `as_` method for this enum variant. + +#### Before +```rust +enum Value { + Number(i32), + Text(String)┃, +} +``` + +#### After +```rust +enum Value { + Number(i32), + Text(String), +} + +impl Value { + fn as_text(&self) -> Option<&String> { + if let Self::Text(v) = self { + Some(v) + } else { + None + } + } +} +``` + + +### `generate_enum_is_method` +**Source:** [generate_enum_is_method.rs](crates/ide-assists/src/handlers/generate_enum_is_method.rs#11) + +Generate an `is_` method for this enum variant. + +#### Before +```rust +enum Version { + Undefined, + Minor┃, + Major, +} +``` + +#### After +```rust +enum Version { + Undefined, + Minor, + Major, +} + +impl Version { + /// Returns `true` if the version is [`Minor`]. + /// + /// [`Minor`]: Version::Minor + #[must_use] + fn is_minor(&self) -> bool { + matches!(self, Self::Minor) + } +} +``` + + +### `generate_enum_try_into_method` +**Source:** [generate_enum_projection_method.rs](crates/ide-assists/src/handlers/generate_enum_projection_method.rs#12) + +Generate a `try_into_` method for this enum variant. + +#### Before +```rust +enum Value { + Number(i32), + Text(String)┃, +} +``` + +#### After +```rust +enum Value { + Number(i32), + Text(String), +} + +impl Value { + fn try_into_text(self) -> Result { + if let Self::Text(v) = self { + Ok(v) + } else { + Err(self) + } + } +} +``` + + +### `generate_enum_variant` +**Source:** [generate_enum_variant.rs](crates/ide-assists/src/handlers/generate_enum_variant.rs#10) + +Adds a variant to an enum. + +#### Before +```rust +enum Countries { + Ghana, +} + +fn main() { + let country = Countries::Lesotho┃; +} +``` + +#### After +```rust +enum Countries { + Ghana, + Lesotho, +} + +fn main() { + let country = Countries::Lesotho; +} +``` + + +### `generate_fn_type_alias_named` +**Source:** [generate_fn_type_alias.rs](crates/ide-assists/src/handlers/generate_fn_type_alias.rs#10) + +Generate a type alias for the function with named parameters. + +#### Before +```rust +unsafe fn fo┃o(n: i32) -> i32 { 42i32 } +``` + +#### After +```rust +type ${0:FooFn} = unsafe fn(n: i32) -> i32; + +unsafe fn foo(n: i32) -> i32 { 42i32 } +``` + + +### `generate_fn_type_alias_unnamed` +**Source:** [generate_fn_type_alias.rs](crates/ide-assists/src/handlers/generate_fn_type_alias.rs#24) + +Generate a type alias for the function with unnamed parameters. + +#### Before +```rust +unsafe fn fo┃o(n: i32) -> i32 { 42i32 } +``` + +#### After +```rust +type ${0:FooFn} = unsafe fn(i32) -> i32; + +unsafe fn foo(n: i32) -> i32 { 42i32 } +``` + + +### `generate_from_impl_for_enum` +**Source:** [generate_from_impl_for_enum.rs](crates/ide-assists/src/handlers/generate_from_impl_for_enum.rs#8) + +Adds a From impl for this enum variant with one tuple field. + +#### Before +```rust +enum A { ┃One(u32) } +``` + +#### After +```rust +enum A { One(u32) } + +impl From for A { + fn from(v: u32) -> Self { + Self::One(v) + } +} +``` + + +### `generate_function` +**Source:** [generate_function.rs](crates/ide-assists/src/handlers/generate_function.rs#28) + +Adds a stub function with a signature matching the function under the cursor. + +#### Before +```rust +struct Baz; +fn baz() -> Baz { Baz } +fn foo() { + bar┃("", baz()); +} + +``` + +#### After +```rust +struct Baz; +fn baz() -> Baz { Baz } +fn foo() { + bar("", baz()); +} + +fn bar(arg: &str, baz: Baz) ${0:-> _} { + todo!() +} + +``` + + +### `generate_getter` +**Source:** [generate_getter_or_setter.rs](crates/ide-assists/src/handlers/generate_getter_or_setter.rs#73) + +Generate a getter method. + +#### Before +```rust +struct Person { + nam┃e: String, +} +``` + +#### After +```rust +struct Person { + name: String, +} + +impl Person { + fn ┃name(&self) -> &str { + &self.name + } +} +``` + + +### `generate_getter_mut` +**Source:** [generate_getter_or_setter.rs](crates/ide-assists/src/handlers/generate_getter_or_setter.rs#127) + +Generate a mut getter method. + +#### Before +```rust +struct Person { + nam┃e: String, +} +``` + +#### After +```rust +struct Person { + name: String, +} + +impl Person { + fn ┃name_mut(&mut self) -> &mut String { + &mut self.name + } +} +``` + + +### `generate_impl` +**Source:** [generate_impl.rs](crates/ide-assists/src/handlers/generate_impl.rs#20) + +Adds a new inherent impl for a type. + +#### Before +```rust +struct Ctx┃ { + data: T, +} +``` + +#### After +```rust +struct Ctx { + data: T, +} + +impl Ctx {┃} +``` + + +### `generate_is_empty_from_len` +**Source:** [generate_is_empty_from_len.rs](crates/ide-assists/src/handlers/generate_is_empty_from_len.rs#12) + +Generates is_empty implementation from the len method. + +#### Before +```rust +struct MyStruct { data: Vec } + +impl MyStruct { + #[must_use] + p┃ub fn len(&self) -> usize { + self.data.len() + } +} +``` + +#### After +```rust +struct MyStruct { data: Vec } + +impl MyStruct { + #[must_use] + pub fn len(&self) -> usize { + self.data.len() + } + + #[must_use] + pub fn is_empty(&self) -> bool { + self.len() == 0 + } +} +``` + + +### `generate_mut_trait_impl` +**Source:** [generate_mut_trait_impl.rs](crates/ide-assists/src/handlers/generate_mut_trait_impl.rs#12) + +Adds a IndexMut impl from the `Index` trait. + +#### Before +```rust +pub enum Axis { X = 0, Y = 1, Z = 2 } + +impl core::ops::Index┃ for [T; 3] { + type Output = T; + + fn index(&self, index: Axis) -> &Self::Output { + &self[index as usize] + } +} +``` + +#### After +```rust +pub enum Axis { X = 0, Y = 1, Z = 2 } + +┃impl core::ops::IndexMut for [T; 3] { + fn index_mut(&mut self, index: Axis) -> &mut Self::Output { + &self[index as usize] + } +} + +impl core::ops::Index for [T; 3] { + type Output = T; + + fn index(&self, index: Axis) -> &Self::Output { + &self[index as usize] + } +} +``` + + +### `generate_new` +**Source:** [generate_new.rs](crates/ide-assists/src/handlers/generate_new.rs#14) + +Adds a `fn new` for a type. + +#### Before +```rust +struct Ctx { + data: T,┃ +} +``` + +#### After +```rust +struct Ctx { + data: T, +} + +impl Ctx { + fn ┃new(data: T) -> Self { + Self { data } + } +} +``` + + +### `generate_setter` +**Source:** [generate_getter_or_setter.rs](crates/ide-assists/src/handlers/generate_getter_or_setter.rs#13) + +Generate a setter method. + +#### Before +```rust +struct Person { + nam┃e: String, +} +``` + +#### After +```rust +struct Person { + name: String, +} + +impl Person { + fn ┃set_name(&mut self, name: String) { + self.name = name; + } +} +``` + + +### `generate_trait_from_impl` +**Source:** [generate_trait_from_impl.rs](crates/ide-assists/src/handlers/generate_trait_from_impl.rs#18) + +Generate trait for an already defined inherent impl and convert impl to a trait impl. + +#### Before +```rust +struct Foo([i32; N]); + +macro_rules! const_maker { + ($t:ty, $v:tt) => { + const CONST: $t = $v; + }; +} + +impl Fo┃o { + // Used as an associated constant. + const CONST_ASSOC: usize = N * 4; + + fn create() -> Option<()> { + Some(()) + } + + const_maker! {i32, 7} +} +``` + +#### After +```rust +struct Foo([i32; N]); + +macro_rules! const_maker { + ($t:ty, $v:tt) => { + const CONST: $t = $v; + }; +} + +trait ${0:NewTrait} { + // Used as an associated constant. + const CONST_ASSOC: usize = N * 4; + + fn create() -> Option<()>; + + const_maker! {i32, 7} +} + +impl ${0:NewTrait} for Foo { + // Used as an associated constant. + const CONST_ASSOC: usize = N * 4; + + fn create() -> Option<()> { + Some(()) + } + + const_maker! {i32, 7} +} +``` + + +### `generate_trait_impl` +**Source:** [generate_impl.rs](crates/ide-assists/src/handlers/generate_impl.rs#66) + +Adds a new trait impl for a type. + +#### Before +```rust +struct ┃Ctx { + data: T, +} +``` + +#### After +```rust +struct Ctx { + data: T, +} + +impl ${0:_} for Ctx {} +``` + + +### `inline_call` +**Source:** [inline_call.rs](crates/ide-assists/src/handlers/inline_call.rs#170) + +Inlines a function or method body creating a `let` statement per parameter unless the parameter +can be inlined. The parameter will be inlined either if it the supplied argument is a simple local +or if the parameter is only accessed inside the function body once. + +#### Before +```rust +fn foo(name: Option<&str>) { + let name = name.unwrap┃(); +} +``` + +#### After +```rust +fn foo(name: Option<&str>) { + let name = match name { + Some(val) => val, + None => panic!("called `Option::unwrap()` on a `None` value"), + }; +} +``` + + +### `inline_const_as_literal` +**Source:** [inline_const_as_literal.rs](crates/ide-assists/src/handlers/inline_const_as_literal.rs#6) + +Evaluate and inline const variable as literal. + +#### Before +```rust +const STRING: &str = "Hello, World!"; + +fn something() -> &'static str { + STRING┃ +} +``` + +#### After +```rust +const STRING: &str = "Hello, World!"; + +fn something() -> &'static str { + "Hello, World!" +} +``` + + +### `inline_into_callers` +**Source:** [inline_call.rs](crates/ide-assists/src/handlers/inline_call.rs#32) + +Inline a function or method body into all of its callers where possible, creating a `let` statement per parameter +unless the parameter can be inlined. The parameter will be inlined either if it the supplied argument is a simple local +or if the parameter is only accessed inside the function body once. +If all calls can be inlined the function will be removed. + +#### Before +```rust +fn print(_: &str) {} +fn foo┃(word: &str) { + if !word.is_empty() { + print(word); + } +} +fn bar() { + foo("안녕하세요"); + foo("여러분"); +} +``` + +#### After +```rust +fn print(_: &str) {} + +fn bar() { + { + let word: &str = "안녕하세요"; + if !word.is_empty() { + print(word); + } + }; + { + let word: &str = "여러분"; + if !word.is_empty() { + print(word); + } + }; +} +``` + + +### `inline_local_variable` +**Source:** [inline_local_variable.rs](crates/ide-assists/src/handlers/inline_local_variable.rs#17) + +Inlines a local variable. + +#### Before +```rust +fn main() { + let x┃ = 1 + 2; + x * 4; +} +``` + +#### After +```rust +fn main() { + (1 + 2) * 4; +} +``` + + +### `inline_macro` +**Source:** [inline_macro.rs](crates/ide-assists/src/handlers/inline_macro.rs#7) + +Takes a macro and inlines it one step. + +#### Before +```rust +macro_rules! num { + (+$($t:tt)+) => (1 + num!($($t )+)); + (-$($t:tt)+) => (-1 + num!($($t )+)); + (+) => (1); + (-) => (-1); +} + +fn main() { + let number = num┃!(+ + + - + +); + println!("{number}"); +} +``` + +#### After +```rust +macro_rules! num { + (+$($t:tt)+) => (1 + num!($($t )+)); + (-$($t:tt)+) => (-1 + num!($($t )+)); + (+) => (1); + (-) => (-1); +} + +fn main() { + let number = 1+num!(+ + - + +); + println!("{number}"); +} +``` + + +### `inline_type_alias` +**Source:** [inline_type_alias.rs](crates/ide-assists/src/handlers/inline_type_alias.rs#106) + +Replace a type alias with its concrete type. + +#### Before +```rust +type A = Vec; + +fn main() { + let a: ┃A; +} +``` + +#### After +```rust +type A = Vec; + +fn main() { + let a: Vec; +} +``` + + +### `inline_type_alias_uses` +**Source:** [inline_type_alias.rs](crates/ide-assists/src/handlers/inline_type_alias.rs#24) + +Inline a type alias into all of its uses where possible. + +#### Before +```rust +type ┃A = i32; +fn id(x: A) -> A { + x +}; +fn foo() { + let _: A = 3; +} +``` + +#### After +```rust + +fn id(x: i32) -> i32 { + x +}; +fn foo() { + let _: i32 = 3; +} +``` + + +### `into_to_qualified_from` +**Source:** [into_to_qualified_from.rs](crates/ide-assists/src/handlers/into_to_qualified_from.rs#10) + +Convert an `into` method call to a fully qualified `from` call. + +#### Before +```rust +//- minicore: from +struct B; +impl From for B { + fn from(a: i32) -> Self { + B + } +} + +fn main() -> () { + let a = 3; + let b: B = a.in┃to(); +} +``` + +#### After +```rust +struct B; +impl From for B { + fn from(a: i32) -> Self { + B + } +} + +fn main() -> () { + let a = 3; + let b: B = B::from(a); +} +``` + + +### `introduce_named_generic` +**Source:** [introduce_named_generic.rs](crates/ide-assists/src/handlers/introduce_named_generic.rs#7) + +Replaces `impl Trait` function argument with the named generic. + +#### Before +```rust +fn foo(bar: ┃impl Bar) {} +``` + +#### After +```rust +fn foo<┃B: Bar>(bar: B) {} +``` + + +### `introduce_named_lifetime` +**Source:** [introduce_named_lifetime.rs](crates/ide-assists/src/handlers/introduce_named_lifetime.rs#13) + +Change an anonymous lifetime to a named lifetime. + +#### Before +```rust +impl Cursor<'_┃> { + fn node(self) -> &SyntaxNode { + match self { + Cursor::Replace(node) | Cursor::Before(node) => node, + } + } +} +``` + +#### After +```rust +impl<'a> Cursor<'a> { + fn node(self) -> &SyntaxNode { + match self { + Cursor::Replace(node) | Cursor::Before(node) => node, + } + } +} +``` + + +### `invert_if` +**Source:** [invert_if.rs](crates/ide-assists/src/handlers/invert_if.rs#13) + +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 +```rust +fn main() { + if┃ !y { A } else { B } +} +``` + +#### After +```rust +fn main() { + if y { B } else { A } +} +``` + + +### `line_to_block` +**Source:** [convert_comment_block.rs](crates/ide-assists/src/handlers/convert_comment_block.rs#9) + +Converts comments between block and single-line form. + +#### Before +```rust + // Multi-line┃ + // comment +``` + +#### After +```rust + /* + Multi-line + comment + */ +``` + + +### `make_raw_string` +**Source:** [raw_string.rs](crates/ide-assists/src/handlers/raw_string.rs#7) + +Adds `r#` to a plain string literal. + +#### Before +```rust +fn main() { + "Hello,┃ World!"; +} +``` + +#### After +```rust +fn main() { + r#"Hello, World!"#; +} +``` + + +### `make_usual_string` +**Source:** [raw_string.rs](crates/ide-assists/src/handlers/raw_string.rs#47) + +Turns a raw string into a plain string. + +#### Before +```rust +fn main() { + r#"Hello,┃ "World!""#; +} +``` + +#### After +```rust +fn main() { + "Hello, \"World!\""; +} +``` + + +### `merge_imports` +**Source:** [merge_imports.rs](crates/ide-assists/src/handlers/merge_imports.rs#21) + +Merges neighbor imports with a common prefix. + +#### Before +```rust +use std::┃fmt::Formatter; +use std::io; +``` + +#### After +```rust +use std::{fmt::Formatter, io}; +``` + + +### `merge_match_arms` +**Source:** [merge_match_arms.rs](crates/ide-assists/src/handlers/merge_match_arms.rs#12) + +Merges the current match arm with the following if their bodies are identical. + +#### Before +```rust +enum Action { Move { distance: u32 }, Stop } + +fn handle(action: Action) { + match action { + ┃Action::Move(..) => foo(), + Action::Stop => foo(), + } +} +``` + +#### After +```rust +enum Action { Move { distance: u32 }, Stop } + +fn handle(action: Action) { + match action { + Action::Move(..) | Action::Stop => foo(), + } +} +``` + + +### `merge_nested_if` +**Source:** [merge_nested_if.rs](crates/ide-assists/src/handlers/merge_nested_if.rs#11) + +This transforms if expressions of the form `if x { if y {A} }` into `if x && y {A}` +This assist can only be applied with the cursor on `if`. + +#### Before +```rust +fn main() { + i┃f x == 3 { if y == 4 { 1 } } +} +``` + +#### After +```rust +fn main() { + if x == 3 && y == 4 { 1 } +} +``` + + +### `move_arm_cond_to_match_guard` +**Source:** [move_guard.rs](crates/ide-assists/src/handlers/move_guard.rs#69) + +Moves if expression from match arm body into a guard. + +#### Before +```rust +enum Action { Move { distance: u32 }, Stop } + +fn handle(action: Action) { + match action { + Action::Move { distance } => ┃if distance > 10 { foo() }, + _ => (), + } +} +``` + +#### After +```rust +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](crates/ide-assists/src/handlers/move_bounds.rs#12) + +Moves inline type bounds to a where clause. + +#### Before +```rust +fn apply U>(f: F, x: T) -> U { + f(x) +} +``` + +#### After +```rust +fn apply(f: F, x: T) -> U where F: FnOnce(T) -> U { + f(x) +} +``` + + +### `move_const_to_impl` +**Source:** [move_const_to_impl.rs](crates/ide-assists/src/handlers/move_const_to_impl.rs#14) + +Move a local constant item in a method to impl's associated constant. All the references will be +qualified with `Self::`. + +#### Before +```rust +struct S; +impl S { + fn foo() -> usize { + /// The answer. + const C┃: usize = 42; + + C * C + } +} +``` + +#### After +```rust +struct S; +impl S { + /// The answer. + const C: usize = 42; + + fn foo() -> usize { + Self::C * Self::C + } +} +``` + + +### `move_from_mod_rs` +**Source:** [move_from_mod_rs.rs](crates/ide-assists/src/handlers/move_from_mod_rs.rs#12) + +Moves xxx/mod.rs to xxx.rs. + +#### Before +```rust +//- /main.rs +mod a; +//- /a/mod.rs +┃fn t() {}┃ +``` + +#### After +```rust +fn t() {} +``` + + +### `move_guard_to_arm_body` +**Source:** [move_guard.rs](crates/ide-assists/src/handlers/move_guard.rs#8) + +Moves match guard into match arm body. + +#### Before +```rust +enum Action { Move { distance: u32 }, Stop } + +fn handle(action: Action) { + match action { + Action::Move { distance } ┃if distance > 10 => foo(), + _ => (), + } +} +``` + +#### After +```rust +enum Action { Move { distance: u32 }, Stop } + +fn handle(action: Action) { + match action { + Action::Move { distance } => if distance > 10 { + foo() + }, + _ => (), + } +} +``` + + +### `move_module_to_file` +**Source:** [move_module_to_file.rs](crates/ide-assists/src/handlers/move_module_to_file.rs#15) + +Moves inline module's contents to a separate file. + +#### Before +```rust +mod ┃foo { + fn t() {} +} +``` + +#### After +```rust +mod foo; +``` + + +### `move_to_mod_rs` +**Source:** [move_to_mod_rs.rs](crates/ide-assists/src/handlers/move_to_mod_rs.rs#12) + +Moves xxx.rs to xxx/mod.rs. + +#### Before +```rust +//- /main.rs +mod a; +//- /a.rs +┃fn t() {}┃ +``` + +#### After +```rust +fn t() {} +``` + + +### `normalize_import` +**Source:** [normalize_import.rs](crates/ide-assists/src/handlers/normalize_import.rs#9) + +Normalizes an import. + +#### Before +```rust +use┃ std::{io, {fmt::Formatter}}; +``` + +#### After +```rust +use std::{fmt::Formatter, io}; +``` + + +### `promote_local_to_const` +**Source:** [promote_local_to_const.rs](crates/ide-assists/src/handlers/promote_local_to_const.rs#17) + +Promotes a local variable to a const item changing its name to a `SCREAMING_SNAKE_CASE` variant +if the local uses no non-const expressions. + +#### Before +```rust +fn main() { + let foo┃ = true; + + if foo { + println!("It's true"); + } else { + println!("It's false"); + } +} +``` + +#### After +```rust +fn main() { + const ┃FOO: bool = true; + + if FOO { + println!("It's true"); + } else { + println!("It's false"); + } +} +``` + + +### `pull_assignment_up` +**Source:** [pull_assignment_up.rs](crates/ide-assists/src/handlers/pull_assignment_up.rs#11) + +Extracts variable assignment to outside an if or match statement. + +#### Before +```rust +fn main() { + let mut foo = 6; + + if true { + ┃foo = 5; + } else { + foo = 4; + } +} +``` + +#### After +```rust +fn main() { + let mut foo = 6; + + foo = if true { + 5 + } else { + 4 + }; +} +``` + + +### `qualify_method_call` +**Source:** [qualify_method_call.rs](crates/ide-assists/src/handlers/qualify_method_call.rs#10) + +Replaces the method call with a qualified function call. + +#### Before +```rust +struct Foo; +impl Foo { + fn foo(&self) {} +} +fn main() { + let foo = Foo; + foo.fo┃o(); +} +``` + +#### After +```rust +struct Foo; +impl Foo { + fn foo(&self) {} +} +fn main() { + let foo = Foo; + Foo::foo(&foo); +} +``` + + +### `qualify_path` +**Source:** [qualify_path.rs](crates/ide-assists/src/handlers/qualify_path.rs#24) + +If the name is unresolved, provides all possible qualified paths for it. + +#### Before +```rust +fn main() { + let map = HashMap┃::new(); +} +``` + +#### After +```rust +fn main() { + let map = std::collections::HashMap::new(); +} +``` + + +### `reformat_number_literal` +**Source:** [number_representation.rs](crates/ide-assists/src/handlers/number_representation.rs#7) + +Adds or removes separators from integer literal. + +#### Before +```rust +const _: i32 = 1012345┃; +``` + +#### After +```rust +const _: i32 = 1_012_345; +``` + + +### `remove_dbg` +**Source:** [remove_dbg.rs](crates/ide-assists/src/handlers/remove_dbg.rs#9) + +Removes `dbg!()` macro call. + +#### Before +```rust +fn main() { + let x = ┃dbg!(42 * dbg!(4 + 2));┃ +} +``` + +#### After +```rust +fn main() { + let x = 42 * (4 + 2); +} +``` + + +### `remove_hash` +**Source:** [raw_string.rs](crates/ide-assists/src/handlers/raw_string.rs#117) + +Removes a hash from a raw string literal. + +#### Before +```rust +fn main() { + r#"Hello,┃ World!"#; +} +``` + +#### After +```rust +fn main() { + r"Hello, World!"; +} +``` + + +### `remove_mut` +**Source:** [remove_mut.rs](crates/ide-assists/src/handlers/remove_mut.rs#5) + +Removes the `mut` keyword. + +#### Before +```rust +impl Walrus { + fn feed(&mut┃ self, amount: u32) {} +} +``` + +#### After +```rust +impl Walrus { + fn feed(&self, amount: u32) {} +} +``` + + +### `remove_parentheses` +**Source:** [remove_parentheses.rs](crates/ide-assists/src/handlers/remove_parentheses.rs#5) + +Removes redundant parentheses. + +#### Before +```rust +fn main() { + _ = ┃(2) + 2; +} +``` + +#### After +```rust +fn main() { + _ = 2 + 2; +} +``` + + +### `remove_unused_imports` +**Source:** [remove_unused_imports.rs](crates/ide-assists/src/handlers/remove_unused_imports.rs#17) + +Removes any use statements in the current selection that are unused. + +#### Before +```rust +struct X(); +mod foo { + use super::X┃; +} +``` + +#### After +```rust +struct X(); +mod foo { +} +``` + + +### `remove_unused_param` +**Source:** [remove_unused_param.rs](crates/ide-assists/src/handlers/remove_unused_param.rs#15) + +Removes unused function parameter. + +#### Before +```rust +fn frobnicate(x: i32┃) {} + +fn main() { + frobnicate(92); +} +``` + +#### After +```rust +fn frobnicate() {} + +fn main() { + frobnicate(); +} +``` + + +### `reorder_fields` +**Source:** [reorder_fields.rs](crates/ide-assists/src/handlers/reorder_fields.rs#8) + +Reorder the fields of record literals and record patterns in the same order as in +the definition. + +#### Before +```rust +struct Foo {foo: i32, bar: i32}; +const test: Foo = ┃Foo {bar: 0, foo: 1} +``` + +#### After +```rust +struct Foo {foo: i32, bar: i32}; +const test: Foo = Foo {foo: 1, bar: 0} +``` + + +### `reorder_impl_items` +**Source:** [reorder_impl_items.rs](crates/ide-assists/src/handlers/reorder_impl_items.rs#11) + +Reorder the items of an `impl Trait`. The items will be ordered +in the same order as in the trait definition. + +#### Before +```rust +trait Foo { + type A; + const B: u8; + fn c(); +} + +struct Bar; +┃impl Foo for Bar┃ { + const B: u8 = 17; + fn c() {} + type A = String; +} +``` + +#### After +```rust +trait Foo { + type A; + const B: u8; + fn c(); +} + +struct Bar; +impl Foo for Bar { + type A = String; + const B: u8 = 17; + fn c() {} +} +``` + + +### `replace_arith_with_checked` +**Source:** [replace_arith_op.rs](crates/ide-assists/src/handlers/replace_arith_op.rs#9) + +Replaces arithmetic on integers with the `checked_*` equivalent. + +#### Before +```rust +fn main() { + let x = 1 ┃+ 2; +} +``` + +#### After +```rust +fn main() { + let x = 1.checked_add(2); +} +``` + + +### `replace_arith_with_saturating` +**Source:** [replace_arith_op.rs](crates/ide-assists/src/handlers/replace_arith_op.rs#28) + +Replaces arithmetic on integers with the `saturating_*` equivalent. + +#### Before +```rust +fn main() { + let x = 1 ┃+ 2; +} +``` + +#### After +```rust +fn main() { + let x = 1.saturating_add(2); +} +``` + + +### `replace_arith_with_wrapping` +**Source:** [replace_arith_op.rs](crates/ide-assists/src/handlers/replace_arith_op.rs#50) + +Replaces arithmetic on integers with the `wrapping_*` equivalent. + +#### Before +```rust +fn main() { + let x = 1 ┃+ 2; +} +``` + +#### After +```rust +fn main() { + let x = 1.wrapping_add(2); +} +``` + + +### `replace_char_with_string` +**Source:** [replace_string_with_char.rs](crates/ide-assists/src/handlers/replace_string_with_char.rs#51) + +Replace a char literal with a string literal. + +#### Before +```rust +fn main() { + find('{┃'); +} +``` + +#### After +```rust +fn main() { + find("{"); +} +``` + + +### `replace_derive_with_manual_impl` +**Source:** [replace_derive_with_manual_impl.rs](crates/ide-assists/src/handlers/replace_derive_with_manual_impl.rs#20) + +Converts a `derive` impl into a manual one. + +#### Before +```rust +#[derive(Deb┃ug, Display)] +struct S; +``` + +#### After +```rust +#[derive(Display)] +struct S; + +impl Debug for S { + ┃fn fmt(&self, f: &mut Formatter) -> Result<()> { + f.debug_struct("S").finish() + } +} +``` + + +### `replace_if_let_with_match` +**Source:** [replace_if_let_with_match.rs](crates/ide-assists/src/handlers/replace_if_let_with_match.rs#20) + +Replaces a `if let` expression with a `match` expression. + +#### Before +```rust +enum Action { Move { distance: u32 }, Stop } + +fn handle(action: Action) { + ┃if let Action::Move { distance } = action { + foo(distance) + } else { + bar() + } +} +``` + +#### After +```rust +enum Action { Move { distance: u32 }, Stop } + +fn handle(action: Action) { + match action { + Action::Move { distance } => foo(distance), + _ => bar(), + } +} +``` + + +### `replace_is_some_with_if_let_some` +**Source:** [replace_is_method_with_if_let_method.rs](crates/ide-assists/src/handlers/replace_is_method_with_if_let_method.rs#9) + +Replace `if x.is_some()` with `if let Some(_tmp) = x` or `if x.is_ok()` with `if let Ok(_tmp) = x`. + +#### Before +```rust +fn main() { + let x = Some(1); + if x.is_som┃e() {} +} +``` + +#### After +```rust +fn main() { + let x = Some(1); + if let Some(${0:x1}) = x {} +} +``` + + +### `replace_let_with_if_let` +**Source:** [replace_let_with_if_let.rs](crates/ide-assists/src/handlers/replace_let_with_if_let.rs#9) + +Replaces `let` with an `if let`. + +#### Before +```rust + +fn main(action: Action) { + ┃let x = compute(); +} + +fn compute() -> Option { None } +``` + +#### After +```rust + +fn main(action: Action) { + if let Some(x) = compute() { + } +} + +fn compute() -> Option { None } +``` + + +### `replace_match_with_if_let` +**Source:** [replace_if_let_with_match.rs](crates/ide-assists/src/handlers/replace_if_let_with_match.rs#188) + +Replaces a binary `match` with a wildcard pattern and no guards with an `if let` expression. + +#### Before +```rust +enum Action { Move { distance: u32 }, Stop } + +fn handle(action: Action) { + ┃match action { + Action::Move { distance } => foo(distance), + _ => bar(), + } +} +``` + +#### After +```rust +enum Action { Move { distance: u32 }, Stop } + +fn handle(action: Action) { + if let Action::Move { distance } = action { + foo(distance) + } else { + bar() + } +} +``` + + +### `replace_named_generic_with_impl` +**Source:** [replace_named_generic_with_impl.rs](crates/ide-assists/src/handlers/replace_named_generic_with_impl.rs#18) + +Replaces named generic with an `impl Trait` in function argument. + +#### Before +```rust +fn new>(location: P) -> Self {} +``` + +#### After +```rust +fn new(location: impl AsRef) -> Self {} +``` + + +### `replace_qualified_name_with_use` +**Source:** [replace_qualified_name_with_use.rs](crates/ide-assists/src/handlers/replace_qualified_name_with_use.rs#13) + +Adds a use statement for a given fully-qualified name. + +#### Before +```rust +fn process(map: std::collections::┃HashMap) {} +``` + +#### After +```rust +use std::collections::HashMap; + +fn process(map: HashMap) {} +``` + + +### `replace_string_with_char` +**Source:** [replace_string_with_char.rs](crates/ide-assists/src/handlers/replace_string_with_char.rs#11) + +Replace string literal with char literal. + +#### Before +```rust +fn main() { + find("{┃"); +} +``` + +#### After +```rust +fn main() { + find('{'); +} +``` + + +### `replace_try_expr_with_match` +**Source:** [replace_try_expr_with_match.rs](crates/ide-assists/src/handlers/replace_try_expr_with_match.rs#18) + +Replaces a `try` expression with a `match` expression. + +#### Before +```rust +fn handle() { + let pat = Some(true)┃?; +} +``` + +#### After +```rust +fn handle() { + let pat = match Some(true) { + Some(it) => it, + None => return None, + }; +} +``` + + +### `replace_turbofish_with_explicit_type` +**Source:** [replace_turbofish_with_explicit_type.rs](crates/ide-assists/src/handlers/replace_turbofish_with_explicit_type.rs#12) + +Converts `::<_>` to an explicit type assignment. + +#### Before +```rust +fn make() -> T { ) } +fn main() { + let a = make┃::(); +} +``` + +#### After +```rust +fn make() -> T { ) } +fn main() { + let a: i32 = make(); +} +``` + + +### `replace_with_eager_method` +**Source:** [replace_method_eager_lazy.rs](crates/ide-assists/src/handlers/replace_method_eager_lazy.rs#89) + +Replace `unwrap_or_else` with `unwrap_or` and `ok_or_else` with `ok_or`. + +#### Before +```rust +fn foo() { + let a = Some(1); + a.unwra┃p_or_else(|| 2); +} +``` + +#### After +```rust +fn foo() { + let a = Some(1); + a.unwrap_or(2); +} +``` + + +### `replace_with_lazy_method` +**Source:** [replace_method_eager_lazy.rs](crates/ide-assists/src/handlers/replace_method_eager_lazy.rs#9) + +Replace `unwrap_or` with `unwrap_or_else` and `ok_or` with `ok_or_else`. + +#### Before +```rust +fn foo() { + let a = Some(1); + a.unwra┃p_or(2); +} +``` + +#### After +```rust +fn foo() { + let a = Some(1); + a.unwrap_or_else(|| 2); +} +``` + + +### `sort_items` +**Source:** [sort_items.rs](crates/ide-assists/src/handlers/sort_items.rs#12) + +Sorts item members alphabetically: fields, enum variants and methods. + +#### Before +```rust +struct ┃Foo┃ { second: u32, first: String } +``` + +#### After +```rust +struct Foo { first: String, second: u32 } +``` + +--- + +#### Before +```rust +trait ┃Bar┃ { + fn second(&self) -> u32; + fn first(&self) -> String; +} +``` + +#### After +```rust +trait Bar { + fn first(&self) -> String; + fn second(&self) -> u32; +} +``` + +--- + +#### Before +```rust +struct Baz; +impl ┃Baz┃ { + fn second(&self) -> u32; + fn first(&self) -> String; +} +``` + +#### After +```rust +struct Baz; +impl Baz { + fn first(&self) -> String; + fn second(&self) -> u32; +} +``` + +--- +There is a difference between sorting enum variants: + +#### Before +```rust +enum ┃Animal┃ { + Dog(String, f64), + Cat { weight: f64, name: String }, +} +``` + +#### After +```rust +enum Animal { + Cat { weight: f64, name: String }, + Dog(String, f64), +} +``` + +and sorting a single enum struct variant: + +#### Before +```rust +enum Animal { + Dog(String, f64), + Cat ┃{ weight: f64, name: String }┃, +} +``` + +#### After +```rust +enum Animal { + Dog(String, f64), + Cat { name: String, weight: f64 }, +} +``` + + +### `split_import` +**Source:** [split_import.rs](crates/ide-assists/src/handlers/split_import.rs#5) + +Wraps the tail of import into braces. + +#### Before +```rust +use std::┃collections::HashMap; +``` + +#### After +```rust +use std::{collections::HashMap}; +``` + + +### `sugar_impl_future_into_async` +**Source:** [toggle_async_sugar.rs](crates/ide-assists/src/handlers/toggle_async_sugar.rs#13) + +Rewrites asynchronous function from `-> impl Future` into `async fn`. +This action does not touch the function body and therefore `async { 0 }` +block does not transform to just `0`. + +#### Before +```rust +pub fn foo() -> impl core::future::F┃uture { + async { 0 } +} +``` + +#### After +```rust +pub async fn foo() -> usize { + async { 0 } +} +``` + + +### `toggle_ignore` +**Source:** [toggle_ignore.rs](crates/ide-assists/src/handlers/toggle_ignore.rs#8) + +Adds `#[ignore]` attribute to the test. + +#### Before +```rust +┃#[test] +fn arithmetics { + assert_eq!(2 + 2, 5); +} +``` + +#### After +```rust +#[test] +#[ignore] +fn arithmetics { + assert_eq!(2 + 2, 5); +} +``` + + +### `toggle_macro_delimiter` +**Source:** [toggle_macro_delimiter.rs](crates/ide-assists/src/handlers/toggle_macro_delimiter.rs#9) + +Change macro delimiters in the order of `( -> { -> [ -> (`. + +#### Before +```rust +macro_rules! sth { + () => {}; +} + +sth!┃( ); +``` + +#### After +```rust +macro_rules! sth { + () => {}; +} + +sth!{ } +``` + + +### `unmerge_match_arm` +**Source:** [unmerge_match_arm.rs](crates/ide-assists/src/handlers/unmerge_match_arm.rs#10) + +Splits the current match with a `|` pattern into two arms with identical bodies. + +#### Before +```rust +enum Action { Move { distance: u32 }, Stop } + +fn handle(action: Action) { + match action { + Action::Move(..) ┃| Action::Stop => foo(), + } +} +``` + +#### After +```rust +enum Action { Move { distance: u32 }, Stop } + +fn handle(action: Action) { + match action { + Action::Move(..) => foo(), + Action::Stop => foo(), + } +} +``` + + +### `unmerge_use` +**Source:** [unmerge_use.rs](crates/ide-assists/src/handlers/unmerge_use.rs#12) + +Extracts single use item from use list. + +#### Before +```rust +use std::fmt::{Debug, Display┃}; +``` + +#### After +```rust +use std::fmt::{Debug}; +use std::fmt::Display; +``` + + +### `unnecessary_async` +**Source:** [unnecessary_async.rs](crates/ide-assists/src/handlers/unnecessary_async.rs#17) + +Removes the `async` mark from functions which have no `.await` in their body. +Looks for calls to the functions and removes the `.await` on the call site. + +#### Before +```rust +pub asy┃nc fn foo() {} +pub async fn bar() { foo().await } +``` + +#### After +```rust +pub fn foo() {} +pub async fn bar() { foo() } +``` + + +### `unqualify_method_call` +**Source:** [unqualify_method_call.rs](crates/ide-assists/src/handlers/unqualify_method_call.rs#9) + +Transforms universal function call syntax into a method call. + +#### Before +```rust +fn main() { + std::ops::Add::add┃(1, 2); +} +``` + +#### After +```rust +use std::ops::Add; + +fn main() { + 1.add(2); +} +``` + + +### `unwrap_block` +**Source:** [unwrap_block.rs](crates/ide-assists/src/handlers/unwrap_block.rs#12) + +This assist removes if...else, for, while and loop control statements to just keep the body. + +#### Before +```rust +fn foo() { + if true {┃ + println!("foo"); + } +} +``` + +#### After +```rust +fn foo() { + println!("foo"); +} +``` + + +### `unwrap_option_return_type` +**Source:** [unwrap_return_type.rs](crates/ide-assists/src/handlers/unwrap_return_type.rs#13) + +Unwrap the function's return type. + +#### Before +```rust +fn foo() -> Option┃ { Some(42i32) } +``` + +#### After +```rust +fn foo() -> i32 { 42i32 } +``` + + +### `unwrap_result_return_type` +**Source:** [unwrap_return_type.rs](crates/ide-assists/src/handlers/unwrap_return_type.rs#26) + +Unwrap the function's return type. + +#### Before +```rust +fn foo() -> Result┃ { Ok(42i32) } +``` + +#### After +```rust +fn foo() -> i32 { 42i32 } +``` + + +### `unwrap_tuple` +**Source:** [unwrap_tuple.rs](crates/ide-assists/src/handlers/unwrap_tuple.rs#8) + +Unwrap the tuple to different variables. + +#### Before +```rust +fn main() { + ┃let (foo, bar) = ("Foo", "Bar"); +} +``` + +#### After +```rust +fn main() { + let foo = "Foo"; + let bar = "Bar"; +} +``` + + +### `wrap_return_type_in_option` +**Source:** [wrap_return_type.rs](crates/ide-assists/src/handlers/wrap_return_type.rs#16) + +Wrap the function's return type into Option. + +#### Before +```rust +fn foo() -> i32┃ { 42i32 } +``` + +#### After +```rust +fn foo() -> Option { Some(42i32) } +``` + + +### `wrap_return_type_in_result` +**Source:** [wrap_return_type.rs](crates/ide-assists/src/handlers/wrap_return_type.rs#29) + +Wrap the function's return type into Result. + +#### Before +```rust +fn foo() -> i32┃ { 42i32 } +``` + +#### After +```rust +fn foo() -> Result { Ok(42i32) } +``` + + +### `wrap_unwrap_cfg_attr` +**Source:** [wrap_unwrap_cfg_attr.rs](crates/ide-assists/src/handlers/wrap_unwrap_cfg_attr.rs#12) + +Wraps an attribute to a cfg_attr attribute or unwraps a cfg_attr attribute to the inner attributes. + +#### Before +```rust +#[derive┃(Debug)] +struct S { + field: i32 +} +``` + +#### After +```rust +#[cfg_attr(┃, derive(Debug))] +struct S { + field: i32 +} +``` diff --git a/docs/book/src/diagnostics_generated.md b/docs/book/src/diagnostics_generated.md new file mode 100644 index 000000000000..d34c459ad025 --- /dev/null +++ b/docs/book/src/diagnostics_generated.md @@ -0,0 +1,516 @@ +//! Generated by `cargo xtask codegen diagnostics-docs`, do not edit by hand. + +#### attribute-expansion-disabled + +Source: [macro_error.rs](crates/ide-diagnostics/src/handlers/macro_error.rs#7) + + +This diagnostic is shown for attribute proc macros when attribute expansions have been disabled. + + + + +#### await-outside-of-async + +Source: [await_outside_of_async.rs](crates/ide-diagnostics/src/handlers/await_outside_of_async.rs#3) + + +This diagnostic is triggered if the `await` keyword is used outside of an async function or block + + + + +#### break-outside-of-loop + +Source: [break_outside_of_loop.rs](crates/ide-diagnostics/src/handlers/break_outside_of_loop.rs#3) + + +This diagnostic is triggered if the `break` keyword is used outside of a loop. + + + + +#### cast-to-unsized + +Source: [invalid_cast.rs](crates/ide-diagnostics/src/handlers/invalid_cast.rs#106) + + +This diagnostic is triggered when casting to an unsized type + + + + +#### expected-function + +Source: [expected_function.rs](crates/ide-diagnostics/src/handlers/expected_function.rs#5) + + +This diagnostic is triggered if a call is made on something that is not callable. + + + + +#### generic-args-prohibited + +Source: [generic_args_prohibited.rs](crates/ide-diagnostics/src/handlers/generic_args_prohibited.rs#10) + + +This diagnostic is shown when generic arguments are provided for a type that does not accept +generic arguments. + + + + +#### inactive-code + +Source: [inactive_code.rs](crates/ide-diagnostics/src/handlers/inactive_code.rs#6) + + +This diagnostic is shown for code with inactive `#[cfg]` attributes. + + + + +#### incoherent-impl + +Source: [incoherent_impl.rs](crates/ide-diagnostics/src/handlers/incoherent_impl.rs#6) + + +This diagnostic is triggered if the targe type of an impl is from a foreign crate. + + + + +#### incorrect-ident-case + +Source: [incorrect_case.rs](crates/ide-diagnostics/src/handlers/incorrect_case.rs#13) + + +This diagnostic is triggered if an item name doesn't follow [Rust naming convention](https://doc.rust-lang.org/1.0.0/style/style/naming/README.html). + + + + +#### invalid-cast + +Source: [invalid_cast.rs](crates/ide-diagnostics/src/handlers/invalid_cast.rs#18) + + +This diagnostic is triggered if the code contains an illegal cast + + + + +#### invalid-derive-target + +Source: [invalid_derive_target.rs](crates/ide-diagnostics/src/handlers/invalid_derive_target.rs#3) + + +This diagnostic is shown when the derive attribute is used on an item other than a `struct`, +`enum` or `union`. + + + + +#### macro-def-error + +Source: [macro_error.rs](crates/ide-diagnostics/src/handlers/macro_error.rs#24) + + +This diagnostic is shown for macro expansion errors. + + + + +#### macro-error + +Source: [macro_error.rs](crates/ide-diagnostics/src/handlers/macro_error.rs#3) + + +This diagnostic is shown for macro expansion errors. + + + + +#### malformed-derive + +Source: [malformed_derive.rs](crates/ide-diagnostics/src/handlers/malformed_derive.rs#3) + + +This diagnostic is shown when the derive attribute has invalid input. + + + + +#### mismatched-arg-count + +Source: [mismatched_arg_count.rs](crates/ide-diagnostics/src/handlers/mismatched_arg_count.rs#31) + + +This diagnostic is triggered if a function is invoked with an incorrect amount of arguments. + + + + +#### mismatched-tuple-struct-pat-arg-count + +Source: [mismatched_arg_count.rs](crates/ide-diagnostics/src/handlers/mismatched_arg_count.rs#11) + + +This diagnostic is triggered if a function is invoked with an incorrect amount of arguments. + + + + +#### missing-fields + +Source: [missing_fields.rs](crates/ide-diagnostics/src/handlers/missing_fields.rs#19) + + +This diagnostic is triggered if record lacks some fields that exist in the corresponding structure. + +Example: + +```rust +struct A { a: u8, b: u8 } + +let a = A { a: 10 }; +``` + + + + +#### missing-match-arm + +Source: [missing_match_arms.rs](crates/ide-diagnostics/src/handlers/missing_match_arms.rs#3) + + +This diagnostic is triggered if `match` block is missing one or more match arms. + + + + +#### missing-unsafe + +Source: [missing_unsafe.rs](crates/ide-diagnostics/src/handlers/missing_unsafe.rs#10) + + +This diagnostic is triggered if an operation marked as `unsafe` is used outside of an `unsafe` function or block. + + + + +#### moved-out-of-ref + +Source: [moved_out_of_ref.rs](crates/ide-diagnostics/src/handlers/moved_out_of_ref.rs#4) + + +This diagnostic is triggered on moving non copy things out of references. + + + + +#### need-mut + +Source: [mutability_errors.rs](crates/ide-diagnostics/src/handlers/mutability_errors.rs#8) + + +This diagnostic is triggered on mutating an immutable variable. + + + + +#### no-such-field + +Source: [no_such_field.rs](crates/ide-diagnostics/src/handlers/no_such_field.rs#12) + + +This diagnostic is triggered if created structure does not have field provided in record. + + + + +#### non-exhaustive-let + +Source: [non_exhaustive_let.rs](crates/ide-diagnostics/src/handlers/non_exhaustive_let.rs#3) + + +This diagnostic is triggered if a `let` statement without an `else` branch has a non-exhaustive +pattern. + + + + +#### private-assoc-item + +Source: [private_assoc_item.rs](crates/ide-diagnostics/src/handlers/private_assoc_item.rs#3) + + +This diagnostic is triggered if the referenced associated item is not visible from the current +module. + + + + +#### private-field + +Source: [private_field.rs](crates/ide-diagnostics/src/handlers/private_field.rs#3) + + +This diagnostic is triggered if the accessed field is not visible from the current module. + + + + +#### proc-macro-disabled + +Source: [macro_error.rs](crates/ide-diagnostics/src/handlers/macro_error.rs#11) + + +This diagnostic is shown for proc macros that have been specifically disabled via `rust-analyzer.procMacro.ignored`. + + + + +#### remove-trailing-return + +Source: [remove_trailing_return.rs](crates/ide-diagnostics/src/handlers/remove_trailing_return.rs#8) + + +This diagnostic is triggered when there is a redundant `return` at the end of a function +or closure. + + + + +#### remove-unnecessary-else + +Source: [remove_unnecessary_else.rs](crates/ide-diagnostics/src/handlers/remove_unnecessary_else.rs#17) + + +This diagnostic is triggered when there is an `else` block for an `if` expression whose +then branch diverges (e.g. ends with a `return`, `continue`, `break` e.t.c). + + + + +#### replace-filter-map-next-with-find-map + +Source: [replace_filter_map_next_with_find_map.rs](crates/ide-diagnostics/src/handlers/replace_filter_map_next_with_find_map.rs#11) + + +This diagnostic is triggered when `.filter_map(..).next()` is used, rather than the more concise `.find_map(..)`. + + + + +#### trait-impl-incorrect-safety + +Source: [trait_impl_incorrect_safety.rs](crates/ide-diagnostics/src/handlers/trait_impl_incorrect_safety.rs#6) + + +Diagnoses incorrect safety annotations of trait impls. + + + + +#### trait-impl-missing-assoc_item + +Source: [trait_impl_missing_assoc_item.rs](crates/ide-diagnostics/src/handlers/trait_impl_missing_assoc_item.rs#7) + + +Diagnoses missing trait items in a trait impl. + + + + +#### trait-impl-orphan + +Source: [trait_impl_orphan.rs](crates/ide-diagnostics/src/handlers/trait_impl_orphan.rs#5) + + +Only traits defined in the current crate can be implemented for arbitrary types + + + + +#### trait-impl-redundant-assoc_item + +Source: [trait_impl_redundant_assoc_item.rs](crates/ide-diagnostics/src/handlers/trait_impl_redundant_assoc_item.rs#12) + + +Diagnoses redundant trait items in a trait impl. + + + + +#### type-mismatch + +Source: [type_mismatch.rs](crates/ide-diagnostics/src/handlers/type_mismatch.rs#20) + + +This diagnostic is triggered when the type of an expression or pattern does not match +the expected type. + + + + +#### typed-hole + +Source: [typed_hole.rs](crates/ide-diagnostics/src/handlers/typed_hole.rs#18) + + +This diagnostic is triggered when an underscore expression is used in an invalid position. + + + + +#### undeclared-label + +Source: [undeclared_label.rs](crates/ide-diagnostics/src/handlers/undeclared_label.rs#3) + + + + + + +#### unimplemented-builtin-macro + +Source: [unimplemented_builtin_macro.rs](crates/ide-diagnostics/src/handlers/unimplemented_builtin_macro.rs#3) + + +This diagnostic is shown for builtin macros which are not yet implemented by rust-analyzer + + + + +#### unlinked-file + +Source: [unlinked_file.rs](crates/ide-diagnostics/src/handlers/unlinked_file.rs#20) + + +This diagnostic is shown for files that are not included in any crate, or files that are part of +crates rust-analyzer failed to discover. The file will not have IDE features available. + + + + +#### unnecessary-braces + +Source: [useless_braces.rs](crates/ide-diagnostics/src/handlers/useless_braces.rs#9) + + +Diagnostic for unnecessary braces in `use` items. + + + + +#### unreachable-label + +Source: [unreachable_label.rs](crates/ide-diagnostics/src/handlers/unreachable_label.rs#3) + + + + + + +#### unresolved-assoc-item + +Source: [unresolved_assoc_item.rs](crates/ide-diagnostics/src/handlers/unresolved_assoc_item.rs#3) + + +This diagnostic is triggered if the referenced associated item does not exist. + + + + +#### unresolved-extern-crate + +Source: [unresolved_extern_crate.rs](crates/ide-diagnostics/src/handlers/unresolved_extern_crate.rs#3) + + +This diagnostic is triggered if rust-analyzer is unable to discover referred extern crate. + + + + +#### unresolved-field + +Source: [unresolved_field.rs](crates/ide-diagnostics/src/handlers/unresolved_field.rs#23) + + +This diagnostic is triggered if a field does not exist on a given type. + + + + +#### unresolved-ident + +Source: [unresolved_ident.rs](crates/ide-diagnostics/src/handlers/unresolved_ident.rs#3) + + +This diagnostic is triggered if an expr-position ident is invalid. + + + + +#### unresolved-import + +Source: [unresolved_import.rs](crates/ide-diagnostics/src/handlers/unresolved_import.rs#3) + + +This diagnostic is triggered if rust-analyzer is unable to resolve a path in +a `use` declaration. + + + + +#### unresolved-macro-call + +Source: [unresolved_macro_call.rs](crates/ide-diagnostics/src/handlers/unresolved_macro_call.rs#3) + + +This diagnostic is triggered if rust-analyzer is unable to resolve the path +to a macro in a macro invocation. + + + + +#### unresolved-method + +Source: [unresolved_method.rs](crates/ide-diagnostics/src/handlers/unresolved_method.rs#15) + + +This diagnostic is triggered if a method does not exist on a given type. + + + + +#### unresolved-module + +Source: [unresolved_module.rs](crates/ide-diagnostics/src/handlers/unresolved_module.rs#8) + + +This diagnostic is triggered if rust-analyzer is unable to discover referred module. + + + + +#### unused-mut + +Source: [mutability_errors.rs](crates/ide-diagnostics/src/handlers/mutability_errors.rs#62) + + +This diagnostic is triggered when a mutable variable isn't actually mutated. + + + + +#### unused-variables + +Source: [unused_variables.rs](crates/ide-diagnostics/src/handlers/unused_variables.rs#13) + + +This diagnostic is triggered when a local variable is not used. + + diff --git a/docs/book/src/features_generated.md b/docs/book/src/features_generated.md new file mode 100644 index 000000000000..2c5829b1f54c --- /dev/null +++ b/docs/book/src/features_generated.md @@ -0,0 +1,940 @@ +//! Generated by `cargo xtask codegen feature-docs`, do not edit by hand. + +### Annotations +**Source:** [annotations.rs](crates/ide/src/annotations.rs#19) + +Provides user with annotations above items for looking up references or impl blocks +and running/debugging binaries. + +![Annotations](https://user-images.githubusercontent.com/48062697/113020672-b7c34f00-917a-11eb-8f6e-858735660a0e.png) + + +### Auto Import +**Source:** [auto_import.rs](crates/ide-assists/src/handlers/auto_import.rs#15) + +Using the `auto-import` assist it is possible to insert missing imports for unresolved items. +When inserting an import it will do so in a structured manner by keeping imports grouped, +separated by a newline in the following order: + +- `std` and `core` +- External Crates +- Current Crate, paths prefixed by `crate` +- Current Module, paths prefixed by `self` +- Super Module, paths prefixed by `super` + +Example: +```rust +use std::fs::File; + +use itertools::Itertools; +use syntax::ast; + +use crate::utils::insert_use; + +use self::auto_import; + +use super::AssistContext; +``` + +#### Import Granularity + +It is possible to configure how use-trees are merged with the `imports.granularity.group` setting. +It has the following configurations: + +- `crate`: Merge imports from the same crate into a single use statement. This kind of + nesting is only supported in Rust versions later than 1.24. +- `module`: Merge imports from the same module into a single use statement. +- `item`: Don't merge imports at all, creating one import per item. +- `preserve`: Do not change the granularity of any imports. For auto-import this has the same + effect as `item`. +- `one`: Merge all imports into a single use statement as long as they have the same visibility + and attributes. + +In `VS Code` the configuration for this is `rust-analyzer.imports.granularity.group`. + +#### Import Prefix + +The style of imports in the same crate is configurable through the `imports.prefix` setting. +It has the following configurations: + +- `crate`: This setting will force paths to be always absolute, starting with the `crate` + prefix, unless the item is defined outside of the current crate. +- `self`: This setting will force paths that are relative to the current module to always + start with `self`. This will result in paths that always start with either `crate`, `self`, + `super` or an extern crate identifier. +- `plain`: This setting does not impose any restrictions in imports. + +In `VS Code` the configuration for this is `rust-analyzer.imports.prefix`. + +![Auto Import](https://user-images.githubusercontent.com/48062697/113020673-b85be580-917a-11eb-9022-59585f35d4f8.gif) + + +### Completion With Autoimport +**Source:** [flyimport.rs](crates/ide-completion/src/completions/flyimport.rs#20) + +When completing names in the current scope, proposes additional imports from other modules or crates, +if they can be qualified in the scope, and their name contains all symbols from the completion input. + +To be considered applicable, the name must contain all input symbols in the given order, not necessarily adjacent. +If any input symbol is not lowercased, the name must contain all symbols in exact case; otherwise the containing is checked case-insensitively. + +``` +fn main() { + pda$0 +} +# pub mod std { pub mod marker { pub struct PhantomData { } } } +``` +-> +``` +use std::marker::PhantomData; + +fn main() { + PhantomData +} +# pub mod std { pub mod marker { pub struct PhantomData { } } } +``` + +Also completes associated items, that require trait imports. +If any unresolved and/or partially-qualified path precedes the input, it will be taken into account. +Currently, only the imports with their import path ending with the whole qualifier will be proposed +(no fuzzy matching for qualifier). + +``` +mod foo { + pub mod bar { + pub struct Item; + + impl Item { + pub const TEST_ASSOC: usize = 3; + } + } +} + +fn main() { + bar::Item::TEST_A$0 +} +``` +-> +``` +use foo::bar; + +mod foo { + pub mod bar { + pub struct Item; + + impl Item { + pub const TEST_ASSOC: usize = 3; + } + } +} + +fn main() { + bar::Item::TEST_ASSOC +} +``` + +NOTE: currently, if an assoc item comes from a trait that's not currently imported, and it also has an unresolved and/or partially-qualified path, +no imports will be proposed. + +#### Fuzzy search details + +To avoid an excessive amount of the results returned, completion input is checked for inclusion in the names only +(i.e. in `HashMap` in the `std::collections::HashMap` path). +For the same reasons, avoids searching for any path imports for inputs with their length less than 2 symbols +(but shows all associated items for any input length). + +#### Import configuration + +It is possible to configure how use-trees are merged with the `imports.granularity.group` setting. +Mimics the corresponding behavior of the `Auto Import` feature. + +#### LSP and performance implications + +The feature is enabled only if the LSP client supports LSP protocol version 3.16+ and reports the `additionalTextEdits` +(case-sensitive) resolve client capability in its client capabilities. +This way the server is able to defer the costly computations, doing them for a selected completion item only. +For clients with no such support, all edits have to be calculated on the completion request, including the fuzzy search completion ones, +which might be slow ergo the feature is automatically disabled. + +#### Feature toggle + +The feature can be forcefully turned off in the settings with the `rust-analyzer.completion.autoimport.enable` flag. +Note that having this flag set to `true` does not guarantee that the feature is enabled: your client needs to have the corresponding +capability enabled. + + +### Debug ItemTree +**Source:** [view_item_tree.rs](crates/ide/src/view_item_tree.rs#5) + +Displays the ItemTree of the currently open file, for debugging. + +| Editor | Action Name | +|---------|-------------| +| VS Code | **rust-analyzer: Debug ItemTree** | + + +### Expand Macro Recursively +**Source:** [expand_macro.rs](crates/ide/src/expand_macro.rs#18) + +Shows the full macro expansion of the macro at the current caret position. + +| Editor | Action Name | +|---------|-------------| +| VS Code | **rust-analyzer: Expand macro recursively at caret** | + +![Expand Macro Recursively](https://user-images.githubusercontent.com/48062697/113020648-b3973180-917a-11eb-84a9-ecb921293dc5.gif) + + +### Expand and Shrink Selection +**Source:** [extend_selection.rs](crates/ide/src/extend_selection.rs#15) + +Extends or shrinks the current selection to the encompassing syntactic construct +(expression, statement, item, module, etc). It works with multiple cursors. + +| Editor | Shortcut | +|---------|----------| +| VS Code | Alt+Shift+→, Alt+Shift+← | + +![Expand and Shrink Selection](https://user-images.githubusercontent.com/48062697/113020651-b42fc800-917a-11eb-8a4f-cf1a07859fac.gif) + + +### File Structure +**Source:** [file_structure.rs](crates/ide/src/file_structure.rs#26) + +Provides a tree of the symbols defined in the file. Can be used to + +* fuzzy search symbol in a file (super useful) +* draw breadcrumbs to describe the context around the cursor +* draw outline of the file + +| Editor | Shortcut | +|---------|----------| +| VS Code | Ctrl+Shift+O | + +![File Structure](https://user-images.githubusercontent.com/48062697/113020654-b42fc800-917a-11eb-8388-e7dc4d92b02e.gif) + + +### Find All References +**Source:** [references.rs](crates/ide/src/references.rs#42) + +Shows all references of the item at the cursor location + +| Editor | Shortcut | +|---------|----------| +| VS Code | Shift+Alt+F12 | + +![Find All References](https://user-images.githubusercontent.com/48062697/113020670-b7c34f00-917a-11eb-8003-370ac5f2b3cb.gif) + + +### Folding +**Source:** [folding_ranges.rs](crates/ide/src/folding_ranges.rs#36) + +Defines folding regions for curly braced blocks, runs of consecutive use, mod, const or static +items, and `region` / `endregion` comment markers. + + +### Format String Completion +**Source:** [format_like.rs](crates/ide-completion/src/completions/postfix/format_like.rs#0) + +`"Result {result} is {2 + 2}"` is expanded to the `"Result {} is {}", result, 2 + 2`. + +The following postfix snippets are available: + +* `format` -> `format!(...)` +* `panic` -> `panic!(...)` +* `println` -> `println!(...)` +* `log`: +** `logd` -> `log::debug!(...)` +** `logt` -> `log::trace!(...)` +** `logi` -> `log::info!(...)` +** `logw` -> `log::warn!(...)` +** `loge` -> `log::error!(...)` + +![Format String Completion](https://user-images.githubusercontent.com/48062697/113020656-b560f500-917a-11eb-87de-02991f61beb8.gif) + + +### Go to Declaration +**Source:** [goto_declaration.rs](crates/ide/src/goto_declaration.rs#13) + +Navigates to the declaration of an identifier. + +This is the same as `Go to Definition` with the following exceptions: +- outline modules will navigate to the `mod name;` item declaration +- trait assoc items will navigate to the assoc item of the trait declaration as opposed to the trait impl +- fields in patterns will navigate to the field declaration of the struct, union or variant + + +### Go to Definition +**Source:** [goto_definition.rs](crates/ide/src/goto_definition.rs#28) + +Navigates to the definition of an identifier. + +For outline modules, this will navigate to the source file of the module. + +| Editor | Shortcut | +|---------|----------| +| VS Code | F12 | + +![Go to Definition](https://user-images.githubusercontent.com/48062697/113065563-025fbe00-91b1-11eb-83e4-a5a703610b23.gif) + + +### Go to Implementation +**Source:** [goto_implementation.rs](crates/ide/src/goto_implementation.rs#11) + +Navigates to the impl items of types. + +| Editor | Shortcut | +|---------|----------| +| VS Code | Ctrl+F12 + +![Go to Implementation](https://user-images.githubusercontent.com/48062697/113065566-02f85480-91b1-11eb-9288-aaad8abd8841.gif) + + +### Go to Type Definition +**Source:** [goto_type_definition.rs](crates/ide/src/goto_type_definition.rs#7) + +Navigates to the type of an identifier. + +| Editor | Action Name | +|---------|-------------| +| VS Code | **Go to Type Definition** | + +![Go to Type Definition](https://user-images.githubusercontent.com/48062697/113020657-b560f500-917a-11eb-9007-0f809733a338.gif) + + +### Highlight Related +**Source:** [highlight_related.rs](crates/ide/src/highlight_related.rs#42) + +Highlights constructs related to the thing under the cursor: + +1. if on an identifier, highlights all references to that identifier in the current file + * additionally, if the identifier is a trait in a where clause, type parameter trait bound or use item, highlights all references to that trait's assoc items in the corresponding scope +1. if on an `async` or `await` token, highlights all yield points for that async context +1. if on a `return` or `fn` keyword, `?` character or `->` return type arrow, highlights all exit points for that context +1. if on a `break`, `loop`, `while` or `for` token, highlights all break points for that loop or block context +1. if on a `move` or `|` token that belongs to a closure, highlights all captures of the closure. + +Note: `?`, `|` and `->` do not currently trigger this behavior in the VSCode editor. + + +### Hover +**Source:** [hover.rs](crates/ide/src/hover.rs#116) + +Shows additional information, like the type of an expression or the documentation for a definition when "focusing" code. +Focusing is usually hovering with a mouse, but can also be triggered with a shortcut. + +![Hover](https://user-images.githubusercontent.com/48062697/113020658-b5f98b80-917a-11eb-9f88-3dbc27320c95.gif) + + +### Inlay Hints +**Source:** [inlay_hints.rs](crates/ide/src/inlay_hints.rs#41) + +rust-analyzer shows additional information inline with the source code. +Editors usually render this using read-only virtual text snippets interspersed with code. + +rust-analyzer by default shows hints for + +* types of local variables +* names of function arguments +* names of const generic parameters +* types of chained expressions + +Optionally, one can enable additional hints for + +* return types of closure expressions +* elided lifetimes +* compiler inserted reborrows +* names of generic type and lifetime parameters + +Note: inlay hints for function argument names are heuristically omitted to reduce noise and will not appear if +any of the +[following criteria](https://github.com/rust-lang/rust-analyzer/blob/6b8b8ff4c56118ddee6c531cde06add1aad4a6af/crates/ide/src/inlay_hints/param_name.rs#L92-L99) +are met: + +* the parameter name is a suffix of the function's name +* the argument is a qualified constructing or call expression where the qualifier is an ADT +* exact argument<->parameter match(ignoring leading underscore) or parameter is a prefix/suffix + of argument with _ splitting it off +* the parameter name starts with `ra_fixture` +* the parameter name is a +[well known name](https://github.com/rust-lang/rust-analyzer/blob/6b8b8ff4c56118ddee6c531cde06add1aad4a6af/crates/ide/src/inlay_hints/param_name.rs#L200) +in a unary function +* the parameter name is a +[single character](https://github.com/rust-lang/rust-analyzer/blob/6b8b8ff4c56118ddee6c531cde06add1aad4a6af/crates/ide/src/inlay_hints/param_name.rs#L201) +in a unary function + +![Inlay hints](https://user-images.githubusercontent.com/48062697/113020660-b5f98b80-917a-11eb-8d70-3be3fd558cdd.png) + + +### Interpret A Function, Static Or Const. +**Source:** [interpret.rs](crates/ide/src/interpret.rs#8) + +| Editor | Action Name | +|---------|-------------| +| VS Code | **rust-analyzer: Interpret** | + + +### Join Lines +**Source:** [join_lines.rs](crates/ide/src/join_lines.rs#20) + +Join selected lines into one, smartly fixing up whitespace, trailing commas, and braces. + +See [this gif](https://user-images.githubusercontent.com/1711539/124515923-4504e800-dde9-11eb-8d58-d97945a1a785.gif) for the cases handled specially by joined lines. + +| Editor | Action Name | +|---------|-------------| +| VS Code | **rust-analyzer: Join lines** | + +![Join Lines](https://user-images.githubusercontent.com/48062697/113020661-b6922200-917a-11eb-87c4-b75acc028f11.gif) + + +### Magic Completions +**Source:** [lib.rs](crates/ide-completion/src/lib.rs#78) + +In addition to usual reference completion, rust-analyzer provides some ✨magic✨ +completions as well: + +Keywords like `if`, `else` `while`, `loop` are completed with braces, and cursor +is placed at the appropriate position. Even though `if` is easy to type, you +still want to complete it, to get ` { }` for free! `return` is inserted with a +space or `;` depending on the return type of the function. + +When completing a function call, `()` are automatically inserted. If a function +takes arguments, the cursor is positioned inside the parenthesis. + +There are postfix completions, which can be triggered by typing something like +`foo().if`. The word after `.` determines postfix completion. Possible variants are: + +- `expr.if` -> `if expr {}` or `if let ... {}` for `Option` or `Result` +- `expr.match` -> `match expr {}` +- `expr.while` -> `while expr {}` or `while let ... {}` for `Option` or `Result` +- `expr.ref` -> `&expr` +- `expr.refm` -> `&mut expr` +- `expr.let` -> `let $0 = expr;` +- `expr.lete` -> `let $1 = expr else { $0 };` +- `expr.letm` -> `let mut $0 = expr;` +- `expr.not` -> `!expr` +- `expr.dbg` -> `dbg!(expr)` +- `expr.dbgr` -> `dbg!(&expr)` +- `expr.call` -> `(expr)` + +There also snippet completions: + +#### Expressions + +- `pd` -> `eprintln!(" = {:?}", );` +- `ppd` -> `eprintln!(" = {:#?}", );` + +#### Items + +- `tfn` -> `#[test] fn feature(){}` +- `tmod` -> +```rust +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_name() {} +} +``` + +And the auto import completions, enabled with the `rust-analyzer.completion.autoimport.enable` setting and the corresponding LSP client capabilities. +Those are the additional completion options with automatic `use` import and options from all project importable items, +fuzzy matched against the completion input. + +![Magic Completions](https://user-images.githubusercontent.com/48062697/113020667-b72ab880-917a-11eb-8778-716cf26a0eb3.gif) + + +### Matching Brace +**Source:** [matching_brace.rs](crates/ide/src/matching_brace.rs#6) + +If the cursor is on any brace (`<>(){}[]||`) which is a part of a brace-pair, +moves cursor to the matching brace. It uses the actual parser to determine +braces, so it won't confuse generics with comparisons. + +| Editor | Action Name | +|---------|-------------| +| VS Code | **rust-analyzer: Find matching brace** | + +![Matching Brace](https://user-images.githubusercontent.com/48062697/113065573-04298180-91b1-11eb-8dec-d4e2a202f304.gif) + + +### Memory Usage +**Source:** [apply_change.rs](crates/ide-db/src/apply_change.rs#43) + +Clears rust-analyzer's internal database and prints memory usage statistics. + +| Editor | Action Name | +|---------|-------------| +| VS Code | **rust-analyzer: Memory Usage (Clears Database)** + + +### Move Item +**Source:** [move_item.rs](crates/ide/src/move_item.rs#16) + +Move item under cursor or selection up and down. + +| Editor | Action Name | +|---------|-------------| +| VS Code | **rust-analyzer: Move item up** +| VS Code | **rust-analyzer: Move item down** + +![Move Item](https://user-images.githubusercontent.com/48062697/113065576-04298180-91b1-11eb-91ce-4505e99ed598.gif) + + +### On Enter +**Source:** [on_enter.rs](crates/ide/src/typing/on_enter.rs#17) + +rust-analyzer can override Enter key to make it smarter: + +- Enter inside triple-slash comments automatically inserts `///` +- Enter in the middle or after a trailing space in `//` inserts `//` +- Enter inside `//!` doc comments automatically inserts `//!` +- Enter after `{` indents contents and closing `}` of single-line block + +This action needs to be assigned to shortcut explicitly. + +Note that, depending on the other installed extensions, this feature can visibly slow down typing. +Similarly, if rust-analyzer crashes or stops responding, `Enter` might not work. +In that case, you can still press `Shift-Enter` to insert a newline. + +#### VS Code + +Add the following to `keybindings.json`: +```json +{ + "key": "Enter", + "command": "rust-analyzer.onEnter", + "when": "editorTextFocus && !suggestWidgetVisible && editorLangId == rust" +} +```` + +When using the Vim plugin: +```json +{ + "key": "Enter", + "command": "rust-analyzer.onEnter", + "when": "editorTextFocus && !suggestWidgetVisible && editorLangId == rust && vim.mode == 'Insert'" +} +```` + +![On Enter](https://user-images.githubusercontent.com/48062697/113065578-04c21800-91b1-11eb-82b8-22b8c481e645.gif) + + +### On Typing Assists +**Source:** [typing.rs](crates/ide/src/typing.rs#42) + +Some features trigger on typing certain characters: + +- typing `let =` tries to smartly add `;` if `=` is followed by an existing expression +- typing `=` between two expressions adds `;` when in statement position +- typing `=` to turn an assignment into an equality comparison removes `;` when in expression position +- typing `.` in a chain method call auto-indents +- typing `{` or `(` in front of an expression inserts a closing `}` or `)` after the expression +- typing `{` in a use item adds a closing `}` in the right place +- typing `>` to complete a return type `->` will insert a whitespace after it + +#### VS Code + +Add the following to `settings.json`: +```json +"editor.formatOnType": true, +``` + +![On Typing Assists](https://user-images.githubusercontent.com/48062697/113166163-69758500-923a-11eb-81ee-eb33ec380399.gif) +![On Typing Assists](https://user-images.githubusercontent.com/48062697/113171066-105c2000-923f-11eb-87ab-f4a263346567.gif) + + +### Open Docs +**Source:** [doc_links.rs](crates/ide/src/doc_links.rs#118) + +Retrieve a links to documentation for the given symbol. + +The simplest way to use this feature is via the context menu. Right-click on +the selected item. The context menu opens. Select **Open Docs**. + +| Editor | Action Name | +|---------|-------------| +| VS Code | **rust-analyzer: Open Docs** | + + +### Parent Module +**Source:** [parent_module.rs](crates/ide/src/parent_module.rs#14) + +Navigates to the parent module of the current module. + +| Editor | Action Name | +|---------|-------------| +| VS Code | **rust-analyzer: Locate parent module** | + +![Parent Module](https://user-images.githubusercontent.com/48062697/113065580-04c21800-91b1-11eb-9a32-00086161c0bd.gif) + + +### Related Tests +**Source:** [runnables.rs](crates/ide/src/runnables.rs#202) + +Provides a sneak peek of all tests where the current item is used. + +The simplest way to use this feature is via the context menu. Right-click on +the selected item. The context menu opens. Select **Peek Related Tests**. + +| Editor | Action Name | +|---------|-------------| +| VS Code | **rust-analyzer: Peek Related Tests** | + + +### Rename +**Source:** [rename.rs](crates/ide/src/rename.rs#70) + +Renames the item below the cursor and all of its references + +| Editor | Shortcut | +|---------|----------| +| VS Code | F2 | + +![Rename](https://user-images.githubusercontent.com/48062697/113065582-055aae80-91b1-11eb-8ade-2b58e6d81883.gif) + + +### Run +**Source:** [runnables.rs](crates/ide/src/runnables.rs#116) + +Shows a popup suggesting to run a test/benchmark/binary **at the current cursor +location**. Super useful for repeatedly running just a single test. Do bind this +to a shortcut! + +| Editor | Action Name | +|---------|-------------| +| VS Code | **rust-analyzer: Run** | + +![Run](https://user-images.githubusercontent.com/48062697/113065583-055aae80-91b1-11eb-958f-d67efcaf6a2f.gif) + + +### Semantic Syntax Highlighting +**Source:** [syntax_highlighting.rs](crates/ide/src/syntax_highlighting.rs#68) + +rust-analyzer highlights the code semantically. +For example, `Bar` in `foo::Bar` might be colored differently depending on whether `Bar` is an enum or a trait. +rust-analyzer does not specify colors directly, instead it assigns a tag (like `struct`) and a set of modifiers (like `declaration`) to each token. +It's up to the client to map those to specific colors. + +The general rule is that a reference to an entity gets colored the same way as the entity itself. +We also give special modifier for `mut` and `&mut` local variables. + + +#### Token Tags + +Rust-analyzer currently emits the following token tags: + +- For items: + +| | | +|-----------|--------------------------------| +| attribute | Emitted for attribute macros. | +|enum| Emitted for enums. | +|function| Emitted for free-standing functions. | +|derive| Emitted for derive macros. | +|macro| Emitted for function-like macros. | +|method| Emitted for associated functions, also knowns as methods. | +|namespace| Emitted for modules. | +|struct| Emitted for structs.| +|trait| Emitted for traits.| +|typeAlias| Emitted for type aliases and `Self` in `impl`s.| +|union| Emitted for unions.| + +- For literals: + +| | | +|-----------|--------------------------------| +| boolean| Emitted for the boolean literals `true` and `false`.| +| character| Emitted for character literals.| +| number| Emitted for numeric literals.| +| string| Emitted for string literals.| +| escapeSequence| Emitted for escaped sequences inside strings like `\n`.| +| formatSpecifier| Emitted for format specifiers `{:?}` in `format!`-like macros.| + +- For operators: + +| | | +|-----------|--------------------------------| +|operator| Emitted for general operators.| +|arithmetic| Emitted for the arithmetic operators `+`, `-`, `*`, `/`, `+=`, `-=`, `*=`, `/=`.| +|bitwise| Emitted for the bitwise operators `|`, `&`, `!`, `^`, `|=`, `&=`, `^=`.| +|comparison| Emitted for the comparison oerators `>`, `<`, `==`, `>=`, `<=`, `!=`.| +|logical| Emitted for the logical operatos `||`, `&&`, `!`.| + +- For punctuation: + +| | | +|-----------|--------------------------------| +|punctuation| Emitted for general punctuation.| +|attributeBracket| Emitted for attribute invocation brackets, that is the `#[` and `]` tokens.| +|angle| Emitted for `<>` angle brackets.| +|brace| Emitted for `{}` braces.| +|bracket| Emitted for `[]` brackets.| +|parenthesis| Emitted for `()` parentheses.| +|colon| Emitted for the `:` token.| +|comma| Emitted for the `,` token.| +|dot| Emitted for the `.` token.| +|semi| Emitted for the `;` token.| +|macroBang| Emitted for the `!` token in macro calls.| + +- + +| | | +|-----------|--------------------------------| +|builtinAttribute| Emitted for names to builtin attributes in attribute path, the `repr` in `#[repr(u8)]` for example.| +|builtinType| Emitted for builtin types like `u32`, `str` and `f32`.| +|comment| Emitted for comments.| +|constParameter| Emitted for const parameters.| +|deriveHelper| Emitted for derive helper attributes.| +|enumMember| Emitted for enum variants.| +|generic| Emitted for generic tokens that have no mapping.| +|keyword| Emitted for keywords.| +|label| Emitted for labels.| +|lifetime| Emitted for lifetimes.| +|parameter| Emitted for non-self function parameters.| +|property| Emitted for struct and union fields.| +|selfKeyword| Emitted for the self function parameter and self path-specifier.| +|selfTypeKeyword| Emitted for the Self type parameter.| +|toolModule| Emitted for tool modules.| +|typeParameter| Emitted for type parameters.| +|unresolvedReference| Emitted for unresolved references, names that rust-analyzer can't find the definition of.| +|variable| Emitted for locals, constants and statics.| + + +#### Token Modifiers + +Token modifiers allow to style some elements in the source code more precisely. + +Rust-analyzer currently emits the following token modifiers: + +| | | +|-----------|--------------------------------| +|async| Emitted for async functions and the `async` and `await` keywords.| +|attribute| Emitted for tokens inside attributes.| +|callable| Emitted for locals whose types implements one of the `Fn*` traits.| +|constant| Emitted for const.| +|consuming| Emitted for locals that are being consumed when use in a function call.| +|controlFlow| Emitted for control-flow related tokens, this includes th `?` operator.| +|crateRoot| Emitted for crate names, like `serde` and `crate.| +|declaration| Emitted for names of definitions, like `foo` in `fn foo(){}`.| +|defaultLibrary| Emitted for items from built-in crates (std, core, allc, test and proc_macro).| +|documentation| Emitted for documentation comment.| +|injected| Emitted for doc-string injected highlighting like rust source blocks in documentation.| +|intraDocLink| Emitted for intra doc links in doc-string.| +|library| Emitted for items that are defined outside of the current crae.| +|macro| Emitted for tokens inside macro call.| +|mutable| Emitted for mutable locals and statics as well as functions taking `&mut self`.| +|public| Emitted for items that are from the current crate and are `pub.| +|reference| Emitted for locals behind a reference and functions taking self` by reference.| +|static| Emitted for "static" functions, also known as functions that d not take a `self` param, as well as statics and consts.| +|trait| Emitted for associated trait item.| +|unsafe| Emitted for unsafe operations, like unsafe function calls, as ell as the `unsafe` token.| + +![Semantic Syntax Highlighting](https://user-images.githubusercontent.com/48062697/113164457-06cfb980-9239-11eb-819b-0f93e646acf8.png) +![Semantic Syntax Highlighting](https://user-images.githubusercontent.com/48062697/113187625-f7f50100-9250-11eb-825e-91c58f236071.png) + + +### Show Dependency Tree +**Source:** [fetch_crates.rs](crates/ide/src/fetch_crates.rs#13) + +Shows a view tree with all the dependencies of this project + +| Editor | Panel Name | +|---------|------------| +| VS Code | **Rust Dependencies** | + +![Show Dependency Tree](https://user-images.githubusercontent.com/5748995/229394139-2625beab-f4c9-484b-84ed-ad5dee0b1e1a.png) + + +### Show Syntax Tree +**Source:** [view_syntax_tree.rs](crates/ide/src/view_syntax_tree.rs#14) + +Shows a tree view with the syntax tree of the current file + +| Editor | Panel Name | +|---------|-------------| +| VS Code | **Rust Syntax Tree** | + + +### Status +**Source:** [status.rs](crates/ide/src/status.rs#28) + +Shows internal statistic about memory usage of rust-analyzer. + +| Editor | Action Name | +|---------|-------------| +| VS Code | **rust-analyzer: Status** | + +![Status](https://user-images.githubusercontent.com/48062697/113065584-05f34500-91b1-11eb-98cc-5c196f76be7f.gif) + + +### Structural Search and Replace +**Source:** [lib.rs](crates/ide-ssr/src/lib.rs#6) + +Search and replace with named wildcards that will match any expression, type, path, pattern or item. +The syntax for a structural search replace command is ` ==>> `. +A `$` placeholder in the search pattern will match any AST node and `$` will reference it in the replacement. +Within a macro call, a placeholder will match up until whatever token follows the placeholder. + +All paths in both the search pattern and the replacement template must resolve in the context +in which this command is invoked. Paths in the search pattern will then match the code if they +resolve to the same item, even if they're written differently. For example if we invoke the +command in the module `foo` with a pattern of `Bar`, then code in the parent module that refers +to `foo::Bar` will match. + +Paths in the replacement template will be rendered appropriately for the context in which the +replacement occurs. For example if our replacement template is `foo::Bar` and we match some +code in the `foo` module, we'll insert just `Bar`. + +Inherent method calls should generally be written in UFCS form. e.g. `foo::Bar::baz($s, $a)` will +match `$s.baz($a)`, provided the method call `baz` resolves to the method `foo::Bar::baz`. When a +placeholder is the receiver of a method call in the search pattern (e.g. `$s.foo()`), but not in +the replacement template (e.g. `bar($s)`), then *, & and &mut will be added as needed to mirror +whatever autoderef and autoref was happening implicitly in the matched code. + +The scope of the search / replace will be restricted to the current selection if any, otherwise +it will apply to the whole workspace. + +Placeholders may be given constraints by writing them as `${::...}`. + +Supported constraints: + +| Constraint | Restricts placeholder | +|---------------|------------------------| +| kind(literal) | Is a literal (e.g. `42` or `"forty two"`) | +| not(a) | Negates the constraint `a` | + +Available via the command `rust-analyzer.ssr`. + +```rust +// Using structural search replace command [foo($a, $b) ==>> ($a).foo($b)] + +// BEFORE +String::from(foo(y + 5, z)) + +// AFTER +String::from((y + 5).foo(z)) +``` + +| Editor | Action Name | +|---------|--------------| +| VS Code | **rust-analyzer: Structural Search Replace** | + +Also available as an assist, by writing a comment containing the structural +search and replace rule. You will only see the assist if the comment can +be parsed as a valid structural search and replace rule. + +```rust +// Place the cursor on the line below to see the assist 💡. +// foo($a, $b) ==>> ($a).foo($b) +``` + + +### User Snippet Completions +**Source:** [snippet.rs](crates/ide-completion/src/snippet.rs#5) + +rust-analyzer allows the user to define custom (postfix)-snippets that may depend on items to be accessible for the current scope to be applicable. + +A custom snippet can be defined by adding it to the `rust-analyzer.completion.snippets.custom` object respectively. + +```json +{ + "rust-analyzer.completion.snippets.custom": { + "thread spawn": { + "prefix": ["spawn", "tspawn"], + "body": [ + "thread::spawn(move || {", + "\t$0", + "});", + ], + "description": "Insert a thread::spawn call", + "requires": "std::thread", + "scope": "expr", + } + } +} +``` + +In the example above: + +* `"thread spawn"` is the name of the snippet. + +* `prefix` defines one or more trigger words that will trigger the snippets completion. +Using `postfix` will instead create a postfix snippet. + +* `body` is one or more lines of content joined via newlines for the final output. + +* `description` is an optional description of the snippet, if unset the snippet name will be used. + +* `requires` is an optional list of item paths that have to be resolvable in the current crate where the completion is rendered. + + +### View Crate Graph +**Source:** [view_crate_graph.rs](crates/ide/src/view_crate_graph.rs#8) + +Renders the currently loaded crate graph as an SVG graphic. Requires the `dot` tool, which +is part of graphviz, to be installed. + +Only workspace crates are included, no crates.io dependencies or sysroot crates. + +| Editor | Action Name | +|---------|-------------| +| VS Code | **rust-analyzer: View Crate Graph** | + + +### View Hir +**Source:** [view_hir.rs](crates/ide/src/view_hir.rs#5) + +| Editor | Action Name | +|---------|--------------| +| VS Code | **rust-analyzer: View Hir** + +![View Hir](https://user-images.githubusercontent.com/48062697/113065588-068bdb80-91b1-11eb-9a78-0b4ef1e972fb.gif) + + +### View Memory Layout +**Source:** [view_memory_layout.rs](crates/ide/src/view_memory_layout.rs#74) + +Displays the recursive memory layout of a datatype. + +| Editor | Action Name | +|---------|-------------| +| VS Code | **rust-analyzer: View Memory Layout** | + + +### View Mir +**Source:** [view_mir.rs](crates/ide/src/view_mir.rs#5) + +| Editor | Action Name | +|---------|-------------| +| VS Code | **rust-analyzer: View Mir** + + +### Workspace Symbol +**Source:** [symbol_index.rs](crates/ide-db/src/symbol_index.rs#174) + +Uses fuzzy-search to find types, modules and functions by name across your +project and dependencies. This is **the** most useful feature, which improves code +navigation tremendously. It mostly works on top of the built-in LSP +functionality, however `#` and `*` symbols can be used to narrow down the +search. Specifically, + +- `Foo` searches for `Foo` type in the current workspace +- `foo#` searches for `foo` function in the current workspace +- `Foo*` searches for `Foo` type among dependencies, including `stdlib` +- `foo#*` searches for `foo` function among dependencies + +That is, `#` switches from "types" to all symbols, `*` switches from the current +workspace to dependencies. + +Note that filtering does not currently work in VSCode due to the editor never +sending the special symbols to the language server. Instead, you can configure +the filtering via the `rust-analyzer.workspace.symbol.search.scope` and +`rust-analyzer.workspace.symbol.search.kind` settings. Symbols prefixed +with `__` are hidden from the search results unless configured otherwise. + +| Editor | Shortcut | +|---------|-----------| +| VS Code | Ctrl+T