1
0
Fork 0
mirror of synced 2024-12-22 15:08:57 -05:00
ultimate-vim/sources_non_forked/vim-go/autoload/go/rename.vim

182 lines
5.1 KiB
VimL
Raw Normal View History

2014-10-31 17:30:24 -04:00
if !exists("g:go_gorename_bin")
2016-06-26 07:12:36 -04:00
let g:go_gorename_bin = "gorename"
2014-10-31 17:30:24 -04:00
endif
" Set the default value. A value of "1" is a shortcut for this, for
" compatibility reasons.
function! s:default() abort
if !exists("g:go_gorename_prefill") || g:go_gorename_prefill == 1
let g:go_gorename_prefill = 'expand("<cword>") =~# "^[A-Z]"' .
\ '? go#util#pascalcase(expand("<cword>"))' .
\ ': go#util#camelcase(expand("<cword>"))'
endif
endfunction
call s:default()
2016-02-20 08:13:10 -05:00
2016-12-27 09:46:49 -05:00
function! go#rename#Rename(bang, ...) abort
call s:default()
2016-12-27 09:46:49 -05:00
let to_identifier = ""
2016-06-26 07:12:36 -04:00
if a:0 == 0
let ask = printf("vim-go: rename '%s' to: ", expand("<cword>"))
if g:go_gorename_prefill != ''
let to_identifier = input(ask, eval(g:go_gorename_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
" return with a warning if the bin doesn't exist
2016-06-26 07:12:36 -04:00
let bin_path = go#path#CheckBinPath(g:go_gorename_bin)
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)
2014-10-31 17:30:24 -04:00
2016-12-27 09:46:49 -05:00
" no need to escape for job call
let bin_path = go#util#has_job() ? bin_path : shellescape(bin_path)
let offset = go#util#has_job() ? offset : shellescape(offset)
let to_identifier = go#util#has_job() ? to_identifier : shellescape(to_identifier)
2016-06-11 09:56:50 -04:00
2016-12-27 09:46:49 -05:00
let cmd = [bin_path, "-offset", offset, "-to", to_identifier]
2017-03-14 11:16:07 -04:00
" check for any tags
if exists('g:go_build_tags')
let tags = get(g:, 'go_build_tags')
call extend(cmd, ["-tags", tags])
endif
2016-12-27 09:46:49 -05:00
if go#util#has_job()
call go#util#EchoProgress(printf("renaming to '%s' ...", to_identifier))
call s:rename_job({
\ 'cmd': cmd,
\ 'bang': a:bang,
\})
return
endif
let command = join(cmd, " ")
let out = go#tool#ExecuteInDir(command)
let splitted = split(out, '\n')
call s:parse_errors(go#util#ShellError(), a:bang, splitted)
endfunction
function s:rename_job(args)
2018-03-31 10:56:26 -04:00
let state = {
\ 'exited': 0,
\ 'closed': 0,
\ 'exitval': 0,
\ 'messages': [],
\ 'status_dir': expand('%:p:h'),
\ 'bang': a:args.bang
\ }
function! s:callback(chan, msg) dict
call add(self.messages, a:msg)
2016-12-27 09:46:49 -05:00
endfunction
2018-03-31 10:56:26 -04:00
function! s:exit_cb(job, exitval) dict
let self.exited = 1
let self.exitval = a:exitval
2018-02-04 06:35:08 -05:00
2016-12-27 09:46:49 -05:00
let status = {
\ 'desc': 'last status',
\ 'type': "gorename",
\ 'state': "finished",
\ }
2017-05-02 08:42:08 -04:00
if a:exitval
2016-12-27 09:46:49 -05:00
let status.state = "failed"
endif
2018-03-31 10:56:26 -04:00
call go#statusline#Update(self.status_dir, status)
2016-12-27 09:46:49 -05:00
2018-03-31 10:56:26 -04:00
if self.closed
call s:parse_errors(self.exitval, self.bang, self.messages)
2018-02-04 06:35:08 -05:00
endif
endfunction
2018-03-31 10:56:26 -04:00
function! s:close_cb(ch) dict
let self.closed = 1
2018-02-04 06:35:08 -05:00
2018-03-31 10:56:26 -04:00
if self.exited
call s:parse_errors(self.exitval, self.bang, self.messages)
2018-02-04 06:35:08 -05:00
endif
2016-12-27 09:46:49 -05:00
endfunction
2018-03-31 10:56:26 -04:00
" explicitly bind the callbacks to state so that self within them always
" refers to state. See :help Partial for more information.
2016-12-27 09:46:49 -05:00
let start_options = {
2018-03-31 10:56:26 -04:00
\ 'callback': funcref("s:callback", [], state),
\ 'exit_cb': funcref("s:exit_cb", [], state),
\ 'close_cb': funcref("s:close_cb", [], state),
2016-12-27 09:46:49 -05:00
\ }
2018-03-31 10:56:26 -04:00
call go#statusline#Update(state.status_dir, {
2016-12-27 09:46:49 -05:00
\ 'desc': "current status",
\ 'type': "gorename",
\ 'state': "started",
\})
call job_start(a:args.cmd, start_options)
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
let l:listtype = go#list#Type("GoRename")
2016-12-27 09:46:49 -05:00
if a:exit_val != 0
call go#util#EchoError("FAILED")
let errors = go#tool#ParseErrors(a:out)
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
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
" TODO(arslan): also find all other buffers and refresh them too. For this
" we need a way to get the list of changes from gorename upon an success
" change.
silent execute ":e"
2014-10-31 17:30:24 -04:00
endfunction
" 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
2016-06-26 07:12:36 -04:00
" vim: sw=2 ts=2 et