Commit 2bf4ba73 authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Refactor kodo#toggleterm#Terminal.collapse in Vim plugins

Post-collapse behaviour (e.g. close window or restore buffer) is now in
a dedicated "on_collapse" handler which can be pointed to alternate
handlers like "restore_buffer".
parent 3e64629d
Loading
Loading
Loading
Loading
+30 −7
Original line number Diff line number Diff line
@@ -64,14 +64,14 @@ function! kodo#toggleterm#Terminal(new_win, ...)
		let window = win_getid()
		if !win_gotoid(self.window)
			echoerr "Toggle-terminal window not found, ignoring"
		else
			if self.was_new
				close
			elseif bufnr('') == self.buffer
				silent execute 'buffer' self.origin_buffer
			else
			let self.window = v:null
			return
		elseif bufnr('') != self.buffer
			echoerr "Buffer in toggle-terminal window changed, ignoring"
		else
			call self.on_collapse()
		endif
		if self.window != window
			call win_gotoid(window)
		endif
		let self.window = v:null
@@ -98,6 +98,29 @@ function! kodo#toggleterm#Terminal(new_win, ...)
		let self.closed = v:true
	endfunction

	" Perform window cleanup on collapse
	" The current buffer is guranteed to be the terminal buffer, and the current 
	" window is guranteed to be the window it was expanded in, otherwise this 
	" hook is not called.
	function term.on_collapse()
		if self.was_new
			call self.close_window()
		else
			call self.restore_buffer()
		endif
	endfunction

	function term.close_window()
		let self.window = v:null
		close
	endfunction

	function term.restore_buffer()
		if self.origin_buffer != v:null
			silent execute 'buffer' self.origin_buffer
		endif
	endfunction

	function term.open()
		" Create the terminal buffer
		let origin = bufnr('')