rust/crates/ra_ide_api/src/folding_ranges.rs

327 lines
8.1 KiB
Rust
Raw Normal View History

use rustc_hash::FxHashSet;
use ra_syntax::{
2019-01-26 16:26:30 +03:00
AstNode, Direction, SourceFile, SyntaxNode, TextRange,
2018-10-12 19:49:08 +02:00
SyntaxKind::{self, *},
2019-01-26 16:26:30 +03:00
ast::{self, VisibilityOwner},
};
2018-09-24 10:48:13 -04:00
#[derive(Debug, PartialEq, Eq)]
pub enum FoldKind {
Comment,
Imports,
2019-01-25 23:37:45 +01:00
Mods,
2018-12-20 22:13:16 +03:00
Block,
}
2018-09-24 10:48:13 -04:00
#[derive(Debug)]
pub struct Fold {
pub range: TextRange,
pub kind: FoldKind,
}
2019-03-22 13:29:58 +03:00
pub(crate) fn folding_ranges(file: &SourceFile) -> Vec<Fold> {
let mut res = vec![];
2018-10-12 19:49:08 +02:00
let mut visited_comments = FxHashSet::default();
2018-10-23 14:58:02 +02:00
let mut visited_imports = FxHashSet::default();
2019-01-25 23:37:45 +01:00
let mut visited_mods = FxHashSet::default();
for node in file.syntax().descendants() {
// Fold items that span multiple lines
if let Some(kind) = fold_kind(node.kind()) {
2019-01-26 16:26:30 +03:00
if node.text().contains('\n') {
2019-02-08 14:49:43 +03:00
res.push(Fold { range: node.range(), kind });
}
}
2018-10-23 14:58:02 +02:00
// Fold groups of comments
if node.kind() == COMMENT && !visited_comments.contains(&node) {
if let Some(range) = contiguous_range_for_comment(node, &mut visited_comments) {
2019-02-08 14:49:43 +03:00
res.push(Fold { range, kind: FoldKind::Comment })
}
}
2018-10-23 14:58:02 +02: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) {
2019-02-08 14:49:43 +03:00
res.push(Fold { range, kind: FoldKind::Imports })
2018-10-23 14:58:02 +02:00
}
}
2019-01-25 23:37:45 +01:00
// Fold groups of mods
if node.kind() == MODULE && !has_visibility(&node) && !visited_mods.contains(&node) {
if let Some(range) =
contiguous_range_for_group_unless(node, has_visibility, &mut visited_mods)
{
2019-02-08 14:49:43 +03:00
res.push(Fold { range, kind: FoldKind::Mods })
2019-01-25 23:37:45 +01:00
}
}
}
res
}
fn fold_kind(kind: SyntaxKind) -> Option<FoldKind> {
match kind {
2018-10-12 19:49:08 +02:00
COMMENT => Some(FoldKind::Comment),
USE_ITEM => Some(FoldKind::Imports),
2018-12-20 22:13:16 +03:00
NAMED_FIELD_DEF_LIST | FIELD_PAT_LIST | ITEM_LIST | EXTERN_ITEM_LIST | USE_TREE_LIST
2019-01-22 16:26:32 +03:00
| BLOCK | ENUM_VARIANT_LIST | TOKEN_TREE => Some(FoldKind::Block),
_ => None,
}
}
2019-01-25 23:37:45 +01:00
fn has_visibility(node: &SyntaxNode) -> bool {
2019-02-08 14:49:43 +03:00
ast::Module::cast(node).and_then(|m| m.visibility()).is_some()
}
2018-10-23 14:58:02 +02:00
fn contiguous_range_for_group<'a>(
2019-01-07 16:53:24 +03:00
first: &'a SyntaxNode,
visited: &mut FxHashSet<&'a SyntaxNode>,
2019-01-25 23:37:45 +01:00
) -> Option<TextRange> {
contiguous_range_for_group_unless(first, |_| false, visited)
}
fn contiguous_range_for_group_unless<'a>(
first: &'a SyntaxNode,
unless: impl Fn(&'a SyntaxNode) -> bool,
visited: &mut FxHashSet<&'a SyntaxNode>,
2018-10-23 14:58:02 +02:00
) -> 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
2019-01-25 23:37:45 +01:00
if node.kind() != first.kind() || unless(node) {
2018-10-23 14:58:02 +02:00
break;
}
visited.insert(node);
last = node;
}
if first != last {
2019-02-08 14:49:43 +03:00
Some(TextRange::from_to(first.range().start(), last.range().end()))
2018-10-23 14:58:02 +02:00
} else {
// The group consists of only one element, therefore it cannot be folded
None
}
}
2018-10-12 19:49:08 +02:00
fn contiguous_range_for_comment<'a>(
2019-01-07 16:53:24 +03:00
first: &'a SyntaxNode,
visited: &mut FxHashSet<&'a SyntaxNode>,
) -> Option<TextRange> {
visited.insert(first);
2018-10-12 19:49:08 +02:00
// Only fold comments of the same flavor
let group_flavor = ast::Comment::cast(first)?.flavor();
2018-10-12 19:49:08 +02:00
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 the group ends here
if ws.count_newlines_lazy().take(2).count() == 2 {
break;
}
// Ignore whitespace without blank lines
continue;
}
2018-10-12 19:49:08 +02: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,
}
}
if first != last {
2019-02-08 14:49:43 +03:00
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-09-24 10:48:13 -04:00
}
#[cfg(test)]
mod tests {
use super::*;
2018-10-13 21:33:15 +02:00
use test_utils::extract_ranges;
fn do_check(text: &str, fold_kinds: &[FoldKind]) {
let (ranges, text) = extract_ranges(text, "fold");
2019-01-07 16:53:24 +03:00
let file = SourceFile::parse(&text);
2018-10-13 21:33:15 +02:00
let folds = folding_ranges(&file);
2018-10-31 23:41:43 +03: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"
);
2019-02-08 14:49:43 +03:00
for ((fold, range), fold_kind) in
folds.into_iter().zip(ranges.into_iter()).zip(fold_kinds.into_iter())
{
2018-10-13 21:33:15 +02:00
assert_eq!(fold.range.start(), range.start());
assert_eq!(fold.range.end(), range.end());
assert_eq!(&fold.kind, fold_kind);
}
}
2018-09-24 10:48:13 -04:00
#[test]
fn test_fold_comments() {
let text = r#"
<fold>// Hello
2018-09-24 10:48:13 -04:00
// this is a multiline
// comment
//</fold>
2018-09-24 10:48:13 -04:00
// But this is not
2018-12-20 22:13:16 +03:00
fn main() <fold>{
<fold>// We should
2018-09-24 10:48:13 -04:00
// also
// fold
// this one.</fold>
<fold>//! But this one is different
//! because it has another flavor</fold>
<fold>/* As does this
multiline comment */</fold>
2018-12-20 22:13:16 +03:00
}</fold>"#;
2018-09-24 10:48:13 -04:00
2018-10-13 21:33:15 +02:00
let fold_kinds = &[
FoldKind::Comment,
2018-12-20 22:13:16 +03:00
FoldKind::Block,
2018-10-13 21:33:15 +02:00
FoldKind::Comment,
FoldKind::Comment,
FoldKind::Comment,
];
do_check(text, fold_kinds);
2018-09-24 10:48:13 -04:00
}
#[test]
fn test_fold_imports() {
let text = r#"
2018-12-20 22:13:16 +03:00
<fold>use std::<fold>{
2018-10-12 19:49:08 +02:00
str,
vec,
io as iop
2018-12-20 22:13:16 +03:00
}</fold>;</fold>
2018-09-24 10:48:13 -04:00
2018-12-20 22:13:16 +03:00
fn main() <fold>{
}</fold>"#;
2018-09-24 10:48:13 -04:00
2018-12-20 22:13:16 +03:00
let folds = &[FoldKind::Imports, FoldKind::Block, FoldKind::Block];
2018-10-13 21:33:15 +02:00
do_check(text, folds);
2018-09-24 10:48:13 -04:00
}
2019-01-25 23:37:45 +01:00
#[test]
fn test_fold_mods() {
let text = r#"
pub mod foo;
<fold>mod after_pub;
mod after_pub_next;</fold>
<fold>mod before_pub;
mod before_pub_next;</fold>
pub mod bar;
mod not_folding_single;
pub mod foobar;
pub not_folding_single_next;
<fold>#[cfg(test)]
mod with_attribute;
mod with_attribute_next;</fold>
fn main() <fold>{
}</fold>"#;
2019-02-08 14:49:43 +03:00
let folds = &[FoldKind::Mods, FoldKind::Mods, FoldKind::Mods, FoldKind::Block];
2019-01-25 23:37:45 +01:00
do_check(text, folds);
}
2018-10-23 14:58:02 +02:00
#[test]
fn test_fold_import_groups() {
let text = r#"
<fold>use std::str;
2018-10-23 14:58:02 +02:00
use std::vec;
use std::io as iop;</fold>
2018-10-23 14:58:02 +02:00
<fold>use std::mem;
use std::f64;</fold>
2018-10-23 14:58:02 +02:00
use std::collections::HashMap;
// Some random comment
use std::collections::VecDeque;
2018-12-20 22:13:16 +03:00
fn main() <fold>{
}</fold>"#;
2018-10-23 14:58:02 +02:00
2018-12-20 22:13:16 +03:00
let folds = &[FoldKind::Imports, FoldKind::Imports, FoldKind::Block];
2018-10-23 14:58:02 +02:00
do_check(text, folds);
}
#[test]
fn test_fold_import_and_groups() {
let text = r#"
<fold>use std::str;
2018-10-23 14:58:02 +02:00
use std::vec;
use std::io as iop;</fold>
2018-10-23 14:58:02 +02:00
<fold>use std::mem;
use std::f64;</fold>
2018-10-23 14:58:02 +02:00
2018-12-20 22:13:16 +03:00
<fold>use std::collections::<fold>{
2018-10-23 14:58:02 +02:00
HashMap,
VecDeque,
2018-12-20 22:13:16 +03:00
}</fold>;</fold>
2018-10-23 14:58:02 +02:00
// Some random comment
2018-12-20 22:13:16 +03:00
fn main() <fold>{
}</fold>"#;
2018-10-23 14:58:02 +02:00
2018-12-20 22:13:16 +03:00
let folds = &[
FoldKind::Imports,
FoldKind::Imports,
FoldKind::Imports,
FoldKind::Block,
FoldKind::Block,
];
2018-10-23 14:58:02 +02:00
do_check(text, folds);
}
2019-01-22 16:26:32 +03:00
#[test]
fn test_folds_macros() {
let text = r#"
macro_rules! foo <fold>{
($($tt:tt)*) => { $($tt)* }
}</fold>
"#;
let folds = &[FoldKind::Block];
do_check(text, folds);
}
}