MaiaVictor
MaiaVictor

Reputation: 52957

Is there any way to make VIM fold one line more?

I am using an adaptation of the VIM folding settings published here and here. Now, notice the program below:

a

When folded on my VIM, it produces the following result:

b

I don't really like that look, I'd prefer the following:

c

Which I got by rewriting the code:

d

But leaving the code like that is horrible. Is it possible to update the VimScript so I get the folding I want, without having to leave the JavaScript code like that?

Here is my exact setting:

setlocal foldmethod=expr
setlocal foldexpr=GetPotionFold(v:lnum)
setlocal foldminlines=0

function! s:NextNonBlankLine(lnum)
    let numlines = line('$')
    let current = a:lnum + 1

    while current <= numlines
        if getline(current) =~? '\v\S'
            return current
        endif

        let current += 1
    endwhile

    return -2
endfunction

function! s:IndentLevel(lnum)
    return indent(a:lnum) / &shiftwidth
endfunction

function! GetPotionFold(lnum)
    if getline(a:lnum) =~? '\v^\s*$'
        return '-1'
    endif

    let this_indent = <SID>IndentLevel(a:lnum)
    let next_indent = <SID>IndentLevel(<SID>NextNonBlankLine(a:lnum))

    if next_indent == this_indent
        return this_indent
    elseif next_indent < this_indent
        return this_indent
    elseif next_indent > this_indent
        return '>' . next_indent
    endif
endfunction

function! NeatFoldText()
  let line = getline(v:foldstart)
  let lines_count = v:foldend - v:foldstart + 1
  let lines_count_text = '| ' . printf("%10s", lines_count . ' lines') . ' |'
  let foldchar = ' '
  let foldtextstart = strpart(line, 0, (winwidth(0)*2)/3)
  let foldtextend = lines_count_text . repeat(foldchar, 6)
  let foldtextlength = strlen(substitute(foldtextstart . foldtextend, '.', 'x', 'g')) + &foldcolumn
  return foldtextstart . repeat(foldchar, winwidth(0)-foldtextlength) . foldtextend
endfunction
set foldtext=NeatFoldText()

hi Folded ctermbg=255 ctermfg=21
hi FoldColumn ctermbg=white ctermfg=darkred

And here is the sample code:

function foo(x){
    var y = x*x;
    var z = y+y;
    return z;
};

function bar(x){
    var y = x*x;
    var z = y+y;
    return z;
};

function foobar(x){
    var y = x*x;
    var z = y+y;
    return z;
};

function barfoo(x){
    var y = x*x;
    var z = y+y;
    return z;
};

Upvotes: 1

Views: 524

Answers (2)

romainl
romainl

Reputation: 196466

set foldmethod=marker
set foldmarker={,}

Should give you what you want.

Upvotes: 1

Amadan
Amadan

Reputation: 198294

You are using a fold expression that is (as far as I can see from a glance) designed for indent folding (used for HAML, Python, Haskell and other indent-syntax languages).

For C, you should just be using :set foldmethod=syntax.

Upvotes: 0

Related Questions