fold curly blocks

This commit is contained in:
Aleksey Kladov 2018-12-20 22:13:16 +03:00
parent 8d7e8a175e
commit 23b040962f
3 changed files with 30 additions and 18 deletions

View File

@ -10,6 +10,7 @@ use ra_syntax::{
pub enum FoldKind {
Comment,
Imports,
Block,
}
#[derive(Debug)]
@ -62,6 +63,8 @@ fn fold_kind(kind: SyntaxKind) -> Option<FoldKind> {
match kind {
COMMENT => Some(FoldKind::Comment),
USE_ITEM => Some(FoldKind::Imports),
NAMED_FIELD_DEF_LIST | FIELD_PAT_LIST | ITEM_LIST | EXTERN_ITEM_LIST | USE_TREE_LIST
| BLOCK | ENUM_VARIANT_LIST => Some(FoldKind::Block),
_ => None,
}
}
@ -205,7 +208,7 @@ mod tests {
// But this is not
fn main() {
fn main() <fold>{
<fold>// We should
// also
// fold
@ -214,10 +217,11 @@ fn main() {
//! because it has another flavor</fold>
<fold>/* As does this
multiline comment */</fold>
}"#;
}</fold>"#;
let fold_kinds = &[
FoldKind::Comment,
FoldKind::Block,
FoldKind::Comment,
FoldKind::Comment,
FoldKind::Comment,
@ -228,16 +232,16 @@ fn main() {
#[test]
fn test_fold_imports() {
let text = r#"
<fold>use std::{
<fold>use std::<fold>{
str,
vec,
io as iop
};</fold>
}</fold>;</fold>
fn main() {
}"#;
fn main() <fold>{
}</fold>"#;
let folds = &[FoldKind::Imports];
let folds = &[FoldKind::Imports, FoldKind::Block, FoldKind::Block];
do_check(text, folds);
}
@ -255,10 +259,10 @@ use std::collections::HashMap;
// Some random comment
use std::collections::VecDeque;
fn main() {
}"#;
fn main() <fold>{
}</fold>"#;
let folds = &[FoldKind::Imports, FoldKind::Imports];
let folds = &[FoldKind::Imports, FoldKind::Imports, FoldKind::Block];
do_check(text, folds);
}
@ -272,16 +276,22 @@ use std::io as iop;</fold>
<fold>use std::mem;
use std::f64;</fold>
<fold>use std::collections::{
<fold>use std::collections::<fold>{
HashMap,
VecDeque,
};</fold>
}</fold>;</fold>
// Some random comment
fn main() {
}"#;
fn main() <fold>{
}</fold>"#;
let folds = &[FoldKind::Imports, FoldKind::Imports, FoldKind::Imports];
let folds = &[
FoldKind::Imports,
FoldKind::Imports,
FoldKind::Imports,
FoldKind::Block,
FoldKind::Block,
];
do_check(text, folds);
}

View File

@ -446,8 +446,9 @@ pub fn handle_folding_range(
.into_iter()
.map(|fold| {
let kind = match fold.kind {
FoldKind::Comment => FoldingRangeKind::Comment,
FoldKind::Imports => FoldingRangeKind::Imports,
FoldKind::Comment => Some(FoldingRangeKind::Comment),
FoldKind::Imports => Some(FoldingRangeKind::Imports),
FoldKind::Block => None,
};
let range = fold.range.conv_with(&line_index);
FoldingRange {
@ -455,7 +456,7 @@ pub fn handle_folding_range(
start_character: Some(range.start.character),
end_line: range.end.line,
end_character: Some(range.start.character),
kind: Some(kind),
kind,
}
})
.collect(),

View File

@ -98,6 +98,7 @@ pub fn extract_ranges(mut text: &str, tag: &str) -> (Vec<TextRange>, String) {
}
}
assert!(stack.is_empty(), "unmatched <{}>", tag);
ranges.sort_by_key(|r| (r.start(), r.end()));
(ranges, res)
}