robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
46.4 KiB1386 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3local Screen = require('test.functional.ui.screen')
4local tt = require('test.functional.testterm')
5
6local describe, it, before_each, setup, teardown =
7  t.describe, t.it, t.before_each, t.setup, t.teardown
8local clear, eq, neq = n.clear, t.eq, t.neq
9local feed, testprg = n.feed, n.testprg
10local fn = n.fn
11local eval = n.eval
12local command = n.command
13local poke_eventloop = n.poke_eventloop
14local retry = t.retry
15local api = n.api
16local feed_data = tt.feed_data
17local pcall_err = t.pcall_err
18local exec_lua = n.exec_lua
19local assert_alive = n.assert_alive
20local skip = t.skip
21local is_os = t.is_os
22
23local function test_terminal_scrollback(hide_curbuf)
24  local screen --- @type test.functional.ui.screen
25  local buf --- @type integer
26  local chan --- @type integer
27  local otherbuf --- @type integer
28  local restore_terminal_mode --- @type boolean?
29  local save_feed_data = feed_data
30
31  local function may_hide_curbuf()
32    if hide_curbuf then
33      eq(nil, restore_terminal_mode)
34      restore_terminal_mode = vim.startswith(api.nvim_get_mode().mode, 't')
35      api.nvim_set_current_buf(otherbuf)
36    end
37  end
38
39  local function may_restore_curbuf()
40    if hide_curbuf then
41      neq(nil, restore_terminal_mode)
42      eq(buf, fn.bufnr('#'))
43      feed('<C-^>') -- "view" in 'jumpoptions' applies to this
44      if restore_terminal_mode then
45        feed('i')
46      else
47        -- Cursor position was restored from wi_mark, not b_last_cursor.
48        -- Check that b_last_cursor and wi_mark are the same.
49        --- @type integer[], integer[]
50        local last_cursor, restored_cursor = unpack(exec_lua(function()
51          -- Get these two positions on the same RPC call.
52          return { vim.fn.getpos([['"]]), vim.fn.getpos('.') }
53        end))
54        if last_cursor[2] > 0 then
55          eq(restored_cursor, last_cursor)
56        else
57          eq({ 0, 0, 0, 0 }, last_cursor)
58          eq({ 0, 1, 1, 0 }, restored_cursor)
59        end
60      end
61      restore_terminal_mode = nil
62    end
63  end
64
65  setup(function()
66    feed_data = function(data)
67      may_hide_curbuf()
68      api.nvim_chan_send(chan, data)
69      may_restore_curbuf()
70    end
71  end)
72
73  teardown(function()
74    feed_data = save_feed_data
75  end)
76
77  --- @param prefix string
78  --- @param start integer
79  --- @param stop integer
80  local function feed_lines(prefix, start, stop)
81    may_hide_curbuf()
82    local data = ''
83    for i = start, stop do
84      data = data .. prefix .. tostring(i) .. '\n'
85    end
86    api.nvim_chan_send(chan, data)
87    retry(nil, 1000, function()
88      eq({ prefix .. tostring(stop), '' }, api.nvim_buf_get_lines(buf, -3, -1, true))
89    end)
90    may_restore_curbuf()
91  end
92
93  local function try_resize(width, height)
94    may_hide_curbuf()
95    screen:try_resize(width, height)
96    may_restore_curbuf()
97  end
98
99  before_each(function()
100    clear()
101    command('set nostartofline jumpoptions+=view')
102    screen = tt.setup_screen(nil, nil, 30)
103    buf = api.nvim_get_current_buf()
104    chan = api.nvim_get_option_value('channel', { buf = buf })
105    otherbuf = hide_curbuf and api.nvim_create_buf(true, false) or nil
106    restore_terminal_mode = nil
107  end)
108
109  describe('when the limit is exceeded', function()
110    before_each(function()
111      feed_lines('line', 1, 30)
112      screen:expect([[
113        line26                        |
114        line27                        |
115        line28                        |
116        line29                        |
117        line30                        |
118        ^                              |
119        {5:-- TERMINAL --}                |
120      ]])
121      eq(16, api.nvim_buf_line_count(0))
122    end)
123
124    it('will delete extra lines at the top', function()
125      feed('<c-\\><c-n>gg')
126      screen:expect([[
127        ^line16                        |
128        line17                        |
129        line18                        |
130        line19                        |
131        line20                        |
132        line21                        |
133                                      |
134      ]])
135    end)
136
137    describe('and cursor on non-last row in screen', function()
138      before_each(function()
139        feed([[<C-\><C-N>M$]])
140        fn.setpos("'m", { 0, 13, 4, 0 })
141        local ns = api.nvim_create_namespace('test')
142        api.nvim_buf_set_extmark(0, ns, 12, 0, { end_col = 6, hl_group = 'ErrorMsg' })
143        screen:expect([[
144          line26                        |
145          line27                        |
146          {101:line2^8}                        |
147          line29                        |
148          line30                        |
149                                        |*2
150        ]])
151      end)
152
153      it("outputting fewer than 'scrollback' lines", function()
154        feed_lines('new_line', 1, 6)
155        screen:expect([[
156          line26                        |
157          line27                        |
158          {101:line2^8}                        |
159          line29                        |
160          line30                        |
161          new_line1                     |
162                                        |
163        ]])
164        eq({ 0, 7, 4, 0 }, fn.getpos("'m"))
165        eq({ 0, 7, 6, 0 }, fn.getpos('.'))
166      end)
167
168      it("outputting more than 'scrollback' lines", function()
169        feed_lines('new_line', 1, 11)
170        screen:expect([[
171          line27                        |
172          {101:line2^8}                        |
173          line29                        |
174          line30                        |
175          new_line1                     |
176          new_line2                     |
177                                        |
178        ]])
179        eq({ 0, 2, 4, 0 }, fn.getpos("'m"))
180        eq({ 0, 2, 6, 0 }, fn.getpos('.'))
181      end)
182
183      it('outputting more lines than whole buffer', function()
184        feed_lines('new_line', 1, 20)
185        screen:expect([[
186          ^new_line6                     |
187          new_line7                     |
188          new_line8                     |
189          new_line9                     |
190          new_line10                    |
191          new_line11                    |
192                                        |
193        ]])
194        eq({ 0, 0, 0, 0 }, fn.getpos("'m"))
195        eq({ 0, 1, 1, 0 }, fn.getpos('.'))
196      end)
197
198      it('clearing scrollback with ED 3', function()
199        feed_data('\027[3J')
200        screen:expect_unchanged(hide_curbuf)
201        eq({ 0, 3, 4, 0 }, fn.getpos("'m"))
202        eq({ 0, 3, 6, 0 }, fn.getpos('.'))
203        feed('gg')
204        screen:expect([[
205          line2^6                        |
206          line27                        |
207          {101:line28}                        |
208          line29                        |
209          line30                        |
210                                        |*2
211        ]])
212      end)
213
214      it('clearing scrollback with ED 3 and outputting lines', function()
215        feed_data('\027[3J' .. 'new_line1\nnew_line2\nnew_line3')
216        screen:expect([[
217          line26                        |
218          line27                        |
219          {101:line2^8}                        |
220          line29                        |
221          line30                        |
222          new_line1                     |
223                                        |
224        ]])
225        eq({ 0, 3, 4, 0 }, fn.getpos("'m"))
226        eq({ 0, 3, 6, 0 }, fn.getpos('.'))
227      end)
228
229      it('clearing scrollback with ED 3 between outputting lines', function()
230        skip(is_os('win'), 'FIXME: wrong behavior on Windows, ConPTY bug?')
231        feed_data('line31\nline32\n' .. '\027[3J' .. 'new_line1\nnew_line2')
232        screen:expect([[
233          {101:line2^8}                        |
234          line29                        |
235          line30                        |
236          line31                        |
237          line32                        |
238          new_line1                     |
239                                        |
240        ]])
241        eq({ 0, 1, 4, 0 }, fn.getpos("'m"))
242        eq({ 0, 1, 6, 0 }, fn.getpos('.'))
243      end)
244    end)
245
246    describe('and cursor on scrollback row #12651', function()
247      before_each(function()
248        feed([[<C-\><C-N>Hk$]])
249        fn.setpos("'m", { 0, 10, 4, 0 })
250        local ns = api.nvim_create_namespace('test')
251        api.nvim_buf_set_extmark(0, ns, 9, 0, { end_col = 6, hl_group = 'ErrorMsg' })
252        screen:expect([[
253          {101:line2^5}                        |
254          line26                        |
255          line27                        |
256          line28                        |
257          line29                        |
258          line30                        |
259                                        |
260        ]])
261      end)
262
263      it("outputting fewer than 'scrollback' lines", function()
264        feed_lines('new_line', 1, 6)
265        screen:expect_unchanged(hide_curbuf)
266        eq({ 0, 4, 4, 0 }, fn.getpos("'m"))
267        eq({ 0, 4, 6, 0 }, fn.getpos('.'))
268      end)
269
270      it("outputting more than 'scrollback' lines", function()
271        feed_lines('new_line', 1, 11)
272        screen:expect([[
273          ^line27                        |
274          line28                        |
275          line29                        |
276          line30                        |
277          new_line1                     |
278          new_line2                     |
279                                        |
280        ]])
281        eq({ 0, 0, 0, 0 }, fn.getpos("'m"))
282        eq({ 0, 1, 1, 0 }, fn.getpos('.'))
283      end)
284    end)
285
286    it('changing window height does not duplicate lines', function()
287      -- XXX: Can't test this reliably on Windows unless the cursor is _moved_
288      --      by the resize. http://docs.libuv.org/en/v1.x/signal.html
289      --      See also: https://github.com/rprichard/winpty/issues/110
290      skip(is_os('win'))
291      try_resize(screen._width, screen._height + 4)
292      screen:expect([[
293        line23                        |
294        line24                        |
295        line25                        |
296        line26                        |
297        line27                        |
298        line28                        |
299        line29                        |
300        line30                        |
301        rows: 10, cols: 30            |
302        ^                              |
303        {5:-- TERMINAL --}                |
304      ]])
305      eq(17, api.nvim_buf_line_count(0))
306      try_resize(screen._width, screen._height - 2)
307      screen:expect([[
308        line26                        |
309        line27                        |
310        line28                        |
311        line29                        |
312        line30                        |
313        rows: 10, cols: 30            |
314        rows: 8, cols: 30             |
315        ^                              |
316        {5:-- TERMINAL --}                |
317      ]])
318      eq(18, api.nvim_buf_line_count(0))
319      try_resize(screen._width, screen._height - 3)
320      screen:expect([[
321        line30                        |
322        rows: 10, cols: 30            |
323        rows: 8, cols: 30             |
324        rows: 5, cols: 30             |
325        ^                              |
326        {5:-- TERMINAL --}                |
327      ]])
328      eq(15, api.nvim_buf_line_count(0))
329      try_resize(screen._width, screen._height + 3)
330      screen:expect([[
331        line28                        |
332        line29                        |
333        line30                        |
334        rows: 10, cols: 30            |
335        rows: 8, cols: 30             |
336        rows: 5, cols: 30             |
337        rows: 8, cols: 30             |
338        ^                              |
339        {5:-- TERMINAL --}                |
340      ]])
341      eq(16, api.nvim_buf_line_count(0))
342      feed([[<C-\><C-N>8<C-Y>]])
343      screen:expect([[
344        line20                        |
345        line21                        |
346        line22                        |
347        line23                        |
348        line24                        |
349        line25                        |
350        line26                        |
351        ^line27                        |
352                                      |
353      ]])
354    end)
355  end)
356
357  describe('with cursor at last row', function()
358    before_each(function()
359      feed_lines('line', 1, 4)
360      screen:expect([[
361        tty ready                     |
362        line1                         |
363        line2                         |
364        line3                         |
365        line4                         |
366        ^                              |
367        {5:-- TERMINAL --}                |
368      ]])
369      fn.setpos("'m", { 0, 3, 4, 0 })
370      local ns = api.nvim_create_namespace('test')
371      api.nvim_buf_set_extmark(0, ns, 2, 0, { end_col = 5, hl_group = 'ErrorMsg' })
372      screen:expect([[
373        tty ready                     |
374        line1                         |
375        {101:line2}                         |
376        line3                         |
377        line4                         |
378        ^                              |
379        {5:-- TERMINAL --}                |
380      ]])
381    end)
382
383    it("outputting more than 'scrollback' lines in Normal mode", function()
384      feed([[<C-\><C-N>]])
385      feed_lines('new_line', 1, 11)
386      screen:expect([[
387        new_line7                     |
388        new_line8                     |
389        new_line9                     |
390        new_line10                    |
391        new_line11                    |
392        ^                              |
393                                      |
394      ]])
395      feed('gg')
396      screen:expect([[
397        ^line1                         |
398        {101:line2}                         |
399        line3                         |
400        line4                         |
401        new_line1                     |
402        new_line2                     |
403                                      |
404      ]])
405      eq({ 0, 2, 4, 0 }, fn.getpos("'m"))
406      feed('G')
407      feed_lines('new_line', 12, 31)
408      screen:expect([[
409        new_line27                    |
410        new_line28                    |
411        new_line29                    |
412        new_line30                    |
413        new_line31                    |
414        ^                              |
415                                      |
416      ]])
417      feed('gg')
418      screen:expect([[
419        ^new_line17                    |
420        new_line18                    |
421        new_line19                    |
422        new_line20                    |
423        new_line21                    |
424        new_line22                    |
425                                      |
426      ]])
427      eq({ 0, 0, 0, 0 }, fn.getpos("'m"))
428    end)
429
430    it('clearing scrollback with ED 3', function()
431      -- Clearing empty scrollback and then outputting a line
432      feed_data('\027[3J' .. 'line5\n')
433      screen:expect([[
434        line1                         |
435        {101:line2}                         |
436        line3                         |
437        line4                         |
438        line5                         |
439        ^                              |
440        {5:-- TERMINAL --}                |
441      ]])
442      eq(7, api.nvim_buf_line_count(0))
443      eq({ 0, 3, 4, 0 }, fn.getpos("'m"))
444      -- Clearing 1 line of scrollback
445      feed_data('\027[3J')
446      screen:expect_unchanged(hide_curbuf)
447      eq(6, api.nvim_buf_line_count(0))
448      eq({ 0, 2, 4, 0 }, fn.getpos("'m"))
449      -- Outputting a line
450      feed_data('line6\n')
451      screen:expect([[
452        {101:line2}                         |
453        line3                         |
454        line4                         |
455        line5                         |
456        line6                         |
457        ^                              |
458        {5:-- TERMINAL --}                |
459      ]])
460      eq(7, api.nvim_buf_line_count(0))
461      eq({ 0, 2, 4, 0 }, fn.getpos("'m"))
462      -- Clearing 1 line of scrollback and then outputting a line
463      feed_data('\027[3J' .. 'line7\n')
464      screen:expect([[
465        line3                         |
466        line4                         |
467        line5                         |
468        line6                         |
469        line7                         |
470        ^                              |
471        {5:-- TERMINAL --}                |
472      ]])
473      eq(7, api.nvim_buf_line_count(0))
474      eq({ 0, 1, 4, 0 }, fn.getpos("'m"))
475      -- Check first line of buffer in Normal mode
476      feed([[<C-\><C-N>gg]])
477      screen:expect([[
478        {101:^line2}                         |
479        line3                         |
480        line4                         |
481        line5                         |
482        line6                         |
483        line7                         |
484                                      |
485      ]])
486      feed('G')
487      -- Outputting lines and then clearing scrollback
488      skip(is_os('win'), 'FIXME: wrong behavior on Windows, ConPTY bug?')
489      feed_data('line8\nline9\n' .. '\027[3J')
490      screen:expect([[
491        line5                         |
492        line6                         |
493        line7                         |
494        line8                         |
495        line9                         |
496        ^                              |
497                                      |
498      ]])
499      eq(6, api.nvim_buf_line_count(0))
500      eq({ 0, 0, 0, 0 }, fn.getpos("'m"))
501    end)
502
503    describe('and 1 line is printed', function()
504      before_each(function()
505        feed_lines('line', 5, 5)
506      end)
507
508      it('will hide the top line', function()
509        screen:expect([[
510          line1                         |
511          {101:line2}                         |
512          line3                         |
513          line4                         |
514          line5                         |
515          ^                              |
516          {5:-- TERMINAL --}                |
517        ]])
518        eq(7, api.nvim_buf_line_count(0))
519        eq({ 0, 3, 4, 0 }, fn.getpos("'m"))
520      end)
521
522      describe('and then 3 more lines are printed', function()
523        before_each(function()
524          feed_lines('line', 6, 8)
525        end)
526
527        it('will hide the top 4 lines', function()
528          screen:expect([[
529            line4                         |
530            line5                         |
531            line6                         |
532            line7                         |
533            line8                         |
534            ^                              |
535            {5:-- TERMINAL --}                |
536          ]])
537          eq({ 0, 3, 4, 0 }, fn.getpos("'m"))
538
539          feed('<c-\\><c-n>6k')
540          screen:expect([[
541            ^line3                         |
542            line4                         |
543            line5                         |
544            line6                         |
545            line7                         |
546            line8                         |
547                                          |
548          ]])
549
550          feed('gg')
551          screen:expect([[
552            ^tty ready                     |
553            line1                         |
554            {101:line2}                         |
555            line3                         |
556            line4                         |
557            line5                         |
558                                          |
559          ]])
560
561          feed('G')
562          screen:expect([[
563            line4                         |
564            line5                         |
565            line6                         |
566            line7                         |
567            line8                         |
568            ^                              |
569                                          |
570          ]])
571        end)
572      end)
573    end)
574
575    describe('and height decreased by 1', function()
576      local function will_hide_top_line()
577        feed([[<C-\><C-N>]])
578        try_resize(screen._width - 2, screen._height - 1)
579        screen:expect([[
580          {101:line2}                       |
581          line3                       |
582          line4                       |
583          rows: 5, cols: 28           |
584          ^                            |
585                                      |
586        ]])
587        eq({ 0, 3, 4, 0 }, fn.getpos("'m"))
588      end
589
590      it('will hide top line', will_hide_top_line)
591
592      describe('and then decreased by 2', function()
593        before_each(function()
594          will_hide_top_line()
595          try_resize(screen._width - 2, screen._height - 2)
596        end)
597
598        it('will hide the top 3 lines', function()
599          screen:expect([[
600            rows: 5, cols: 28         |
601            rows: 3, cols: 26         |
602            ^                          |
603                                      |
604          ]])
605          eq(8, api.nvim_buf_line_count(0))
606          eq({ 0, 3, 4, 0 }, fn.getpos("'m"))
607          feed('3k')
608          screen:expect([[
609            ^line4                     |
610            rows: 5, cols: 28         |
611            rows: 3, cols: 26         |
612                                      |
613          ]])
614          feed('gg')
615          screen:expect([[
616            ^tty ready                 |
617            line1                     |
618            {101:line2}                     |
619                                      |
620          ]])
621        end)
622      end)
623    end)
624  end)
625
626  describe('with empty lines after the cursor', function()
627    -- XXX: Can't test this reliably on Windows unless the cursor is _moved_
628    --      by the resize. http://docs.libuv.org/en/v1.x/signal.html
629    --      See also: https://github.com/rprichard/winpty/issues/110
630    if skip(is_os('win')) then
631      return
632    end
633
634    describe('and the height is decreased by 2', function()
635      before_each(function()
636        try_resize(screen._width, screen._height - 2)
637      end)
638
639      local function will_delete_last_two_lines()
640        screen:expect([[
641          tty ready                     |
642          rows: 4, cols: 30             |
643          ^                              |
644                                        |
645          {5:-- TERMINAL --}                |
646        ]])
647        eq(4, api.nvim_buf_line_count(0))
648      end
649
650      it('will delete the last two empty lines', will_delete_last_two_lines)
651
652      describe('and then decreased by 1', function()
653        before_each(function()
654          will_delete_last_two_lines()
655          try_resize(screen._width, screen._height - 1)
656        end)
657
658        it('will delete the last line and hide the first', function()
659          screen:expect([[
660            rows: 4, cols: 30             |
661            rows: 3, cols: 30             |
662            ^                              |
663            {5:-- TERMINAL --}                |
664          ]])
665          eq(4, api.nvim_buf_line_count(0))
666          feed('<c-\\><c-n>gg')
667          screen:expect([[
668            ^tty ready                     |
669            rows: 4, cols: 30             |
670            rows: 3, cols: 30             |
671                                          |
672          ]])
673          feed('a')
674          screen:expect([[
675            rows: 4, cols: 30             |
676            rows: 3, cols: 30             |
677            ^                              |
678            {5:-- TERMINAL --}                |
679          ]])
680        end)
681      end)
682    end)
683  end)
684
685  describe('with 4 lines hidden in the scrollback', function()
686    before_each(function()
687      feed_lines('line', 1, 4)
688      screen:expect([[
689        tty ready                     |
690        line1                         |
691        line2                         |
692        line3                         |
693        line4                         |
694        ^                              |
695        {5:-- TERMINAL --}                |
696      ]])
697      fn.setpos("'m", { 0, 3, 4, 0 })
698      local ns = api.nvim_create_namespace('test')
699      api.nvim_buf_set_extmark(0, ns, 2, 0, { end_col = 5, hl_group = 'ErrorMsg' })
700      screen:expect([[
701        tty ready                     |
702        line1                         |
703        {101:line2}                         |
704        line3                         |
705        line4                         |
706        ^                              |
707        {5:-- TERMINAL --}                |
708      ]])
709      try_resize(screen._width, screen._height - 3)
710      screen:expect([[
711        line4                         |
712        rows: 3, cols: 30             |
713        ^                              |
714        {5:-- TERMINAL --}                |
715      ]])
716      eq(7, api.nvim_buf_line_count(0))
717    end)
718
719    describe('and the height is increased by 1', function()
720      -- XXX: Can't test this reliably on Windows unless the cursor is _moved_
721      --      by the resize. http://docs.libuv.org/en/v1.x/signal.html
722      --      See also: https://github.com/rprichard/winpty/issues/110
723      if skip(is_os('win')) then
724        return
725      end
726      local function pop_then_push()
727        try_resize(screen._width, screen._height + 1)
728        screen:expect([[
729          line4                         |
730          rows: 3, cols: 30             |
731          rows: 4, cols: 30             |
732          ^                              |
733          {5:-- TERMINAL --}                |
734        ]])
735        eq({ 0, 3, 4, 0 }, fn.getpos("'m"))
736      end
737
738      it('will pop 1 line and then push it back', pop_then_push)
739
740      describe('and then by 3', function()
741        before_each(function()
742          pop_then_push()
743          eq(8, api.nvim_buf_line_count(0))
744          try_resize(screen._width, screen._height + 3)
745        end)
746
747        local function pop3_then_push1()
748          screen:expect([[
749            {101:line2}                         |
750            line3                         |
751            line4                         |
752            rows: 3, cols: 30             |
753            rows: 4, cols: 30             |
754            rows: 7, cols: 30             |
755            ^                              |
756            {5:-- TERMINAL --}                |
757          ]])
758          eq(9, api.nvim_buf_line_count(0))
759          eq({ 0, 3, 4, 0 }, fn.getpos("'m"))
760          feed('<c-\\><c-n>gg')
761          screen:expect([[
762            ^tty ready                     |
763            line1                         |
764            {101:line2}                         |
765            line3                         |
766            line4                         |
767            rows: 3, cols: 30             |
768            rows: 4, cols: 30             |
769                                          |
770          ]])
771        end
772
773        it('will pop 3 lines and then push one back', pop3_then_push1)
774
775        describe('and then by 4', function()
776          before_each(function()
777            pop3_then_push1()
778            feed('Gi')
779            try_resize(screen._width, screen._height + 4)
780          end)
781
782          it('will show all lines and leave a blank one at the end', function()
783            screen:expect([[
784              tty ready                     |
785              line1                         |
786              {101:line2}                         |
787              line3                         |
788              line4                         |
789              rows: 3, cols: 30             |
790              rows: 4, cols: 30             |
791              rows: 7, cols: 30             |
792              rows: 11, cols: 30            |
793              ^                              |
794                                            |
795              {5:-- TERMINAL --}                |
796            ]])
797            -- since there's an empty line after the cursor, the buffer line
798            -- count equals the terminal screen height
799            eq(11, api.nvim_buf_line_count(0))
800            eq({ 0, 3, 4, 0 }, fn.getpos("'m"))
801          end)
802        end)
803      end)
804    end)
805  end)
806
807  it('reducing &scrollback deletes extra lines immediately', function()
808    feed_lines('line', 1, 30)
809    screen:expect([[
810      line26                        |
811      line27                        |
812      line28                        |
813      line29                        |
814      line30                        |
815      ^                              |
816      {5:-- TERMINAL --}                |
817    ]])
818    local term_height = 6 -- Actual terminal screen height, not the scrollback
819    -- Initial
820    local scrollback = api.nvim_get_option_value('scrollback', { buf = buf })
821    eq(scrollback + term_height, fn.line('$'))
822    eq(scrollback + term_height, fn.line('.'))
823    n.fn.setpos("'m", { 0, scrollback + 1, 4, 0 })
824    local ns = api.nvim_create_namespace('test')
825    api.nvim_buf_set_extmark(0, ns, scrollback, 0, { end_col = 6, hl_group = 'ErrorMsg' })
826    screen:expect([[
827      {101:line26}                        |
828      line27                        |
829      line28                        |
830      line29                        |
831      line30                        |
832      ^                              |
833      {5:-- TERMINAL --}                |
834    ]])
835    -- Reduction
836    scrollback = scrollback - 2
837    may_hide_curbuf()
838    api.nvim_set_option_value('scrollback', scrollback, { buf = buf })
839    may_restore_curbuf()
840    eq(scrollback + term_height, fn.line('$'))
841    eq(scrollback + term_height, fn.line('.'))
842    screen:expect_unchanged(hide_curbuf)
843    eq({ 0, scrollback + 1, 4, 0 }, n.fn.getpos("'m"))
844  end)
845end
846
847describe(':terminal scrollback', function()
848  describe('in current buffer', function()
849    test_terminal_scrollback(false)
850  end)
851
852  describe('in hidden buffer', function()
853    test_terminal_scrollback(true)
854  end)
855end)
856
857describe(':terminal prints more lines than the screen height and exits', function()
858  it('will push extra lines to scrollback', function()
859    clear()
860    local screen = Screen.new(30, 7, { rgb = false })
861    screen:add_extra_attr_ids({ [100] = { foreground = 12 } })
862    command(
863      ("call jobstart(['%s', '10'], {'term':v:true}) | startinsert"):format(testprg('tty-test'))
864    )
865    screen:expect([[
866      line5                         |
867      line6                         |
868      line7                         |
869      line8                         |
870      line9                         |
871      ^[Process exited 0]            |
872      {5:-- TERMINAL --}                |
873    ]])
874    feed('<cr>')
875    -- closes the buffer correctly after pressing a key
876    screen:expect([[
877      ^                              |
878      {100:~                             }|*5
879                                    |
880    ]])
881  end)
882end)
883
884describe("'scrollback' option", function()
885  before_each(function()
886    clear()
887  end)
888
889  local function set_fake_shell()
890    api.nvim_set_option_value('shell', string.format('"%s" INTERACT', testprg('shell-test')), {})
891  end
892
893  local function expect_lines(expected, epsilon)
894    local ep = epsilon and epsilon or 0
895    local actual = eval("line('$')")
896    if expected > actual + ep and expected < actual - ep then
897      error('expected (+/- ' .. ep .. '): ' .. expected .. ', actual: ' .. tostring(actual))
898    end
899  end
900
901  it('set to 0 behaves as 1', function()
902    local screen
903    if is_os('win') then
904      screen = tt.setup_screen(nil, { 'cmd.exe' }, 30)
905    else
906      screen = tt.setup_screen(nil, { 'sh' }, 30)
907    end
908
909    api.nvim_set_option_value('scrollback', 0, {})
910    feed_data(('%s REP 31 line%s'):format(testprg('shell-test'), is_os('win') and '\r' or '\n'))
911    screen:expect { any = '30: line                      ' }
912    retry(nil, nil, function()
913      expect_lines(7)
914    end)
915  end)
916
917  it('deletes lines (only) if necessary', function()
918    local screen
919    if is_os('win') then
920      command([[let $PROMPT='$$']])
921      screen = tt.setup_screen(nil, { 'cmd.exe' }, 30)
922    else
923      command('let $PS1 = "$"')
924      screen = tt.setup_screen(nil, { 'sh' }, 30)
925    end
926
927    api.nvim_set_option_value('scrollback', 200, {})
928
929    -- Wait for prompt.
930    screen:expect { any = '%$' }
931
932    feed_data(('%s REP 31 line%s'):format(testprg('shell-test'), is_os('win') and '\r' or '\n'))
933    screen:expect { any = '30: line                      ' }
934
935    retry(nil, nil, function()
936      expect_lines(33, 2)
937    end)
938    api.nvim_set_option_value('scrollback', 10, {})
939    poke_eventloop()
940    retry(nil, nil, function()
941      expect_lines(16)
942    end)
943    api.nvim_set_option_value('scrollback', 10000, {})
944    retry(nil, nil, function()
945      expect_lines(16)
946    end)
947    -- Terminal job data is received asynchronously, may happen before the
948    -- 'scrollback' option is synchronized with the internal sb_buffer.
949    command('sleep 100m')
950
951    feed_data(('%s REP 41 line%s'):format(testprg('shell-test'), is_os('win') and '\r' or '\n'))
952    if is_os('win') then
953      screen:expect([[
954        37: line                      |
955        38: line                      |
956        39: line                      |
957        40: line                      |
958                                      |
959        $^                             |
960        {5:-- TERMINAL --}                |
961      ]])
962    else
963      screen:expect([[
964        36: line                      |
965        37: line                      |
966        38: line                      |
967        39: line                      |
968        40: line                      |
969        {MATCH:.*}|
970        {5:-- TERMINAL --}                |
971      ]])
972    end
973    expect_lines(58)
974
975    -- Verify off-screen state
976    eq((is_os('win') and '36: line' or '35: line'), eval("getline(line('w0') - 1)->trim(' ', 2)"))
977    eq((is_os('win') and '27: line' or '26: line'), eval("getline(line('w0') - 10)->trim(' ', 2)"))
978  end)
979
980  it('defaults to 10000 in :terminal buffers', function()
981    set_fake_shell()
982    command('terminal')
983    eq(10000, api.nvim_get_option_value('scrollback', {}))
984  end)
985
986  it('error if set to invalid value', function()
987    eq('Vim(set):E474: Invalid argument: scrollback=-2', pcall_err(command, 'set scrollback=-2'))
988    eq(
989      'Vim(set):E474: Invalid argument: scrollback=1000001',
990      pcall_err(command, 'set scrollback=1000001')
991    )
992  end)
993
994  it('defaults to -1 on normal buffers', function()
995    command('new')
996    eq(-1, api.nvim_get_option_value('scrollback', {}))
997  end)
998
999  it(':setlocal in a :terminal buffer', function()
1000    set_fake_shell()
1001
1002    -- _Global_ scrollback=-1 defaults :terminal to 10_000.
1003    command('setglobal scrollback=-1')
1004    command('terminal')
1005    eq(10000, api.nvim_get_option_value('scrollback', {}))
1006
1007    -- _Local_ scrollback=-1 in :terminal forces the _maximum_.
1008    command('setlocal scrollback=-1')
1009    retry(nil, nil, function() -- Fixup happens on refresh, not immediately.
1010      eq(1000000, api.nvim_get_option_value('scrollback', {}))
1011    end)
1012
1013    -- _Local_ scrollback=-1 during TermOpen forces the maximum. #9605
1014    command('setglobal scrollback=-1')
1015    command('autocmd TermOpen * setlocal scrollback=-1')
1016    command('terminal')
1017    eq(1000000, api.nvim_get_option_value('scrollback', {}))
1018  end)
1019
1020  it(':setlocal in a normal buffer', function()
1021    command('new')
1022    -- :setlocal to -1.
1023    command('setlocal scrollback=-1')
1024    eq(-1, api.nvim_get_option_value('scrollback', {}))
1025    -- :setlocal to anything except -1. Currently, this just has no effect.
1026    command('setlocal scrollback=42')
1027    eq(42, api.nvim_get_option_value('scrollback', {}))
1028  end)
1029
1030  it(':set updates local value and global default', function()
1031    set_fake_shell()
1032    command('set scrollback=42') -- set global value
1033    eq(42, api.nvim_get_option_value('scrollback', {}))
1034    command('terminal')
1035    eq(42, api.nvim_get_option_value('scrollback', {})) -- inherits global default
1036    command('setlocal scrollback=99')
1037    eq(99, api.nvim_get_option_value('scrollback', {}))
1038    command('set scrollback<') -- reset to global default
1039    eq(42, api.nvim_get_option_value('scrollback', {}))
1040    command('setglobal scrollback=734') -- new global default
1041    eq(42, api.nvim_get_option_value('scrollback', {})) -- local value did not change
1042    command('terminal')
1043    eq(734, api.nvim_get_option_value('scrollback', {}))
1044  end)
1045end)
1046
1047describe('pending scrollback line handling', function()
1048  local screen
1049
1050  before_each(function()
1051    clear()
1052    screen = Screen.new(30, 7)
1053  end)
1054
1055  it("does not crash after setting 'number' #14891", function()
1056    exec_lua [[
1057      local api = vim.api
1058      local buf = api.nvim_create_buf(true, true)
1059      local chan = api.nvim_open_term(buf, {})
1060      vim.wo.number = true
1061      api.nvim_chan_send(chan, ("a\n"):rep(11) .. "a")
1062      api.nvim_win_set_buf(0, buf)
1063    ]]
1064    screen:expect [[
1065      {8:  1 }^a                         |
1066      {8:  2 }a                         |
1067      {8:  3 }a                         |
1068      {8:  4 }a                         |
1069      {8:  5 }a                         |
1070      {8:  6 }a                         |
1071                                    |
1072    ]]
1073    feed('G')
1074    screen:expect [[
1075      {8:  7 }a                         |
1076      {8:  8 }a                         |
1077      {8:  9 }a                         |
1078      {8: 10 }a                         |
1079      {8: 11 }a                         |
1080      {8: 12 }^a                         |
1081                                    |
1082    ]]
1083    assert_alive()
1084  end)
1085
1086  it('does not crash after nvim_buf_call #14891', function()
1087    exec_lua(
1088      [[
1089      local bufnr = vim.api.nvim_create_buf(false, true)
1090      local args = ...
1091      vim.api.nvim_buf_call(bufnr, function()
1092        vim.fn.jobstart(args, { term = true })
1093      end)
1094      vim.api.nvim_win_set_buf(0, bufnr)
1095      vim.cmd('startinsert')
1096    ]],
1097      is_os('win') and { 'cmd.exe', '/c', 'for /L %I in (1,1,12) do @echo hi' }
1098        or { 'printf', ('hi\n'):rep(12) }
1099    )
1100    screen:expect [[
1101      hi                            |*5
1102      ^[Process exited 0]            |
1103      {5:-- TERMINAL --}                |
1104    ]]
1105    assert_alive()
1106  end)
1107
1108  it('does not crash after deleting buffer lines', function()
1109    local buf = api.nvim_get_current_buf()
1110    local chan = api.nvim_open_term(buf, {})
1111    api.nvim_chan_send(chan, ('a\n'):rep(11) .. 'a')
1112    screen:expect([[
1113      ^a                             |
1114      a                             |*5
1115                                    |
1116    ]])
1117    api.nvim_set_option_value('modifiable', true, { buf = buf })
1118    api.nvim_buf_set_lines(buf, 0, -1, true, {})
1119    screen:expect([[
1120      ^                              |
1121      {1:~                             }|*5
1122                                    |
1123    ]])
1124    api.nvim_chan_send(chan, ('\nb'):rep(11) .. '\n')
1125    screen:expect([[
1126      b                             |*5
1127      ^                              |
1128                                    |
1129    ]])
1130    assert_alive()
1131  end)
1132end)
1133
1134describe('scrollback is correct', function()
1135  local screen --- @type test.functional.ui.screen
1136  local buf --- @type integer
1137  local win --- @type integer
1138
1139  before_each(function()
1140    clear()
1141    screen = Screen.new(30, 7)
1142    screen:add_extra_attr_ids({
1143      [100] = { foreground = tonumber('0xe00000'), fg_indexed = true },
1144      [101] = { foreground = Screen.colors.White, background = Screen.colors.DarkGreen },
1145      [102] = {
1146        bold = true,
1147        foreground = Screen.colors.White,
1148        background = Screen.colors.DarkGreen,
1149      },
1150    })
1151    api.nvim_buf_set_lines(0, 0, -1, true, { '\027[31mTEST\027[0m 0' })
1152    feed('yy99pG$<C-V>98kg<C-A>')
1153    screen:expect([[
1154      {18:^[}[31mTEST{18:^[}[0m 0             |
1155      {18:^[}[31mTEST{18:^[}[0m ^1             |
1156      {18:^[}[31mTEST{18:^[}[0m 2             |
1157      {18:^[}[31mTEST{18:^[}[0m 3             |
1158      {18:^[}[31mTEST{18:^[}[0m 4             |
1159      {18:^[}[31mTEST{18:^[}[0m 5             |
1160      99 lines changed              |
1161    ]])
1162    buf = api.nvim_get_current_buf()
1163    win = api.nvim_get_current_win()
1164    command('set winwidth=10 | 10vnew')
1165  end)
1166
1167  local function check_buffer_lines(start, stop)
1168    local lines = api.nvim_buf_get_lines(buf, 0, -1, true)
1169    for i = start, stop do
1170      eq(('TEST %d'):format(i), lines[i - start + 1])
1171    end
1172    eq('', lines[#lines])
1173    eq(stop - start + 2, #lines)
1174  end
1175
1176  local function check_common()
1177    feed('<C-W>lG')
1178    screen:expect([[
1179                │{100:TEST} 96            |
1180      {1:~         }│{100:TEST} 97            |
1181      {1:~         }│{100:TEST} 98            |
1182      {1:~         }│{100:TEST} 99            |
1183      {1:~         }│^                   |
1184      {2:[No Name]  }{102:[Scratch] [-]      }|
1185      99 lines changed              |
1186    ]])
1187  end
1188
1189  it('with nvim_open_term() on buffer with fewer lines than scrollback', function()
1190    exec_lua(function()
1191      vim.api.nvim_open_term(buf, {})
1192      vim.api.nvim_win_set_cursor(win, { 3, 0 })
1193    end)
1194    screen:expect([[
1195      ^          │{100:TEST} 0             |
1196      {1:~         }│{100:TEST} 1             |
1197      {1:~         }│{100:TEST} 2             |
1198      {1:~         }│{100:TEST} 3             |
1199      {1:~         }│{100:TEST} 4             |
1200      {3:[No Name]  }{101:[Scratch] [-]      }|
1201      99 lines changed              |
1202    ]])
1203    eq({ 3, 0 }, api.nvim_win_get_cursor(win))
1204    check_buffer_lines(0, 99)
1205    check_common()
1206  end)
1207
1208  it('with nvim_open_term() on buffer with more lines than scrollback', function()
1209    api.nvim_set_option_value('scrollback', 10, { buf = buf })
1210    exec_lua(function()
1211      vim.api.nvim_open_term(buf, {})
1212      vim.api.nvim_win_set_cursor(win, { 3, 3 })
1213    end)
1214    screen:expect([[
1215      ^          │{100:TEST} 86            |
1216      {1:~         }│{100:TEST} 87            |
1217      {1:~         }│{100:TEST} 88            |
1218      {1:~         }│{100:TEST} 89            |
1219      {1:~         }│{100:TEST} 90            |
1220      {3:[No Name]  }{101:[Scratch] [-]      }|
1221      99 lines changed              |
1222    ]])
1223    eq({ 1, 0 }, api.nvim_win_get_cursor(win))
1224    check_buffer_lines(86, 99)
1225    check_common()
1226  end)
1227
1228  describe('when window height', function()
1229    before_each(function()
1230      feed('<C-W>lGV4kdgg')
1231      screen:try_resize(30, 20)
1232      command('botright 9new | wincmd p')
1233      exec_lua(function()
1234        vim.g.chan = vim.api.nvim_open_term(buf, {})
1235        vim.cmd('$')
1236      end)
1237      screen:expect([[
1238                  │{100:TEST} 88            |
1239        {1:~         }│{100:TEST} 89            |
1240        {1:~         }│{100:TEST} 90            |
1241        {1:~         }│{100:TEST} 91            |
1242        {1:~         }│{100:TEST} 92            |
1243        {1:~         }│{100:TEST} 93            |
1244        {1:~         }│{100:TEST} 94            |
1245        {1:~         }│^                   |
1246        {2:[No Name]  }{102:[Scratch] [-]      }|
1247                                      |
1248        {1:~                             }|*8
1249        {2:[No Name]                     }|
1250                                      |
1251      ]])
1252      check_buffer_lines(0, 94)
1253    end)
1254
1255    local send_cmd = 'call chansend(g:chan, @")'
1256
1257    describe('increases in the same refresh cycle as outputting lines', function()
1258      --- @type string[][]
1259      local perms = t.concat_tables(
1260        t.permutations({ 'resize +2', send_cmd }),
1261        t.permutations({ 'resize +4', 'resize -2', send_cmd }),
1262        t.permutations({ 'resize +6', 'resize -4', send_cmd })
1263      )
1264      assert(#perms == 2 + 6 + 6)
1265      local screen_final = [[
1266                  │{100:TEST} 91            |
1267        {1:~         }│{100:TEST} 92            |
1268        {1:~         }│{100:TEST} 93            |
1269        {1:~         }│{100:TEST} 94            |
1270        {1:~         }│{100:TEST} 95            |
1271        {1:~         }│{100:TEST} 96            |
1272        {1:~         }│{100:TEST} 97            |
1273        {1:~         }│{100:TEST} 98            |
1274        {1:~         }│{100:TEST} 99            |
1275        {1:~         }│^                   |
1276        {2:[No Name]  }{102:[Scratch] [-]      }|
1277                                      |
1278        {1:~                             }|*6
1279        {2:[No Name]                     }|
1280                                      |
1281      ]]
1282
1283      for i, perm in ipairs(perms) do
1284        it(('permutation %d'):format(i), function()
1285          exec_lua(function()
1286            for _, cmd in ipairs(perm) do
1287              vim.cmd(cmd)
1288            end
1289          end)
1290          screen:expect(screen_final)
1291          check_buffer_lines(0, 99)
1292        end)
1293      end
1294
1295      describe('with full scrollback,', function()
1296        before_each(function()
1297          api.nvim_set_option_value('scrollback', 6, { buf = buf })
1298          check_buffer_lines(82, 94)
1299        end)
1300
1301        it('output first', function()
1302          command(send_cmd .. ' | resize +2')
1303          screen:expect(screen_final)
1304          check_buffer_lines(87, 99)
1305        end)
1306
1307        it('resize first', function()
1308          command('resize +2 | ' .. send_cmd)
1309          screen:expect(screen_final)
1310          check_buffer_lines(85, 99)
1311        end)
1312      end)
1313    end)
1314
1315    describe('decreases in the same refresh cycle as outputting lines', function()
1316      --- @type string[][]
1317      local perms = t.concat_tables(
1318        t.permutations({ 'resize -2', send_cmd }),
1319        t.permutations({ 'resize -4', 'resize +2', send_cmd }),
1320        t.permutations({ 'resize -6', 'resize +4', send_cmd })
1321      )
1322      assert(#perms == 2 + 6 + 6)
1323      local screen_final = [[
1324                  │{100:TEST} 95            |
1325        {1:~         }│{100:TEST} 96            |
1326        {1:~         }│{100:TEST} 97            |
1327        {1:~         }│{100:TEST} 98            |
1328        {1:~         }│{100:TEST} 99            |
1329        {1:~         }│^                   |
1330        {2:[No Name]  }{102:[Scratch] [-]      }|
1331                                      |
1332        {1:~                             }|*10
1333        {2:[No Name]                     }|
1334                                      |
1335      ]]
1336
1337      for i, perm in ipairs(perms) do
1338        it(('permutation %d'):format(i), function()
1339          exec_lua(function()
1340            for _, cmd in ipairs(perm) do
1341              vim.cmd(cmd)
1342            end
1343          end)
1344          screen:expect(screen_final)
1345          check_buffer_lines(0, 99)
1346        end)
1347      end
1348    end)
1349
1350    describe("decreases by more than 'scrollback'", function()
1351      before_each(function()
1352        api.nvim_set_option_value('scrollback', 4, { buf = buf })
1353        check_buffer_lines(84, 94)
1354      end)
1355
1356      local perms = {
1357        { send_cmd, 'resize -6' },
1358        { 'resize -6', send_cmd },
1359        { send_cmd, 'resize +6', 'resize -12' },
1360        { 'resize +6', send_cmd, 'resize -12' },
1361        { 'resize +6', 'resize -12', send_cmd },
1362      }
1363      local screen_final = [[
1364                  │{100:TEST} 99            |
1365        {1:~         }│^                   |
1366        {2:[No Name]  }{102:[Scratch] [-]      }|
1367                                      |
1368        {1:~                             }|*14
1369        {2:[No Name]                     }|
1370                                      |
1371      ]]
1372
1373      for i, perm in ipairs(perms) do
1374        it(('permutation %d'):format(i), function()
1375          exec_lua(function()
1376            for _, cmd in ipairs(perm) do
1377              vim.cmd(cmd)
1378            end
1379          end)
1380          screen:expect(screen_final)
1381          check_buffer_lines(95, 99)
1382        end)
1383      end
1384    end)
1385  end)
1386end)