fix(terminal): scrollback delete lines immediately #18832

* on_scrollback_option_changed renamed to adjust_scrollback. The
  function name did not correspond to what it was doing. It is
  called unconditionally in every refresh of the terminal
  unrelated if the scrollback option was changed.
* new on_scrollback_option_changed function, which calls
  refresh_terminal, which then calls adjust_scrollback
* terminal_check_size is not the appropriate function to call when the
  option is changed since it only conditionally adjusts the scrollback.
  Use the new on_scrollback_option_changed

fixes #15477
fixes #11811
This commit is contained in:
Javier Lopez
2022-06-08 19:46:57 -05:00
committed by GitHub
parent 10291bb854
commit 916d848049
3 changed files with 44 additions and 6 deletions

View File

@@ -4544,9 +4544,9 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, char *errbuf,
check_colorcolumn(wp); check_colorcolumn(wp);
} }
} else if (pp == &curbuf->b_p_scbk || pp == &p_scbk) { } else if (pp == &curbuf->b_p_scbk || pp == &p_scbk) {
if (curbuf->terminal) { if (curbuf->terminal && value < old_value) {
// Force the scrollback to take effect. // Force the scrollback to take immediate effect only when decreasing it.
terminal_check_size(curbuf->terminal); on_scrollback_option_changed(curbuf->terminal);
} }
} else if (pp == &curwin->w_p_nuw) { } else if (pp == &curwin->w_p_nuw) {
curwin->w_nrwidth_line_count = 0; curwin->w_nrwidth_line_count = 0;

View File

@@ -233,6 +233,8 @@ Terminal *terminal_open(buf_T *buf, TerminalOptions opts)
RESET_BINDING(curwin); RESET_BINDING(curwin);
// Reset cursor in current window. // Reset cursor in current window.
curwin->w_cursor = (pos_T){ .lnum = 1, .col = 0, .coladd = 0 }; curwin->w_cursor = (pos_T){ .lnum = 1, .col = 0, .coladd = 0 };
// Initialize to check if the scrollback buffer has been allocated inside a TermOpen autocmd
rv->sb_buffer = NULL;
// Apply TermOpen autocmds _before_ configuring the scrollback buffer. // Apply TermOpen autocmds _before_ configuring the scrollback buffer.
apply_autocmds(EVENT_TERMOPEN, NULL, NULL, false, buf); apply_autocmds(EVENT_TERMOPEN, NULL, NULL, false, buf);
// Local 'scrollback' _after_ autocmds. // Local 'scrollback' _after_ autocmds.
@@ -1481,8 +1483,16 @@ static void refresh_size(Terminal *term, buf_T *buf)
term->opts.resize_cb((uint16_t)width, (uint16_t)height, term->opts.data); term->opts.resize_cb((uint16_t)width, (uint16_t)height, term->opts.data);
} }
/// Adjusts scrollback storage after 'scrollback' option changed. void on_scrollback_option_changed(Terminal *term)
static void on_scrollback_option_changed(Terminal *term, buf_T *buf) {
// Scrollback buffer may not exist yet, e.g. if 'scrollback' is set in a TermOpen autocmd.
if (term->sb_buffer != NULL) {
refresh_terminal(term);
}
}
/// Adjusts scrollback storage and the terminal buffer scrollback lines
static void adjust_scrollback(Terminal *term, buf_T *buf)
{ {
if (buf->b_p_scbk < 1) { // Local 'scrollback' was set to -1. if (buf->b_p_scbk < 1) { // Local 'scrollback' was set to -1.
buf->b_p_scbk = SB_MAX; buf->b_p_scbk = SB_MAX;
@@ -1554,7 +1564,7 @@ static void refresh_scrollback(Terminal *term, buf_T *buf)
deleted_lines(buf->b_ml.ml_line_count, 1); deleted_lines(buf->b_ml.ml_line_count, 1);
} }
on_scrollback_option_changed(term, buf); adjust_scrollback(term, buf);
} }
// Refresh the screen (visible part of the buffer when the terminal is // Refresh the screen (visible part of the buffer when the terminal is

View File

@@ -465,6 +465,34 @@ describe("'scrollback' option", function()
matches((iswin() and '^27: line[ ]*$' or '^26: line[ ]*$'), eval("getline(line('w0') - 10)")) matches((iswin() and '^27: line[ ]*$' or '^26: line[ ]*$'), eval("getline(line('w0') - 10)"))
end) end)
it('deletes extra lines immediately', function()
-- Scrollback is 10 on screen_setup
local screen = thelpers.screen_setup(nil, nil, 30)
local lines = {}
for i = 1, 30 do
table.insert(lines, 'line'..tostring(i))
end
table.insert(lines, '')
feed_data(lines)
screen:expect([[
line26 |
line27 |
line28 |
line29 |
line30 |
{1: } |
{3:-- TERMINAL --} |
]])
local term_height = 6 -- Actual terminal screen height, not the scrollback
-- Initial
local scrollback = curbufmeths.get_option('scrollback')
eq(scrollback + term_height, eval('line("$")'))
-- Reduction
scrollback = scrollback - 2
curbufmeths.set_option('scrollback', scrollback)
eq(scrollback + term_height, eval('line("$")'))
end)
it('defaults to 10000 in :terminal buffers', function() it('defaults to 10000 in :terminal buffers', function()
set_fake_shell() set_fake_shell()
command('terminal') command('terminal')