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

128 lines
3.4 KiB
VimL
Raw Normal View History

2018-12-17 06:28:27 -05:00
" don't spam the user when Vim is started in Vi compatibility mode
let s:cpo_save = &cpo
set cpo&vim
2015-12-16 08:53:53 -05:00
" 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
2018-06-14 06:31:12 -04:00
return go#term#newmode(a:bang, a:cmd, go#config#TermMode())
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)
2018-06-14 06:31:12 -04:00
let mode = go#config#TermMode()
2016-06-26 07:12:36 -04:00
endif
2015-12-16 08:53:53 -05:00
2018-03-31 10:56:26 -04:00
let state = {
\ 'cmd': a:cmd,
\ 'bang' : a:bang,
\ 'winid': win_getid(winnr()),
\ 'stdout': []
\ }
2016-06-26 07:12:36 -04:00
" execute go build in the files directory
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
2018-03-31 10:56:26 -04:00
" explicitly bind callbacks to state so that within them, self will always
" refer to state. See :help Partial for more information.
"
" Don't set an on_stderr, because it will be passed the same data as
" on_stdout. See https://github.com/neovim/neovim/issues/2836
2016-12-27 09:46:49 -05:00
let job = {
2018-03-31 10:56:26 -04:00
\ 'on_stdout': function('s:on_stdout', [], state),
\ 'on_exit' : function('s:on_exit', [], state),
\ }
2015-12-16 08:53:53 -05:00
2018-03-31 10:56:26 -04:00
let state.id = termopen(a:cmd, job)
let state.termwinid = win_getid(winnr())
2015-12-16 08:53:53 -05:00
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
" resize new term if needed.
2018-06-14 06:31:12 -04:00
let height = go#config#TermHeight()
let width = go#config#TermWidth()
2015-12-16 08:53:53 -05:00
2018-03-31 10:56:26 -04:00
" Adjust the window width or height depending on whether it's a vertical or
" horizontal split.
2018-02-04 06:35:08 -05:00
if mode =~ "vertical" || mode =~ "vsplit" || mode =~ "vnew"
2016-06-26 07:12:36 -04:00
exe 'vertical resize ' . width
2018-02-04 06:35:08 -05:00
elseif mode =~ "split" || mode =~ "new"
exe 'resize ' . height
2016-06-26 07:12:36 -04:00
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...
2018-03-31 10:56:26 -04:00
call jobresize(state.id, width, height)
2015-12-16 08:53:53 -05:00
2018-03-31 10:56:26 -04:00
call win_gotoid(state.winid)
2018-02-04 06:35:08 -05:00
2018-03-31 10:56:26 -04:00
return state.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
2018-03-31 10:56:26 -04:00
call extend(self.stdout, 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
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
2018-03-31 10:56:26 -04:00
if empty(self.stdout)
call s:cleanlist(self.winid, l:listtype)
2016-06-26 07:12:36 -04:00
return
endif
2016-04-12 04:31:09 -04:00
2018-03-31 10:56:26 -04:00
let errors = go#tool#ParseErrors(self.stdout)
2016-06-26 07:12:36 -04:00
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)
2018-03-31 10:56:26 -04:00
" close terminal; we don't need it anymore
call win_gotoid(self.termwinid)
2016-12-27 09:46:49 -05:00
close
2016-04-12 04:31:09 -04:00
2018-03-31 10:56:26 -04:00
call win_gotoid(self.winid)
2018-06-14 06:31:12 -04:00
let title = self.cmd
if type(title) == v:t_list
let title = join(self.cmd)
endif
call go#list#Populate(l:listtype, errors, title)
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
2018-03-31 10:56:26 -04:00
2016-06-26 07:12:36 -04:00
return
endif
2015-12-16 08:53:53 -05:00
2018-03-31 10:56:26 -04:00
call s:cleanlist(self.winid, l:listtype)
endfunction
2016-04-12 04:31:09 -04:00
2018-03-31 10:56:26 -04:00
function! s:cleanlist(winid, listtype) abort
" There are no errors. Clean and close the list. Jump to the window to which
" the location list is attached, close the list, and then jump back to the
" current window.
let winid = win_getid(winnr())
call win_gotoid(a:winid)
call go#list#Clean(a:listtype)
call win_gotoid(l:winid)
2015-12-16 08:53:53 -05:00
endfunction
2018-12-17 06:28:27 -05:00
" restore Vi compatibility settings
let &cpo = s:cpo_save
unlet s:cpo_save
2016-06-26 07:12:36 -04:00
" vim: sw=2 ts=2 et