【问题标题】:vim - folding with more than one markervim - 用多个标记折叠
【发布时间】:2017-09-04 17:34:12
【问题描述】:

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

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

基本上,我希望能够折叠每个功能,最终看到如下内容:

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

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

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

有什么办法吗?谢谢。

【问题讨论】:

    标签: vim vi collapse folding code-folding


    【解决方案1】:

    不匹配的标记需要额外的句柄,这里有一个 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
    

    【讨论】:

    • 我的问题是,在调试文件中可能存在这样的情况,例如 func4 之类的函数将具有开始标记但没有结束标记,并且它会破坏所有折叠:==>func1: 。 .... .. ==>func2: ...... ==>func4: ... func3: ...... ...
    • 答案已更新为仅折叠这些配对标记。
    猜你喜欢
    • 2012-06-13
    • 1970-01-01
    • 2014-01-12
    • 2015-11-04
    • 1970-01-01
    • 2010-10-22
    • 1970-01-01
    • 1970-01-01
    • 2014-02-25
    相关资源
    最近更新 更多