Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8b66db9

Browse files
committed
Support ghc-mod sig command (#68)
1 parent 8a630ed commit 8b66db9

6 files changed

Lines changed: 69 additions & 0 deletions

File tree

after/ftplugin/haskell/ghcmod.vim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ endif
4949
command! -buffer -nargs=0 -bang GhcModType call ghcmod#command#type(<bang>0)
5050
command! -buffer -nargs=0 -bang GhcModTypeInsert call ghcmod#command#type_insert(<bang>0)
5151
command! -buffer -nargs=0 -bang GhcModSplitFunCase call ghcmod#command#split_function_case(<bang>0)
52+
command! -buffer -nargs=0 -bang GhcModSigCodegen call ghcmod#command#initial_code_from_signature(<bang>0)
5253
command! -buffer -nargs=? -bang GhcModInfo call ghcmod#command#info(<q-args>, <bang>0)
5354
command! -buffer -nargs=0 GhcModTypeClear call ghcmod#command#type_clear()
5455
command! -buffer -nargs=? -bang GhcModInfoPreview call ghcmod#command#info_preview(<q-args>, <bang>0)
@@ -61,6 +62,8 @@ command! -buffer -nargs=0 -bang GhcModExpand call ghcmod#command#expand(<bang>0)
6162
let b:undo_ftplugin .= join(map([
6263
\ 'GhcModType',
6364
\ 'GhcModTypeInsert',
65+
\ 'GhcModSplitFunCase',
66+
\ 'GhcModSigCodegen',
6467
\ 'GhcModInfo',
6568
\ 'GhcModInfoPreview',
6669
\ 'GhcModTypeClear',

autoload/ghcmod.vim

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ function! ghcmod#split(line, col, path, module) "{{{
3636
return split(l:parsed[5], '\n')
3737
endfunction "}}}
3838

39+
function! ghcmod#sig(line, col, path, module) "{{{
40+
" `ghc-mod sig` is available since v5.0.0.
41+
let l:cmd = ghcmod#build_command(['sig', a:path, a:module, a:line, a:col])
42+
let l:lines = s:system('sig', l:cmd)
43+
if len(l:lines) < 3
44+
return []
45+
endif
46+
return [l:lines[0], l:lines[2 :]]
47+
endfunction "}}}
48+
3949
function! ghcmod#type(line, col, path, module) "{{{
4050
let l:cmd = ghcmod#build_command(['type', a:path, a:module, a:line, a:col])
4151
let l:output = ghcmod#system(l:cmd)

autoload/ghcmod/command.vim

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,27 @@ function! ghcmod#command#split_function_case(force) "{{{
7373
delete _
7474
endfunction "}}}
7575

76+
function! ghcmod#command#initial_code_from_signature(force) "{{{
77+
let l:path = s:buffer_path(a:force)
78+
if empty(l:path)
79+
return
80+
endif
81+
82+
let l:initial_code = ghcmod#sig(line('.'), col('.'), l:path, ghcmod#detect_module())
83+
if empty(l:initial_code)
84+
call ghcmod#util#print_warning('Cannot generate initial code')
85+
return
86+
endif
87+
88+
let [l:sort, l:codes] = l:initial_code
89+
if l:sort == 'instance'
90+
let l:sw = exists('*shifwidth') ? shiftwidth() : &shiftwidth
91+
let l:indent = repeat(' ', l:sw)
92+
call map(l:codes, 'l:indent . v:val')
93+
endif
94+
call append('.', l:codes)
95+
endfunction "}}}
96+
7697
function! ghcmod#command#type_insert(force) "{{{
7798
let l:path = s:buffer_path(a:force)
7899
if empty(l:path)

test/data/sig/SigFunction.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module SigFunction where
2+
3+
func :: [a] -> Maybe b -> (a -> b) -> (a,b)

test/data/sig/SigInstance.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module SigInstance where
2+
3+
newtype D = D (Int,String)
4+
5+
class C a where
6+
cInt :: a -> Int
7+
cString :: a -> String
8+
9+
instance C D where

test/test_command_sig_codegen.vim

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
let s:unit = tinytest#new()
2+
3+
function! s:unit.test_command_sig_function()
4+
edit test/data/sig/SigFunction.hs
5+
call cursor(3, 1)
6+
GhcModSigCodegen
7+
call self.assert.match('^func x y f = ', getline(line('.')+1))
8+
endfunction
9+
10+
function! s:unit.test_command_sig_instance()
11+
edit test/data/sig/SigInstance.hs
12+
call cursor(9, 1)
13+
GhcModSigCodegen
14+
call self.assert.match('^\s*cInt x = ', getline(line('.')+1))
15+
call self.assert.match('^\s*cString x = ', getline(line('.')+2))
16+
endfunction
17+
18+
function! s:unit.teardown()
19+
" Discard changes
20+
bdelete!
21+
endfunction
22+
23+
call s:unit.run()

0 commit comments

Comments
 (0)