robin/laufey

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

robinmerge: 9a29622b3b00ddb601

master
34.2 KiB1156 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3local Screen = require('test.functional.ui.screen')
4
5local describe, it, before_each = t.describe, t.it, t.before_each
6local feed = n.feed
7local fn = n.call
8local source = n.source
9local clear = n.clear
10local command = n.command
11local expect = n.expect
12local poke_eventloop = n.poke_eventloop
13local api = n.api
14local eq = t.eq
15local pcall_err = t.pcall_err
16local neq = t.neq
17local exec_lua = n.exec_lua
18
19describe('prompt buffer', function()
20  local screen
21
22  before_each(function()
23    clear()
24    screen = Screen.new(25, 10)
25    command('set laststatus=0 nohidden')
26  end)
27
28  local function source_script()
29    source([[
30      func TextEntered(text)
31        if a:text == "exit"
32          stopinsert
33          close
34        else
35          " Add the output above the current prompt.
36          call prompt_appendbuf(bufnr(''), split('Command: "' . a:text . '"', '\n'))
37          " Reset &modified to allow the buffer to be closed.
38          set nomodified
39          call timer_start(20, {id -> TimerFunc(a:text)})
40        endif
41      endfunc
42
43      func TimerFunc(text)
44        " Add the output above the current prompt.
45        call prompt_appendbuf(bufnr(''),  split('Result: "' . a:text .'"', '\n'))
46        " Reset &modified to allow the buffer to be closed.
47        set nomodified
48      endfunc
49
50      func SwitchWindows()
51        call timer_start(0, {-> execute("wincmd p", "")})
52      endfunc
53
54      call setline(1, "other buffer")
55      set nomodified
56      new
57      set buftype=prompt
58      call prompt_setcallback(bufnr(''), function("TextEntered"))
59      eval bufnr("")->prompt_setprompt("cmd: ")
60      startinsert
61    ]])
62    screen:expect([[
63      cmd: ^                    |
64      {1:~                        }|*3
65      {3:[Prompt]                 }|
66      other buffer             |
67      {1:~                        }|*3
68      {5:-- INSERT --}             |
69    ]])
70  end
71
72  -- oldtest: Test_prompt_basic()
73  it('works', function()
74    source_script()
75    feed('hello\n')
76    screen:expect([[
77      cmd: hello               |
78      Command: "hello"         |
79      Result: "hello"          |
80      cmd: ^                    |
81      {3:[Prompt]                 }|
82      other buffer             |
83      {1:~                        }|*3
84      {5:-- INSERT --}             |
85    ]])
86    feed('exit\n')
87    screen:expect([[
88      ^other buffer             |
89      {1:~                        }|*8
90                               |
91    ]])
92
93    command('new')
94    command('set buftype=prompt')
95    feed('iabc<BS><BS>')
96    eq('a', fn('prompt_getinput', fn('bufnr')))
97    command('quit')
98    eq(1, #api.nvim_list_wins())
99
100    command('new')
101    command('set buftype=prompt modified')
102    eq(
103      'Vim(quit):E37: No write since last change (add ! to override)',
104      t.pcall_err(command, 'quit')
105    )
106    eq(2, #api.nvim_list_wins())
107  end)
108
109  -- oldtest: Test_prompt_editing()
110  it('editing', function()
111    source_script()
112    feed('hello<BS><BS>')
113    screen:expect([[
114      cmd: hel^                 |
115      {1:~                        }|*3
116      {3:[Prompt]                 }|
117      other buffer             |
118      {1:~                        }|*3
119      {5:-- INSERT --}             |
120    ]])
121    feed('<Left><Left><Left><BS>-')
122    screen:expect([[
123      cmd: -^hel                |
124      {1:~                        }|*3
125      {3:[Prompt]                 }|
126      other buffer             |
127      {1:~                        }|*3
128      {5:-- INSERT --}             |
129    ]])
130    feed('<C-O>lz')
131    screen:expect([[
132      cmd: -hz^el               |
133      {1:~                        }|*3
134      {3:[Prompt]                 }|
135      other buffer             |
136      {1:~                        }|*3
137      {5:-- INSERT --}             |
138    ]])
139    feed('<End>x')
140    screen:expect([[
141      cmd: -hzelx^              |
142      {1:~                        }|*3
143      {3:[Prompt]                 }|
144      other buffer             |
145      {1:~                        }|*3
146      {5:-- INSERT --}             |
147    ]])
148
149    -- :edit doesn't apply on prompt buffer
150    eq('Vim(edit):cannot :edit a prompt buffer', t.pcall_err(api.nvim_command, 'edit'))
151
152    feed('<C-U>exit\n')
153    screen:expect([[
154      ^other buffer             |
155      {1:~                        }|*8
156                               |
157    ]])
158  end)
159
160  -- oldtest: Test_prompt_switch_windows()
161  it('switch windows', function()
162    source_script()
163    feed('<C-O>:call SwitchWindows()<CR>')
164    screen:expect([[
165      cmd:                     |
166      {1:~                        }|*3
167      {2:[Prompt]                 }|
168      ^other buffer             |
169      {1:~                        }|*3
170                               |
171    ]])
172    feed('<C-O>:call SwitchWindows()<CR>')
173    screen:expect([[
174      cmd: ^                    |
175      {1:~                        }|*3
176      {3:[Prompt]                 }|
177      other buffer             |
178      {1:~                        }|*3
179      {5:-- INSERT --}             |
180    ]])
181    feed('<Esc>')
182    screen:expect([[
183      cmd:^                     |
184      {1:~                        }|*3
185      {3:[Prompt]                 }|
186      other buffer             |
187      {1:~                        }|*3
188                               |
189    ]])
190  end)
191
192  -- oldtest: Test_prompt_while_writing_to_hidden_buffer()
193  it('keeps insert mode after aucmd_restbuf in callback', function()
194    source_script()
195    source [[
196      let s:buf = nvim_create_buf(1, 1)
197      call timer_start(0, {-> nvim_buf_set_lines(s:buf, -1, -1, 0, ['walrus'])})
198    ]]
199    poke_eventloop()
200    eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
201  end)
202
203  -- oldtest: Test_prompt_appending_while_hidden()
204  it('accessing hidden prompt buffer does not start insert mode', function()
205    local prev_win = api.nvim_get_current_win()
206    source([[
207      new prompt
208      set buftype=prompt
209      set bufhidden=hide
210
211      func s:TextEntered(text)
212          if a:text == 'exit'
213              close
214          endif
215          let g:entered = a:text
216      endfunc
217      call prompt_setcallback(bufnr(), function('s:TextEntered'))
218
219      func DoAppend(cmd_before = '')
220        exe a:cmd_before
221        call appendbufline('prompt', '$', 'Test')
222        return ''
223      endfunc
224
225      autocmd User SwitchTabPages tabprevious | tabnext
226      func DoAutoAll(cmd_before = '')
227        exe a:cmd_before
228        doautoall User SwitchTabPages
229        return ''
230      endfunc
231    ]])
232    feed('asomething<CR>')
233    eq('something', api.nvim_get_var('entered'))
234    neq(prev_win, api.nvim_get_current_win())
235    feed('exit<CR>')
236    eq(prev_win, api.nvim_get_current_win())
237    eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
238    command('call DoAppend()')
239    eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
240    feed('i')
241    eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
242    command('call DoAppend()')
243    eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
244    command("call DoAppend('stopinsert')")
245    eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
246    command("call DoAppend('startreplace')")
247    eq({ mode = 'R', blocking = false }, api.nvim_get_mode())
248    feed('<Esc>')
249    command('tabnew')
250    eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
251    command("call DoAutoAll('startinsert')")
252    eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
253    command("call DoAutoAll('stopinsert')")
254    eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
255  end)
256
257  -- oldtest: Test_prompt_leave_modify_hidden()
258  it('modifying hidden buffer does not prevent prompt buffer mode change', function()
259    source([[
260      file hidden
261      set bufhidden=hide
262      enew
263      new prompt
264      set buftype=prompt
265
266      inoremap <buffer> w <Cmd>wincmd w<CR>
267      inoremap <buffer> q <Cmd>bwipe!<CR>
268      autocmd BufLeave prompt call appendbufline('hidden', '$', 'Leave')
269      autocmd BufEnter prompt call appendbufline('hidden', '$', 'Enter')
270      autocmd BufWinLeave prompt call appendbufline('hidden', '$', 'Close')
271    ]])
272    feed('a')
273    eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
274    feed('w')
275    eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
276    feed('<C-W>w')
277    eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
278    feed('q')
279    eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
280    command('bwipe!')
281    expect([[
282
283      Leave
284      Enter
285      Leave
286      Close]])
287  end)
288
289  it('can insert multiline text', function()
290    source_script()
291    local buf = api.nvim_get_current_buf()
292
293    feed('line 1<s-cr>line 2<s-cr>line 3')
294    screen:expect([[
295      cmd: line 1              |
296      line 2                   |
297      line 3^                   |
298      {1:~                        }|
299      {3:[Prompt]                 }|
300      other buffer             |
301      {1:~                        }|*3
302      {5:-- INSERT --}             |
303    ]])
304
305    -- prompt_getinput works with multiline input
306    eq('line 1\nline 2\nline 3', fn('prompt_getinput', buf))
307
308    feed('<cr>')
309    -- submitting multiline text works
310    screen:expect([[
311      Result: "line 1          |
312      line 2                   |
313      line 3"                  |
314      cmd: ^                    |
315      {3:[Prompt]                 }|
316      other buffer             |
317      {1:~                        }|*3
318      {5:-- INSERT --}             |
319    ]])
320
321    eq('', fn('prompt_getinput', buf))
322
323    -- % prompt is not repeated with formatoptions+=r
324    source([[
325      bwipeout!
326      set formatoptions+=r
327      call prompt_setprompt(bufnr(), "% ")
328      set buftype=prompt
329    ]])
330    feed('iline1<s-cr>line2')
331    screen:expect([[
332      other buffer             |
333      % line1                  |
334      line2^                    |
335      {1:~                        }|*6
336      {5:-- INSERT --}             |
337    ]])
338
339    -- ensure cursor gets placed on first line of user input.
340    -- when insert mode is entered from read-only region of prompt buffer.
341    local prompt_pos = api.nvim_buf_get_mark(0, ':')
342    feed('<esc>')
343    -- works before prompt
344    api.nvim_win_set_cursor(0, { prompt_pos[1] - 1, 0 })
345    screen:expect([[
346      ^other buffer             |
347      % line1                  |
348      line2                    |
349      {1:~                        }|*6
350                               |
351    ]])
352    feed('a')
353    feed('<esc>')
354    screen:expect([[
355      other buffer             |
356      %^ line1                  |
357      line2                    |
358      {1:~                        }|*6
359                               |
360    ]])
361    -- works on prompt
362    api.nvim_win_set_cursor(0, { prompt_pos[1], 0 })
363    screen:expect([[
364      other buffer             |
365      ^% line1                  |
366      line2                    |
367      {1:~                        }|*6
368                               |
369    ]])
370    feed('a')
371    feed('<esc>')
372    screen:expect([[
373      other buffer             |
374      %^ line1                  |
375      line2                    |
376      {1:~                        }|*6
377                               |
378    ]])
379
380    -- i_<Left> i_<C-Left> i_<Home> i_<End> keys on prompt-line doesn't put cursor
381    -- at end of text
382    feed('a<Left><C-Left>')
383    screen:expect([[
384      other buffer             |
385      % ^line1                  |
386      line2                    |
387      {1:~                        }|*6
388      {5:-- INSERT --}             |
389    ]])
390
391    feed('<End>')
392    screen:expect([[
393      other buffer             |
394      % line1^                  |
395      line2                    |
396      {1:~                        }|*6
397      {5:-- INSERT --}             |
398    ]])
399
400    feed('<Home>')
401    screen:expect([[
402      other buffer             |
403      % ^line1                  |
404      line2                    |
405      {1:~                        }|*6
406      {5:-- INSERT --}             |
407    ]])
408  end)
409
410  it('can put (p) multiline text', function()
411    source_script()
412    local buf = api.nvim_get_current_buf()
413
414    fn('setreg', 'a', 'line 1\nline 2\nline 3')
415    feed('<esc>"ap')
416    screen:expect([[
417      cmd: ^line 1              |
418      line 2                   |
419      line 3                   |
420      {1:~                        }|
421      {3:[Prompt]                 }|
422      other buffer             |
423      {1:~                        }|*3
424                               |
425    ]])
426
427    -- prompt_getinput works with pasted input
428    eq('line 1\nline 2\nline 3', fn('prompt_getinput', buf))
429
430    feed('i<cr>')
431    screen:expect([[
432      Result: "line 1          |
433      line 2                   |
434      line 3"                  |
435      cmd: ^                    |
436      {3:[Prompt]                 }|
437      other buffer             |
438      {1:~                        }|*3
439      {5:-- INSERT --}             |
440    ]])
441  end)
442
443  it('can put multiline text with nvim_paste', function()
444    source_script()
445    api.nvim_paste('line 1\nline 2\nline 3', false, -1)
446    screen:expect([[
447      cmd: line 1              |
448      line 2                   |
449      line 3^                   |
450      {1:~                        }|
451      {3:[Prompt]                 }|
452      other buffer             |
453      {1:~                        }|*3
454      {5:-- INSERT --}             |
455    ]])
456  end)
457
458  it('can undo current prompt', function()
459    source_script()
460    local buf = api.nvim_get_current_buf()
461
462    -- text editing allowed in current prompt
463    feed('tests-initial<esc>')
464    feed('bimiddle-<esc>')
465    screen:expect([[
466      cmd: tests-middle^-initial|
467      {1:~                        }|*3
468      {3:[Prompt]                 }|
469      other buffer             |
470      {1:~                        }|*3
471                               |
472    ]])
473
474    feed('Fdx')
475    screen:expect([[
476      cmd: tests-mid^le-initial |
477      {1:~                        }|*3
478      {3:[Prompt]                 }|
479      other buffer             |
480      {1:~                        }|*3
481                               |
482    ]])
483
484    -- can undo edits until prompt has been submitted
485    feed('u')
486    screen:expect([[
487      cmd: tests-mid^dle-initial|
488      {1:~                        }|*3
489      {3:[Prompt]                 }|
490      other buffer             |
491      {1:~                        }|*3
492      1 change; {MATCH:.*} |
493    ]])
494
495    -- undo is reflected in prompt_getinput
496    eq('tests-middle-initial', fn('prompt_getinput', buf))
497
498    feed('u')
499    screen:expect([[
500      cmd: tests-^initial       |
501      {1:~                        }|*3
502      {3:[Prompt]                 }|
503      other buffer             |
504      {1:~                        }|*3
505      1 change; {MATCH:.*} |
506    ]])
507
508    feed('i<cr><esc>')
509    screen:expect([[
510      cmd: tests-initial       |
511      Command: "tests-initial" |
512      Result: "tests-initial"  |
513      cmd:^                     |
514      {3:[Prompt]                 }|
515      other buffer             |
516      {1:~                        }|*3
517                               |
518    ]])
519
520    -- after submit undo does nothing
521    feed('u')
522    screen:expect([[
523      cmd: tests-initial       |
524      Command: "tests-initial" |
525      cmd:^                     |
526      {1:~                        }|
527      {3:[Prompt]                 }|
528      other buffer             |
529      {1:~                        }|*3
530      1 line {MATCH:.*} |
531    ]])
532
533    -- "S" does not clear undo
534    feed('ihello<Esc>S')
535    screen:expect([[
536      cmd: tests-initial       |
537      Command: "tests-initial" |
538      cmd: ^                    |
539      {1:~                        }|
540      {3:[Prompt]                 }|
541      other buffer             |
542      {1:~                        }|*3
543      {5:-- INSERT --}             |
544    ]])
545    feed('<Esc>u')
546    -- Nvim: the cursor returns to its pre-"S" position (|restore-undo-cursor|).
547    screen:expect([[
548      cmd: tests-initial       |
549      Command: "tests-initial" |
550      cmd: hell^o               |
551      {1:~                        }|
552      {3:[Prompt]                 }|
553      other buffer             |
554      {1:~                        }|*3
555      1 change; {MATCH:.*} |
556    ]])
557
558    -- undo cleared if prompt changes
559    -- (otherwise undoing would abort it and append a new prompt, which isn't useful)
560    fn('prompt_setprompt', '', 'cmd > ')
561    feed('u')
562    screen:expect([[
563      cmd: tests-initial       |
564      Command: "tests-initial" |
565      cmd > hell^o              |
566      {1:~                        }|
567      {3:[Prompt]                 }|
568      other buffer             |
569      {1:~                        }|*3
570      Already at oldest change |
571    ]])
572
573    -- new prompt line appended to fix missing prompt also clears undo
574    feed('A there')
575    fn('setpos', "':", { 0, fn('line', '.'), 99, 0 })
576    feed('<Esc>u')
577    screen:expect([[
578      cmd: tests-initial       |
579      Command: "tests-initial" |
580      cmd > hello there        |
581      cmd >^                    |
582      {3:[Prompt]                 }|
583      other buffer             |
584      {1:~                        }|*3
585      Already at oldest change |
586    ]])
587  end)
588
589  it('o/O can create new lines', function()
590    source_script()
591    local buf = api.nvim_get_current_buf()
592
593    feed('line 1<s-cr>line 2<s-cr>line 3')
594    screen:expect([[
595      cmd: line 1              |
596      line 2                   |
597      line 3^                   |
598      {1:~                        }|
599      {3:[Prompt]                 }|
600      other buffer             |
601      {1:~                        }|*3
602      {5:-- INSERT --}             |
603    ]])
604
605    feed('<esc>koafter')
606    screen:expect([[
607      cmd: line 1              |
608      line 2                   |
609      after^                    |
610      line 3                   |
611      {3:[Prompt]                 }|
612      other buffer             |
613      {1:~                        }|*3
614      {5:-- INSERT --}             |
615    ]])
616
617    -- newline created with o is reflected in prompt_getinput
618    eq('line 1\nline 2\nafter\nline 3', fn('prompt_getinput', buf))
619
620    feed('<esc>kObefore')
621
622    screen:expect([[
623      cmd: line 1              |
624      before^                   |
625      line 2                   |
626      after                    |
627      {3:[Prompt]                 }|
628      other buffer             |
629      {1:~                        }|*3
630      {5:-- INSERT --}             |
631    ]])
632
633    -- newline created with O is reflected in prompt_getinput
634    eq('line 1\nbefore\nline 2\nafter\nline 3', fn('prompt_getinput', buf))
635
636    feed('<cr>')
637    vim.uv.sleep(20)
638    eq('', fn('prompt_getinput', buf))
639    screen:expect([[
640      line 2                   |
641      after                    |
642      line 3"                  |
643      cmd: ^                    |
644      {3:[Prompt]                 }|
645      other buffer             |
646      {1:~                        }|*3
647      {5:-- INSERT --}             |
648    ]])
649
650    feed('line 4<s-cr>line 5')
651    screen:expect([[
652      after                    |
653      line 3"                  |
654      cmd: line 4              |
655      line 5^                   |
656      {3:[Prompt]                 }|
657      other buffer             |
658      {1:~                        }|*3
659      {5:-- INSERT --}             |
660    ]])
661
662    feed('<esc>k0oafter prompt')
663    screen:expect([[
664      after                    |
665      line 3"                  |
666      cmd: line 4              |
667      after prompt^             |
668      {3:[Prompt]                 }|
669      other buffer             |
670      {1:~                        }|*3
671      {5:-- INSERT --}             |
672    ]])
673
674    feed('<esc>k0Oat prompt')
675    screen:expect([[
676      after                    |
677      line 3"                  |
678      cmd: at prompt^           |
679      line 4                   |
680      {3:[Prompt]                 }|
681      other buffer             |
682      {1:~                        }|*3
683      {5:-- INSERT --}             |
684    ]])
685
686    feed('<cr>')
687    vim.uv.sleep(20)
688    eq('', fn('prompt_getinput', buf))
689    screen:expect([[
690      line 4                   |
691      after prompt             |
692      line 5"                  |
693      cmd: ^                    |
694      {3:[Prompt]                 }|
695      other buffer             |
696      {1:~                        }|*3
697      {5:-- INSERT --}             |
698    ]])
699  end)
700
701  it('deleting prompt adds it back on insert', function()
702    source_script()
703    feed('asdf')
704    screen:expect([[
705      cmd: asdf^                |
706      {1:~                        }|*3
707      {3:[Prompt]                 }|
708      other buffer             |
709      {1:~                        }|*3
710      {5:-- INSERT --}             |
711    ]])
712
713    feed('<esc>ddi')
714    screen:expect([[
715      cmd: ^                    |
716      {1:~                        }|*3
717      {3:[Prompt]                 }|
718      other buffer             |
719      {1:~                        }|*3
720      {5:-- INSERT --}             |
721    ]])
722
723    feed('asdf')
724    screen:expect([[
725      cmd: asdf^                |
726      {1:~                        }|*3
727      {3:[Prompt]                 }|
728      other buffer             |
729      {1:~                        }|*3
730      {5:-- INSERT --}             |
731    ]])
732
733    feed('<esc>cc')
734    screen:expect([[
735      cmd: ^                    |
736      {1:~                        }|*3
737      {3:[Prompt]                 }|
738      other buffer             |
739      {1:~                        }|*3
740      {5:-- INSERT --}             |
741    ]])
742  end)
743
744  it("sets the ': mark", function()
745    api.nvim_set_option_value('buftype', 'prompt', { buf = 0 })
746    exec_lua(function()
747      local buf = vim.api.nvim_get_current_buf()
748      vim.fn.prompt_setprompt(buf, 'cmd > ')
749      vim.fn.prompt_setcallback(buf, function(str)
750        local last_line = vim.api.nvim_buf_line_count(buf)
751        vim.api.nvim_buf_set_lines(buf, last_line - 1, last_line - 1, true, vim.split(str, '\n'))
752      end)
753    end)
754
755    feed('asdf')
756    eq({ 1, 6 }, api.nvim_buf_get_mark(0, ':'))
757    feed('<cr>')
758    eq({ 3, 6 }, api.nvim_buf_get_mark(0, ':'))
759    -- Multiline prompt.
760    feed('<s-cr>line1<s-cr>line2<s-cr>line3<cr>')
761    eq({ 11, 6 }, api.nvim_buf_get_mark(0, ':'))
762
763    -- ': mark is only available in prompt buffer.
764    api.nvim_set_option_value('buftype', '', { buf = 0 })
765    eq("Invalid mark name: ':'", t.pcall_err(api.nvim_buf_get_mark, 0, ':'))
766
767    -- mark can be moved
768    api.nvim_set_option_value('buftype', 'prompt', { buf = 0 })
769    local last_line = api.nvim_buf_line_count(0)
770    eq({ last_line, 6 }, api.nvim_buf_get_mark(0, ':'))
771    eq(true, api.nvim_buf_set_mark(0, ':', 1, 5, {}))
772    eq({ 1, 5 }, api.nvim_buf_get_mark(0, ':'))
773
774    -- No crash from invalid col.
775    eq(true, api.nvim_buf_set_mark(0, ':', fn('line', '.'), 999, {}))
776    eq({ 12, 6 }, api.nvim_buf_get_mark(0, ':'))
777
778    -- Clamps lnum to at least 1. Do in one event to repro the leak.
779    exec_lua(function()
780      vim.fn.setpos("':", { 0, 0, 0, 0 })
781      vim.fn.prompt_setprompt('', 'bar > ')
782    end)
783    eq({ 1, 6 }, api.nvim_buf_get_mark(0, ':'))
784
785    -- No ml_get error from invalid lnum.
786    command('set messagesopt+=wait:0 messagesopt-=hit-enter')
787    fn('setpos', "':", { 0, 999, 7, 0 })
788    eq('', api.nvim_get_vvar('errmsg'))
789    command('set messagesopt&')
790    eq({ 13, 6 }, api.nvim_buf_get_mark(0, ':'))
791  end)
792
793  describe('prompt_getinput', function()
794    it('returns current prompts text', function()
795      command('new')
796      local bufnr = fn('bufnr')
797      api.nvim_set_option_value('buftype', 'prompt', { buf = 0 })
798      eq('', fn('prompt_getinput', bufnr))
799      feed('iasdf')
800      eq('asdf', fn('prompt_getinput', bufnr))
801      feed('<esc>dd')
802      eq('', fn('prompt_getinput', bufnr))
803      feed('iasdf2')
804      eq('asdf2', fn('prompt_getinput', bufnr))
805
806      -- returns empty string when called from non prompt buffer
807      api.nvim_set_option_value('buftype', '', { buf = 0 })
808      eq('', fn('prompt_getinput', bufnr))
809    end)
810  end)
811
812  it('programmatic (non-user) edits', function()
813    api.nvim_set_option_value('buftype', 'prompt', { buf = 0 })
814
815    -- with nvim_buf_set_lines
816    exec_lua([[
817      local buf = vim.api.nvim_get_current_buf()
818      vim.fn.prompt_setcallback(buf, function(text)
819        vim.api.nvim_buf_set_lines(buf, -2, -2, true, vim.split(text, '\n'))
820      end)
821    ]])
822    feed('iset_lines<cr>')
823    feed('set_lines2<cr>')
824    screen:expect([[
825      % set_lines              |
826      set_lines                |
827      % set_lines2             |
828      set_lines2               |
829      % ^                       |
830      {1:~                        }|*4
831      {5:-- INSERT --}             |
832    ]])
833
834    feed('set_lines3(multi-1)<s-cr>set_lines3(multi-2)<cr>')
835    screen:expect([[
836      % set_lines              |
837      set_lines                |
838      % set_lines2             |
839      set_lines2               |
840      % set_lines3(multi-1)    |
841      set_lines3(multi-2)      |
842      set_lines3(multi-1)      |
843      set_lines3(multi-2)      |
844      % ^                       |
845      {5:-- INSERT --}             |
846    ]])
847    -- with nvim_buf_set_text
848    source('bwipeout!')
849    api.nvim_set_option_value('buftype', 'prompt', { buf = 0 })
850    exec_lua([[
851      local buf = vim.api.nvim_get_current_buf()
852      vim.fn.prompt_setcallback(buf, function(text)
853        local lines = vim.split(text, '\n')
854        if lines[#lines] ~= '' then
855          table.insert(lines, '')
856        end
857        vim.api.nvim_buf_set_text(buf, -1, 0, -1, 0, lines)
858      end)
859    ]])
860    feed('set_text<cr>')
861    feed('set_text2<cr>')
862    screen:expect([[
863      % set_text               |
864      set_text                 |
865      % set_text2              |
866      set_text2                |
867      % ^                       |
868      {1:~                        }|*4
869      {5:-- INSERT --}             |
870    ]])
871
872    feed('set_text3(multi-1)<s-cr>set_text3(multi-2)<cr>')
873    screen:expect([[
874      % set_text               |
875      set_text                 |
876      % set_text2              |
877      set_text2                |
878      % set_text3(multi-1)     |
879      set_text3(multi-2)       |
880      set_text3(multi-1)       |
881      set_text3(multi-2)       |
882      % ^                       |
883      {5:-- INSERT --}             |
884    ]])
885  end)
886
887  it('works correctly with empty string as prompt', function()
888    api.nvim_set_option_value('buftype', 'prompt', { buf = 0 })
889    exec_lua(function()
890      local buf = vim.api.nvim_get_current_buf()
891      vim.fn.prompt_setprompt(buf, '')
892    end)
893
894    source('startinsert')
895
896    -- mark correctly set
897    eq({ 1, 0 }, api.nvim_buf_get_mark(0, ':'))
898
899    feed('asdf')
900    screen:expect([[
901      asdf^                     |
902      {1:~                        }|*8
903      {5:-- INSERT --}             |
904    ]])
905
906    -- can clear all of it
907    feed('<backspace><backspace><backspace><backspace>')
908    screen:expect([[
909      ^                         |
910      {1:~                        }|*8
911      {5:-- INSERT --}             |
912    ]])
913    feed('<cr>')
914
915    eq({ 2, 0 }, api.nvim_buf_get_mark(0, ':'))
916  end)
917
918  it('prompt can be changed without interrupting user input', function()
919    api.nvim_set_option_value('buftype', 'prompt', { buf = 0 })
920    local buf = api.nvim_get_current_buf()
921
922    local function set_prompt(prompt, b)
923      fn('prompt_setprompt', b or buf, prompt)
924    end
925
926    set_prompt('> ')
927
928    source('startinsert')
929
930    feed('user input')
931    -- Move the cursor a bit to check cursor maintaining position
932    feed('<esc>hhi')
933
934    screen:expect([[
935      > user in^put             |
936      {1:~                        }|*8
937      {5:-- INSERT --}             |
938    ]])
939
940    eq({ 1, 2 }, api.nvim_buf_get_mark(0, ':'))
941
942    set_prompt('new-prompt > ')
943
944    screen:expect([[
945      new-prompt > user in^put  |
946      {1:~                        }|*8
947      {5:-- INSERT --}             |
948    ]])
949
950    eq({ 1, 13 }, api.nvim_buf_get_mark(0, ':'))
951
952    set_prompt('new-prompt(status) > ')
953
954    screen:expect([[
955      new-prompt(status) > user|
956       in^put                   |
957      {1:~                        }|*7
958      {5:-- INSERT --}             |
959    ]])
960    eq({ 1, 21 }, api.nvim_buf_get_mark(0, ':'))
961
962    set_prompt('new-prompt > ')
963
964    screen:expect([[
965      new-prompt > user in^put  |
966      {1:~                        }|*8
967      {5:-- INSERT --}             |
968    ]])
969    eq({ 1, 13 }, api.nvim_buf_get_mark(0, ':'))
970
971    -- Cursor not moved when not on the prompt line.
972    feed('<CR>user input<Esc>k')
973    screen:expect([[
974      new-prompt > user inpu^t  |
975      new-prompt > user input  |
976      {1:~                        }|*7
977                               |
978    ]])
979    set_prompt('<>< ')
980    screen:expect([[
981      new-prompt > user inpu^t  |
982      <>< user input           |
983      {1:~                        }|*7
984                               |
985    ]])
986    -- Correct col when prompt has multi-cell chars.
987    feed('i<Left><Left>')
988    screen:expect([[
989      new-prompt > user input  |
990      <>< user inp^ut           |
991      {1:~                        }|*7
992      {5:-- INSERT --}             |
993    ]])
994    set_prompt('\t > ')
995    screen:expect([[
996      new-prompt > user input  |
997               > user inp^ut    |
998      {1:~                        }|*7
999      {5:-- INSERT --}             |
1000    ]])
1001    -- Works with 'virtualedit': coladd remains sensible. Cursor is redrawn correctly.
1002    -- Tab size visually changes due to multiples of 'tabstop'.
1003    command('set virtualedit=all')
1004    feed('<C-O>Sa<Tab>b<C-O>3h')
1005    screen:expect([[
1006      new-prompt > user input  |
1007               > a  ^  b        |
1008      {1:~                        }|*7
1009      {5:-- INSERT --}             |
1010    ]])
1011    set_prompt('😊 > ')
1012    screen:expect([[
1013      new-prompt > user input  |
1014      😊 > a ^ b                |
1015      {1:~                        }|*7
1016      {5:-- INSERT --}             |
1017    ]])
1018    -- Minimum col should be 1. Same event to avoid corrections from the state loop.
1019    feed('<Esc>0')
1020    local colnr = exec_lua(function()
1021      vim.fn.prompt_setprompt('', '')
1022      return vim.fn.col('.')
1023    end)
1024    eq(1, colnr)
1025    -- Correct cursor adjustment when old ': col and old prompt length differs.
1026    set_prompt('foo > ')
1027    fn('setpos', "':", { 0, fn('line', '.'), 10, 0 })
1028    fn('setline', '.', '   foo > hello')
1029    feed('fh')
1030    screen:expect([[
1031      new-prompt > user input  |
1032         foo > ^hello           |
1033      {1:~                        }|*7
1034                               |
1035    ]])
1036    set_prompt('bar > ')
1037    screen:expect([[
1038      new-prompt > user input  |
1039      bar > ^hello              |
1040      {1:~                        }|*7
1041                               |
1042    ]])
1043
1044    -- No crash when setting shorter prompt than curbuf's in other buffer.
1045    feed('ztA')
1046    command('set virtualedit& | new | setlocal buftype=prompt')
1047    set_prompt('looooooooooooooooooooooooooooooooooooooooooooong > ', '') -- curbuf
1048    set_prompt('foo > ')
1049    screen:expect([[
1050      loooooooooooooooooooooooo|
1051      ooooooooooooooooooooong >|
1052       ^                        |
1053      {1:~                        }|
1054      {3:[Prompt]                 }|
1055      foo > hello              |
1056      {1:~                        }|*3
1057      {5:-- INSERT --}             |
1058    ]])
1059
1060    -- No prompt_setprompt crash from invalid ': col. Must happen in the same event.
1061    exec_lua(function()
1062      vim.cmd 'bwipeout!'
1063      vim.api.nvim_buf_set_mark(0, ':', vim.fn.line('.'), 999, {})
1064      vim.fn.prompt_setprompt('', 'new-prompt > ')
1065    end)
1066    screen:expect([[
1067      new-prompt > ^            |
1068      {1:~                        }|*8
1069      {5:-- INSERT --}             |
1070    ]])
1071
1072    -- No leak if prompt_setprompt called for unloaded prompt buffer.
1073    local unloaded_buf = fn('bufadd', '')
1074    api.nvim_set_option_value('buftype', 'prompt', { buf = unloaded_buf })
1075    fn('prompt_setprompt', unloaded_buf, 'hello unloaded! > ')
1076    eq('hello unloaded! > ', fn('prompt_getprompt', unloaded_buf))
1077  end)
1078
1079  it('prompt_appendbuf with multi-element list and singleline prompt', function()
1080    command('new')
1081    local buf = api.nvim_get_current_buf()
1082    api.nvim_set_option_value('buftype', 'prompt', { buf = buf })
1083    fn('prompt_setprompt', buf, 'cmd: ')
1084
1085    -- Single element list: appends before prompt
1086    fn('prompt_appendbuf', buf, { 'line1' })
1087    eq({ 'line1', 'cmd: ' }, api.nvim_buf_get_lines(buf, 0, -1, false))
1088
1089    -- Multi-element list: first element appended, rest inserted as new lines before prompt
1090    fn('prompt_appendbuf', buf, { '-append', 'line2' })
1091    eq({ 'line1-append', 'line2', 'cmd: ' }, api.nvim_buf_get_lines(buf, 0, -1, false))
1092
1093    -- Multi-element list after multi-element list
1094    fn('prompt_appendbuf', buf, { '', 'line3', 'line4', 'line5', 'line6' })
1095    eq(
1096      { 'line1-append', 'line2', 'line3', 'line4', 'line5', 'line6', 'cmd: ' },
1097      api.nvim_buf_get_lines(buf, 0, -1, false)
1098    )
1099  end)
1100
1101  it('prompt_appendbuf with multi-element list and multiline prompt', function()
1102    command('new')
1103    local buf = api.nvim_get_current_buf()
1104    api.nvim_set_option_value('buftype', 'prompt', { buf = buf })
1105    fn('prompt_setprompt', buf, 'cmd: ')
1106    source('startinsert')
1107
1108    -- User types multiline input in the prompt
1109    feed('input1<s-cr>input2')
1110    eq({ 'cmd: input1', 'input2' }, api.nvim_buf_get_lines(buf, 0, -1, false))
1111
1112    -- Single element list: appends before prompt
1113    fn('prompt_appendbuf', buf, { 'line1' })
1114    eq({ 'line1', 'cmd: input1', 'input2' }, api.nvim_buf_get_lines(buf, 0, -1, false))
1115
1116    -- Multi-element list: first element appended, rest inserted as new lines before prompt
1117    fn('prompt_appendbuf', buf, { '-append', 'line2' })
1118    eq(
1119      { 'line1-append', 'line2', 'cmd: input1', 'input2' },
1120      api.nvim_buf_get_lines(buf, 0, -1, false)
1121    )
1122
1123    -- Multi-element list after multi-element list
1124    fn('prompt_appendbuf', buf, { '', 'line3', 'line4', 'line5', 'line6' })
1125    eq(
1126      { 'line1-append', 'line2', 'line3', 'line4', 'line5', 'line6', 'cmd: input1', 'input2' },
1127      api.nvim_buf_get_lines(buf, 0, -1, false)
1128    )
1129  end)
1130
1131  it("respects 'scrollback'", function()
1132    exec_lua(function()
1133      local buf = vim.api.nvim_create_buf(false, true)
1134      vim.api.nvim_set_option_value('buftype', 'prompt', { buf = buf })
1135      vim.api.nvim_set_option_value('scrollback', 3, { buf = buf })
1136      vim.fn.prompt_setcallback(buf, function(_)
1137        vim.fn.prompt_setprompt(buf, '> ')
1138      end)
1139      vim.fn.prompt_setprompt(buf, '> ')
1140      vim.api.nvim_set_current_buf(buf)
1141      vim.cmd('startinsert')
1142    end)
1143    local buf = fn('bufnr')
1144
1145    -- Submit 5 times, each creates a new prompt line as history
1146    feed('a<cr>')
1147    feed('b<cr>')
1148    feed('c<cr>')
1149    feed('d<cr>')
1150    feed('e<cr>')
1151
1152    -- Should only keep 3 lines above the prompt (scrollback limit = 3)
1153    local lines = api.nvim_buf_get_lines(buf, 0, -1, false)
1154    eq({ '> c', '> d', '> e', '> ' }, lines)
1155  end)
1156end)