2020-02-05 04:53:33 -06:00
|
|
|
use std::iter::successors;
|
|
|
|
|
|
|
|
use ra_syntax::{
|
2020-03-19 05:38:26 -05:00
|
|
|
algo::neighbor,
|
2020-02-05 04:53:33 -06:00
|
|
|
ast::{self, AstNode},
|
2020-04-24 16:40:41 -05:00
|
|
|
Direction, TextSize,
|
2020-02-05 04:53:33 -06:00
|
|
|
};
|
2020-02-05 04:46:05 -06:00
|
|
|
|
2020-02-05 04:53:33 -06:00
|
|
|
use crate::{Assist, AssistCtx, AssistId, TextRange};
|
2019-07-29 15:59:52 -05:00
|
|
|
|
2019-10-27 03:26:46 -05:00
|
|
|
// 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(),
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// ```
|
2020-02-06 09:58:57 -06:00
|
|
|
pub(crate) fn merge_match_arms(ctx: AssistCtx) -> Option<Assist> {
|
2020-02-05 04:46:05 -06:00
|
|
|
let current_arm = ctx.find_node_at_offset::<ast::MatchArm>()?;
|
2019-07-29 15:59:52 -05:00
|
|
|
// Don't try to handle arms with guards for now - can add support for this later
|
2020-02-05 04:53:33 -06:00
|
|
|
if current_arm.guard().is_some() {
|
2019-07-29 15:59:52 -05:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let current_expr = current_arm.expr()?;
|
2020-02-05 04:53:33 -06:00
|
|
|
let current_text_range = current_arm.syntax().text_range();
|
2020-02-05 06:41:43 -06:00
|
|
|
|
|
|
|
enum CursorPos {
|
2020-04-24 16:40:41 -05:00
|
|
|
InExpr(TextSize),
|
|
|
|
InPat(TextSize),
|
2020-02-05 06:41:43 -06:00
|
|
|
}
|
|
|
|
let cursor_pos = ctx.frange.range.start();
|
|
|
|
let cursor_pos = if current_expr.syntax().text_range().contains(cursor_pos) {
|
|
|
|
CursorPos::InExpr(current_text_range.end() - cursor_pos)
|
|
|
|
} else {
|
|
|
|
CursorPos::InPat(cursor_pos)
|
|
|
|
};
|
2019-07-29 15:59:52 -05:00
|
|
|
|
2020-02-05 04:53:33 -06:00
|
|
|
// We check if the following match arms match this one. We could, but don't,
|
|
|
|
// compare to the previous match arm as well.
|
2020-03-19 05:38:26 -05:00
|
|
|
let arms_to_merge = successors(Some(current_arm), |it| neighbor(it, Direction::Next))
|
2020-02-05 04:53:33 -06:00
|
|
|
.take_while(|arm| {
|
|
|
|
if arm.guard().is_some() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
match arm.expr() {
|
|
|
|
Some(expr) => expr.syntax().text() == current_expr.syntax().text(),
|
|
|
|
None => false,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
if arms_to_merge.len() <= 1 {
|
2019-07-29 15:59:52 -05:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2020-01-14 11:32:26 -06:00
|
|
|
ctx.add_assist(AssistId("merge_match_arms"), "Merge match arms", |edit| {
|
2020-02-05 04:53:33 -06:00
|
|
|
let pats = if arms_to_merge.iter().any(contains_placeholder) {
|
2019-07-29 15:59:52 -05:00
|
|
|
"_".into()
|
|
|
|
} else {
|
2020-02-05 04:53:33 -06:00
|
|
|
arms_to_merge
|
|
|
|
.iter()
|
2020-02-09 12:57:01 -06:00
|
|
|
.filter_map(ast::MatchArm::pat)
|
2019-07-29 15:59:52 -05:00
|
|
|
.map(|x| x.syntax().to_string())
|
2020-02-05 04:53:33 -06:00
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(" | ")
|
2019-07-29 15:59:52 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
let arm = format!("{} => {}", pats, current_expr.syntax().text());
|
|
|
|
|
2020-02-05 04:53:33 -06:00
|
|
|
let start = arms_to_merge.first().unwrap().syntax().text_range().start();
|
|
|
|
let end = arms_to_merge.last().unwrap().syntax().text_range().end();
|
2019-07-29 15:59:52 -05:00
|
|
|
|
2020-02-05 04:53:33 -06:00
|
|
|
edit.target(current_text_range);
|
2020-02-05 06:41:43 -06:00
|
|
|
edit.set_cursor(match cursor_pos {
|
2020-04-24 17:57:47 -05:00
|
|
|
CursorPos::InExpr(back_offset) => start + TextSize::of(&arm) - back_offset,
|
2020-02-05 06:41:43 -06:00
|
|
|
CursorPos::InPat(offset) => offset,
|
|
|
|
});
|
2020-04-24 16:40:41 -05:00
|
|
|
edit.replace(TextRange::new(start, end), arm);
|
2019-10-27 09:35:37 -05:00
|
|
|
})
|
2019-07-29 15:59:52 -05:00
|
|
|
}
|
|
|
|
|
2020-02-05 04:46:05 -06:00
|
|
|
fn contains_placeholder(a: &ast::MatchArm) -> bool {
|
2020-02-09 12:57:01 -06:00
|
|
|
match a.pat() {
|
|
|
|
Some(ra_syntax::ast::Pat::PlaceholderPat(..)) => true,
|
2020-02-05 04:46:05 -06:00
|
|
|
_ => false,
|
2020-02-09 12:57:01 -06:00
|
|
|
}
|
2020-02-05 04:46:05 -06:00
|
|
|
}
|
|
|
|
|
2019-07-29 15:59:52 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::helpers::{check_assist, check_assist_not_applicable};
|
|
|
|
|
2020-03-19 05:38:26 -05:00
|
|
|
use super::*;
|
|
|
|
|
2019-07-29 15:59:52 -05:00
|
|
|
#[test]
|
|
|
|
fn merge_match_arms_single_patterns() {
|
|
|
|
check_assist(
|
|
|
|
merge_match_arms,
|
|
|
|
r#"
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum X { A, B, C }
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let x = X::A;
|
|
|
|
let y = match x {
|
|
|
|
X::A => { 1i32<|> }
|
|
|
|
X::B => { 1i32 }
|
|
|
|
X::C => { 2i32 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum X { A, B, C }
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let x = X::A;
|
|
|
|
let y = match x {
|
|
|
|
X::A | X::B => { 1i32<|> }
|
|
|
|
X::C => { 2i32 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn merge_match_arms_multiple_patterns() {
|
|
|
|
check_assist(
|
|
|
|
merge_match_arms,
|
|
|
|
r#"
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum X { A, B, C, D, E }
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let x = X::A;
|
|
|
|
let y = match x {
|
|
|
|
X::A | X::B => {<|> 1i32 },
|
|
|
|
X::C | X::D => { 1i32 },
|
|
|
|
X::E => { 2i32 },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum X { A, B, C, D, E }
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let x = X::A;
|
|
|
|
let y = match x {
|
|
|
|
X::A | X::B | X::C | X::D => {<|> 1i32 },
|
|
|
|
X::E => { 2i32 },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn merge_match_arms_placeholder_pattern() {
|
|
|
|
check_assist(
|
|
|
|
merge_match_arms,
|
|
|
|
r#"
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum X { A, B, C, D, E }
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let x = X::A;
|
|
|
|
let y = match x {
|
|
|
|
X::A => { 1i32 },
|
|
|
|
X::B => { 2i<|>32 },
|
|
|
|
_ => { 2i32 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum X { A, B, C, D, E }
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let x = X::A;
|
|
|
|
let y = match x {
|
|
|
|
X::A => { 1i32 },
|
|
|
|
_ => { 2i<|>32 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-05 04:53:33 -06:00
|
|
|
#[test]
|
|
|
|
fn merges_all_subsequent_arms() {
|
|
|
|
check_assist(
|
|
|
|
merge_match_arms,
|
|
|
|
r#"
|
|
|
|
enum X { A, B, C, D, E }
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
match X::A {
|
2020-02-05 06:41:43 -06:00
|
|
|
X::A<|> => 92,
|
2020-02-05 04:53:33 -06:00
|
|
|
X::B => 92,
|
|
|
|
X::C => 92,
|
|
|
|
X::D => 62,
|
|
|
|
_ => panic!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
enum X { A, B, C, D, E }
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
match X::A {
|
2020-02-05 06:41:43 -06:00
|
|
|
X::A<|> | X::B | X::C => 92,
|
2020-02-05 04:53:33 -06:00
|
|
|
X::D => 62,
|
|
|
|
_ => panic!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-07-29 15:59:52 -05:00
|
|
|
#[test]
|
|
|
|
fn merge_match_arms_rejects_guards() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
merge_match_arms,
|
|
|
|
r#"
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum X {
|
|
|
|
A(i32),
|
|
|
|
B,
|
|
|
|
C
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let x = X::A;
|
|
|
|
let y = match x {
|
|
|
|
X::A(a) if a > 5 => { <|>1i32 },
|
|
|
|
X::B => { 1i32 },
|
|
|
|
X::C => { 2i32 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|