From 4ef9b8d17a75d3f68951f506ad390e1367c1b2ad Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 26 Oct 2019 18:03:55 +0300 Subject: use correct spacing for enum pattern --- crates/ra_syntax/src/ast/make.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs index 00422ea91..76dad9155 100644 --- a/crates/ra_syntax/src/ast/make.rs +++ b/crates/ra_syntax/src/ast/make.rs @@ -77,7 +77,7 @@ pub fn tuple_struct_pat( pub fn record_pat(path: ast::Path, pats: impl Iterator) -> ast::RecordPat { let pats_str = pats.map(|p| p.syntax().to_string()).join(", "); - return from_text(&format!("{}{{ {} }}", path.syntax(), pats_str)); + return from_text(&format!("{} {{ {} }}", path.syntax(), pats_str)); fn from_text(text: &str) -> ast::RecordPat { ast_from_text(&format!("fn f({}: ())", text)) -- cgit v1.2.3 From 1438f38eb63e5a79350fa6c877d9960ab90e183d Mon Sep 17 00:00:00 2001 From: Jeremy Kolb Date: Sun, 27 Oct 2019 16:56:25 -0400 Subject: Preserve whitespace at the end of doc comments Whitespace can have special meaning in markdown. For instance ending a line with three spaces will render a new line. Note that this behavior diverges from RLS. Fixes #1997 --- crates/ra_syntax/src/ast.rs | 24 ++++++++++++++++++++++-- crates/ra_syntax/src/ast/traits.rs | 7 +++++-- 2 files changed, 27 insertions(+), 4 deletions(-) (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index 1b2ce921a..a12da5be2 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs @@ -173,7 +173,7 @@ fn test_doc_comment_single_line_block_strips_suffix_whitespace() { .ok() .unwrap(); let module = file.syntax().descendants().find_map(Module::cast).unwrap(); - assert_eq!("this is mod foo", module.doc_comment_text().unwrap()); + assert_eq!("this is mod foo ", module.doc_comment_text().unwrap()); } #[test] @@ -191,7 +191,27 @@ fn test_doc_comment_multi_line_block_strips_suffix() { .ok() .unwrap(); let module = file.syntax().descendants().find_map(Module::cast).unwrap(); - assert_eq!(" this\n is\n mod foo", module.doc_comment_text().unwrap()); + assert_eq!( + " this\n is\n mod foo\n ", + module.doc_comment_text().unwrap() + ); +} + +#[test] +fn test_comments_preserve_trailing_whitespace() { + let file = SourceFile::parse( + r#" +/// Representation of a Realm. +/// In the specification these are called Realm Records. +struct Realm {}"#, + ) + .ok() + .unwrap(); + let def = file.syntax().descendants().find_map(StructDef::cast).unwrap(); + assert_eq!( + "Representation of a Realm. \nIn the specification these are called Realm Records.", + def.doc_comment_text().unwrap() + ); } #[test] diff --git a/crates/ra_syntax/src/ast/traits.rs b/crates/ra_syntax/src/ast/traits.rs index f275a4955..76313684e 100644 --- a/crates/ra_syntax/src/ast/traits.rs +++ b/crates/ra_syntax/src/ast/traits.rs @@ -120,7 +120,7 @@ pub trait DocCommentsOwner: AstNode { has_comments = true; let prefix_len = comment.prefix().len(); - let line = comment.text().as_str(); + let line: &str = comment.text().as_str(); // Determine if the prefix or prefix + 1 char is stripped let pos = @@ -136,7 +136,10 @@ pub trait DocCommentsOwner: AstNode { line.len() }; - line[pos..end].trim_end().to_owned() + // Note that we do not trim the end of the line here + // since whitespace can have special meaning at the end + // of a line in markdown. + line[pos..end].to_owned() }) .join("\n"); -- cgit v1.2.3 From 5a6db3ca297357b7fb6319ce9b096f4ec15e7997 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 28 Oct 2019 15:36:38 +0300 Subject: fix parsing of for loops inside expressions closes #2051 --- .../test_data/parser/err/0024_many_type_parens.txt | 91 +++++++++---------- .../test_data/parser/ok/0059_loops_in_parens.rs | 5 + .../test_data/parser/ok/0059_loops_in_parens.txt | 101 +++++++++++++++++++++ 3 files changed, 149 insertions(+), 48 deletions(-) create mode 100644 crates/ra_syntax/test_data/parser/ok/0059_loops_in_parens.rs create mode 100644 crates/ra_syntax/test_data/parser/ok/0059_loops_in_parens.txt (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/test_data/parser/err/0024_many_type_parens.txt b/crates/ra_syntax/test_data/parser/err/0024_many_type_parens.txt index 198daf7b4..0a93e11a5 100644 --- a/crates/ra_syntax/test_data/parser/err/0024_many_type_parens.txt +++ b/crates/ra_syntax/test_data/parser/err/0024_many_type_parens.txt @@ -179,50 +179,47 @@ SOURCE_FILE@[0; 240) ERROR@[148; 149) PLUS@[148; 149) "+" WHITESPACE@[149; 150) " " - EXPR_STMT@[150; 151) - PAREN_EXPR@[150; 151) + EXPR_STMT@[150; 180) + TUPLE_EXPR@[150; 180) L_PAREN@[150; 151) "(" - EXPR_STMT@[151; 157) - FOR_EXPR@[151; 157) - FOR_KW@[151; 154) "for" - ERROR@[154; 155) - L_ANGLE@[154; 155) "<" - ERROR@[155; 157) - LIFETIME@[155; 157) "\'a" - EXPR_STMT@[157; 158) - ERROR@[157; 158) - R_ANGLE@[157; 158) ">" - WHITESPACE@[158; 159) " " - EXPR_STMT@[159; 180) - BIN_EXPR@[159; 180) - BIN_EXPR@[159; 178) - BIN_EXPR@[159; 169) - BIN_EXPR@[159; 167) - PATH_EXPR@[159; 164) - PATH@[159; 164) - PATH_SEGMENT@[159; 164) - NAME_REF@[159; 164) - IDENT@[159; 164) "Trait" - L_ANGLE@[164; 165) "<" - ERROR@[165; 167) - LIFETIME@[165; 167) "\'a" - R_ANGLE@[167; 168) ">" - ERROR@[168; 169) - R_PAREN@[168; 169) ")" - WHITESPACE@[169; 170) " " - PLUS@[170; 171) "+" - WHITESPACE@[171; 172) " " - PAREN_EXPR@[172; 178) - L_PAREN@[172; 173) "(" - PATH_EXPR@[173; 177) - PATH@[173; 177) - PATH_SEGMENT@[173; 177) - NAME_REF@[173; 177) - IDENT@[173; 177) "Copy" - R_PAREN@[177; 178) ")" - R_ANGLE@[178; 179) ">" - ERROR@[179; 180) - SEMI@[179; 180) ";" + BIN_EXPR@[151; 180) + BIN_EXPR@[151; 178) + BIN_EXPR@[151; 169) + BIN_EXPR@[151; 167) + BIN_EXPR@[151; 164) + FOR_EXPR@[151; 157) + FOR_KW@[151; 154) "for" + ERROR@[154; 155) + L_ANGLE@[154; 155) "<" + ERROR@[155; 157) + LIFETIME@[155; 157) "\'a" + R_ANGLE@[157; 158) ">" + WHITESPACE@[158; 159) " " + PATH_EXPR@[159; 164) + PATH@[159; 164) + PATH_SEGMENT@[159; 164) + NAME_REF@[159; 164) + IDENT@[159; 164) "Trait" + L_ANGLE@[164; 165) "<" + ERROR@[165; 167) + LIFETIME@[165; 167) "\'a" + R_ANGLE@[167; 168) ">" + ERROR@[168; 169) + R_PAREN@[168; 169) ")" + WHITESPACE@[169; 170) " " + PLUS@[170; 171) "+" + WHITESPACE@[171; 172) " " + PAREN_EXPR@[172; 178) + L_PAREN@[172; 173) "(" + PATH_EXPR@[173; 177) + PATH@[173; 177) + PATH_SEGMENT@[173; 177) + NAME_REF@[173; 177) + IDENT@[173; 177) "Copy" + R_PAREN@[177; 178) ")" + R_ANGLE@[178; 179) ">" + ERROR@[179; 180) + SEMI@[179; 180) ";" WHITESPACE@[180; 185) "\n " LET_STMT@[185; 235) LET_KW@[185; 188) "let" @@ -307,18 +304,16 @@ error 146: expected expression error 147: expected SEMI error 148: expected expression error 149: expected SEMI -error 151: expected expression -error 151: expected R_PAREN -error 151: expected SEMI error 154: expected pattern error 155: expected IN_KW error 155: expected expression error 157: expected a block -error 157: expected expression -error 158: expected SEMI error 165: expected expression error 168: expected expression error 179: expected expression +error 180: expected COMMA +error 180: expected expression +error 180: expected R_PAREN error 180: expected SEMI error 215: expected COMMA error 215: expected R_ANGLE diff --git a/crates/ra_syntax/test_data/parser/ok/0059_loops_in_parens.rs b/crates/ra_syntax/test_data/parser/ok/0059_loops_in_parens.rs new file mode 100644 index 000000000..6e8b718aa --- /dev/null +++ b/crates/ra_syntax/test_data/parser/ok/0059_loops_in_parens.rs @@ -0,0 +1,5 @@ +fn main() { + Some(for _ in [1].into_iter() {}); + Some(loop { break; }); + Some(while true {}); +} diff --git a/crates/ra_syntax/test_data/parser/ok/0059_loops_in_parens.txt b/crates/ra_syntax/test_data/parser/ok/0059_loops_in_parens.txt new file mode 100644 index 000000000..c011187ea --- /dev/null +++ b/crates/ra_syntax/test_data/parser/ok/0059_loops_in_parens.txt @@ -0,0 +1,101 @@ +SOURCE_FILE@[0; 105) + FN_DEF@[0; 104) + FN_KW@[0; 2) "fn" + WHITESPACE@[2; 3) " " + NAME@[3; 7) + IDENT@[3; 7) "main" + PARAM_LIST@[7; 9) + L_PAREN@[7; 8) "(" + R_PAREN@[8; 9) ")" + WHITESPACE@[9; 10) " " + BLOCK_EXPR@[10; 104) + BLOCK@[10; 104) + L_CURLY@[10; 11) "{" + WHITESPACE@[11; 16) "\n " + EXPR_STMT@[16; 50) + CALL_EXPR@[16; 49) + PATH_EXPR@[16; 20) + PATH@[16; 20) + PATH_SEGMENT@[16; 20) + NAME_REF@[16; 20) + IDENT@[16; 20) "Some" + ARG_LIST@[20; 49) + L_PAREN@[20; 21) "(" + FOR_EXPR@[21; 48) + FOR_KW@[21; 24) "for" + WHITESPACE@[24; 25) " " + PLACEHOLDER_PAT@[25; 26) + UNDERSCORE@[25; 26) "_" + WHITESPACE@[26; 27) " " + IN_KW@[27; 29) "in" + WHITESPACE@[29; 30) " " + METHOD_CALL_EXPR@[30; 45) + ARRAY_EXPR@[30; 33) + L_BRACK@[30; 31) "[" + LITERAL@[31; 32) + INT_NUMBER@[31; 32) "1" + R_BRACK@[32; 33) "]" + DOT@[33; 34) "." + NAME_REF@[34; 43) + IDENT@[34; 43) "into_iter" + ARG_LIST@[43; 45) + L_PAREN@[43; 44) "(" + R_PAREN@[44; 45) ")" + WHITESPACE@[45; 46) " " + BLOCK_EXPR@[46; 48) + BLOCK@[46; 48) + L_CURLY@[46; 47) "{" + R_CURLY@[47; 48) "}" + R_PAREN@[48; 49) ")" + SEMI@[49; 50) ";" + WHITESPACE@[50; 55) "\n " + EXPR_STMT@[55; 77) + CALL_EXPR@[55; 76) + PATH_EXPR@[55; 59) + PATH@[55; 59) + PATH_SEGMENT@[55; 59) + NAME_REF@[55; 59) + IDENT@[55; 59) "Some" + ARG_LIST@[59; 76) + L_PAREN@[59; 60) "(" + LOOP_EXPR@[60; 75) + LOOP_KW@[60; 64) "loop" + WHITESPACE@[64; 65) " " + BLOCK_EXPR@[65; 75) + BLOCK@[65; 75) + L_CURLY@[65; 66) "{" + WHITESPACE@[66; 67) " " + EXPR_STMT@[67; 73) + BREAK_EXPR@[67; 72) + BREAK_KW@[67; 72) "break" + SEMI@[72; 73) ";" + WHITESPACE@[73; 74) " " + R_CURLY@[74; 75) "}" + R_PAREN@[75; 76) ")" + SEMI@[76; 77) ";" + WHITESPACE@[77; 82) "\n " + EXPR_STMT@[82; 102) + CALL_EXPR@[82; 101) + PATH_EXPR@[82; 86) + PATH@[82; 86) + PATH_SEGMENT@[82; 86) + NAME_REF@[82; 86) + IDENT@[82; 86) "Some" + ARG_LIST@[86; 101) + L_PAREN@[86; 87) "(" + WHILE_EXPR@[87; 100) + WHILE_KW@[87; 92) "while" + WHITESPACE@[92; 93) " " + CONDITION@[93; 97) + LITERAL@[93; 97) + TRUE_KW@[93; 97) "true" + WHITESPACE@[97; 98) " " + BLOCK_EXPR@[98; 100) + BLOCK@[98; 100) + L_CURLY@[98; 99) "{" + R_CURLY@[99; 100) "}" + R_PAREN@[100; 101) ")" + SEMI@[101; 102) ";" + WHITESPACE@[102; 103) "\n" + R_CURLY@[103; 104) "}" + WHITESPACE@[104; 105) "\n" -- cgit v1.2.3 From b441b4e8effeaf4532fd2e45c4d864480857c49e Mon Sep 17 00:00:00 2001 From: kjeremy Date: Wed, 30 Oct 2019 13:36:37 -0400 Subject: Some clippy fixes --- crates/ra_syntax/src/ast/make.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs index 76dad9155..3d5f18bfa 100644 --- a/crates/ra_syntax/src/ast/make.rs +++ b/crates/ra_syntax/src/ast/make.rs @@ -129,11 +129,11 @@ pub fn where_clause(preds: impl Iterator) -> ast::WhereCl } pub fn if_expression(condition: &ast::Expr, statement: &str) -> ast::IfExpr { - return ast_from_text(&format!( + ast_from_text(&format!( "fn f() {{ if !{} {{\n {}\n}}\n}}", condition.syntax().text(), statement - )); + )) } fn ast_from_text(text: &str) -> N { -- cgit v1.2.3 From 7ad55e976c3f88e92075379c8a4c1f413665b458 Mon Sep 17 00:00:00 2001 From: kjeremy Date: Wed, 30 Oct 2019 14:38:45 -0400 Subject: Document match_ast! --- crates/ra_syntax/src/lib.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs index c315ba552..5dcb6a95a 100644 --- a/crates/ra_syntax/src/lib.rs +++ b/crates/ra_syntax/src/lib.rs @@ -160,6 +160,20 @@ impl SourceFile { } } +/// Matches a `SyntaxNode` against an `ast` type. +/// +/// # Example: +/// +/// ```ignore +/// match_ast! { +/// match node { +/// ast::CallExpr(it) => { ... }, +/// ast::MethodCallExpr(it) => { ... }, +/// ast::MacroCall(it) => { ... }, +/// _ => None, +/// } +/// } +/// ``` #[macro_export] macro_rules! match_ast { (match $node:ident { -- cgit v1.2.3 From 78f93c8033d4803167536a070c2162b072e16055 Mon Sep 17 00:00:00 2001 From: kjeremy Date: Wed, 30 Oct 2019 16:08:59 -0400 Subject: traits => match_ast! --- crates/ra_syntax/src/ast/traits.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/src/ast/traits.rs b/crates/ra_syntax/src/ast/traits.rs index 76313684e..c2b005886 100644 --- a/crates/ra_syntax/src/ast/traits.rs +++ b/crates/ra_syntax/src/ast/traits.rs @@ -6,6 +6,7 @@ use itertools::Itertools; use crate::{ ast::{self, child_opt, children, AstChildren, AstNode, AstToken}, + match_ast, syntax_node::{SyntaxElementChildren, SyntaxNodeChildren}, }; @@ -68,11 +69,12 @@ impl Iterator for ItemOrMacroIter { fn next(&mut self) -> Option { loop { let n = self.0.next()?; - if let Some(item) = ast::ModuleItem::cast(n.clone()) { - return Some(ItemOrMacro::Item(item)); - } - if let Some(call) = ast::MacroCall::cast(n) { - return Some(ItemOrMacro::Macro(call)); + match_ast! { + match n { + ast::ModuleItem(it) => { return Some(ItemOrMacro::Item(it)) }, + ast::MacroCall(it) => { return Some(ItemOrMacro::Macro(it)) }, + _ => {}, + } } } } -- cgit v1.2.3