1
0
Fork 0
mirror of synced 2024-11-13 20:58:57 -05:00
ultimate-vim/sources_non_forked/vim-go/autoload/go/term.vim

146 lines
3.5 KiB
VimL
Raw Normal View History

2015-12-16 08:53:53 -05:00
if has('nvim') && !exists("g:go_term_mode")
2016-06-26 07:12:36 -04:00
let g:go_term_mode = 'vsplit'
2015-12-16 08:53:53 -05:00
endif
" s:jobs is a global reference to all jobs started with new()
let s:jobs = {}
" new creates a new terminal with the given command. Mode is set based on the
" global variable g:go_term_mode, which is by default set to :vsplit
2016-12-27 09:46:49 -05:00
function! go#term#new(bang, cmd) abort
2016-06-26 07:12:36 -04:00
return go#term#newmode(a:bang, a:cmd, g:go_term_mode)
2015-12-16 08:53:53 -05:00
endfunction
" new creates a new terminal with the given command and window mode.
2016-12-27 09:46:49 -05:00
function! go#term#newmode(bang, cmd, mode) abort
2016-06-26 07:12:36 -04:00
let mode = a:mode
if empty(mode)
let mode = g:go_term_mode
endif
2015-12-16 08:53:53 -05:00
2016-06-26 07:12:36 -04:00
" modify GOPATH if needed
let old_gopath = $GOPATH
let $GOPATH = go#path#Detect()
2015-12-16 08:53:53 -05:00
2016-06-26 07:12:36 -04:00
" execute go build in the files directory
2017-07-06 08:57:35 -04:00
let l:winnr = winnr()
2016-06-26 07:12:36 -04:00
let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd ' : 'cd '
let dir = getcwd()
2015-12-16 08:53:53 -05:00
2016-06-26 07:12:36 -04:00
execute cd . fnameescape(expand("%:p:h"))
2015-12-16 08:53:53 -05:00
2016-06-26 07:12:36 -04:00
execute mode.' __go_term__'
2015-12-16 08:53:53 -05:00
2016-06-26 07:12:36 -04:00
setlocal filetype=goterm
setlocal bufhidden=delete
setlocal winfixheight
setlocal noswapfile
setlocal nobuflisted
2015-12-16 08:53:53 -05:00
2016-12-27 09:46:49 -05:00
let job = {
2016-06-26 07:12:36 -04:00
\ 'stderr' : [],
\ 'stdout' : [],
\ 'bang' : a:bang,
\ 'on_stdout': function('s:on_stdout'),
\ 'on_stderr': function('s:on_stderr'),
\ 'on_exit' : function('s:on_exit'),
\ }
2015-12-16 08:53:53 -05:00
2016-06-26 07:12:36 -04:00
let id = termopen(a:cmd, job)
2015-12-16 08:53:53 -05:00
2017-07-06 08:57:35 -04:00
if l:winnr !=# winnr()
exe l:winnr . "wincmd w"
endif
2016-06-26 07:12:36 -04:00
execute cd . fnameescape(dir)
2015-12-16 08:53:53 -05:00
2016-06-26 07:12:36 -04:00
" restore back GOPATH
let $GOPATH = old_gopath
2015-12-16 08:53:53 -05:00
2016-06-26 07:12:36 -04:00
let job.id = id
2016-12-27 09:46:49 -05:00
let job.cmd = a:cmd
2016-06-26 07:12:36 -04:00
startinsert
2015-12-16 08:53:53 -05:00
2016-06-26 07:12:36 -04:00
" resize new term if needed.
let height = get(g:, 'go_term_height', winheight(0))
let width = get(g:, 'go_term_width', winwidth(0))
2015-12-16 08:53:53 -05:00
2016-06-26 07:12:36 -04:00
" we are careful how to resize. for example it's vertical we don't change
" the height. The below command resizes the buffer
if a:mode == "split"
exe 'resize ' . height
elseif a:mode == "vertical"
exe 'vertical resize ' . width
endif
2015-12-16 08:53:53 -05:00
2016-06-26 07:12:36 -04:00
" we also need to resize the pty, so there you go...
call jobresize(id, width, height)
2015-12-16 08:53:53 -05:00
2016-06-26 07:12:36 -04:00
let s:jobs[id] = job
stopinsert
2016-06-26 07:12:36 -04:00
return id
2015-12-16 08:53:53 -05:00
endfunction
2016-12-27 09:46:49 -05:00
function! s:on_stdout(job_id, data, event) dict abort
2016-06-26 07:12:36 -04:00
if !has_key(s:jobs, a:job_id)
return
endif
let job = s:jobs[a:job_id]
2015-12-16 08:53:53 -05:00
2016-06-26 07:12:36 -04:00
call extend(job.stdout, a:data)
2015-12-16 08:53:53 -05:00
endfunction
2016-12-27 09:46:49 -05:00
function! s:on_stderr(job_id, data, event) dict abort
2016-06-26 07:12:36 -04:00
if !has_key(s:jobs, a:job_id)
return
endif
let job = s:jobs[a:job_id]
2015-12-16 08:53:53 -05:00
2016-06-26 07:12:36 -04:00
call extend(job.stderr, a:data)
2015-12-16 08:53:53 -05:00
endfunction
2016-12-27 09:46:49 -05:00
function! s:on_exit(job_id, exit_status, event) dict abort
2016-06-26 07:12:36 -04:00
if !has_key(s:jobs, a:job_id)
return
endif
let job = s:jobs[a:job_id]
2015-12-16 08:53:53 -05:00
let l:listtype = go#list#Type("_term")
2016-04-12 04:31:09 -04:00
2016-06-26 07:12:36 -04:00
" usually there is always output so never branch into this clause
if empty(job.stdout)
call go#list#Clean(l:listtype)
call go#list#Window(l:listtype)
unlet s:jobs[a:job_id]
return
endif
2016-04-12 04:31:09 -04:00
2016-06-26 07:12:36 -04:00
let errors = go#tool#ParseErrors(job.stdout)
let errors = go#tool#FilterValids(errors)
2015-12-16 08:53:53 -05:00
2016-06-26 07:12:36 -04:00
if !empty(errors)
" close terminal we don't need it anymore
2016-12-27 09:46:49 -05:00
close
2016-04-12 04:31:09 -04:00
2016-12-27 09:46:49 -05:00
call go#list#Populate(l:listtype, errors, job.cmd)
2016-06-26 07:12:36 -04:00
call go#list#Window(l:listtype, len(errors))
if !self.bang
call go#list#JumpToFirst(l:listtype)
2015-12-16 08:53:53 -05:00
endif
2016-06-26 07:12:36 -04:00
unlet s:jobs[a:job_id]
return
endif
2015-12-16 08:53:53 -05:00
2016-04-12 04:31:09 -04:00
" tests are passing clean the list and close the list. But we only can
" close them from a normal view, so jump back, close the list and then
" again jump back to the terminal
wincmd p
call go#list#Clean(l:listtype)
call go#list#Window(l:listtype)
wincmd p
2015-12-16 08:53:53 -05:00
unlet s:jobs[a:job_id]
endfunction
2016-06-26 07:12:36 -04:00
" vim: sw=2 ts=2 et