2018-10-11 13:07:44 -05:00
|
|
|
use rustc_hash::FxHashSet;
|
2018-09-24 08:52:33 -05:00
|
|
|
|
|
|
|
use ra_syntax::{
|
2018-11-07 09:32:33 -06:00
|
|
|
ast, AstNode, Direction, SourceFileNode,
|
2018-10-12 12:49:08 -05:00
|
|
|
SyntaxKind::{self, *},
|
2018-10-15 16:44:23 -05:00
|
|
|
SyntaxNodeRef, TextRange,
|
2018-09-24 08:52:33 -05:00
|
|
|
};
|
|
|
|
|
2018-09-24 09:48:13 -05:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2018-09-24 08:52:33 -05:00
|
|
|
pub enum FoldKind {
|
|
|
|
Comment,
|
|
|
|
Imports,
|
|
|
|
}
|
|
|
|
|
2018-09-24 09:48:13 -05:00
|
|
|
#[derive(Debug)]
|
2018-09-24 08:52:33 -05:00
|
|
|
pub struct Fold {
|
|
|
|
pub range: TextRange,
|
|
|
|
pub kind: FoldKind,
|
|
|
|
}
|
|
|
|
|
2018-11-07 09:32:33 -06:00
|
|
|
pub fn folding_ranges(file: &SourceFileNode) -> Vec<Fold> {
|
2018-09-24 08:52:33 -05:00
|
|
|
let mut res = vec![];
|
2018-10-12 12:49:08 -05:00
|
|
|
let mut visited_comments = FxHashSet::default();
|
2018-10-23 07:58:02 -05:00
|
|
|
let mut visited_imports = FxHashSet::default();
|
2018-09-24 08:52:33 -05:00
|
|
|
|
2018-10-02 10:02:57 -05:00
|
|
|
for node in file.syntax().descendants() {
|
2018-10-12 12:20:58 -05:00
|
|
|
// Fold items that span multiple lines
|
|
|
|
if let Some(kind) = fold_kind(node.kind()) {
|
|
|
|
if has_newline(node) {
|
2018-10-15 16:44:23 -05:00
|
|
|
res.push(Fold {
|
|
|
|
range: node.range(),
|
|
|
|
kind,
|
|
|
|
});
|
2018-10-12 12:20:58 -05:00
|
|
|
}
|
2018-09-24 08:52:33 -05:00
|
|
|
}
|
|
|
|
|
2018-10-23 07:58:02 -05:00
|
|
|
// Fold groups of comments
|
|
|
|
if node.kind() == COMMENT && !visited_comments.contains(&node) {
|
2018-10-16 10:51:58 -05:00
|
|
|
if let Some(range) = contiguous_range_for_comment(node, &mut visited_comments) {
|
2018-10-15 16:44:23 -05:00
|
|
|
res.push(Fold {
|
|
|
|
range,
|
|
|
|
kind: FoldKind::Comment,
|
|
|
|
})
|
2018-10-16 10:51:58 -05:00
|
|
|
}
|
2018-10-12 12:20:58 -05:00
|
|
|
}
|
2018-10-23 07:58:02 -05:00
|
|
|
|
|
|
|
// Fold groups of imports
|
|
|
|
if node.kind() == USE_ITEM && !visited_imports.contains(&node) {
|
|
|
|
if let Some(range) = contiguous_range_for_group(node, &mut visited_imports) {
|
|
|
|
res.push(Fold {
|
|
|
|
range,
|
|
|
|
kind: FoldKind::Imports,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2018-09-24 08:52:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2018-10-12 12:20:58 -05:00
|
|
|
fn fold_kind(kind: SyntaxKind) -> Option<FoldKind> {
|
|
|
|
match kind {
|
2018-10-12 12:49:08 -05:00
|
|
|
COMMENT => Some(FoldKind::Comment),
|
|
|
|
USE_ITEM => Some(FoldKind::Imports),
|
2018-10-15 16:44:23 -05:00
|
|
|
_ => None,
|
2018-10-12 12:20:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-15 16:44:23 -05:00
|
|
|
fn has_newline(node: SyntaxNodeRef) -> bool {
|
2018-10-12 12:20:58 -05:00
|
|
|
for descendant in node.descendants() {
|
|
|
|
if let Some(ws) = ast::Whitespace::cast(descendant) {
|
|
|
|
if ws.has_newlines() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if let Some(comment) = ast::Comment::cast(descendant) {
|
|
|
|
if comment.has_newlines() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2018-10-23 07:58:02 -05:00
|
|
|
fn contiguous_range_for_group<'a>(
|
|
|
|
first: SyntaxNodeRef<'a>,
|
|
|
|
visited: &mut FxHashSet<SyntaxNodeRef<'a>>,
|
|
|
|
) -> Option<TextRange> {
|
|
|
|
visited.insert(first);
|
|
|
|
|
|
|
|
let mut last = first;
|
|
|
|
for node in first.siblings(Direction::Next) {
|
|
|
|
if let Some(ws) = ast::Whitespace::cast(node) {
|
|
|
|
// There is a blank line, which means that the group ends here
|
|
|
|
if ws.count_newlines_lazy().take(2).count() == 2 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore whitespace without blank lines
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop if we find a node that doesn't belong to the group
|
|
|
|
if node.kind() != first.kind() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
visited.insert(node);
|
|
|
|
last = node;
|
|
|
|
}
|
|
|
|
|
|
|
|
if first != last {
|
|
|
|
Some(TextRange::from_to(
|
|
|
|
first.range().start(),
|
|
|
|
last.range().end(),
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
// The group consists of only one element, therefore it cannot be folded
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-12 12:49:08 -05:00
|
|
|
fn contiguous_range_for_comment<'a>(
|
2018-10-12 12:20:58 -05:00
|
|
|
first: SyntaxNodeRef<'a>,
|
2018-10-11 13:07:44 -05:00
|
|
|
visited: &mut FxHashSet<SyntaxNodeRef<'a>>,
|
2018-09-24 08:52:33 -05:00
|
|
|
) -> Option<TextRange> {
|
2018-10-12 12:20:58 -05:00
|
|
|
visited.insert(first);
|
|
|
|
|
2018-10-12 12:49:08 -05:00
|
|
|
// Only fold comments of the same flavor
|
|
|
|
let group_flavor = ast::Comment::cast(first)?.flavor();
|
2018-09-24 08:52:33 -05:00
|
|
|
|
2018-10-12 12:49:08 -05:00
|
|
|
let mut last = first;
|
2018-10-12 12:20:58 -05:00
|
|
|
for node in first.siblings(Direction::Next) {
|
|
|
|
if let Some(ws) = ast::Whitespace::cast(node) {
|
|
|
|
// There is a blank line, which means the group ends here
|
|
|
|
if ws.count_newlines_lazy().take(2).count() == 2 {
|
|
|
|
break;
|
2018-09-24 08:52:33 -05:00
|
|
|
}
|
2018-10-12 12:20:58 -05:00
|
|
|
|
|
|
|
// Ignore whitespace without blank lines
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-10-12 12:49:08 -05:00
|
|
|
match ast::Comment::cast(node) {
|
|
|
|
Some(next_comment) if next_comment.flavor() == group_flavor => {
|
|
|
|
visited.insert(node);
|
|
|
|
last = node;
|
|
|
|
}
|
|
|
|
// The comment group ends because either:
|
|
|
|
// * An element of a different kind was reached
|
|
|
|
// * A comment of a different flavor was reached
|
2018-10-15 16:44:23 -05:00
|
|
|
_ => break,
|
2018-09-24 08:52:33 -05:00
|
|
|
}
|
|
|
|
}
|
2018-10-12 12:20:58 -05:00
|
|
|
|
|
|
|
if first != last {
|
2018-09-24 08:52:33 -05:00
|
|
|
Some(TextRange::from_to(
|
2018-10-12 12:20:58 -05:00
|
|
|
first.range().start(),
|
|
|
|
last.range().end(),
|
2018-09-24 08:52:33 -05:00
|
|
|
))
|
|
|
|
} else {
|
2018-10-12 12:20:58 -05:00
|
|
|
// The group consists of only one element, therefore it cannot be folded
|
2018-09-24 08:52:33 -05:00
|
|
|
None
|
|
|
|
}
|
2018-09-24 09:48:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2018-10-13 14:33:15 -05:00
|
|
|
use test_utils::extract_ranges;
|
|
|
|
|
|
|
|
fn do_check(text: &str, fold_kinds: &[FoldKind]) {
|
|
|
|
let (ranges, text) = extract_ranges(text);
|
2018-11-07 09:32:33 -06:00
|
|
|
let file = SourceFileNode::parse(&text);
|
2018-10-13 14:33:15 -05:00
|
|
|
let folds = folding_ranges(&file);
|
|
|
|
|
2018-10-31 15:41:43 -05:00
|
|
|
assert_eq!(
|
|
|
|
folds.len(),
|
|
|
|
ranges.len(),
|
|
|
|
"The amount of folds is different than the expected amount"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
folds.len(),
|
|
|
|
fold_kinds.len(),
|
|
|
|
"The amount of fold kinds is different than the expected amount"
|
|
|
|
);
|
2018-10-15 16:44:23 -05:00
|
|
|
for ((fold, range), fold_kind) in folds
|
|
|
|
.into_iter()
|
|
|
|
.zip(ranges.into_iter())
|
|
|
|
.zip(fold_kinds.into_iter())
|
|
|
|
{
|
2018-10-13 14:33:15 -05:00
|
|
|
assert_eq!(fold.range.start(), range.start());
|
|
|
|
assert_eq!(fold.range.end(), range.end());
|
|
|
|
assert_eq!(&fold.kind, fold_kind);
|
|
|
|
}
|
|
|
|
}
|
2018-09-24 09:48:13 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fold_comments() {
|
|
|
|
let text = r#"
|
2018-10-13 14:33:15 -05:00
|
|
|
<|>// Hello
|
2018-09-24 09:48:13 -05:00
|
|
|
// this is a multiline
|
|
|
|
// comment
|
2018-10-13 14:33:15 -05:00
|
|
|
//<|>
|
2018-09-24 09:48:13 -05:00
|
|
|
|
|
|
|
// But this is not
|
|
|
|
|
|
|
|
fn main() {
|
2018-10-13 14:33:15 -05:00
|
|
|
<|>// We should
|
2018-09-24 09:48:13 -05:00
|
|
|
// also
|
|
|
|
// fold
|
2018-10-13 14:33:15 -05:00
|
|
|
// this one.<|>
|
|
|
|
<|>//! But this one is different
|
|
|
|
//! because it has another flavor<|>
|
|
|
|
<|>/* As does this
|
|
|
|
multiline comment */<|>
|
2018-09-24 09:48:13 -05:00
|
|
|
}"#;
|
|
|
|
|
2018-10-13 14:33:15 -05:00
|
|
|
let fold_kinds = &[
|
|
|
|
FoldKind::Comment,
|
|
|
|
FoldKind::Comment,
|
|
|
|
FoldKind::Comment,
|
|
|
|
FoldKind::Comment,
|
|
|
|
];
|
|
|
|
do_check(text, fold_kinds);
|
2018-09-24 09:48:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fold_imports() {
|
|
|
|
let text = r#"
|
2018-10-13 14:33:15 -05:00
|
|
|
<|>use std::{
|
2018-10-12 12:49:08 -05:00
|
|
|
str,
|
|
|
|
vec,
|
|
|
|
io as iop
|
2018-10-13 14:33:15 -05:00
|
|
|
};<|>
|
2018-09-24 09:48:13 -05:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
}"#;
|
|
|
|
|
2018-10-13 14:33:15 -05:00
|
|
|
let folds = &[FoldKind::Imports];
|
|
|
|
do_check(text, folds);
|
2018-09-24 09:48:13 -05:00
|
|
|
}
|
|
|
|
|
2018-10-23 07:58:02 -05:00
|
|
|
#[test]
|
|
|
|
fn test_fold_import_groups() {
|
|
|
|
let text = r#"
|
|
|
|
<|>use std::str;
|
|
|
|
use std::vec;
|
|
|
|
use std::io as iop;<|>
|
|
|
|
|
|
|
|
<|>use std::mem;
|
|
|
|
use std::f64;<|>
|
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
// Some random comment
|
|
|
|
use std::collections::VecDeque;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
}"#;
|
|
|
|
|
|
|
|
let folds = &[FoldKind::Imports, FoldKind::Imports];
|
|
|
|
do_check(text, folds);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fold_import_and_groups() {
|
|
|
|
let text = r#"
|
|
|
|
<|>use std::str;
|
|
|
|
use std::vec;
|
|
|
|
use std::io as iop;<|>
|
|
|
|
|
|
|
|
<|>use std::mem;
|
|
|
|
use std::f64;<|>
|
|
|
|
|
|
|
|
<|>use std::collections::{
|
|
|
|
HashMap,
|
|
|
|
VecDeque,
|
|
|
|
};<|>
|
|
|
|
// Some random comment
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
}"#;
|
|
|
|
|
|
|
|
let folds = &[FoldKind::Imports, FoldKind::Imports, FoldKind::Imports];
|
|
|
|
do_check(text, folds);
|
|
|
|
}
|
|
|
|
|
2018-10-02 10:02:57 -05:00
|
|
|
}
|