From 8d7e8a175e6067e4956a23cfd0c1baf003678734 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 20 Dec 2018 22:30:30 +0300 Subject: generalize folding tests By using xml-like tags, we will be able to test nested foldings. --- crates/ra_editor/src/folding_ranges.rs | 42 +++++++++++++++++----------------- crates/test_utils/src/lib.rs | 38 ++++++++++++++++++++++++------ 2 files changed, 52 insertions(+), 28 deletions(-) (limited to 'crates') diff --git a/crates/ra_editor/src/folding_ranges.rs b/crates/ra_editor/src/folding_ranges.rs index 2a8fa3cda..a4add8702 100644 --- a/crates/ra_editor/src/folding_ranges.rs +++ b/crates/ra_editor/src/folding_ranges.rs @@ -170,7 +170,7 @@ mod tests { use test_utils::extract_ranges; fn do_check(text: &str, fold_kinds: &[FoldKind]) { - let (ranges, text) = extract_ranges(text); + let (ranges, text) = extract_ranges(text, "fold"); let file = SourceFileNode::parse(&text); let folds = folding_ranges(&file); @@ -198,22 +198,22 @@ mod tests { #[test] fn test_fold_comments() { let text = r#" -<|>// Hello +// Hello // this is a multiline // comment -//<|> +// // But this is not fn main() { - <|>// We should + // We should // also // fold - // this one.<|> - <|>//! But this one is different - //! because it has another flavor<|> - <|>/* As does this - multiline comment */<|> + // this one. + //! But this one is different + //! because it has another flavor + /* As does this + multiline comment */ }"#; let fold_kinds = &[ @@ -228,11 +228,11 @@ fn main() { #[test] fn test_fold_imports() { let text = r#" -<|>use std::{ +use std::{ str, vec, io as iop -};<|> +}; fn main() { }"#; @@ -244,12 +244,12 @@ fn main() { #[test] fn test_fold_import_groups() { let text = r#" -<|>use std::str; +use std::str; use std::vec; -use std::io as iop;<|> +use std::io as iop; -<|>use std::mem; -use std::f64;<|> +use std::mem; +use std::f64; use std::collections::HashMap; // Some random comment @@ -265,17 +265,17 @@ fn main() { #[test] fn test_fold_import_and_groups() { let text = r#" -<|>use std::str; +use std::str; use std::vec; -use std::io as iop;<|> +use std::io as iop; -<|>use std::mem; -use std::f64;<|> +use std::mem; +use std::f64; -<|>use std::collections::{ +use std::collections::{ HashMap, VecDeque, -};<|> +}; // Some random comment fn main() { diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index 0a94adb74..894a22769 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -66,15 +66,39 @@ pub fn try_extract_range(text: &str) -> Option<(TextRange, String)> { Some((TextRange::from_to(start, end), text)) } -pub fn extract_ranges(text: &str) -> (Vec, String) { +/// Extracts ranges, marked with ` ` paris from the `text` +pub fn extract_ranges(mut text: &str, tag: &str) -> (Vec, String) { + let open = format!("<{}>", tag); + let close = format!("", tag); let mut ranges = Vec::new(); - let mut text = String::from(text); - while let Some((range, new_text)) = try_extract_range(&text) { - text = new_text; - ranges.push(range); + let mut res = String::new(); + let mut stack = Vec::new(); + loop { + match text.find('<') { + None => { + res.push_str(text); + break; + } + Some(i) => { + res.push_str(&text[..i]); + text = &text[i..]; + if text.starts_with(&open) { + text = &text[open.len()..]; + let from = TextUnit::of_str(&res); + stack.push(from); + } else if text.starts_with(&close) { + text = &text[close.len()..]; + let from = stack + .pop() + .unwrap_or_else(|| panic!("unmatched ", tag)); + let to = TextUnit::of_str(&res); + ranges.push(TextRange::from_to(from, to)); + } + } + } } - - (ranges, text) + assert!(stack.is_empty(), "unmatched <{}>", tag); + (ranges, res) } pub fn add_cursor(text: &str, offset: TextUnit) -> String { -- cgit v1.2.3 From 23b040962ff299feeef1f967bc2d5ba92b01c2bc Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 20 Dec 2018 22:13:16 +0300 Subject: fold curly blocks --- crates/ra_editor/src/folding_ranges.rs | 40 ++++++++++++++++---------- crates/ra_lsp_server/src/main_loop/handlers.rs | 7 +++-- crates/test_utils/src/lib.rs | 1 + 3 files changed, 30 insertions(+), 18 deletions(-) (limited to 'crates') diff --git a/crates/ra_editor/src/folding_ranges.rs b/crates/ra_editor/src/folding_ranges.rs index a4add8702..da542ecf0 100644 --- a/crates/ra_editor/src/folding_ranges.rs +++ b/crates/ra_editor/src/folding_ranges.rs @@ -10,6 +10,7 @@ use ra_syntax::{ pub enum FoldKind { Comment, Imports, + Block, } #[derive(Debug)] @@ -62,6 +63,8 @@ fn fold_kind(kind: SyntaxKind) -> Option { match kind { COMMENT => Some(FoldKind::Comment), USE_ITEM => Some(FoldKind::Imports), + NAMED_FIELD_DEF_LIST | FIELD_PAT_LIST | ITEM_LIST | EXTERN_ITEM_LIST | USE_TREE_LIST + | BLOCK | ENUM_VARIANT_LIST => Some(FoldKind::Block), _ => None, } } @@ -205,7 +208,7 @@ mod tests { // But this is not -fn main() { +fn main() { // We should // also // fold @@ -214,10 +217,11 @@ fn main() { //! because it has another flavor /* As does this multiline comment */ -}"#; +}"#; let fold_kinds = &[ FoldKind::Comment, + FoldKind::Block, FoldKind::Comment, FoldKind::Comment, FoldKind::Comment, @@ -228,16 +232,16 @@ fn main() { #[test] fn test_fold_imports() { let text = r#" -use std::{ +use std::{ str, vec, io as iop -}; +}; -fn main() { -}"#; +fn main() { +}"#; - let folds = &[FoldKind::Imports]; + let folds = &[FoldKind::Imports, FoldKind::Block, FoldKind::Block]; do_check(text, folds); } @@ -255,10 +259,10 @@ use std::collections::HashMap; // Some random comment use std::collections::VecDeque; -fn main() { -}"#; +fn main() { +}"#; - let folds = &[FoldKind::Imports, FoldKind::Imports]; + let folds = &[FoldKind::Imports, FoldKind::Imports, FoldKind::Block]; do_check(text, folds); } @@ -272,16 +276,22 @@ use std::io as iop; use std::mem; use std::f64; -use std::collections::{ +use std::collections::{ HashMap, VecDeque, -}; +}; // Some random comment -fn main() { -}"#; +fn main() { +}"#; - let folds = &[FoldKind::Imports, FoldKind::Imports, FoldKind::Imports]; + let folds = &[ + FoldKind::Imports, + FoldKind::Imports, + FoldKind::Imports, + FoldKind::Block, + FoldKind::Block, + ]; do_check(text, folds); } diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index 572ae7fb5..801966304 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -446,8 +446,9 @@ pub fn handle_folding_range( .into_iter() .map(|fold| { let kind = match fold.kind { - FoldKind::Comment => FoldingRangeKind::Comment, - FoldKind::Imports => FoldingRangeKind::Imports, + FoldKind::Comment => Some(FoldingRangeKind::Comment), + FoldKind::Imports => Some(FoldingRangeKind::Imports), + FoldKind::Block => None, }; let range = fold.range.conv_with(&line_index); FoldingRange { @@ -455,7 +456,7 @@ pub fn handle_folding_range( start_character: Some(range.start.character), end_line: range.end.line, end_character: Some(range.start.character), - kind: Some(kind), + kind, } }) .collect(), diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index 894a22769..1ae800d7c 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -98,6 +98,7 @@ pub fn extract_ranges(mut text: &str, tag: &str) -> (Vec, String) { } } assert!(stack.is_empty(), "unmatched <{}>", tag); + ranges.sort_by_key(|r| (r.start(), r.end())); (ranges, res) } -- cgit v1.2.3 From 2956e812955772cdbfb7666141a3a584d78f713f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 20 Dec 2018 22:57:38 +0300 Subject: :arrow_up: 1.31.1 --- crates/tools/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates') diff --git a/crates/tools/src/lib.rs b/crates/tools/src/lib.rs index 580d8b802..2795afe0b 100644 --- a/crates/tools/src/lib.rs +++ b/crates/tools/src/lib.rs @@ -15,7 +15,7 @@ pub type Result = std::result::Result; pub const GRAMMAR: &str = "crates/ra_syntax/src/grammar.ron"; pub const SYNTAX_KINDS: &str = "crates/ra_syntax/src/syntax_kinds/generated.rs.tera"; pub const AST: &str = "crates/ra_syntax/src/ast/generated.rs.tera"; -const TOOLCHAIN: &str = "1.31.0"; +const TOOLCHAIN: &str = "1.31.1"; #[derive(Debug)] pub struct Test { -- cgit v1.2.3