Skip to content

Implement "insert type definition of outer function under cursor". #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions autoload/hdevtools.vim
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,89 @@ function! hdevtools#type_clear()
endif
endfunction

function! hdevtools#insert_type()
let l:file = expand('%')
if l:file ==# ''
call hdevtools#print_warning( "hdevtool#insert_type: current version of "
\ . "hdevtools.vim doesn't support running on "
\ . "an unnamed buffer."
\ )
return
endif

if &l:modified
call hdevtools#print_warning( 'hdevtools#insert_type: '
\ . 'The buffer has been modified but not written.'
\ )
endif

let l:line = line('.')
let l:col = col('.')
let l:get_pos_of_identifier = hdevtools#build_command_bare
\ ( 'type'
\ , shellescape(l:file) . ' ' . l:line . ' ' . l:col
\ )
let l:pos_raw = system(l:get_pos_of_identifier)
let l:pos_lines = split(l:pos_raw, '\n')

if v:shell_error != 0
call hdevtools#print_error( 'hdevtools#insert_type: No Type Information. '
\ . 'hdevtools answered:'
\ )
for l:line in pos_lines
call hdevtools#print_error(l:line)
endfor
return
endif

if len(l:pos_lines) == 0
call hdevtools#print_error('hdevtools#insert_type: No Type Information.')
return
endif
let l:pos_line = l:pos_lines[-1]
let l:pos_parsed = matchlist(l:pos_line, '\(\d\+\) \(\d\+\) \(\d\+\) \(\d\+\) "\([^"]\+\)"')
if len(l:pos_parsed) == 0
call hdevtools#print_error( 'hdevtools#insert_type: No Type Information.'
\ . 'hdevtools answered:'
\ )
for l:line in l:pos_lines
call hdevtools#print_error(l:line)
endfor
return
endif
let l:id_linenumber = l:pos_parsed[1]
let l:id_colmnumber = l:pos_parsed[2]
let l:id_line = getline(l:id_linenumber)
let l:identifier = s:extract_identifier(l:id_line, l:id_colmnumber)

if l:id_colmnumber != 1
call hdevtools#print_error( 'hdevtools#insert_type: `' . l:identifier
\ . '` is not a top level variable identifier.'
\ )
return
endif
let l:get_type_of_identifier = hdevtools#build_command_bare
\ ( 'info'
\ , shellescape(l:file) . ' ' . shellescape(l:identifier)
\ )
let l:output_for_type = system(l:get_type_of_identifier)

if v:shell_error != 0
call hdevtools#print_error( "hdevtools#insert_type: Couldn't get type for "
\ . l:identifier . '. hdevtools answered:'
\ )
for l:line in split(l:output_for_type, '\n')
call hdevtools#print_error(l:line)
endfor
return
endif

let l:type_lines = split(l:output_for_type, '\n')
let l:type_definition = l:type_lines[0]

execute(l:id_linenumber . "pu!='" . l:type_definition . "'")
endfunction

function! hdevtools#print_error(msg)
echohl ErrorMsg
echomsg a:msg
Expand Down
1 change: 1 addition & 0 deletions ftplugin/haskell/hdevtools.vim
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ nnoremap <buffer> <silent> <C-W><C-F> :call hdevtools#go_file("sp")<CR>
command! -buffer -nargs=0 HdevtoolsType echo hdevtools#type()[1]
command! -buffer -nargs=0 HdevtoolsClear call hdevtools#type_clear()
command! -buffer -nargs=? HdevtoolsInfo call hdevtools#info(<q-args>)
command! -buffer -nargs=0 HdevtoolsInsertType call hdevtools#insert_type()

let b:undo_ftplugin .= join(map([
\ 'HdevtoolsType',
Expand Down