有没有办法让VIM折叠一行?

时间:2014-06-20 02:50:58

标签: vim folding

我正在使用已发布的herehere的VIM折叠设置。现在,请注意以下程序:

a

折叠在我的VIM上时会产生以下结果:

b

我不喜欢那种表情,我更喜欢以下内容:

c

我通过重写代码得到了:

d

但是留下这样的代码是可怕的。是否有可能更新VimScript以便我得到我想要的折叠,而不必像这样留下JavaScript代码?

这是我的确切设置:

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

以下是示例代码:

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;
};

2 个答案:

答案 0 :(得分:1)

set foldmethod=marker
set foldmarker={,}

应该给你你想要的东西。

答案 1 :(得分:0)

你正在使用折叠表达式(据我所知,一目了然)设计用于缩进折叠(用于HAML,Python,Haskell和其他缩进语法语言)。

对于C,您应该使用:set foldmethod=syntax