mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-25 20:43:21 +00:00
document almost all assists
This commit is contained in:
parent
a455635b48
commit
da5528824a
7 changed files with 359 additions and 183 deletions
|
@ -1,5 +1,3 @@
|
|||
//! FIXME: write short doc here
|
||||
|
||||
use hir::db::HirDatabase;
|
||||
use ra_syntax::{
|
||||
SyntaxKind::{RAW_STRING, STRING},
|
||||
|
@ -9,6 +7,21 @@ use rustc_lexer;
|
|||
|
||||
use crate::{Assist, AssistCtx, AssistId};
|
||||
|
||||
// Assist: make_raw_string
|
||||
//
|
||||
// Adds `r#` to a plain string literal.
|
||||
//
|
||||
// ```
|
||||
// fn main() {
|
||||
// "Hello,<|> World!";
|
||||
// }
|
||||
// ```
|
||||
// ->
|
||||
// ```
|
||||
// fn main() {
|
||||
// r#"Hello, World!"#;
|
||||
// }
|
||||
// ```
|
||||
pub(crate) fn make_raw_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
||||
let token = ctx.find_token_at_offset(STRING)?;
|
||||
let text = token.text().as_str();
|
||||
|
@ -40,6 +53,21 @@ pub(crate) fn make_raw_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option<As
|
|||
ctx.build()
|
||||
}
|
||||
|
||||
// Assist: make_usual_string
|
||||
//
|
||||
// Turns a raw string into a plain string.
|
||||
//
|
||||
// ```
|
||||
// fn main() {
|
||||
// r#"Hello,<|> "World!""#;
|
||||
// }
|
||||
// ```
|
||||
// ->
|
||||
// ```
|
||||
// fn main() {
|
||||
// "Hello, \"World!\"";
|
||||
// }
|
||||
// ```
|
||||
pub(crate) fn make_usual_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
||||
let token = ctx.find_token_at_offset(RAW_STRING)?;
|
||||
let text = token.text().as_str();
|
||||
|
@ -56,6 +84,21 @@ pub(crate) fn make_usual_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option<
|
|||
ctx.build()
|
||||
}
|
||||
|
||||
// Assist: add_hash
|
||||
//
|
||||
// Adds a hash to a raw string literal.
|
||||
//
|
||||
// ```
|
||||
// fn main() {
|
||||
// r#"Hello,<|> World!"#;
|
||||
// }
|
||||
// ```
|
||||
// ->
|
||||
// ```
|
||||
// fn main() {
|
||||
// r##"Hello, World!"##;
|
||||
// }
|
||||
// ```
|
||||
pub(crate) fn add_hash(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
||||
let token = ctx.find_token_at_offset(RAW_STRING)?;
|
||||
ctx.add_action(AssistId("add_hash"), "add hash to raw string", |edit| {
|
||||
|
@ -66,6 +109,21 @@ pub(crate) fn add_hash(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
|||
ctx.build()
|
||||
}
|
||||
|
||||
// Assist: remove_hash
|
||||
//
|
||||
// Removes a hash from a raw string literal.
|
||||
//
|
||||
// ```
|
||||
// fn main() {
|
||||
// r#"Hello,<|> World!"#;
|
||||
// }
|
||||
// ```
|
||||
// ->
|
||||
// ```
|
||||
// fn main() {
|
||||
// r"Hello, World!";
|
||||
// }
|
||||
// ```
|
||||
pub(crate) fn remove_hash(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
||||
let token = ctx.find_token_at_offset(RAW_STRING)?;
|
||||
let text = token.text().as_str();
|
||||
|
|
|
@ -1,12 +1,26 @@
|
|||
//! FIXME: write short doc here
|
||||
|
||||
use crate::{Assist, AssistCtx, AssistId};
|
||||
use hir::db::HirDatabase;
|
||||
use ra_syntax::{
|
||||
ast::{self, AstNode},
|
||||
TextUnit, T,
|
||||
};
|
||||
|
||||
use crate::{Assist, AssistCtx, AssistId};
|
||||
|
||||
// Assist: remove_dbg
|
||||
//
|
||||
// Removes `dbg!()` macro call.
|
||||
//
|
||||
// ```
|
||||
// fn main() {
|
||||
// <|>dbg!(92);
|
||||
// }
|
||||
// ```
|
||||
// ->
|
||||
// ```
|
||||
// fn main() {
|
||||
// 92;
|
||||
// }
|
||||
// ```
|
||||
pub(crate) fn remove_dbg(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
||||
let macro_call = ctx.find_node_at_offset::<ast::MacroCall>()?;
|
||||
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
//! FIXME: write short doc here
|
||||
|
||||
use format_buf::format;
|
||||
use hir::db::HirDatabase;
|
||||
use ra_fmt::extract_trivial_expression;
|
||||
|
@ -7,6 +5,32 @@ use ra_syntax::{ast, AstNode};
|
|||
|
||||
use crate::{Assist, AssistCtx, AssistId};
|
||||
|
||||
// Assist: replace_if_let_with_match
|
||||
//
|
||||
// Replaces `if let` with an else branch with a `match` expression.
|
||||
//
|
||||
// ```
|
||||
// enum Action { Move { distance: u32 }, Stop }
|
||||
//
|
||||
// fn handle(action: Action) {
|
||||
// <|>if let Action::Move { distance } = action {
|
||||
// foo(distance)
|
||||
// } else {
|
||||
// bar()
|
||||
// }
|
||||
// }
|
||||
// ```
|
||||
// ->
|
||||
// ```
|
||||
// enum Action { Move { distance: u32 }, Stop }
|
||||
//
|
||||
// fn handle(action: Action) {
|
||||
// match action {
|
||||
// Action::Move { distance } => foo(distance),
|
||||
// _ => bar(),
|
||||
// }
|
||||
// }
|
||||
// ```
|
||||
pub(crate) fn replace_if_let_with_match(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
||||
let if_expr: ast::IfExpr = ctx.find_node_at_offset()?;
|
||||
let cond = if_expr.condition()?;
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
//! FIXME: write short doc here
|
||||
|
||||
use std::iter::successors;
|
||||
|
||||
use hir::db::HirDatabase;
|
||||
|
@ -7,6 +5,17 @@ use ra_syntax::{ast, AstNode, TextUnit, T};
|
|||
|
||||
use crate::{Assist, AssistCtx, AssistId};
|
||||
|
||||
// Assist: split_import
|
||||
//
|
||||
// Wraps the tail of import into braces.
|
||||
//
|
||||
// ```
|
||||
// use std::<|>collections::HashMap;
|
||||
// ```
|
||||
// ->
|
||||
// ```
|
||||
// use std::{collections::HashMap};
|
||||
// ```
|
||||
pub(crate) fn split_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
||||
let colon_colon = ctx.find_token_at_offset(T![::])?;
|
||||
let path = ast::Path::cast(colon_colon.parent())?;
|
||||
|
|
|
@ -39,6 +39,23 @@ fn main() {
|
|||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn doctest_add_hash() {
|
||||
check(
|
||||
"add_hash",
|
||||
r#####"
|
||||
fn main() {
|
||||
r#"Hello,<|> World!"#;
|
||||
}
|
||||
"#####,
|
||||
r#####"
|
||||
fn main() {
|
||||
r##"Hello, World!"##;
|
||||
}
|
||||
"#####,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn doctest_add_impl() {
|
||||
check(
|
||||
|
@ -274,6 +291,40 @@ fn main() {
|
|||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn doctest_make_raw_string() {
|
||||
check(
|
||||
"make_raw_string",
|
||||
r#####"
|
||||
fn main() {
|
||||
"Hello,<|> World!";
|
||||
}
|
||||
"#####,
|
||||
r#####"
|
||||
fn main() {
|
||||
r#"Hello, World!"#;
|
||||
}
|
||||
"#####,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn doctest_make_usual_string() {
|
||||
check(
|
||||
"make_usual_string",
|
||||
r#####"
|
||||
fn main() {
|
||||
r#"Hello,<|> "World!""#;
|
||||
}
|
||||
"#####,
|
||||
r#####"
|
||||
fn main() {
|
||||
"Hello, \"World!\"";
|
||||
}
|
||||
"#####,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn doctest_merge_match_arms() {
|
||||
check(
|
||||
|
@ -370,3 +421,78 @@ fn handle(action: Action) {
|
|||
"#####,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn doctest_remove_dbg() {
|
||||
check(
|
||||
"remove_dbg",
|
||||
r#####"
|
||||
fn main() {
|
||||
<|>dbg!(92);
|
||||
}
|
||||
"#####,
|
||||
r#####"
|
||||
fn main() {
|
||||
92;
|
||||
}
|
||||
"#####,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn doctest_remove_hash() {
|
||||
check(
|
||||
"remove_hash",
|
||||
r#####"
|
||||
fn main() {
|
||||
r#"Hello,<|> World!"#;
|
||||
}
|
||||
"#####,
|
||||
r#####"
|
||||
fn main() {
|
||||
r"Hello, World!";
|
||||
}
|
||||
"#####,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn doctest_replace_if_let_with_match() {
|
||||
check(
|
||||
"replace_if_let_with_match",
|
||||
r#####"
|
||||
enum Action { Move { distance: u32 }, Stop }
|
||||
|
||||
fn handle(action: Action) {
|
||||
<|>if let Action::Move { distance } = action {
|
||||
foo(distance)
|
||||
} else {
|
||||
bar()
|
||||
}
|
||||
}
|
||||
"#####,
|
||||
r#####"
|
||||
enum Action { Move { distance: u32 }, Stop }
|
||||
|
||||
fn handle(action: Action) {
|
||||
match action {
|
||||
Action::Move { distance } => foo(distance),
|
||||
_ => bar(),
|
||||
}
|
||||
}
|
||||
"#####,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn doctest_split_import() {
|
||||
check(
|
||||
"split_import",
|
||||
r#####"
|
||||
use std::<|>collections::HashMap;
|
||||
"#####,
|
||||
r#####"
|
||||
use std::{collections::HashMap};
|
||||
"#####,
|
||||
)
|
||||
}
|
||||
|
|
|
@ -38,6 +38,22 @@ fn main() {
|
|||
}
|
||||
```
|
||||
|
||||
## `add_hash`
|
||||
|
||||
Adds a hash to a raw string literal.
|
||||
|
||||
```rust
|
||||
// BEFORE
|
||||
fn main() {
|
||||
r#"Hello,┃ World!"#;
|
||||
}
|
||||
|
||||
// AFTER
|
||||
fn main() {
|
||||
r##"Hello, World!"##;
|
||||
}
|
||||
```
|
||||
|
||||
## `add_impl`
|
||||
|
||||
Adds a new inherent impl for a type.
|
||||
|
@ -266,6 +282,38 @@ fn main() {
|
|||
}
|
||||
```
|
||||
|
||||
## `make_raw_string`
|
||||
|
||||
Adds `r#` to a plain string literal.
|
||||
|
||||
```rust
|
||||
// BEFORE
|
||||
fn main() {
|
||||
"Hello,┃ World!";
|
||||
}
|
||||
|
||||
// AFTER
|
||||
fn main() {
|
||||
r#"Hello, World!"#;
|
||||
}
|
||||
```
|
||||
|
||||
## `make_usual_string`
|
||||
|
||||
Turns a raw string into a plain string.
|
||||
|
||||
```rust
|
||||
// BEFORE
|
||||
fn main() {
|
||||
r#"Hello,┃ "World!""#;
|
||||
}
|
||||
|
||||
// AFTER
|
||||
fn main() {
|
||||
"Hello, \"World!\"";
|
||||
}
|
||||
```
|
||||
|
||||
## `merge_match_arms`
|
||||
|
||||
Merges identical match arms.
|
||||
|
@ -358,3 +406,74 @@ fn handle(action: Action) {
|
|||
}
|
||||
}
|
||||
```
|
||||
|
||||
## `remove_dbg`
|
||||
|
||||
Removes `dbg!()` macro call.
|
||||
|
||||
```rust
|
||||
// BEFORE
|
||||
fn main() {
|
||||
┃dbg!(92);
|
||||
}
|
||||
|
||||
// AFTER
|
||||
fn main() {
|
||||
92;
|
||||
}
|
||||
```
|
||||
|
||||
## `remove_hash`
|
||||
|
||||
Removes a hash from a raw string literal.
|
||||
|
||||
```rust
|
||||
// BEFORE
|
||||
fn main() {
|
||||
r#"Hello,┃ World!"#;
|
||||
}
|
||||
|
||||
// AFTER
|
||||
fn main() {
|
||||
r"Hello, World!";
|
||||
}
|
||||
```
|
||||
|
||||
## `replace_if_let_with_match`
|
||||
|
||||
Replaces `if let` with an else branch with a `match` expression.
|
||||
|
||||
```rust
|
||||
// BEFORE
|
||||
enum Action { Move { distance: u32 }, Stop }
|
||||
|
||||
fn handle(action: Action) {
|
||||
┃if let Action::Move { distance } = action {
|
||||
foo(distance)
|
||||
} else {
|
||||
bar()
|
||||
}
|
||||
}
|
||||
|
||||
// AFTER
|
||||
enum Action { Move { distance: u32 }, Stop }
|
||||
|
||||
fn handle(action: Action) {
|
||||
match action {
|
||||
Action::Move { distance } => foo(distance),
|
||||
_ => bar(),
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## `split_import`
|
||||
|
||||
Wraps the tail of import into braces.
|
||||
|
||||
```rust
|
||||
// BEFORE
|
||||
use std::┃collections::HashMap;
|
||||
|
||||
// AFTER
|
||||
use std::{collections::HashMap};
|
||||
```
|
||||
|
|
|
@ -118,180 +118,6 @@ impl Debug<|> for Foo {
|
|||
}
|
||||
```
|
||||
|
||||
- Fill struct fields
|
||||
|
||||
```rust
|
||||
// before:
|
||||
struct S<'a, D> {
|
||||
a: u32,
|
||||
b: String,
|
||||
c: (i32, i32),
|
||||
d: D,
|
||||
r: &'a str,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let s = S<|> {}
|
||||
}
|
||||
|
||||
// after:
|
||||
struct S<'a, D> {
|
||||
a: u32,
|
||||
b: String,
|
||||
c: (i32, i32),
|
||||
d: D,
|
||||
r: &'a str,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let s = <|>S {
|
||||
a: (),
|
||||
b: (),
|
||||
c: (),
|
||||
d: (),
|
||||
r: (),
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
- Remove `dbg!`
|
||||
|
||||
```rust
|
||||
// before:
|
||||
fn foo(n: usize) {
|
||||
if let Some(_) = dbg!(n.<|>checked_sub(4)) {
|
||||
// ...
|
||||
}
|
||||
}
|
||||
|
||||
// after:
|
||||
fn foo(n: usize) {
|
||||
if let Some(_) = n.<|>checked_sub(4) {
|
||||
// ...
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
- Replace if-let with match:
|
||||
|
||||
```rust
|
||||
// before:
|
||||
impl VariantData {
|
||||
pub fn is_struct(&self) -> bool {
|
||||
if <|>let VariantData::Struct(..) = *self {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// after:
|
||||
impl VariantData {
|
||||
pub fn is_struct(&self) -> bool {
|
||||
<|>match *self {
|
||||
VariantData::Struct(..) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
- Split import
|
||||
|
||||
```rust
|
||||
// before:
|
||||
use crate:<|>:db::{RootDatabase, FileSymbol};
|
||||
// after:
|
||||
use crate::{<|>db::{RootDatabase, FileSymbol}};
|
||||
```
|
||||
|
||||
- Move if condition to match arm guard
|
||||
```rust
|
||||
// before:
|
||||
fn f() {
|
||||
let mut t = 'a';
|
||||
let chars = "abcd";
|
||||
match t {
|
||||
'\r' => if chars.clone().next().is_some() {
|
||||
t = 'e';<|>
|
||||
false
|
||||
},
|
||||
_ => true
|
||||
}
|
||||
}
|
||||
|
||||
// after:
|
||||
fn f() {
|
||||
let mut t = 'a';
|
||||
let chars = "abcd";
|
||||
match t {
|
||||
'\r' <|>if chars.clone().next().is_some() => {
|
||||
t = 'e';
|
||||
false
|
||||
},
|
||||
_ => true
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
- Make raw string unescaped
|
||||
|
||||
```rust
|
||||
// before:
|
||||
fn f() {
|
||||
let s = <|>"ab\ncd";
|
||||
}
|
||||
|
||||
// after:
|
||||
fn f() {
|
||||
let s = <|>r#"ab
|
||||
cd"#;
|
||||
}
|
||||
```
|
||||
|
||||
- Make usual string
|
||||
|
||||
```rust
|
||||
// before:
|
||||
fn f() {
|
||||
let s = <|>r#"abcd"#;
|
||||
}
|
||||
|
||||
// after:
|
||||
fn f() {
|
||||
let s = <|>"abcd";
|
||||
}
|
||||
```
|
||||
|
||||
- Add hash
|
||||
|
||||
```rust
|
||||
// before:
|
||||
fn f() {
|
||||
let s = <|>r"abcd";
|
||||
}
|
||||
|
||||
// after:
|
||||
fn f() {
|
||||
let s = <|>r#"abcd"#;
|
||||
}
|
||||
```
|
||||
|
||||
- Remove hash
|
||||
|
||||
```rust
|
||||
// before:
|
||||
fn f() {
|
||||
let s = <|>r#"abcd"#;
|
||||
}
|
||||
|
||||
// after:
|
||||
fn f() {
|
||||
let s = <|>r"abcd";
|
||||
}
|
||||
```
|
||||
|
||||
### Magic Completions
|
||||
|
||||
In addition to usual reference completion, rust-analyzer provides some ✨magic✨
|
||||
|
|
Loading…
Reference in a new issue