aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_editor/src/folding_ranges.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_editor/src/folding_ranges.rs')
-rw-r--r--crates/ra_editor/src/folding_ranges.rs36
1 files changed, 20 insertions, 16 deletions
diff --git a/crates/ra_editor/src/folding_ranges.rs b/crates/ra_editor/src/folding_ranges.rs
index a1699d449..e5bc0c4ee 100644
--- a/crates/ra_editor/src/folding_ranges.rs
+++ b/crates/ra_editor/src/folding_ranges.rs
@@ -1,11 +1,9 @@
1use rustc_hash::FxHashSet; 1use rustc_hash::FxHashSet;
2 2
3use ra_syntax::{ 3use ra_syntax::{
4 ast, 4 ast, AstNode, Direction, File,
5 AstNode,
6 File, TextRange, SyntaxNodeRef,
7 SyntaxKind::{self, *}, 5 SyntaxKind::{self, *},
8 Direction, 6 SyntaxNodeRef, TextRange,
9}; 7};
10 8
11#[derive(Debug, PartialEq, Eq)] 9#[derive(Debug, PartialEq, Eq)]
@@ -28,7 +26,10 @@ pub fn folding_ranges(file: &File) -> Vec<Fold> {
28 // Fold items that span multiple lines 26 // Fold items that span multiple lines
29 if let Some(kind) = fold_kind(node.kind()) { 27 if let Some(kind) = fold_kind(node.kind()) {
30 if has_newline(node) { 28 if has_newline(node) {
31 res.push(Fold { range: node.range(), kind }); 29 res.push(Fold {
30 range: node.range(),
31 kind,
32 });
32 } 33 }
33 } 34 }
34 35
@@ -37,8 +38,12 @@ pub fn folding_ranges(file: &File) -> Vec<Fold> {
37 continue; 38 continue;
38 } 39 }
39 if node.kind() == COMMENT { 40 if node.kind() == COMMENT {
40 contiguous_range_for_comment(node, &mut visited_comments) 41 contiguous_range_for_comment(node, &mut visited_comments).map(|range| {
41 .map(|range| res.push(Fold { range, kind: FoldKind::Comment })); 42 res.push(Fold {
43 range,
44 kind: FoldKind::Comment,
45 })
46 });
42 } 47 }
43 } 48 }
44 49
@@ -49,13 +54,11 @@ fn fold_kind(kind: SyntaxKind) -> Option<FoldKind> {
49 match kind { 54 match kind {
50 COMMENT => Some(FoldKind::Comment), 55 COMMENT => Some(FoldKind::Comment),
51 USE_ITEM => Some(FoldKind::Imports), 56 USE_ITEM => Some(FoldKind::Imports),
52 _ => None 57 _ => None,
53 } 58 }
54} 59}
55 60
56fn has_newline( 61fn has_newline(node: SyntaxNodeRef) -> bool {
57 node: SyntaxNodeRef,
58) -> bool {
59 for descendant in node.descendants() { 62 for descendant in node.descendants() {
60 if let Some(ws) = ast::Whitespace::cast(descendant) { 63 if let Some(ws) = ast::Whitespace::cast(descendant) {
61 if ws.has_newlines() { 64 if ws.has_newlines() {
@@ -100,9 +103,7 @@ fn contiguous_range_for_comment<'a>(
100 // The comment group ends because either: 103 // The comment group ends because either:
101 // * An element of a different kind was reached 104 // * An element of a different kind was reached
102 // * A comment of a different flavor was reached 105 // * A comment of a different flavor was reached
103 _ => { 106 _ => break,
104 break
105 }
106 } 107 }
107 } 108 }
108 109
@@ -128,7 +129,11 @@ mod tests {
128 let folds = folding_ranges(&file); 129 let folds = folding_ranges(&file);
129 130
130 assert_eq!(folds.len(), ranges.len()); 131 assert_eq!(folds.len(), ranges.len());
131 for ((fold, range), fold_kind) in folds.into_iter().zip(ranges.into_iter()).zip(fold_kinds.into_iter()) { 132 for ((fold, range), fold_kind) in folds
133 .into_iter()
134 .zip(ranges.into_iter())
135 .zip(fold_kinds.into_iter())
136 {
132 assert_eq!(fold.range.start(), range.start()); 137 assert_eq!(fold.range.start(), range.start());
133 assert_eq!(fold.range.end(), range.end()); 138 assert_eq!(fold.range.end(), range.end());
134 assert_eq!(&fold.kind, fold_kind); 139 assert_eq!(&fold.kind, fold_kind);
@@ -181,5 +186,4 @@ fn main() {
181 do_check(text, folds); 186 do_check(text, folds);
182 } 187 }
183 188
184
185} 189}