Skip to content

Commit cef27f8

Browse files
committed
Initial Commit, providing 'HdevtoolsType' and 'HdevtoolsClear'
0 parents  commit cef27f8

File tree

2 files changed

+176
-0
lines changed

2 files changed

+176
-0
lines changed

autoload/hdevtools.vim

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
" ----------------------------------------------------------------------------
2+
" Most of the code below has been taken from ghcmod-vim, with a few
3+
" adjustments and tweaks.
4+
"
5+
" ghcmod-vim:
6+
" https://github.com/eagletmt/ghcmod-vim/
7+
8+
let s:hdevtools_type = {
9+
\ 'ix': 0,
10+
\ 'types': [],
11+
\ }
12+
13+
function! s:hdevtools_type.spans(line, col)
14+
if empty(self.types)
15+
return 0
16+
endif
17+
let [l:line1, l:col1, l:line2, l:col2] = self.types[self.ix][0]
18+
return l:line1 <= a:line && a:line <= l:line2 && l:col1 <= a:col && a:col <= l:col2
19+
endfunction
20+
21+
function! s:hdevtools_type.type()
22+
return self.types[self.ix]
23+
endfunction
24+
25+
function! s:hdevtools_type.incr_ix()
26+
let self.ix = (self.ix + 1) % len(self.types)
27+
endfunction
28+
29+
function! s:hdevtools_type.highlight(group)
30+
if empty(self.types)
31+
return
32+
endif
33+
call hdevtools#clear_highlight()
34+
let [l:line1, l:col1, l:line2, l:col2] = self.types[self.ix][0]
35+
let w:hdevtools_type_matchid = matchadd(a:group, '\%' . l:line1 . 'l\%' . l:col1 . 'c\_.*\%' . l:line2 . 'l\%' . l:col2 . 'c')
36+
endfunction
37+
38+
function! s:highlight_group()
39+
return get(g:, 'hdevtools_type_highlight', 'Search')
40+
endfunction
41+
42+
function! s:on_enter()
43+
if exists('b:hdevtools_type')
44+
call b:hdevtools_type.highlight(s:highlight_group())
45+
endif
46+
endfunction
47+
48+
function! s:on_leave()
49+
call hdevtools#clear_highlight()
50+
endfunction
51+
52+
function! hdevtools#build_command(command, args)
53+
let l:cmd = 'hdevtools'
54+
let l:cmd = l:cmd . ' ' . a:command . ' '
55+
56+
let l:cmd = l:cmd . get(g:, 'hdevtools_options', '') . ' '
57+
let l:cmd = l:cmd . a:args
58+
return l:cmd
59+
endfunction
60+
61+
function! hdevtools#clear_highlight()
62+
if exists('w:hdevtools_type_matchid')
63+
call matchdelete(w:hdevtools_type_matchid)
64+
unlet w:hdevtools_type_matchid
65+
endif
66+
endfunction
67+
68+
function! hdevtools#type()
69+
if &l:modified
70+
call hdevtools#print_warning('hdevtools#type: the buffer has been modified but not written')
71+
endif
72+
let l:line = line('.')
73+
let l:col = col('.')
74+
if exists('b:hdevtools_type') && b:hdevtools_type.spans(l:line, l:col)
75+
call b:hdevtools_type.incr_ix()
76+
call b:hdevtools_type.highlight(s:highlight_group())
77+
return b:hdevtools_type.type()
78+
endif
79+
80+
let l:file = expand('%')
81+
if l:file ==# ''
82+
call hdevtools#print_warning("current version of hdevtools.vim doesn't support running on an unnamed buffer.")
83+
return ['', '']
84+
endif
85+
let l:cmd = hdevtools#build_command('type', shellescape(l:file) . ' ' . l:line . ' ' . l:col)
86+
let l:output = system(l:cmd)
87+
88+
if v:shell_error != 0
89+
for l:line in split(l:output, '\n')
90+
call hdevtools#print_error(l:line)
91+
endfor
92+
return
93+
endif
94+
95+
let l:types = []
96+
for l:line in split(l:output, '\n')
97+
let l:m = matchlist(l:line, '\(\d\+\) \(\d\+\) \(\d\+\) \(\d\+\) "\([^"]\+\)"')
98+
call add(l:types, [l:m[1 : 4], l:m[5]])
99+
endfor
100+
101+
call hdevtools#clear_highlight()
102+
103+
let l:len = len(l:types)
104+
if l:len == 0
105+
return [0, '-- No Type Information']
106+
endif
107+
108+
let b:hdevtools_type = deepcopy(s:hdevtools_type)
109+
110+
let b:hdevtools_type.types = l:types
111+
let l:ret = b:hdevtools_type.type()
112+
let [l:line1, l:col1, l:line2, l:col2] = l:ret[0]
113+
call b:hdevtools_type.highlight(s:highlight_group())
114+
115+
augroup hdevtools-type-highlight
116+
autocmd! * <buffer>
117+
autocmd BufEnter <buffer> call s:on_enter()
118+
autocmd WinEnter <buffer> call s:on_enter()
119+
autocmd BufLeave <buffer> call s:on_leave()
120+
autocmd WinLeave <buffer> call s:on_leave()
121+
augroup END
122+
123+
return l:ret
124+
endfunction
125+
126+
function! hdevtools#type_clear()
127+
if exists('b:hdevtools_type')
128+
call hdevtools#clear_highlight()
129+
unlet b:hdevtools_type
130+
endif
131+
endfunction
132+
133+
function! hdevtools#print_error(msg)
134+
echohl ErrorMsg
135+
echomsg a:msg
136+
echohl None
137+
endfunction
138+
139+
function! hdevtools#print_warning(msg)
140+
echohl WarningMsg
141+
echomsg a:msg
142+
echohl None
143+
endfunction

ftplugin/haskell/hdevtools.vim

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
if exists('b:did_ftplugin_hdevtools') && b:did_ftplugin_hdevtools
2+
finish
3+
endif
4+
let b:did_ftplugin_hdevtools = 1
5+
6+
if !exists('s:has_hdevtools')
7+
let s:has_hdevtools = 0
8+
9+
if !executable('hdevtools')
10+
call hdevtools#print_error('hdevtools: hdevtools is not executable!')
11+
finish
12+
endif
13+
14+
let s:has_hdevtools = 1
15+
endif
16+
17+
if !s:has_hdevtools
18+
finish
19+
endif
20+
21+
if exists('b:undo_ftplugin')
22+
let b:undo_ftplugin .= ' | '
23+
else
24+
let b:undo_ftplugin = ''
25+
endif
26+
27+
command! -buffer -nargs=0 HdevtoolsType echo hdevtools#type()[1]
28+
command! -buffer -nargs=0 HdevtoolsClear call hdevtools#type_clear()
29+
let b:undo_ftplugin .= join(map([
30+
\ 'HdevtoolsType',
31+
\ 'HdevtoolsClear'
32+
\ ], '"delcommand " . v:val'), ' | ')
33+
let b:undo_ftplugin .= ' | unlet b:did_ftplugin_hdevtools'

0 commit comments

Comments
 (0)