2018-10-11 13:07:44 -05:00
|
|
|
use rustc_hash::FxHashSet;
|
2018-09-24 08:52:33 -05:00
|
|
|
|
2020-08-12 11:26:51 -05:00
|
|
|
use syntax::{
|
2021-07-15 10:44:23 -05:00
|
|
|
ast::{self, AstNode, AstToken},
|
|
|
|
match_ast, Direction, NodeOrToken, SourceFile,
|
2019-07-04 15:05:17 -05:00
|
|
|
SyntaxKind::{self, *},
|
2021-07-15 10:44:23 -05:00
|
|
|
TextRange, TextSize,
|
2018-09-24 08:52:33 -05:00
|
|
|
};
|
|
|
|
|
2021-07-15 10:44:23 -05:00
|
|
|
use std::hash::Hash;
|
|
|
|
|
2021-07-15 10:02:45 -05:00
|
|
|
const REGION_START: &str = "// region:";
|
|
|
|
const REGION_END: &str = "// endregion";
|
|
|
|
|
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,
|
2020-07-01 11:17:08 -05:00
|
|
|
ArgList,
|
2021-01-18 12:45:42 -06:00
|
|
|
Region,
|
2021-03-29 05:49:14 -05:00
|
|
|
Consts,
|
|
|
|
Statics,
|
2021-04-07 17:05:08 -05:00
|
|
|
Array,
|
2021-04-30 03:18:36 -05:00
|
|
|
WhereClause,
|
2021-05-28 06:39:02 -05:00
|
|
|
ReturnType,
|
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,
|
|
|
|
}
|
|
|
|
|
2021-05-22 08:53:47 -05:00
|
|
|
// Feature: Folding
|
|
|
|
//
|
2021-07-15 10:02:45 -05:00
|
|
|
// Defines folding regions for curly braced blocks, runs of consecutive use, mod, const or static
|
|
|
|
// items, and `region` / `endregion` comment markers.
|
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();
|
2021-03-29 06:17:19 -05:00
|
|
|
let mut visited_consts = FxHashSet::default();
|
2021-03-29 06:20:26 -05:00
|
|
|
let mut visited_statics = FxHashSet::default();
|
2021-07-15 10:02:45 -05:00
|
|
|
|
2021-01-18 12:45:42 -06:00
|
|
|
// regions can be nested, here is a LIFO buffer
|
2021-07-15 10:02:45 -05:00
|
|
|
let mut region_starts: Vec<TextSize> = vec![];
|
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) {
|
2021-07-15 10:02:45 -05:00
|
|
|
if visited_comments.contains(&comment) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let text = comment.text().trim_start();
|
|
|
|
if text.starts_with(REGION_START) {
|
|
|
|
region_starts.push(comment.syntax().text_range().start());
|
|
|
|
} else if text.starts_with(REGION_END) {
|
|
|
|
if let Some(region) = region_starts.pop() {
|
|
|
|
res.push(Fold {
|
|
|
|
range: TextRange::new(region, comment.syntax().text_range().end()),
|
|
|
|
kind: FoldKind::Region,
|
|
|
|
})
|
2019-03-30 05:25:53 -05:00
|
|
|
}
|
2021-07-15 10:02:45 -05:00
|
|
|
} else if let Some(range) =
|
|
|
|
contiguous_range_for_comment(comment, &mut visited_comments)
|
|
|
|
{
|
|
|
|
res.push(Fold { range, kind: FoldKind::Comment })
|
2019-03-30 05:25:53 -05:00
|
|
|
}
|
|
|
|
}
|
2018-10-16 10:51:58 -05:00
|
|
|
}
|
2019-07-20 12:04:34 -05:00
|
|
|
NodeOrToken::Node(node) => {
|
2021-07-15 10:44:23 -05:00
|
|
|
match_ast! {
|
|
|
|
match node {
|
|
|
|
ast::Module(module) => {
|
|
|
|
if module.item_list().is_none() {
|
|
|
|
if let Some(range) = contiguous_range_for_item_group(
|
|
|
|
module,
|
|
|
|
&mut visited_mods,
|
|
|
|
) {
|
|
|
|
res.push(Fold { range, kind: FoldKind::Mods })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ast::Use(use_) => {
|
|
|
|
if let Some(range) = contiguous_range_for_item_group(use_, &mut visited_imports) {
|
|
|
|
res.push(Fold { range, kind: FoldKind::Imports })
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ast::Const(konst) => {
|
|
|
|
if let Some(range) = contiguous_range_for_item_group(konst, &mut visited_consts) {
|
|
|
|
res.push(Fold { range, kind: FoldKind::Consts })
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ast::Static(statik) => {
|
|
|
|
if let Some(range) = contiguous_range_for_item_group(statik, &mut visited_statics) {
|
|
|
|
res.push(Fold { range, kind: FoldKind::Statics })
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ast::WhereClause(where_clause) => {
|
|
|
|
if let Some(range) = fold_range_for_where_clause(where_clause) {
|
|
|
|
res.push(Fold { range, kind: FoldKind::WhereClause })
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => (),
|
2021-04-30 03:18:36 -05:00
|
|
|
}
|
|
|
|
}
|
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),
|
2020-07-27 06:42:36 -05:00
|
|
|
ARG_LIST | PARAM_LIST => Some(FoldKind::ArgList),
|
2021-04-07 17:05:08 -05:00
|
|
|
ARRAY_EXPR => Some(FoldKind::Array),
|
2021-05-28 06:39:02 -05:00
|
|
|
RET_TYPE => Some(FoldKind::ReturnType),
|
2020-08-03 07:45:39 -05:00
|
|
|
ASSOC_ITEM_LIST
|
|
|
|
| RECORD_FIELD_LIST
|
2020-07-31 12:54:16 -05:00
|
|
|
| RECORD_PAT_FIELD_LIST
|
2020-07-30 09:21:30 -05:00
|
|
|
| RECORD_EXPR_FIELD_LIST
|
2019-08-23 07:55:21 -05:00
|
|
|
| ITEM_LIST
|
|
|
|
| EXTERN_ITEM_LIST
|
|
|
|
| USE_TREE_LIST
|
2020-05-01 18:18:19 -05:00
|
|
|
| BLOCK_EXPR
|
2019-09-19 10:22:08 -05:00
|
|
|
| MATCH_ARM_LIST
|
2020-07-30 10:56:53 -05:00
|
|
|
| VARIANT_LIST
|
2019-08-23 07:55:21 -05:00
|
|
|
| TOKEN_TREE => Some(FoldKind::Block),
|
2018-10-15 16:44:23 -05:00
|
|
|
_ => None,
|
2018-10-12 12:20:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-15 10:44:23 -05:00
|
|
|
fn contiguous_range_for_item_group<N>(first: N, visited: &mut FxHashSet<N>) -> Option<TextRange>
|
|
|
|
where
|
|
|
|
N: ast::VisibilityOwner + Clone + Hash + Eq,
|
|
|
|
{
|
|
|
|
if !visited.insert(first.clone()) {
|
|
|
|
return None;
|
|
|
|
}
|
2018-10-23 07:58:02 -05:00
|
|
|
|
2021-07-15 10:44:23 -05:00
|
|
|
let (mut last, mut last_vis) = (first.clone(), first.visibility());
|
|
|
|
for element in first.syntax().siblings_with_tokens(Direction::Next) {
|
2019-03-30 05:25:53 -05:00
|
|
|
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
|
|
|
|
2021-07-15 10:44:23 -05:00
|
|
|
if let Some(next) = N::cast(node) {
|
|
|
|
let next_vis = next.visibility();
|
|
|
|
if eq_visibility(next_vis.clone(), last_vis) {
|
|
|
|
visited.insert(next.clone());
|
|
|
|
last_vis = next_vis;
|
|
|
|
last = next;
|
|
|
|
continue;
|
|
|
|
}
|
2018-10-23 07:58:02 -05:00
|
|
|
}
|
2021-07-15 10:44:23 -05:00
|
|
|
// Stop if we find an item of a different kind or with a different visibility.
|
|
|
|
break;
|
2018-10-23 07:58:02 -05:00
|
|
|
}
|
|
|
|
|
2021-07-15 10:44:23 -05:00
|
|
|
if first != last {
|
|
|
|
Some(TextRange::new(first.syntax().text_range().start(), last.syntax().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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-15 10:44:23 -05:00
|
|
|
fn eq_visibility(vis0: Option<ast::Visibility>, vis1: Option<ast::Visibility>) -> bool {
|
|
|
|
match (vis0, vis1) {
|
|
|
|
(None, None) => true,
|
|
|
|
(Some(vis0), Some(vis1)) => vis0.is_eq_to(&vis1),
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-07-15 10:44:23 -05:00
|
|
|
let text = c.text().trim_start();
|
2021-02-05 07:32:03 -06:00
|
|
|
// regions are not real comments
|
2021-07-15 10:44:23 -05:00
|
|
|
if !(text.starts_with(REGION_START) || text.starts_with(REGION_END)) {
|
2021-01-18 12:45:42 -06:00
|
|
|
visited.insert(c.clone());
|
|
|
|
last = c;
|
|
|
|
continue;
|
|
|
|
}
|
2019-03-30 05:25:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// 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 {
|
2020-04-24 16:51:02 -05:00
|
|
|
Some(TextRange::new(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
|
|
|
}
|
|
|
|
|
2021-07-15 10:44:23 -05:00
|
|
|
fn fold_range_for_where_clause(where_clause: ast::WhereClause) -> Option<TextRange> {
|
|
|
|
let first_where_pred = where_clause.predicates().next();
|
|
|
|
let last_where_pred = where_clause.predicates().last();
|
2021-04-30 03:18:36 -05:00
|
|
|
|
|
|
|
if first_where_pred != last_where_pred {
|
2021-07-15 10:44:23 -05:00
|
|
|
let start = where_clause.where_token()?.text_range().end();
|
|
|
|
let end = where_clause.syntax().text_range().end();
|
|
|
|
return Some(TextRange::new(start, end));
|
2021-04-30 03:18:36 -05:00
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2018-09-24 09:48:13 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-07-01 11:17:08 -05:00
|
|
|
use test_utils::extract_tags;
|
|
|
|
|
2018-09-24 09:48:13 -05:00
|
|
|
use super::*;
|
2018-10-13 14:33:15 -05:00
|
|
|
|
2020-07-01 11:17:08 -05:00
|
|
|
fn check(ra_fixture: &str) {
|
|
|
|
let (ranges, text) = extract_tags(ra_fixture, "fold");
|
|
|
|
|
2019-07-12 11:41:13 -05:00
|
|
|
let parse = SourceFile::parse(&text);
|
2021-07-15 10:02:45 -05:00
|
|
|
let mut folds = folding_ranges(&parse.tree());
|
|
|
|
folds.sort_by_key(|fold| (fold.range.start(), fold.range.end()));
|
|
|
|
|
2018-10-31 15:41:43 -05:00
|
|
|
assert_eq!(
|
|
|
|
folds.len(),
|
|
|
|
ranges.len(),
|
|
|
|
"The amount of folds is different than the expected amount"
|
|
|
|
);
|
2020-07-01 11:17:08 -05:00
|
|
|
|
|
|
|
for (fold, (range, attr)) in folds.iter().zip(ranges.into_iter()) {
|
2021-07-15 10:02:45 -05:00
|
|
|
assert_eq!(fold.range.start(), range.start(), "mismatched start of folding ranges");
|
|
|
|
assert_eq!(fold.range.end(), range.end(), "mismatched end of folding ranges");
|
2020-07-01 11:17:08 -05:00
|
|
|
|
|
|
|
let kind = match fold.kind {
|
|
|
|
FoldKind::Comment => "comment",
|
|
|
|
FoldKind::Imports => "imports",
|
|
|
|
FoldKind::Mods => "mods",
|
|
|
|
FoldKind::Block => "block",
|
|
|
|
FoldKind::ArgList => "arglist",
|
2021-01-18 12:45:42 -06:00
|
|
|
FoldKind::Region => "region",
|
2021-03-29 05:49:14 -05:00
|
|
|
FoldKind::Consts => "consts",
|
2021-03-29 06:56:02 -05:00
|
|
|
FoldKind::Statics => "statics",
|
2021-04-07 17:05:08 -05:00
|
|
|
FoldKind::Array => "array",
|
2021-04-30 03:18:36 -05:00
|
|
|
FoldKind::WhereClause => "whereclause",
|
2021-05-28 06:39:02 -05:00
|
|
|
FoldKind::ReturnType => "returntype",
|
2020-07-01 11:17:08 -05:00
|
|
|
};
|
|
|
|
assert_eq!(kind, &attr.unwrap());
|
2018-10-13 14:33:15 -05:00
|
|
|
}
|
|
|
|
}
|
2018-09-24 09:48:13 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fold_comments() {
|
2020-07-01 11:17:08 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
<fold comment>// 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
|
|
|
|
|
2020-07-01 11:17:08 -05:00
|
|
|
fn main() <fold block>{
|
|
|
|
<fold comment>// We should
|
2018-09-24 09:48:13 -05:00
|
|
|
// also
|
|
|
|
// fold
|
2018-12-20 13:30:30 -06:00
|
|
|
// this one.</fold>
|
2020-07-01 11:17:08 -05:00
|
|
|
<fold comment>//! But this one is different
|
2018-12-20 13:30:30 -06:00
|
|
|
//! because it has another flavor</fold>
|
2020-07-01 11:17:08 -05:00
|
|
|
<fold comment>/* As does this
|
2018-12-20 13:30:30 -06:00
|
|
|
multiline comment */</fold>
|
2021-07-15 09:35:31 -05:00
|
|
|
}</fold>
|
|
|
|
"#,
|
2020-07-01 11:17:08 -05:00
|
|
|
);
|
2018-09-24 09:48:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fold_imports() {
|
2020-07-01 11:17:08 -05:00
|
|
|
check(
|
|
|
|
r#"
|
2020-12-04 08:51:23 -06:00
|
|
|
use std::<fold block>{
|
2018-10-12 12:49:08 -05:00
|
|
|
str,
|
|
|
|
vec,
|
|
|
|
io as iop
|
2020-12-04 08:51:23 -06:00
|
|
|
}</fold>;
|
2021-07-15 09:35:31 -05:00
|
|
|
"#,
|
2020-07-01 11:17:08 -05:00
|
|
|
);
|
2018-09-24 09:48:13 -05:00
|
|
|
}
|
|
|
|
|
2019-01-25 16:37:45 -06:00
|
|
|
#[test]
|
|
|
|
fn test_fold_mods() {
|
2020-07-01 11:17:08 -05:00
|
|
|
check(
|
|
|
|
r#"
|
2019-01-25 16:37:45 -06:00
|
|
|
|
|
|
|
pub mod foo;
|
2020-07-01 11:17:08 -05:00
|
|
|
<fold mods>mod after_pub;
|
2019-01-25 16:37:45 -06:00
|
|
|
mod after_pub_next;</fold>
|
|
|
|
|
2020-07-01 11:17:08 -05:00
|
|
|
<fold mods>mod before_pub;
|
2019-01-25 16:37:45 -06:00
|
|
|
mod before_pub_next;</fold>
|
|
|
|
pub mod bar;
|
|
|
|
|
|
|
|
mod not_folding_single;
|
|
|
|
pub mod foobar;
|
|
|
|
pub not_folding_single_next;
|
|
|
|
|
2020-07-01 11:17:08 -05:00
|
|
|
<fold mods>#[cfg(test)]
|
2019-01-25 16:37:45 -06:00
|
|
|
mod with_attribute;
|
|
|
|
mod with_attribute_next;</fold>
|
|
|
|
|
2021-07-15 09:35:31 -05:00
|
|
|
mod inline0 {}
|
|
|
|
mod inline1 {}
|
|
|
|
|
|
|
|
mod inline2 <fold block>{
|
|
|
|
|
|
|
|
}</fold>
|
|
|
|
"#,
|
2020-07-01 11:17:08 -05:00
|
|
|
);
|
2019-01-25 16:37:45 -06:00
|
|
|
}
|
|
|
|
|
2018-10-23 07:58:02 -05:00
|
|
|
#[test]
|
|
|
|
fn test_fold_import_groups() {
|
2020-07-01 11:17:08 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
<fold imports>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
|
|
|
|
2020-07-01 11:17:08 -05:00
|
|
|
<fold imports>use std::mem;
|
2018-12-20 13:30:30 -06:00
|
|
|
use std::f64;</fold>
|
2018-10-23 07:58:02 -05:00
|
|
|
|
2020-12-04 08:51:23 -06:00
|
|
|
<fold imports>use std::collections::HashMap;
|
2018-10-23 07:58:02 -05:00
|
|
|
// Some random comment
|
2020-12-04 08:51:23 -06:00
|
|
|
use std::collections::VecDeque;</fold>
|
2021-07-15 09:35:31 -05:00
|
|
|
"#,
|
2020-07-01 11:17:08 -05:00
|
|
|
);
|
2018-10-23 07:58:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fold_import_and_groups() {
|
2020-07-01 11:17:08 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
<fold imports>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
|
|
|
|
2020-07-01 11:17:08 -05:00
|
|
|
<fold imports>use std::mem;
|
2018-12-20 13:30:30 -06:00
|
|
|
use std::f64;</fold>
|
2018-10-23 07:58:02 -05:00
|
|
|
|
2020-12-04 08:51:23 -06:00
|
|
|
use std::collections::<fold block>{
|
2018-10-23 07:58:02 -05:00
|
|
|
HashMap,
|
|
|
|
VecDeque,
|
2020-12-04 08:51:23 -06:00
|
|
|
}</fold>;
|
2018-10-23 07:58:02 -05:00
|
|
|
// Some random comment
|
2021-07-15 09:35:31 -05:00
|
|
|
"#,
|
2020-07-01 11:17:08 -05:00
|
|
|
);
|
2018-10-23 07:58:02 -05:00
|
|
|
}
|
|
|
|
|
2020-08-03 07:45:39 -05:00
|
|
|
#[test]
|
|
|
|
fn test_folds_structs() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
struct Foo <fold block>{
|
|
|
|
}</fold>
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_folds_traits() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
trait Foo <fold block>{
|
|
|
|
}</fold>
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-22 07:26:32 -06:00
|
|
|
#[test]
|
|
|
|
fn test_folds_macros() {
|
2020-07-01 11:17:08 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
macro_rules! foo <fold block>{
|
2019-01-22 07:26:32 -06:00
|
|
|
($($tt:tt)*) => { $($tt)* }
|
|
|
|
}</fold>
|
2020-07-01 11:17:08 -05:00
|
|
|
"#,
|
|
|
|
);
|
2019-01-22 07:26:32 -06:00
|
|
|
}
|
2019-09-19 10:22:08 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fold_match_arms() {
|
2020-07-01 11:17:08 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn main() <fold block>{
|
|
|
|
match 0 <fold block>{
|
2019-09-19 10:22:08 -05:00
|
|
|
0 => 0,
|
|
|
|
_ => 1,
|
|
|
|
}</fold>
|
2020-07-09 11:49:17 -05:00
|
|
|
}</fold>
|
|
|
|
"#,
|
2020-07-01 11:17:08 -05:00
|
|
|
);
|
|
|
|
}
|
2019-09-19 10:22:08 -05:00
|
|
|
|
2020-07-01 11:17:08 -05:00
|
|
|
#[test]
|
|
|
|
fn fold_big_calls() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn main() <fold block>{
|
|
|
|
frobnicate<fold arglist>(
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
)</fold>
|
|
|
|
}</fold>
|
2020-07-09 11:49:17 -05:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fold_record_literals() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
const _: S = S <fold block>{
|
|
|
|
|
|
|
|
}</fold>;
|
2020-07-27 06:42:36 -05:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fold_multiline_params() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn foo<fold arglist>(
|
|
|
|
x: i32,
|
|
|
|
y: String,
|
|
|
|
)</fold> {}
|
2020-07-09 11:49:17 -05:00
|
|
|
"#,
|
2020-07-01 11:17:08 -05:00
|
|
|
)
|
2019-09-19 10:22:08 -05:00
|
|
|
}
|
2021-01-18 12:45:42 -06:00
|
|
|
|
2021-04-07 17:05:08 -05:00
|
|
|
#[test]
|
|
|
|
fn fold_multiline_array() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
const FOO: [usize; 4] = <fold array>[
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
4,
|
|
|
|
]</fold>;
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-01-18 12:45:42 -06:00
|
|
|
#[test]
|
|
|
|
fn fold_region() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
// 1. some normal comment
|
|
|
|
<fold region>// region: test
|
|
|
|
// 2. some normal comment
|
2021-07-15 10:02:45 -05:00
|
|
|
<fold region>// region: inner
|
|
|
|
fn f() {}
|
|
|
|
// endregion</fold>
|
|
|
|
fn f2() {}
|
2021-01-18 12:45:42 -06:00
|
|
|
// endregion: test</fold>
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
2021-03-29 05:43:03 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fold_consecutive_const() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
<fold consts>const FIRST_CONST: &str = "first";
|
|
|
|
const SECOND_CONST: &str = "second";</fold>
|
2021-07-15 09:35:31 -05:00
|
|
|
"#,
|
2021-03-29 05:43:03 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fold_consecutive_static() {
|
|
|
|
check(
|
|
|
|
r#"
|
2021-03-29 05:49:14 -05:00
|
|
|
<fold statics>static FIRST_STATIC: &str = "first";
|
2021-03-29 05:43:03 -05:00
|
|
|
static SECOND_STATIC: &str = "second";</fold>
|
2021-07-15 09:35:31 -05:00
|
|
|
"#,
|
2021-03-29 05:43:03 -05:00
|
|
|
)
|
|
|
|
}
|
2021-04-30 03:18:36 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fold_where_clause() {
|
|
|
|
// fold multi-line and don't fold single line.
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn foo()
|
|
|
|
where<fold whereclause>
|
|
|
|
A: Foo,
|
|
|
|
B: Foo,
|
|
|
|
C: Foo,
|
|
|
|
D: Foo,</fold> {}
|
|
|
|
|
|
|
|
fn bar()
|
|
|
|
where
|
|
|
|
A: Bar, {}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
2021-05-28 06:39:02 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fold_return_type() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn foo()<fold returntype>-> (
|
|
|
|
bool,
|
|
|
|
bool,
|
|
|
|
)</fold> { (true, true) }
|
2021-05-28 07:10:41 -05:00
|
|
|
|
|
|
|
fn bar() -> (bool, bool) { (true, true) }
|
2021-07-15 09:35:31 -05:00
|
|
|
"#,
|
2021-05-28 06:39:02 -05:00
|
|
|
)
|
|
|
|
}
|
2018-10-02 10:02:57 -05:00
|
|
|
}
|