2018-10-11 13:07:44 -05:00
|
|
|
use rustc_hash::FxHashSet;
|
2018-09-24 08:52:33 -05:00
|
|
|
|
|
|
|
use ra_syntax::{
|
2018-10-12 01:59:12 -05:00
|
|
|
ast,
|
|
|
|
AstNode,
|
2018-09-24 08:52:33 -05:00
|
|
|
File, TextRange, SyntaxNodeRef,
|
2018-10-12 12:49:08 -05:00
|
|
|
SyntaxKind::{self, *},
|
2018-10-02 10:14:33 -05:00
|
|
|
Direction,
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn folding_ranges(file: &File) -> Vec<Fold> {
|
|
|
|
let mut res = vec![];
|
2018-10-12 12:49:08 -05:00
|
|
|
let mut visited_comments = 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) {
|
|
|
|
res.push(Fold { range: node.range(), kind });
|
|
|
|
}
|
2018-09-24 08:52:33 -05:00
|
|
|
}
|
|
|
|
|
2018-10-12 12:49:08 -05:00
|
|
|
// Also fold groups of comments
|
|
|
|
if visited_comments.contains(&node) {
|
2018-10-12 12:20:58 -05:00
|
|
|
continue;
|
2018-09-24 08:52:33 -05:00
|
|
|
}
|
2018-10-12 12:49:08 -05:00
|
|
|
if node.kind() == COMMENT {
|
|
|
|
contiguous_range_for_comment(node, &mut visited_comments)
|
|
|
|
.map(|range| res.push(Fold { range, kind: FoldKind::Comment }));
|
2018-10-12 12:20:58 -05:00
|
|
|
}
|
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-12 12:20:58 -05:00
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn has_newline(
|
|
|
|
node: SyntaxNodeRef,
|
|
|
|
) -> bool {
|
|
|
|
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-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
|
|
|
|
_ => {
|
|
|
|
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::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fold_comments() {
|
|
|
|
let text = r#"
|
|
|
|
// Hello
|
|
|
|
// this is a multiline
|
|
|
|
// comment
|
|
|
|
//
|
|
|
|
|
|
|
|
// But this is not
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
// We should
|
|
|
|
// also
|
|
|
|
// fold
|
|
|
|
// this one.
|
|
|
|
}"#;
|
|
|
|
|
|
|
|
let file = File::parse(&text);
|
|
|
|
let folds = folding_ranges(&file);
|
|
|
|
assert_eq!(folds.len(), 2);
|
|
|
|
assert_eq!(folds[0].range.start(), 1.into());
|
|
|
|
assert_eq!(folds[0].range.end(), 46.into());
|
|
|
|
assert_eq!(folds[0].kind, FoldKind::Comment);
|
|
|
|
|
|
|
|
assert_eq!(folds[1].range.start(), 84.into());
|
|
|
|
assert_eq!(folds[1].range.end(), 137.into());
|
|
|
|
assert_eq!(folds[1].kind, FoldKind::Comment);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fold_imports() {
|
|
|
|
let text = r#"
|
2018-10-12 12:49:08 -05:00
|
|
|
use std::{
|
|
|
|
str,
|
|
|
|
vec,
|
|
|
|
io as iop
|
|
|
|
};
|
2018-09-24 09:48:13 -05:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
}"#;
|
|
|
|
|
|
|
|
let file = File::parse(&text);
|
|
|
|
let folds = folding_ranges(&file);
|
|
|
|
assert_eq!(folds.len(), 1);
|
|
|
|
assert_eq!(folds[0].range.start(), 1.into());
|
2018-10-12 12:49:08 -05:00
|
|
|
assert_eq!(folds[0].range.end(), 46.into());
|
2018-09-24 09:48:13 -05:00
|
|
|
assert_eq!(folds[0].kind, FoldKind::Imports);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-02 10:02:57 -05:00
|
|
|
}
|