vimrc/sources_non_forked/vim-go/autoload/go/keyify.vim

65 lines
1.6 KiB
VimL
Raw Normal View History

2018-12-17 11:28:27 +00:00
" don't spam the user when Vim is started in Vi compatibility mode
let s:cpo_save = &cpo
set cpo&vim
2017-05-26 09:30:32 +00:00
function! go#keyify#Keyify()
2018-06-14 10:31:12 +00:00
" Needs: https://github.com/dominikh/go-tools/pull/272
"\ '-tags', go#config#BuildTags(),
let l:cmd = ['keyify',
\ '-json',
\ printf('%s:#%s', fnamemodify(expand('%'), ':p:gs?\\?/?'), go#util#OffsetCursor())]
let [l:out, l:err] = go#util#Exec(l:cmd)
if l:err
call go#util#EchoError("non-zero exit code: " . l:out)
2017-05-26 09:30:32 +00:00
return
endif
2018-06-14 10:31:12 +00:00
silent! let result = json_decode(l:out)
2017-05-26 09:30:32 +00:00
" We want to output the error message in case the result isn't a JSON
if type(result) != type({})
2018-06-14 10:31:12 +00:00
call go#util#EchoError(s:chomp(l:out))
2017-05-26 09:30:32 +00:00
return
endif
" Because keyify returns the byte before the region we want, we goto the
" byte after that
execute "goto" result.start + 1
let start = getpos('.')
execute "goto" result.end
let end = getpos('.')
let vis_start = getpos("'<")
let vis_end = getpos("'>")
" Replace contents between start and end with `replacement`
call setpos("'<", start)
call setpos("'>", end)
let select = 'gv'
" Make sure the visual mode is 'v', to avoid some bugs
normal! gv
if mode() !=# 'v'
let select .= 'v'
endif
silent! execute "normal!" select."\"=result.replacement\<cr>p"
" Replacement text isn't aligned, so it needs fix
normal! '<v'>=
call setpos("'<", vis_start)
call setpos("'>", vis_end)
endfunction
function! s:chomp(string)
return substitute(a:string, '\n\+$', '', '')
endfunction
2018-12-17 11:28:27 +00:00
" restore Vi compatibility settings
let &cpo = s:cpo_save
unlet s:cpo_save
" vim: sw=2 ts=2 et