//
// fn handle(action: Action) {
// match action {
-// $0Action::Move { distance } => {}
-// Action::Stop => {}
+// $0Action::Move { distance } => todo!(),
+// Action::Stop => todo!(),
// }
// }
// ```
|builder| {
let new_match_arm_list = match_arm_list.clone_for_update();
let missing_arms = missing_pats
- .map(|pat| make::match_arm(iter::once(pat), make::expr_empty_block()))
+ .map(|pat| make::match_arm(iter::once(pat), make::ext::expr_todo()))
.map(|it| it.clone_for_update());
let catch_all_arm = new_match_arm_list
r#"
fn foo(a: bool) {
match a {
- $0true => {}
- false => {}
+ $0true => todo!(),
+ false => todo!(),
}
}
"#,
fn foo(a: bool) {
match a {
true => {}
- $0false => {}
+ $0false => todo!(),
}
}
"#,
r#"
fn foo(a: bool) {
match (a, a) {
- $0(true, true) => {}
- (true, false) => {}
- (false, true) => {}
- (false, false) => {}
+ $0(true, true) => todo!(),
+ (true, false) => todo!(),
+ (false, true) => todo!(),
+ (false, false) => todo!(),
}
}
"#,
fn foo(a: bool) {
match (a, a) {
(false, true) => {}
- $0(true, true) => {}
- (true, false) => {}
- (false, false) => {}
+ $0(true, true) => todo!(),
+ (true, false) => todo!(),
+ (false, false) => todo!(),
}
}
"#,
match A::As {
A::Bs { x, y: Some(_) } => {}
A::Cs(_, Some(_)) => {}
- $0A::As => {}
+ $0A::As => todo!(),
}
}
"#,
fn main() {
match None {
None => {}
- Some(${0:_}) => {}
+ Some(${0:_}) => todo!(),
}
}
"#,
fn main() {
match A::As {
A::Cs(_) | A::Bs => {}
- $0A::As => {}
+ $0A::As => todo!(),
}
}
"#,
A::Bs if 0 < 1 => {}
A::Ds(_value) => { let x = 1; }
A::Es(B::Xs) => (),
- $0A::As => {}
- A::Cs => {}
+ $0A::As => todo!(),
+ A::Cs => todo!(),
}
}
"#,
match A::As {
A::As(_) => {}
a @ A::Bs(_) => {}
- A::Cs(${0:_}) => {}
+ A::Cs(${0:_}) => todo!(),
}
}
"#,
fn main() {
let a = A::As;
match a {
- $0A::As => {}
- A::Bs => {}
- A::Cs(_) => {}
- A::Ds(_, _) => {}
- A::Es { x, y } => {}
+ $0A::As => todo!(),
+ A::Bs => todo!(),
+ A::Cs(_) => todo!(),
+ A::Ds(_, _) => todo!(),
+ A::Es { x, y } => todo!(),
}
}
"#,
let a = A::One;
let b = B::One;
match (a, b) {
- $0(A::One, B::One) => {}
- (A::One, B::Two) => {}
- (A::Two, B::One) => {}
- (A::Two, B::Two) => {}
+ $0(A::One, B::One) => todo!(),
+ (A::One, B::Two) => todo!(),
+ (A::Two, B::One) => todo!(),
+ (A::Two, B::Two) => todo!(),
}
}
"#,
let a = A::One;
let b = B::One;
match (&a, &b) {
- $0(A::One, B::One) => {}
- (A::One, B::Two) => {}
- (A::Two, B::One) => {}
- (A::Two, B::Two) => {}
+ $0(A::One, B::One) => todo!(),
+ (A::One, B::Two) => todo!(),
+ (A::Two, B::One) => todo!(),
+ (A::Two, B::Two) => todo!(),
}
}
"#,
let b = B::One;
match (a, b) {
(A::Two, B::One) => {}
- $0(A::One, B::One) => {}
- (A::One, B::Two) => {}
- (A::Two, B::Two) => {}
+ $0(A::One, B::One) => todo!(),
+ (A::One, B::Two) => todo!(),
+ (A::Two, B::Two) => todo!(),
}
}
"#,
match (a, b) {
(Some(_), _) => {}
(None, Some(_)) => {}
- $0(None, None) => {}
+ $0(None, None) => todo!(),
}
}
"#,
fn main() {
let a = A::One;
match (a, ) {
- $0(A::One,) => {}
- (A::Two,) => {}
+ $0(A::One,) => todo!(),
+ (A::Two,) => todo!(),
}
}
"#,
fn foo(a: &A) {
match a {
- $0A::As => {}
+ $0A::As => todo!(),
}
}
"#,
fn foo(a: &mut A) {
match a {
- $0A::Es { x, y } => {}
+ $0A::Es { x, y } => todo!(),
}
}
"#,
fn main() {
match E::X {
- $0E::X => {}
- E::Y => {}
+ $0E::X => todo!(),
+ E::Y => todo!(),
}
}
"#,
fn main() {
match X {
- $0X => {}
- foo::E::Y => {}
+ $0X => todo!(),
+ foo::E::Y => todo!(),
}
}
"#,
match a {
// foo bar baz
A::One => {}
- $0A::Two => {}
+ $0A::Two => todo!(),
// This is where the rest should be
}
}
enum A { One, Two }
fn foo(a: A) {
match a {
- $0A::One => {}
- A::Two => {}
+ $0A::One => todo!(),
+ A::Two => todo!(),
// foo bar baz
}
}
enum A { One, Two, }
fn foo(a: A) {
match a {
- $0A::One => {}
- A::Two => {}
+ $0A::One => todo!(),
+ A::Two => todo!(),
}
}
"#,
r#"
fn foo(opt: Option<i32>) {
match opt {
- Some(${0:_}) => {}
- None => {}
+ Some(${0:_}) => todo!(),
+ None => todo!(),
}
}
"#,
fn foo(t: Test) {
m!(match t {
- $0Test::A => {}
- Test::B => {}
- Test::C => {}
+ $0Test::A => todo!(),
+ Test::B => todo!(),
+ Test::C => todo!(),
});
}"#,
);
"#,
);
}
+
+ #[test]
+ fn adds_comma_before_new_arms() {
+ check_assist(
+ fill_match_arms,
+ r#"
+fn foo(t: bool) {
+ match $0t {
+ true => 1 + 2
+ }
+}"#,
+ r#"
+fn foo(t: bool) {
+ match t {
+ true => 1 + 2,
+ $0false => todo!(),
+ }
+}"#,
+ );
+ }
}
impl ast::MatchArmList {
pub fn add_arm(&self, arm: ast::MatchArm) {
normalize_ws_between_braces(self.syntax());
+ let mut elements = Vec::new();
let position = match self.arms().last() {
Some(last_arm) => {
- let curly = last_arm
+ let comma = last_arm
.syntax()
.siblings_with_tokens(Direction::Next)
.find(|it| it.kind() == T![,]);
- Position::after(curly.unwrap_or_else(|| last_arm.syntax().clone().into()))
+ if needs_comma(&last_arm) && comma.is_none() {
+ elements.push(make::token(SyntaxKind::COMMA).into());
+ }
+ Position::after(comma.unwrap_or_else(|| last_arm.syntax().clone().into()))
}
None => match self.l_curly_token() {
Some(it) => Position::after(it),
},
};
let indent = IndentLevel::from_node(self.syntax()) + 1;
- let elements = vec![
- make::tokens::whitespace(&format!("\n{}", indent)).into(),
- arm.syntax().clone().into(),
- ];
+ elements.push(make::tokens::whitespace(&format!("\n{}", indent)).into());
+ elements.push(arm.syntax().clone().into());
+ if needs_comma(&arm) {
+ elements.push(make::token(SyntaxKind::COMMA).into());
+ }
ted::insert_all(position, elements);
+
+ fn needs_comma(arm: &ast::MatchArm) -> bool {
+ arm.expr().map_or(false, |e| !e.is_block_like())
+ }
}
}