Skip to content

Commit

Permalink
jobcontrol: add AddHandler and RemoveHandler API to catch on_exit cal…
Browse files Browse the repository at this point in the history
…lback for callers of caller

cmd: add neovim integration for GoCoverage
  • Loading branch information
t-yuki authored and fatih committed Apr 2, 2016
1 parent 3efc952 commit 19cfa6f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 20 deletions.
51 changes: 31 additions & 20 deletions autoload/go/cmd.vim
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,11 @@ function! go#cmd#Test(bang, compile, ...)

if has('nvim')
if get(g:, 'go_term_enabled', 0)
call go#term#new(a:bang, ["go"] + args)
let id = go#term#new(a:bang, ["go"] + args)
else
call go#jobcontrol#Spawn(a:bang, "test", args)
let id = go#jobcontrol#Spawn(a:bang, "test", args)
endif
return
return id
endif

call go#cmd#autowrite()
Expand Down Expand Up @@ -290,29 +290,40 @@ function! go#cmd#TestFunc(bang, ...)
call call('go#cmd#Test', args)
endfunction

let s:coverage_handler_id = ''
let s:coverage_handler_jobs = {}

function! s:coverage_handler(job, exit_status, data)
if !has_key(s:coverage_handler_jobs, a:job.id)
return
endif
let l:tmpname = s:coverage_handler_jobs[a:job.id]
if a:exit_status == 0
let openHTML = 'go tool cover -html='.l:tmpname
call go#tool#ExecuteInDir(openHTML)
endif
call delete(l:tmpname)
unlet s:coverage_handler_jobs[a:job.id]
endfunction

" Coverage creates a new cover profile with 'go test -coverprofile' and opens
" a new HTML coverage page from that profile.
function! go#cmd#Coverage(bang, ...)
let l:tmpname=tempname()
let args = [a:bang, 0, "-coverprofile", l:tmpname]

let command = "go test -coverprofile=" . l:tmpname . ' ' . go#util#Shelljoin(a:000)


let l:listtype = "quickfix"
call go#cmd#autowrite()
let out = go#tool#ExecuteInDir(command)
if v:shell_error
let errors = go#tool#ParseErrors(split(out, '\n'))
call go#list#Populate(l:listtype, errors)
call go#list#Window(l:listtype, len(errors))
if !empty(errors) && !a:bang
call go#list#JumpToFirst(l:listtype)
if a:0
call extend(args, a:000)
endif
let id = call('go#cmd#Test', args)
if has('nvim')
if s:coverage_handler_id == ''
let s:coverage_handler_id = go#jobcontrol#AddHandler(function('s:coverage_handler'))
endif
else
" clear previous location list
call go#list#Clean(l:listtype)
call go#list#Window(l:listtype)

let s:coverage_handler_jobs[id] = l:tmpname
return
endif
if !v:shell_error
let openHTML = 'go tool cover -html='.l:tmpname
call go#tool#ExecuteInDir(openHTML)
endif
Expand Down
34 changes: 34 additions & 0 deletions autoload/go/jobcontrol.vim
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
" internal function s:spawn
let s:jobs = {}

" s:handlers is a global event handlers for all jobs started with Spawn() or
" with the internal function s:spawn
let s:handlers = {}

" 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).
Expand Down Expand Up @@ -36,6 +40,22 @@ function! go#jobcontrol#Statusline() abort
return ''
endfunction

" AddHandler adds a on_exit callback handler and returns the id.
function! go#jobcontrol#AddHandler(handler)
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.
function! go#jobcontrol#RemoveHandler(id)
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. spawn changes the
" GOPATH when g:go_autodetect_gopath is enabled. The job is started inside the
Expand Down Expand Up @@ -101,6 +121,7 @@ function! s:on_exit(job_id, exit_status)

let self.state = "SUCCESS"
call go#util#EchoSuccess("SUCCESS")
call s:callback_handlers_on_exit(s:jobs[a:job_id], a:exit_status, std_combined)
return
endif

Expand All @@ -120,6 +141,7 @@ function! s:on_exit(job_id, exit_status)
if !len(errors)
" failed to parse errors, output the original content
call go#util#EchoError(std_combined[0])
call s:callback_handlers_on_exit(s:jobs[a:job_id], a:exit_status, std_combined)
return
endif

Expand All @@ -132,6 +154,18 @@ function! s:on_exit(job_id, exit_status)
call go#list#JumpToFirst(l:listtype)
endif
endif
call s:callback_handlers_on_exit(s:jobs[a:job_id], a:exit_status, std_combined)
endfunction

" callback_handlers_on_exit runs all handlers for job on exit event.
function! s:callback_handlers_on_exit(job, exit_status, data)
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

" on_stdout is the stdout handler for jobstart(). It collects the output of
Expand Down

0 comments on commit 19cfa6f

Please sign in to comment.