robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
29.7 KiB650 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local tt = require('test.functional.testterm')
5local describe, it, before_each = t.describe, t.it, t.before_each
6local clear, eq, eval = n.clear, t.eq, n.eval
7local feed, api, command = n.feed, n.api, n.command
8local feed_data = tt.feed_data
9local is_os = t.is_os
10local skip = t.skip
11
12describe(':terminal mouse', function()
13  local screen
14
15  before_each(function()
16    clear()
17    api.nvim_set_option_value('statusline', '==========', {})
18    screen = tt.setup_screen()
19    command('highlight StatusLine NONE')
20    command('highlight StatusLineNC NONE')
21    command('highlight StatusLineTerm NONE')
22    command('highlight StatusLineTermNC NONE')
23    command('highlight VertSplit NONE')
24    local lines = {}
25    for i = 1, 30 do
26      table.insert(lines, 'line' .. tostring(i))
27    end
28    table.insert(lines, '')
29    feed_data(lines)
30    screen:expect([[
31      line26                                            |
32      line27                                            |
33      line28                                            |
34      line29                                            |
35      line30                                            |
36      ^                                                  |
37      {5:-- TERMINAL --}                                    |
38    ]])
39  end)
40
41  describe('when the terminal has focus', function()
42    it('will exit focus on mouse-scroll', function()
43      eq('t', eval('mode(1)'))
44      feed('<ScrollWheelUp><0,0>')
45      eq('nt', eval('mode(1)'))
46    end)
47
48    it('will exit focus and trigger Normal mode mapping on mouse click', function()
49      feed([[<C-\><C-N>qri]])
50      command('let g:got_leftmouse = 0')
51      command('nnoremap <LeftMouse> <Cmd>let g:got_leftmouse = 1<CR>')
52      eq('t', eval('mode(1)'))
53      eq(0, eval('g:got_leftmouse'))
54      feed('<LeftMouse>')
55      eq('nt', eval('mode(1)'))
56      eq(1, eval('g:got_leftmouse'))
57      feed('q')
58      eq('i<LeftMouse>', eval('keytrans(@r)'))
59    end)
60
61    it('will exit focus and trigger Normal mode mapping on mouse click with modifier', function()
62      feed([[<C-\><C-N>qri]])
63      command('let g:got_ctrl_leftmouse = 0')
64      command('nnoremap <C-LeftMouse> <Cmd>let g:got_ctrl_leftmouse = 1<CR>')
65      eq('t', eval('mode(1)'))
66      eq(0, eval('g:got_ctrl_leftmouse'))
67      feed('<C-LeftMouse>')
68      eq('nt', eval('mode(1)'))
69      eq(1, eval('g:got_ctrl_leftmouse'))
70      feed('q')
71      eq('i<C-LeftMouse>', eval('keytrans(@r)'))
72    end)
73
74    it('will exit focus on <C-\\> + mouse-scroll', function()
75      eq('t', eval('mode(1)'))
76      feed('<C-\\>')
77      feed('<ScrollWheelUp><0,0>')
78      eq('nt', eval('mode(1)'))
79    end)
80
81    it('will not exit focus on left-release', function()
82      eq('t', eval('mode(1)'))
83      feed('<LeftRelease><0,0>')
84      eq('t', eval('mode(1)'))
85      command('setlocal number')
86      eq('t', eval('mode(1)'))
87      feed('<LeftRelease><0,0>')
88      eq('t', eval('mode(1)'))
89    end)
90
91    it('will not exit focus on mouse movement', function()
92      eq('t', eval('mode(1)'))
93      feed('<MouseMove><0,0>')
94      eq('t', eval('mode(1)'))
95      command('setlocal number')
96      eq('t', eval('mode(1)'))
97      feed('<MouseMove><0,0>')
98      eq('t', eval('mode(1)'))
99    end)
100
101    describe('with mouse events enabled by the program', function()
102      before_each(function()
103        tt.enable_mouse() -- FIXME: this doesn't work on Windows?
104        tt.feed_data('mouse enabled\n')
105        screen:expect([[
106          line27                                            |
107          line28                                            |
108          line29                                            |
109          line30                                            |
110          mouse enabled                                     |
111          ^                                                  |
112          {5:-- TERMINAL --}                                    |
113        ]])
114      end)
115
116      it('will forward mouse press, drag and release to the program', function()
117        skip(is_os('win'))
118        feed('<LeftMouse><1,2>')
119        screen:expect([[
120          line27                                            |
121          line28                                            |
122          line29                                            |
123          line30                                            |
124          mouse enabled                                     |
125           "#^                                               |
126          {5:-- TERMINAL --}                                    |
127        ]])
128        feed('<LeftDrag><2,2>')
129        screen:expect([[
130          line27                                            |
131          line28                                            |
132          line29                                            |
133          line30                                            |
134          mouse enabled                                     |
135             @##^                                            |
136          {5:-- TERMINAL --}                                    |
137        ]])
138        feed('<LeftDrag><3,2>')
139        screen:expect([[
140          line27                                            |
141          line28                                            |
142          line29                                            |
143          line30                                            |
144          mouse enabled                                     |
145                @$#^                                         |
146          {5:-- TERMINAL --}                                    |
147        ]])
148        feed('<LeftRelease><3,2>')
149        screen:expect([[
150          line27                                            |
151          line28                                            |
152          line29                                            |
153          line30                                            |
154          mouse enabled                                     |
155                   #$#^                                      |
156          {5:-- TERMINAL --}                                    |
157        ]])
158      end)
159
160      it('will forward mouse scroll to the program', function()
161        skip(is_os('win'))
162        feed('<ScrollWheelUp><0,0>')
163        screen:expect([[
164          line27                                            |
165          line28                                            |
166          line29                                            |
167          line30                                            |
168          mouse enabled                                     |
169          `!!^                                               |
170          {5:-- TERMINAL --}                                    |
171        ]])
172      end)
173
174      it('dragging and scrolling do not interfere with each other', function()
175        skip(is_os('win'))
176        feed('<LeftMouse><1,2>')
177        screen:expect([[
178          line27                                            |
179          line28                                            |
180          line29                                            |
181          line30                                            |
182          mouse enabled                                     |
183           "#^                                               |
184          {5:-- TERMINAL --}                                    |
185        ]])
186        feed('<ScrollWheelUp><1,2>')
187        screen:expect([[
188          line27                                            |
189          line28                                            |
190          line29                                            |
191          line30                                            |
192          mouse enabled                                     |
193             `"#^                                            |
194          {5:-- TERMINAL --}                                    |
195        ]])
196        feed('<LeftDrag><2,2>')
197        screen:expect([[
198          line27                                            |
199          line28                                            |
200          line29                                            |
201          line30                                            |
202          mouse enabled                                     |
203                @##^                                         |
204          {5:-- TERMINAL --}                                    |
205        ]])
206        feed('<ScrollWheelUp><2,2>')
207        screen:expect([[
208          line27                                            |
209          line28                                            |
210          line29                                            |
211          line30                                            |
212          mouse enabled                                     |
213                   `##^                                      |
214          {5:-- TERMINAL --}                                    |
215        ]])
216        feed('<LeftRelease><2,2>')
217        screen:expect([[
218          line27                                            |
219          line28                                            |
220          line29                                            |
221          line30                                            |
222          mouse enabled                                     |
223                      ###^                                   |
224          {5:-- TERMINAL --}                                    |
225        ]])
226      end)
227
228      it('will forward mouse clicks to the program with the correct even if set nu', function()
229        skip(is_os('win'))
230        command('set number')
231        screen:expect([[
232          {121: 11 }line28                                        |
233          {121: 12 }line29                                        |
234          {121: 13 }line30                                        |
235          {121: 14 }mouse enabled                                 |
236          {121: 15 }rows: 6, cols: 46                             |
237          {121: 16 }^                                              |
238          {5:-- TERMINAL --}                                    |
239        ]])
240        -- When the display area such as a number is clicked, it returns to the
241        -- normal mode.
242        feed('<LeftMouse><3,0>')
243        eq('nt', eval('mode(1)'))
244        screen:expect([[
245          {121: 11 }^line28                                        |
246          {121: 12 }line29                                        |
247          {121: 13 }line30                                        |
248          {121: 14 }mouse enabled                                 |
249          {121: 15 }rows: 6, cols: 46                             |
250          {121: 16 }                                              |
251                                                            |
252        ]])
253        -- If click on the coordinate (0,1) of the region of the terminal
254        -- (i.e. the coordinate (4,1) of vim), 'CSI !"' is sent to the terminal.
255        feed('i<LeftMouse><4,1>')
256        screen:expect([[
257          {121: 11 }line28                                        |
258          {121: 12 }line29                                        |
259          {121: 13 }line30                                        |
260          {121: 14 }mouse enabled                                 |
261          {121: 15 }rows: 6, cols: 46                             |
262          {121: 16 } !"^                                           |
263          {5:-- TERMINAL --}                                    |
264        ]])
265      end)
266
267      it('will lose focus if statusline is clicked', function()
268        command('set laststatus=2')
269        screen:expect([[
270          line29                                            |
271          line30                                            |
272          mouse enabled                                     |
273          rows: 5, cols: 50                                 |
274          ^                                                  |
275          ==========                                        |
276          {5:-- TERMINAL --}                                    |
277        ]])
278        feed('<LeftMouse><0,5>')
279        screen:expect([[
280          line29                                            |
281          line30                                            |
282          mouse enabled                                     |
283          rows: 5, cols: 50                                 |
284          ^                                                  |
285          ==========                                        |
286                                                            |
287        ]])
288        feed('<LeftDrag><0,4>')
289        screen:expect([[
290          mouse enabled                                     |
291          rows: 5, cols: 50                                 |
292          rows: 4, cols: 50                                 |
293          ^                                                  |
294          ==========                                        |
295                                                            |*2
296        ]])
297      end)
298
299      it('will lose focus if right separator is clicked', function()
300        command('rightbelow vnew | wincmd p | startinsert')
301        screen:expect([[
302          line29                  │                         |
303          line30                  │{100:~                        }|
304          mouse enabled           │{100:~                        }|
305          rows: 5, cols: 24       │{100:~                        }|
306          ^                        │{100:~                        }|
307          ==========               ==========               |
308          {5:-- TERMINAL --}                                    |
309        ]])
310        feed('<LeftMouse><24,0>')
311        screen:expect([[
312          line29                  │                         |
313          line30                  │{100:~                        }|
314          mouse enabled           │{100:~                        }|
315          rows: 5, cols: 24       │{100:~                        }|
316          ^                        │{100:~                        }|
317          ==========               ==========               |
318                                                            |
319        ]])
320        feed('<LeftDrag><23,0>')
321        screen:expect([[
322          line30                 │                          |
323          mouse enabled          │{100:~                         }|
324          rows: 5, cols: 24      │{100:~                         }|
325          rows: 5, cols: 23      │{100:~                         }|
326          ^                       │{100:~                         }|
327          ==========              ==========                |
328                                                            |
329        ]])
330      end)
331
332      it('will lose focus if winbar/tabline is clicked', function()
333        command('setlocal winbar=WINBAR')
334        screen:expect([[
335          {5:WINBAR                                            }|
336          line29                                            |
337          line30                                            |
338          mouse enabled                                     |
339          rows: 5, cols: 50                                 |
340          ^                                                  |
341          {5:-- TERMINAL --}                                    |
342        ]])
343        feed('<LeftMouse><0,0>')
344        screen:expect([[
345          {5:WINBAR                                            }|
346          line29                                            |
347          line30                                            |
348          mouse enabled                                     |
349          rows: 5, cols: 50                                 |
350          ^                                                  |
351                                                            |
352        ]])
353        command('set showtabline=2 tabline=TABLINE | startinsert')
354        screen:expect([[
355          {2:TABLINE                                           }|
356          {5:WINBAR                                            }|
357          mouse enabled                                     |
358          rows: 5, cols: 50                                 |
359          rows: 4, cols: 50                                 |
360          ^                                                  |
361          {5:-- TERMINAL --}                                    |
362        ]])
363        feed('<LeftMouse><0,0>')
364        screen:expect([[
365          {2:TABLINE                                           }|
366          {5:WINBAR                                            }|
367          mouse enabled                                     |
368          rows: 5, cols: 50                                 |
369          rows: 4, cols: 50                                 |
370          ^                                                  |
371                                                            |
372        ]])
373        command('setlocal winbar= | startinsert')
374        screen:expect([[
375          {2:TABLINE                                           }|
376          mouse enabled                                     |
377          rows: 5, cols: 50                                 |
378          rows: 4, cols: 50                                 |
379          rows: 5, cols: 50                                 |
380          ^                                                  |
381          {5:-- TERMINAL --}                                    |
382        ]])
383        feed('<LeftMouse><0,0>')
384        screen:expect([[
385          {2:TABLINE                                           }|
386          mouse enabled                                     |
387          rows: 5, cols: 50                                 |
388          rows: 4, cols: 50                                 |
389          rows: 5, cols: 50                                 |
390          ^                                                  |
391                                                            |
392        ]])
393      end)
394
395      it('mouse forwarding works with resized grid', function()
396        screen:detach()
397        local Screen = require('test.functional.ui.screen')
398        screen = Screen.new(50, 7, { ext_multigrid = true })
399        screen:expect([[
400        ## grid 1
401          [2:--------------------------------------------------]|*6
402          [3:--------------------------------------------------]|
403        ## grid 2
404          line27                                            |
405          line28                                            |
406          line29                                            |
407          line30                                            |
408          mouse enabled                                     |
409          ^                                                  |
410        ## grid 3
411          {5:-- TERMINAL --}                                    |
412        ]])
413
414        screen:try_resize_grid(2, 58, 11)
415        screen:expect({ any = vim.pesc('rows: 11, cols: 58') })
416
417        api.nvim_input_mouse('right', 'press', '', 2, 0, 0)
418        screen:expect({ any = vim.pesc('"!!^') })
419        api.nvim_input_mouse('right', 'release', '', 2, 0, 0)
420        screen:expect({ any = vim.pesc('#!!^') })
421
422        api.nvim_input_mouse('right', 'press', '', 2, 10, 0)
423        screen:expect({ any = vim.pesc('"!+^') })
424        api.nvim_input_mouse('right', 'release', '', 2, 10, 0)
425        screen:expect({ any = vim.pesc('#!+^') })
426
427        api.nvim_input_mouse('right', 'press', '', 2, 0, 57)
428        screen:expect({ any = vim.pesc('"Z!^') })
429        api.nvim_input_mouse('right', 'release', '', 2, 0, 57)
430        screen:expect({ any = vim.pesc('#Z!^') })
431
432        api.nvim_input_mouse('right', 'press', '', 2, 10, 57)
433        screen:expect({ any = vim.pesc('"Z+^') })
434        api.nvim_input_mouse('right', 'release', '', 2, 10, 57)
435        screen:expect({ any = vim.pesc('#Z+^') })
436
437        command('setlocal winbar=WINBAR')
438        screen:expect({ any = vim.pesc(('{5:WINBAR%s}'):format((' '):rep(52))) })
439        eq('t', api.nvim_get_mode().mode)
440
441        api.nvim_input_mouse('right', 'press', '', 2, 0, 0)
442        eq('nt', api.nvim_get_mode().mode)
443        api.nvim_input_mouse('right', 'release', '', 2, 0, 0)
444        feed('i')
445        eq('t', api.nvim_get_mode().mode)
446
447        api.nvim_input_mouse('right', 'press', '', 2, 0, 57)
448        eq('nt', api.nvim_get_mode().mode)
449        api.nvim_input_mouse('right', 'release', '', 2, 0, 57)
450        feed('i')
451        eq('t', api.nvim_get_mode().mode)
452
453        api.nvim_input_mouse('right', 'press', '', 2, 1, 0)
454        screen:expect({ any = vim.pesc('"!!^') })
455        api.nvim_input_mouse('right', 'release', '', 2, 1, 0)
456        screen:expect({ any = vim.pesc('#!!^') })
457
458        api.nvim_input_mouse('right', 'press', '', 2, 10, 0)
459        screen:expect({ any = vim.pesc('"!*^') })
460        api.nvim_input_mouse('right', 'release', '', 2, 10, 0)
461        screen:expect({ any = vim.pesc('#!*^') })
462
463        api.nvim_input_mouse('right', 'press', '', 2, 1, 57)
464        screen:expect({ any = vim.pesc('"Z!^') })
465        api.nvim_input_mouse('right', 'release', '', 2, 1, 57)
466        screen:expect({ any = vim.pesc('#Z!^') })
467
468        api.nvim_input_mouse('right', 'press', '', 2, 10, 57)
469        screen:expect({ any = vim.pesc('"Z*^') })
470        api.nvim_input_mouse('right', 'release', '', 2, 10, 57)
471        screen:expect({ any = vim.pesc('#Z*^') })
472      end)
473    end)
474
475    describe('with a split window and other buffer', function()
476      before_each(function()
477        feed('<c-\\><c-n>:vsp<cr>')
478        screen:expect([[
479          line28                   │line28                  |
480          line29                   │line29                  |
481          line30                   │line30                  |
482          rows: 5, cols: 25        │rows: 5, cols: 25       |
483          ^                         │                        |
484          ==========                ==========              |
485          :vsp                                              |
486        ]])
487        feed(':enew | set number<cr>')
488        screen:expect([[
489          {121:  1 }^                     │line29                  |
490          {100:~                        }│line30                  |
491          {100:~                        }│rows: 5, cols: 25       |
492          {100:~                        }│rows: 5, cols: 24       |
493          {100:~                        }│                        |
494          ==========                ==========              |
495          :enew | set number                                |
496        ]])
497        feed('30iline\n<esc>')
498        screen:expect([[
499          {121: 27 }line                 │line29                  |
500          {121: 28 }line                 │line30                  |
501          {121: 29 }line                 │rows: 5, cols: 25       |
502          {121: 30 }line                 │rows: 5, cols: 24       |
503          {121: 31 }^                     │                        |
504          ==========                ==========              |
505                                                            |
506        ]])
507        feed('<c-w>li')
508        screen:expect([[
509          {121: 27 }line                 │line29                  |
510          {121: 28 }line                 │line30                  |
511          {121: 29 }line                 │rows: 5, cols: 25       |
512          {121: 30 }line                 │rows: 5, cols: 24       |
513          {121: 31 }                     │^                        |
514          ==========                ==========              |
515          {5:-- TERMINAL --}                                    |
516        ]])
517
518        -- enabling mouse won't affect interaction with other windows
519        tt.enable_mouse()
520        tt.feed_data('mouse enabled\n')
521        screen:expect([[
522          {121: 27 }line                 │line30                  |
523          {121: 28 }line                 │rows: 5, cols: 25       |
524          {121: 29 }line                 │rows: 5, cols: 24       |
525          {121: 30 }line                 │mouse enabled           |
526          {121: 31 }                     │^                        |
527          ==========                ==========              |
528          {5:-- TERMINAL --}                                    |
529        ]])
530      end)
531
532      it("scrolling another window keeps focus and respects 'mousescroll'", function()
533        feed('<ScrollWheelUp><4,0><ScrollWheelUp><4,0>')
534        screen:expect([[
535          {121: 21 }line                 │line30                  |
536          {121: 22 }line                 │rows: 5, cols: 25       |
537          {121: 23 }line                 │rows: 5, cols: 24       |
538          {121: 24 }line                 │mouse enabled           |
539          {121: 25 }line                 │^                        |
540          ==========                ==========              |
541          {5:-- TERMINAL --}                                    |
542        ]])
543        feed('<S-ScrollWheelDown><4,0>')
544        screen:expect([[
545          {121: 26 }line                 │line30                  |
546          {121: 27 }line                 │rows: 5, cols: 25       |
547          {121: 28 }line                 │rows: 5, cols: 24       |
548          {121: 29 }line                 │mouse enabled           |
549          {121: 30 }line                 │^                        |
550          ==========                ==========              |
551          {5:-- TERMINAL --}                                    |
552        ]])
553        command('set mousescroll=ver:10')
554        feed('<ScrollWheelUp><0,0>')
555        screen:expect([[
556          {121: 16 }line                 │line30                  |
557          {121: 17 }line                 │rows: 5, cols: 25       |
558          {121: 18 }line                 │rows: 5, cols: 24       |
559          {121: 19 }line                 │mouse enabled           |
560          {121: 20 }line                 │^                        |
561          ==========                ==========              |
562          {5:-- TERMINAL --}                                    |
563        ]])
564        command('set mousescroll=ver:0')
565        feed('<ScrollWheelUp><0,0>')
566        screen:expect_unchanged()
567        feed([[<C-\><C-N><C-W>w]])
568        command('setlocal nowrap')
569        feed('0<C-V>gg3ly$4p<C-W>wi')
570        screen:expect([[
571          {121:  1 }linelinelinelineline │line30                  |
572          {121:  2 }linelinelinelineline │rows: 5, cols: 25       |
573          {121:  3 }linelinelinelineline │rows: 5, cols: 24       |
574          {121:  4 }linelinelinelineline │mouse enabled           |
575          {121:  5 }linelinelinelineline │^                        |
576          ==========                ==========              |
577          {5:-- TERMINAL --}                                    |
578        ]])
579        feed('<ScrollWheelRight><4,0>')
580        screen:expect([[
581          {121:  1 }nelinelineline       │line30                  |
582          {121:  2 }nelinelineline       │rows: 5, cols: 25       |
583          {121:  3 }nelinelineline       │rows: 5, cols: 24       |
584          {121:  4 }nelinelineline       │mouse enabled           |
585          {121:  5 }nelinelineline       │^                        |
586          ==========                ==========              |
587          {5:-- TERMINAL --}                                    |
588        ]])
589        command('set mousescroll=hor:4')
590        feed('<ScrollWheelLeft><4,0>')
591        screen:expect([[
592          {121:  1 }nelinelinelineline   │line30                  |
593          {121:  2 }nelinelinelineline   │rows: 5, cols: 25       |
594          {121:  3 }nelinelinelineline   │rows: 5, cols: 24       |
595          {121:  4 }nelinelinelineline   │mouse enabled           |
596          {121:  5 }nelinelinelineline   │^                        |
597          ==========                ==========              |
598          {5:-- TERMINAL --}                                    |
599        ]])
600      end)
601
602      it('will lose focus if another window is clicked', function()
603        feed('<LeftMouse><5,1>')
604        screen:expect([[
605          {121: 27 }line                 │line30                  |
606          {121: 28 }l^ine                 │rows: 5, cols: 25       |
607          {121: 29 }line                 │rows: 5, cols: 24       |
608          {121: 30 }line                 │mouse enabled           |
609          {121: 31 }                     │                        |
610          ==========                ==========              |
611                                                            |
612        ]])
613      end)
614
615      it('handles terminal size when switching buffers', function()
616        api.nvim_set_option_value('hidden', true, {})
617        feed('<c-\\><c-n><c-w><c-w>')
618        screen:expect([[
619          {121: 27 }line                 │line30                  |
620          {121: 28 }line                 │rows: 5, cols: 25       |
621          {121: 29 }line                 │rows: 5, cols: 24       |
622          {121: 30 }line                 │mouse enabled           |
623          {121: 31 }^                     │                        |
624          ==========                ==========              |
625                                                            |
626        ]])
627        feed(':bn<cr>')
628        screen:expect([[
629          rows: 5, cols: 25        │rows: 5, cols: 25       |
630          rows: 5, cols: 24        │rows: 5, cols: 24       |
631          mouse enabled            │mouse enabled           |
632          rows: 5, cols: 25        │rows: 5, cols: 25       |
633          ^                         │                        |
634          ==========                ==========              |
635          :bn                                               |
636        ]])
637        feed(':bn<cr>')
638        screen:expect([[
639          {121: 27 }line                 │rows: 5, cols: 24       |
640          {121: 28 }line                 │mouse enabled           |
641          {121: 29 }line                 │rows: 5, cols: 25       |
642          {121: 30 }line                 │rows: 5, cols: 24       |
643          {121: 31 }^                     │                        |
644          ==========                ==========              |
645          :bn                                               |
646        ]])
647      end)
648    end)
649  end)
650end)