2018-12-17 06:28:27 -05:00
|
|
|
" don't spam the user when Vim is started in Vi compatibility mode
|
|
|
|
let s:cpo_save = &cpo
|
|
|
|
set cpo&vim
|
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
function! go#rename#Rename(bang, ...) abort
|
|
|
|
let to_identifier = ""
|
2016-06-26 07:12:36 -04:00
|
|
|
if a:0 == 0
|
2017-11-24 08:54:40 -05:00
|
|
|
let ask = printf("vim-go: rename '%s' to: ", expand("<cword>"))
|
2018-06-14 06:31:12 -04:00
|
|
|
let prefill = go#config#GorenamePrefill()
|
|
|
|
if prefill != ''
|
|
|
|
let to_identifier = input(ask, eval(prefill))
|
2014-10-31 17:30:24 -04:00
|
|
|
else
|
2016-12-27 09:46:49 -05:00
|
|
|
let to_identifier = input(ask)
|
2014-10-31 17:30:24 -04:00
|
|
|
endif
|
2016-06-26 07:12:36 -04:00
|
|
|
redraw!
|
2016-12-27 09:46:49 -05:00
|
|
|
if empty(to_identifier)
|
2016-06-26 07:12:36 -04:00
|
|
|
return
|
2014-10-31 17:30:24 -04:00
|
|
|
endif
|
2016-06-26 07:12:36 -04:00
|
|
|
else
|
2016-12-27 09:46:49 -05:00
|
|
|
let to_identifier = a:1
|
2016-06-26 07:12:36 -04:00
|
|
|
endif
|
2014-10-31 17:30:24 -04:00
|
|
|
|
2019-11-16 10:28:42 -05:00
|
|
|
let l:bin = go#config#GorenameCommand()
|
|
|
|
|
2017-11-24 08:54:40 -05:00
|
|
|
" return with a warning if the bin doesn't exist
|
2019-11-16 10:28:42 -05:00
|
|
|
let bin_path = go#path#CheckBinPath(l:bin)
|
2016-06-26 07:12:36 -04:00
|
|
|
if empty(bin_path)
|
|
|
|
return
|
|
|
|
endif
|
2014-10-31 17:30:24 -04:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
let fname = expand('%:p')
|
|
|
|
let pos = go#util#OffsetCursor()
|
2016-12-27 09:46:49 -05:00
|
|
|
let offset = printf('%s:#%d', fname, pos)
|
2019-11-16 10:28:42 -05:00
|
|
|
|
|
|
|
let args = []
|
|
|
|
if l:bin == 'gorename'
|
|
|
|
let l:args = extend(l:args, ['-tags', go#config#BuildTags(), '-offset', offset, '-to', to_identifier])
|
|
|
|
elseif l:bin == 'gopls'
|
|
|
|
" TODO(bc): use -tags when gopls supports it
|
|
|
|
let l:args = extend(l:args, ['rename', '-w', l:offset, to_identifier])
|
|
|
|
else
|
|
|
|
call go#util#EchoWarning('unexpected rename command')
|
|
|
|
endif
|
|
|
|
|
|
|
|
let l:cmd = extend([l:bin], l:args)
|
2017-03-14 11:16:07 -04:00
|
|
|
|
2018-09-24 20:40:17 -04:00
|
|
|
if go#util#has_job()
|
2016-12-27 09:46:49 -05:00
|
|
|
call s:rename_job({
|
|
|
|
\ 'cmd': cmd,
|
|
|
|
\ 'bang': a:bang,
|
|
|
|
\})
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2019-03-08 06:04:56 -05:00
|
|
|
let [l:out, l:err] = go#util#ExecInDir(l:cmd)
|
2018-06-14 06:31:12 -04:00
|
|
|
call s:parse_errors(l:err, a:bang, split(l:out, '\n'))
|
2016-12-27 09:46:49 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function s:rename_job(args)
|
2018-08-25 12:13:42 -04:00
|
|
|
let l:job_opts = {
|
|
|
|
\ 'bang': a:args.bang,
|
|
|
|
\ 'for': 'GoRename',
|
|
|
|
\ 'statustype': 'gorename',
|
|
|
|
\ }
|
2016-12-27 09:46:49 -05:00
|
|
|
|
2018-08-25 12:13:42 -04:00
|
|
|
" autowrite is not enabled for jobs
|
|
|
|
call go#cmd#autowrite()
|
|
|
|
let l:cbs = go#job#Options(l:job_opts)
|
2018-02-04 06:35:08 -05:00
|
|
|
|
2018-08-25 12:13:42 -04:00
|
|
|
" wrap l:cbs.exit_cb in s:exit_cb.
|
|
|
|
let l:cbs.exit_cb = funcref('s:exit_cb', [l:cbs.exit_cb])
|
2018-02-04 06:35:08 -05:00
|
|
|
|
2018-08-25 12:13:42 -04:00
|
|
|
call go#job#Start(a:args.cmd, l:cbs)
|
|
|
|
endfunction
|
2016-12-27 09:46:49 -05:00
|
|
|
|
2018-08-25 12:13:42 -04:00
|
|
|
function! s:reload_changed() abort
|
|
|
|
" reload all files to reflect the new changes. We explicitly call
|
|
|
|
" checktime to trigger a reload of all files. See
|
|
|
|
" http://www.mail-archive.com/vim@vim.org/msg05900.html for more info
|
|
|
|
" about the autoread bug
|
|
|
|
let current_autoread = &autoread
|
|
|
|
set autoread
|
|
|
|
silent! checktime
|
|
|
|
let &autoread = current_autoread
|
|
|
|
endfunction
|
2016-12-27 09:46:49 -05:00
|
|
|
|
2018-08-25 12:13:42 -04:00
|
|
|
" s:exit_cb reloads any changed buffers and then calls next.
|
|
|
|
function! s:exit_cb(next, job, exitval) abort
|
|
|
|
call s:reload_changed()
|
|
|
|
call call(a:next, [a:job, a:exitval])
|
2016-12-27 09:46:49 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function s:parse_errors(exit_val, bang, out)
|
2016-06-26 07:12:36 -04:00
|
|
|
" reload all files to reflect the new changes. We explicitly call
|
|
|
|
" checktime to trigger a reload of all files. See
|
|
|
|
" http://www.mail-archive.com/vim@vim.org/msg05900.html for more info
|
|
|
|
" about the autoread bug
|
|
|
|
let current_autoread = &autoread
|
|
|
|
set autoread
|
|
|
|
silent! checktime
|
|
|
|
let &autoread = current_autoread
|
2014-10-31 17:30:24 -04:00
|
|
|
|
2017-11-24 08:54:40 -05:00
|
|
|
let l:listtype = go#list#Type("GoRename")
|
2016-12-27 09:46:49 -05:00
|
|
|
if a:exit_val != 0
|
2019-03-08 06:04:56 -05:00
|
|
|
let errors = go#util#ParseErrors(a:out)
|
2016-12-27 09:46:49 -05:00
|
|
|
call go#list#Populate(l:listtype, errors, 'Rename')
|
2016-06-26 07:12:36 -04:00
|
|
|
call go#list#Window(l:listtype, len(errors))
|
|
|
|
if !empty(errors) && !a:bang
|
|
|
|
call go#list#JumpToFirst(l:listtype)
|
|
|
|
elseif empty(errors)
|
|
|
|
" failed to parse errors, output the original content
|
2017-11-24 08:54:40 -05:00
|
|
|
call go#util#EchoError(a:out)
|
2014-10-31 17:30:24 -04:00
|
|
|
endif
|
2016-12-27 09:46:49 -05:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
return
|
|
|
|
endif
|
2014-10-31 17:30:24 -04:00
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
" strip out newline on the end that gorename puts. If we don't remove, it
|
|
|
|
" will trigger the 'Hit ENTER to continue' prompt
|
|
|
|
call go#list#Clean(l:listtype)
|
|
|
|
call go#util#EchoSuccess(a:out[0])
|
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
" refresh the buffer so we can see the new content
|
|
|
|
silent execute ":e"
|
2014-10-31 17:30:24 -04:00
|
|
|
endfunction
|
|
|
|
|
2017-11-24 08:54:40 -05:00
|
|
|
" Commandline completion: original, unexported camelCase, and exported
|
|
|
|
" CamelCase.
|
|
|
|
function! go#rename#Complete(lead, cmdline, cursor)
|
|
|
|
let l:word = expand('<cword>')
|
|
|
|
return filter(uniq(sort(
|
|
|
|
\ [l:word, go#util#camelcase(l:word), go#util#pascalcase(l:word)])),
|
|
|
|
\ 'strpart(v:val, 0, len(a:lead)) == a:lead')
|
|
|
|
endfunction
|
|
|
|
|
2018-12-17 06:28:27 -05:00
|
|
|
" restore Vi compatibility settings
|
|
|
|
let &cpo = s:cpo_save
|
|
|
|
unlet s:cpo_save
|
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
" vim: sw=2 ts=2 et
|