diff --git a/crates/ra_assists/src/assists/merge_match_arms.rs b/crates/ra_assists/src/assists/merge_match_arms.rs index 17baa98f92..f5ddd71594 100644 --- a/crates/ra_assists/src/assists/merge_match_arms.rs +++ b/crates/ra_assists/src/assists/merge_match_arms.rs @@ -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) -> Option { let current_arm = ctx.node_at_offset::()?; diff --git a/crates/ra_assists/src/assists/move_bounds.rs b/crates/ra_assists/src/assists/move_bounds.rs index d2444b6b9c..f96e19a9f0 100644 --- a/crates/ra_assists/src/assists/move_bounds.rs +++ b/crates/ra_assists/src/assists/move_bounds.rs @@ -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 ra_syntax::{ use crate::{Assist, AssistCtx, AssistId}; +// Assist: move_bounds_to_where_clause +// +// Moves inline type bounds to a where clause. +// +// ``` +// fn applyF: FnOnce(T) -> U>(f: F, x: T) -> U { +// f(x) +// } +// ``` +// -> +// ``` +// fn apply(f: F, x: T) -> U where F: FnOnce(T) -> U { +// f(x) +// } +// ``` pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx) -> Option { let type_param_list = ctx.node_at_offset::()?; diff --git a/crates/ra_assists/src/assists/move_guard.rs b/crates/ra_assists/src/assists/move_guard.rs index 51aea63340..36c95128d3 100644 --- a/crates/ra_assists/src/assists/move_guard.rs +++ b/crates/ra_assists/src/assists/move_guard.rs @@ -1,5 +1,3 @@ -//! FIXME: write short doc here - use hir::db::HirDatabase; use ra_syntax::{ ast, @@ -9,6 +7,31 @@ use ra_syntax::{ 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) -> Option { let match_arm = ctx.node_at_offset::()?; let guard = match_arm.guard()?; @@ -42,6 +65,31 @@ pub(crate) fn move_guard_to_arm_body(mut ctx: AssistCtx) -> 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) -> Option { let match_arm: MatchArm = ctx.node_at_offset::()?; let last_match_pat = match_arm.pats().last()?; diff --git a/crates/ra_assists/src/doc_tests/generated.rs b/crates/ra_assists/src/doc_tests/generated.rs index b96d5772e0..09677af68f 100644 --- a/crates/ra_assists/src/doc_tests/generated.rs +++ b/crates/ra_assists/src/doc_tests/generated.rs @@ -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 applyF: FnOnce(T) -> U>(f: F, x: T) -> U { + f(x) +} +"#####, + r#####" +fn apply(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() }, + _ => (), + } +} +"#####, + ) +} diff --git a/docs/user/assists.md b/docs/user/assists.md index ee1cfa142d..34a95696ca 100644 --- a/docs/user/assists.md +++ b/docs/user/assists.md @@ -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 U>(f: F, x: T) -> U { + f(x) +} + +// AFTER +fn apply(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() }, + _ => (), + } +} +``` diff --git a/docs/user/features.md b/docs/user/features.md index 39dab710dd..2e213e34c3 100644 --- a/docs/user/features.md +++ b/docs/user/features.md @@ -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>() {} - -// after: -fn foo() where T: u32, F: FnOnce(T) -> T {} -``` - - Make raw string unescaped ```rust