robin/laufey

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

Justin M. Keyesrefactor: 'previewpopup' #40945bf3d4210c3

master
23.3 KiB647 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3local Screen = require('test.functional.ui.screen')
4local os = require('os')
5
6local describe, it, before_each = t.describe, t.it, t.before_each
7local after_each, finally = t.after_each, t.finally
8local clear, command, api, fn = n.clear, n.command, n.api, n.fn
9local eq, pcall_err, write_file = t.eq, t.pcall_err, t.write_file
10local exec, feed = n.exec, n.feed
11
12--- Joins lines "1".."count", with `overrides` (1-based row → text) substituted.
13--- @param count integer
14--- @param overrides table<integer, string>
15local function numbered_lines(count, overrides)
16  local lines = {} --- @type string[]
17  for i = 1, count do
18    lines[i] = overrides[i] or tostring(i)
19  end
20  return table.concat(lines, '\n')
21end
22
23describe("'previewpopup'", function()
24  before_each(function()
25    clear()
26  end)
27
28  local function with_ext_multigrid(multigrid)
29    local screen ---@type test.functional.ui.screen
30    before_each(function()
31      screen = Screen.new(40, 7, { ext_multigrid = multigrid })
32      screen:add_extra_attr_ids({
33        [100] = {
34          foreground = Screen.colors.Magenta1,
35          background = Screen.colors.Plum1,
36          bold = true,
37        },
38      })
39    end)
40
41    it('validation', function()
42      local err = pcall_err(n.exec_capture, 'set previewpopup=height:yes')
43      eq(
44        "nvim_exec2(), line 1: Vim(set):E474: 'height' requires a number: previewpopup=height:yes",
45        err
46      )
47
48      err = pcall_err(n.exec_capture, 'set previewpopup=width:yes')
49      eq(
50        "nvim_exec2(), line 1: Vim(set):E474: 'width' requires a number: previewpopup=width:yes",
51        err
52      )
53
54      err = pcall_err(n.exec_capture, 'set previewpopup=width:20,height;10')
55      eq(
56        "nvim_exec2(), line 1: Vim(set):E474: Unknown item 'height;10': previewpopup=width:20,height;10",
57        err
58      )
59
60      err = pcall_err(n.exec_capture, 'set previewpopup=border:fancy')
61      eq(
62        'nvim_exec2(), line 1: Vim(set):E474: '
63          .. "'border' must be one of: double, single, shadow, rounded, solid, bold, none: "
64          .. 'previewpopup=border:fancy',
65        err
66      )
67
68      -- height/width must be >= 1 (semantic check, not the schema).
69      err = pcall_err(n.exec_capture, 'set previewpopup=height:0')
70      eq('nvim_exec2(), line 1: Vim(set):E474: Invalid argument: previewpopup=height:0', err)
71    end)
72
73    -- oldtest: Test_previewpopup
74    it('with tags and search', function()
75      finally(function()
76        os.remove('Xtags')
77        os.remove('Xtagfile')
78        os.remove('Xheader.h')
79      end)
80      screen:try_resize(40, 20)
81      write_file(
82        'Xtags',
83        t.dedent([[
84          !_TAG_FILE_ENCODING	utf-8	//
85          another	Xtagfile	/^this is another
86          theword	Xtagfile	/^theword
87        ]])
88      )
89      write_file(
90        'Xtagfile',
91        numbered_lines(40, { [21] = 'theword is here', [28] = 'this is another place' })
92      )
93      write_file('Xheader.h', numbered_lines(20, { [11] = 'searched word is here' }))
94      command('set tags=Xtags')
95      api.nvim_buf_set_lines(0, 0, -1, false, {
96        'one',
97        '#include "Xheader.h"',
98        'three',
99        'four',
100        'five',
101        'six',
102        'seven',
103        'find theword somewhere',
104        'nine',
105        'this is another word',
106        'very long line where the word is also another',
107      })
108      command('set previewpopup=height:4,width:40')
109      command('set winborder=single')
110      command('set path=.')
111
112      feed('/theword<CR><C-W>}')
113      if multigrid then
114        screen:expect({
115          grid = [[
116          ## grid 1
117            [2:----------------------------------------]|*19
118            [3:----------------------------------------]|
119          ## grid 2
120            one                                     |
121            #include "Xheader.h"                    |
122            three                                   |
123            four                                    |
124            five                                    |
125            six                                     |
126            seven                                   |
127            find {10:^theword} somewhere                  |
128            nine                                    |
129            this is another word                    |
130            very long line where the word is also an|
131            other                                   |
132            {1:~                                       }|*7
133          ## grid 3
134            /theword                                |
135          ## grid 4
136            {4:┌────────────}{100:Xtagfile}{4:────────────┐}|
137            {4:│20                              │}|
138            {4:│}{10:theword}{4: is here                 │}|
139            {4:│22                              │}|
140            {4:│23                              │}|
141            {4:└────────────────────────────────┘}|
142          ]],
143          win_pos = {
144            [2] = { height = 19, startcol = 0, startrow = 0, width = 40, win = 1000 },
145          },
146          float_pos = {
147            [4] = { 1001, 'NW', 1, 8, 6, true, 50, 1, 8, 6 },
148          },
149        })
150      else
151        screen:expect([[
152          one                                     |
153          #include "Xheader.h"                    |
154          three                                   |
155          four                                    |
156          five                                    |
157          six                                     |
158          seven                                   |
159          find {10:^theword} somewhere                  |
160          nine  {4:┌────────────}{100:Xtagfile}{4:────────────┐}|
161          this i{4:│20                              │}|
162          very l{4:│}{10:theword}{4: is here                 │}|
163          other {4:│22                              │}|
164          {1:~     }{4:│23                              │}|
165          {1:~     }{4:└────────────────────────────────┘}|
166          {1:~                                       }|*5
167          /theword                                |
168        ]])
169      end
170
171      command('pclose')
172      if multigrid then
173        screen:expect({
174          grid = [[
175          ## grid 1
176            [2:----------------------------------------]|*19
177            [3:----------------------------------------]|
178          ## grid 2
179            one                                     |
180            #include "Xheader.h"                    |
181            three                                   |
182            four                                    |
183            five                                    |
184            six                                     |
185            seven                                   |
186            find {10:^theword} somewhere                  |
187            nine                                    |
188            this is another word                    |
189            very long line where the word is also an|
190            other                                   |
191            {1:~                                       }|*7
192          ## grid 3
193            /theword                                |
194          ]],
195          win_pos = {
196            [2] = { height = 19, startcol = 0, startrow = 0, width = 40, win = 1000 },
197          },
198        })
199      else
200        screen:expect([[
201          one                                     |
202          #include "Xheader.h"                    |
203          three                                   |
204          four                                    |
205          five                                    |
206          six                                     |
207          seven                                   |
208          find {10:^theword} somewhere                  |
209          nine                                    |
210          this is another word                    |
211          very long line where the word is also an|
212          other                                   |
213          {1:~                                       }|*7
214          /theword                                |
215        ]])
216      end
217
218      command([[set include=^\s*#\s*include]])
219      command('pedit +/theword Xtagfile')
220      if multigrid then
221        screen:expect({
222          grid = [[
223          ## grid 1
224            [2:----------------------------------------]|*19
225            [3:----------------------------------------]|
226          ## grid 2
227            one                                     |
228            #include "Xheader.h"                    |
229            three                                   |
230            four                                    |
231            five                                    |
232            six                                     |
233            seven                                   |
234            find {10:^theword} somewhere                  |
235            nine                                    |
236            this is another word                    |
237            very long line where the word is also an|
238            other                                   |
239            {1:~                                       }|*7
240          ## grid 3
241            /theword                                |
242          ## grid 5
243            {4:┌────────────}{100:Xtagfile}{4:────────────┐}|
244            {4:│20                              │}|
245            {4:│}{10:theword}{4: is here                 │}|
246            {4:│22                              │}|
247            {4:│23                              │}|
248            {4:└────────────────────────────────┘}|
249          ]],
250          win_pos = {
251            [2] = { height = 19, startcol = 0, startrow = 0, width = 40, win = 1000 },
252          },
253          float_pos = {
254            [5] = { 1002, 'NW', 1, 8, 6, true, 50, 1, 8, 6 },
255          },
256        })
257      else
258        screen:expect([[
259          one                                     |
260          #include "Xheader.h"                    |
261          three                                   |
262          four                                    |
263          five                                    |
264          six                                     |
265          seven                                   |
266          find {10:^theword} somewhere                  |
267          nine  {4:┌────────────}{100:Xtagfile}{4:────────────┐}|
268          this i{4:│20                              │}|
269          very l{4:│}{10:theword}{4: is here                 │}|
270          other {4:│22                              │}|
271          {1:~     }{4:│23                              │}|
272          {1:~     }{4:└────────────────────────────────┘}|
273          {1:~                                       }|*5
274          /theword                                |
275        ]])
276      end
277
278      command('pclose | psearch searched')
279      if multigrid then
280        screen:expect({
281          grid = [[
282          ## grid 1
283            [2:----------------------------------------]|*19
284            [3:----------------------------------------]|
285          ## grid 2
286            one                                     |
287            #include "Xheader.h"                    |
288            three                                   |
289            four                                    |
290            five                                    |
291            six                                     |
292            seven                                   |
293            find {10:^theword} somewhere                  |
294            nine                                    |
295            this is another word                    |
296            very long line where the word is also an|
297            other                                   |
298            {1:~                                       }|*7
299          ## grid 3
300            /theword                                |
301          ## grid 6
302            {4:┌───────────}{100:Xheader.h}{4:────────────┐}|
303            {4:│10                              │}|
304            {4:│searched word is here           │}|
305            {4:│12                              │}|
306            {4:│13                              │}|
307            {4:└────────────────────────────────┘}|
308          ]],
309          win_pos = {
310            [2] = { height = 19, startcol = 0, startrow = 0, width = 40, win = 1000 },
311          },
312          float_pos = {
313            [6] = { 1003, 'NW', 1, 8, 6, true, 50, 1, 8, 6 },
314          },
315        })
316      else
317        screen:expect([[
318          one                                     |
319          #include "Xheader.h"                    |
320          three                                   |
321          four                                    |
322          five                                    |
323          six                                     |
324          seven                                   |
325          find {10:^theword} somewhere                  |
326          nine  {4:┌───────────}{100:Xheader.h}{4:────────────┐}|
327          this i{4:│10                              │}|
328          very l{4:│searched word is here           │}|
329          other {4:│12                              │}|
330          {1:~     }{4:│13                              │}|
331          {1:~     }{4:└────────────────────────────────┘}|
332          {1:~                                       }|*5
333          /theword                                |
334        ]])
335      end
336    end)
337
338    describe('with pum', function()
339      before_each(function()
340        write_file(
341          'XpreviewText.vim',
342          t.dedent([[
343            let a = 3
344            let b = 1
345            echo a
346            echo b
347            call system('echo hello')
348            " the end
349          ]])
350        )
351      end)
352      after_each(function()
353        os.remove('XpreviewText.vim')
354      end)
355      local expect_screen = [[
356        one other^                     |
357        t{4:le}{12: other          }{4:           }|
358        t{4:le once                      }|
359        o{4:ec only                      }|
360        o{4:ec off                       }|
361        o{4:ca one            hello')    }|
362        o{4:" the end                    }|
363        {1:~                             }|*2
364        {5:-- }{6:match 1 of 5}               |
365      ]]
366
367      -- oldtest: Test_previewpopup_pum_pedit
368      it('pum pedit', function()
369        screen:try_resize(30, 10)
370        exec([[
371          call setline(1, ['one', 'two', 'three', 'other', 'once', 'only', 'off'])
372          set previewpopup=height:6,width:40
373        ]])
374        command('pedit XpreviewText.vim')
375        feed('A o<C-N>')
376        if not multigrid then
377          screen:expect(expect_screen)
378        end
379      end)
380
381      -- oldtest: Test_previewpopup_pum_pbuffer
382      it('pum pbuffer', function()
383        screen:try_resize(30, 10)
384        exec([[
385          call setline(1, ['one', 'two', 'three', 'other', 'once', 'only', 'off'])
386          set previewpopup=height:6,width:40
387          badd XpreviewText.vim
388        ]])
389        command(fn.bufnr('$') .. 'pbuffer')
390        feed('A o<C-N>')
391        if not multigrid then
392          screen:expect(expect_screen)
393        end
394      end)
395    end)
396
397    it(':pedit and border overrides', function()
398      command('call writefile(["bar"], "foo", "a")')
399      finally(function()
400        os.remove('foo')
401      end)
402      command('set winborder=single | set previewpopup=height:2,width:5 | pedit foo')
403      if multigrid then
404        screen:expect({
405          grid = [[
406          ## grid 1
407            [2:----------------------------------------]|*6
408            [3:----------------------------------------]|
409          ## grid 2
410            ^                                        |
411            {1:~                                       }|*5
412          ## grid 3
413                                                    |
414          ## grid 4
415            {4:┌─}{100:foo}{4:─┐}|
416            {4:│bar  │}|
417            {4:│     │}|
418            {4:└─────┘}|
419          ]],
420          win_pos = {
421            [2] = {
422              height = 6,
423              startcol = 0,
424              startrow = 0,
425              width = 40,
426              win = 1000,
427            },
428          },
429          float_pos = {
430            [4] = { 1001, 'NW', 1, 1, 1, true, 50, 1, 1, 1 },
431          },
432          win_viewport = {
433            [2] = {
434              win = 1000,
435              topline = 0,
436              botline = 2,
437              curline = 0,
438              curcol = 0,
439              linecount = 1,
440              sum_scroll_delta = 0,
441            },
442            [4] = {
443              win = 1001,
444              topline = 0,
445              botline = 2,
446              curline = 0,
447              curcol = 0,
448              linecount = 1,
449              sum_scroll_delta = 0,
450            },
451          },
452        })
453      else
454        screen:expect([[
455          ^                                        |
456          {1:~}{4:┌─}{100:foo}{4:─┐}{1:                                }|
457          {1:~}{4:│bar  │}{1:                                }|
458          {1:~}{4:│     │}{1:                                }|
459          {1:~}{4:└─────┘}{1:                                }|
460          {1:~                                       }|
461                                                  |
462        ]])
463      end
464
465      -- move floating preview window to current split window
466      command('vs | wincmd p | pedit none_exist')
467      if multigrid then
468        screen:expect({
469          grid = [[
470          ## grid 1
471            [5:-------------------]│[2:--------------------]|*5
472            {2:[No Name]           }{3:[No Name]           }|
473            [3:----------------------------------------]|
474          ## grid 2
475            ^                    |
476            {1:~                   }|*4
477          ## grid 3
478                                                    |
479          ## grid 4
480            {4:┌}{100:<xist}{4:┐}|
481            {4:│     │}|*2
482            {4:└─────┘}|
483          ## grid 5
484                               |
485            {1:~                  }|*4
486          ]],
487          win_pos = {
488            [2] = {
489              height = 5,
490              startcol = 20,
491              startrow = 0,
492              width = 20,
493              win = 1000,
494            },
495            [5] = {
496              height = 5,
497              startcol = 0,
498              startrow = 0,
499              width = 19,
500              win = 1002,
501            },
502          },
503          float_pos = {
504            [4] = { 1001, 'NW', 1, 1, 21, true, 50, 1, 1, 21 },
505          },
506          win_viewport = {
507            [2] = {
508              win = 1000,
509              topline = 0,
510              botline = 2,
511              curline = 0,
512              curcol = 0,
513              linecount = 1,
514              sum_scroll_delta = 0,
515            },
516            [4] = {
517              win = 1001,
518              topline = 0,
519              botline = 2,
520              curline = 0,
521              curcol = 0,
522              linecount = 1,
523              sum_scroll_delta = 0,
524            },
525            [5] = {
526              win = 1002,
527              topline = 0,
528              botline = 2,
529              curline = 0,
530              curcol = 0,
531              linecount = 1,
532              sum_scroll_delta = 0,
533            },
534          },
535          win_viewport_margins = {
536            [2] = {
537              bottom = 0,
538              left = 0,
539              right = 0,
540              top = 0,
541              win = 1000,
542            },
543            [4] = {
544              bottom = 1,
545              left = 1,
546              right = 1,
547              top = 1,
548              win = 1001,
549            },
550            [5] = {
551              bottom = 0,
552              left = 0,
553              right = 0,
554              top = 0,
555              win = 1002,
556            },
557          },
558        })
559      else
560        screen:expect([[
561                             │^                    |
562          {1:~                  }│{1:~}{4:┌}{100:<xist}{4:┐}{1:            }|
563          {1:~                  }│{1:~}{4:│     │}{1:            }|*2
564          {1:~                  }│{1:~}{4:└─────┘}{1:            }|
565          {2:[No Name]           }{3:[No Name]           }|
566                                                  |
567        ]])
568      end
569      command('pclose') -- can close by pclose command
570      eq(2, #api.nvim_list_wins())
571
572      -- border overrides and falls back to 'winborder'
573      command('only | set previewpopup=height:2,width:5,border:rounded | pedit foo')
574      if not multigrid then
575        screen:expect([[
576          ^                                        |
577          {1:~}{4:╭─}{100:foo}{4:─╮}{1:                                }|
578          {1:~}{4:│bar  │}{1:                                }|
579          {1:~}{4:│     │}{1:                                }|
580          {1:~}{4:╰─────╯}{1:                                }|
581          {1:~                                       }|
582                                                  |
583        ]])
584      end
585      command('pclose | set previewpopup+=border:none | pedit foo')
586      if multigrid then
587        screen:expect({
588          grid = [[
589          ## grid 1
590            [2:----------------------------------------]|*6
591            [3:----------------------------------------]|
592          ## grid 2
593            ^                                        |
594            {1:~                                       }|*5
595          ## grid 3
596                                                    |
597          ## grid 7
598            {4:bar  }|
599            {4:     }|
600          ]],
601          win_pos = {
602            [2] = { height = 6, startcol = 0, startrow = 0, width = 40, win = 1000 },
603          },
604          float_pos = {
605            [7] = { 1004, 'NW', 1, 1, 1, true, 50, 1, 1, 1 },
606          },
607        })
608      else
609        screen:expect([[
610          ^                                        |
611          {1:~}{4:bar  }{1:                                  }|
612          {1:~}{4:     }{1:                                  }|
613          {1:~                                       }|*3
614                                                  |
615        ]])
616      end
617    end)
618
619    it('autosizes to content when height/width are omitted', function()
620      screen:try_resize(40, 20)
621      command('call writefile(["short", "a much longer line here"], "foo")')
622      finally(function()
623        os.remove('foo')
624      end)
625      command('set previewpopup=border:none | pedit foo')
626
627      local pwin ---@type integer
628      for _, w in ipairs(api.nvim_list_wins()) do
629        if api.nvim_win_get_config(w).relative ~= '' then
630          pwin = w
631          break
632        end
633      end
634      local conf = api.nvim_win_get_config(pwin)
635      eq(23, conf.width) -- longest line
636      eq(2, conf.height) -- line count
637    end)
638  end
639
640  describe('with ext_multigrid', function()
641    with_ext_multigrid(true)
642  end)
643
644  describe('without ext_multigrid', function()
645    with_ext_multigrid(false)
646  end)
647end)