@@ -23,6 +23,7 @@ pub enum FoldKind {
2323 WhereClause ,
2424 ReturnType ,
2525 MatchArm ,
26+ Function ,
2627 // region: item runs
2728 Modules ,
2829 Consts ,
@@ -60,31 +61,32 @@ pub(crate) fn folding_ranges(file: &SourceFile) -> Vec<Fold> {
6061 NodeOrToken :: Token ( token) => token. text ( ) . contains ( '\n' ) ,
6162 } ;
6263 if is_multiline {
63- // for the arg list, we need to special handle
64- if matches ! ( element. kind( ) , ARG_LIST | PARAM_LIST ) {
64+ // for the func with multiline param list
65+ if matches ! ( element. kind( ) , FN ) {
6566 if let NodeOrToken :: Node ( node) = & element {
66- if let Some ( fn_node) = node. parent ( ) . and_then ( ast:: Fn :: cast) {
67+ if let Some ( fn_node) = ast:: Fn :: cast ( node. clone ( ) ) {
68+ if !fn_node
69+ . param_list ( )
70+ . map ( |param_list| param_list. syntax ( ) . text ( ) . contains_char ( '\n' ) )
71+ . unwrap_or ( false )
72+ {
73+ continue ;
74+ }
75+
6776 if let Some ( body) = fn_node. body ( ) {
68- // just add a big fold combine the params and body
6977 res. push ( Fold {
7078 range : TextRange :: new (
7179 node. text_range ( ) . start ( ) ,
72- body . syntax ( ) . text_range ( ) . end ( ) ,
80+ node . text_range ( ) . end ( ) ,
7381 ) ,
74- kind : FoldKind :: ArgList ,
82+ kind : FoldKind :: Function ,
7583 } ) ;
7684 merged_fn_bodies. insert ( body. syntax ( ) . text_range ( ) ) ;
7785 continue ;
7886 }
7987 }
8088 }
8189 }
82- // skip the merged function body
83- if matches ! ( element. kind( ) , BLOCK_EXPR )
84- && merged_fn_bodies. contains ( & element. text_range ( ) )
85- {
86- continue ;
87- }
8890 res. push ( Fold { range : element. text_range ( ) , kind } ) ;
8991 continue ;
9092 }
@@ -178,6 +180,7 @@ fn fold_kind(kind: SyntaxKind) -> Option<FoldKind> {
178180 ARG_LIST | PARAM_LIST | GENERIC_ARG_LIST | GENERIC_PARAM_LIST => Some ( FoldKind :: ArgList ) ,
179181 ARRAY_EXPR => Some ( FoldKind :: Array ) ,
180182 RET_TYPE => Some ( FoldKind :: ReturnType ) ,
183+ FN => Some ( FoldKind :: Function ) ,
181184 WHERE_CLAUSE => Some ( FoldKind :: WhereClause ) ,
182185 ASSOC_ITEM_LIST
183186 | RECORD_FIELD_LIST
@@ -349,6 +352,7 @@ mod tests {
349352 FoldKind :: WhereClause => "whereclause" ,
350353 FoldKind :: ReturnType => "returntype" ,
351354 FoldKind :: MatchArm => "matcharm" ,
355+ FoldKind :: Function => "function" ,
352356 FoldKind :: TraitAliases => "traitaliases" ,
353357 FoldKind :: ExternCrates => "externcrates" ,
354358 } ;
0 commit comments