2019-09-30 03:58:53 -05:00
|
|
|
//! FIXME: write short doc here
|
|
|
|
|
2018-10-11 13:07:44 -05:00
|
|
|
use rustc_hash::FxHashSet;
|
2018-09-24 08:52:33 -05:00
|
|
|
|
|
|
|
use ra_syntax::{
|
2019-04-02 02:23:18 -05:00
|
|
|
ast::{self, AstNode, AstToken, VisibilityOwner},
|
2019-07-20 12:04:34 -05:00
|
|
|
Direction, NodeOrToken, SourceFile,
|
2019-07-04 15:05:17 -05:00
|
|
|
SyntaxKind::{self, *},
|
|
|
|
SyntaxNode, 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,
|
2019-01-25 16:37:45 -06:00
|
|
|
Mods,
|
2018-12-20 13:13:16 -06:00
|
|
|
Block,
|
2018-09-24 08:52:33 -05:00
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2019-03-22 05:29:58 -05:00
|
|
|
pub(crate) fn folding_ranges(file: &SourceFile) -> 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();
|
2019-01-25 16:37:45 -06:00
|
|
|
let mut visited_mods = FxHashSet::default();
|
2018-09-24 08:52:33 -05:00
|
|
|
|
2019-03-30 05:25:53 -05:00
|
|
|
for element in file.syntax().descendants_with_tokens() {
|
2018-10-12 12:20:58 -05:00
|
|
|
// Fold items that span multiple lines
|
2019-03-30 05:25:53 -05:00
|
|
|
if let Some(kind) = fold_kind(element.kind()) {
|
2019-07-19 04:56:47 -05:00
|
|
|
let is_multiline = match &element {
|
2019-07-20 12:04:34 -05:00
|
|
|
NodeOrToken::Node(node) => node.text().contains_char('\n'),
|
|
|
|
NodeOrToken::Token(token) => token.text().contains('\n'),
|
2019-03-30 05:25:53 -05:00
|
|
|
};
|
|
|
|
if is_multiline {
|
2019-07-20 04:58:27 -05:00
|
|
|
res.push(Fold { range: element.text_range(), kind });
|
2019-03-30 05:25:53 -05:00
|
|
|
continue;
|
2018-10-12 12:20:58 -05:00
|
|
|
}
|
2018-09-24 08:52:33 -05:00
|
|
|
}
|
|
|
|
|
2019-03-30 05:25:53 -05:00
|
|
|
match element {
|
2019-07-20 12:04:34 -05:00
|
|
|
NodeOrToken::Token(token) => {
|
2019-03-30 05:25:53 -05:00
|
|
|
// Fold groups of comments
|
|
|
|
if let Some(comment) = ast::Comment::cast(token) {
|
|
|
|
if !visited_comments.contains(&comment) {
|
|
|
|
if let Some(range) =
|
|
|
|
contiguous_range_for_comment(comment, &mut visited_comments)
|
|
|
|
{
|
|
|
|
res.push(Fold { range, kind: FoldKind::Comment })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-16 10:51:58 -05:00
|
|
|
}
|
2019-07-20 12:04:34 -05:00
|
|
|
NodeOrToken::Node(node) => {
|
2019-03-30 05:25:53 -05:00
|
|
|
// Fold groups of imports
|
|
|
|
if node.kind() == USE_ITEM && !visited_imports.contains(&node) {
|
2019-07-19 04:56:47 -05:00
|
|
|
if let Some(range) = contiguous_range_for_group(&node, &mut visited_imports) {
|
2019-03-30 05:25:53 -05:00
|
|
|
res.push(Fold { range, kind: FoldKind::Imports })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fold groups of mods
|
|
|
|
if node.kind() == MODULE && !has_visibility(&node) && !visited_mods.contains(&node)
|
|
|
|
{
|
|
|
|
if let Some(range) =
|
2019-07-19 04:56:47 -05:00
|
|
|
contiguous_range_for_group_unless(&node, has_visibility, &mut visited_mods)
|
2019-03-30 05:25:53 -05:00
|
|
|
{
|
|
|
|
res.push(Fold { range, kind: FoldKind::Mods })
|
|
|
|
}
|
|
|
|
}
|
2019-01-25 16:37:45 -06: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),
|
2019-08-23 07:55:21 -05:00
|
|
|
RECORD_FIELD_DEF_LIST
|
|
|
|
| RECORD_FIELD_PAT_LIST
|
|
|
|
| ITEM_LIST
|
|
|
|
| EXTERN_ITEM_LIST
|
|
|
|
| USE_TREE_LIST
|
|
|
|
| BLOCK
|
2019-09-19 10:22:08 -05:00
|
|
|
| MATCH_ARM_LIST
|
2019-08-23 07:55:21 -05:00
|
|
|
| ENUM_VARIANT_LIST
|
|
|
|
| TOKEN_TREE => Some(FoldKind::Block),
|
2018-10-15 16:44:23 -05:00
|
|
|
_ => None,
|
2018-10-12 12:20:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-25 16:37:45 -06:00
|
|
|
fn has_visibility(node: &SyntaxNode) -> bool {
|
2019-07-19 04:56:47 -05:00
|
|
|
ast::Module::cast(node.clone()).and_then(|m| m.visibility()).is_some()
|
2018-10-12 12:20:58 -05:00
|
|
|
}
|
|
|
|
|
2019-07-19 04:56:47 -05:00
|
|
|
fn contiguous_range_for_group(
|
|
|
|
first: &SyntaxNode,
|
|
|
|
visited: &mut FxHashSet<SyntaxNode>,
|
2019-01-25 16:37:45 -06:00
|
|
|
) -> Option<TextRange> {
|
|
|
|
contiguous_range_for_group_unless(first, |_| false, visited)
|
|
|
|
}
|
|
|
|
|
2019-07-19 04:56:47 -05:00
|
|
|
fn contiguous_range_for_group_unless(
|
|
|
|
first: &SyntaxNode,
|
|
|
|
unless: impl Fn(&SyntaxNode) -> bool,
|
|
|
|
visited: &mut FxHashSet<SyntaxNode>,
|
2018-10-23 07:58:02 -05:00
|
|
|
) -> Option<TextRange> {
|
2019-07-19 04:56:47 -05:00
|
|
|
visited.insert(first.clone());
|
2018-10-23 07:58:02 -05:00
|
|
|
|
2019-07-19 04:56:47 -05:00
|
|
|
let mut last = first.clone();
|
2019-03-30 05:25:53 -05:00
|
|
|
for element in first.siblings_with_tokens(Direction::Next) {
|
|
|
|
let node = match element {
|
2019-07-20 12:04:34 -05:00
|
|
|
NodeOrToken::Token(token) => {
|
2019-03-30 05:25:53 -05:00
|
|
|
if let Some(ws) = ast::Whitespace::cast(token) {
|
|
|
|
if !ws.spans_multiple_lines() {
|
|
|
|
// Ignore whitespace without blank lines
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// There is a blank line or another token, which means that the
|
|
|
|
// group ends here
|
2018-10-23 07:58:02 -05:00
|
|
|
break;
|
|
|
|
}
|
2019-07-20 12:04:34 -05:00
|
|
|
NodeOrToken::Node(node) => node,
|
2019-03-30 05:25:53 -05:00
|
|
|
};
|
2018-10-23 07:58:02 -05:00
|
|
|
|
|
|
|
// Stop if we find a node that doesn't belong to the group
|
2019-07-19 04:56:47 -05:00
|
|
|
if node.kind() != first.kind() || unless(&node) {
|
2018-10-23 07:58:02 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-07-19 04:56:47 -05:00
|
|
|
visited.insert(node.clone());
|
2018-10-23 07:58:02 -05:00
|
|
|
last = node;
|
|
|
|
}
|
|
|
|
|
2019-07-19 04:56:47 -05:00
|
|
|
if first != &last {
|
2019-07-20 04:58:27 -05:00
|
|
|
Some(TextRange::from_to(first.text_range().start(), last.text_range().end()))
|
2018-10-23 07:58:02 -05:00
|
|
|
} else {
|
|
|
|
// The group consists of only one element, therefore it cannot be folded
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-19 04:56:47 -05:00
|
|
|
fn contiguous_range_for_comment(
|
|
|
|
first: ast::Comment,
|
|
|
|
visited: &mut FxHashSet<ast::Comment>,
|
2018-09-24 08:52:33 -05:00
|
|
|
) -> Option<TextRange> {
|
2019-07-19 04:56:47 -05:00
|
|
|
visited.insert(first.clone());
|
2018-10-12 12:20:58 -05:00
|
|
|
|
2018-10-12 12:49:08 -05:00
|
|
|
// Only fold comments of the same flavor
|
2019-04-02 04:18:52 -05:00
|
|
|
let group_kind = first.kind();
|
|
|
|
if !group_kind.shape.is_line() {
|
|
|
|
return None;
|
|
|
|
}
|
2018-09-24 08:52:33 -05:00
|
|
|
|
2019-07-19 04:56:47 -05:00
|
|
|
let mut last = first.clone();
|
2019-03-30 05:25:53 -05:00
|
|
|
for element in first.syntax().siblings_with_tokens(Direction::Next) {
|
|
|
|
match element {
|
2019-07-20 12:04:34 -05:00
|
|
|
NodeOrToken::Token(token) => {
|
2019-07-19 04:56:47 -05:00
|
|
|
if let Some(ws) = ast::Whitespace::cast(token.clone()) {
|
2019-03-30 05:25:53 -05:00
|
|
|
if !ws.spans_multiple_lines() {
|
|
|
|
// Ignore whitespace without blank lines
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2019-04-02 02:23:18 -05:00
|
|
|
if let Some(c) = ast::Comment::cast(token) {
|
2019-04-02 04:18:52 -05:00
|
|
|
if c.kind() == group_kind {
|
2019-07-19 04:56:47 -05:00
|
|
|
visited.insert(c.clone());
|
2019-03-30 05:25:53 -05:00
|
|
|
last = c;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// The comment group ends because either:
|
|
|
|
// * An element of a different kind was reached
|
|
|
|
// * A comment of a different flavor was reached
|
2018-10-12 12:20:58 -05:00
|
|
|
break;
|
2018-09-24 08:52:33 -05:00
|
|
|
}
|
2019-07-20 12:04:34 -05:00
|
|
|
NodeOrToken::Node(_) => break,
|
2019-03-30 05:25:53 -05:00
|
|
|
};
|
2018-09-24 08:52:33 -05:00
|
|
|
}
|
2018-10-12 12:20:58 -05:00
|
|
|
|
|
|
|
if first != last {
|
2019-07-20 04:58:27 -05:00
|
|
|
Some(TextRange::from_to(
|
|
|
|
first.syntax().text_range().start(),
|
|
|
|
last.syntax().text_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]) {
|
2018-12-20 13:30:30 -06:00
|
|
|
let (ranges, text) = extract_ranges(text, "fold");
|
2019-07-12 11:41:13 -05:00
|
|
|
let parse = SourceFile::parse(&text);
|
2019-07-19 04:56:47 -05:00
|
|
|
let folds = folding_ranges(&parse.tree());
|
2018-10-13 14:33:15 -05:00
|
|
|
|
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"
|
|
|
|
);
|
2019-02-08 05:49:43 -06:00
|
|
|
for ((fold, range), fold_kind) in
|
2019-06-04 01:28:50 -05:00
|
|
|
folds.iter().zip(ranges.into_iter()).zip(fold_kinds.iter())
|
2018-10-15 16:44:23 -05:00
|
|
|
{
|
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-12-20 13:30:30 -06:00
|
|
|
<fold>// Hello
|
2018-09-24 09:48:13 -05:00
|
|
|
// this is a multiline
|
|
|
|
// comment
|
2018-12-20 13:30:30 -06:00
|
|
|
//</fold>
|
2018-09-24 09:48:13 -05:00
|
|
|
|
|
|
|
// But this is not
|
|
|
|
|
2018-12-20 13:13:16 -06:00
|
|
|
fn main() <fold>{
|
2018-12-20 13:30:30 -06:00
|
|
|
<fold>// We should
|
2018-09-24 09:48:13 -05:00
|
|
|
// also
|
|
|
|
// fold
|
2018-12-20 13:30:30 -06:00
|
|
|
// 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 13:13:16 -06:00
|
|
|
}</fold>"#;
|
2018-09-24 09:48:13 -05:00
|
|
|
|
2018-10-13 14:33:15 -05:00
|
|
|
let fold_kinds = &[
|
|
|
|
FoldKind::Comment,
|
2018-12-20 13:13:16 -06:00
|
|
|
FoldKind::Block,
|
2018-10-13 14:33:15 -05:00
|
|
|
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-12-20 13:13:16 -06:00
|
|
|
<fold>use std::<fold>{
|
2018-10-12 12:49:08 -05:00
|
|
|
str,
|
|
|
|
vec,
|
|
|
|
io as iop
|
2018-12-20 13:13:16 -06:00
|
|
|
}</fold>;</fold>
|
2018-09-24 09:48:13 -05:00
|
|
|
|
2018-12-20 13:13:16 -06:00
|
|
|
fn main() <fold>{
|
|
|
|
}</fold>"#;
|
2018-09-24 09:48:13 -05:00
|
|
|
|
2018-12-20 13:13:16 -06:00
|
|
|
let folds = &[FoldKind::Imports, FoldKind::Block, FoldKind::Block];
|
2018-10-13 14:33:15 -05:00
|
|
|
do_check(text, folds);
|
2018-09-24 09:48:13 -05:00
|
|
|
}
|
|
|
|
|
2019-01-25 16:37:45 -06: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 05:49:43 -06:00
|
|
|
let folds = &[FoldKind::Mods, FoldKind::Mods, FoldKind::Mods, FoldKind::Block];
|
2019-01-25 16:37:45 -06:00
|
|
|
do_check(text, folds);
|
|
|
|
}
|
|
|
|
|
2018-10-23 07:58:02 -05:00
|
|
|
#[test]
|
|
|
|
fn test_fold_import_groups() {
|
|
|
|
let text = r#"
|
2018-12-20 13:30:30 -06:00
|
|
|
<fold>use std::str;
|
2018-10-23 07:58:02 -05:00
|
|
|
use std::vec;
|
2018-12-20 13:30:30 -06:00
|
|
|
use std::io as iop;</fold>
|
2018-10-23 07:58:02 -05:00
|
|
|
|
2018-12-20 13:30:30 -06:00
|
|
|
<fold>use std::mem;
|
|
|
|
use std::f64;</fold>
|
2018-10-23 07:58:02 -05:00
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
// Some random comment
|
|
|
|
use std::collections::VecDeque;
|
|
|
|
|
2018-12-20 13:13:16 -06:00
|
|
|
fn main() <fold>{
|
|
|
|
}</fold>"#;
|
2018-10-23 07:58:02 -05:00
|
|
|
|
2018-12-20 13:13:16 -06:00
|
|
|
let folds = &[FoldKind::Imports, FoldKind::Imports, FoldKind::Block];
|
2018-10-23 07:58:02 -05:00
|
|
|
do_check(text, folds);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fold_import_and_groups() {
|
|
|
|
let text = r#"
|
2018-12-20 13:30:30 -06:00
|
|
|
<fold>use std::str;
|
2018-10-23 07:58:02 -05:00
|
|
|
use std::vec;
|
2018-12-20 13:30:30 -06:00
|
|
|
use std::io as iop;</fold>
|
2018-10-23 07:58:02 -05:00
|
|
|
|
2018-12-20 13:30:30 -06:00
|
|
|
<fold>use std::mem;
|
|
|
|
use std::f64;</fold>
|
2018-10-23 07:58:02 -05:00
|
|
|
|
2018-12-20 13:13:16 -06:00
|
|
|
<fold>use std::collections::<fold>{
|
2018-10-23 07:58:02 -05:00
|
|
|
HashMap,
|
|
|
|
VecDeque,
|
2018-12-20 13:13:16 -06:00
|
|
|
}</fold>;</fold>
|
2018-10-23 07:58:02 -05:00
|
|
|
// Some random comment
|
|
|
|
|
2018-12-20 13:13:16 -06:00
|
|
|
fn main() <fold>{
|
|
|
|
}</fold>"#;
|
2018-10-23 07:58:02 -05:00
|
|
|
|
2018-12-20 13:13:16 -06:00
|
|
|
let folds = &[
|
|
|
|
FoldKind::Imports,
|
|
|
|
FoldKind::Imports,
|
|
|
|
FoldKind::Imports,
|
|
|
|
FoldKind::Block,
|
|
|
|
FoldKind::Block,
|
|
|
|
];
|
2018-10-23 07:58:02 -05:00
|
|
|
do_check(text, folds);
|
|
|
|
}
|
|
|
|
|
2019-01-22 07:26:32 -06: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);
|
|
|
|
}
|
2019-09-19 10:22:08 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fold_match_arms() {
|
|
|
|
let text = r#"
|
|
|
|
fn main() <fold>{
|
|
|
|
match 0 <fold>{
|
|
|
|
0 => 0,
|
|
|
|
_ => 1,
|
|
|
|
}</fold>
|
|
|
|
}</fold>"#;
|
|
|
|
|
2019-09-19 10:37:41 -05:00
|
|
|
let folds = &[FoldKind::Block, FoldKind::Block];
|
2019-09-19 10:22:08 -05:00
|
|
|
do_check(text, folds);
|
|
|
|
}
|
2018-10-02 10:02:57 -05:00
|
|
|
}
|