From 31ed6d56646ba038d4921b03ccbdd490965e3336 Mon Sep 17 00:00:00 2001 From: "Tejasvi S. Tomar" <45873379+tejasvi@users.noreply.github.com> Date: Thu, 20 Jan 2022 22:07:25 +0530 Subject: [PATCH 1/2] Fold last line of the block Fixes https://github.com/pseewald/vim-anyfold/issues/37 --- autoload/anyfold.vim | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/autoload/anyfold.vim b/autoload/anyfold.vim index bf0022c..8be9c36 100644 --- a/autoload/anyfold.vim +++ b/autoload/anyfold.vim @@ -295,27 +295,35 @@ function! s:ActualIndents(line_start, line_end) abort let curr_line += 1 let prev_indent = ind_list[-1] let next_indent = indent(s:NextNonBlankLine(curr_line)) - if s:ConsiderLine(curr_line) - " non-empty lines that define an indent - let ind_list += [indent(curr_line)] - elseif getline(curr_line) =~? '^\s*{\W*$' + if getline(curr_line) =~? '^\s*{\W*$' " line consisting of { brace: this increases indent level let ind_list += [min([ind_list[-1] + shiftwidth(), next_indent])] - elseif getline(curr_line) =~? '^\s*}\W*$' - " line consisting of } brace: this has indent of line with matching { - let restore = winsaveview() - keepjumps exe curr_line - keepjumps normal! % - let br_open_pos = getpos('.')[1] - call winrestview(restore) + else + let cur_indent = indent(curr_line) + if cur_indent < prev_indent + let restore = winsaveview() + keepjumps exe curr_line + keepjumps normal % + let br_open_pos = getpos('.')[1] + call winrestview(restore) + else + let br_open_pos = curr_line + endif if br_open_pos < curr_line && br_open_pos >= a:line_start - offset - let ind_list += [ind_list[offset + br_open_pos - a:line_start]] + " Found scope start + let ind_list += [ind_list[s:NextNonBlankLine(offset + br_open_pos - a:line_start)]] else - " in case matching { does not exist or is out of range - let ind_list += [max([prev_indent, next_indent])] + if s:ConsiderLine(curr_line) + " non-empty lines that define an indent + let ind_list += [cur_indent] + elseif getline(curr_line) =~? '^\s*}\W*$' + " line consisting of } brace: this has indent of line with matching { + " in case matching { does not exist or is out of range + let ind_list += [max([prev_indent, next_indent])] + else + let ind_list += [max([prev_indent, next_indent])] + endif endif - else - let ind_list += [max([prev_indent, next_indent])] endif endwhile return ind_list[offset : ] From b09884ed8105c3aaf5aef716ae7a332f391704da Mon Sep 17 00:00:00 2001 From: tejasvi <45873379+tejasvi@users.noreply.github.com> Date: Fri, 21 Jan 2022 00:03:39 +0530 Subject: [PATCH 2/2] Handle edge case --- autoload/anyfold.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoload/anyfold.vim b/autoload/anyfold.vim index 8be9c36..da15b49 100644 --- a/autoload/anyfold.vim +++ b/autoload/anyfold.vim @@ -311,7 +311,7 @@ function! s:ActualIndents(line_start, line_end) abort endif if br_open_pos < curr_line && br_open_pos >= a:line_start - offset " Found scope start - let ind_list += [ind_list[s:NextNonBlankLine(offset + br_open_pos - a:line_start)]] + let ind_list += [ind_list[min([s:NextNonBlankLine(offset + br_open_pos - a:line_start), len(ind_list) - 1])]] else if s:ConsiderLine(curr_line) " non-empty lines that define an indent