The attached patch fixes a problem with syntax-based folding. Steps to
reproduce:
- make sure that you have folding for comments in C source code enabled:
: if exists("c_no_comment_fold")
: unlet c_no_comment_fold
: endif
- with the following settings:
:set ft=c fdm=syntax fdc=4 fdl=3 number
- edit the following file:
1 int fun1()
- 2 {
| 3 int i = 3;
| 4 switch (i)
|- 5 {
|| 6 case 1:
||- 7 {
||| 8 }
|| 9 case 2:
||- 10 {
||| 11 }
|| 12 }
| 13 }
- go to line 6 and start appending at the end of the line:
:6
:normal A
- enter a C-style comment (e.g. "/* comment */") observing how the
folding changes in the fold column.
When you open the comment, the fold starting at line 4 extends to line
13 and a new fold appears - from line 6 to line 13. However, after you
close the comment, although the fold at line 6 correctly disappears, the
length of the fold at line 5 is not adjusted and instead of two two-line
folds at lines 7 and 10, a single fold at line 7 appears, which extends
to line 13:
1 int fun1()
- 2 {
| 3 int i = 3;
| 4 switch (i)
|- 5 {
|| 6 case 1: /* comment */
||- 7 {
||| 8 }
||| 9 case 2:
||| 10 {
||| 11 }
||| 12 }
||| 13 }
As mentioned, the attached patch removes the problem so that the
original folding is restored after the comment has been entered.
--
Cheers,
Lech
--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_dev" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---
diff --git a/src/fold.c b/src/fold.c
index a127bf6..50d504e 100644
--- a/src/fold.c
+++ b/src/fold.c
@@ -2256,6 +2256,38 @@ foldUpdateIEMS(wp, top, bot)
}
}
+ /* If folding is defined by the syntax, it is possible that a change in one
+ * line will cause all sub-folds of the current fold to change (e.g. closing
+ * a C-style comment can cause folds in the subsequent lines to appear). To
+ * take that into account we should adjust the value of "bot" to point to
+ * the end of the current fold:
+ */
+ if (foldlevelSyntax == getlevel)
+ {
+ garray_T *gap = &wp->w_folds;
+ fold_T *fp = NULL;
+ int current_fdl = 0;
+ linenr_T fold_start_lnum = 0;
+ linenr_T lnum_rel = fline.lnum;
+
+ while (current_fdl < fline.lvl)
+ {
+ if (!foldFind(gap, lnum_rel, &fp))
+ break;
+ ++current_fdl;
+
+ fold_start_lnum += fp->fd_top;
+ gap = &fp->fd_nested;
+ lnum_rel -= fp->fd_top;
+ }
+ if (fp && current_fdl == fline.lvl)
+ {
+ linenr_T fold_end_lnum = fold_start_lnum + fp->fd_len;
+ if (fold_end_lnum > bot)
+ bot = fold_end_lnum;
+ }
+ }
+
start = fline.lnum;
end = bot;
/* Do at least one line. */