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

View File

@ -446,8 +446,9 @@ pub fn handle_folding_range(
.into_iter() .into_iter()
.map(|fold| { .map(|fold| {
let kind = match fold.kind { let kind = match fold.kind {
FoldKind::Comment => FoldingRangeKind::Comment, FoldKind::Comment => Some(FoldingRangeKind::Comment),
FoldKind::Imports => FoldingRangeKind::Imports, FoldKind::Imports => Some(FoldingRangeKind::Imports),
FoldKind::Block => None,
}; };
let range = fold.range.conv_with(&line_index); let range = fold.range.conv_with(&line_index);
FoldingRange { FoldingRange {
@ -455,7 +456,7 @@ pub fn handle_folding_range(
start_character: Some(range.start.character), start_character: Some(range.start.character),
end_line: range.end.line, end_line: range.end.line,
end_character: Some(range.start.character), end_character: Some(range.start.character),
kind: Some(kind), kind,
} }
}) })
.collect(), .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); assert!(stack.is_empty(), "unmatched <{}>", tag);
ranges.sort_by_key(|r| (r.start(), r.end()));
(ranges, res) (ranges, res)
} }