@@ -2505,7 +2505,11 @@ foldUpdateIEMSRecurse(
25052505 * before where we started looking, extend it. If it
25062506 * starts at another line, update nested folds to keep
25072507 * their position, compensating for the new fd_top. */
2508- if (fp -> fd_top >= startlnum && fp -> fd_top != firstlnum )
2508+ if (fp -> fd_top == firstlnum )
2509+ {
2510+ /* have found a fold beginning where we want */
2511+ }
2512+ else if (fp -> fd_top >= startlnum )
25092513 {
25102514 if (fp -> fd_top > firstlnum )
25112515 /* like lines are inserted */
@@ -2523,18 +2527,44 @@ foldUpdateIEMSRecurse(
25232527 fp -> fd_top = firstlnum ;
25242528 fold_changed = TRUE;
25252529 }
2526- else if (flp -> start != 0 && lvl == level
2527- && fp -> fd_top != firstlnum )
2530+ else if (( flp -> start != 0 && lvl == level )
2531+ || firstlnum != startlnum )
25282532 {
2529- /* Existing fold that includes startlnum must stop
2530- * if we find the start of a new fold at the same
2531- * level. Split it. Delete contained folds at
2532- * this point to split them too. */
2533- foldRemove (& fp -> fd_nested , flp -> lnum - fp -> fd_top ,
2534- flp -> lnum - fp -> fd_top );
2533+ linenr_T breakstart ;
2534+ linenr_T breakend ;
2535+
2536+ /*
2537+ * Before there was a fold spanning from above
2538+ * startlnum to below firstlnum. This fold is valid
2539+ * above startlnum (because we are not updating
2540+ * that range), but there should now be a break in
2541+ * it.
2542+ * If the break is because we are now forced to
2543+ * start a new fold at the level "level" at line
2544+ * fline->lnum, then we need to split the fold at
2545+ * fline->lnum.
2546+ * If the break is because the range
2547+ * [startlnum, firstlnum) is now at a lower indent
2548+ * than "level", we need to split the fold in this
2549+ * range.
2550+ * Any splits have to be done recursively.
2551+ */
2552+ if (firstlnum != startlnum )
2553+ {
2554+ breakstart = startlnum ;
2555+ breakend = firstlnum ;
2556+ }
2557+ else
2558+ {
2559+ breakstart = flp -> lnum ;
2560+ breakend = flp -> lnum ;
2561+ }
2562+ foldRemove (& fp -> fd_nested , breakstart - fp -> fd_top ,
2563+ breakend - fp -> fd_top );
25352564 i = (int )(fp - (fold_T * )gap -> ga_data );
2536- foldSplit (gap , i , flp -> lnum , flp -> lnum - 1 );
2565+ foldSplit (gap , i , breakstart , breakend - 1 );
25372566 fp = (fold_T * )gap -> ga_data + i + 1 ;
2567+
25382568 /* If using the "marker" or "syntax" method, we
25392569 * need to continue until the end of the fold is
25402570 * found. */
@@ -2543,6 +2573,20 @@ foldUpdateIEMSRecurse(
25432573 || getlevel == foldlevelSyntax )
25442574 finish = TRUE;
25452575 }
2576+
2577+ if (fp -> fd_top == startlnum && concat )
2578+ {
2579+ i = (int )(fp - (fold_T * )gap -> ga_data );
2580+ if (i != 0 )
2581+ {
2582+ fp2 = fp - 1 ;
2583+ if (fp2 -> fd_top + fp2 -> fd_len == fp -> fd_top )
2584+ {
2585+ foldMerge (fp2 , gap , fp );
2586+ fp = fp2 ;
2587+ }
2588+ }
2589+ }
25462590 break ;
25472591 }
25482592 if (fp -> fd_top >= startlnum )
0 commit comments