robin/laufey

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

Justin M. Keyesfeat(exmode): "1q:", :exmode #41010fbcb7a056c

master
5.8 KiB141 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 clear = n.clear
7local command = n.command
8local eq = t.eq
9local feed = n.feed
10local fn = n.fn
11
12before_each(clear)
13
14-- Interactive Ex mode ([count]q:) is a keep-open |cmdwin| REPL. #40962
15describe('Ex mode', function()
16  it('executes commands against the caller window, auto-prints, records history', function()
17    local screen = Screen.new(60, 10)
18    command([[call setline(1, ['line1', 'line2', 'line3'])]])
19    feed('1q:')
20    screen:expect([[
21      line1                                                       |
22      {2:[No Name] [+]                                               }|
23      {1::}^                                                           |
24      {1:~                                                           }|*5
25      {3:[Ex mode]                                                   }|
26      {5:-- INSERT --}                                                |
27    ]])
28    t.matches('Entering Ex mode%.', n.exec_capture('messages'))
29    feed("put ='inserted'<CR>")
30    feed('visual<CR>')
31    eq('n', fn.mode(1))
32    eq({ 'line1', 'inserted', 'line2', 'line3' }, fn.getline(1, '$'))
33    eq('visual', fn.histget('cmd', -1))
34    eq("put ='inserted'", fn.histget('cmd', -2))
35    -- Ex mode can be re-entered (the "[Ex mode]" buffer name is not duplicated).
36    feed('1q:')
37    screen:expect({ any = vim.pesc('{3:[Ex mode]') })
38    feed('visual<CR>')
39    eq('n', fn.mode(1))
40    -- ":exmode" (but not ":ex", which is ":edit") also enters Ex mode.
41    eq('ex', fn.fullcommand('ex'))
42    eq('exmode', fn.fullcommand('exm'))
43    command('exmode')
44    screen:expect({ any = vim.pesc('{3:[Ex mode]') })
45    feed('visual<CR>')
46    eq('n', fn.mode(1))
47  end)
48
49  it('bare address and empty line move the cursor and auto-print', function()
50    local screen = Screen.new(60, 8)
51    command([[call setline(1, ['line1', 'line2'])]])
52    feed('1q:')
53    feed('1<CR>') -- Bare address: move and auto-print.
54    screen:expect({ any = vim.pesc('" line1') })
55    feed('<CR>') -- Empty line: advance and auto-print (POSIX ex "+").
56    screen:expect({ any = vim.pesc('" line2') })
57    feed('<CR>') -- At end-of-file.
58    screen:expect({ any = 'E501' })
59    feed('visual<CR>')
60    eq(2, fn.line('.'))
61  end)
62
63  it('explicit :print is not doubled by auto-print', function()
64    command([[call setline(1, ['line1', 'line2'])]])
65    feed('1q:')
66    feed('1,2print<CR>')
67    -- Transcript records each printed line once, not the cursor line again.
68    eq({ '1,2print', '" line1', '" line2', '' }, n.api.nvim_buf_get_lines(0, 0, -1, false))
69    feed('visual<CR>')
70  end)
71
72  it('substitute confirmation prompt', function()
73    local screen = Screen.new(60, 8)
74    command('set inccommand=')
75    command([[call setline(1, repeat(['foo foo'], 2))]])
76    feed('1q:')
77    feed('%s/foo/bar/gc<CR>')
78    screen:expect({ any = vim.pesc('replace with bar? (y)es/(n)o/(a)ll') })
79    feed('y') -- Replace the first match ...
80    feed('q') -- ... then quit at the second prompt: back to the REPL.
81    screen:expect({ any = vim.pesc('{5:-- INSERT --}') })
82    feed('visual<CR>')
83    eq({ 'bar foo', 'foo foo' }, fn.getline(1, '$'))
84  end)
85
86  it('collects multi-line commands until the block is complete', function()
87    local screen = Screen.new(60, 10)
88    feed('1q:')
89    feed('append<CR>')
90    -- Continuation lines should not have ":" prefix in 'statuscolumn'.
91    screen:expect([[
92                                                                  |
93      {2:[No Name]                                                   }|
94      {1::}append                                                     |
95      {1: }^                                                           |
96      {1:~                                                           }|*4
97      {3:[Ex mode]                                                   }|
98      {5:-- INSERT --}                                                |
99    ]])
100    feed('x1<CR>x2<CR>.<CR>')
101    -- Executing :append does not end the REPL's Insert mode (ex_append restores "State").
102    eq('i', fn.mode(1))
103    -- Ending :append with "." does not auto-print the last inserted line.
104    eq({ 'append', 'x1', 'x2', '.', '' }, n.api.nvim_buf_get_lines(0, 0, -1, false))
105    feed('let g:x = 0<CR>')
106    feed('if 1<CR>')
107    -- Continuation lines should not have ":" prefix in 'statuscolumn'.
108    screen:expect([[
109      x1                                                          |
110      {2:[No Name] [+]                                               }|
111      {1: }x1                                                         |
112      {1: }x2                                                         |
113      {1: }.                                                          |
114      {1::}let g:x = 0                                                |
115      {1::}if 1                                                       |
116      {1: }^                                                           |
117      {3:[Ex mode]                                                   }|
118      {5:-- INSERT --}                                                |
119    ]])
120    feed('let g:x = 1<CR>endif<CR>')
121    feed('function! F()<CR>return 42<CR>endfunction<CR>')
122    feed('let g:h =<lt><lt> trim EOS<CR>  hey<CR>EOS<CR>')
123    feed('visual<CR>')
124    eq({ 'x1', 'x2' }, fn.getline(1, '$'))
125    eq(1, n.eval('g:x'))
126    eq(42, n.eval('F()'))
127    eq({ 'hey' }, n.eval('g:h'))
128  end)
129
130  it('pressing Ctrl-C in :append inside a loop does not hang', function()
131    feed('1q:')
132    feed('for i in range(1)<CR>')
133    feed('append<CR>')
134    -- CTRL-C cancels the cmdwin (ends Ex mode): back to the ":" cmdline, like |q:|.
135    feed('<C-C>')
136    eq('', fn.getcmdwintype())
137    feed('<Esc>')
138    eq('n', fn.mode(1))
139    eq({ '' }, fn.getline(1, '$'))
140  end)
141end)