fix(session): respect sessionoptions=terminal #19497

fixes #13078

Co-authored-by: Yuta Katayama <8683947+yutkat@users.noreply.github.com>
This commit is contained in:
Gustavo Sampaio 2022-08-01 09:13:46 -03:00 committed by GitHub
parent bcb4186cf6
commit ece0850b73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 94 additions and 21 deletions

View File

@ -5112,7 +5112,7 @@ A jump table for the options with a short description can be found at |Q_op|.
*'sessionoptions'* *'ssop'*
'sessionoptions' 'ssop' string (default: "blank,buffers,curdir,folds,
help,tabpages,winsize")
help,tabpages,winsize,terminal")
global
Changes the effect of the |:mksession| command. It is a comma-
separated list of words. Each word enables saving and restoring

View File

@ -193,6 +193,9 @@ static int ses_do_win(win_T *wp)
if (bt_help(wp->w_buffer)) {
return ssop_flags & SSOP_HELP;
}
if (bt_terminal(wp->w_buffer)) {
return ssop_flags & SSOP_TERMINAL;
}
return true;
}
@ -407,6 +410,8 @@ static int put_view(FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int curr
if ((flagp == &ssop_flags) && alt != NULL && alt->b_fname != NULL
&& *alt->b_fname != NUL
&& alt->b_p_bl
// do not set balt if buffer is terminal and "terminal" is not set in options
&& !(bt_terminal(alt) && !(ssop_flags & SSOP_TERMINAL))
&& (fputs("balt ", fd) < 0
|| ses_fname(fd, alt, flagp, true) == FAIL)) {
return FAIL;
@ -616,6 +621,7 @@ static int makeopens(FILE *fd, char_u *dirnow)
FOR_ALL_BUFFERS(buf) {
if (!(only_save_windows && buf->b_nwindows == 0)
&& !(buf->b_help && !(ssop_flags & SSOP_HELP))
&& !(bt_terminal(buf) && !(ssop_flags & SSOP_TERMINAL))
&& buf->b_fname != NULL
&& buf->b_p_bl) {
if (fprintf(fd, "badd +%" PRId64 " ",

View File

@ -2063,7 +2063,7 @@ return {
type='string', list='onecomma', scope={'global'},
deny_duplicates=true,
varname='p_ssop',
defaults={if_true="blank,buffers,curdir,folds,help,tabpages,winsize"}
defaults={if_true="blank,buffers,curdir,folds,help,tabpages,winsize,terminal"}
},
{
full_name='shada', abbreviation='sd',

View File

@ -53,6 +53,73 @@ describe(':mksession', function()
neq(funcs.winbufnr(1), funcs.winbufnr(3))
end)
-- common testing procedure for testing "sessionoptions-=terminal"
local function test_terminal_session_disabled(expected_buf_count)
command('set sessionoptions-=terminal')
command('mksession ' .. session_file)
-- Create a new test instance of Nvim.
clear()
-- Restore session.
command('source ' .. session_file)
eq(expected_buf_count, #meths.list_bufs())
end
it(
'do not restore :terminal if not set in sessionoptions, terminal in current window #13078',
function()
local tmpfile_base = file_prefix .. '-tmpfile'
command('edit ' .. tmpfile_base)
command('terminal')
local buf_count = #meths.list_bufs()
eq(2, buf_count)
eq('terminal', meths.buf_get_option(0, 'buftype'))
test_terminal_session_disabled(2)
-- no terminal should be set. As a side effect we end up with a blank buffer
eq('', meths.buf_get_option(meths.list_bufs()[1], 'buftype'))
eq('', meths.buf_get_option(meths.list_bufs()[2], 'buftype'))
end
)
it('do not restore :terminal if not set in sessionoptions, terminal hidden #13078', function()
command('terminal')
local terminal_bufnr = meths.get_current_buf()
local tmpfile_base = file_prefix .. '-tmpfile'
-- make terminal hidden by opening a new file
command('edit ' .. tmpfile_base .. '1')
local buf_count = #meths.list_bufs()
eq(2, buf_count)
eq(1, funcs.getbufinfo(terminal_bufnr)[1].hidden)
test_terminal_session_disabled(1)
-- no terminal should exist here
neq('', meths.buf_get_name(meths.list_bufs()[1]))
end)
it('do not restore :terminal if not set in sessionoptions, only buffer #13078', function()
command('terminal')
eq('terminal', meths.buf_get_option(0, 'buftype'))
local buf_count = #meths.list_bufs()
eq(1, buf_count)
test_terminal_session_disabled(1)
-- no terminal should be set
eq('', meths.buf_get_option(0, 'buftype'))
end)
it('restores tab-local working directories', function()
local tmpfile_base = file_prefix .. '-tmpfile'
local cwd_dir = funcs.getcwd()
@ -179,7 +246,7 @@ describe(':mksession', function()
height = 3,
row = 0,
col = 1,
style = 'minimal'
style = 'minimal',
}
meths.open_win(buf, false, config)
local cmdheight = meths.get_option('cmdheight')