From ee0a6bf0535a5a6c7e536d2cffa11959c3ee2ae3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20Ochagav=C3=ADa?= Date: Fri, 12 Oct 2018 08:59:12 +0200 Subject: Fold multiline comments --- crates/ra_editor/src/folding_ranges.rs | 38 +++++++++++++++++----------------- 1 file changed, 19 insertions(+), 19 deletions(-) (limited to 'crates/ra_editor/src') diff --git a/crates/ra_editor/src/folding_ranges.rs b/crates/ra_editor/src/folding_ranges.rs index 3aabd54ae..1d89ac853 100644 --- a/crates/ra_editor/src/folding_ranges.rs +++ b/crates/ra_editor/src/folding_ranges.rs @@ -1,6 +1,8 @@ use rustc_hash::FxHashSet; use ra_syntax::{ + ast, + AstNode, File, TextRange, SyntaxNodeRef, SyntaxKind, Direction, @@ -27,27 +29,25 @@ pub fn folding_ranges(file: &File) -> Vec { continue; } - let range_and_kind = match node.kind() { - SyntaxKind::COMMENT => ( - contiguous_range_for(SyntaxKind::COMMENT, node, &mut visited), - Some(FoldKind::Comment), - ), - SyntaxKind::USE_ITEM => ( - contiguous_range_for(SyntaxKind::USE_ITEM, node, &mut visited), - Some(FoldKind::Imports), - ), - _ => (None, None), - }; + if let Some(comment) = ast::Comment::cast(node) { + // Multiline comments (`/* ... */`) can only be folded if they span multiple lines + let range = if let ast::CommentFlavor::Multiline = comment.flavor() { + if comment.text().contains('\n') { + Some(comment.syntax().range()) + } else { + None + } + } else { + contiguous_range_for(SyntaxKind::COMMENT, node, &mut visited) + }; - match range_and_kind { - (Some(range), Some(kind)) => { - res.push(Fold { - range: range, - kind: kind - }); - } - _ => {} + range.map(|range| res.push(Fold { range, kind: FoldKind::Comment })); } + + if let SyntaxKind::USE_ITEM = node.kind() { + contiguous_range_for(SyntaxKind::USE_ITEM, node, &mut visited) + .map(|range| res.push(Fold { range, kind: FoldKind::Imports})); + }; } res -- cgit v1.2.3