1
0
Fork 0
mirror of synced 2024-12-27 00:53:20 -05:00
ultimate-vim/sources_non_forked/vim-go/autoload/go/jobcontrol.vim

191 lines
5.4 KiB
VimL
Raw Normal View History

2015-12-16 08:53:53 -05:00
" s:jobs is a global reference to all jobs started with Spawn() or with the
" internal function s:spawn
let s:jobs = {}
2016-04-12 04:31:09 -04:00
" s:handlers is a global event handlers for all jobs started with Spawn() or
" with the internal function s:spawn
let s:handlers = {}
2015-12-16 08:53:53 -05:00
" Spawn is a wrapper around s:spawn. It can be executed by other files and
" scripts if needed. Desc defines the description for printing the status
" during the job execution (useful for statusline integration).
function! go#jobcontrol#Spawn(bang, desc, for, args) abort
2015-12-16 08:53:53 -05:00
" autowrite is not enabled for jobs
call go#cmd#autowrite()
let job = s:spawn(a:bang, a:desc, a:for, a:args)
2015-12-16 08:53:53 -05:00
return job.id
endfunction
2016-04-12 04:31:09 -04:00
" AddHandler adds a on_exit callback handler and returns the id.
2016-12-27 09:46:49 -05:00
function! go#jobcontrol#AddHandler(handler) abort
2016-04-12 04:31:09 -04:00
let i = len(s:handlers)
while has_key(s:handlers, string(i))
let i += 1
break
endwhile
let s:handlers[string(i)] = a:handler
return string(i)
endfunction
" RemoveHandler removes a callback handler by id.
2016-12-27 09:46:49 -05:00
function! go#jobcontrol#RemoveHandler(id) abort
2016-04-12 04:31:09 -04:00
unlet s:handlers[a:id]
endfunction
" spawn spawns a go subcommand with the name and arguments with jobstart. Once a
" job is started a reference will be stored inside s:jobs. The job is started
" inside the current files folder.
function! s:spawn(bang, desc, for, args) abort
2017-02-11 08:01:38 -05:00
let status_type = a:args[0]
let status_dir = expand('%:p:h')
let started_at = reltime()
call go#statusline#Update(status_dir, {
\ 'desc': "current status",
\ 'type': status_type,
\ 'state': "started",
\})
2016-12-27 09:46:49 -05:00
let job = {
\ 'desc': a:desc,
\ 'bang': a:bang,
2015-12-16 08:53:53 -05:00
\ 'winnr': winnr(),
2017-07-06 08:57:35 -04:00
\ 'importpath': go#package#ImportPath(),
2015-12-16 08:53:53 -05:00
\ 'state': "RUNNING",
\ 'stderr' : [],
\ 'stdout' : [],
\ 'on_stdout': function('s:on_stdout'),
\ 'on_stderr': function('s:on_stderr'),
\ 'on_exit' : function('s:on_exit'),
2017-02-11 08:01:38 -05:00
\ 'status_type' : status_type,
\ 'status_dir' : status_dir,
\ 'started_at' : started_at,
\ 'for' : a:for,
2018-03-31 10:56:26 -04:00
\ 'errorformat': &errorformat,
2015-12-16 08:53:53 -05:00
\ }
" execute go build in the files directory
let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd ' : 'cd '
" cleanup previous jobs for this file
for jb in values(s:jobs)
if jb.importpath == job.importpath
unlet s:jobs[jb.id]
endif
endfor
let dir = getcwd()
2016-01-05 13:18:45 -05:00
let jobdir = fnameescape(expand("%:p:h"))
execute cd . jobdir
2015-12-16 08:53:53 -05:00
" append the subcommand, such as 'build'
let argv = ['go'] + a:args
" run, forrest, run!
let id = jobstart(argv, job)
let job.id = id
2016-01-05 13:18:45 -05:00
let job.dir = jobdir
2015-12-16 08:53:53 -05:00
let s:jobs[id] = job
execute cd . fnameescape(dir)
return job
endfunction
" on_exit is the exit handler for jobstart(). It handles cleaning up the job
" references and also displaying errors in the quickfix window collected by
" on_stderr handler. If there are no errors and a quickfix window is open,
" it'll be closed.
2016-12-27 09:46:49 -05:00
function! s:on_exit(job_id, exit_status, event) dict abort
2017-02-11 08:01:38 -05:00
let status = {
\ 'desc': 'last status',
\ 'type': self.status_type,
\ 'state': "success",
\ }
if a:exit_status
let status.state = "failed"
endif
let elapsed_time = reltimestr(reltime(self.started_at))
" strip whitespace
let elapsed_time = substitute(elapsed_time, '^\s*\(.\{-}\)\s*$', '\1', '')
let status.state .= printf(" (%ss)", elapsed_time)
call go#statusline#Update(self.status_dir, status)
2015-12-16 08:53:53 -05:00
let std_combined = self.stderr + self.stdout
2016-06-26 07:12:36 -04:00
let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd ' : 'cd '
let dir = getcwd()
execute cd self.dir
2016-04-12 04:31:09 -04:00
call s:callback_handlers_on_exit(s:jobs[a:job_id], a:exit_status, std_combined)
let l:listtype = go#list#Type(self.for)
2016-01-05 13:18:45 -05:00
if a:exit_status == 0
2016-12-27 09:46:49 -05:00
call go#list#Clean(l:listtype)
2015-12-16 08:53:53 -05:00
let self.state = "SUCCESS"
2017-02-11 08:01:38 -05:00
if get(g:, 'go_echo_command_info', 1)
call go#util#EchoSuccess("[" . self.status_type . "] SUCCESS")
endif
2016-06-26 07:12:36 -04:00
execute cd . fnameescape(dir)
2015-12-16 08:53:53 -05:00
return
endif
2016-01-05 13:18:45 -05:00
let self.state = "FAILED"
2017-02-11 08:01:38 -05:00
if get(g:, 'go_echo_command_info', 1)
call go#util#EchoError("[" . self.status_type . "] FAILED")
endif
2015-12-16 08:53:53 -05:00
2018-03-31 10:56:26 -04:00
" parse the errors relative to self.jobdir
call go#list#ParseFormat(l:listtype, self.errorformat, std_combined, self.for)
let errors = go#list#Get(l:listtype)
2016-06-26 07:12:36 -04:00
execute cd . fnameescape(dir)
2015-12-16 08:53:53 -05:00
2016-01-05 13:18:45 -05:00
if !len(errors)
" failed to parse errors, output the original content
call go#util#EchoError(std_combined[0])
2015-12-16 08:53:53 -05:00
return
endif
" if we are still in the same windows show the list
if self.winnr == winnr()
2016-02-20 08:13:10 -05:00
call go#list#Window(l:listtype, len(errors))
2016-01-05 13:18:45 -05:00
if !empty(errors) && !self.bang
2016-02-20 08:13:10 -05:00
call go#list#JumpToFirst(l:listtype)
2016-01-05 13:18:45 -05:00
endif
2015-12-16 08:53:53 -05:00
endif
endfunction
2016-04-12 04:31:09 -04:00
" callback_handlers_on_exit runs all handlers for job on exit event.
2016-12-27 09:46:49 -05:00
function! s:callback_handlers_on_exit(job, exit_status, data) abort
2016-04-12 04:31:09 -04:00
if empty(s:handlers)
return
endif
for s:handler in values(s:handlers)
call s:handler(a:job, a:exit_status, a:data)
endfor
endfunction
2015-12-16 08:53:53 -05:00
" on_stdout is the stdout handler for jobstart(). It collects the output of
2016-12-27 09:46:49 -05:00
" stderr and stores them to the jobs internal stdout list.
2017-02-11 08:01:38 -05:00
function! s:on_stdout(job_id, data, event) dict abort
2015-12-16 08:53:53 -05:00
call extend(self.stdout, a:data)
endfunction
" on_stderr is the stderr handler for jobstart(). It collects the output of
" stderr and stores them to the jobs internal stderr list.
2017-02-11 08:01:38 -05:00
function! s:on_stderr(job_id, data, event) dict abort
2015-12-16 08:53:53 -05:00
call extend(self.stderr, a:data)
endfunction
2016-06-26 07:12:36 -04:00
" vim: sw=2 ts=2 et