Skip to content

Commit

Permalink
Support ghc-mod sig command (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
eagletmt committed Jul 26, 2015
1 parent 8a630ed commit 8b66db9
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 0 deletions.
3 changes: 3 additions & 0 deletions after/ftplugin/haskell/ghcmod.vim
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ endif
command! -buffer -nargs=0 -bang GhcModType call ghcmod#command#type(<bang>0)
command! -buffer -nargs=0 -bang GhcModTypeInsert call ghcmod#command#type_insert(<bang>0)
command! -buffer -nargs=0 -bang GhcModSplitFunCase call ghcmod#command#split_function_case(<bang>0)
command! -buffer -nargs=0 -bang GhcModSigCodegen call ghcmod#command#initial_code_from_signature(<bang>0)
command! -buffer -nargs=? -bang GhcModInfo call ghcmod#command#info(<q-args>, <bang>0)
command! -buffer -nargs=0 GhcModTypeClear call ghcmod#command#type_clear()
command! -buffer -nargs=? -bang GhcModInfoPreview call ghcmod#command#info_preview(<q-args>, <bang>0)
Expand All @@ -61,6 +62,8 @@ command! -buffer -nargs=0 -bang GhcModExpand call ghcmod#command#expand(<bang>0)
let b:undo_ftplugin .= join(map([
\ 'GhcModType',
\ 'GhcModTypeInsert',
\ 'GhcModSplitFunCase',
\ 'GhcModSigCodegen',
\ 'GhcModInfo',
\ 'GhcModInfoPreview',
\ 'GhcModTypeClear',
Expand Down
10 changes: 10 additions & 0 deletions autoload/ghcmod.vim
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ function! ghcmod#split(line, col, path, module) "{{{
return split(l:parsed[5], '\n')
endfunction "}}}

function! ghcmod#sig(line, col, path, module) "{{{
" `ghc-mod sig` is available since v5.0.0.
let l:cmd = ghcmod#build_command(['sig', a:path, a:module, 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, module) "{{{
let l:cmd = ghcmod#build_command(['type', a:path, a:module, a:line, a:col])
let l:output = ghcmod#system(l:cmd)
Expand Down
21 changes: 21 additions & 0 deletions autoload/ghcmod/command.vim
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,27 @@ function! ghcmod#command#split_function_case(force) "{{{
delete _
endfunction "}}}

function! ghcmod#command#initial_code_from_signature(force) "{{{
let l:path = s:buffer_path(a:force)
if empty(l:path)
return
endif

let l:initial_code = ghcmod#sig(line('.'), col('.'), l:path, ghcmod#detect_module())
if empty(l:initial_code)
call ghcmod#util#print_warning('Cannot generate initial code')
return
endif

let [l:sort, l:codes] = l:initial_code
if l:sort == 'instance'
let l:sw = exists('*shifwidth') ? shiftwidth() : &shiftwidth
let l:indent = repeat(' ', l:sw)
call map(l:codes, 'l:indent . v:val')
endif
call append('.', l:codes)
endfunction "}}}

function! ghcmod#command#type_insert(force) "{{{
let l:path = s:buffer_path(a:force)
if empty(l:path)
Expand Down
3 changes: 3 additions & 0 deletions test/data/sig/SigFunction.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module SigFunction where

func :: [a] -> Maybe b -> (a -> b) -> (a,b)
9 changes: 9 additions & 0 deletions test/data/sig/SigInstance.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module SigInstance where

newtype D = D (Int,String)

class C a where
cInt :: a -> Int
cString :: a -> String

instance C D where
23 changes: 23 additions & 0 deletions test/test_command_sig_codegen.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
let s:unit = tinytest#new()

function! s:unit.test_command_sig_function()
edit test/data/sig/SigFunction.hs
call cursor(3, 1)
GhcModSigCodegen
call self.assert.match('^func x y f = ', getline(line('.')+1))
endfunction

function! s:unit.test_command_sig_instance()
edit test/data/sig/SigInstance.hs
call cursor(9, 1)
GhcModSigCodegen
call self.assert.match('^\s*cInt x = ', getline(line('.')+1))
call self.assert.match('^\s*cString x = ', getline(line('.')+2))
endfunction

function! s:unit.teardown()
" Discard changes
bdelete!
endfunction

call s:unit.run()

0 comments on commit 8b66db9

Please sign in to comment.