1
0
Fork 0
mirror of synced 2024-11-23 17:25:35 -05:00
ultimate-vim/sources_non_forked/vim-go/autoload/go/jobcontrol.vim

177 lines
4.5 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 = {}
" 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).
2016-01-05 13:18:45 -05:00
function! go#jobcontrol#Spawn(bang, desc, args)
2015-12-16 08:53:53 -05:00
" autowrite is not enabled for jobs
call go#cmd#autowrite()
2016-01-05 13:18:45 -05:00
let job = s:spawn(a:bang, a:desc, a:args)
2015-12-16 08:53:53 -05:00
return job.id
endfunction
" Statusline returns the current status of the job
function! go#jobcontrol#Statusline() abort
if empty(s:jobs)
return ''
endif
let import_path = go#package#ImportPath(expand('%:p:h'))
for job in values(s:jobs)
if job.importpath != import_path
continue
endif
if job.state == "SUCCESS"
return ''
endif
return printf("%s ... [%s]", job.desc, job.state)
endfor
return ''
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. spawn changes the
" GOPATH when g:go_autodetect_gopath is enabled. The job is started inside the
" current files folder.
2016-01-05 13:18:45 -05:00
function! s:spawn(bang, desc, args)
2015-12-16 08:53:53 -05:00
let job = {
\ 'desc': a:desc,
2016-01-05 13:18:45 -05:00
\ 'bang': a:bang,
2015-12-16 08:53:53 -05:00
\ 'winnr': winnr(),
\ 'importpath': go#package#ImportPath(expand('%:p:h')),
\ 'state': "RUNNING",
\ 'stderr' : [],
\ 'stdout' : [],
\ 'on_stdout': function('s:on_stdout'),
\ 'on_stderr': function('s:on_stderr'),
\ 'on_exit' : function('s:on_exit'),
\ }
" modify GOPATH if needed
let old_gopath = $GOPATH
let $GOPATH = go#path#Detect()
" 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)
" restore back GOPATH
let $GOPATH = old_gopath
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-01-05 13:18:45 -05:00
function! s:on_exit(job_id, exit_status)
2015-12-16 08:53:53 -05:00
let std_combined = self.stderr + self.stdout
2016-01-05 13:18:45 -05:00
if a:exit_status == 0
2015-12-16 08:53:53 -05:00
call go#list#Clean()
call go#list#Window()
let self.state = "SUCCESS"
return
endif
2016-01-05 13:18:45 -05:00
let self.state = "FAILED"
2015-12-16 08:53:53 -05:00
2016-01-05 13:18:45 -05:00
let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd ' : 'cd '
let dir = getcwd()
try
execute cd self.dir
let errors = go#tool#ParseErrors(std_combined)
let errors = go#tool#FilterValids(errors)
finally
execute cd . fnameescape(dir)
endtry
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()
call go#list#Populate(errors)
call go#list#Window(len(errors))
2016-01-05 13:18:45 -05:00
if !empty(errors) && !self.bang
call go#list#JumpToFirst()
endif
2015-12-16 08:53:53 -05:00
endif
endfunction
" on_stdout is the stdout handler for jobstart(). It collects the output of
" stderr and stores them to the jobs internal stdout list.
function! s:on_stdout(job_id, data)
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.
function! s:on_stderr(job_id, data)
call extend(self.stderr, a:data)
endfunction
" abort_all aborts all current jobs created with s:spawn()
function! s:abort_all()
if empty(s:jobs)
return
endif
for id in keys(s:jobs)
if id > 0
silent! call jobstop(id)
endif
endfor
let s:jobs = {}
endfunction
" abort aborts the job with the given name, where name is the first argument
" passed to s:spawn()
function! s:abort(path)
if empty(s:jobs)
return
endif
for job in values(s:jobs)
if job.importpath == path && job.id > 0
silent! call jobstop(job.id)
unlet s:jobs['job.id']
endif
endfor
endfunction
" vim:ts=2:sw=2:et