From 4de447b293d5c6d7ad00196dc8409d29575a8c86 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 29 Jul 2021 22:32:24 +0200 Subject: [PATCH 1/4] Attach comma token to MATCH_ARM instead of MATCH_ARM_LIST --- crates/ide/src/join_lines.rs | 14 ++----- crates/ide/src/move_item.rs | 40 +++++++++---------- crates/parser/src/grammar/expressions/atom.rs | 32 +++++++-------- .../err/0032_match_arms_inner_attrs.rast | 24 +++++------ .../err/0033_match_arms_outer_attrs.rast | 8 ++-- .../inline/ok/0055_literal_pattern.rast | 16 ++++---- .../parser/inline/ok/0058_range_pat.rast | 12 +++--- .../inline/ok/0059_match_arms_commas.rast | 4 +- .../parser/inline/ok/0066_match_arm.rast | 20 +++++----- .../parser/inline/ok/0118_match_guard.rast | 4 +- .../ok/0120_match_arms_inner_attribute.rast | 4 +- .../ok/0121_match_arms_outer_attributes.rast | 12 +++--- .../parser/inline/ok/0156_or_pattern.rast | 16 ++++---- .../test_data/parser/ok/0035_weird_exprs.rast | 4 +- .../ok/0047_minus_in_inner_pattern.rast | 24 +++++------ 15 files changed, 113 insertions(+), 121 deletions(-) diff --git a/crates/ide/src/join_lines.rs b/crates/ide/src/join_lines.rs index fd8ea8bf2e..30334ffcc8 100644 --- a/crates/ide/src/join_lines.rs +++ b/crates/ide/src/join_lines.rs @@ -3,11 +3,10 @@ use std::convert::TryFrom; use ide_assists::utils::extract_trivial_expression; use itertools::Itertools; use syntax::{ - algo::non_trivia_sibling, ast::{self, AstNode, AstToken, IsString}, - Direction, NodeOrToken, SourceFile, SyntaxElement, + NodeOrToken, SourceFile, SyntaxElement, SyntaxKind::{self, USE_TREE, WHITESPACE}, - SyntaxNode, SyntaxToken, TextRange, TextSize, T, + SyntaxToken, TextRange, TextSize, T, }; use text_edit::{TextEdit, TextEditBuilder}; @@ -204,13 +203,6 @@ fn remove_newline( edit.replace(token.text_range(), compute_ws(prev.kind(), next.kind()).to_string()); } -fn has_comma_after(node: &SyntaxNode) -> bool { - match non_trivia_sibling(node.clone().into(), Direction::Next) { - Some(n) => n.kind() == T![,], - _ => false, - } -} - fn join_single_expr_block(edit: &mut TextEditBuilder, token: &SyntaxToken) -> Option<()> { let block_expr = ast::BlockExpr::cast(token.parent()?)?; if !block_expr.is_standalone() { @@ -223,7 +215,7 @@ fn join_single_expr_block(edit: &mut TextEditBuilder, token: &SyntaxToken) -> Op // Match block needs to have a comma after the block if let Some(match_arm) = block_expr.syntax().parent().and_then(ast::MatchArm::cast) { - if !has_comma_after(match_arm.syntax()) { + if match_arm.comma_token().is_none() { buf.push(','); } } diff --git a/crates/ide/src/move_item.rs b/crates/ide/src/move_item.rs index 1223c2810f..6245f84801 100644 --- a/crates/ide/src/move_item.rs +++ b/crates/ide/src/move_item.rs @@ -205,16 +205,16 @@ fn main() { } "#, expect![[r#" -fn main() { - match true { - false =>$0 { - println!("Test"); - }, - true => { - println!("Hello, world"); - } - }; -} + fn main() { + match true { + false =>$0 { + println!("Test"); + } + true => { + println!("Hello, world"); + }, + }; + } "#]], Direction::Up, ); @@ -236,16 +236,16 @@ fn main() { } "#, expect![[r#" -fn main() { - match true { - false => { - println!("Test"); - }, - true =>$0 { - println!("Hello, world"); - } - }; -} + fn main() { + match true { + false => { + println!("Test"); + } + true =>$0 { + println!("Hello, world"); + }, + }; + } "#]], Direction::Down, ); diff --git a/crates/parser/src/grammar/expressions/atom.rs b/crates/parser/src/grammar/expressions/atom.rs index abdfca1feb..8e7dbea3e7 100644 --- a/crates/parser/src/grammar/expressions/atom.rs +++ b/crates/parser/src/grammar/expressions/atom.rs @@ -400,20 +400,7 @@ pub(crate) fn match_arm_list(p: &mut Parser) { error_block(p, "expected match arm"); continue; } - - // test match_arms_commas - // fn foo() { - // match () { - // _ => (), - // _ => {} - // _ => () - // } - // } - if match_arm(p).is_block() { - p.eat(T![,]); - } else if !p.at(T!['}']) { - p.expect(T![,]); - } + match_arm(p); } p.expect(T!['}']); m.complete(p, MATCH_ARM_LIST); @@ -429,7 +416,7 @@ pub(crate) fn match_arm_list(p: &mut Parser) { // | X => (), // }; // } -fn match_arm(p: &mut Parser) -> BlockLike { +fn match_arm(p: &mut Parser) { let m = p.start(); // test match_arms_outer_attributes // fn foo() { @@ -452,8 +439,21 @@ fn match_arm(p: &mut Parser) -> BlockLike { } p.expect(T![=>]); let blocklike = expr_stmt(p).1; + + // test match_arms_commas + // fn foo() { + // match () { + // _ => (), + // _ => {} + // _ => () + // } + // } + if blocklike.is_block() { + p.eat(T![,]); + } else if !p.at(T!['}']) { + p.expect(T![,]); + } m.complete(p, MATCH_ARM); - blocklike } // test match_guard diff --git a/crates/syntax/test_data/parser/err/0032_match_arms_inner_attrs.rast b/crates/syntax/test_data/parser/err/0032_match_arms_inner_attrs.rast index b6209639d5..83b9e79c3a 100644 --- a/crates/syntax/test_data/parser/err/0032_match_arms_inner_attrs.rast +++ b/crates/syntax/test_data/parser/err/0032_match_arms_inner_attrs.rast @@ -22,7 +22,7 @@ SOURCE_FILE@0..293 MATCH_ARM_LIST@24..101 L_CURLY@24..25 "{" WHITESPACE@25..34 "\n " - MATCH_ARM@34..41 + MATCH_ARM@34..42 WILDCARD_PAT@34..35 UNDERSCORE@34..35 "_" WHITESPACE@35..36 " " @@ -31,7 +31,7 @@ SOURCE_FILE@0..293 TUPLE_EXPR@39..41 L_PAREN@39..40 "(" R_PAREN@40..41 ")" - COMMA@41..42 "," + COMMA@41..42 "," WHITESPACE@42..51 "\n " MATCH_ARM@51..78 ATTR@51..52 @@ -53,7 +53,7 @@ SOURCE_FILE@0..293 R_PAREN@76..77 ")" R_BRACK@77..78 "]" WHITESPACE@78..87 "\n " - MATCH_ARM@87..94 + MATCH_ARM@87..95 WILDCARD_PAT@87..88 UNDERSCORE@87..88 "_" WHITESPACE@88..89 " " @@ -62,7 +62,7 @@ SOURCE_FILE@0..293 TUPLE_EXPR@92..94 L_PAREN@92..93 "(" R_PAREN@93..94 ")" - COMMA@94..95 "," + COMMA@94..95 "," WHITESPACE@95..100 "\n " R_CURLY@100..101 "}" WHITESPACE@101..107 "\n\n " @@ -77,7 +77,7 @@ SOURCE_FILE@0..293 MATCH_ARM_LIST@116..185 L_CURLY@116..117 "{" WHITESPACE@117..126 "\n " - MATCH_ARM@126..133 + MATCH_ARM@126..134 WILDCARD_PAT@126..127 UNDERSCORE@126..127 "_" WHITESPACE@127..128 " " @@ -86,9 +86,9 @@ SOURCE_FILE@0..293 TUPLE_EXPR@131..133 L_PAREN@131..132 "(" R_PAREN@132..133 ")" - COMMA@133..134 "," + COMMA@133..134 "," WHITESPACE@134..143 "\n " - MATCH_ARM@143..150 + MATCH_ARM@143..151 WILDCARD_PAT@143..144 UNDERSCORE@143..144 "_" WHITESPACE@144..145 " " @@ -97,7 +97,7 @@ SOURCE_FILE@0..293 TUPLE_EXPR@148..150 L_PAREN@148..149 "(" R_PAREN@149..150 ")" - COMMA@150..151 "," + COMMA@150..151 "," WHITESPACE@151..160 "\n " MATCH_ARM@160..179 ATTR@160..161 @@ -165,7 +165,7 @@ SOURCE_FILE@0..293 R_PAREN@248..249 ")" R_BRACK@249..250 "]" WHITESPACE@250..259 "\n " - MATCH_ARM@259..266 + MATCH_ARM@259..267 WILDCARD_PAT@259..260 UNDERSCORE@259..260 "_" WHITESPACE@260..261 " " @@ -174,9 +174,9 @@ SOURCE_FILE@0..293 TUPLE_EXPR@264..266 L_PAREN@264..265 "(" R_PAREN@265..266 ")" - COMMA@266..267 "," + COMMA@266..267 "," WHITESPACE@267..276 "\n " - MATCH_ARM@276..283 + MATCH_ARM@276..284 WILDCARD_PAT@276..277 UNDERSCORE@276..277 "_" WHITESPACE@277..278 " " @@ -185,7 +185,7 @@ SOURCE_FILE@0..293 TUPLE_EXPR@281..283 L_PAREN@281..282 "(" R_PAREN@282..283 ")" - COMMA@283..284 "," + COMMA@283..284 "," WHITESPACE@284..289 "\n " R_CURLY@289..290 "}" WHITESPACE@290..291 "\n" diff --git a/crates/syntax/test_data/parser/err/0033_match_arms_outer_attrs.rast b/crates/syntax/test_data/parser/err/0033_match_arms_outer_attrs.rast index 84c8e9ee79..b6095d5030 100644 --- a/crates/syntax/test_data/parser/err/0033_match_arms_outer_attrs.rast +++ b/crates/syntax/test_data/parser/err/0033_match_arms_outer_attrs.rast @@ -21,7 +21,7 @@ SOURCE_FILE@0..89 MATCH_ARM_LIST@24..86 L_CURLY@24..25 "{" WHITESPACE@25..34 "\n " - MATCH_ARM@34..41 + MATCH_ARM@34..42 WILDCARD_PAT@34..35 UNDERSCORE@34..35 "_" WHITESPACE@35..36 " " @@ -30,9 +30,9 @@ SOURCE_FILE@0..89 TUPLE_EXPR@39..41 L_PAREN@39..40 "(" R_PAREN@40..41 ")" - COMMA@41..42 "," + COMMA@41..42 "," WHITESPACE@42..51 "\n " - MATCH_ARM@51..58 + MATCH_ARM@51..59 WILDCARD_PAT@51..52 UNDERSCORE@51..52 "_" WHITESPACE@52..53 " " @@ -41,7 +41,7 @@ SOURCE_FILE@0..89 TUPLE_EXPR@56..58 L_PAREN@56..57 "(" R_PAREN@57..58 ")" - COMMA@58..59 "," + COMMA@58..59 "," WHITESPACE@59..68 "\n " MATCH_ARM@68..80 ATTR@68..80 diff --git a/crates/syntax/test_data/parser/inline/ok/0055_literal_pattern.rast b/crates/syntax/test_data/parser/inline/ok/0055_literal_pattern.rast index acf18fc2b2..32187facfd 100644 --- a/crates/syntax/test_data/parser/inline/ok/0055_literal_pattern.rast +++ b/crates/syntax/test_data/parser/inline/ok/0055_literal_pattern.rast @@ -21,7 +21,7 @@ SOURCE_FILE@0..113 MATCH_ARM_LIST@25..110 L_CURLY@25..26 "{" WHITESPACE@26..35 "\n " - MATCH_ARM@35..43 + MATCH_ARM@35..44 LITERAL_PAT@35..37 MINUS@35..36 "-" LITERAL@36..37 @@ -32,9 +32,9 @@ SOURCE_FILE@0..113 TUPLE_EXPR@41..43 L_PAREN@41..42 "(" R_PAREN@42..43 ")" - COMMA@43..44 "," + COMMA@43..44 "," WHITESPACE@44..53 "\n " - MATCH_ARM@53..61 + MATCH_ARM@53..62 LITERAL_PAT@53..55 LITERAL@53..55 INT_NUMBER@53..55 "92" @@ -44,9 +44,9 @@ SOURCE_FILE@0..113 TUPLE_EXPR@59..61 L_PAREN@59..60 "(" R_PAREN@60..61 ")" - COMMA@61..62 "," + COMMA@61..62 "," WHITESPACE@62..71 "\n " - MATCH_ARM@71..80 + MATCH_ARM@71..81 LITERAL_PAT@71..74 LITERAL@71..74 CHAR@71..74 "'c'" @@ -56,9 +56,9 @@ SOURCE_FILE@0..113 TUPLE_EXPR@78..80 L_PAREN@78..79 "(" R_PAREN@79..80 ")" - COMMA@80..81 "," + COMMA@80..81 "," WHITESPACE@81..90 "\n " - MATCH_ARM@90..103 + MATCH_ARM@90..104 LITERAL_PAT@90..97 LITERAL@90..97 STRING@90..97 "\"hello\"" @@ -68,7 +68,7 @@ SOURCE_FILE@0..113 TUPLE_EXPR@101..103 L_PAREN@101..102 "(" R_PAREN@102..103 ")" - COMMA@103..104 "," + COMMA@103..104 "," WHITESPACE@104..109 "\n " R_CURLY@109..110 "}" WHITESPACE@110..111 "\n" diff --git a/crates/syntax/test_data/parser/inline/ok/0058_range_pat.rast b/crates/syntax/test_data/parser/inline/ok/0058_range_pat.rast index 3e72f9671c..3598f3bc26 100644 --- a/crates/syntax/test_data/parser/inline/ok/0058_range_pat.rast +++ b/crates/syntax/test_data/parser/inline/ok/0058_range_pat.rast @@ -20,7 +20,7 @@ SOURCE_FILE@0..112 MATCH_ARM_LIST@25..109 L_CURLY@25..26 "{" WHITESPACE@26..35 "\n " - MATCH_ARM@35..50 + MATCH_ARM@35..51 RANGE_PAT@35..44 LITERAL_PAT@35..36 LITERAL@35..36 @@ -37,9 +37,9 @@ SOURCE_FILE@0..112 TUPLE_EXPR@48..50 L_PAREN@48..49 "(" R_PAREN@49..50 ")" - COMMA@50..51 "," + COMMA@50..51 "," WHITESPACE@51..60 "\n " - MATCH_ARM@60..77 + MATCH_ARM@60..78 RANGE_PAT@60..71 LITERAL_PAT@60..63 LITERAL@60..63 @@ -56,9 +56,9 @@ SOURCE_FILE@0..112 TUPLE_EXPR@75..77 L_PAREN@75..76 "(" R_PAREN@76..77 ")" - COMMA@77..78 "," + COMMA@77..78 "," WHITESPACE@78..87 "\n " - MATCH_ARM@87..102 + MATCH_ARM@87..103 RANGE_PAT@87..97 LITERAL_PAT@87..90 LITERAL@87..90 @@ -74,7 +74,7 @@ SOURCE_FILE@0..112 TUPLE_EXPR@100..102 L_PAREN@100..101 "(" R_PAREN@101..102 ")" - COMMA@102..103 "," + COMMA@102..103 "," WHITESPACE@103..108 "\n " R_CURLY@108..109 "}" WHITESPACE@109..110 "\n" diff --git a/crates/syntax/test_data/parser/inline/ok/0059_match_arms_commas.rast b/crates/syntax/test_data/parser/inline/ok/0059_match_arms_commas.rast index 984829317e..6155726e91 100644 --- a/crates/syntax/test_data/parser/inline/ok/0059_match_arms_commas.rast +++ b/crates/syntax/test_data/parser/inline/ok/0059_match_arms_commas.rast @@ -21,7 +21,7 @@ SOURCE_FILE@0..83 MATCH_ARM_LIST@24..80 L_CURLY@24..25 "{" WHITESPACE@25..34 "\n " - MATCH_ARM@34..41 + MATCH_ARM@34..42 WILDCARD_PAT@34..35 UNDERSCORE@34..35 "_" WHITESPACE@35..36 " " @@ -30,7 +30,7 @@ SOURCE_FILE@0..83 TUPLE_EXPR@39..41 L_PAREN@39..40 "(" R_PAREN@40..41 ")" - COMMA@41..42 "," + COMMA@41..42 "," WHITESPACE@42..51 "\n " MATCH_ARM@51..58 WILDCARD_PAT@51..52 diff --git a/crates/syntax/test_data/parser/inline/ok/0066_match_arm.rast b/crates/syntax/test_data/parser/inline/ok/0066_match_arm.rast index 57d0661a54..566b478ea8 100644 --- a/crates/syntax/test_data/parser/inline/ok/0066_match_arm.rast +++ b/crates/syntax/test_data/parser/inline/ok/0066_match_arm.rast @@ -22,7 +22,7 @@ SOURCE_FILE@0..167 MATCH_ARM_LIST@24..163 L_CURLY@24..25 "{" WHITESPACE@25..34 "\n " - MATCH_ARM@34..41 + MATCH_ARM@34..42 WILDCARD_PAT@34..35 UNDERSCORE@34..35 "_" WHITESPACE@35..36 " " @@ -31,9 +31,9 @@ SOURCE_FILE@0..167 TUPLE_EXPR@39..41 L_PAREN@39..40 "(" R_PAREN@40..41 ")" - COMMA@41..42 "," + COMMA@41..42 "," WHITESPACE@42..51 "\n " - MATCH_ARM@51..83 + MATCH_ARM@51..84 WILDCARD_PAT@51..52 UNDERSCORE@51..52 "_" WHITESPACE@52..53 " " @@ -70,9 +70,9 @@ SOURCE_FILE@0..167 TUPLE_EXPR@81..83 L_PAREN@81..82 "(" R_PAREN@82..83 ")" - COMMA@83..84 "," + COMMA@83..84 "," WHITESPACE@84..93 "\n " - MATCH_ARM@93..109 + MATCH_ARM@93..110 OR_PAT@93..98 IDENT_PAT@93..94 NAME@93..94 @@ -98,9 +98,9 @@ SOURCE_FILE@0..167 TUPLE_EXPR@107..109 L_PAREN@107..108 "(" R_PAREN@108..109 ")" - COMMA@109..110 "," + COMMA@109..110 "," WHITESPACE@110..119 "\n " - MATCH_ARM@119..137 + MATCH_ARM@119..138 PIPE@119..120 "|" WHITESPACE@120..121 " " OR_PAT@121..126 @@ -128,9 +128,9 @@ SOURCE_FILE@0..167 TUPLE_EXPR@135..137 L_PAREN@135..136 "(" R_PAREN@136..137 ")" - COMMA@137..138 "," + COMMA@137..138 "," WHITESPACE@138..147 "\n " - MATCH_ARM@147..156 + MATCH_ARM@147..157 PIPE@147..148 "|" WHITESPACE@148..149 " " IDENT_PAT@149..150 @@ -142,7 +142,7 @@ SOURCE_FILE@0..167 TUPLE_EXPR@154..156 L_PAREN@154..155 "(" R_PAREN@155..156 ")" - COMMA@156..157 "," + COMMA@156..157 "," WHITESPACE@157..162 "\n " R_CURLY@162..163 "}" SEMICOLON@163..164 ";" diff --git a/crates/syntax/test_data/parser/inline/ok/0118_match_guard.rast b/crates/syntax/test_data/parser/inline/ok/0118_match_guard.rast index 0cf4eb0a5a..12d498af62 100644 --- a/crates/syntax/test_data/parser/inline/ok/0118_match_guard.rast +++ b/crates/syntax/test_data/parser/inline/ok/0118_match_guard.rast @@ -21,7 +21,7 @@ SOURCE_FILE@0..58 MATCH_ARM_LIST@24..55 L_CURLY@24..25 "{" WHITESPACE@25..34 "\n " - MATCH_ARM@34..48 + MATCH_ARM@34..49 WILDCARD_PAT@34..35 UNDERSCORE@34..35 "_" WHITESPACE@35..36 " " @@ -39,7 +39,7 @@ SOURCE_FILE@0..58 TUPLE_EXPR@46..48 L_PAREN@46..47 "(" R_PAREN@47..48 ")" - COMMA@48..49 "," + COMMA@48..49 "," WHITESPACE@49..54 "\n " R_CURLY@54..55 "}" WHITESPACE@55..56 "\n" diff --git a/crates/syntax/test_data/parser/inline/ok/0120_match_arms_inner_attribute.rast b/crates/syntax/test_data/parser/inline/ok/0120_match_arms_inner_attribute.rast index 7fbeee2035..443f368532 100644 --- a/crates/syntax/test_data/parser/inline/ok/0120_match_arms_inner_attribute.rast +++ b/crates/syntax/test_data/parser/inline/ok/0120_match_arms_inner_attribute.rast @@ -66,7 +66,7 @@ SOURCE_FILE@0..139 R_PAREN@111..112 ")" R_BRACK@112..113 "]" WHITESPACE@113..122 "\n " - MATCH_ARM@122..129 + MATCH_ARM@122..130 WILDCARD_PAT@122..123 UNDERSCORE@122..123 "_" WHITESPACE@123..124 " " @@ -75,7 +75,7 @@ SOURCE_FILE@0..139 TUPLE_EXPR@127..129 L_PAREN@127..128 "(" R_PAREN@128..129 ")" - COMMA@129..130 "," + COMMA@129..130 "," WHITESPACE@130..135 "\n " R_CURLY@135..136 "}" WHITESPACE@136..137 "\n" diff --git a/crates/syntax/test_data/parser/inline/ok/0121_match_arms_outer_attributes.rast b/crates/syntax/test_data/parser/inline/ok/0121_match_arms_outer_attributes.rast index 40852f5140..1b2db27e4f 100644 --- a/crates/syntax/test_data/parser/inline/ok/0121_match_arms_outer_attributes.rast +++ b/crates/syntax/test_data/parser/inline/ok/0121_match_arms_outer_attributes.rast @@ -21,7 +21,7 @@ SOURCE_FILE@0..259 MATCH_ARM_LIST@24..256 L_CURLY@24..25 "{" WHITESPACE@25..34 "\n " - MATCH_ARM@34..74 + MATCH_ARM@34..75 ATTR@34..58 POUND@34..35 "#" L_BRACK@35..36 "[" @@ -48,9 +48,9 @@ SOURCE_FILE@0..259 TUPLE_EXPR@72..74 L_PAREN@72..73 "(" R_PAREN@73..74 ")" - COMMA@74..75 "," + COMMA@74..75 "," WHITESPACE@75..84 "\n " - MATCH_ARM@84..125 + MATCH_ARM@84..126 ATTR@84..109 POUND@84..85 "#" L_BRACK@85..86 "[" @@ -77,9 +77,9 @@ SOURCE_FILE@0..259 TUPLE_EXPR@123..125 L_PAREN@123..124 "(" R_PAREN@124..125 ")" - COMMA@125..126 "," + COMMA@125..126 "," WHITESPACE@126..135 "\n " - MATCH_ARM@135..249 + MATCH_ARM@135..250 ATTR@135..159 POUND@135..136 "#" L_BRACK@136..137 "[" @@ -142,7 +142,7 @@ SOURCE_FILE@0..259 TUPLE_EXPR@247..249 L_PAREN@247..248 "(" R_PAREN@248..249 ")" - COMMA@249..250 "," + COMMA@249..250 "," WHITESPACE@250..255 "\n " R_CURLY@255..256 "}" WHITESPACE@256..257 "\n" diff --git a/crates/syntax/test_data/parser/inline/ok/0156_or_pattern.rast b/crates/syntax/test_data/parser/inline/ok/0156_or_pattern.rast index 88a513cee2..41119dbc16 100644 --- a/crates/syntax/test_data/parser/inline/ok/0156_or_pattern.rast +++ b/crates/syntax/test_data/parser/inline/ok/0156_or_pattern.rast @@ -21,7 +21,7 @@ SOURCE_FILE@0..130 MATCH_ARM_LIST@25..127 L_CURLY@25..26 "{" WHITESPACE@26..35 "\n " - MATCH_ARM@35..48 + MATCH_ARM@35..49 PAREN_PAT@35..42 L_PAREN@35..36 "(" OR_PAT@36..41 @@ -39,9 +39,9 @@ SOURCE_FILE@0..130 TUPLE_EXPR@46..48 L_PAREN@46..47 "(" R_PAREN@47..48 ")" - COMMA@48..49 "," + COMMA@48..49 "," WHITESPACE@49..58 "\n " - MATCH_ARM@58..72 + MATCH_ARM@58..73 REF_PAT@58..66 AMP@58..59 "&" PAREN_PAT@59..66 @@ -61,9 +61,9 @@ SOURCE_FILE@0..130 TUPLE_EXPR@70..72 L_PAREN@70..71 "(" R_PAREN@71..72 ")" - COMMA@72..73 "," + COMMA@72..73 "," WHITESPACE@73..82 "\n " - MATCH_ARM@82..96 + MATCH_ARM@82..97 TUPLE_PAT@82..90 L_PAREN@82..83 "(" OR_PAT@83..88 @@ -82,9 +82,9 @@ SOURCE_FILE@0..130 TUPLE_EXPR@94..96 L_PAREN@94..95 "(" R_PAREN@95..96 ")" - COMMA@96..97 "," + COMMA@96..97 "," WHITESPACE@97..106 "\n " - MATCH_ARM@106..120 + MATCH_ARM@106..121 SLICE_PAT@106..114 L_BRACK@106..107 "[" OR_PAT@107..112 @@ -103,7 +103,7 @@ SOURCE_FILE@0..130 TUPLE_EXPR@118..120 L_PAREN@118..119 "(" R_PAREN@119..120 ")" - COMMA@120..121 "," + COMMA@120..121 "," WHITESPACE@121..126 "\n " R_CURLY@126..127 "}" WHITESPACE@127..128 "\n" diff --git a/crates/syntax/test_data/parser/ok/0035_weird_exprs.rast b/crates/syntax/test_data/parser/ok/0035_weird_exprs.rast index 2fa46ad2dd..5b114df801 100644 --- a/crates/syntax/test_data/parser/ok/0035_weird_exprs.rast +++ b/crates/syntax/test_data/parser/ok/0035_weird_exprs.rast @@ -1093,7 +1093,7 @@ SOURCE_FILE@0..3813 MATCH_ARM_LIST@1853..1885 L_CURLY@1853..1854 "{" WHITESPACE@1854..1855 " " - MATCH_ARM@1855..1863 + MATCH_ARM@1855..1864 LITERAL_PAT@1855..1856 LITERAL@1855..1856 INT_NUMBER@1855..1856 "1" @@ -1104,7 +1104,7 @@ SOURCE_FILE@0..3813 L_CURLY@1860..1861 "{" WHITESPACE@1861..1862 " " R_CURLY@1862..1863 "}" - COMMA@1863..1864 "," + COMMA@1863..1864 "," WHITESPACE@1864..1865 " " MATCH_ARM@1865..1883 WILDCARD_PAT@1865..1866 diff --git a/crates/syntax/test_data/parser/ok/0047_minus_in_inner_pattern.rast b/crates/syntax/test_data/parser/ok/0047_minus_in_inner_pattern.rast index 4f8dff9094..731a2315aa 100644 --- a/crates/syntax/test_data/parser/ok/0047_minus_in_inner_pattern.rast +++ b/crates/syntax/test_data/parser/ok/0047_minus_in_inner_pattern.rast @@ -34,7 +34,7 @@ SOURCE_FILE@0..395 MATCH_ARM_LIST@93..141 L_CURLY@93..94 "{" WHITESPACE@94..103 "\n " - MATCH_ARM@103..117 + MATCH_ARM@103..118 TUPLE_STRUCT_PAT@103..111 PATH@103..107 PATH_SEGMENT@103..107 @@ -52,9 +52,9 @@ SOURCE_FILE@0..395 TUPLE_EXPR@115..117 L_PAREN@115..116 "(" R_PAREN@116..117 ")" - COMMA@117..118 "," + COMMA@117..118 "," WHITESPACE@118..127 "\n " - MATCH_ARM@127..134 + MATCH_ARM@127..135 WILDCARD_PAT@127..128 UNDERSCORE@127..128 "_" WHITESPACE@128..129 " " @@ -63,7 +63,7 @@ SOURCE_FILE@0..395 TUPLE_EXPR@132..134 L_PAREN@132..133 "(" R_PAREN@133..134 ")" - COMMA@134..135 "," + COMMA@134..135 "," WHITESPACE@135..140 "\n " R_CURLY@140..141 "}" WHITESPACE@141..147 "\n\n " @@ -97,7 +97,7 @@ SOURCE_FILE@0..395 MATCH_ARM_LIST@168..222 L_CURLY@168..169 "{" WHITESPACE@169..178 "\n " - MATCH_ARM@178..198 + MATCH_ARM@178..199 TUPLE_STRUCT_PAT@178..192 PATH@178..182 PATH_SEGMENT@178..182 @@ -124,9 +124,9 @@ SOURCE_FILE@0..395 TUPLE_EXPR@196..198 L_PAREN@196..197 "(" R_PAREN@197..198 ")" - COMMA@198..199 "," + COMMA@198..199 "," WHITESPACE@199..208 "\n " - MATCH_ARM@208..215 + MATCH_ARM@208..216 WILDCARD_PAT@208..209 UNDERSCORE@208..209 "_" WHITESPACE@209..210 " " @@ -135,7 +135,7 @@ SOURCE_FILE@0..395 TUPLE_EXPR@213..215 L_PAREN@213..214 "(" R_PAREN@214..215 ")" - COMMA@215..216 "," + COMMA@215..216 "," WHITESPACE@216..221 "\n " R_CURLY@221..222 "}" WHITESPACE@222..228 "\n\n " @@ -171,7 +171,7 @@ SOURCE_FILE@0..395 MATCH_ARM_LIST@247..299 L_CURLY@247..248 "{" WHITESPACE@248..257 "\n " - MATCH_ARM@257..275 + MATCH_ARM@257..276 TUPLE_STRUCT_PAT@257..269 PATH@257..261 PATH@257..258 @@ -200,9 +200,9 @@ SOURCE_FILE@0..395 TUPLE_EXPR@273..275 L_PAREN@273..274 "(" R_PAREN@274..275 ")" - COMMA@275..276 "," + COMMA@275..276 "," WHITESPACE@276..285 "\n " - MATCH_ARM@285..292 + MATCH_ARM@285..293 WILDCARD_PAT@285..286 UNDERSCORE@285..286 "_" WHITESPACE@286..287 " " @@ -211,7 +211,7 @@ SOURCE_FILE@0..395 TUPLE_EXPR@290..292 L_PAREN@290..291 "(" R_PAREN@291..292 ")" - COMMA@292..293 "," + COMMA@292..293 "," WHITESPACE@293..298 "\n " R_CURLY@298..299 "}" WHITESPACE@299..305 "\n\n " From 836784f02be6c05df0a15c22a3c63cac033b5d29 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 30 Jul 2021 13:03:28 +0200 Subject: [PATCH 2/4] Fix move_item test fixture indentations --- crates/ide/src/move_item.rs | 348 ++++++++++++++++++------------------ 1 file changed, 174 insertions(+), 174 deletions(-) diff --git a/crates/ide/src/move_item.rs b/crates/ide/src/move_item.rs index 6245f84801..f053580a56 100644 --- a/crates/ide/src/move_item.rs +++ b/crates/ide/src/move_item.rs @@ -203,7 +203,7 @@ fn main() { } }; } - "#, +"#, expect![[r#" fn main() { match true { @@ -234,7 +234,7 @@ fn main() { } }; } - "#, +"#, expect![[r#" fn main() { match true { @@ -265,18 +265,18 @@ fn main() { } }; } - "#, +"#, expect![[r#" -fn main() { - match true { - true => { - println!("Hello, world"); - }, - false => { - println!("Test"); - } - }; -} + fn main() { + match true { + true => { + println!("Hello, world"); + }, + false => { + println!("Test"); + } + }; + } "#]], Direction::Up, ); @@ -290,12 +290,12 @@ fn main() { let test = 123; let test2$0$0 = 456; } - "#, +"#, expect![[r#" -fn main() { - let test2$0 = 456; - let test = 123; -} + fn main() { + let test2$0 = 456; + let test = 123; + } "#]], Direction::Up, ); @@ -309,12 +309,12 @@ fn main() { println!("Hello, world"); println!("All I want to say is...");$0$0 } - "#, +"#, expect![[r#" -fn main() { - println!("All I want to say is...");$0 - println!("Hello, world"); -} + fn main() { + println!("All I want to say is...");$0 + println!("Hello, world"); + } "#]], Direction::Up, ); @@ -327,15 +327,15 @@ fn main() { println!("Test"); }$0$0 } - "#, +"#, expect![[r#" -fn main() { - if true { - println!("Test"); - }$0 + fn main() { + if true { + println!("Test"); + }$0 - println!("Hello, world"); -} + println!("Hello, world"); + } "#]], Direction::Up, ); @@ -348,15 +348,15 @@ fn main() { println!("Test"); }$0$0 } - "#, +"#, expect![[r#" -fn main() { - for i in 0..10 { - println!("Test"); - }$0 + fn main() { + for i in 0..10 { + println!("Test"); + }$0 - println!("Hello, world"); -} + println!("Hello, world"); + } "#]], Direction::Up, ); @@ -369,15 +369,15 @@ fn main() { println!("Test"); }$0$0 } - "#, +"#, expect![[r#" -fn main() { - loop { - println!("Test"); - }$0 + fn main() { + loop { + println!("Test"); + }$0 - println!("Hello, world"); -} + println!("Hello, world"); + } "#]], Direction::Up, ); @@ -390,15 +390,15 @@ fn main() { println!("Test"); }$0$0 } - "#, +"#, expect![[r#" -fn main() { - while true { - println!("Test"); - }$0 + fn main() { + while true { + println!("Test"); + }$0 - println!("Hello, world"); -} + println!("Hello, world"); + } "#]], Direction::Up, ); @@ -409,13 +409,13 @@ fn main() { return 123;$0$0 } - "#, +"#, expect![[r#" -fn main() { - return 123;$0 + fn main() { + return 123;$0 - println!("Hello, world"); -} + println!("Hello, world"); + } "#]], Direction::Up, ); @@ -429,12 +429,12 @@ fn main() { println!("All I want to say is...");$0$0 println!("Hello, world"); } - "#, +"#, expect![[r#" -fn main() { - println!("All I want to say is..."); - println!("Hello, world"); -} + fn main() { + println!("All I want to say is..."); + println!("Hello, world"); + } "#]], Direction::Up, ); @@ -447,11 +447,11 @@ fn main() { fn main() {} fn foo() {}$0$0 - "#, +"#, expect![[r#" -fn foo() {}$0 + fn foo() {}$0 -fn main() {} + fn main() {} "#]], Direction::Up, ); @@ -466,13 +466,13 @@ struct Yay; trait Wow {} impl Wow for Yay $0$0{} - "#, +"#, expect![[r#" -struct Yay; + struct Yay; -impl Wow for Yay $0{} + impl Wow for Yay $0{} -trait Wow {} + trait Wow {} "#]], Direction::Up, ); @@ -484,10 +484,10 @@ trait Wow {} r#" use std::vec::Vec; use std::collections::HashMap$0$0; - "#, +"#, expect![[r#" -use std::collections::HashMap$0; -use std::vec::Vec; + use std::collections::HashMap$0; + use std::vec::Vec; "#]], Direction::Up, ); @@ -505,16 +505,16 @@ fn main() { _ => {} };$0 } - "#, +"#, expect![[r#" -fn main() { - match test { - 456 => {}, - _ => {} - }; + fn main() { + match test { + 456 => {}, + _ => {} + }; - let test = 123; -} + let test = 123; + } "#]], Direction::Up, ); @@ -529,22 +529,22 @@ fn test(one: i32, two$0$0: u32) {} fn main() { test(123, 456); } - "#, +"#, expect![[r#" -fn test(two$0: u32, one: i32) {} + fn test(two$0: u32, one: i32) {} -fn main() { - test(123, 456); -} + fn main() { + test(123, 456); + } "#]], Direction::Up, ); check( r#" fn f($0$0arg: u8, arg2: u16) {} - "#, +"#, expect![[r#" -fn f(arg2: u16, $0arg: u8) {} + fn f(arg2: u16, $0arg: u8) {} "#]], Direction::Down, ); @@ -559,13 +559,13 @@ fn test(one: i32, two: u32) {} fn main() { test(123, 456$0$0); } - "#, +"#, expect![[r#" -fn test(one: i32, two: u32) {} + fn test(one: i32, two: u32) {} -fn main() { - test(456$0, 123); -} + fn main() { + test(456$0, 123); + } "#]], Direction::Up, ); @@ -580,13 +580,13 @@ fn test(one: i32, two: u32) {} fn main() { test(123$0$0, 456); } - "#, +"#, expect![[r#" -fn test(one: i32, two: u32) {} + fn test(one: i32, two: u32) {} -fn main() { - test(456, 123$0); -} + fn main() { + test(456, 123$0); + } "#]], Direction::Down, ); @@ -601,13 +601,13 @@ fn test(one: i32, two: u32) {} fn main() { test(123$0$0, 456); } - "#, +"#, expect![[r#" -fn test(one: i32, two: u32) {} + fn test(one: i32, two: u32) {} -fn main() { - test(123, 456); -} + fn main() { + test(123, 456); + } "#]], Direction::Up, ); @@ -620,11 +620,11 @@ fn main() { struct Test(A, B); fn main() {} - "#, +"#, expect![[r#" -struct Test(A, B); + struct Test(A, B); -fn main() {} + fn main() {} "#]], Direction::Up, ); @@ -639,13 +639,13 @@ struct Test(A, B); fn main() { let t = Test::(123, "yay"); } - "#, +"#, expect![[r#" -struct Test(A, B); + struct Test(A, B); -fn main() { - let t = Test::<&str$0, i32>(123, "yay"); -} + fn main() { + let t = Test::<&str$0, i32>(123, "yay"); + } "#]], Direction::Up, ); @@ -661,14 +661,14 @@ enum Hello { } fn main() {} - "#, +"#, expect![[r#" -enum Hello { - Two$0, - One -} + enum Hello { + Two$0, + One + } -fn main() {} + fn main() {} "#]], Direction::Up, ); @@ -685,15 +685,15 @@ trait Two {} fn test(t: T) {} fn main() {} - "#, +"#, expect![[r#" -trait One {} + trait One {} -trait Two {} + trait Two {} -fn test(t: T) {} + fn test(t: T) {} -fn main() {} + fn main() {} "#]], Direction::Up, ); @@ -722,27 +722,27 @@ impl Yay for Test { println!("Mmmm"); } } - "#, +"#, expect![[r#" -struct Test; + struct Test; -trait Yay { - type One; + trait Yay { + type One; - type Two; + type Two; - fn inner(); -} + fn inner(); + } -impl Yay for Test { - type One = i32; + impl Yay for Test { + type One = i32; - fn inner() {$0 - println!("Mmmm"); - } + fn inner() {$0 + println!("Mmmm"); + } - type Two = u32; -} + type Two = u32; + } "#]], Direction::Up, ); @@ -761,17 +761,17 @@ fn test() { fn inner() {} } } - "#, +"#, expect![[r#" -fn test() { - mod hi {$0 - fn inner() {} - } + fn test() { + mod hi {$0 + fn inner() {} + } - mod hello { - fn inner() {} - } -} + mod hello { + fn inner() {} + } + } "#]], Direction::Up, ); @@ -788,16 +788,16 @@ enum FooBar { } fn main() {} - "#, - expect![[r#" -fn main() {} +"#, + expect![[r##" + fn main() {} -$0#[derive(Debug)] -enum FooBar { - Foo, - Bar, -} - "#]], + $0#[derive(Debug)] + enum FooBar { + Foo, + Bar, + } + "##]], Direction::Down, ); check( @@ -808,14 +808,14 @@ $0$0enum FooBar { } fn main() {} - "#, +"#, expect![[r#" -fn main() {} + fn main() {} -$0enum FooBar { - Foo, - Bar, -} + $0enum FooBar { + Foo, + Bar, + } "#]], Direction::Down, ); @@ -828,15 +828,15 @@ trait SomeTrait {} $0$0impl SomeTrait for Test {} fn main() {} - "#, +"#, expect![[r#" -struct Test; + struct Test; -$0impl SomeTrait for Test {} + $0impl SomeTrait for Test {} -trait SomeTrait {} + trait SomeTrait {} -fn main() {} + fn main() {} "#]], Direction::Up, ); @@ -852,14 +852,14 @@ enum FooBar { }$0$0 fn main() {} - "#, +"#, expect![[r#" -fn main() {} + fn main() {} -enum FooBar { - Foo, - Bar, -}$0 + enum FooBar { + Foo, + Bar, + }$0 "#]], Direction::Down, ); @@ -872,15 +872,15 @@ trait SomeTrait {} impl SomeTrait for Test {}$0$0 fn main() {} - "#, +"#, expect![[r#" -struct Test; + struct Test; -impl SomeTrait for Test {}$0 + impl SomeTrait for Test {}$0 -trait SomeTrait {} + trait SomeTrait {} -fn main() {} + fn main() {} "#]], Direction::Up, ); From f04cff102f2748f8b51e25cf1c28a6097fee1592 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 30 Jul 2021 13:12:52 +0200 Subject: [PATCH 3/4] Simplify --- crates/parser/src/grammar/expressions/atom.rs | 6 ++---- .../test_data/parser/err/0032_match_arms_inner_attrs.rast | 4 ++-- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/crates/parser/src/grammar/expressions/atom.rs b/crates/parser/src/grammar/expressions/atom.rs index 8e7dbea3e7..70037c02d4 100644 --- a/crates/parser/src/grammar/expressions/atom.rs +++ b/crates/parser/src/grammar/expressions/atom.rs @@ -448,10 +448,8 @@ fn match_arm(p: &mut Parser) { // _ => () // } // } - if blocklike.is_block() { - p.eat(T![,]); - } else if !p.at(T!['}']) { - p.expect(T![,]); + if !p.eat(T![,]) && !blocklike.is_block() && !p.at(T!['}']) { + p.error("expected `,`"); } m.complete(p, MATCH_ARM); } diff --git a/crates/syntax/test_data/parser/err/0032_match_arms_inner_attrs.rast b/crates/syntax/test_data/parser/err/0032_match_arms_inner_attrs.rast index 83b9e79c3a..9524f5ccc7 100644 --- a/crates/syntax/test_data/parser/err/0032_match_arms_inner_attrs.rast +++ b/crates/syntax/test_data/parser/err/0032_match_arms_inner_attrs.rast @@ -194,11 +194,11 @@ SOURCE_FILE@0..293 error 52..52: expected `[` error 52..52: expected pattern error 53..53: expected FAT_ARROW -error 78..78: expected COMMA +error 78..78: expected `,` error 161..161: expected `[` error 161..161: expected pattern error 162..162: expected FAT_ARROW error 232..232: expected `[` error 232..232: expected pattern error 233..233: expected FAT_ARROW -error 250..250: expected COMMA +error 250..250: expected `,` From 82c1e61887e45bf138888181173cc669352173a7 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 30 Jul 2021 15:51:07 +0200 Subject: [PATCH 4/4] Fix assists assuming comma belonging to MATCH_ARM_LIST --- .../ide_assists/src/handlers/merge_match_arms.rs | 6 +++--- .../ide_diagnostics/src/handlers/inactive_code.rs | 2 +- crates/syntax/src/ast/edit_in_place.rs | 14 +++++--------- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/crates/ide_assists/src/handlers/merge_match_arms.rs b/crates/ide_assists/src/handlers/merge_match_arms.rs index 5c6bb986b9..0ebc5d267c 100644 --- a/crates/ide_assists/src/handlers/merge_match_arms.rs +++ b/crates/ide_assists/src/handlers/merge_match_arms.rs @@ -72,7 +72,7 @@ pub(crate) fn merge_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Option .join(" | ") }; - let arm = format!("{} => {}", pats, current_expr.syntax().text()); + let arm = format!("{} => {},", pats, current_expr.syntax().text()); if let [first, .., last] = &*arms_to_merge { let start = first.syntax().text_range().start(); @@ -118,7 +118,7 @@ enum X { A, B, C } fn main() { let x = X::A; let y = match x { - X::A | X::B => { 1i32 } + X::A | X::B => { 1i32 }, X::C => { 2i32 } } } @@ -183,7 +183,7 @@ fn main() { let x = X::A; let y = match x { X::A => { 1i32 }, - _ => { 2i32 } + _ => { 2i32 }, } } "#, diff --git a/crates/ide_diagnostics/src/handlers/inactive_code.rs b/crates/ide_diagnostics/src/handlers/inactive_code.rs index dfd0e33519..b6a7aee59e 100644 --- a/crates/ide_diagnostics/src/handlers/inactive_code.rs +++ b/crates/ide_diagnostics/src/handlers/inactive_code.rs @@ -64,7 +64,7 @@ fn f() { match () { () => (), #[cfg(a)] () => (), - //^^^^^^^^^^^^^^^^^^ weak: code is inactive due to #[cfg] directives: a is disabled + //^^^^^^^^^^^^^^^^^^^ weak: code is inactive due to #[cfg] directives: a is disabled } #[cfg(a)] 0 // Trailing expression of block diff --git a/crates/syntax/src/ast/edit_in_place.rs b/crates/syntax/src/ast/edit_in_place.rs index 30af469bc7..e9280ab45c 100644 --- a/crates/syntax/src/ast/edit_in_place.rs +++ b/crates/syntax/src/ast/edit_in_place.rs @@ -359,14 +359,10 @@ impl ast::MatchArmList { let mut elements = Vec::new(); let position = match self.arms().last() { Some(last_arm) => { - let comma = last_arm - .syntax() - .siblings_with_tokens(Direction::Next) - .find(|it| it.kind() == T![,]); - if needs_comma(&last_arm) && comma.is_none() { - elements.push(make::token(SyntaxKind::COMMA).into()); + if needs_comma(&last_arm) { + ted::append_child(last_arm.syntax(), make::token(SyntaxKind::COMMA)); } - Position::after(comma.unwrap_or_else(|| last_arm.syntax().clone().into())) + Position::after(last_arm.syntax().clone()) } None => match self.l_curly_token() { Some(it) => Position::after(it), @@ -377,12 +373,12 @@ impl ast::MatchArmList { 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::append_child(arm.syntax(), make::token(SyntaxKind::COMMA)); } ted::insert_all(position, elements); fn needs_comma(arm: &ast::MatchArm) -> bool { - arm.expr().map_or(false, |e| !e.is_block_like()) + arm.expr().map_or(false, |e| !e.is_block_like()) && arm.comma_token().is_none() } } }