Fold multiline comments

This commit is contained in:
Adolfo Ochagavía 2018-10-12 08:59:12 +02:00
parent 2ba6f18586
commit ee0a6bf053
2 changed files with 21 additions and 21 deletions

View file

@ -1,6 +1,8 @@
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
use ra_syntax::{ use ra_syntax::{
ast,
AstNode,
File, TextRange, SyntaxNodeRef, File, TextRange, SyntaxNodeRef,
SyntaxKind, SyntaxKind,
Direction, Direction,
@ -27,27 +29,25 @@ pub fn folding_ranges(file: &File) -> Vec<Fold> {
continue; continue;
} }
let range_and_kind = match node.kind() { if let Some(comment) = ast::Comment::cast(node) {
SyntaxKind::COMMENT => ( // Multiline comments (`/* ... */`) can only be folded if they span multiple lines
contiguous_range_for(SyntaxKind::COMMENT, node, &mut visited), let range = if let ast::CommentFlavor::Multiline = comment.flavor() {
Some(FoldKind::Comment), if comment.text().contains('\n') {
), Some(comment.syntax().range())
SyntaxKind::USE_ITEM => ( } else {
contiguous_range_for(SyntaxKind::USE_ITEM, node, &mut visited), None
Some(FoldKind::Imports), }
), } else {
_ => (None, None), contiguous_range_for(SyntaxKind::COMMENT, node, &mut visited)
}; };
match range_and_kind { range.map(|range| res.push(Fold { range, kind: FoldKind::Comment }));
(Some(range), Some(kind)) => {
res.push(Fold {
range: range,
kind: kind
});
}
_ => {}
} }
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 res

View file

@ -20,8 +20,8 @@ export function activate(context: vscode.ExtensionContext) {
f: (...args: any[]) => Promise<boolean> f: (...args: any[]) => Promise<boolean>
) { ) {
const defaultCmd = `default:${name}`; const defaultCmd = `default:${name}`;
const original = async (...args: any[]) => const original = (...args: any[]) =>
await vscode.commands.executeCommand(defaultCmd, ...args); vscode.commands.executeCommand(defaultCmd, ...args);
registerCommand(name, async (...args: any[]) => { registerCommand(name, async (...args: any[]) => {
const editor = vscode.window.activeTextEditor; const editor = vscode.window.activeTextEditor;