Hi Folks, I'd like to improve C-syntax folding for a common code format style.
With `foldmethod=syntax` the code: if (...) { ... } else if (...) { ... } else { ... } folds like this: +--- 3 lines: if (...) {--------------------------- +--- 3 lines: else if (...) {---------------------- +--- 3 lines: else {------------------------------- However, the code: if (...) { ... } else if (...) { ... } else { ... } folds like this: +--- 7 lines: if (...) {--------------------------- We can make the latter case fold like this: +--- 2 lines: if (...) {--------------------------- +--- 2 lines: } else if (...) {-------------------- +--- 3 lines: } else {----------------------------- by choosing on each line the lowest fold level that is followed by a higher fold level. Please see patch attached. FWIW I've been running with this patch for months to test it. Thanks, -Brad -- -- You received this message from the "vim_dev" maillist. Do not top-post! Type your reply below the text you are replying to. For more information, visit http://www.vim.org/maillist.php --- You received this message because you are subscribed to the Google Groups "vim_dev" group. To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
>From 5f1a9107cf9a9732d7f827cd66928b8565fe6a72 Mon Sep 17 00:00:00 2001 Message-Id: <5f1a9107cf9a9732d7f827cd66928b8565fe6a72.1476457236.git.brad.k...@kitware.com> From: Brad King <brad.k...@kitware.com> Date: Wed, 11 May 2016 21:51:34 -0400 Subject: [PATCH] syntax: Improve foldlevel on lines where it changes horizontally With `foldmethod=syntax` the code: if (...) { ... } else if (...) { ... } else { ... } folds like this: +--- 3 lines: if (...) {--------------------------- +--- 3 lines: else if (...) {---------------------- +--- 3 lines: else {------------------------------- However, the code: if (...) { ... } else if (...) { ... } else { ... } folds like this: +--- 7 lines: if (...) {--------------------------- We can make the latter case fold like this: +--- 2 lines: if (...) {--------------------------- +--- 2 lines: } else if (...) {-------------------- +--- 3 lines: } else {----------------------------- by choosing on each line the lowest fold level that is followed by a higher fold level. --- src/syntax.c | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/src/syntax.c b/src/syntax.c index 75ede36..c0c87f8 100644 --- a/src/syntax.c +++ b/src/syntax.c @@ -6537,6 +6537,17 @@ syn_get_stack_item(int i) #endif #if defined(FEAT_FOLDING) || defined(PROTO) + static int +syn_cur_foldlevel(void) +{ + int level = 0; + int i; + for (i = 0; i < current_state.ga_len; ++i) + if (CUR_STATE(i).si_flags & HL_FOLD) + ++level; + return level; +} + /* * Function called to get folding level for line "lnum" in window "wp". */ @@ -6544,16 +6555,30 @@ syn_get_stack_item(int i) syn_get_foldlevel(win_T *wp, long lnum) { int level = 0; - int i; + int low_level; + int cur_level; /* Return quickly when there are no fold items at all. */ if (wp->w_s->b_syn_folditems != 0) { syntax_start(wp, lnum); - for (i = 0; i < current_state.ga_len; ++i) - if (CUR_STATE(i).si_flags & HL_FOLD) - ++level; + /* Start with the fold level at the start of the line. */ + cur_level = syn_cur_foldlevel(); + level = cur_level; + low_level = cur_level; + + /* Find the lowest fold level that is followed by a higher one. */ + while (!current_finished) + { + (void)syn_current_attr(FALSE, FALSE, NULL, FALSE); + cur_level = syn_cur_foldlevel(); + if (cur_level < low_level) + low_level = cur_level; + else if (cur_level > low_level) + level = low_level; + ++current_col; + } } if (level > wp->w_p_fdn) { -- 2.9.3