aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_assists/src/assists/merge_match_arms.rs23
1 files changed, 12 insertions, 11 deletions
diff --git a/crates/ra_assists/src/assists/merge_match_arms.rs b/crates/ra_assists/src/assists/merge_match_arms.rs
index aca391155..885d5036d 100644
--- a/crates/ra_assists/src/assists/merge_match_arms.rs
+++ b/crates/ra_assists/src/assists/merge_match_arms.rs
@@ -1,6 +1,7 @@
1use crate::{Assist, AssistCtx, AssistId, TextRange, TextUnit};
2use hir::db::HirDatabase; 1use hir::db::HirDatabase;
3use ra_syntax::ast::{AstNode, MatchArm}; 2use ra_syntax::ast::{self, AstNode};
3
4use crate::{Assist, AssistCtx, AssistId, TextRange, TextUnit};
4 5
5// Assist: merge_match_arms 6// Assist: merge_match_arms
6// 7//
@@ -27,12 +28,12 @@ use ra_syntax::ast::{AstNode, MatchArm};
27// } 28// }
28// ``` 29// ```
29pub(crate) fn merge_match_arms(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 30pub(crate) fn merge_match_arms(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
30 let current_arm = ctx.find_node_at_offset::<MatchArm>()?; 31 let current_arm = ctx.find_node_at_offset::<ast::MatchArm>()?;
31 32
32 // We check if the following match arm matches this one. We could, but don't, 33 // We check if the following match arm matches this one. We could, but don't,
33 // compare to the previous match arm as well. 34 // compare to the previous match arm as well.
34 let next = current_arm.syntax().next_sibling(); 35 let next = current_arm.syntax().next_sibling();
35 let next_arm = MatchArm::cast(next?)?; 36 let next_arm = ast::MatchArm::cast(next?)?;
36 37
37 // Don't try to handle arms with guards for now - can add support for this later 38 // Don't try to handle arms with guards for now - can add support for this later
38 if current_arm.guard().is_some() || next_arm.guard().is_some() { 39 if current_arm.guard().is_some() || next_arm.guard().is_some() {
@@ -53,13 +54,6 @@ pub(crate) fn merge_match_arms(ctx: AssistCtx<impl HirDatabase>) -> Option<Assis
53 let cursor_to_end = current_arm.syntax().text_range().end() - ctx.frange.range.start(); 54 let cursor_to_end = current_arm.syntax().text_range().end() - ctx.frange.range.start();
54 55
55 ctx.add_assist(AssistId("merge_match_arms"), "Merge match arms", |edit| { 56 ctx.add_assist(AssistId("merge_match_arms"), "Merge match arms", |edit| {
56 fn contains_placeholder(a: &MatchArm) -> bool {
57 a.pats().any(|x| match x {
58 ra_syntax::ast::Pat::PlaceholderPat(..) => true,
59 _ => false,
60 })
61 }
62
63 let pats = if contains_placeholder(&current_arm) || contains_placeholder(&next_arm) { 57 let pats = if contains_placeholder(&current_arm) || contains_placeholder(&next_arm) {
64 "_".into() 58 "_".into()
65 } else { 59 } else {
@@ -83,6 +77,13 @@ pub(crate) fn merge_match_arms(ctx: AssistCtx<impl HirDatabase>) -> Option<Assis
83 }) 77 })
84} 78}
85 79
80fn contains_placeholder(a: &ast::MatchArm) -> bool {
81 a.pats().any(|x| match x {
82 ra_syntax::ast::Pat::PlaceholderPat(..) => true,
83 _ => false,
84 })
85}
86
86#[cfg(test)] 87#[cfg(test)]
87mod tests { 88mod tests {
88 use super::merge_match_arms; 89 use super::merge_match_arms;