Skip to content

Commit db5c6a8

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 variable or operator at the cursor. The type signature is inserted right above the definition of top level variable or operator.
1 parent ab33578 commit db5c6a8

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

autoload/hdevtools.vim

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,89 @@ 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 "
571+
\ . "hdevtools.vim doesn't support running on "
572+
\ . "an unnamed buffer."
573+
\ )
574+
return
575+
endif
576+
577+
if &l:modified
578+
call hdevtools#print_warning( 'hdevtools#insert_type: '
579+
\ . 'The buffer has been modified but not written.'
580+
\ )
581+
endif
582+
583+
let l:line = line('.')
584+
let l:col = col('.')
585+
let l:get_pos_of_identifier = hdevtools#build_command_bare
586+
\ ( 'type'
587+
\ , shellescape(l:file) . ' ' . l:line . ' ' . l:col
588+
\ )
589+
let l:pos_raw = system(l:get_pos_of_identifier)
590+
let l:pos_lines = split(l:pos_raw, '\n')
591+
592+
if v:shell_error != 0
593+
call hdevtools#print_error( 'hdevtools#insert_type: No Type Information. '
594+
\ . 'hdevtools answered:'
595+
\ )
596+
for l:line in pos_lines
597+
call hdevtools#print_error(l:line)
598+
endfor
599+
return
600+
endif
601+
602+
if len(l:pos_lines) == 0
603+
call hdevtools#print_error('hdevtools#insert_type: No Type Information.')
604+
return
605+
endif
606+
let l:pos_line = l:pos_lines[-1]
607+
let l:pos_parsed = matchlist(l:pos_line, '\(\d\+\) \(\d\+\) \(\d\+\) \(\d\+\) "\([^"]\+\)"')
608+
if len(l:pos_parsed) == 0
609+
call hdevtools#print_error( 'hdevtools#insert_type: No Type Information.'
610+
\ . 'hdevtools answered:'
611+
\ )
612+
for l:line in l:pos_lines
613+
call hdevtools#print_error(l:line)
614+
endfor
615+
return
616+
endif
617+
let l:id_linenumber = l:pos_parsed[1]
618+
let l:id_colmnumber = l:pos_parsed[2]
619+
let l:id_line = getline(l:id_linenumber)
620+
let l:identifier = s:extract_identifier(l:id_line, l:id_colmnumber)
621+
622+
if l:id_colmnumber != 1
623+
call hdevtools#print_error( 'hdevtools#insert_type: `' . l:identifier
624+
\ . '` is not a top level variable identifier.'
625+
\ )
626+
return
627+
endif
628+
let l:get_type_of_identifier = hdevtools#build_command_bare
629+
\ ( 'info'
630+
\ , shellescape(l:file) . ' ' . shellescape(l:identifier)
631+
\ )
632+
let l:output_for_type = system(l:get_type_of_identifier)
633+
634+
if v:shell_error != 0
635+
call hdevtools#print_error( "hdevtools#insert_type: Couldn't get type for "
636+
\ . l:identifier . '. hdevtools answered:'
637+
\ )
638+
for l:line in split(l:output_for_type, '\n')
639+
call hdevtools#print_error(l:line)
640+
endfor
641+
return
642+
endif
643+
644+
let l:type_lines = split(l:output_for_type, '\n')
645+
let l:type_definition = l:type_lines[0]
646+
647+
execute(l:id_linenumber . "pu!='" . l:type_definition . "'")
648+
endfunction
649+
567650
function! hdevtools#print_error(msg)
568651
echohl ErrorMsg
569652
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)