-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathghcmod.vim
356 lines (327 loc) · 10.7 KB
/
ghcmod.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
function! ghcmod#highlight_group() "{{{
return get(g:, 'ghcmod_type_highlight', 'Search')
endfunction "}}}
" Return the current haskell identifier
function! ghcmod#getHaskellIdentifier() "{{{
let c = col ('.')-1
let l = line('.')
let ll = getline(l)
let ll1 = strpart(ll,0,c)
let ll1 = matchstr(ll1,"[a-zA-Z0-9_'.]*$")
let ll2 = strpart(ll,c,strlen(ll)-c+1)
let ll2 = matchstr(ll2,"^[a-zA-Z0-9_'.]*")
return ll1.ll2
endfunction "}}}
function! ghcmod#info(fexp, path, ...) "{{{
let l:cmd = ghcmod#build_command(["-b \n", 'info', a:path, a:fexp])
let l:output = ghcmod#system(l:cmd)
" Remove trailing newlines to prevent empty lines
let l:output = substitute(l:output, '\n*$', '', '')
return s:remove_dummy_prefix(l:output)
endfunction "}}}
function! ghcmod#split(line, col, path, ...) "{{{
" `ghc-mod split` is available since v5.0.0.
let l:cmd = ghcmod#build_command(['split', a:path, a:line, a:col])
let l:lines = s:system('split', l:cmd)
if empty(l:lines)
return []
endif
let l:parsed = matchlist(l:lines[0], '\(\d\+\) \(\d\+\) \(\d\+\) \(\d\+\) "\(.*\)"')
if len(l:parsed) < 5
return []
endif
return split(l:parsed[5], '\n')
endfunction "}}}
function! ghcmod#sig(line, col, path, ...) "{{{
" `ghc-mod sig` is available since v5.0.0.
let l:cmd = ghcmod#build_command(['sig', a:path, a:line, a:col])
let l:lines = s:system('sig', l:cmd)
if len(l:lines) < 3
return []
endif
return [l:lines[0], l:lines[2 :]]
endfunction "}}}
function! ghcmod#type(line, col, path, ...) "{{{
let l:cmd = ghcmod#build_command(['type', a:path, a:line, a:col])
let l:output = ghcmod#system(l:cmd)
let l:types = []
for l:line in split(l:output, '\n')
let l:m = matchlist(l:line, '\(\d\+\) \(\d\+\) \(\d\+\) \(\d\+\) "\([^"]\+\)"')
if !empty(l:m)
call add(l:types, [map(l:m[1 : 4], 'str2nr(v:val, 10)'), l:m[5]])
endif
endfor
return l:types
endfunction "}}}
function! ghcmod#detect_module() "{{{
let l:regex = '^\C>\=\s*module\s\+\zs[A-Za-z0-9.]\+'
for l:lineno in range(1, line('$'))
let l:line = getline(l:lineno)
let l:pos = match(l:line, l:regex)
if l:pos != -1
let l:synname = synIDattr(synID(l:lineno, l:pos+1, 0), 'name')
if l:synname !~# 'Comment'
return matchstr(l:line, l:regex)
endif
endif
let l:lineno += 1
endfor
return 'Main'
endfunction "}}}
function! s:fix_qf_lnum_col(qf) "{{{
" ghc-mod reports dummy error message with lnum=0 and col=0.
" This is not suitable for Vim, so tweak them.
for l:key in ['lnum', 'col']
if get(a:qf, l:key, -1) == 0
let a:qf[l:key] = 1
endif
endfor
endfunction "}}}
function! ghcmod#parse_make(lines, basedir) "{{{
" `ghc-mod check` and `ghc-mod lint` produces <NUL> characters but Vim cannot
" treat them correctly. Vim converts <NUL> characters to <NL> in readfile().
" See also :help readfile() and :help NL-used-for-Nul.
let l:qflist = []
for l:output in a:lines
if empty(l:output)
continue
endif
let l:qf = {}
let l:m = matchlist(l:output, '^\(\(\f\| \)\+\):\(\d\+\):\(\d\+\):\s*\(.*\)$')
if len(l:m) < 5
let l:qf.bufnr = 0
let l:qf.type = 'E'
let l:qf.text = 'parse error in ghcmod! Could not parse the following ghc-mod output:' . l:output
call add(l:qflist, l:qf)
break
end
let [l:qf.filename, _, l:qf.lnum, l:qf.col, l:rest] = l:m[1 : 5]
let l:qf.filename = ghcmod#util#join_path(a:basedir, l:qf.filename)
if l:rest =~# '^Warning:'
let l:qf.type = 'W'
let l:rest = matchstr(l:rest, '^Warning:\s*\zs.*$')
elseif l:rest =~# '^Error:'
let l:qf.type = 'E'
let l:rest = matchstr(l:rest, '^Error:\s*\zs.*$')
else
let l:qf.type = 'E'
endif
let l:texts = split(l:rest, '\n')
if len(l:texts) > 0
let l:qf.text = l:texts[0]
call add(l:qflist, l:qf)
for l:text in l:texts[1 :]
call add(l:qflist, {'text': l:text})
endfor
else
let l:qf.type = 'E'
call s:fix_qf_lnum_col(l:qf)
let l:qf.text = 'parse error in ghcmod! Could not parse the following ghc-mod output:'
call add(l:qflist, l:qf)
for l:text in a:lines
call add(l:qflist, {'text': l:text})
endfor
break
endif
endfor
return l:qflist
endfunction "}}}
function! s:build_make_command(type, path) "{{{
let l:cmd = ghcmod#build_command([a:type])
if a:type ==# 'lint'
for l:hopt in get(g:, 'ghcmod_hlint_options', [])
call extend(l:cmd, ['-h', l:hopt])
endfor
endif
call add(l:cmd, a:path)
return l:cmd
endfunction "}}}
function! ghcmod#make(type, path) "{{{
try
let l:args = s:build_make_command(a:type, a:path)
return ghcmod#parse_make(s:system(a:type, l:args), b:ghcmod_basedir)
catch
call ghcmod#util#print_error(printf('%s %s', v:throwpoint, v:exception))
endtry
endfunction "}}}
function! ghcmod#async_make(type, path, callback) "{{{
let l:tmpfile = tempname()
let l:args = s:build_make_command(a:type, a:path)
let l:proc = s:plineopen3([{'args': l:args, 'fd': { 'stdin': '', 'stdout': l:tmpfile, 'stderr': '' }}])
let l:obj = {
\ 'proc': l:proc,
\ 'tmpfile': l:tmpfile,
\ 'callback': a:callback,
\ 'type': a:type,
\ 'basedir': ghcmod#basedir(),
\ }
function! l:obj.on_finish(cond, status)
let l:qflist = ghcmod#parse_make(readfile(self.tmpfile), self.basedir)
call delete(self.tmpfile)
call self.callback.on_finish(l:qflist)
endfunction
if !ghcmod#async#register(l:obj)
call l:proc.kill(15)
call l:proc.waitpid()
call delete(l:tmpfile)
endif
endfunction "}}}
function! ghcmod#expand(path) "{{{
let l:dir = fnamemodify(a:path, ':h')
let l:qflist = []
let l:cmd = ghcmod#build_command(['expand', "-b '\n'", a:path])
for l:line in split(ghcmod#system(l:cmd), '\n')
let l:line = s:remove_dummy_prefix(l:line)
" path:line:col1-col2: message
" or path:line:col: message
let l:m = matchlist(l:line, '^\s*\(\(\f\| \)\+\):\(\d\+\):\(\d\+\)\%(-\(\d\+\)\)\?\%(:\s*\(.*\)\)\?$')
if !empty(l:m)
let l:qf = {}
let [l:qf.filename, _, l:qf.lnum, l:qf.col, l:col2, l:qf.text] = l:m[1 : 6]
call add(l:qflist, l:qf)
if !empty(l:col2)
let l:qf2 = deepcopy(l:qf)
let l:qf2.col = l:col2
let l:qf2.text = 'Splicing end here'
call add(l:qflist, l:qf2)
endif
else
" path:(line1,col1)-(line2,col2): message
let l:m = matchlist(l:line, '^\s*\(\(\f\| \)\+\):(\(\d\+\),\(\d\+\))-(\(\d\+\),\(\d\+\))\%(:\s*\(.*\)\)\?$')
if !empty(l:m)
let [l:filename, _, l:lnum1, l:col1, l:lnum2, l:col2, l:text] = l:m[1 : 7]
call add(l:qflist, { 'filename': l:filename, 'lnum': l:lnum1, 'col': l:col1, 'text': l:text })
call add(l:qflist, { 'filename': l:filename, 'lnum': l:lnum2, 'col': l:col2, 'text': 'Splicing end here' })
else
" message
let l:text = substitute(l:line, '^\s\{2\}', '', '')
call add(l:qflist, { 'text': l:text })
endif
endif
endfor
for l:qf in l:qflist
if has_key(l:qf, 'filename')
let l:qf.filename = ghcmod#util#join_path(l:dir, l:qf.filename)
endif
if has_key(l:qf, 'lnum')
let l:qf.lnum = str2nr(l:qf.lnum)
let l:qf.col = str2nr(l:qf.col)
endif
call s:fix_qf_lnum_col(l:qf)
endfor
return l:qflist
endfunction "}}}
function! s:remove_dummy_prefix(str) "{{{
return substitute(a:str, '^Dummy:0:0:Error:', '', '')
endfunction "}}}
function! ghcmod#add_autogen_dir(path, cmd) "{{{
" detect autogen directory
let l:autogen_dir = a:path . '/autogen'
if isdirectory(l:autogen_dir)
call extend(a:cmd, ['-g', '-i' . l:autogen_dir, '-g', '-I' . l:autogen_dir])
let l:macros_path = l:autogen_dir . '/cabal_macros.h'
if filereadable(l:macros_path)
call extend(a:cmd, ['-g', '-optP-include', '-g', '-optP' . l:macros_path])
endif
endif
endfunction "}}}
function! ghcmod#build_command(args) "{{{
let l:cmd = ['ghc-mod', '--silent']
let l:dist_top = s:find_basedir() . '/dist'
let l:sandboxes = split(glob(l:dist_top . '/dist-*', 1), '\n')
for l:dist_dir in [l:dist_top] + l:sandboxes
let l:build_dir = l:dist_dir . '/build'
if isdirectory(l:build_dir)
call ghcmod#add_autogen_dir(l:build_dir, l:cmd)
let l:tmps = ghcmod#util#globlist(l:build_dir . '/*/*-tmp')
if !empty(l:tmps)
" add *-tmp directory to include path for executable project
for l:tmp in l:tmps
call extend(l:cmd, ['-g', '-i' . l:tmp, '-g', '-I' . l:tmp])
endfor
else
" add build directory to include path for library project
call extend(l:cmd, ['-g', '-i' . l:build_dir, '-g', '-I' . l:build_dir])
endif
endif
endfor
if exists('b:ghcmod_ghc_options')
let l:opts = b:ghcmod_ghc_options
else
let l:opts = get(g:, 'ghcmod_ghc_options', [])
endif
for l:opt in l:opts
call extend(l:cmd, ['-g', l:opt])
endfor
call extend(l:cmd, a:args)
return l:cmd
endfunction "}}}
function! ghcmod#system(...) "{{{
let l:dir = getcwd()
try
lcd `=ghcmod#basedir()`
let l:ret = call('vimproc#system', a:000)
finally
lcd `=l:dir`
endtry
return l:ret
endfunction "}}}
function! s:plineopen3(...) "{{{
let l:dir = getcwd()
try
lcd `=ghcmod#basedir()`
let l:ret = call('vimproc#plineopen3', a:000)
finally
lcd `=l:dir`
endtry
return l:ret
endfunction "}}}
function! s:system(type, args) "{{{
let l:tmpfile = tempname()
try
let l:proc = s:plineopen3([{'args': a:args, 'fd': { 'stdin': '', 'stdout': l:tmpfile, 'stderr': '' }}])
let [l:cond, l:status] = ghcmod#util#wait(l:proc)
let l:tries = 1
while l:cond ==# 'run'
if l:tries >= 50
call l:proc.kill(15) " SIGTERM
call l:proc.waitpid()
throw printf('ghcmod#make: `ghc-mod %s` takes too long time!', a:type)
endif
sleep 100m
let [l:cond, l:status] = ghcmod#util#wait(l:proc)
let l:tries += 1
endwhile
let l:lines = readfile(l:tmpfile)
return l:lines
finally
call delete(l:tmpfile)
endtry
endfunction "}}}
function! ghcmod#basedir() "{{{
let l:use_basedir = get(g:, 'ghcmod_use_basedir', '')
if empty(l:use_basedir)
return s:find_basedir()
else
return l:use_basedir
endif
endfunction "}}}
function! s:find_basedir() "{{{
" search Cabal file
if !exists('b:ghcmod_basedir')
" `ghc-mod root` is available since v4.0.0.
let l:dir = getcwd()
try
lcd `=expand('%:p:h')`
let b:ghcmod_basedir =
\ substitute(vimproc#system(['ghc-mod', '--silent', 'root']), '\n*$', '', '')
finally
lcd `=l:dir`
endtry
endif
return b:ghcmod_basedir
endfunction "}}}
function! ghcmod#version() "{{{
return [1, 3, 1]
endfunction "}}}
" vim: set ts=2 sw=2 et fdm=marker: