1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
local t = require('test.testutil')
local n = require('test.functional.testnvim')()
local Screen = require('test.functional.ui.screen')
local clear = n.clear
local eq = t.eq
local eval = n.eval
local exec = n.exec
local command = n.command
local feed = n.feed
local api = n.api
local assert_alive = n.assert_alive
before_each(clear)
describe('WinResized', function()
-- oldtest: Test_WinResized()
it('works', function()
exec([[
set scrolloff=0
call setline(1, ['111', '222'])
vnew
call setline(1, ['aaa', 'bbb'])
new
call setline(1, ['foo', 'bar'])
let g:resized = 0
au WinResized * let g:resized += 1
au WinResized * let g:v_event = deepcopy(v:event)
]])
eq(0, eval('g:resized'))
-- increase window height, two windows will be reported
feed('<C-W>+')
eq(1, eval('g:resized'))
eq({ windows = { 1002, 1001 } }, eval('g:v_event'))
-- increase window width, three windows will be reported
feed('<C-W>>')
eq(2, eval('g:resized'))
eq({ windows = { 1002, 1001, 1000 } }, eval('g:v_event'))
end)
it('is triggered in terminal mode #21197 #27207', function()
exec([[
autocmd TermOpen * startinsert
let g:resized = 0
autocmd WinResized * let g:resized += 1
]])
eq(0, eval('g:resized'))
command('vsplit term://')
eq({ mode = 't', blocking = false }, api.nvim_get_mode())
eq(1, eval('g:resized'))
command('split')
eq({ mode = 't', blocking = false }, api.nvim_get_mode())
eq(2, eval('g:resized'))
end)
end)
describe('WinScrolled', function()
local win_id
before_each(function()
win_id = api.nvim_get_current_win()
command(string.format('autocmd WinScrolled %d let g:matched = v:true', win_id))
exec([[
let g:scrolled = 0
au WinScrolled * let g:scrolled += 1
au WinScrolled * let g:amatch = str2nr(expand('<amatch>'))
au WinScrolled * let g:afile = str2nr(expand('<afile>'))
au WinScrolled * let g:v_event = deepcopy(v:event)
]])
end)
after_each(function()
eq(true, eval('g:matched'))
eq(win_id, eval('g:amatch'))
eq(win_id, eval('g:afile'))
end)
it('is triggered by scrolling vertically', function()
local lines = { '123', '123' }
api.nvim_buf_set_lines(0, 0, -1, true, lines)
eq(0, eval('g:scrolled'))
feed('<C-E>')
eq(1, eval('g:scrolled'))
eq({
all = { leftcol = 0, topline = 1, topfill = 0, width = 0, height = 0, skipcol = 0 },
['1000'] = { leftcol = 0, topline = 1, topfill = 0, width = 0, height = 0, skipcol = 0 },
}, eval('g:v_event'))
feed('<C-Y>')
eq(2, eval('g:scrolled'))
eq({
all = { leftcol = 0, topline = 1, topfill = 0, width = 0, height = 0, skipcol = 0 },
['1000'] = { leftcol = 0, topline = -1, topfill = 0, width = 0, height = 0, skipcol = 0 },
}, eval('g:v_event'))
end)
it('is triggered by scrolling horizontally', function()
command('set nowrap')
local width = api.nvim_win_get_width(0)
local line = '123' .. ('*'):rep(width * 2)
local lines = { line, line }
api.nvim_buf_set_lines(0, 0, -1, true, lines)
eq(0, eval('g:scrolled'))
feed('zl')
eq(1, eval('g:scrolled'))
eq({
all = { leftcol = 1, topline = 0, topfill = 0, width = 0, height = 0, skipcol = 0 },
['1000'] = { leftcol = 1, topline = 0, topfill = 0, width = 0, height = 0, skipcol = 0 },
}, eval('g:v_event'))
feed('zh')
eq(2, eval('g:scrolled'))
eq({
all = { leftcol = 1, topline = 0, topfill = 0, width = 0, height = 0, skipcol = 0 },
['1000'] = { leftcol = -1, topline = 0, topfill = 0, width = 0, height = 0, skipcol = 0 },
}, eval('g:v_event'))
end)
it('is triggered by horizontal scrolling from cursor move', function()
command('set nowrap')
local lines = { '', '', 'Foo' }
api.nvim_buf_set_lines(0, 0, -1, true, lines)
api.nvim_win_set_cursor(0, { 3, 0 })
eq(0, eval('g:scrolled'))
feed('zl')
eq(1, eval('g:scrolled'))
eq({
all = { leftcol = 1, topline = 0, topfill = 0, width = 0, height = 0, skipcol = 0 },
['1000'] = { leftcol = 1, topline = 0, topfill = 0, width = 0, height = 0, skipcol = 0 },
}, eval('g:v_event'))
feed('zl')
eq(2, eval('g:scrolled'))
eq({
all = { leftcol = 1, topline = 0, topfill = 0, width = 0, height = 0, skipcol = 0 },
['1000'] = { leftcol = 1, topline = 0, topfill = 0, width = 0, height = 0, skipcol = 0 },
}, eval('g:v_event'))
feed('h')
eq(3, eval('g:scrolled'))
eq({
all = { leftcol = 1, topline = 0, topfill = 0, width = 0, height = 0, skipcol = 0 },
['1000'] = { leftcol = -1, topline = 0, topfill = 0, width = 0, height = 0, skipcol = 0 },
}, eval('g:v_event'))
feed('zh')
eq(4, eval('g:scrolled'))
eq({
all = { leftcol = 1, topline = 0, topfill = 0, width = 0, height = 0, skipcol = 0 },
['1000'] = { leftcol = -1, topline = 0, topfill = 0, width = 0, height = 0, skipcol = 0 },
}, eval('g:v_event'))
end)
-- oldtest: Test_WinScrolled_long_wrapped()
it('is triggered by scrolling on a long wrapped line #19968', function()
local height = api.nvim_win_get_height(0)
local width = api.nvim_win_get_width(0)
api.nvim_buf_set_lines(0, 0, -1, true, { ('foo'):rep(height * width) })
api.nvim_win_set_cursor(0, { 1, height * width - 1 })
eq(0, eval('g:scrolled'))
feed('gj')
eq(1, eval('g:scrolled'))
eq({
all = { leftcol = 0, topline = 0, topfill = 0, width = 0, height = 0, skipcol = width },
['1000'] = { leftcol = 0, topline = 0, topfill = 0, width = 0, height = 0, skipcol = width },
}, eval('g:v_event'))
feed('0')
eq(2, eval('g:scrolled'))
eq({
all = { leftcol = 0, topline = 0, topfill = 0, width = 0, height = 0, skipcol = width },
['1000'] = { leftcol = 0, topline = 0, topfill = 0, width = 0, height = 0, skipcol = -width },
}, eval('g:v_event'))
feed('$')
eq(3, eval('g:scrolled'))
end)
it('is triggered when the window scrolls in Insert mode', function()
local height = api.nvim_win_get_height(0)
local lines = {}
for i = 1, height * 2 do
lines[i] = tostring(i)
end
api.nvim_buf_set_lines(0, 0, -1, true, lines)
feed('M')
eq(0, eval('g:scrolled'))
feed('i<C-X><C-E><Esc>')
eq(1, eval('g:scrolled'))
eq({
all = { leftcol = 0, topline = 1, topfill = 0, width = 0, height = 0, skipcol = 0 },
['1000'] = { leftcol = 0, topline = 1, topfill = 0, width = 0, height = 0, skipcol = 0 },
}, eval('g:v_event'))
feed('i<C-X><C-Y><Esc>')
eq(2, eval('g:scrolled'))
eq({
all = { leftcol = 0, topline = 1, topfill = 0, width = 0, height = 0, skipcol = 0 },
['1000'] = { leftcol = 0, topline = -1, topfill = 0, width = 0, height = 0, skipcol = 0 },
}, eval('g:v_event'))
feed('L')
eq(2, eval('g:scrolled'))
feed('A<CR><Esc>')
eq(3, eval('g:scrolled'))
eq({
all = { leftcol = 0, topline = 1, topfill = 0, width = 0, height = 0, skipcol = 0 },
['1000'] = { leftcol = 0, topline = 1, topfill = 0, width = 0, height = 0, skipcol = 0 },
}, eval('g:v_event'))
end)
end)
describe('WinScrolled', function()
-- oldtest: Test_WinScrolled_mouse()
it('is triggered by mouse scrolling in another window', function()
local _ = Screen.new(75, 10)
exec([[
set nowrap scrolloff=0
set mouse=a
call setline(1, ['foo']->repeat(32))
split
let g:scrolled = 0
au WinScrolled * let g:scrolled += 1
]])
eq(0, eval('g:scrolled'))
-- With the upper split focused, send a scroll-down event to the unfocused one.
api.nvim_input_mouse('wheel', 'down', '', 0, 6, 0)
eq(1, eval('g:scrolled'))
-- Again, but this time while we're in insert mode.
feed('i')
api.nvim_input_mouse('wheel', 'down', '', 0, 6, 0)
feed('<Esc>')
eq(2, eval('g:scrolled'))
end)
-- oldtest: Test_WinScrolled_close_curwin()
it('closing window does not cause use-after-free #13265', function()
exec([[
set nowrap scrolloff=0
call setline(1, ['aaa', 'bbb'])
vsplit
au WinScrolled * close
]])
-- This was using freed memory
feed('<C-E>')
assert_alive()
end)
-- oldtest: Test_WinScrolled_diff()
it('is triggered for both windows when scrolling in diff mode', function()
exec([[
set diffopt+=foldcolumn:0
call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
vnew
call setline(1, ['d', 'e', 'f', 'g', 'h', 'i'])
windo diffthis
au WinScrolled * let g:v_event = deepcopy(v:event)
]])
feed('<C-E>')
eq({
all = { leftcol = 0, topline = 1, topfill = 1, width = 0, height = 0, skipcol = 0 },
['1000'] = { leftcol = 0, topline = 1, topfill = 0, width = 0, height = 0, skipcol = 0 },
['1001'] = { leftcol = 0, topline = 0, topfill = -1, width = 0, height = 0, skipcol = 0 },
}, eval('g:v_event'))
feed('2<C-E>')
eq({
all = { leftcol = 0, topline = 2, topfill = 2, width = 0, height = 0, skipcol = 0 },
['1000'] = { leftcol = 0, topline = 2, topfill = 0, width = 0, height = 0, skipcol = 0 },
['1001'] = { leftcol = 0, topline = 0, topfill = -2, width = 0, height = 0, skipcol = 0 },
}, eval('g:v_event'))
feed('<C-E>')
eq({
all = { leftcol = 0, topline = 2, topfill = 0, width = 0, height = 0, skipcol = 0 },
['1000'] = { leftcol = 0, topline = 1, topfill = 0, width = 0, height = 0, skipcol = 0 },
['1001'] = { leftcol = 0, topline = 1, topfill = 0, width = 0, height = 0, skipcol = 0 },
}, eval('g:v_event'))
feed('2<C-Y>')
eq({
all = { leftcol = 0, topline = 3, topfill = 1, width = 0, height = 0, skipcol = 0 },
['1000'] = { leftcol = 0, topline = -2, topfill = 0, width = 0, height = 0, skipcol = 0 },
['1001'] = { leftcol = 0, topline = -1, topfill = 1, width = 0, height = 0, skipcol = 0 },
}, eval('g:v_event'))
end)
it('is triggered by mouse scrolling in unfocused floating window #18222', function()
local screen = Screen.new(80, 24)
exec([[
let g:scrolled = 0
autocmd WinScrolled * let g:scrolled += 1
autocmd WinScrolled * let g:amatch = expand('<amatch>')
autocmd WinScrolled * let g:v_event = deepcopy(v:event)
]])
eq(0, eval('g:scrolled'))
local buf = api.nvim_create_buf(true, true)
api.nvim_buf_set_lines(
buf,
0,
-1,
false,
{ '@', 'b', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n' }
)
local win = api.nvim_open_win(buf, false, {
height = 5,
width = 10,
col = 0,
row = 1,
relative = 'editor',
style = 'minimal',
})
screen:expect({ any = '@' })
local winid_str = tostring(win)
-- WinScrolled should not be triggered when creating a new floating window
eq(0, eval('g:scrolled'))
api.nvim_input_mouse('wheel', 'down', '', 0, 3, 3)
eq(1, eval('g:scrolled'))
eq(winid_str, eval('g:amatch'))
eq({
all = { leftcol = 0, topline = 3, topfill = 0, width = 0, height = 0, skipcol = 0 },
[winid_str] = { leftcol = 0, topline = 3, topfill = 0, width = 0, height = 0, skipcol = 0 },
}, eval('g:v_event'))
api.nvim_input_mouse('wheel', 'up', '', 0, 3, 3)
eq(2, eval('g:scrolled'))
eq(tostring(win), eval('g:amatch'))
eq({
all = { leftcol = 0, topline = 3, topfill = 0, width = 0, height = 0, skipcol = 0 },
[winid_str] = { leftcol = 0, topline = -3, topfill = 0, width = 0, height = 0, skipcol = 0 },
}, eval('g:v_event'))
end)
end)
|