Skip to content

Commit 5e531bd

Browse files
Implement "insert type signature of f at cursor".
Implement the function hdevtools#insert_type() which inserts the type signature of the top level expression at the cursor. The type signature is inserted right above the top level expression.
1 parent ab33578 commit 5e531bd

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

autoload/hdevtools.vim

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,56 @@ function! hdevtools#type_clear()
564564
endif
565565
endfunction
566566

567+
function! hdevtools#insert_type()
568+
let l:file = expand('%')
569+
if l:file ==# ''
570+
call hdevtools#print_warning("hdevtool#insert_type: current version of hdevtools.vim doesn't support running on an unnamed buffer.")
571+
return
572+
endif
573+
574+
if &l:modified
575+
call hdevtools#print_warning('hdevtools#insert_type: the buffer has been modified but not written')
576+
endif
577+
578+
let l:line = line('.')
579+
let l:col = col('.')
580+
let l:cmd_to_get_pos_of_identifier = hdevtools#build_command_bare('type', shellescape(l:file) . ' ' . l:line . ' ' . l:col)
581+
let l:output_for_pos = system(l:cmd_to_get_pos_of_identifier)
582+
583+
if v:shell_error != 0
584+
for l:line in split(l:output_for_pos, '\n')
585+
call hdevtools#print_error(l:line)
586+
endfor
587+
return
588+
endif
589+
590+
let l:pos_lines = split(l:output_for_pos, '\n')
591+
let l:pos_last_line = l:pos_lines[-1]
592+
let l:pos_last_line_parsed = matchlist(l:pos_last_line, '\(\d\+\) \(\d\+\) \(\d\+\) \(\d\+\) "\([^"]\+\)"')
593+
if len(l:pos_last_line_parsed) == 0
594+
call hdevtools#print_error('hdevtools#insert_type: No Type Information. hdevtools answered: ' . l:last_line)
595+
return
596+
endif
597+
let l:linenumber_of_identifier = l:pos_last_line_parsed[1]
598+
let l:string_at_linenumber = getline(l:linenumber_of_identifier)
599+
let l:identifier = (split(l:string_at_linenumber))[0]
600+
601+
let l:cmd_to_get_type_of_identifier = hdevtools#build_command_bare('info', shellescape(l:file) . ' ' . l:identifier)
602+
let l:output_for_type = system(l:cmd_to_get_type_of_identifier)
603+
604+
if v:shell_error != 0
605+
for l:line in split(l:output_for_type, '\n')
606+
call hdevtools#print_error(l:line)
607+
endfor
608+
return
609+
endif
610+
611+
let l:type_lines = split(l:output_for_type, '\n')
612+
let l:type_definition = l:type_lines[0]
613+
614+
execute(l:linenumber_of_identifier . "pu!='" . l:type_definition . "'")
615+
endfunction
616+
567617
function! hdevtools#print_error(msg)
568618
echohl ErrorMsg
569619
echomsg a:msg

ftplugin/haskell/hdevtools.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ nnoremap <buffer> <silent> <C-W><C-F> :call hdevtools#go_file("sp")<CR>
4141
command! -buffer -nargs=0 HdevtoolsType echo hdevtools#type()[1]
4242
command! -buffer -nargs=0 HdevtoolsClear call hdevtools#type_clear()
4343
command! -buffer -nargs=? HdevtoolsInfo call hdevtools#info(<q-args>)
44+
command! -buffer -nargs=0 HdevtoolsInsertType call hdevtools#insert_type()
4445

4546
let b:undo_ftplugin .= join(map([
4647
\ 'HdevtoolsType',

0 commit comments

Comments
 (0)