vim - 使用多个标记折叠

时间:2017-09-04 17:34:12

标签: vim vi collapse folding code-folding

我有一个如下所示的调试文件:

==>func1:
.....
..
==>func2:
......
...
<==func2
..
<==func1
==>func3:
......
...
<==func3

基本上,我希望能够折叠每个函数,最终看到这样的东西:

+-- x lines: ==> func1:
+-- x lines: ==> func3:

但仍然可以展开func1并看到func2折叠:

==>func1:
.....
..
+-- x lines: ==> func2:
..
<==func1

有没有办法这样做?感谢。

1 个答案:

答案 0 :(得分:0)

无与伦比的标记需要额外的处理,这里是一个 expr 解决方案(参见:h fold-expr):

setlocal foldmethod=expr
setlocal foldexpr=GetFoldlevel(v:lnum)

function GetFoldlevel(lnum)
    let line = getline(a:lnum)

    let ret = '='
    if line[0:2] == '==>'
        let name = matchstr(line, '^==>\zs\w*')
        let has_match = HasMarkerMatch(a:lnum, name)
        if has_match
            let ret = 'a1'
        endif
    elseif line[0:2] == '<=='
        let ret ='s1'
    endif

    return ret
endfunction

function HasMarkerMatch(lnum, name)
    let endline = line('$')
    let current = a:lnum + 1

    let has_match = 0
    while current <= endline
        let line = getline(current)

        if line =~ '^<=='.a:name
            let has_match = 1
            break
        endif

        let current += 1
    endwhile

    return has_match
endfunction