robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
22.8 KiB718 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 api = n.api
7local clear = n.clear
8local eq = t.eq
9local exec_lua = n.exec_lua
10local insert = n.insert
11local feed = n.feed
12local command = n.command
13local assert_alive = n.assert_alive
14
15-- Implements a :Replace command that works like :substitute and has multibuffer support.
16local setup_replace_cmd = [[
17  local function show_replace_preview(use_preview_win, preview_ns, preview_buf, matches)
18    -- Find the width taken by the largest line number, used for padding the line numbers
19    local highest_lnum = math.max(matches[#matches][1], 1)
20    local highest_lnum_width = math.floor(math.log10(highest_lnum))
21    local preview_buf_line = 0
22    local multibuffer = #matches > 1
23
24    for _, match in ipairs(matches) do
25      local buf = match[1]
26      local buf_matches = match[2]
27
28      if multibuffer and #buf_matches > 0 and use_preview_win then
29        local bufname = vim.api.nvim_buf_get_name(buf)
30
31        if bufname == "" then
32          bufname = string.format("Buffer #%d", buf)
33        end
34
35        vim.api.nvim_buf_set_lines(
36          preview_buf,
37          preview_buf_line,
38          preview_buf_line,
39          0,
40          { bufname .. ':' }
41        )
42
43        preview_buf_line = preview_buf_line + 1
44      end
45
46      for _, buf_match in ipairs(buf_matches) do
47        local lnum = buf_match[1]
48        local line_matches = buf_match[2]
49        local prefix
50
51        if use_preview_win then
52          prefix = string.format(
53            '|%s%d| ',
54            string.rep(' ', highest_lnum_width - math.floor(math.log10(lnum))),
55            lnum
56          )
57
58          vim.api.nvim_buf_set_lines(
59            preview_buf,
60            preview_buf_line,
61            preview_buf_line,
62            0,
63            { prefix .. vim.api.nvim_buf_get_lines(buf, lnum - 1, lnum, false)[1] }
64          )
65        end
66
67        for _, line_match in ipairs(line_matches) do
68          vim.api.nvim_buf_add_highlight(
69            buf,
70            preview_ns,
71            'Substitute',
72            lnum - 1,
73            line_match[1],
74            line_match[2]
75          )
76
77          if use_preview_win then
78            vim.api.nvim_buf_add_highlight(
79              preview_buf,
80              preview_ns,
81              'Substitute',
82              preview_buf_line,
83              #prefix + line_match[1],
84              #prefix + line_match[2]
85            )
86          end
87        end
88
89        preview_buf_line = preview_buf_line + 1
90      end
91    end
92
93    if use_preview_win then
94      return 2
95    else
96      return 1
97    end
98  end
99
100  local function do_replace(opts, preview, preview_ns, preview_buf)
101    local pat1 = opts.fargs[1]
102
103    if not pat1 then return end
104
105    local pat2 = opts.fargs[2] or ''
106    local line1 = opts.line1
107    local line2 = opts.line2
108    local matches = {}
109
110    -- Get list of valid and listed buffers
111    local buffers = vim.tbl_filter(
112        function(buf)
113          if not (vim.api.nvim_buf_is_valid(buf) and vim.bo[buf].buflisted and buf ~= preview_buf)
114          then
115            return false
116          end
117
118          -- Check if there's at least one window using the buffer
119          for _, win in ipairs(vim.api.nvim_tabpage_list_wins(0)) do
120            if vim.api.nvim_win_get_buf(win) == buf then
121              return true
122            end
123          end
124
125          return false
126        end,
127        vim.api.nvim_list_bufs()
128    )
129
130    for _, buf in ipairs(buffers) do
131      local lines = vim.api.nvim_buf_get_lines(buf, line1 - 1, line2, false)
132      local buf_matches = {}
133
134      for i, line in ipairs(lines) do
135        local startidx, endidx = 0, 0
136        local line_matches = {}
137        local num = 1
138
139        while startidx ~= -1 do
140          local match = vim.fn.matchstrpos(line, pat1, 0, num)
141          startidx, endidx = match[2], match[3]
142
143          if startidx ~= -1 then
144            line_matches[#line_matches+1] = { startidx, endidx }
145          end
146
147          num = num + 1
148        end
149
150        if #line_matches > 0 then
151          buf_matches[#buf_matches+1] = { line1 + i - 1, line_matches }
152        end
153      end
154
155      local new_lines = {}
156
157      for _, buf_match in ipairs(buf_matches) do
158        local lnum = buf_match[1]
159        local line_matches = buf_match[2]
160        local line = lines[lnum - line1 + 1]
161        local pat_width_differences = {}
162
163        -- If previewing, only replace the text in current buffer if pat2 isn't empty
164        -- Otherwise, always replace the text
165        if pat2 ~= '' or not preview then
166          if preview then
167            for _, line_match in ipairs(line_matches) do
168              local startidx, endidx = unpack(line_match)
169              local pat_match = line:sub(startidx + 1, endidx)
170
171              pat_width_differences[#pat_width_differences+1] =
172                #vim.fn.substitute(pat_match, pat1, pat2, 'g') - #pat_match
173            end
174          end
175
176          new_lines[lnum] = vim.fn.substitute(line, pat1, pat2, 'g')
177        end
178
179        -- Highlight the matches if previewing
180        if preview then
181          local idx_offset = 0
182          for i, line_match in ipairs(line_matches) do
183            local startidx, endidx = unpack(line_match)
184            -- Starting index of replacement text
185            local repl_startidx = startidx + idx_offset
186            -- Ending index of the replacement text (if pat2 isn't empty)
187            local repl_endidx
188
189            if pat2 ~= '' then
190              repl_endidx = endidx + idx_offset + pat_width_differences[i]
191            else
192              repl_endidx = endidx + idx_offset
193            end
194
195            if pat2 ~= '' then
196              idx_offset = idx_offset + pat_width_differences[i]
197            end
198
199            line_matches[i] = { repl_startidx, repl_endidx }
200          end
201        end
202      end
203
204      for lnum, line in pairs(new_lines) do
205        vim.api.nvim_buf_set_lines(buf, lnum - 1, lnum, false, { line })
206      end
207
208      matches[#matches+1] = { buf, buf_matches }
209    end
210
211    if preview then
212      local lnum = vim.api.nvim_win_get_cursor(0)[1]
213      -- Use preview window only if preview buffer is provided and range isn't just the current line
214      local use_preview_win = (preview_buf ~= nil) and (line1 ~= lnum or line2 ~= lnum)
215      return show_replace_preview(use_preview_win, preview_ns, preview_buf, matches)
216    end
217  end
218
219  local function replace(opts)
220    do_replace(opts, false)
221  end
222
223  local function replace_preview(opts, preview_ns, preview_buf)
224    return do_replace(opts, true, preview_ns, preview_buf)
225  end
226
227  -- ":<range>Replace <pat1> <pat2>"
228  -- Replaces all occurrences of <pat1> in <range> with <pat2>
229  vim.api.nvim_create_user_command(
230    'Replace',
231    replace,
232    { nargs = '*', range = '%', addr = 'lines',
233      preview = replace_preview }
234  )
235]]
236
237describe("'inccommand' for user commands", function()
238  local screen
239
240  before_each(function()
241    clear()
242    screen = Screen.new(40, 17)
243    exec_lua(setup_replace_cmd)
244    command('set cmdwinheight=5')
245    insert [[
246      text on line 1
247      more text on line 2
248      oh no, even more text
249      will the text ever stop
250      oh well
251      did the text stop
252      why won't it stop
253      make the text stop
254    ]]
255  end)
256
257  it("can preview 'nomodifiable' buffer", function()
258    exec_lua([[
259      vim.api.nvim_create_user_command("PreviewTest", function() end, {
260        preview = function(ev)
261          vim.bo.modifiable = true
262          vim.api.nvim_buf_set_lines(0, 0, -1, false, {"cats"})
263          return 2
264        end,
265      })
266    ]])
267    command('set inccommand=split')
268
269    command('set nomodifiable')
270    eq(false, api.nvim_get_option_value('modifiable', { buf = 0 }))
271
272    feed(':PreviewTest')
273
274    screen:expect([[
275      cats                                    |
276      {1:~                                       }|*8
277      {3:[No Name] [+]                           }|
278                                              |
279      {1:~                                       }|*4
280      {2:[Preview]                               }|
281      :PreviewTest^                            |
282    ]])
283    feed('<Esc>')
284    screen:expect([[
285      text on line 1                          |
286      more text on line 2                     |
287      oh no, even more text                   |
288      will the text ever stop                 |
289      oh well                                 |
290      did the text stop                       |
291      why won't it stop                       |
292      make the text stop                      |
293      ^                                        |
294      {1:~                                       }|*7
295                                              |
296    ]])
297
298    eq(false, api.nvim_get_option_value('modifiable', { buf = 0 }))
299  end)
300
301  it('works with inccommand=nosplit', function()
302    command('set inccommand=nosplit')
303    feed(':Replace text cats')
304    screen:expect([[
305      {10:cats} on line 1                          |
306      more {10:cats} on line 2                     |
307      oh no, even more {10:cats}                   |
308      will the {10:cats} ever stop                 |
309      oh well                                 |
310      did the {10:cats} stop                       |
311      why won't it stop                       |
312      make the {10:cats} stop                      |
313                                              |
314      {1:~                                       }|*7
315      :Replace text cats^                      |
316    ]])
317  end)
318
319  it('works with inccommand=split', function()
320    command('set inccommand=split')
321    feed(':Replace text cats')
322    screen:expect([[
323      {10:cats} on line 1                          |
324      more {10:cats} on line 2                     |
325      oh no, even more {10:cats}                   |
326      will the {10:cats} ever stop                 |
327      oh well                                 |
328      did the {10:cats} stop                       |
329      why won't it stop                       |
330      make the {10:cats} stop                      |
331                                              |
332      {3:[No Name] [+]                           }|
333      |1| {10:cats} on line 1                      |
334      |2| more {10:cats} on line 2                 |
335      |3| oh no, even more {10:cats}               |
336      |4| will the {10:cats} ever stop             |
337      |6| did the {10:cats} stop                   |
338      {2:[Preview]                               }|
339      :Replace text cats^                      |
340    ]])
341  end)
342
343  it('properly closes preview when inccommand=split', function()
344    command('set inccommand=split')
345    feed(':Replace text cats<Esc>')
346    screen:expect([[
347      text on line 1                          |
348      more text on line 2                     |
349      oh no, even more text                   |
350      will the text ever stop                 |
351      oh well                                 |
352      did the text stop                       |
353      why won't it stop                       |
354      make the text stop                      |
355      ^                                        |
356      {1:~                                       }|*7
357                                              |
358    ]])
359  end)
360
361  it('properly executes command when inccommand=split', function()
362    command('set inccommand=split')
363    feed(':Replace text cats<CR>')
364    screen:expect([[
365      cats on line 1                          |
366      more cats on line 2                     |
367      oh no, even more cats                   |
368      will the cats ever stop                 |
369      oh well                                 |
370      did the cats stop                       |
371      why won't it stop                       |
372      make the cats stop                      |
373      ^                                        |
374      {1:~                                       }|*7
375      :Replace text cats                      |
376    ]])
377  end)
378
379  it('shows preview window only when range is not current line', function()
380    command('set inccommand=split')
381    feed('gg:.Replace text cats')
382    screen:expect([[
383      {10:cats} on line 1                          |
384      more text on line 2                     |
385      oh no, even more text                   |
386      will the text ever stop                 |
387      oh well                                 |
388      did the text stop                       |
389      why won't it stop                       |
390      make the text stop                      |
391                                              |
392      {1:~                                       }|*7
393      :.Replace text cats^                     |
394    ]])
395  end)
396
397  it('no crash on ambiguous command #18825', function()
398    command('set inccommand=split')
399    command('command Reply echo 1')
400    feed(':R')
401    assert_alive()
402    feed('e')
403    assert_alive()
404  end)
405
406  it('no crash if preview callback changes inccommand option', function()
407    command('set inccommand=nosplit')
408    exec_lua([[
409      vim.api.nvim_create_user_command('Replace', function() end, {
410        nargs = '*',
411        preview = function()
412          vim.api.nvim_set_option_value('inccommand', 'split', {})
413          return 2
414        end,
415      })
416    ]])
417    feed(':R')
418    assert_alive()
419    feed('e')
420    assert_alive()
421  end)
422
423  it('no crash when adding highlight after :substitute #21495', function()
424    command('set inccommand=nosplit')
425    exec_lua([[
426      vim.api.nvim_create_user_command("Crash", function() end, {
427        preview = function(_, preview_ns, _)
428          vim.cmd("%s/text/cats/g")
429          vim.api.nvim_buf_add_highlight(0, preview_ns, "Search", 0, 0, -1)
430          return 1
431        end,
432      })
433    ]])
434    feed(':C')
435    screen:expect([[
436      {10:cats on line 1}                          |
437      more cats on line 2                     |
438      oh no, even more cats                   |
439      will the cats ever stop                 |
440      oh well                                 |
441      did the cats stop                       |
442      why won't it stop                       |
443      make the cats stop                      |
444                                              |
445      {1:~                                       }|*7
446      :C^                                      |
447    ]])
448    assert_alive()
449  end)
450
451  it('no crash if preview callback executes undo #20036', function()
452    command('set inccommand=nosplit')
453    exec_lua([[
454      vim.api.nvim_create_user_command('Foo', function() end, {
455        nargs = '?',
456        preview = function(_, _, _)
457          vim.cmd.undo()
458        end,
459      })
460    ]])
461
462    -- Clear undo history
463    command('set undolevels=-1')
464    feed('ggyyp')
465    command('set undolevels=1000')
466
467    feed('yypp:Fo')
468    assert_alive()
469    feed('<Esc>:Fo')
470    assert_alive()
471  end)
472
473  local function test_preview_break_undo()
474    command('set inccommand=nosplit')
475    exec_lua([[
476      vim.api.nvim_create_user_command('Test', function() end, {
477        nargs = 1,
478        preview = function(opts, _, _)
479          vim.cmd('norm i' .. opts.args)
480          return 1
481        end
482      })
483    ]])
484    feed(':Test a.a.a.a.')
485    screen:expect([[
486      text on line 1                          |
487      more text on line 2                     |
488      oh no, even more text                   |
489      will the text ever stop                 |
490      oh well                                 |
491      did the text stop                       |
492      why won't it stop                       |
493      make the text stop                      |
494      a.a.a.a.                                |
495      {1:~                                       }|*7
496      :Test a.a.a.a.^                          |
497    ]])
498    feed('<C-V><Esc>u')
499    screen:expect([[
500      text on line 1                          |
501      more text on line 2                     |
502      oh no, even more text                   |
503      will the text ever stop                 |
504      oh well                                 |
505      did the text stop                       |
506      why won't it stop                       |
507      make the text stop                      |
508      a.a.a.                                  |
509      {1:~                                       }|*7
510      :Test a.a.a.a.{18:^[}u^                       |
511    ]])
512    feed('<Esc>')
513    screen:expect([[
514      text on line 1                          |
515      more text on line 2                     |
516      oh no, even more text                   |
517      will the text ever stop                 |
518      oh well                                 |
519      did the text stop                       |
520      why won't it stop                       |
521      make the text stop                      |
522      ^                                        |
523      {1:~                                       }|*7
524                                              |
525    ]])
526  end
527
528  describe('breaking undo chain in Insert mode works properly', function()
529    it('when using i_CTRL-G_u #20248', function()
530      command('inoremap . .<C-G>u')
531      test_preview_break_undo()
532    end)
533
534    it('when setting &l:undolevels to itself #24575', function()
535      command('inoremap . .<Cmd>let &l:undolevels = &l:undolevels<CR>')
536      test_preview_break_undo()
537    end)
538  end)
539
540  it('disables preview if preview buffer cannot be created #27086', function()
541    command('set inccommand=split')
542    api.nvim_buf_set_name(0, '[Preview]')
543    exec_lua([[
544      vim.api.nvim_create_user_command('Test', function() end, {
545        nargs = '*',
546        preview = function(_, _, _)
547          return 2
548        end
549      })
550    ]])
551    eq('split', api.nvim_get_option_value('inccommand', {}))
552    feed(':Test')
553    eq('nosplit', api.nvim_get_option_value('inccommand', {}))
554  end)
555
556  it('does not flush intermediate cursor position at end of message grid', function()
557    exec_lua([[
558      vim.api.nvim_create_user_command('Test', function() end, {
559        nargs = '*',
560        preview = function(_, _, _)
561          vim.api.nvim_buf_set_text(0, 0, 0, 1, -1, { "Preview" })
562          vim.cmd.sleep("1m")
563          return 1
564        end
565      })
566    ]])
567    local cursor_goto = screen._handle_grid_cursor_goto
568    screen._handle_grid_cursor_goto = function(...)
569      cursor_goto(...)
570      assert(screen._cursor.col < 12)
571    end
572    feed(':Test baz<Left><Left>arb')
573    screen:expect([[
574      Preview                                 |
575      oh no, even more text                   |
576      will the text ever stop                 |
577      oh well                                 |
578      did the text stop                       |
579      why won't it stop                       |
580      make the text stop                      |
581                                              |
582      {1:~                                       }|*8
583      :Test barb^az                            |
584    ]])
585  end)
586
587  it('works when CmdlineChanged calls wildtrigger() #35246', function()
588    api.nvim_buf_set_text(0, 0, 0, 1, -1, { '' })
589    exec_lua([[
590      vim.api.nvim_create_user_command("Repro", function() end, {
591        nargs = '+',
592        preview = function(opts, ns, buf)
593          vim.api.nvim_buf_set_lines(0, 0, -1, true, { opts.args })
594          return 2
595        end
596      })
597    ]])
598    command([[autocmd CmdlineChanged [:/\?] call wildtrigger()]])
599    command('set wildmode=noselect:lastused,full wildoptions=pum')
600    feed(':Repro ')
601    screen:expect([[
602                                              |
603      {1:~                                       }|*15
604      :Repro ^                                 |
605    ]])
606    feed('a')
607    screen:expect([[
608      a                                       |
609      {1:~                                       }|*15
610      :Repro a^                                |
611    ]])
612    feed('bc')
613    screen:expect([[
614      abc                                     |
615      {1:~                                       }|*15
616      :Repro abc^                              |
617    ]])
618  end)
619
620  it('no crash with % + preview + file completion #28851', function()
621    exec_lua([[
622      local function callback() end
623      local function preview()
624        return 0
625      end
626
627      vim.api.nvim_create_user_command('TestCommand', callback, {
628        nargs = '?',
629        complete = 'file',
630        preview = preview,
631      })
632
633      vim.cmd.edit('Xtestscript')
634    ]])
635    feed(':TestCommand %')
636    assert_alive()
637  end)
638end)
639
640describe("'inccommand' with multiple buffers", function()
641  local screen
642
643  before_each(function()
644    clear()
645    screen = Screen.new(40, 17)
646    exec_lua(setup_replace_cmd)
647    command('set cmdwinheight=10')
648    insert [[
649      foo bar baz
650      bar baz foo
651      baz foo bar
652    ]]
653    command('vsplit | enew')
654    insert [[
655      bar baz foo
656      baz foo bar
657      foo bar baz
658    ]]
659  end)
660
661  it('works', function()
662    command('set inccommand=nosplit')
663    feed(':Replace foo bar')
664    screen:expect([[
665      bar baz {10:bar}         │{10:bar} bar baz        |
666      baz {10:bar} bar         │bar baz {10:bar}        |
667      {10:bar} bar baz         │baz {10:bar} bar        |
668                          │                   |
669      {1:~                   }│{1:~                  }|*11
670      {3:[No Name] [+]        }{2:[No Name] [+]      }|
671      :Replace foo bar^                        |
672    ]])
673    feed('<CR>')
674    screen:expect([[
675      bar baz bar         │bar bar baz        |
676      baz bar bar         │bar baz bar        |
677      bar bar baz         │baz bar bar        |
678      ^                    │                   |
679      {1:~                   }│{1:~                  }|*11
680      {3:[No Name] [+]        }{2:[No Name] [+]      }|
681      :Replace foo bar                        |
682    ]])
683  end)
684
685  it('works with inccommand=split', function()
686    command('set inccommand=split')
687    feed(':Replace foo bar')
688    screen:expect([[
689      bar baz {10:bar}         │{10:bar} bar baz        |
690      baz {10:bar} bar         │bar baz {10:bar}        |
691      {10:bar} bar baz         │baz {10:bar} bar        |
692                          │                   |
693      {3:[No Name] [+]        }{2:[No Name] [+]      }|
694      Buffer #1:                              |
695      |1| {10:bar} bar baz                         |
696      |2| bar baz {10:bar}                         |
697      |3| baz {10:bar} bar                         |
698      Buffer #2:                              |
699      |1| bar baz {10:bar}                         |
700      |2| baz {10:bar} bar                         |
701      |3| {10:bar} bar baz                         |
702                                              |
703      {1:~                                       }|
704      {2:[Preview]                               }|
705      :Replace foo bar^                        |
706    ]])
707    feed('<CR>')
708    screen:expect([[
709      bar baz bar         │bar bar baz        |
710      baz bar bar         │bar baz bar        |
711      bar bar baz         │baz bar bar        |
712      ^                    │                   |
713      {1:~                   }│{1:~                  }|*11
714      {3:[No Name] [+]        }{2:[No Name] [+]      }|
715      :Replace foo bar                        |
716    ]])
717  end)
718end)