2017-09-04 142 views
0

我有一个调试文件看起来像这样折叠:VIM有多于一个的标志

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

基本上,我想能够折叠的功能,最终看到这样的事情每个人:

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

,但仍然能够扩大func1,看到func2折叠:

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

有没有办法做到这一点?谢谢。

回答

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 
+0

我的问题是,有可能的情况下在调试文件,例如像FUNC4功能将有打开标记,但没有关闭标记,它会破坏所有的折叠:==> func1:....... ==> func2:...... ==> func4:... <== func2。 。<==func1 ==> func3:...... ... <== func3。它看起来你的解决方案几乎是我需要的,除了func4开始标记将与func2的结束标记一起折叠的问题。有没有办法改变你的解决方案,所以折叠将精确根据函数名称? –

+0

答案更新为只折叠这些配对标记。 – leaf