document some more assists
This commit is contained in:
parent
85984b09e1
commit
a490ba06fa
@ -1,9 +1,31 @@
|
||||
//! FIXME: write short doc here
|
||||
|
||||
use crate::{Assist, AssistCtx, AssistId, TextRange, TextUnit};
|
||||
use hir::db::HirDatabase;
|
||||
use ra_syntax::ast::{AstNode, MatchArm};
|
||||
|
||||
// Assist: merge_match_arms
|
||||
//
|
||||
// Merges identical match arms.
|
||||
//
|
||||
// ```
|
||||
// enum Action { Move { distance: u32 }, Stop }
|
||||
//
|
||||
// fn handle(action: Action) {
|
||||
// match action {
|
||||
// <|>Action::Move(..) => foo(),
|
||||
// Action::Stop => foo(),
|
||||
// }
|
||||
// }
|
||||
// ```
|
||||
// ->
|
||||
// ```
|
||||
// enum Action { Move { distance: u32 }, Stop }
|
||||
//
|
||||
// fn handle(action: Action) {
|
||||
// match action {
|
||||
// Action::Move(..) | Action::Stop => foo(),
|
||||
// }
|
||||
// }
|
||||
// ```
|
||||
pub(crate) fn merge_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
||||
let current_arm = ctx.node_at_offset::<MatchArm>()?;
|
||||
|
||||
|
@ -1,5 +1,3 @@
|
||||
//! FIXME: write short doc here
|
||||
|
||||
use hir::db::HirDatabase;
|
||||
use ra_syntax::{
|
||||
ast::{self, edit, make, AstNode, NameOwner, TypeBoundsOwner},
|
||||
@ -9,6 +7,21 @@
|
||||
|
||||
use crate::{Assist, AssistCtx, AssistId};
|
||||
|
||||
// Assist: move_bounds_to_where_clause
|
||||
//
|
||||
// Moves inline type bounds to a where clause.
|
||||
//
|
||||
// ```
|
||||
// fn apply<T, U, <|>F: FnOnce(T) -> U>(f: F, x: T) -> U {
|
||||
// f(x)
|
||||
// }
|
||||
// ```
|
||||
// ->
|
||||
// ```
|
||||
// fn apply<T, U, F>(f: F, x: T) -> U where F: FnOnce(T) -> U {
|
||||
// f(x)
|
||||
// }
|
||||
// ```
|
||||
pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
||||
let type_param_list = ctx.node_at_offset::<ast::TypeParamList>()?;
|
||||
|
||||
|
@ -1,5 +1,3 @@
|
||||
//! FIXME: write short doc here
|
||||
|
||||
use hir::db::HirDatabase;
|
||||
use ra_syntax::{
|
||||
ast,
|
||||
@ -9,6 +7,31 @@
|
||||
|
||||
use crate::{Assist, AssistCtx, AssistId};
|
||||
|
||||
// Assist: move_guard_to_arm_body
|
||||
//
|
||||
// Moves match guard into match arm body.
|
||||
//
|
||||
// ```
|
||||
// enum Action { Move { distance: u32 }, Stop }
|
||||
//
|
||||
// fn handle(action: Action) {
|
||||
// match action {
|
||||
// Action::Move { distance } <|>if distance > 10 => foo(),
|
||||
// _ => (),
|
||||
// }
|
||||
// }
|
||||
// ```
|
||||
// ->
|
||||
// ```
|
||||
// enum Action { Move { distance: u32 }, Stop }
|
||||
//
|
||||
// fn handle(action: Action) {
|
||||
// match action {
|
||||
// Action::Move { distance } => if distance > 10 { foo() },
|
||||
// _ => (),
|
||||
// }
|
||||
// }
|
||||
// ```
|
||||
pub(crate) fn move_guard_to_arm_body(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
||||
let match_arm = ctx.node_at_offset::<MatchArm>()?;
|
||||
let guard = match_arm.guard()?;
|
||||
@ -42,6 +65,31 @@ pub(crate) fn move_guard_to_arm_body(mut ctx: AssistCtx<impl HirDatabase>) -> Op
|
||||
ctx.build()
|
||||
}
|
||||
|
||||
// Assist: move_arm_cond_to_match_guard
|
||||
//
|
||||
// Moves if expression from match arm body into a guard.
|
||||
//
|
||||
// ```
|
||||
// enum Action { Move { distance: u32 }, Stop }
|
||||
//
|
||||
// fn handle(action: Action) {
|
||||
// match action {
|
||||
// Action::Move { distance } => <|>if distance > 10 { foo() },
|
||||
// _ => (),
|
||||
// }
|
||||
// }
|
||||
// ```
|
||||
// ->
|
||||
// ```
|
||||
// enum Action { Move { distance: u32 }, Stop }
|
||||
//
|
||||
// fn handle(action: Action) {
|
||||
// match action {
|
||||
// Action::Move { distance } if distance > 10 => foo(),
|
||||
// _ => (),
|
||||
// }
|
||||
// }
|
||||
// ```
|
||||
pub(crate) fn move_arm_cond_to_match_guard(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
||||
let match_arm: MatchArm = ctx.node_at_offset::<MatchArm>()?;
|
||||
let last_match_pat = match_arm.pats().last()?;
|
||||
|
@ -273,3 +273,100 @@ fn main() {
|
||||
"#####,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn doctest_merge_match_arms() {
|
||||
check(
|
||||
"merge_match_arms",
|
||||
r#####"
|
||||
enum Action { Move { distance: u32 }, Stop }
|
||||
|
||||
fn handle(action: Action) {
|
||||
match action {
|
||||
<|>Action::Move(..) => foo(),
|
||||
Action::Stop => foo(),
|
||||
}
|
||||
}
|
||||
"#####,
|
||||
r#####"
|
||||
enum Action { Move { distance: u32 }, Stop }
|
||||
|
||||
fn handle(action: Action) {
|
||||
match action {
|
||||
Action::Move(..) | Action::Stop => foo(),
|
||||
}
|
||||
}
|
||||
"#####,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn doctest_move_arm_cond_to_match_guard() {
|
||||
check(
|
||||
"move_arm_cond_to_match_guard",
|
||||
r#####"
|
||||
enum Action { Move { distance: u32 }, Stop }
|
||||
|
||||
fn handle(action: Action) {
|
||||
match action {
|
||||
Action::Move { distance } => <|>if distance > 10 { foo() },
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
"#####,
|
||||
r#####"
|
||||
enum Action { Move { distance: u32 }, Stop }
|
||||
|
||||
fn handle(action: Action) {
|
||||
match action {
|
||||
Action::Move { distance } if distance > 10 => foo(),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
"#####,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn doctest_move_bounds_to_where_clause() {
|
||||
check(
|
||||
"move_bounds_to_where_clause",
|
||||
r#####"
|
||||
fn apply<T, U, <|>F: FnOnce(T) -> U>(f: F, x: T) -> U {
|
||||
f(x)
|
||||
}
|
||||
"#####,
|
||||
r#####"
|
||||
fn apply<T, U, F>(f: F, x: T) -> U where F: FnOnce(T) -> U {
|
||||
f(x)
|
||||
}
|
||||
"#####,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn doctest_move_guard_to_arm_body() {
|
||||
check(
|
||||
"move_guard_to_arm_body",
|
||||
r#####"
|
||||
enum Action { Move { distance: u32 }, Stop }
|
||||
|
||||
fn handle(action: Action) {
|
||||
match action {
|
||||
Action::Move { distance } <|>if distance > 10 => foo(),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
"#####,
|
||||
r#####"
|
||||
enum Action { Move { distance: u32 }, Stop }
|
||||
|
||||
fn handle(action: Action) {
|
||||
match action {
|
||||
Action::Move { distance } => if distance > 10 { foo() },
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
"#####,
|
||||
)
|
||||
}
|
||||
|
@ -265,3 +265,96 @@ fn main() {
|
||||
var_name * 4;
|
||||
}
|
||||
```
|
||||
|
||||
## `merge_match_arms`
|
||||
|
||||
Merges identical match arms.
|
||||
|
||||
```rust
|
||||
// BEFORE
|
||||
enum Action { Move { distance: u32 }, Stop }
|
||||
|
||||
fn handle(action: Action) {
|
||||
match action {
|
||||
┃Action::Move(..) => foo(),
|
||||
Action::Stop => foo(),
|
||||
}
|
||||
}
|
||||
|
||||
// AFTER
|
||||
enum Action { Move { distance: u32 }, Stop }
|
||||
|
||||
fn handle(action: Action) {
|
||||
match action {
|
||||
Action::Move(..) | Action::Stop => foo(),
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## `move_arm_cond_to_match_guard`
|
||||
|
||||
Moves if expression from match arm body into a guard.
|
||||
|
||||
```rust
|
||||
// BEFORE
|
||||
enum Action { Move { distance: u32 }, Stop }
|
||||
|
||||
fn handle(action: Action) {
|
||||
match action {
|
||||
Action::Move { distance } => ┃if distance > 10 { foo() },
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
// AFTER
|
||||
enum Action { Move { distance: u32 }, Stop }
|
||||
|
||||
fn handle(action: Action) {
|
||||
match action {
|
||||
Action::Move { distance } if distance > 10 => foo(),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## `move_bounds_to_where_clause`
|
||||
|
||||
Moves inline type bounds to a where clause.
|
||||
|
||||
```rust
|
||||
// BEFORE
|
||||
fn apply<T, U, ┃F: FnOnce(T) -> U>(f: F, x: T) -> U {
|
||||
f(x)
|
||||
}
|
||||
|
||||
// AFTER
|
||||
fn apply<T, U, F>(f: F, x: T) -> U where F: FnOnce(T) -> U {
|
||||
f(x)
|
||||
}
|
||||
```
|
||||
|
||||
## `move_guard_to_arm_body`
|
||||
|
||||
Moves match guard into match arm body.
|
||||
|
||||
```rust
|
||||
// BEFORE
|
||||
enum Action { Move { distance: u32 }, Stop }
|
||||
|
||||
fn handle(action: Action) {
|
||||
match action {
|
||||
Action::Move { distance } ┃if distance > 10 => foo(),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
// AFTER
|
||||
enum Action { Move { distance: u32 }, Stop }
|
||||
|
||||
fn handle(action: Action) {
|
||||
match action {
|
||||
Action::Move { distance } => if distance > 10 { foo() },
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
```
|
||||
|
@ -154,45 +154,6 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
- Flip `,`
|
||||
|
||||
```rust
|
||||
// before:
|
||||
fn foo(x: usize,<|> dim: (usize, usize)) {}
|
||||
// after:
|
||||
fn foo(dim: (usize, usize), x: usize) {}
|
||||
```
|
||||
|
||||
- Introduce variable:
|
||||
|
||||
```rust
|
||||
// before:
|
||||
fn foo() {
|
||||
foo(<|>1 + 1<|>);
|
||||
}
|
||||
|
||||
// after:
|
||||
fn foo() {
|
||||
let var_name = 1 + 1;
|
||||
foo(var_name);
|
||||
}
|
||||
```
|
||||
|
||||
- Inline local variable:
|
||||
|
||||
```rust
|
||||
// before:
|
||||
fn foo() {
|
||||
let a<|> = 1 + 1;
|
||||
let b = a * 10;
|
||||
}
|
||||
|
||||
// after:
|
||||
fn foo() {
|
||||
let b = (1 + 1) * 10;
|
||||
}
|
||||
```
|
||||
|
||||
- Remove `dbg!`
|
||||
|
||||
```rust
|
||||
@ -245,41 +206,6 @@ use crate:<|>:db::{RootDatabase, FileSymbol};
|
||||
use crate::{<|>db::{RootDatabase, FileSymbol}};
|
||||
```
|
||||
|
||||
- Flip binary expression
|
||||
|
||||
```rust
|
||||
// before:
|
||||
fn foo() {
|
||||
if 1 <<|> 2 {
|
||||
println!("Who would have thought?");
|
||||
}
|
||||
}
|
||||
// after:
|
||||
fn foo() {
|
||||
if 2 ><|> 1 {
|
||||
println!("Who would have thought?");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
- Move guard expression to match arm body
|
||||
```rust
|
||||
// before:
|
||||
fn f() {
|
||||
match x {
|
||||
<|>y @ 4 | y @ 5 if y > 5 => true,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
// after:
|
||||
fn f() {
|
||||
match x {
|
||||
y @ 4 | y @ 5 => if y > 5 { <|>true },
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
- Move if condition to match arm guard
|
||||
```rust
|
||||
// before:
|
||||
@ -309,16 +235,6 @@ fn f() {
|
||||
}
|
||||
```
|
||||
|
||||
- Move type bounds to where clause
|
||||
|
||||
```rust
|
||||
// before:
|
||||
fn foo<T: u32, F: FnOnce(T) -> T>() {}
|
||||
|
||||
// after:
|
||||
fn foo<T, F>() where T: u32, F: FnOnce(T) -> T {}
|
||||
```
|
||||
|
||||
- Make raw string unescaped
|
||||
|
||||
```rust
|
||||
|
Loading…
Reference in New Issue
Block a user