-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhaskellFold.vim
More file actions
100 lines (86 loc) · 2.81 KB
/
haskellFold.vim
File metadata and controls
100 lines (86 loc) · 2.81 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
" =============================================================================
" Descriptions: Provide a function providing folding information for haskell
" files.
" Maintainer: Vincent B (twinside@gmail.com)
" Warning: Assume the presence of type signatures on top of your functions to
" work well.
" Usage: drop in ~/vimfiles/plugin or ~/.vim/plugin
" Version: 1.1
" Changelog: - 1.1 : Adding foldtext to bet more information.
" - 1.0 : initial version
" =============================================================================
if exists("g:__HASKELLFOLD_VIM__")
finish
endif
let g:__HASKELLFOLD_VIM__ = 1
" Top level bigdefs
fun! s:HaskellFoldMaster( line ) "{{{
return a:line =~ '^data\s'
\ || a:line =~ '^type\s'
\ || a:line =~ '^newdata\s'
\ || a:line =~ '^class\s'
\ || a:line =~ '^instance\s'
\ || a:line =~ '^[^:]\+\s*::'
endfunction "}}}
" Top Level one line shooters.
fun! s:HaskellSnipGlobal(line) "{{{
return a:line =~ '^module'
\ || a:line =~ '^import'
\ || a:line =~ '^infix[lr]\s'
endfunction "}}}
" The real folding function
fun! HaskellFold( lineNum ) "{{{
let line = getline( a:lineNum )
" Beginning of comment
if line =~ '^\s*--'
return 2
endif
if s:HaskellSnipGlobal( line )
return 0
endif
if line =~ '^\s*$'
let nextline = getline(a:lineNum + 1)
if s:HaskellFoldMaster( nextline ) > 0 || s:HaskellSnipGlobal( nextline ) > 0
\ || nextline =~ "^--"
return 0
else
return -1
endif
endif
return 1
endfunction "}}}
" This function skim over function definitions
" skiping comments line :
" -- ....
" and merging lines without first non space element, to
" catch the full type expression.
fun! HaskellFoldText() "{{{
let i = v:foldstart
let retVal = ''
let began = 0
while i <= v:foldend
let line = getline(i)
if began == 0 && !(line =~ '^\s*--.*$')
let retVal = line
let began = 1
elseif began != 0 && line =~ '^\s\+\S'
let retVal = retVal . substitute( substitute( line
\ , '\s\+\(.*\)$'
\ , ' \1', '' )
\ , '\s\+--.*', ' ','')
elseif began != 0
break
endif
let i = i + 1
endwhile
if retVal == ''
" We didn't found any meaningfull text
return foldtext()
endif
return retVal
endfunction "}}}
augroup HaskellFold
au BufNewFile,BufRead,BufCreate *.hs setlocal foldexpr=HaskellFold(v:lnum)
au BufNewFile,BufRead,BufCreate *.hs setlocal foldtext=HaskellFoldText()
au BufNewFile,BufRead,BufCreate *.hs setlocal foldmethod=expr
augroup END