-
Notifications
You must be signed in to change notification settings - Fork 2
/
rust-fold.vim
53 lines (42 loc) · 1.17 KB
/
rust-fold.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
function! MakeRustFuncDefs()
let b:RustFuncDefs = []
let lnum = 1
while lnum <= line('$')
let current_line = getline(lnum)
if match(current_line, '^ *\(pub \)\?fn') > -1
call AddRustFunc(lnum)
endif
let lnum += 1
endwhile
endfunction
function! AddRustFunc(lnum)
let save_pos = getpos('.')
call setpos('.', [0, a:lnum, 1, 0])
call search('{')
let start_lnum = line('.')
let end_lnum = searchpair('{', '', '}', 'n')
if end_lnum < 1
call setpos('.', save_pos)
return
endif
call add(b:RustFuncDefs, [start_lnum, end_lnum]);
call setpos('.', save_pos)
endfunction
function! RustFold()
if !exists("b:RustFuncDefs")
call MakeRustFuncDefs()
endif
for [start_lnum, end_lnum] in b:RustFuncDefs
if start_lnum > v:lnum
return 0
endif
if v:lnum == start_lnum + 1
return ">1"
elseif v:lnum == end_lnum
return "<1"
elseif v:lnum > start_lnum && v:lnum < end_lnum
return "="
endif
endfor
endfunction
autocmd FileType rust setlocal foldmethod=expr foldexpr=RustFold()