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 +++++++++++++++++----------------- editors/code/src/extension.ts | 4 ++-- 2 files changed, 21 insertions(+), 21 deletions(-) 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 diff --git a/editors/code/src/extension.ts b/editors/code/src/extension.ts index ff8f23c7a..d1c525f68 100644 --- a/editors/code/src/extension.ts +++ b/editors/code/src/extension.ts @@ -20,8 +20,8 @@ export function activate(context: vscode.ExtensionContext) { f: (...args: any[]) => Promise ) { const defaultCmd = `default:${name}`; - const original = async (...args: any[]) => - await vscode.commands.executeCommand(defaultCmd, ...args); + const original = (...args: any[]) => + vscode.commands.executeCommand(defaultCmd, ...args); registerCommand(name, async (...args: any[]) => { const editor = vscode.window.activeTextEditor; -- cgit v1.2.3