blob: 21d6819a4f9990c6592bc13c1e948576ef379b72 [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001" Tests for popup windows
2
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003CheckFeature popupwin
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004
Christian Brabandteb380b92025-07-07 20:53:55 +02005source util/screendump.vim
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006
7func Test_simple_popup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02008 CheckScreendump
9
Bram Moolenaare7eb9272019-06-24 00:58:07 +020010 let lines =<< trim END
11 call setline(1, range(1, 100))
12 hi PopupColor1 ctermbg=lightblue
13 hi PopupColor2 ctermbg=lightcyan
Bram Moolenaar1666ac92019-11-10 17:22:31 +010014 hi EndOfBuffer ctermbg=lightgrey
Bram Moolenaare7eb9272019-06-24 00:58:07 +020015 hi Comment ctermfg=red
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020016 call prop_type_add('comment', #{highlight: 'Comment'})
17 let winid = popup_create('hello there', #{line: 3, col: 11, minwidth: 20, highlight: 'PopupColor1'})
18 let winid2 = popup_create(['another one', 'another two', 'another three'], #{line: 3, col: 25, minwidth: 20})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020019 call setwinvar(winid2, '&wincolor', 'PopupColor2')
20 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +010021 call writefile(lines, 'XtestPopup', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020022 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar4d784b22019-05-25 19:51:39 +020023 call VerifyScreenDump(buf, 'Test_popupwin_01', {})
24
Bram Moolenaarec583842019-05-26 14:11:23 +020025 " Add a tabpage
26 call term_sendkeys(buf, ":tabnew\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020027 call term_sendkeys(buf, ":let popupwin = popup_create(["
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020028 \ .. "#{text: 'other tab'},"
29 \ .. "#{text: 'a comment line', props: [#{"
Bram Moolenaard5abb4c2019-07-13 22:46:10 +020030 \ .. "col: 3, length: 7, minwidth: 20, type: 'comment'"
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020031 \ .. "}]},"
Boris Staletic13c11532024-12-19 20:22:19 +010032 \ .. "], #{line: 4, col: 9, minwidth: 20, fixed: v:true})\<CR>")
Bram Moolenaarec583842019-05-26 14:11:23 +020033 call VerifyScreenDump(buf, 'Test_popupwin_02', {})
34
35 " switch back to first tabpage
36 call term_sendkeys(buf, "gt")
37 call VerifyScreenDump(buf, 'Test_popupwin_03', {})
38
39 " close that tabpage
40 call term_sendkeys(buf, ":quit!\<CR>")
41 call VerifyScreenDump(buf, 'Test_popupwin_04', {})
42
Bram Moolenaar202d9822019-06-11 21:56:30 +020043 " set 'columns' to a small value, size must be recomputed
44 call term_sendkeys(buf, ":let cols = &columns\<CR>")
45 call term_sendkeys(buf, ":set columns=12\<CR>")
46 call VerifyScreenDump(buf, 'Test_popupwin_04a', {})
47 call term_sendkeys(buf, ":let &columns = cols\<CR>")
48
Bram Moolenaar17146962019-05-30 00:12:11 +020049 " resize popup, show empty line at bottom
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020050 call term_sendkeys(buf, ":call popup_move(popupwin, #{minwidth: 15, maxwidth: 25, minheight: 3, maxheight: 5})\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020051 call term_sendkeys(buf, ":redraw\<CR>")
52 call VerifyScreenDump(buf, 'Test_popupwin_05', {})
53
Bram Moolenaar17146962019-05-30 00:12:11 +020054 " show not fitting line at bottom
55 call term_sendkeys(buf, ":call setbufline(winbufnr(popupwin), 3, 'this line will not fit here')\<CR>")
56 call term_sendkeys(buf, ":redraw\<CR>")
57 call VerifyScreenDump(buf, 'Test_popupwin_06', {})
58
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020059 " move popup over ruler
60 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020061 call term_sendkeys(buf, ":call popup_move(popupwin, #{line: 7, col: 55})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +020062 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020063 call VerifyScreenDump(buf, 'Test_popupwin_07', {})
64
65 " clear all popups after moving the cursor a bit, so that ruler is updated
66 call term_sendkeys(buf, "axxx\<Esc>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +020067 call TermWait(buf)
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020068 call term_sendkeys(buf, "0")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +020069 call TermWait(buf)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +020070 call term_sendkeys(buf, ":call popup_clear()\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020071 call VerifyScreenDump(buf, 'Test_popupwin_08', {})
72
Bram Moolenaar4d784b22019-05-25 19:51:39 +020073 " clean up
74 call StopVimInTerminal(buf)
Bram Moolenaar4d784b22019-05-25 19:51:39 +020075endfunc
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020076
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020077func Test_popup_with_border_and_padding()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +020078 CheckScreendump
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020079
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020080 for iter in range(0, 1)
Bram Moolenaare7eb9272019-06-24 00:58:07 +020081 let lines =<< trim END
82 call setline(1, range(1, 100))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020083 call popup_create('hello border', #{line: 2, col: 3, border: []})
84 call popup_create('hello padding', #{line: 2, col: 23, padding: []})
Bram Moolenaarc363fe12019-08-04 18:13:46 +020085 call popup_create('hello both', #{line: 2, col: 43, border: [], padding: [], highlight: 'Normal'})
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020086 call popup_create('border TL', #{line: 6, col: 3, border: [1, 0, 0, 4]})
Bram Moolenaarb0992022020-01-30 14:55:42 +010087 call popup_create('paddings', #{line: 6, col: 23, padding: range(1, 4)})
Boris Staletic13c11532024-12-19 20:22:19 +010088 call popup_create('wrapped longer text', #{line: 8, col: 55, padding: [0, 3, 0, 3], border: [0, 1, 0, 1], fixed: v:true})
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020089 call popup_create('right aligned text', #{line: 11, col: 56, wrap: 0, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
Bram Moolenaarfbcdf672020-01-06 23:07:48 +010090 call popup_create('X', #{line: 2, col: 73})
91 call popup_create('X', #{line: 3, col: 74})
92 call popup_create('X', #{line: 4, col: 75})
93 call popup_create('X', #{line: 5, col: 76})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020094 END
95 call insert(lines, iter == 1 ? '' : 'set enc=latin1')
Bram Moolenaar145d1fd2022-09-30 21:57:11 +010096 call writefile(lines, 'XtestPopupBorder', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020097 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 15})
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020098 call VerifyScreenDump(buf, 'Test_popupwin_2' .. iter, {})
99
100 call StopVimInTerminal(buf)
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +0200101 endfor
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200102
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200103 let lines =<< trim END
104 call setline(1, range(1, 100))
105 hi BlueColor ctermbg=lightblue
106 hi TopColor ctermbg=253
107 hi RightColor ctermbg=245
108 hi BottomColor ctermbg=240
109 hi LeftColor ctermbg=248
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200110 call popup_create('hello border', #{line: 2, col: 3, border: [], borderhighlight: ['BlueColor']})
111 call popup_create(['hello border', 'and more'], #{line: 2, col: 23, border: [], borderhighlight: ['TopColor', 'RightColor', 'BottomColor', 'LeftColor']})
112 call popup_create(['hello border', 'lines only'], #{line: 2, col: 43, border: [], borderhighlight: ['BlueColor'], borderchars: ['x']})
113 call popup_create(['hello border', 'with corners'], #{line: 2, col: 60, border: [], borderhighlight: ['BlueColor'], borderchars: ['x', '#']})
114 let winid = popup_create(['hello border', 'with numbers'], #{line: 6, col: 3, border: [], borderhighlight: ['BlueColor'], borderchars: ['0', '1', '2', '3', '4', '5', '6', '7']})
115 call popup_create(['hello border', 'just blanks'], #{line: 7, col: 23, border: [], borderhighlight: ['BlueColor'], borderchars: [' ']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200116 func MultiByte()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200117 call popup_create(['hello'], #{line: 8, col: 43, border: [], borderchars: ['─', '│', '─', '│', '┌', '┐', '┘', '└']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200118 endfunc
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200119 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100120 call writefile(lines, 'XtestPopupBorder', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200121 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 12})
Bram Moolenaar790498b2019-06-01 22:15:29 +0200122 call VerifyScreenDump(buf, 'Test_popupwin_22', {})
123
Bram Moolenaarad24a712019-06-17 20:05:45 +0200124 " check that changing borderchars triggers a redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200125 call term_sendkeys(buf, ":call popup_setoptions(winid, #{borderchars: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']})\<CR>")
Bram Moolenaarad24a712019-06-17 20:05:45 +0200126 call VerifyScreenDump(buf, 'Test_popupwin_23', {})
127
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200128 " check multi-byte border only with 'ambiwidth' single
129 if &ambiwidth == 'single'
130 call term_sendkeys(buf, ":call MultiByte()\<CR>")
131 call VerifyScreenDump(buf, 'Test_popupwin_24', {})
132 endif
133
Bram Moolenaar790498b2019-06-01 22:15:29 +0200134 call StopVimInTerminal(buf)
Bram Moolenaar790498b2019-06-01 22:15:29 +0200135
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200136 let with_border_or_padding = #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200137 \ line: 2,
138 \ core_line: 3,
139 \ col: 3,
140 \ core_col: 4,
141 \ width: 14,
142 \ core_width: 12,
143 \ height: 3,
144 \ core_height: 1,
145 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100146 \ lastline: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200147 \ scrollbar: 0,
148 \ visible: 1}
Doug Kearnsd84cbdb2025-04-05 15:51:26 +0200149 let winid = popup_create('hello border', #{line: 2, col: 3, border: []})
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200150 call assert_equal(with_border_or_padding, winid->popup_getpos())
Bram Moolenaarae943152019-06-16 22:54:14 +0200151 let options = popup_getoptions(winid)
152 call assert_equal([], options.border)
153 call assert_false(has_key(options, "padding"))
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200154
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200155 let winid = popup_create('hello padding', #{line: 2, col: 3, padding: []})
Bram Moolenaarae943152019-06-16 22:54:14 +0200156 let with_border_or_padding.width = 15
157 let with_border_or_padding.core_width = 13
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200158 call assert_equal(with_border_or_padding, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200159 let options = popup_getoptions(winid)
160 call assert_false(has_key(options, "border"))
161 call assert_equal([], options.padding)
162
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200163 call popup_setoptions(winid, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200164 \ padding: [1, 2, 3, 4],
165 \ border: [4, 0, 7, 8],
166 \ borderhighlight: ['Top', 'Right', 'Bottom', 'Left'],
167 \ borderchars: ['1', '^', '2', '>', '3', 'v', '4', '<'],
Bram Moolenaarae943152019-06-16 22:54:14 +0200168 \ })
169 let options = popup_getoptions(winid)
170 call assert_equal([1, 0, 1, 1], options.border)
171 call assert_equal([1, 2, 3, 4], options.padding)
172 call assert_equal(['Top', 'Right', 'Bottom', 'Left'], options.borderhighlight)
173 call assert_equal(['1', '^', '2', '>', '3', 'v', '4', '<'], options.borderchars)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200174
Bram Moolenaara1396152019-10-20 18:46:05 +0200175 " Check that popup_setoptions() takes the output of popup_getoptions()
176 call popup_setoptions(winid, options)
177 call assert_equal(options, popup_getoptions(winid))
178
Bram Moolenaarb0992022020-01-30 14:55:42 +0100179 " Check that range() doesn't crash
180 call popup_setoptions(winid, #{
181 \ padding: range(1, 4),
182 \ border: range(5, 8),
183 \ borderhighlight: range(4),
184 \ borderchars: range(8),
185 \ })
186
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200187 let winid = popup_create('hello both', #{line: 3, col: 8, border: [], padding: []})
188 call assert_equal(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200189 \ line: 3,
190 \ core_line: 5,
191 \ col: 8,
192 \ core_col: 10,
193 \ width: 14,
194 \ core_width: 10,
195 \ height: 5,
196 \ scrollbar: 0,
197 \ core_height: 1,
198 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100199 \ lastline: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200200 \ visible: 1}, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200201
202 call popup_clear()
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200203endfunc
204
Bram Moolenaarb4230122019-05-30 18:40:53 +0200205func Test_popup_with_syntax_win_execute()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200206 CheckScreendump
207
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200208 let lines =<< trim END
209 call setline(1, range(1, 100))
210 hi PopupColor ctermbg=lightblue
211 let winid = popup_create([
212 \ '#include <stdio.h>',
213 \ 'int main(void)',
214 \ '{',
215 \ ' printf(123);',
216 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200217 \], #{line: 3, col: 25, highlight: 'PopupColor'})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200218 call win_execute(winid, 'set syntax=cpp')
219 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100220 call writefile(lines, 'XtestPopup', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200221 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200222 call VerifyScreenDump(buf, 'Test_popupwin_10', {})
223
224 " clean up
225 call StopVimInTerminal(buf)
Bram Moolenaarb4230122019-05-30 18:40:53 +0200226endfunc
227
228func Test_popup_with_syntax_setbufvar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200229 CheckScreendump
230
Bram Moolenaar402502d2019-05-30 22:07:36 +0200231 let lines =<< trim END
232 call setline(1, range(1, 100))
233 hi PopupColor ctermbg=lightgrey
234 let winid = popup_create([
235 \ '#include <stdio.h>',
236 \ 'int main(void)',
237 \ '{',
Bram Moolenaare089c3f2019-07-09 20:25:25 +0200238 \ "\tprintf(567);",
Bram Moolenaar402502d2019-05-30 22:07:36 +0200239 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200240 \], #{line: 3, col: 21, highlight: 'PopupColor'})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200241 call setbufvar(winbufnr(winid), '&syntax', 'cpp')
242 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100243 call writefile(lines, 'XtestPopup', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200244 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200245 call VerifyScreenDump(buf, 'Test_popupwin_11', {})
246
247 " clean up
248 call StopVimInTerminal(buf)
Bram Moolenaarb4230122019-05-30 18:40:53 +0200249endfunc
250
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200251func Test_popup_with_matches()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200252 CheckScreendump
253
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200254 let lines =<< trim END
255 call setline(1, ['111 222 333', '444 555 666'])
256 let winid = popup_create([
257 \ '111 222 333',
258 \ '444 555 666',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200259 \], #{line: 3, col: 10, border: []})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200260 set hlsearch
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100261 hi VeryBlue ctermfg=blue guifg=blue
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200262 /666
263 call matchadd('ErrorMsg', '111')
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100264 call matchadd('VeryBlue', '444')
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200265 call win_execute(winid, "call matchadd('ErrorMsg', '111')")
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100266 call win_execute(winid, "call matchadd('VeryBlue', '555')")
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200267 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100268 call writefile(lines, 'XtestPopupMatches', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200269 let buf = RunVimInTerminal('-S XtestPopupMatches', #{rows: 10})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200270 call VerifyScreenDump(buf, 'Test_popupwin_matches', {})
271
272 " clean up
273 call StopVimInTerminal(buf)
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200274endfunc
275
Bram Moolenaar399d8982019-06-02 15:34:29 +0200276func Test_popup_all_corners()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200277 CheckScreendump
278
Bram Moolenaar399d8982019-06-02 15:34:29 +0200279 let lines =<< trim END
280 call setline(1, repeat([repeat('-', 60)], 15))
281 set so=0
282 normal 2G3|r#
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200283 let winid1 = popup_create(['first', 'second'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200284 \ line: 'cursor+1',
285 \ col: 'cursor',
286 \ pos: 'topleft',
287 \ border: [],
288 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200289 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200290 normal 24|r@
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200291 let winid1 = popup_create(['First', 'SeconD'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200292 \ line: 'cursor+1',
293 \ col: 'cursor',
294 \ pos: 'topright',
295 \ border: [],
296 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200297 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200298 normal 9G27|r%
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200299 let winid1 = popup_create(['fiRSt', 'seCOnd'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200300 \ line: 'cursor-1',
301 \ col: 'cursor',
302 \ pos: 'botleft',
303 \ border: [],
304 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200305 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200306 normal 48|r&
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200307 let winid1 = popup_create(['FIrsT', 'SEcoND'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200308 \ line: 'cursor-1',
309 \ col: 'cursor',
310 \ pos: 'botright',
311 \ border: [],
312 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200313 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200314 normal 1G51|r*
315 let winid1 = popup_create(['one', 'two'], #{
316 \ line: 'cursor-1',
317 \ col: 'cursor',
318 \ pos: 'botleft',
319 \ border: [],
320 \ padding: [],
321 \ })
Bram Moolenaar399d8982019-06-02 15:34:29 +0200322 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100323 call writefile(lines, 'XtestPopupCorners', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200324 let buf = RunVimInTerminal('-S XtestPopupCorners', #{rows: 12})
Bram Moolenaar399d8982019-06-02 15:34:29 +0200325 call VerifyScreenDump(buf, 'Test_popupwin_corners', {})
326
327 " clean up
328 call StopVimInTerminal(buf)
Bram Moolenaar399d8982019-06-02 15:34:29 +0200329endfunc
330
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100331func Test_popup_nospace()
332 CheckScreendump
333
334 let lines =<< trim END
335 call setline(1, repeat([repeat('-', 60)], 15))
336 set so=0
337
338 " cursor in a line in top half, using "botleft" with popup that
339 " does fit
340 normal 5G2|r@
341 let winid1 = popup_create(['one', 'two'], #{
342 \ line: 'cursor-1',
343 \ col: 'cursor',
344 \ pos: 'botleft',
345 \ border: [],
346 \ })
347 " cursor in a line in top half, using "botleft" with popup that
348 " doesn't fit: gets truncated
349 normal 5G9|r#
350 let winid1 = popup_create(['one', 'two', 'tee'], #{
351 \ line: 'cursor-1',
352 \ col: 'cursor',
353 \ pos: 'botleft',
354 \ posinvert: 0,
355 \ border: [],
356 \ })
357 " cursor in a line in top half, using "botleft" with popup that
358 " doesn't fit and 'posinvert' set: flips to below.
359 normal 5G16|r%
360 let winid1 = popup_create(['one', 'two', 'tee'], #{
361 \ line: 'cursor-1',
362 \ col: 'cursor',
363 \ pos: 'botleft',
364 \ border: [],
365 \ })
366 " cursor in a line in bottom half, using "botleft" with popup that
367 " doesn't fit: does not flip.
368 normal 8G23|r*
369 let winid1 = popup_create(['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff'], #{
370 \ line: 'cursor-1',
371 \ col: 'cursor',
372 \ pos: 'botleft',
373 \ border: [],
374 \ })
375
376 " cursor in a line in bottom half, using "topleft" with popup that
377 " does fit
378 normal 8G30|r@
379 let winid1 = popup_create(['one', 'two'], #{
380 \ line: 'cursor+1',
381 \ col: 'cursor',
382 \ pos: 'topleft',
383 \ border: [],
384 \ })
385 " cursor in a line in top half, using "topleft" with popup that
386 " doesn't fit: truncated
387 normal 8G37|r#
388 let winid1 = popup_create(['one', 'two', 'tee'], #{
389 \ line: 'cursor+1',
390 \ col: 'cursor',
391 \ pos: 'topleft',
392 \ posinvert: 0,
393 \ border: [],
394 \ })
395 " cursor in a line in top half, using "topleft" with popup that
Bram Moolenaar5c6b6182019-11-10 17:51:38 +0100396 " doesn't fit and "posinvert" set: flips to above.
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100397 normal 8G44|r%
Bram Moolenaar5c6b6182019-11-10 17:51:38 +0100398 let winid1 = popup_create(['one', 'two', 'tee', 'fou', 'fiv'], #{
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100399 \ line: 'cursor+1',
400 \ col: 'cursor',
401 \ pos: 'topleft',
402 \ border: [],
403 \ })
404 " cursor in a line in top half, using "topleft" with popup that
405 " doesn't fit: does not flip.
406 normal 5G51|r*
407 let winid1 = popup_create(['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff'], #{
408 \ line: 'cursor+1',
409 \ col: 'cursor',
410 \ pos: 'topleft',
411 \ border: [],
412 \ })
413 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100414 call writefile(lines, 'XtestPopupNospace', 'D')
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100415 let buf = RunVimInTerminal('-S XtestPopupNospace', #{rows: 12})
416 call VerifyScreenDump(buf, 'Test_popupwin_nospace', {})
417
418 " clean up
419 call StopVimInTerminal(buf)
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100420endfunc
421
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200422func Test_popup_firstline_dump()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200423 CheckScreendump
424
Bram Moolenaar8d241042019-06-12 23:40:01 +0200425 let lines =<< trim END
426 call setline(1, range(1, 20))
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200427 let winid = popup_create(['1111', '222222', '33333', '44', '5', '666666', '77777', '888', '9999999999999999'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200428 \ maxheight: 4,
429 \ firstline: 3,
Bram Moolenaar8d241042019-06-12 23:40:01 +0200430 \ })
431 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100432 call writefile(lines, 'XtestPopupFirstline', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200433 let buf = RunVimInTerminal('-S XtestPopupFirstline', #{rows: 10})
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200434 call VerifyScreenDump(buf, 'Test_popupwin_firstline_1', {})
435
436 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: -1})\<CR>")
437 call term_sendkeys(buf, ":\<CR>")
438 call VerifyScreenDump(buf, 'Test_popupwin_firstline_2', {})
Bram Moolenaar8d241042019-06-12 23:40:01 +0200439
440 " clean up
441 call StopVimInTerminal(buf)
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200442endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +0200443
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200444func Test_popup_firstline()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200445 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200446 \ maxheight: 2,
447 \ firstline: 3,
Bram Moolenaarae943152019-06-16 22:54:14 +0200448 \ })
449 call assert_equal(3, popup_getoptions(winid).firstline)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200450 call popup_setoptions(winid, #{firstline: 1})
Bram Moolenaarae943152019-06-16 22:54:14 +0200451 call assert_equal(1, popup_getoptions(winid).firstline)
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200452 eval winid->popup_close()
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200453
454 let winid = popup_create(['xxx']->repeat(50), #{
455 \ maxheight: 3,
456 \ firstline: 11,
457 \ })
458 redraw
459 call assert_equal(11, popup_getoptions(winid).firstline)
460 call assert_equal(11, popup_getpos(winid).firstline)
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +0200461 " check line() works with popup window
462 call assert_equal(11, line('.', winid))
463 call assert_equal(50, line('$', winid))
464 call assert_equal(0, line('$', 123456))
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200465
466 " Normal command changes what is displayed but not "firstline"
467 call win_execute(winid, "normal! \<c-y>")
468 call assert_equal(11, popup_getoptions(winid).firstline)
469 call assert_equal(10, popup_getpos(winid).firstline)
470
471 " Making some property change applies "firstline" again
472 call popup_setoptions(winid, #{line: 4})
473 call assert_equal(11, popup_getoptions(winid).firstline)
474 call assert_equal(11, popup_getpos(winid).firstline)
475
476 " Remove "firstline" property and scroll
477 call popup_setoptions(winid, #{firstline: 0})
478 call win_execute(winid, "normal! \<c-y>")
479 call assert_equal(0, popup_getoptions(winid).firstline)
480 call assert_equal(10, popup_getpos(winid).firstline)
481
482 " Making some property change has no side effect
483 call popup_setoptions(winid, #{line: 3})
484 call assert_equal(0, popup_getoptions(winid).firstline)
485 call assert_equal(10, popup_getpos(winid).firstline)
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200486 call popup_close(winid)
Bram Moolenaarae943152019-06-16 22:54:14 +0200487
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100488 " CTRL-D scrolls down half a page
489 let winid = popup_create(['xxx']->repeat(50), #{
490 \ maxheight: 8,
491 \ })
492 redraw
493 call assert_equal(1, popup_getpos(winid).firstline)
494 call win_execute(winid, "normal! \<C-D>")
495 call assert_equal(5, popup_getpos(winid).firstline)
496 call win_execute(winid, "normal! \<C-D>")
497 call assert_equal(9, popup_getpos(winid).firstline)
498 call win_execute(winid, "normal! \<C-U>")
499 call assert_equal(5, popup_getpos(winid).firstline)
500
501 call win_execute(winid, "normal! \<C-F>")
502 call assert_equal(11, popup_getpos(winid).firstline)
503 call win_execute(winid, "normal! \<C-B>")
504 call assert_equal(5, popup_getpos(winid).firstline)
505
Bram Moolenaarae943152019-06-16 22:54:14 +0200506 call popup_close(winid)
LemonBoy0044e512022-04-20 19:47:37 +0100507
508 " Popup with less elements than the maximum height and negative firstline:
509 " check that the popup height is correctly computed.
510 let winid = popup_create(['xxx']->repeat(4), #{
511 \ firstline: -1,
512 \ maxheight: 6,
513 \ })
514
515 let pos = popup_getpos(winid)
516 call assert_equal(3, pos.width)
517 call assert_equal(4, pos.height)
518
519 call popup_close(winid)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200520endfunc
521
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200522func Test_popup_firstline_cursorline()
523 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
524 \ maxheight: 2,
525 \ firstline: 3,
526 \ cursorline: 1,
527 \ })
528 call assert_equal(3, popup_getoptions(winid).firstline)
529 call assert_equal(3, getwininfo(winid)[0].topline)
530 call assert_equal(3, getcurpos(winid)[1])
531
532 call popup_close(winid)
533endfunc
534
Bram Moolenaara112f2d2019-09-01 17:38:09 +0200535func Test_popup_noscrolloff()
536 set scrolloff=5
537 let winid = popup_create(['xxx']->repeat(50), #{
538 \ maxheight: 5,
539 \ firstline: 11,
540 \ })
541 redraw
542 call assert_equal(11, popup_getoptions(winid).firstline)
543 call assert_equal(11, popup_getpos(winid).firstline)
544
545 call popup_setoptions(winid, #{firstline: 0})
546 call win_execute(winid, "normal! \<c-y>")
547 call assert_equal(0, popup_getoptions(winid).firstline)
548 call assert_equal(10, popup_getpos(winid).firstline)
549
550 call popup_close(winid)
551endfunc
552
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200553func Test_popup_drag()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200554 CheckScreendump
555
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200556 " create a popup that covers the command line
557 let lines =<< trim END
558 call setline(1, range(1, 20))
Bram Moolenaar356375f2019-08-24 14:46:29 +0200559 split
560 vsplit
561 $wincmd w
562 vsplit
563 1wincmd w
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200564 let winid = popup_create(['1111', '222222', '33333'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200565 \ drag: 1,
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200566 \ resize: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200567 \ border: [],
568 \ line: &lines - 4,
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200569 \ })
570 func Dragit()
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000571 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
572 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2 - 20)<CR>
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200573 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
574 endfunc
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200575 func Resize()
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000576 map <silent> <F5> :call test_setmouse(6, 21)<CR>
577 map <silent> <F6> :call test_setmouse(7, 25)<CR>
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200578 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
579 endfunc
Bram Moolenaarbfc57862021-11-26 15:57:40 +0000580 func ClickAndDrag()
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000581 map <silent> <F3> :call test_setmouse(5, 2)<CR>
582 map <silent> <F4> :call test_setmouse(3, 14)<CR>
583 map <silent> <F5> :call test_setmouse(3, 18)<CR>
584 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
585 call feedkeys("\<F4>\<LeftMouse>\<F5>\<LeftDrag>\<LeftRelease>", "xt")
Bram Moolenaarbfc57862021-11-26 15:57:40 +0000586 endfunc
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000587 func DragAllStart()
588 call popup_clear()
589 call popup_create('hello', #{line: 3, col: 5, dragall: 1})
590 endfunc
591 func DragAllDrag()
592 map <silent> <F3> :call test_setmouse(3, 5)<CR>
593 map <silent> <F4> :call test_setmouse(5, 36)<CR>
594 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
595 endfunc
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200596 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100597 call writefile(lines, 'XtestPopupDrag', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200598 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200599 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
600
601 call term_sendkeys(buf, ":call Dragit()\<CR>")
602 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
603
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200604 call term_sendkeys(buf, ":call Resize()\<CR>")
605 call VerifyScreenDump(buf, 'Test_popupwin_drag_03', {})
606
Bram Moolenaarbfc57862021-11-26 15:57:40 +0000607 " dragging works after click on a status line
608 call term_sendkeys(buf, ":call ClickAndDrag()\<CR>")
609 call VerifyScreenDump(buf, 'Test_popupwin_drag_04', {})
610
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000611 " dragging without border
612 call term_sendkeys(buf, ":call DragAllStart()\<CR>")
613 call VerifyScreenDump(buf, 'Test_popupwin_drag_05', {})
614 call term_sendkeys(buf, ":call DragAllDrag()\<CR>")
615 call VerifyScreenDump(buf, 'Test_popupwin_drag_06', {})
616
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200617 " clean up
618 call StopVimInTerminal(buf)
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200619endfunc
620
Bram Moolenaareabddc42022-04-02 15:32:16 +0100621func Test_popup_drag_minwidth()
622 CheckScreendump
623
624 " create a popup that does not fit
625 let lines =<< trim END
626 call range(40)
627 \ ->map({_,i -> string(i)})
628 \ ->popup_create({
629 \ 'drag': 1,
630 \ 'wrap': 0,
631 \ 'border': [],
632 \ 'scrollbar': 1,
633 \ 'minwidth': 100,
634 \ 'filter': {w, k -> k ==# 'q' ? len([popup_close(w)]) : 0},
635 \ })
636 func DragitDown()
637 map <silent> <F3> :call test_setmouse(1, 10)<CR>
638 map <silent> <F4> :call test_setmouse(5, 40)<CR>
639 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
640 endfunc
641 func DragitUp()
642 map <silent> <F3> :call test_setmouse(5, 40)<CR>
643 map <silent> <F4> :call test_setmouse(4, 40)<CR>
644 map <silent> <F5> :call test_setmouse(3, 40)<CR>
645 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<F5>\<LeftDrag>\<LeftRelease>", "xt")
646 endfunc
647 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100648 call writefile(lines, 'XtestPopupDrag', 'D')
Bram Moolenaareabddc42022-04-02 15:32:16 +0100649 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
650 call VerifyScreenDump(buf, 'Test_popupwin_drag_minwidth_1', {})
651
652 call term_sendkeys(buf, ":call DragitDown()\<CR>")
653 call VerifyScreenDump(buf, 'Test_popupwin_drag_minwidth_2', {})
654
655 call term_sendkeys(buf, ":call DragitUp()\<CR>")
656 call VerifyScreenDump(buf, 'Test_popupwin_drag_minwidth_3', {})
657
658 call term_sendkeys(buf, 'q')
659
660 " clean up
661 call StopVimInTerminal(buf)
Bram Moolenaareabddc42022-04-02 15:32:16 +0100662endfunc
663
Bram Moolenaar35910f22020-07-12 19:24:10 +0200664func Test_popup_drag_termwin()
665 CheckUnix
666 CheckScreendump
667 CheckFeature terminal
668
669 " create a popup that covers the terminal window
670 let lines =<< trim END
Bram Moolenaar452143c2020-07-15 17:38:21 +0200671 set foldmethod=marker
672 call setline(1, range(100))
673 for nr in range(7)
674 call setline(nr * 12 + 1, "fold {{{")
Bram Moolenaar9d489562020-07-30 20:08:50 +0200675 call setline(nr * 12 + 11, "end }}}")
Bram Moolenaar452143c2020-07-15 17:38:21 +0200676 endfor
677 %foldclose
Bram Moolenaar35910f22020-07-12 19:24:10 +0200678 set shell=/bin/sh noruler
Bram Moolenaar4b2ce122020-11-21 21:41:41 +0100679 unlet $PROMPT_COMMAND
Bram Moolenaara4dc6f92020-07-12 19:52:36 +0200680 let $PS1 = 'vim> '
Bram Moolenaar452143c2020-07-15 17:38:21 +0200681 terminal ++rows=4
Bram Moolenaar35910f22020-07-12 19:24:10 +0200682 $wincmd w
683 let winid = popup_create(['1111', '2222'], #{
684 \ drag: 1,
685 \ resize: 1,
686 \ border: [],
687 \ line: 3,
688 \ })
Bram Moolenaar452143c2020-07-15 17:38:21 +0200689 func DragitLeft()
Bram Moolenaar35910f22020-07-12 19:24:10 +0200690 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
691 endfunc
Bram Moolenaar452143c2020-07-15 17:38:21 +0200692 func DragitDown()
693 call feedkeys("\<F4>\<LeftMouse>\<F5>\<LeftDrag>\<LeftRelease>", "xt")
694 endfunc
695 func DragitDownLeft()
696 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
697 endfunc
Bram Moolenaar35910f22020-07-12 19:24:10 +0200698 map <silent> <F3> :call test_setmouse(3, &columns / 2)<CR>
699 map <silent> <F4> :call test_setmouse(3, &columns / 2 - 20)<CR>
Bram Moolenaar452143c2020-07-15 17:38:21 +0200700 map <silent> <F5> :call test_setmouse(12, &columns / 2)<CR>
701 map <silent> <F6> :call test_setmouse(12, &columns / 2 - 20)<CR>
Bram Moolenaar35910f22020-07-12 19:24:10 +0200702 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100703 call writefile(lines, 'XtestPopupTerm', 'D')
Bram Moolenaar452143c2020-07-15 17:38:21 +0200704 let buf = RunVimInTerminal('-S XtestPopupTerm', #{rows: 16})
Bram Moolenaar35910f22020-07-12 19:24:10 +0200705 call VerifyScreenDump(buf, 'Test_popupwin_term_01', {})
706
Bram Moolenaar452143c2020-07-15 17:38:21 +0200707 call term_sendkeys(buf, ":call DragitLeft()\<CR>")
Bram Moolenaar35910f22020-07-12 19:24:10 +0200708 call VerifyScreenDump(buf, 'Test_popupwin_term_02', {})
709
Bram Moolenaar452143c2020-07-15 17:38:21 +0200710 call term_sendkeys(buf, ":call DragitDown()\<CR>")
711 call VerifyScreenDump(buf, 'Test_popupwin_term_03', {})
712
713 call term_sendkeys(buf, ":call DragitDownLeft()\<CR>")
714 call VerifyScreenDump(buf, 'Test_popupwin_term_04', {})
715
Bram Moolenaar35910f22020-07-12 19:24:10 +0200716 " clean up
717 call StopVimInTerminal(buf)
Bram Moolenaar35910f22020-07-12 19:24:10 +0200718endfunc
719
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200720func Test_popup_close_with_mouse()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200721 CheckScreendump
722
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200723 let lines =<< trim END
724 call setline(1, range(1, 20))
725 " With border, can click on X
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200726 let winid = popup_create('foobar', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200727 \ close: 'button',
728 \ border: [],
729 \ line: 1,
730 \ col: 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200731 \ })
732 func CloseMsg(id, result)
733 echomsg 'Popup closed with ' .. a:result
734 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200735 let winid = popup_create('notification', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200736 \ close: 'click',
737 \ line: 3,
738 \ col: 15,
739 \ callback: 'CloseMsg',
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200740 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200741 let winid = popup_create('no border here', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200742 \ close: 'button',
743 \ line: 5,
744 \ col: 3,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200745 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200746 let winid = popup_create('only padding', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200747 \ close: 'button',
748 \ padding: [],
749 \ line: 5,
750 \ col: 23,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200751 \ })
752 func CloseWithX()
753 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
754 endfunc
755 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
756 func CloseWithClick()
757 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
758 endfunc
759 map <silent> <F4> :call test_setmouse(3, 17)<CR>
Bram Moolenaarf6396232019-08-24 19:36:00 +0200760 func CreateWithMenuFilter()
761 let winid = popup_create('barfoo', #{
762 \ close: 'button',
763 \ filter: 'popup_filter_menu',
764 \ border: [],
765 \ line: 1,
766 \ col: 40,
767 \ })
768 endfunc
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200769 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100770 call writefile(lines, 'XtestPopupClose', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200771 let buf = RunVimInTerminal('-S XtestPopupClose', #{rows: 10})
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200772 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
773
774 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
775 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
776
777 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
778 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
779
Bram Moolenaarf6396232019-08-24 19:36:00 +0200780 call term_sendkeys(buf, ":call CreateWithMenuFilter()\<CR>")
781 call VerifyScreenDump(buf, 'Test_popupwin_close_04', {})
782
783 " We have to send the actual mouse code, feedkeys() would be caught the
784 " filter.
785 call term_sendkeys(buf, "\<Esc>[<0;47;1M")
786 call VerifyScreenDump(buf, 'Test_popupwin_close_05', {})
787
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200788 " clean up
789 call StopVimInTerminal(buf)
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200790endfunction
791
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200792func Test_popup_menu_wrap()
793 CheckScreendump
794
795 let lines =<< trim END
796 call setline(1, range(1, 20))
797 call popup_create([
798 \ 'one',
799 \ 'asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfas',
800 \ 'three',
801 \ 'four',
802 \ ], #{
803 \ pos: "botleft",
804 \ border: [],
805 \ padding: [0,1,0,1],
806 \ maxheight: 3,
807 \ cursorline: 1,
808 \ filter: 'popup_filter_menu',
809 \ })
810 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100811 call writefile(lines, 'XtestPopupWrap', 'D')
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200812 let buf = RunVimInTerminal('-S XtestPopupWrap', #{rows: 10})
813 call VerifyScreenDump(buf, 'Test_popupwin_wrap_1', {})
814
815 call term_sendkeys(buf, "jj")
816 call VerifyScreenDump(buf, 'Test_popupwin_wrap_2', {})
817
818 " clean up
819 call term_sendkeys(buf, "\<Esc>")
820 call StopVimInTerminal(buf)
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200821endfunction
822
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200823func Test_popup_with_mask()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200824 CheckScreendump
825
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200826 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200827 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200828 hi PopupColor ctermbg=lightgrey
829 let winid = popup_create([
830 \ 'some text',
831 \ 'another line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200832 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200833 \ line: 1,
834 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100835 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200836 \ wrap: 0,
837 \ fixed: 1,
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +0200838 \ scrollbar: v:false,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200839 \ zindex: 90,
840 \ padding: [],
841 \ highlight: 'PopupColor',
842 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200843 call popup_create([
844 \ 'xxxxxxxxx',
845 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200846 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200847 \ line: 3,
848 \ col: 18,
849 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200850 let winidb = popup_create([
851 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200852 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200853 \ line: 7,
854 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100855 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200856 \ wrap: 0,
857 \ fixed: 1,
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +0200858 \ scrollbar: v:false,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200859 \ close: 'button',
860 \ zindex: 90,
861 \ padding: [],
862 \ border: [],
863 \ mask: [[1,2,1,1], [-5,-1,4,4], [7,9,2,3], [3,5,5,5],[-7,-4,5,5]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200864 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100865 call writefile(lines, 'XtestPopupMask', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200866 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200867 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
868
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200869 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
870 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200871 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200872 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
873
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200874 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
875 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200876 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200877 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
878
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200879 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
880 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200881 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200882 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
883
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200884 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
885 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200886 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200887 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
888
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200889 " clean up
890 call StopVimInTerminal(buf)
Bram Moolenaar4012d262020-12-29 11:57:46 +0100891
892 " this was causing a crash
893 call popup_create('test', #{mask: [[0, 0, 0, 0]]})
894 call popup_clear()
Bram Moolenaar10ccfb22021-02-13 21:31:18 +0100895
896 " this was causing an internal error
897 enew
898 set nowrap
899 call repeat('x', &columns)->setline(1)
900 call prop_type_add('textprop', {})
901 call prop_add(1, 1, #{length: &columns, type: 'textprop'})
902 vsplit
903 let opts = popup_create('', #{textprop: 'textprop'})
904 \ ->popup_getoptions()
905 \ ->extend(#{mask: [[1, 1, 1, 1]]})
906 call popup_create('', opts)
907 redraw
908
909 close!
910 bwipe!
911 call prop_type_delete('textprop')
912 call popup_clear()
913 set wrap&
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200914endfunc
915
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200916func Test_popup_select()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200917 CheckScreendump
918 CheckFeature clipboard_working
919
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200920 " create a popup with some text to be selected
921 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200922 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200923 call setline(1, range(1, 20))
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200924 let winid = popup_create(['the word', 'some more', 'several words here', 'invisible', '5', '6', '7'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200925 \ drag: 1,
926 \ border: [],
927 \ line: 3,
928 \ col: 10,
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200929 \ maxheight: 3,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200930 \ })
931 func Select1()
932 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
933 endfunc
934 map <silent> <F3> :call test_setmouse(4, 15)<CR>
935 map <silent> <F4> :call test_setmouse(6, 23)<CR>
936 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100937 call writefile(lines, 'XtestPopupSelect', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200938 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200939 call term_sendkeys(buf, ":call Select1()\<CR>")
940 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
941
942 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
943 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200944 " clean the command line, sometimes it still shows a command
945 call term_sendkeys(buf, ":\<esc>")
946
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200947 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
948
949 " clean up
950 call StopVimInTerminal(buf)
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200951endfunc
952
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200953func Test_popup_in_tab()
954 " default popup is local to tab, not visible when in other tab
955 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200956 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200957 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200958 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200959 tabnew
960 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200961 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200962 quit
963 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200964
965 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200966 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200967 " buffer is gone now
968 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200969
970 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200971 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200972 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200973 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200974 tabnew
975 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200976 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200977 quit
978 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200979 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200980
981 " create popup in other tab
982 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200983 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200984 call assert_equal(0, popup_getpos(winid).visible)
985 call assert_equal(1, popup_getoptions(winid).tabpage)
986 quit
987 call assert_equal(1, popup_getpos(winid).visible)
988 call assert_equal(0, popup_getoptions(winid).tabpage)
989 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200990endfunc
991
992func Test_popup_valid_arguments()
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200993 call assert_equal(0, len(popup_list()))
994
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200995 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200996 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200997 let pos = popup_getpos(winid)
998 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200999 call assert_equal([winid], popup_list())
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001000 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001001
1002 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001003 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001004 let pos = popup_getpos(winid)
1005 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001006 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001007
1008 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001009 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001010 let pos = popup_getpos(winid)
1011 let around = (&columns - pos.width) / 2
1012 call assert_inrange(around - 1, around + 1, pos.col)
1013 let around = (&lines - pos.height) / 2
1014 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001015 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001016endfunc
1017
1018func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001019 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001020 call popup_clear()
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001021 call assert_fails('call popup_create("text", "none")', 'E1206:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001022 call popup_clear()
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001023 call assert_fails('call popup_create(test_null_string(), {})', 'E450:')
1024 call assert_fails('call popup_create(test_null_list(), {})', 'E450:')
1025 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001026
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001027 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001028 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001029 call assert_fails('call popup_create("text", #{col: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001030 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001031 call assert_fails('call popup_create("text", #{col: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001032 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001033 call assert_fails('call popup_create("text", #{col: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001034 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001035
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001036 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001037 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001038 call assert_fails('call popup_create("text", #{line: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001039 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001040 call assert_fails('call popup_create("text", #{line: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001041 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001042 call assert_fails('call popup_create("text", #{line: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001043 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001044
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001045 call assert_fails('call popup_create("text", #{pos: "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001046 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001047 call assert_fails('call popup_create("text", #{padding: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001048 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001049 call assert_fails('call popup_create("text", #{border: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001050 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001051 call assert_fails('call popup_create("text", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001052 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +02001053 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
1054 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001055 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001056 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001057
Bram Moolenaar83bd7a92022-05-29 17:13:24 +01001058 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E1284: Argument 1, list item 2: Dictionary required')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001059 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001060 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001061 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001062 call assert_fails('call popup_create([#{text: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001063 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +01001064 call assert_fails('call popup_create([#{text: "text", props: range(3)}], {})', 'E715:')
1065 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +02001066 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
1067 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +01001068 call assert_fails('call popup_create("text", #{mask: range(5)})', 'E475:')
1069 call popup_clear()
1070 call popup_create("text", #{mask: [range(4)]})
1071 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +02001072 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001073 call assert_fails('call popup_create("text", #{mapping: []})', 'E745:')
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +02001074 call popup_clear()
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001075 call assert_fails('call popup_create("text", #{tabpage : 4})', 'E997:')
1076 call popup_clear()
Bram Moolenaar83bd7a92022-05-29 17:13:24 +01001077
1078 call assert_fails('call popup_create(range(10), {})', 'E1024:')
1079 call popup_clear()
1080 call assert_fails('call popup_create([1, 2], {})', 'E1284: Argument 1, list item 1: Dictionary required')
1081 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001082endfunc
1083
Bram Moolenaareea16992019-05-31 17:34:48 +02001084func Test_win_execute_closing_curwin()
1085 split
1086 let winid = popup_create('some text', {})
Bram Moolenaare2e40752020-09-04 21:18:46 +02001087 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001088 call popup_clear()
Bram Moolenaar49540192019-12-11 19:34:54 +01001089
1090 let winid = popup_create('some text', {})
Bram Moolenaare2e40752020-09-04 21:18:46 +02001091 call assert_fails('call win_execute(winid, printf("normal! :\<C-u>call popup_close(%d)\<CR>", winid))', 'E994:')
Bram Moolenaar49540192019-12-11 19:34:54 +01001092 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001093endfunc
1094
1095func Test_win_execute_not_allowed()
1096 let winid = popup_create('some text', {})
1097 call assert_fails('call win_execute(winid, "split")', 'E994:')
1098 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
1099 call assert_fails('call win_execute(winid, "close")', 'E994:')
1100 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +02001101 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001102 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
1103 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
1104 call assert_fails('call win_execute(winid, "next")', 'E994:')
1105 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01001106 call assert_fails('call win_execute(winid, "pedit filename")', 'E994:')
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01001107 call assert_fails('call win_execute(winid, "pbuffer filename")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001108 call assert_fails('call win_execute(winid, "buf")', 'E994:')
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01001109 call assert_fails('call win_execute(winid, "bnext")', 'E994:')
1110 call assert_fails('call win_execute(winid, "bprev")', 'E994:')
1111 call assert_fails('call win_execute(winid, "bfirst")', 'E994:')
1112 call assert_fails('call win_execute(winid, "blast")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001113 call assert_fails('call win_execute(winid, "edit")', 'E994:')
1114 call assert_fails('call win_execute(winid, "enew")', 'E994:')
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001115 call assert_fails('call win_execute(winid, "help")', 'E994:')
1116 call assert_fails('call win_execute(winid, "1only")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001117 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
1118 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
1119 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
1120 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001121 call popup_clear()
Christian Brabandt8e831052025-06-18 18:33:31 +02001122 bw filename
Bram Moolenaareea16992019-05-31 17:34:48 +02001123endfunc
1124
Bram Moolenaar402502d2019-05-30 22:07:36 +02001125func Test_popup_with_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001126 CheckScreendump
1127
Bram Moolenaar402502d2019-05-30 22:07:36 +02001128 let lines =<< trim END
1129 call setline(1, range(1, 100))
1130 let winid = popup_create(
1131 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001132 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001133 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001134 call writefile(lines, 'XtestPopup', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001135 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001136 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
1137
1138 " clean up
1139 call StopVimInTerminal(buf)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001140endfunc
1141
1142func Test_popup_without_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001143 CheckScreendump
1144
Bram Moolenaar402502d2019-05-30 22:07:36 +02001145 let lines =<< trim END
1146 call setline(1, range(1, 100))
1147 let winid = popup_create(
1148 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001149 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001150 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001151 call writefile(lines, 'XtestPopup', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001152 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001153 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
1154
1155 " clean up
1156 call StopVimInTerminal(buf)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001157endfunc
1158
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001159func Test_popup_with_showbreak()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001160 CheckScreendump
1161
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001162 let lines =<< trim END
1163 set showbreak=>>\
1164 call setline(1, range(1, 20))
1165 let winid = popup_dialog(
Bram Moolenaar8ae54372019-09-15 18:11:16 +02001166 \ 'a long line here that wraps',
1167 \ #{filter: 'popup_filter_yesno',
1168 \ maxwidth: 12})
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001169 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001170 call writefile(lines, 'XtestPopupShowbreak', 'D')
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001171 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
1172 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
1173
1174 " clean up
1175 call term_sendkeys(buf, "y")
1176 call StopVimInTerminal(buf)
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001177endfunc
1178
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001179func Test_popup_time()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001180 CheckFeature timers
1181
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001182 topleft vnew
1183 call setline(1, 'hello')
1184
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001185 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001186 \ line: 1,
1187 \ col: 1,
1188 \ minwidth: 20,
1189 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001190 \})
1191 redraw
1192 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1193 call assert_equal('world', line)
1194
Bram Moolenaarb4f06282019-07-12 21:07:54 +02001195 call assert_equal(winid, popup_locate(1, 1))
1196 call assert_equal(winid, popup_locate(1, 20))
1197 call assert_equal(0, popup_locate(1, 21))
1198 call assert_equal(0, popup_locate(2, 1))
1199
Bram Moolenaar9c8d12c2022-12-02 15:06:07 +00001200 " Mac is usually a bit slow
1201 let delay = has('mac') ? '900m' : '700m'
1202 exe 'sleep ' .. delay
Bram Moolenaar35d5af62019-05-26 20:44:10 +02001203 redraw
Bram Moolenaar9c8d12c2022-12-02 15:06:07 +00001204
Bram Moolenaar196b4662019-09-06 21:34:30 +02001205 let line = join(map(range(1, 5), '1->screenstring(v:val)'), '')
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001206 call assert_equal('hello', line)
1207
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001208 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001209 \ line: &lines,
1210 \ col: 10,
1211 \ minwidth: 20,
1212 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001213 \})
1214 redraw
1215 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1216 call assert_match('.*on the command line.*', line)
1217
Bram Moolenaar9c8d12c2022-12-02 15:06:07 +00001218 exe 'sleep ' .. delay
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001219 redraw
1220 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1221 call assert_notmatch('.*on the command line.*', line)
1222
1223 bwipe!
1224endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001225
1226func Test_popup_hide()
1227 topleft vnew
1228 call setline(1, 'hello')
1229
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001230 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001231 \ line: 1,
1232 \ col: 1,
1233 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001234 \})
1235 redraw
1236 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1237 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001238 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001239 " buffer is still listed and active
1240 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001241
1242 call popup_hide(winid)
1243 redraw
1244 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1245 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001246 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001247 " buffer is still listed but hidden
Bram Moolenaarca7c0782020-01-14 20:42:48 +01001248 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001249
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001250 eval winid->popup_show()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001251 redraw
1252 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1253 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001254 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001255
1256
1257 call popup_close(winid)
1258 redraw
1259 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1260 call assert_equal('hello', line)
1261
1262 " error is given for existing non-popup window
1263 call assert_fails('call popup_hide(win_getid())', 'E993:')
1264
1265 " no error non-existing window
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001266 eval 1234234->popup_hide()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001267 call popup_show(41234234)
1268
1269 bwipe!
1270endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001271
1272func Test_popup_move()
1273 topleft vnew
1274 call setline(1, 'hello')
1275
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001276 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001277 \ line: 1,
1278 \ col: 1,
1279 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001280 \})
1281 redraw
1282 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1283 call assert_equal('world ', line)
1284
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001285 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001286 redraw
1287 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1288 call assert_equal('hello ', line)
1289 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
1290 call assert_equal('~world', line)
1291
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001292 eval winid->popup_move(#{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001293 redraw
1294 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1295 call assert_equal('hworld', line)
1296
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001297 call assert_fails('call popup_move(winid, [])', 'E1206:')
1298 call assert_fails('call popup_move(winid, test_null_dict())', 'E1297:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001299
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001300 call popup_close(winid)
1301
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001302 call assert_equal(0, popup_move(-1, {}))
1303
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001304 bwipe!
1305endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +02001306
Bram Moolenaar402502d2019-05-30 22:07:36 +02001307func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001308 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001309 \ line: 2,
1310 \ col: 3,
1311 \ minwidth: 10,
1312 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +02001313 \})
1314 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001315 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001316 call assert_equal(2, res.line)
1317 call assert_equal(3, res.col)
1318 call assert_equal(10, res.width)
1319 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001320 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001321
1322 call popup_close(winid)
1323endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001324
1325func Test_popup_width_longest()
1326 let tests = [
1327 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
1328 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
1329 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
1330 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
1331 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
1332 \ ]
1333
1334 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001335 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001336 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001337 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001338 call assert_equal(test[1], position.width)
1339 call popup_close(winid)
1340 endfor
1341endfunc
1342
1343func Test_popup_wraps()
1344 let tests = [
1345 \ ['nowrap', 6, 1],
1346 \ ['a line that wraps once', 12, 2],
1347 \ ['a line that wraps two times', 12, 3],
1348 \ ]
1349 for test in tests
1350 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001351 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001352 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001353 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001354 call assert_equal(test[1], position.width)
1355 call assert_equal(test[2], position.height)
1356
1357 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001358 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001359 endfor
1360endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001361
1362func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001363 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001364 \ line: 2,
1365 \ col: 3,
1366 \ minwidth: 10,
1367 \ minheight: 11,
1368 \ maxwidth: 20,
1369 \ maxheight: 21,
1370 \ zindex: 100,
1371 \ time: 5000,
1372 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001373 \})
1374 redraw
1375 let res = popup_getoptions(winid)
1376 call assert_equal(2, res.line)
1377 call assert_equal(3, res.col)
1378 call assert_equal(10, res.minwidth)
1379 call assert_equal(11, res.minheight)
1380 call assert_equal(20, res.maxwidth)
1381 call assert_equal(21, res.maxheight)
1382 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001383 call assert_equal(1, res.fixed)
Bram Moolenaarb8350ab2019-08-04 17:59:49 +02001384 call assert_equal(1, res.mapping)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001385 if has('timers')
1386 call assert_equal(5000, res.time)
1387 endif
1388 call popup_close(winid)
1389
1390 let winid = popup_create('hello', {})
1391 redraw
1392 let res = popup_getoptions(winid)
1393 call assert_equal(0, res.line)
1394 call assert_equal(0, res.col)
1395 call assert_equal(0, res.minwidth)
1396 call assert_equal(0, res.minheight)
1397 call assert_equal(0, res.maxwidth)
1398 call assert_equal(0, res.maxheight)
1399 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001400 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001401 if has('timers')
1402 call assert_equal(0, res.time)
1403 endif
1404 call popup_close(winid)
1405 call assert_equal({}, popup_getoptions(winid))
1406endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001407
1408func Test_popup_option_values()
1409 new
1410 " window-local
1411 setlocal number
1412 setlocal nowrap
1413 " buffer-local
1414 setlocal omnifunc=Something
1415 " global/buffer-local
1416 setlocal path=/there
1417 " global/window-local
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001418 setlocal statusline=2
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001419
1420 let winid = popup_create('hello', {})
1421 call assert_equal(0, getwinvar(winid, '&number'))
1422 call assert_equal(1, getwinvar(winid, '&wrap'))
1423 call assert_equal('', getwinvar(winid, '&omnifunc'))
1424 call assert_equal(&g:path, getwinvar(winid, '&path'))
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001425 call assert_equal(&g:statusline, getwinvar(winid, '&statusline'))
1426
1427 " 'scrolloff' is reset to zero
1428 call assert_equal(5, &scrolloff)
1429 call assert_equal(0, getwinvar(winid, '&scrolloff'))
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001430
1431 call popup_close(winid)
1432 bwipe
1433endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001434
1435func Test_popup_atcursor()
1436 topleft vnew
1437 call setline(1, [
1438 \ 'xxxxxxxxxxxxxxxxx',
1439 \ 'xxxxxxxxxxxxxxxxx',
1440 \ 'xxxxxxxxxxxxxxxxx',
1441 \])
1442
1443 call cursor(2, 2)
1444 redraw
1445 let winid = popup_atcursor('vim', {})
1446 redraw
1447 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1448 call assert_equal('xvimxxxxxxxxxxxxx', line)
1449 call popup_close(winid)
1450
1451 call cursor(3, 4)
1452 redraw
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001453 let winid = 'vim'->popup_atcursor({})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001454 redraw
1455 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
1456 call assert_equal('xxxvimxxxxxxxxxxx', line)
1457 call popup_close(winid)
1458
1459 call cursor(1, 1)
1460 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001461 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001462 \ line: 'cursor+2',
1463 \ col: 'cursor+1',
1464 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001465 redraw
1466 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1467 call assert_equal('xvimxxxxxxxxxxxxx', line)
1468 call popup_close(winid)
1469
1470 call cursor(3, 3)
1471 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001472 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001473 \ line: 'cursor-2',
1474 \ col: 'cursor-1',
1475 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001476 redraw
1477 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1478 call assert_equal('xvimxxxxxxxxxxxxx', line)
1479 call popup_close(winid)
1480
Bram Moolenaar402502d2019-05-30 22:07:36 +02001481 " just enough room above
1482 call cursor(3, 3)
1483 redraw
1484 let winid = popup_atcursor(['vim', 'is great'], {})
1485 redraw
1486 let pos = popup_getpos(winid)
1487 call assert_equal(1, pos.line)
1488 call popup_close(winid)
1489
1490 " not enough room above, popup goes below the cursor
1491 call cursor(3, 3)
1492 redraw
1493 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1494 redraw
1495 let pos = popup_getpos(winid)
1496 call assert_equal(4, pos.line)
1497 call popup_close(winid)
1498
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001499 " cursor in first line, popup in line 2
1500 call cursor(1, 1)
1501 redraw
1502 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1503 redraw
1504 let pos = popup_getpos(winid)
1505 call assert_equal(2, pos.line)
1506 call popup_close(winid)
1507
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001508 bwipe!
1509endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001510
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001511func Test_popup_atcursor_pos()
1512 CheckScreendump
Dominique Pellec60e9592021-07-03 21:41:38 +02001513 CheckFeature conceal
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001514
1515 let lines =<< trim END
1516 call setline(1, repeat([repeat('-', 60)], 15))
1517 set so=0
1518
1519 normal 9G3|r#
1520 let winid1 = popup_atcursor(['first', 'second'], #{
1521 \ moved: [0, 0, 0],
1522 \ })
1523 normal 9G21|r&
1524 let winid1 = popup_atcursor(['FIrsT', 'SEcoND'], #{
1525 \ pos: 'botright',
1526 \ moved: [0, 0, 0],
1527 \ })
1528 normal 3G27|r%
1529 let winid1 = popup_atcursor(['fiRSt', 'seCOnd'], #{
1530 \ pos: 'topleft',
1531 \ moved: [0, 0, 0],
1532 \ })
1533 normal 3G45|r@
1534 let winid1 = popup_atcursor(['First', 'SeconD'], #{
1535 \ pos: 'topright',
Bram Moolenaarb0992022020-01-30 14:55:42 +01001536 \ moved: range(3),
1537 \ mousemoved: range(3),
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001538 \ })
Bram Moolenaarea042672021-06-29 20:22:32 +02001539
1540 normal 9G27|Rconcealed X
1541 syn match Hidden /concealed/ conceal
1542 set conceallevel=2 concealcursor=n
1543 redraw
1544 normal 0fX
1545 call popup_atcursor('mark', {})
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001546 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001547 call writefile(lines, 'XtestPopupAtcursorPos', 'D')
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001548 let buf = RunVimInTerminal('-S XtestPopupAtcursorPos', #{rows: 12})
1549 call VerifyScreenDump(buf, 'Test_popupwin_atcursor_pos', {})
1550
1551 " clean up
1552 call StopVimInTerminal(buf)
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001553endfunc
1554
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001555func Test_popup_beval()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001556 CheckScreendump
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001557 CheckFeature balloon_eval_term
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001558
1559 let lines =<< trim END
1560 call setline(1, range(1, 20))
1561 call setline(5, 'here is some text to hover over')
1562 set balloonevalterm
1563 set balloonexpr=BalloonExpr()
1564 set balloondelay=100
1565 func BalloonExpr()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001566 let s:winid = [v:beval_text]->popup_beval({})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001567 return ''
1568 endfunc
1569 func Hover()
1570 call test_setmouse(5, 15)
1571 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1572 sleep 100m
1573 endfunc
1574 func MoveOntoPopup()
1575 call test_setmouse(4, 17)
1576 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1577 endfunc
1578 func MoveAway()
1579 call test_setmouse(5, 13)
1580 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1581 endfunc
1582 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001583 call writefile(lines, 'XtestPopupBeval', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001584 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001585 call TermWait(buf, 50)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001586 call term_sendkeys(buf, 'j')
1587 call term_sendkeys(buf, ":call Hover()\<CR>")
1588 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1589
1590 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1591 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1592
1593 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1594 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1595
1596 " clean up
1597 call StopVimInTerminal(buf)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001598endfunc
1599
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001600func Test_popup_filter()
1601 new
1602 call setline(1, 'some text')
1603
1604 func MyPopupFilter(winid, c)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001605 if a:c == 'e' || a:c == "\<F9>"
1606 let g:eaten = a:c
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001607 return 1
1608 endif
1609 if a:c == '0'
1610 let g:ignored = '0'
1611 return 0
1612 endif
1613 if a:c == 'x'
1614 call popup_close(a:winid)
1615 return 1
1616 endif
1617 return 0
1618 endfunc
1619
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001620 let winid = 'something'->popup_create(#{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001621 redraw
1622
1623 " e is consumed by the filter
Bram Moolenaar999db232021-07-04 14:00:55 +02001624 let g:eaten = ''
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001625 call feedkeys('e', 'xt')
1626 call assert_equal('e', g:eaten)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001627 call feedkeys("\<F9>", 'xt')
1628 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001629
1630 " 0 is ignored by the filter
Bram Moolenaar999db232021-07-04 14:00:55 +02001631 let g:ignored = ''
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001632 normal $
1633 call assert_equal(9, getcurpos()[2])
1634 call feedkeys('0', 'xt')
1635 call assert_equal('0', g:ignored)
Bram Moolenaar999db232021-07-04 14:00:55 +02001636
1637 if has('win32') && has('gui_running')
1638 echo "FIXME: this check is very flaky on MS-Windows GUI, the cursor doesn't move"
1639 else
1640 call assert_equal(1, getcurpos()[2])
1641 endif
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001642
1643 " x closes the popup
1644 call feedkeys('x', 'xt')
Bram Moolenaarec084d32020-02-28 22:44:47 +01001645 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001646 call assert_equal(-1, winbufnr(winid))
1647
Bram Moolenaar999db232021-07-04 14:00:55 +02001648 unlet g:eaten
1649 unlet g:ignored
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001650 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001651 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001652endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001653
Bram Moolenaar4934ad02020-09-28 22:29:58 +02001654" this tests that the filter is not used for :normal command
Bram Moolenaar189832b2020-09-23 12:29:11 +02001655func Test_popup_filter_normal_cmd()
1656 CheckScreendump
1657
1658 let lines =<< trim END
Bram Moolenaar4934ad02020-09-28 22:29:58 +02001659 let text = range(1, 20)->map({_, v -> string(v)})
1660 let g:winid = popup_create(text, #{maxheight: 5, minwidth: 3, filter: 'invalidfilter'})
1661 call timer_start(0, {-> win_execute(g:winid, 'norm! 10Gzz')})
Bram Moolenaar189832b2020-09-23 12:29:11 +02001662 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001663 call writefile(lines, 'XtestPopupNormal', 'D')
Bram Moolenaar189832b2020-09-23 12:29:11 +02001664 let buf = RunVimInTerminal('-S XtestPopupNormal', #{rows: 10})
1665 call TermWait(buf, 100)
1666 call VerifyScreenDump(buf, 'Test_popupwin_normal_cmd', {})
1667
1668 call StopVimInTerminal(buf)
Bram Moolenaar189832b2020-09-23 12:29:11 +02001669endfunc
1670
Bram Moolenaar10476522020-09-24 22:57:31 +02001671" test that cursor line highlight is updated after using win_execute()
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001672func Test_popup_filter_win_execute()
1673 CheckScreendump
1674
1675 let lines =<< trim END
Bram Moolenaar10476522020-09-24 22:57:31 +02001676 let lines = range(1, &lines * 2)->map({_, v -> string(v)})
1677 let g:id = popup_create(lines, #{
1678 \ minheight: &lines - 5,
1679 \ maxheight: &lines - 5,
1680 \ cursorline: 1,
1681 \ })
1682 redraw
1683 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001684 call writefile(lines, 'XtestPopupWinExecute', 'D')
Bram Moolenaar10476522020-09-24 22:57:31 +02001685 let buf = RunVimInTerminal('-S XtestPopupWinExecute', #{rows: 14})
1686
1687 call term_sendkeys(buf, ":call win_execute(g:id, ['normal 17Gzz'])\<CR>")
1688 call term_sendkeys(buf, ":\<CR>")
1689
1690 call VerifyScreenDump(buf, 'Test_popupwin_win_execute_cursorline', {})
1691
1692 call StopVimInTerminal(buf)
Bram Moolenaar10476522020-09-24 22:57:31 +02001693endfunc
1694
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001695func Test_popup_set_firstline()
1696 CheckScreendump
1697
1698 let lines =<< trim END
1699 let lines = range(1, 50)->map({_, v -> string(v)})
1700 let g:id = popup_create(lines, #{
1701 \ minwidth: 20,
1702 \ maxwidth: 20,
1703 \ minheight: &lines - 5,
1704 \ maxheight: &lines - 5,
1705 \ cursorline: 1,
1706 \ })
1707 call popup_setoptions(g:id, #{firstline: 10})
1708 redraw
1709 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001710 call writefile(lines, 'XtestPopupWinSetFirstline', 'D')
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001711 let buf = RunVimInTerminal('-S XtestPopupWinSetFirstline', #{rows: 16})
1712
1713 call VerifyScreenDump(buf, 'Test_popupwin_set_firstline_1', {})
1714
1715 call term_sendkeys(buf, ":call popup_setoptions(g:id, #{firstline: 5})\<CR>")
1716 call term_sendkeys(buf, ":\<CR>")
1717 call VerifyScreenDump(buf, 'Test_popupwin_set_firstline_2', {})
1718
1719 call StopVimInTerminal(buf)
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001720endfunc
1721
Bram Moolenaar10476522020-09-24 22:57:31 +02001722" this tests that we don't get stuck with an error in "win_execute()"
1723func Test_popup_filter_win_execute_error()
1724 CheckScreendump
1725
1726 let lines =<< trim END
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001727 let g:winid = popup_create('some text', {'filter': 'invalidfilter'})
1728 call timer_start(0, {-> win_execute(g:winid, 'invalidCommand')})
1729 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001730 call writefile(lines, 'XtestPopupWinExecuteError', 'D')
Bram Moolenaar10476522020-09-24 22:57:31 +02001731 let buf = RunVimInTerminal('-S XtestPopupWinExecuteError', #{rows: 10, wait_for_ruler: 0})
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001732
1733 call WaitFor({-> term_getline(buf, 9) =~ 'Not an editor command: invalidCommand'})
1734 call term_sendkeys(buf, "\<CR>")
1735 call WaitFor({-> term_getline(buf, 9) =~ 'Unknown function: invalidfilter'})
1736 call term_sendkeys(buf, "\<CR>")
1737 call WaitFor({-> term_getline(buf, 9) =~ 'Not allowed in a popup window'})
1738 call term_sendkeys(buf, "\<CR>")
1739 call term_sendkeys(buf, "\<CR>")
1740 call VerifyScreenDump(buf, 'Test_popupwin_win_execute', {})
1741
1742 call StopVimInTerminal(buf)
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001743endfunc
1744
Bram Moolenaara42d9452019-06-15 21:46:30 +02001745func ShowDialog(key, result)
1746 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001747 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001748 \ filter: 'popup_filter_yesno',
1749 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001750 \ })
1751 redraw
1752 call feedkeys(a:key, "xt")
1753 call assert_equal(winid, s:cb_winid)
1754 call assert_equal(a:result, s:cb_res)
1755endfunc
1756
1757func Test_popup_dialog()
1758 func QuitCallback(id, res)
1759 let s:cb_winid = a:id
1760 let s:cb_res = a:res
1761 endfunc
1762
1763 let winid = ShowDialog("y", 1)
1764 let winid = ShowDialog("Y", 1)
1765 let winid = ShowDialog("n", 0)
1766 let winid = ShowDialog("N", 0)
1767 let winid = ShowDialog("x", 0)
1768 let winid = ShowDialog("X", 0)
1769 let winid = ShowDialog("\<Esc>", 0)
1770 let winid = ShowDialog("\<C-C>", -1)
1771
1772 delfunc QuitCallback
1773endfunc
1774
Bram Moolenaara730e552019-06-16 19:05:31 +02001775func ShowMenu(key, result)
1776 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001777 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001778 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001779 \ })
1780 redraw
1781 call feedkeys(a:key, "xt")
1782 call assert_equal(winid, s:cb_winid)
1783 call assert_equal(a:result, s:cb_res)
1784endfunc
1785
1786func Test_popup_menu()
1787 func QuitCallback(id, res)
1788 let s:cb_winid = a:id
1789 let s:cb_res = a:res
1790 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001791 " mapping won't be used in popup
1792 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001793
1794 let winid = ShowMenu(" ", 1)
1795 let winid = ShowMenu("j \<CR>", 2)
1796 let winid = ShowMenu("JjK \<CR>", 2)
Christian Brabandtbadeedd2023-08-13 19:25:28 +02001797 " wraps around
1798 let winid = ShowMenu("jjjjjj ", 1)
Bram Moolenaara730e552019-06-16 19:05:31 +02001799 let winid = ShowMenu("kkk ", 1)
1800 let winid = ShowMenu("x", -1)
1801 let winid = ShowMenu("X", -1)
1802 let winid = ShowMenu("\<Esc>", -1)
1803 let winid = ShowMenu("\<C-C>", -1)
1804
1805 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001806 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001807endfunc
1808
1809func Test_popup_menu_screenshot()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001810 CheckScreendump
Bram Moolenaara730e552019-06-16 19:05:31 +02001811
1812 let lines =<< trim END
1813 call setline(1, range(1, 20))
1814 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001815 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001816 func MenuDone(id, res)
1817 echomsg "selected " .. a:res
1818 endfunc
1819 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001820 call writefile(lines, 'XtestPopupMenu', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001821 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001822 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1823
1824 call term_sendkeys(buf, "jj")
1825 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1826
1827 call term_sendkeys(buf, " ")
1828 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1829
1830 " clean up
1831 call StopVimInTerminal(buf)
Bram Moolenaara730e552019-06-16 19:05:31 +02001832endfunc
1833
Bram Moolenaarf914a332019-07-20 15:09:56 +02001834func Test_popup_menu_narrow()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001835 CheckScreendump
Bram Moolenaarf914a332019-07-20 15:09:56 +02001836
1837 let lines =<< trim END
1838 call setline(1, range(1, 20))
1839 hi PopupSelected ctermbg=green
1840 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1841 func MenuDone(id, res)
1842 echomsg "selected " .. a:res
1843 endfunc
1844 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001845 call writefile(lines, 'XtestPopupNarrowMenu', 'D')
Bram Moolenaarf914a332019-07-20 15:09:56 +02001846 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1847 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1848
1849 " clean up
1850 call term_sendkeys(buf, "x")
1851 call StopVimInTerminal(buf)
Bram Moolenaarf914a332019-07-20 15:09:56 +02001852endfunc
1853
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001854func Test_popup_title()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001855 CheckScreendump
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001856
1857 " Create a popup without title or border, a line of padding will be added to
1858 " put the title on.
1859 let lines =<< trim END
1860 call setline(1, range(1, 20))
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001861 let winid = popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001862 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001863 call writefile(lines, 'XtestPopupTitle', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001864 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001865 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1866
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001867 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxwidth: 20, title: 'a very long title that is not going to fit'})\<CR>")
1868 call term_sendkeys(buf, ":\<CR>")
1869 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_1', {})
1870
1871 call term_sendkeys(buf, ":call popup_setoptions(winid, #{border: []})\<CR>")
1872 call term_sendkeys(buf, ":\<CR>")
1873 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_2', {})
1874
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01001875 call term_sendkeys(buf, ":call popup_clear()\<CR>")
1876 call term_sendkeys(buf, ":call popup_create(['aaa', 'bbb'], #{title: 'Title', minwidth: 12, padding: [2, 2, 2, 2]})\<CR>")
1877 call term_sendkeys(buf, ":\<CR>")
1878 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_3', {})
1879
1880 call term_sendkeys(buf, ":call popup_clear()\<CR>")
1881 call term_sendkeys(buf, ":call popup_create(['aaa', 'bbb'], #{title: 'Title', minwidth: 12, border: [], padding: [2, 2, 2, 2]})\<CR>")
1882 call term_sendkeys(buf, ":\<CR>")
1883 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_4', {})
1884
Ralf Schandlbc869872021-05-28 14:12:14 +02001885 call term_sendkeys(buf, ":call popup_clear()\<CR>")
rbtnnc6119412021-08-07 13:08:45 +02001886 call term_sendkeys(buf, ":call popup_menu(['This is a line', 'and another line'], #{title: '▶Äあいうえお◀', })\<CR>")
Ralf Schandlbc869872021-05-28 14:12:14 +02001887 call VerifyScreenDump(buf, 'Test_popupwin_multibytetitle', {})
1888 call term_sendkeys(buf, "x")
1889
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001890 " clean up
1891 call StopVimInTerminal(buf)
Bram Moolenaarae943152019-06-16 22:54:14 +02001892
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001893 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001894 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001895 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001896 call assert_equal('Another Title', popup_getoptions(winid).title)
1897
1898 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001899endfunc
1900
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001901func Test_popup_close_callback()
1902 func PopupDone(id, result)
1903 let g:result = a:result
1904 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001905 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001906 redraw
1907 call popup_close(winid, 'done')
1908 call assert_equal('done', g:result)
1909endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001910
1911func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001912 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001913 redraw
1914 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001915 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001916 call assert_equal(5, pos.height)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001917 call popup_close(winid)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001918
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001919 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001920 redraw
1921 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001922 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001923 call assert_equal(3, pos.height)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001924 call popup_close(winid)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001925endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001926
1927func Test_popup_never_behind()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001928 CheckScreendump
1929
Bram Moolenaar988c4332019-06-02 14:12:11 +02001930 " +-----------------------------+
1931 " | | |
1932 " | | |
1933 " | | |
1934 " | line1 |
1935 " |------------line2------------|
1936 " | line3 |
1937 " | line4 |
1938 " | |
1939 " | |
1940 " +-----------------------------+
1941 let lines =<< trim END
Bram Moolenaar988c4332019-06-02 14:12:11 +02001942 split
1943 vsplit
1944 let info_window1 = getwininfo()[0]
1945 let line = info_window1['height']
1946 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001947 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001948 \ line : line,
1949 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001950 \ })
1951 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001952 call writefile(lines, 'XtestPopupBehind', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001953 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001954 call term_sendkeys(buf, "\<C-W>w")
1955 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1956
1957 " clean up
1958 call StopVimInTerminal(buf)
Bram Moolenaar988c4332019-06-02 14:12:11 +02001959endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001960
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001961func s:VerifyPosition(p, msg, line, col, width, height)
1962 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1963 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1964 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1965 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001966endfunc
1967
1968func Test_popup_position_adjust()
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001969 " Anything placed past the last cell on the right of the screen is moved to
1970 " the left.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001971 "
Boris Staletic13c11532024-12-19 20:22:19 +01001972 " We also shift to the left to display on the
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001973 " screen, unless fixed is set.
1974
1975 " Entries for cases which don't vary based on wrapping.
1976 " Format is per tests described below
1977 let both_wrap_tests = [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001978 \ ['a', 5, &columns, 5, &columns, 1, 1],
1979 \ ['b', 5, &columns + 1, 5, &columns, 1, 1],
1980 \ ['c', 5, &columns - 1, 5, &columns - 1, 1, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001981 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001982 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1]]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001983
1984 " these test groups are dicts with:
1985 " - comment: something to identify the group of tests by
1986 " - options: dict of options to merge with the row/col in tests
1987 " - tests: list of cases. Each one is a list with elements:
1988 " - text
1989 " - row
1990 " - col
1991 " - expected row
1992 " - expected col
1993 " - expected width
1994 " - expected height
1995 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001996 \ #{
Boris Staletic13c11532024-12-19 20:22:19 +01001997 \ comment: 'left aligned with wrapping, fixed position',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001998 \ options: #{
1999 \ wrap: 1,
Boris Staletic13c11532024-12-19 20:22:19 +01002000 \ fixed: v:true,
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002001 \ pos: 'botleft',
2002 \ },
2003 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01002004 \ ['aa', 5, &columns, 4, &columns, 1, 2],
2005 \ ['bb', 5, &columns + 1, 4, &columns, 1, 2],
2006 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
2007 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
2008 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
2009 \
2010 \ ['aaa', 5, &columns, 3, &columns, 1, 3],
2011 \ ['bbb', 5, &columns + 1, 3, &columns, 1, 3],
2012 \ ['ccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
2013 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
2014 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
2015 \
2016 \ ['aaaa', 5, &columns, 2, &columns, 1, 4],
2017 \ ['bbbb', 5, &columns + 1, 2, &columns, 1, 4],
2018 \ ['cccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002019 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
2020 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01002021 \ ['eeee', 5, &columns - 4, 5, &columns - 4, 4, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002022 \ ],
2023 \ },
2024 \ #{
Boris Staletic13c11532024-12-19 20:22:19 +01002025 \ comment: 'left aligned with wrapping, no fixed position',
2026 \ options: #{
2027 \ wrap: 1,
2028 \ fixed: v:false,
2029 \ pos: 'botleft',
2030 \ },
2031 \ tests: both_wrap_tests + [
2032 \ [ repeat('a', &columns*3), 5, &columns, 3, 1, &columns, 3],
2033 \ [ repeat('a', 50), 5, &columns, 5, &columns - 50 + 1, 50, 1],
2034 \ ],
2035 \ },
2036 \ #{
2037 \ comment: 'left aligned without wrapping, no fixed position',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002038 \ options: #{
2039 \ wrap: 0,
Boris Staletic13c11532024-12-19 20:22:19 +01002040 \ fixed: v:false,
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002041 \ pos: 'botleft',
2042 \ },
2043 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01002044 \ ['aa', 5, &columns, 5, &columns - 1, 2, 1],
2045 \ ['bb', 5, &columns + 1, 5, &columns - 1, 2, 1],
2046 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
2047 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
2048 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
2049 \
2050 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
2051 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
2052 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
2053 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
2054 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
2055 \
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002056 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
2057 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
2058 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
2059 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
2060 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
2061 \ ],
2062 \ },
2063 \ #{
Boris Staletic13c11532024-12-19 20:22:19 +01002064 \ comment: 'left aligned without wrapping, fixed position',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002065 \ options: #{
2066 \ wrap: 0,
2067 \ fixed: 1,
2068 \ pos: 'botleft',
2069 \ },
2070 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01002071 \ ['aa', 5, &columns, 5, &columns, 1, 1],
2072 \ ['bb', 5, &columns + 1, 5, &columns, 1, 1],
2073 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
2074 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
2075 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
2076 \
2077 \ ['aaa', 5, &columns, 5, &columns, 1, 1],
2078 \ ['bbb', 5, &columns + 1, 5, &columns, 1, 1],
2079 \ ['ccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
2080 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
2081 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
2082 \
2083 \ ['aaaa', 5, &columns, 5, &columns, 1, 1],
2084 \ ['bbbb', 5, &columns + 1, 5, &columns, 1, 1],
2085 \ ['cccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002086 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
2087 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
2088 \ ],
2089 \ },
2090 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002091
2092 for test_group in tests
2093 for test in test_group.tests
2094 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002095 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002096 \ line: line,
2097 \ col: col,
2098 \ }
2099 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002100
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002101 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002102
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002103 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002104 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
2105 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002106 endfor
2107 endfor
2108
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002109 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002110 %bwipe!
2111endfunc
2112
Bram Moolenaar3397f742019-06-02 18:40:06 +02002113func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002114 " width of screen
2115 let X = join(map(range(&columns), {->'X'}), '')
2116
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002117 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
2118 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002119
2120 redraw
2121 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2122 call assert_equal(X, line)
2123
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002124 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002125 redraw
2126
2127 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002128 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
2129 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002130
2131 redraw
2132 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2133 call assert_equal(X, line)
2134
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002135 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002136 redraw
2137
2138 " Extend so > window width
2139 let X .= 'x'
2140
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002141 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
2142 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002143
2144 redraw
2145 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2146 call assert_equal(X[ : -2 ], line)
2147
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002148 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002149 redraw
2150
2151 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002152 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
2153 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002154
2155 redraw
2156 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2157 call assert_equal(X[ : -2 ], line)
2158
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002159 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002160 redraw
2161
2162 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002163 let p = popup_create(X,
2164 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
2165 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002166
2167 redraw
2168 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2169 let e_line = ' ' . X[ 1 : -2 ]
2170 call assert_equal(e_line, line)
2171
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002172 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002173 redraw
2174
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002175 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002176 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02002177endfunc
2178
2179func Test_popup_moved()
2180 new
2181 call test_override('char_avail', 1)
2182 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
2183
2184 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002185 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002186 redraw
2187 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002188 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002189 " trigger the check for last_cursormoved by going into insert mode
2190 call feedkeys("li\<Esc>", 'xt')
2191 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002192 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002193
2194 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002195 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002196 redraw
2197 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002198 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002199 call feedkeys("hi\<Esc>", 'xt')
2200 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002201 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002202
2203 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002204 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002205 redraw
2206 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002207 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002208 call feedkeys("li\<Esc>", 'xt')
2209 call assert_equal(1, popup_getpos(winid).visible)
2210 call feedkeys("ei\<Esc>", 'xt')
2211 call assert_equal(1, popup_getpos(winid).visible)
2212 call feedkeys("eli\<Esc>", 'xt')
2213 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002214 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002215
Bram Moolenaar17627312019-06-02 19:53:44 +02002216 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02002217 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02002218 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002219 redraw
2220 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002221 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002222 call feedkeys("eli\<Esc>", 'xt')
2223 call assert_equal(1, popup_getpos(winid).visible)
2224 call feedkeys("wi\<Esc>", 'xt')
2225 call assert_equal(1, popup_getpos(winid).visible)
2226 call feedkeys("Eli\<Esc>", 'xt')
2227 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002228 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002229
2230 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002231 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002232 redraw
2233 call assert_equal(1, popup_getpos(winid).visible)
2234 call feedkeys("eli\<Esc>", 'xt')
2235 call feedkeys("ei\<Esc>", 'xt')
2236 call assert_equal(1, popup_getpos(winid).visible)
2237 call feedkeys("eli\<Esc>", 'xt')
2238 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002239 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002240
2241 bwipe!
2242 call test_override('ALL', 0)
2243endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002244
2245func Test_notifications()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002246 CheckFeature timers
2247 CheckScreendump
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002248
Bram Moolenaar0fdddee2019-09-01 15:26:23 +02002249 let lines =<< trim END
2250 call setline(1, range(1, 20))
2251 hi Notification ctermbg=lightblue
2252 call popup_notification('first notification', {})
2253 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01002254 call writefile(lines, 'XtestNotifications', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002255 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002256 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
2257
2258 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002259 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
2260 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002261 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
2262
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002263 " clean up
2264 call StopVimInTerminal(buf)
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002265endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002266
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002267func Test_popup_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002268 CheckScreendump
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002269
2270 let lines =<< trim END
2271 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02002272 hi ScrollThumb ctermbg=blue
2273 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002274 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002275 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002276 \ minwidth: 8,
2277 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002278 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002279 func ScrollUp()
2280 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
2281 endfunc
2282 func ScrollDown()
2283 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
2284 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002285 func ClickTop()
2286 call feedkeys("\<F4>\<LeftMouse>", "xt")
2287 endfunc
2288 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002289 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002290 call feedkeys("\<F5>\<LeftMouse>", "xt")
2291 endfunc
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002292 func Popup_filter(winid, key)
2293 if a:key == 'j'
Bram Moolenaar371806e2020-10-22 13:44:54 +02002294 silent! this_throws_an_error_but_is_ignored
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002295 let line = popup_getoptions(a:winid).firstline
2296 let nlines = line('$', a:winid)
2297 let newline = line < nlines ? (line + 1) : nlines
2298 call popup_setoptions(a:winid, #{firstline: newline})
2299 return v:true
2300 elseif a:key == 'x'
2301 call popup_close(a:winid)
2302 return v:true
2303 endif
2304 endfunc
2305
Bram Moolenaarfc376e02022-05-29 18:18:18 +01002306 def CreatePopup(text: list<string>): number
2307 return popup_create(text, {
Bram Moolenaar6c542f72020-09-27 21:16:45 +02002308 \ minwidth: 30,
2309 \ maxwidth: 30,
2310 \ minheight: 4,
2311 \ maxheight: 4,
2312 \ firstline: 1,
2313 \ lastline: 4,
2314 \ wrap: true,
2315 \ scrollbar: true,
2316 \ mapping: false,
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002317 \ filter: g:Popup_filter,
Bram Moolenaar6c542f72020-09-27 21:16:45 +02002318 \ })
2319 enddef
2320
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002321 func PopupScroll()
2322 call popup_clear()
2323 let text =<< trim END
2324 1
2325 2
2326 3
2327 4
2328 long line long line long line long line long line long line
2329 long line long line long line long line long line long line
2330 long line long line long line long line long line long line
2331 END
Bram Moolenaar6c542f72020-09-27 21:16:45 +02002332 call CreatePopup(text)
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002333 endfunc
Bram Moolenaarfc376e02022-05-29 18:18:18 +01002334 func ScrollBottom()
2335 call popup_clear()
Bram Moolenaar89b25582022-05-30 13:20:56 +01002336 let id = CreatePopup(range(100)->map({k, v -> string(v)}))
2337 call popup_setoptions(id, #{firstline: 100, minheight: 9, maxheight: 9})
Bram Moolenaarfc376e02022-05-29 18:18:18 +01002338 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002339 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002340 map <silent> <F4> :call test_setmouse(4, 42)<CR>
2341 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002342 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01002343 call writefile(lines, 'XtestPopupScroll', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002344 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002345 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
2346
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002347 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002348 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002349 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
2350
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002351 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002352 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002353 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
2354
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002355 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002356 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002357 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
2358
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02002359 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb', firstline: 5})\<CR>")
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002360 " this scrolls two lines (half the window height)
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002361 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
2362 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
2363
2364 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2365 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
2366
2367 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02002368 " wait a bit, otherwise it fails sometimes (double click recognized?)
2369 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002370 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2371 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
2372
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002373 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2374 sleep 100m
2375 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2376 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
2377
2378 call term_sendkeys(buf, ":call ClickBot()\<CR>")
2379 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
2380
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002381 " remove the minwidth and maxheight
2382 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxheight: 0, minwidth: 0})\<CR>")
Bram Moolenaar7e0f4622019-09-17 21:23:39 +02002383 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002384 call VerifyScreenDump(buf, 'Test_popupwin_scroll_10', {})
2385
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002386 " check size with non-wrapping lines
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002387 call term_sendkeys(buf, ":call g:PopupScroll()\<CR>")
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002388 call VerifyScreenDump(buf, 'Test_popupwin_scroll_11', {})
2389
2390 " check size with wrapping lines
2391 call term_sendkeys(buf, "j")
2392 call VerifyScreenDump(buf, 'Test_popupwin_scroll_12', {})
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002393
Bram Moolenaarfc376e02022-05-29 18:18:18 +01002394 " check thumb when scrolled all the way down
2395 call term_sendkeys(buf, ":call ScrollBottom()\<CR>")
2396 call VerifyScreenDump(buf, 'Test_popupwin_scroll_13', {})
2397
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002398 " clean up
Bram Moolenaar20298ce2020-06-19 21:46:52 +02002399 call term_sendkeys(buf, "x")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002400 call StopVimInTerminal(buf)
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002401endfunc
2402
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02002403func Test_popup_too_high_scrollbar()
2404 CheckScreendump
2405
2406 let lines =<< trim END
2407 call setline(1, range(1, 20)->map({i, v -> repeat(v, 10)}))
2408 set scrolloff=0
2409 func ShowPopup()
2410 let winid = popup_atcursor(['one', 'two', 'three', 'four', 'five',
2411 \ 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve'], #{
2412 \ minwidth: 8,
2413 \ border: [],
2414 \ })
2415 endfunc
2416 normal 3G$
2417 call ShowPopup()
2418 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01002419 call writefile(lines, 'XtestPopupToohigh', 'D')
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02002420 let buf = RunVimInTerminal('-S XtestPopupToohigh', #{rows: 10})
2421 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_1', {})
2422
2423 call term_sendkeys(buf, ":call popup_clear()\<CR>")
2424 call term_sendkeys(buf, "8G$")
2425 call term_sendkeys(buf, ":call ShowPopup()\<CR>")
2426 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_2', {})
2427
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02002428 call term_sendkeys(buf, ":call popup_clear()\<CR>")
2429 call term_sendkeys(buf, "gg$")
2430 call term_sendkeys(buf, ":call ShowPopup()\<CR>")
2431 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_3', {})
2432
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02002433 " clean up
2434 call StopVimInTerminal(buf)
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02002435endfunc
2436
Bram Moolenaar437a7462019-07-05 20:17:22 +02002437func Test_popup_fitting_scrollbar()
2438 " this was causing a crash, divide by zero
2439 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002440 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002441 \ scrollbar: 1,
2442 \ maxwidth: 10,
2443 \ maxheight: 5,
2444 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02002445 redraw
2446 call popup_clear()
2447endfunc
2448
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002449func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002450 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002451
2452 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002453 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002454 let p = popup_create('test', opts)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002455 eval p->popup_settext('this is a text')
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002456 END
2457
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01002458 call writefile(lines, 'XtestPopupSetText', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002459 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002460 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
2461
2462 " Setting to empty string clears it
2463 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
2464 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
2465
2466 " Setting a list
2467 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2468 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2469
2470 " Shrinking with a list
2471 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
2472 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
2473
2474 " Growing with a list
2475 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2476 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2477
2478 " Empty list clears
2479 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
2480 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
2481
2482 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002483 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002484 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
2485
Bram Moolenaarb0992022020-01-30 14:55:42 +01002486 " range() (doesn't work)
2487 call term_sendkeys(buf, ":call popup_settext(p, range(4, 8))\<CR>")
2488 call VerifyScreenDump(buf, 'Test_popup_settext_07', {})
2489
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002490 " clean up
2491 call StopVimInTerminal(buf)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002492endfunc
2493
Bram Moolenaar1f42f5a2020-09-03 18:52:24 +02002494func Test_popup_settext_getline()
2495 let id = popup_create('', #{ tabpage: 0 })
2496 call popup_settext(id, ['a','b'])
2497 call assert_equal(2, line('$', id)) " OK :)
2498 call popup_close(id)
2499
2500 let id = popup_create('', #{ tabpage: -1 })
2501 call popup_settext(id, ['a','b'])
2502 call assert_equal(2, line('$', id)) " Fails :(
2503 call popup_close(id)
2504endfunc
2505
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02002506func Test_popup_settext_null()
2507 let id = popup_create('', #{ tabpage: 0 })
2508 call popup_settext(id, test_null_list())
2509 call popup_close(id)
2510
2511 let id = popup_create('', #{ tabpage: 0 })
2512 call popup_settext(id, test_null_string())
2513 call popup_close(id)
2514endfunc
2515
Christian Brabandtfbc37f12024-06-18 20:50:58 +02002516func Test_popup_setbuf()
2517 CheckScreendump
2518
2519 let lines =<< trim END
2520 let opts = #{wrap: 0}
2521 let p = popup_create('test', opts)
2522 let buf = bufnr('%')
2523 END
2524
2525 call writefile(lines, 'XtestPopupSetBuf', 'D')
2526 let buf = RunVimInTerminal('-S XtestPopupSetBuf', #{rows: 10})
2527 call VerifyScreenDump(buf, 'Test_popup_setbuf_01', {})
2528
2529 " Setting to an non-existing buffer doesn't do anything
2530 call term_sendkeys(buf, ":call popup_setbuf(p, 'foobar.txt')\<CR>")
2531 call VerifyScreenDump(buf, 'Test_popup_setbuf_02', {})
2532
2533 " Error
2534 call term_sendkeys(buf, ":call popup_setbuf(p, ['a','b','c'])\<CR>")
2535 call VerifyScreenDump(buf, 'Test_popup_setbuf_03', {})
2536
2537 " Set to help window
2538 call term_sendkeys(buf, ":help\<CR>")
2539 call term_sendkeys(buf, ":call popup_setbuf(p, 'help.txt')\<CR>")
2540 call VerifyScreenDump(buf, 'Test_popup_setbuf_04', {})
2541
2542 " Setting back to original buffer
2543 call term_sendkeys(buf, ":call popup_setbuf(p, buf)\<CR>")
2544 call VerifyScreenDump(buf, 'Test_popup_setbuf_05', {})
2545
2546 " use method
2547 call term_sendkeys(buf, ":echo p->popup_setbuf('help.txt')\<CR>")
2548 call VerifyScreenDump(buf, 'Test_popup_setbuf_06', {})
2549
2550 call term_sendkeys(buf, ":echo p->popup_setbuf(buf)\<CR>")
2551 call VerifyScreenDump(buf, 'Test_popup_setbuf_05', {})
2552
2553 " clean up
2554 call StopVimInTerminal(buf)
2555endfunc
2556
2557func Test_popup_setbuf_terminal()
2558 CheckFeature terminal
2559
2560 " Check Terminal Feature
2561 let termbuf = term_start(&shell, #{hidden: 1})
2562 " Wait for shell to start
2563 call WaitForAssert({-> assert_equal("run", job_status(term_getjob(termbuf)))})
2564
2565 let popup = popup_create('test', {})
2566 call assert_true(popup->popup_setbuf(termbuf))
2567 call popup_close(popup)
2568
2569 let popup1 = popup_create(termbuf, #{minwidth: 40, minheight: 10, border: []})
2570
2571 let popup = popup_create('test', {})
2572 try
2573 call assert_fails(call popup_setbuf(popup, termbuf))
2574 catch
2575 endtry
2576 call popup_close(popup)
2577 call popup_close(popup1)
2578 call assert_equal([], popup_list())
2579 " Close the terminal
2580 call term_sendkeys(termbuf, "exit\<CR>")
2581 " Wait for shell to exit
2582 call WaitForAssert({-> assert_equal("dead", job_status(term_getjob(termbuf)))})
2583endfunc
2584
2585func Test_popup_setbuf_null()
2586 let id = popup_create('', {})
2587 call assert_false(popup_setbuf(id, -1))
2588 call popup_close(id)
2589
2590 let id = popup_create('', {})
2591 call assert_true(popup_setbuf(id, test_null_string()))
2592 call assert_true(popup_setbuf(id, ''))
2593 call popup_close(id)
2594
2595 call assert_false(popup_setbuf(id, 0))
2596endfunc
2597
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002598func Test_popup_hidden()
2599 new
2600
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002601 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002602 redraw
2603 call assert_equal(0, popup_getpos(winid).visible)
2604 call popup_close(winid)
2605
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002606 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002607 redraw
2608 call assert_equal(0, popup_getpos(winid).visible)
2609 call popup_close(winid)
2610
2611 func QuitCallback(id, res)
2612 let s:cb_winid = a:id
2613 let s:cb_res = a:res
2614 endfunc
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002615 let winid = 'make a choice'->popup_dialog(#{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002616 \ filter: 'popup_filter_yesno',
2617 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002618 \ })
2619 redraw
2620 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02002621 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
2622 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002623 exe "normal anot used by filter\<Esc>"
2624 call assert_equal('not used by filter', getline(1))
2625
2626 call popup_show(winid)
2627 call feedkeys('y', "xt")
2628 call assert_equal(1, s:cb_res)
2629
2630 bwipe!
2631 delfunc QuitCallback
2632endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02002633
2634" Test options not checked elsewhere
2635func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002636 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002637 let options = popup_getoptions(winid)
2638 call assert_equal(1, options.wrap)
2639 call assert_equal(0, options.drag)
2640 call assert_equal('Beautiful', options.highlight)
2641
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002642 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002643 let options = popup_getoptions(winid)
2644 call assert_equal(0, options.wrap)
2645 call assert_equal(1, options.drag)
2646 call assert_equal('Another', options.highlight)
2647
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01002648 call assert_fails('call popup_setoptions(winid, [])', 'E1206:')
2649 call assert_fails('call popup_setoptions(winid, test_null_dict())', 'E1297:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002650
Bram Moolenaarae943152019-06-16 22:54:14 +02002651 call popup_close(winid)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002652 call assert_equal(0, popup_setoptions(winid, options.wrap))
Bram Moolenaarae943152019-06-16 22:54:14 +02002653endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002654
2655func Test_popupwin_garbage_collect()
2656 func MyPopupFilter(x, winid, c)
2657 " NOP
2658 endfunc
2659
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002660 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002661 call test_garbagecollect_now()
2662 redraw
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02002663 " Must not crash caused by invalid memory access
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002664 call feedkeys('j', 'xt')
2665 call assert_true(v:true)
2666
2667 call popup_close(winid)
2668 delfunc MyPopupFilter
2669endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002670
Bram Moolenaar581ba392019-09-03 22:08:33 +02002671func Test_popupwin_filter_mode()
2672 func MyPopupFilter(winid, c)
2673 let s:typed = a:c
2674 if a:c == ':' || a:c == "\r" || a:c == 'v'
2675 " can start cmdline mode, get out, and start/stop Visual mode
2676 return 0
2677 endif
2678 return 1
2679 endfunc
2680
2681 " Normal, Visual and Insert mode
2682 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'nvi'})
2683 redraw
2684 call feedkeys('x', 'xt')
2685 call assert_equal('x', s:typed)
2686
2687 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2688 call assert_equal(':', s:typed)
2689 call assert_equal('foo', g:foo)
2690
2691 let @x = 'something'
2692 call feedkeys('v$"xy', 'xt')
2693 call assert_equal('y', s:typed)
2694 call assert_equal('something', @x) " yank command is filtered out
2695 call feedkeys('v', 'xt') " end Visual mode
2696
2697 call popup_close(winid)
2698
2699 " only Normal mode
2700 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'n'})
2701 redraw
2702 call feedkeys('x', 'xt')
2703 call assert_equal('x', s:typed)
2704
2705 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2706 call assert_equal(':', s:typed)
2707 call assert_equal('foo', g:foo)
2708
2709 let @x = 'something'
2710 call feedkeys('v$"xy', 'xt')
2711 call assert_equal('v', s:typed)
2712 call assert_notequal('something', @x)
2713
2714 call popup_close(winid)
2715
2716 " default: all modes
2717 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
2718 redraw
2719 call feedkeys('x', 'xt')
2720 call assert_equal('x', s:typed)
2721
2722 let g:foo = 'bar'
2723 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2724 call assert_equal("\r", s:typed)
2725 call assert_equal('bar', g:foo)
2726
2727 let @x = 'something'
2728 call feedkeys('v$"xy', 'xt')
2729 call assert_equal('y', s:typed)
2730 call assert_equal('something', @x) " yank command is filtered out
2731 call feedkeys('v', 'xt') " end Visual mode
2732
2733 call popup_close(winid)
2734 delfunc MyPopupFilter
2735endfunc
2736
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002737func Test_popupwin_filter_mouse()
2738 func MyPopupFilter(winid, c)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002739 let g:got_mousepos = getmousepos()
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002740 return 0
2741 endfunc
2742
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002743 call setline(1, ['.'->repeat(25)]->repeat(10))
2744 let winid = popup_create(['short', 'long line that will wrap', 'other'], #{
2745 \ line: 2,
2746 \ col: 4,
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002747 \ maxwidth: 12,
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002748 \ padding: [],
2749 \ border: [],
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002750 \ filter: 'MyPopupFilter',
2751 \ })
2752 redraw
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002753 " 123456789012345678901
2754 " 1 .....................
2755 " 2 ...+--------------+..
2756 " 3 ...| |..
2757 " 4 ...| short |..
2758 " 5 ...| long line th |..
2759 " 6 ...| at will wrap |..
2760 " 7 ...| other |..
2761 " 8 ...| |..
2762 " 9 ...+--------------+..
2763 " 10 .....................
2764 let tests = []
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002765
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002766 func AddItemOutsidePopup(tests, row, col)
2767 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2768 \ screenrow: a:row, screencol: a:col,
2769 \ winid: win_getid(), winrow: a:row, wincol: a:col,
zeertzjqf5a94d52023-10-15 10:03:30 +02002770 \ line: a:row, column: a:col, coladd: 0,
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002771 \ }})
2772 endfunc
2773 func AddItemInPopupBorder(tests, winid, row, col)
2774 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2775 \ screenrow: a:row, screencol: a:col,
2776 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
zeertzjqf5a94d52023-10-15 10:03:30 +02002777 \ line: 0, column: 0, coladd: 0,
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002778 \ }})
2779 endfunc
zeertzjqf5a94d52023-10-15 10:03:30 +02002780 func AddItemInPopupText(tests, winid, row, col, textline, textcol, coladd = 0)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002781 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2782 \ screenrow: a:row, screencol: a:col,
2783 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
zeertzjqf5a94d52023-10-15 10:03:30 +02002784 \ line: a:textline, column: a:textcol, coladd: a:coladd,
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002785 \ }})
2786 endfunc
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002787
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002788 " above and below popup
2789 for c in range(1, 21)
2790 call AddItemOutsidePopup(tests, 1, c)
2791 call AddItemOutsidePopup(tests, 10, c)
2792 endfor
2793 " left and right of popup
2794 for r in range(1, 10)
2795 call AddItemOutsidePopup(tests, r, 3)
2796 call AddItemOutsidePopup(tests, r, 20)
2797 endfor
2798 " top and bottom in popup
2799 for c in range(4, 19)
2800 call AddItemInPopupBorder(tests, winid, 2, c)
2801 call AddItemInPopupBorder(tests, winid, 3, c)
2802 call AddItemInPopupBorder(tests, winid, 8, c)
2803 call AddItemInPopupBorder(tests, winid, 9, c)
2804 endfor
2805 " left and right margin in popup
2806 for r in range(2, 9)
2807 call AddItemInPopupBorder(tests, winid, r, 4)
2808 call AddItemInPopupBorder(tests, winid, r, 5)
2809 call AddItemInPopupBorder(tests, winid, r, 18)
2810 call AddItemInPopupBorder(tests, winid, r, 19)
2811 endfor
2812 " text "short"
2813 call AddItemInPopupText(tests, winid, 4, 6, 1, 1)
2814 call AddItemInPopupText(tests, winid, 4, 10, 1, 5)
2815 call AddItemInPopupText(tests, winid, 4, 11, 1, 6)
zeertzjqf5a94d52023-10-15 10:03:30 +02002816 call AddItemInPopupText(tests, winid, 4, 17, 1, 6, 6)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002817 " text "long line th"
2818 call AddItemInPopupText(tests, winid, 5, 6, 2, 1)
2819 call AddItemInPopupText(tests, winid, 5, 10, 2, 5)
2820 call AddItemInPopupText(tests, winid, 5, 17, 2, 12)
2821 " text "at will wrap"
2822 call AddItemInPopupText(tests, winid, 6, 6, 2, 13)
2823 call AddItemInPopupText(tests, winid, 6, 10, 2, 17)
2824 call AddItemInPopupText(tests, winid, 6, 17, 2, 24)
2825 " text "other"
2826 call AddItemInPopupText(tests, winid, 7, 6, 3, 1)
2827 call AddItemInPopupText(tests, winid, 7, 10, 3, 5)
2828 call AddItemInPopupText(tests, winid, 7, 11, 3, 6)
zeertzjqf5a94d52023-10-15 10:03:30 +02002829 call AddItemInPopupText(tests, winid, 7, 17, 3, 6, 6)
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002830
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002831 for item in tests
2832 call test_setmouse(item.clickrow, item.clickcol)
2833 call feedkeys("\<LeftMouse>", 'xt')
2834 call assert_equal(item.result, g:got_mousepos)
2835 endfor
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002836
2837 call popup_close(winid)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002838 enew!
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002839 delfunc MyPopupFilter
2840endfunc
2841
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002842func Test_popupwin_with_buffer()
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01002843 call writefile(['some text', 'in a buffer'], 'XsomeFile', 'D')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002844 let buf = bufadd('XsomeFile')
2845 call assert_equal(0, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002846
2847 setlocal number
2848 call setbufvar(buf, "&wrapmargin", 13)
2849
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002850 let winid = popup_create(buf, {})
2851 call assert_notequal(0, winid)
2852 let pos = popup_getpos(winid)
2853 call assert_equal(2, pos.height)
2854 call assert_equal(1, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002855
2856 " window-local option is set to default, buffer-local is not
2857 call assert_equal(0, getwinvar(winid, '&number'))
2858 call assert_equal(13, getbufvar(buf, '&wrapmargin'))
2859
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002860 call popup_close(winid)
2861 call assert_equal({}, popup_getpos(winid))
2862 call assert_equal(1, bufloaded(buf))
2863 exe 'bwipe! ' .. buf
Bram Moolenaar46451042019-08-24 15:50:46 +02002864 setlocal nonumber
Bram Moolenaar7866b872019-07-01 22:21:01 +02002865
2866 edit test_popupwin.vim
2867 let winid = popup_create(bufnr(''), {})
2868 redraw
2869 call popup_close(winid)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002870endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02002871
Bram Moolenaar188639d2022-04-04 16:57:21 +01002872func Test_popupwin_buffer_with_swapfile()
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01002873 call writefile(['some text', 'in a buffer'], 'XopenFile', 'D')
2874 call writefile([''], '.XopenFile.swp', 'D')
Bram Moolenaar188639d2022-04-04 16:57:21 +01002875 let g:ignoreSwapExists = 1
2876
2877 let bufnr = bufadd('XopenFile')
2878 call assert_equal(0, bufloaded(bufnr))
2879 let winid = popup_create(bufnr, {'hidden': 1})
2880 call assert_equal(1, bufloaded(bufnr))
2881 call popup_close(winid)
2882
2883 exe 'buffer ' .. bufnr
2884 call assert_equal(1, &readonly)
2885 bwipe!
2886
Bram Moolenaar188639d2022-04-04 16:57:21 +01002887 unlet g:ignoreSwapExists
2888endfunc
2889
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002890func Test_popupwin_terminal_buffer()
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002891 CheckFeature terminal
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002892 CheckUnix
Bram Moolenaare06a28f2020-05-13 23:24:12 +02002893 " Starting a terminal to run a shell in is considered flaky.
2894 let g:test_is_flaky = 1
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002895
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002896 let origwin = win_getid()
Bram Moolenaar349f6092020-10-06 20:46:49 +02002897
2898 " open help window to test that :help below fails
2899 help
2900
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002901 let termbuf = term_start(&shell, #{hidden: 1})
Bram Moolenaarf5452692020-11-28 21:56:06 +01002902 let winid = popup_create(termbuf, #{minwidth: 40, minheight: 10, border: []})
Bram Moolenaar0353f562020-12-17 22:27:38 +01002903 " Wait for shell to start
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002904 call WaitForAssert({-> assert_equal("run", job_status(term_getjob(termbuf)))})
Bram Moolenaar0353f562020-12-17 22:27:38 +01002905 " Wait for a prompt (see border char first, then space after prompt)
2906 call WaitForAssert({ -> assert_equal(' ', screenstring(screenrow(), screencol() - 1))})
Bram Moolenaarf5452692020-11-28 21:56:06 +01002907
2908 " When typing a character, the cursor is after it.
2909 call feedkeys("x", 'xt')
Bram Moolenaar0353f562020-12-17 22:27:38 +01002910 call term_wait(termbuf)
Bram Moolenaarf5452692020-11-28 21:56:06 +01002911 redraw
2912 call WaitForAssert({ -> assert_equal('x', screenstring(screenrow(), screencol() - 1))})
2913 call feedkeys("\<BS>", 'xt')
2914
Bram Moolenaar631ebc42020-02-03 22:15:26 +01002915 " Check this doesn't crash
2916 call assert_equal(winnr(), winnr('j'))
2917 call assert_equal(winnr(), winnr('k'))
2918 call assert_equal(winnr(), winnr('h'))
2919 call assert_equal(winnr(), winnr('l'))
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002920
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002921 " Cannot quit while job is running
2922 call assert_fails('call feedkeys("\<C-W>:quit\<CR>", "xt")', 'E948:')
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002923
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002924 " Cannot enter Terminal-Normal mode. (TODO: but it works...)
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002925 call feedkeys("xxx\<C-W>N", 'xt')
2926 call assert_fails('call feedkeys("gf", "xt")', 'E863:')
2927 call feedkeys("a\<C-U>", 'xt')
2928
Bram Moolenaar3f65c662020-05-27 23:15:16 +02002929 " Cannot escape from terminal window
2930 call assert_fails('tab drop xxx', 'E863:')
Bram Moolenaar349f6092020-10-06 20:46:49 +02002931 call assert_fails('help', 'E994:')
Bram Moolenaar3f65c662020-05-27 23:15:16 +02002932
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002933 " Cannot open a second one.
2934 let termbuf2 = term_start(&shell, #{hidden: 1})
2935 call assert_fails('call popup_create(termbuf2, #{})', 'E861:')
2936 call term_sendkeys(termbuf2, "exit\<CR>")
2937
Bram Moolenaard28950f2022-05-29 14:13:04 +01002938 " Exiting shell puts popup window in Terminal-Normal mode.
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002939 call feedkeys("exit\<CR>", 'xt')
2940 " Wait for shell to exit
Bram Moolenaarb2b218d2020-06-22 20:22:19 +02002941 call WaitForAssert({-> assert_equal("dead", job_status(term_getjob(termbuf)))})
Bram Moolenaar373c6512020-06-22 19:24:23 +02002942
Bram Moolenaar349f6092020-10-06 20:46:49 +02002943 helpclose
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002944 call feedkeys(":quit\<CR>", 'xt')
2945 call assert_equal(origwin, win_getid())
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002946endfunc
2947
Bram Moolenaar9e636b92022-05-29 22:37:05 +01002948func Test_popupwin_terminal_buffer_none()
2949 CheckFeature terminal
2950 CheckUnix
2951
2952 " Starting a terminal to run a shell in is considered flaky.
2953 let g:test_is_flaky = 1
2954
2955 let origwin = win_getid()
2956 call term_start("NONE", {"hidden": 1})->popup_create({"border": []})
2957 sleep 50m
2958
2959 " since no actual job is running can close the window with :quit
2960 call feedkeys("\<C-W>:q\<CR>", 'xt')
2961 call assert_equal([], popup_list())
2962
2963 call assert_equal(origwin, win_getid())
2964endfunc
2965
Bram Moolenaard28950f2022-05-29 14:13:04 +01002966func Test_popupwin_terminal_scrollbar()
2967 CheckFeature terminal
2968 CheckScreendump
2969 CheckUnix
2970
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01002971 call writefile(range(50), 'Xtestfile', 'D')
Bram Moolenaard28950f2022-05-29 14:13:04 +01002972 let lines =<< trim END
2973 vim9script
2974
Bram Moolenaar10db31f2022-05-30 17:58:03 +01002975 # testing CTRL-W CTRL-W requires two windows
2976 split
2977
Bram Moolenaard28950f2022-05-29 14:13:04 +01002978 term_start(['cat', 'Xtestfile'], {hidden: true})
2979 ->popup_create({
2980 minwidth: 40,
2981 maxwidth: 40,
2982 minheight: 8,
2983 maxheight: 8,
2984 scrollbar: true,
2985 border: []
2986 })
2987 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01002988 call writefile(lines, 'Xpterm', 'D')
Bram Moolenaard28950f2022-05-29 14:13:04 +01002989 let buf = RunVimInTerminal('-S Xpterm', #{rows: 15})
2990 call VerifyScreenDump(buf, 'Test_popupwin_poptermscroll_1', {})
2991
2992 " scroll to the middle
2993 call term_sendkeys(buf, "50%")
2994 call VerifyScreenDump(buf, 'Test_popupwin_poptermscroll_2', {})
2995
Bram Moolenaar10db31f2022-05-30 17:58:03 +01002996 " get error if trying to escape the window
2997 call term_sendkeys(buf, "\<C-W>\<C-W>")
2998 call VerifyScreenDump(buf, 'Test_popupwin_poptermscroll_3', {})
2999
Bram Moolenaard28950f2022-05-29 14:13:04 +01003000 " close the popupwin.
3001 call term_sendkeys(buf, ":q\<CR>")
Bram Moolenaar10db31f2022-05-30 17:58:03 +01003002 call VerifyScreenDump(buf, 'Test_popupwin_poptermscroll_4', {})
Bram Moolenaard28950f2022-05-29 14:13:04 +01003003
3004 call StopVimInTerminal(buf)
Bram Moolenaard28950f2022-05-29 14:13:04 +01003005endfunc
3006
Bram Moolenaarab176ce2020-06-15 21:19:08 +02003007func Test_popupwin_close_prevwin()
3008 CheckFeature terminal
Bram Moolenaar62f93f42020-09-02 22:33:24 +02003009 call Popupwin_close_prevwin()
3010endfunc
Bram Moolenaarab176ce2020-06-15 21:19:08 +02003011
Bram Moolenaar62f93f42020-09-02 22:33:24 +02003012def Popupwin_close_prevwin()
3013 assert_equal(1, winnr('$'))
Bram Moolenaarab176ce2020-06-15 21:19:08 +02003014 split
3015 wincmd b
Bram Moolenaar62f93f42020-09-02 22:33:24 +02003016 assert_equal(2, winnr())
Bram Moolenaare0de1712020-12-02 17:36:54 +01003017 var buf = term_start(&shell, {hidden: 1})
Bram Moolenaar62f93f42020-09-02 22:33:24 +02003018 popup_create(buf, {})
Bram Moolenaar62aec932022-01-29 21:45:34 +00003019 g:TermWait(buf, 100)
Bram Moolenaar62f93f42020-09-02 22:33:24 +02003020 popup_clear(true)
3021 assert_equal(2, winnr())
Bram Moolenaarab176ce2020-06-15 21:19:08 +02003022
3023 quit
3024 exe 'bwipe! ' .. buf
Bram Moolenaar62f93f42020-09-02 22:33:24 +02003025enddef
Bram Moolenaarab176ce2020-06-15 21:19:08 +02003026
Bram Moolenaar934470e2019-09-01 23:27:05 +02003027func Test_popupwin_with_buffer_and_filter()
3028 new Xwithfilter
3029 call setline(1, range(100))
3030 let bufnr = bufnr()
3031 hide
3032
3033 func BufferFilter(win, key)
3034 if a:key == 'G'
3035 " recursive use of "G" does not cause problems.
3036 call win_execute(a:win, 'normal! G')
3037 return 1
3038 endif
3039 return 0
3040 endfunc
3041
3042 let winid = popup_create(bufnr, #{maxheight: 5, filter: 'BufferFilter'})
3043 call assert_equal(1, popup_getpos(winid).firstline)
3044 redraw
3045 call feedkeys("G", 'xt')
3046 call assert_equal(99, popup_getpos(winid).firstline)
3047
3048 call popup_close(winid)
3049 exe 'bwipe! ' .. bufnr
3050endfunc
3051
Bram Moolenaare296e312019-07-03 23:20:18 +02003052func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003053 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003054 \ maxwidth: 40,
3055 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02003056 \ })
3057 for top in range(1, 20)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003058 eval winid->popup_setoptions(#{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02003059 redraw
3060 call assert_equal(19, popup_getpos(winid).width)
3061 endfor
3062 call popup_clear()
3063endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02003064
3065func Test_popupwin_buf_close()
3066 let buf = bufadd('Xtestbuf')
3067 call bufload(buf)
3068 call setbufline(buf, 1, ['just', 'some', 'lines'])
3069 let winid = popup_create(buf, {})
3070 redraw
3071 call assert_equal(3, popup_getpos(winid).height)
3072 let bufinfo = getbufinfo(buf)[0]
3073 call assert_equal(1, bufinfo.changed)
3074 call assert_equal(0, bufinfo.hidden)
3075 call assert_equal(0, bufinfo.listed)
3076 call assert_equal(1, bufinfo.loaded)
3077 call assert_equal([], bufinfo.windows)
3078 call assert_equal([winid], bufinfo.popups)
3079
3080 call popup_close(winid)
3081 call assert_equal({}, popup_getpos(winid))
3082 let bufinfo = getbufinfo(buf)[0]
3083 call assert_equal(1, bufinfo.changed)
3084 call assert_equal(1, bufinfo.hidden)
3085 call assert_equal(0, bufinfo.listed)
3086 call assert_equal(1, bufinfo.loaded)
3087 call assert_equal([], bufinfo.windows)
3088 call assert_equal([], bufinfo.popups)
3089 exe 'bwipe! ' .. buf
3090endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02003091
3092func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003093 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02003094
3095 let lines =<< trim END
3096 call setline(1, range(1, 10))
3097 hi ScrollThumb ctermbg=blue
3098 hi ScrollBar ctermbg=red
3099 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003100 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003101 \ maxwidth: 10,
3102 \ maxheight: 3,
3103 \ pos : 'topleft',
3104 \ col : a:col,
3105 \ line : a:line,
3106 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02003107 \ })
3108 endfunc
3109 call PopupMenu(['x'], 1, 1)
3110 call PopupMenu(['123456789|'], 1, 16)
3111 call PopupMenu(['123456789|' .. ' '], 7, 1)
3112 call PopupMenu([repeat('123456789|', 100)], 7, 16)
3113 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
3114 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003115 call writefile(lines, 'XtestPopupMenuMaxWidth', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003116 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02003117 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
3118
3119 " close the menu popupwin.
3120 call term_sendkeys(buf, " ")
3121 call term_sendkeys(buf, " ")
3122 call term_sendkeys(buf, " ")
3123 call term_sendkeys(buf, " ")
3124 call term_sendkeys(buf, " ")
3125
3126 " clean up
3127 call StopVimInTerminal(buf)
Bram Moolenaar017c2692019-07-13 14:17:51 +02003128endfunc
3129
Bram Moolenaara901a372019-07-13 16:38:50 +02003130func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003131 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02003132
3133 let lines =<< trim END
3134 call setline(1, range(1, 20))
3135 hi ScrollThumb ctermbg=blue
3136 hi ScrollBar ctermbg=red
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003137 eval ['one', 'two', 'three', 'four', 'five',
3138 \ 'six', 'seven', 'eight', 'nine']
3139 \ ->popup_menu(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003140 \ minwidth: 8,
3141 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02003142 \ })
3143 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003144 call writefile(lines, 'XtestPopupMenuScroll', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003145 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02003146
3147 call term_sendkeys(buf, "j")
3148 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
3149
3150 call term_sendkeys(buf, "jjj")
3151 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
3152
Christian Brabandtbadeedd2023-08-13 19:25:28 +02003153 " the cursor wraps around at the bottom
Bram Moolenaara901a372019-07-13 16:38:50 +02003154 call term_sendkeys(buf, repeat("j", 20))
3155 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
3156
Christian Brabandtbadeedd2023-08-13 19:25:28 +02003157 " if the cursor is again at the bottom line
3158 call term_sendkeys(buf, repeat("j", 2))
3159 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3a', {})
3160
Bram Moolenaara901a372019-07-13 16:38:50 +02003161 call term_sendkeys(buf, "kk")
3162 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
3163
3164 call term_sendkeys(buf, "k")
3165 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
3166
Christian Brabandtbadeedd2023-08-13 19:25:28 +02003167 " the cursor wraps around at the top
Bram Moolenaara901a372019-07-13 16:38:50 +02003168 call term_sendkeys(buf, repeat("k", 20))
3169 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
3170
Christian Brabandtbadeedd2023-08-13 19:25:28 +02003171 " the cursor at the top of the window again
3172 call term_sendkeys(buf, repeat("k", 3))
3173 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6a', {})
3174
Bram Moolenaara901a372019-07-13 16:38:50 +02003175 " close the menu popupwin.
3176 call term_sendkeys(buf, " ")
3177
3178 " clean up
3179 call StopVimInTerminal(buf)
Bram Moolenaara901a372019-07-13 16:38:50 +02003180endfunc
3181
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003182func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003183 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003184
3185 let lines =<< trim END
3186 function! MyFilter(winid, key) abort
3187 if a:key == "0"
3188 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
3189 return 1
3190 endif
3191 if a:key == "G"
3192 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
3193 return 1
3194 endif
3195 if a:key == "j"
3196 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
3197 return 1
3198 endif
3199 if a:key == "k"
3200 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
3201 return 1
3202 endif
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02003203 if a:key == ':'
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003204 call popup_close(a:winid)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02003205 return 0
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003206 endif
3207 return 0
3208 endfunction
3209 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
3210 \ maxheight : 3,
3211 \ filter : 'MyFilter'
3212 \ })
3213 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003214 call writefile(lines, 'XtestPopupMenuFilter', 'D')
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003215 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
3216
3217 call term_sendkeys(buf, "j")
3218 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
3219
3220 call term_sendkeys(buf, "k")
3221 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
3222
3223 call term_sendkeys(buf, "G")
3224 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
3225
3226 call term_sendkeys(buf, "0")
3227 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
3228
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02003229 " check that when the popup is closed in the filter the screen is redrawn
3230 call term_sendkeys(buf, ":")
3231 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_5', {})
3232 call term_sendkeys(buf, "\<CR>")
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003233
3234 " clean up
3235 call StopVimInTerminal(buf)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003236endfunc
3237
3238func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003239 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003240
3241 let winid = popup_create('some text', {})
3242 call assert_equal(0, popup_getoptions(winid).cursorline)
3243 call popup_close(winid)
3244
3245 let winid = popup_create('some text', #{ cursorline: 1, })
3246 call assert_equal(1, popup_getoptions(winid).cursorline)
3247 call popup_close(winid)
3248
Bram Moolenaar6bfc4752021-02-21 23:12:18 +01003249 let winid = popup_create('some text', #{ cursorline: v:true, })
3250 call assert_equal(1, popup_getoptions(winid).cursorline)
3251 call popup_close(winid)
3252
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003253 let winid = popup_create('some text', #{ cursorline: 0, })
3254 call assert_equal(0, popup_getoptions(winid).cursorline)
3255 call popup_close(winid)
3256
3257 let winid = popup_menu('some text', {})
3258 call assert_equal(1, popup_getoptions(winid).cursorline)
3259 call popup_close(winid)
3260
3261 let winid = popup_menu('some text', #{ cursorline: 1, })
3262 call assert_equal(1, popup_getoptions(winid).cursorline)
3263 call popup_close(winid)
3264
3265 let winid = popup_menu('some text', #{ cursorline: 0, })
3266 call assert_equal(0, popup_getoptions(winid).cursorline)
3267 call popup_close(winid)
3268
3269 " ---------
3270 " Pattern 1
3271 " ---------
3272 let lines =<< trim END
3273 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
3274 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003275 call writefile(lines, 'XtestPopupCursorLine', 'D')
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003276 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3277 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
3278 call term_sendkeys(buf, ":call popup_clear()\<cr>")
3279 call StopVimInTerminal(buf)
3280
3281 " ---------
3282 " Pattern 2
3283 " ---------
3284 let lines =<< trim END
3285 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
3286 END
3287 call writefile(lines, 'XtestPopupCursorLine')
3288 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3289 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
3290 call term_sendkeys(buf, ":call popup_clear()\<cr>")
3291 call StopVimInTerminal(buf)
3292
3293 " ---------
3294 " Pattern 3
3295 " ---------
3296 let lines =<< trim END
3297 function! MyFilter(winid, key) abort
3298 if a:key == "j"
3299 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
3300 return 1
3301 endif
3302 if a:key == 'x'
3303 call popup_close(a:winid)
3304 return 1
3305 endif
3306 return 0
3307 endfunction
3308 call popup_menu(['111', '222', '333'], #{
3309 \ cursorline : 0,
3310 \ maxheight : 2,
3311 \ filter : 'MyFilter',
3312 \ })
3313 END
3314 call writefile(lines, 'XtestPopupCursorLine')
3315 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3316 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
3317 call term_sendkeys(buf, "j")
3318 call term_sendkeys(buf, "j")
3319 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
3320 call term_sendkeys(buf, "x")
3321 call StopVimInTerminal(buf)
3322
3323 " ---------
3324 " Pattern 4
3325 " ---------
3326 let lines =<< trim END
3327 function! MyFilter(winid, key) abort
3328 if a:key == "j"
3329 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
3330 return 1
3331 endif
3332 if a:key == 'x'
3333 call popup_close(a:winid)
3334 return 1
3335 endif
3336 return 0
3337 endfunction
3338 call popup_menu(['111', '222', '333'], #{
3339 \ cursorline : 1,
3340 \ maxheight : 2,
3341 \ filter : 'MyFilter',
3342 \ })
3343 END
3344 call writefile(lines, 'XtestPopupCursorLine')
3345 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3346 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
3347 call term_sendkeys(buf, "j")
3348 call term_sendkeys(buf, "j")
3349 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
3350 call term_sendkeys(buf, "x")
3351 call StopVimInTerminal(buf)
3352
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003353 " ---------
3354 " Cursor in second line when creating the popup
3355 " ---------
3356 let lines =<< trim END
3357 let winid = popup_create(['111', '222', '333'], #{
3358 \ cursorline : 1,
3359 \ })
3360 call win_execute(winid, "2")
3361 END
3362 call writefile(lines, 'XtestPopupCursorLine')
3363 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3364 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_7', {})
3365 call StopVimInTerminal(buf)
3366
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01003367 " ---------
3368 " Use current buffer for popupmenu
3369 " ---------
3370 let lines =<< trim END
3371 call setline(1, ['one', 'two', 'three'])
3372 let winid = popup_create(bufnr('%'), #{
3373 \ cursorline : 1,
3374 \ })
3375 call win_execute(winid, "2")
3376 END
3377 call writefile(lines, 'XtestPopupCursorLine')
3378 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3379 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_8', {})
3380 call StopVimInTerminal(buf)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003381endfunc
3382
Bram Moolenaar6bfc4752021-02-21 23:12:18 +01003383def Test_popup_cursorline_vim9()
3384 var winid = popup_create('some text', { cursorline: true, })
3385 assert_equal(1, popup_getoptions(winid).cursorline)
3386 popup_close(winid)
3387
3388 assert_fails("popup_create('some text', { cursorline: 2, })", 'E1023:')
3389 popup_clear()
3390enddef
3391
Bram Moolenaarf914a332019-07-20 15:09:56 +02003392func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003393 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003394 CheckFeature quickfix
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003395
Bram Moolenaarf914a332019-07-20 15:09:56 +02003396 call writefile([
3397 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
3398 \ "another\tXtagfile\t/^this is another",
3399 \ "theword\tXtagfile\t/^theword"],
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003400 \ 'Xtags', 'D')
Bram Moolenaarf914a332019-07-20 15:09:56 +02003401 call writefile(range(1,20)
3402 \ + ['theword is here']
3403 \ + range(22, 27)
3404 \ + ['this is another place']
3405 \ + range(29, 40),
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003406 \ "Xtagfile", 'D')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003407 call writefile(range(1,10)
3408 \ + ['searched word is here']
3409 \ + range(12, 20),
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003410 \ "Xheader.h", 'D')
Bram Moolenaarf914a332019-07-20 15:09:56 +02003411 let lines =<< trim END
3412 set tags=Xtags
3413 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003414 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003415 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003416 \ 'three',
3417 \ 'four',
3418 \ 'five',
3419 \ 'six',
3420 \ 'seven',
3421 \ 'find theword somewhere',
3422 \ 'nine',
3423 \ 'this is another word',
3424 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02003425 set previewpopup=height:4,width:40
Bram Moolenaar60577482021-03-04 21:35:07 +01003426 hi OtherColor ctermbg=lightcyan guibg=lightcyan
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003427 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02003428 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003429 call writefile(lines, 'XtestPreviewPopup', 'D')
Bram Moolenaarf914a332019-07-20 15:09:56 +02003430 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
3431
3432 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
3433 call term_sendkeys(buf, ":\<CR>")
3434 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
3435
Bram Moolenaar60577482021-03-04 21:35:07 +01003436 call term_sendkeys(buf, ":set previewpopup+=highlight:OtherColor\<CR>")
Bram Moolenaarf914a332019-07-20 15:09:56 +02003437 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
3438 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
3439
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003440 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003441 call term_sendkeys(buf, ":\<CR>")
3442 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
3443
3444 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
3445 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
3446
Bram Moolenaar799439a2020-02-11 21:44:17 +01003447 call term_sendkeys(buf, ":silent cd ..\<CR>:\<CR>")
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003448 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar799439a2020-02-11 21:44:17 +01003449 call term_sendkeys(buf, ":silent cd testdir\<CR>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003450
Bram Moolenaar60577482021-03-04 21:35:07 +01003451 call term_sendkeys(buf, ":set previewpopup-=highlight:OtherColor\<CR>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003452 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02003453 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003454 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
3455
3456 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
3457 call term_sendkeys(buf, ":\<CR>")
3458 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
3459
3460 call term_sendkeys(buf, ":pclose\<CR>")
3461 call term_sendkeys(buf, ":psearch searched\<CR>")
3462 call term_sendkeys(buf, ":\<CR>")
3463 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003464
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01003465 call term_sendkeys(buf, "\<C-W>p")
3466 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_9', {})
3467
3468 call term_sendkeys(buf, ":call win_execute(popup_findpreview(), 'call popup_clear()')\<CR>")
3469 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_10', {})
3470
Bram Moolenaarf914a332019-07-20 15:09:56 +02003471 call StopVimInTerminal(buf)
Bram Moolenaarf914a332019-07-20 15:09:56 +02003472endfunc
3473
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01003474func s:run_preview_popuppum(preview_lines, dumpfile_name)
Bram Moolenaar393f8d62022-10-02 14:28:30 +01003475 CheckScreendump
3476 CheckFeature quickfix
3477
3478 let lines =<< trim END
3479 let a = 3
3480 let b = 1
3481 echo a
3482 echo b
3483 call system('echo hello')
3484 " the end
3485 END
3486 call writefile(lines, 'XpreviewText.vim', 'D')
3487
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01003488 call writefile(a:preview_lines, 'XtestPreviewPum', 'D')
Bram Moolenaar393f8d62022-10-02 14:28:30 +01003489 let buf = RunVimInTerminal('-S XtestPreviewPum', #{rows: 12})
3490
3491 call term_sendkeys(buf, "A o\<C-N>")
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01003492 call VerifyScreenDump(buf, 'Test_pum_preview_' . a:dumpfile_name . '_1', {})
Bram Moolenaar393f8d62022-10-02 14:28:30 +01003493
3494 call term_sendkeys(buf, "\<C-N>")
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01003495 call VerifyScreenDump(buf, 'Test_pum_preview_' . a:dumpfile_name . '_2', {})
Bram Moolenaar393f8d62022-10-02 14:28:30 +01003496
3497 call term_sendkeys(buf, "\<C-N>")
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01003498 call VerifyScreenDump(buf, 'Test_pum_preview_' . a:dumpfile_name . '_3', {})
Bram Moolenaar393f8d62022-10-02 14:28:30 +01003499
3500 call term_sendkeys(buf, "\<C-N>")
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01003501 call VerifyScreenDump(buf, 'Test_pum_preview_' . a:dumpfile_name . '_4', {})
Bram Moolenaar393f8d62022-10-02 14:28:30 +01003502
3503 call term_sendkeys(buf, "\<Esc>")
3504 call StopVimInTerminal(buf)
3505endfunc
3506
Yinzuo Jianga2a2fe82024-12-16 21:22:09 +01003507func Test_previewpopup_pum_pedit()
3508 let lines =<< trim END
3509 call setline(1, ['one', 'two', 'three', 'other', 'once', 'only', 'off'])
3510 set previewpopup=height:6,width:40
3511 pedit XpreviewText.vim
3512 END
3513 call s:run_preview_popuppum(lines, 'pedit')
3514endfunc
3515
3516func Test_previewpopup_pum_pbuffer()
3517 let lines =<< trim END
3518 call setline(1, ['one', 'two', 'three', 'other', 'once', 'only', 'off'])
3519 set previewpopup=height:6,width:40
3520 badd XpreviewText.vim
3521 exe bufnr('$') . 'pbuffer'
3522 END
3523 call s:run_preview_popuppum(lines, 'pbuffer')
3524endfunc
Bram Moolenaar393f8d62022-10-02 14:28:30 +01003525
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003526func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003527 let lines =<< trim END
3528 set completeopt+=preview,popup
3529 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02003530 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003531
3532 func CompleteFuncDict(findstart, base)
3533 if a:findstart
3534 if col('.') > 10
3535 return col('.') - 10
3536 endif
3537 return 0
3538 endif
3539
3540 return {
3541 \ 'words': [
3542 \ {
3543 \ 'word': 'aword',
3544 \ 'abbr': 'wrd',
3545 \ 'menu': 'extra text',
3546 \ 'info': 'words are cool',
3547 \ 'kind': 'W',
3548 \ 'user_data': 'test'
3549 \ },
3550 \ {
3551 \ 'word': 'anotherword',
3552 \ 'abbr': 'anotwrd',
3553 \ 'menu': 'extra text',
3554 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
3555 \ 'kind': 'W',
3556 \ 'user_data': 'notest'
3557 \ },
3558 \ {
3559 \ 'word': 'noinfo',
3560 \ 'abbr': 'noawrd',
3561 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02003562 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003563 \ 'kind': 'W',
3564 \ 'user_data': 'notest'
3565 \ },
3566 \ {
3567 \ 'word': 'thatword',
3568 \ 'abbr': 'thatwrd',
3569 \ 'menu': 'extra text',
3570 \ 'info': 'that word is cool',
3571 \ 'kind': 'W',
3572 \ 'user_data': 'notest'
3573 \ },
3574 \ ]
3575 \ }
3576 endfunc
3577 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003578 func ChangeColor()
3579 let id = popup_findinfo()
Bram Moolenaard356fc62020-12-09 18:13:44 +01003580 if buflisted(winbufnr(id))
3581 call setline(1, 'buffer is listed')
3582 endif
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003583 eval id->popup_setoptions(#{highlight: 'InfoPopup'})
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003584 endfunc
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003585
3586 func InfoHidden()
3587 set completepopup=height:4,border:off,align:menu
3588 set completeopt-=popup completeopt+=popuphidden
3589 au CompleteChanged * call HandleChange()
3590 endfunc
3591
3592 let s:counter = 0
3593 func HandleChange()
3594 let s:counter += 1
3595 let selected = complete_info(['selected']).selected
3596 if selected <= 0
3597 " First time: do nothing, info remains hidden
3598 return
3599 endif
3600 if selected == 1
3601 " Second time: show info right away
3602 let id = popup_findinfo()
3603 if id
3604 call popup_settext(id, 'immediate info ' .. s:counter)
3605 call popup_show(id)
3606 endif
3607 else
3608 " Third time: show info after a short delay
3609 call timer_start(100, 'ShowInfo')
3610 endif
3611 endfunc
3612
3613 func ShowInfo(...)
3614 let id = popup_findinfo()
3615 if id
3616 call popup_settext(id, 'async info ' .. s:counter)
3617 call popup_show(id)
3618 endif
3619 endfunc
Bram Moolenaar2dfae042020-11-15 14:09:37 +01003620
Bakudankun65555002021-11-17 20:40:16 +00003621 func OpenOtherPopups()
3622 call popup_create([
3623 \ 'popup below',
3624 \ 'popup below',
3625 \ 'popup below',
3626 \ 'popup below',
3627 \ ], #{
3628 \ line: 'cursor',
3629 \ col: 'cursor+3',
3630 \ highlight: 'ErrorMsg',
3631 \ minwidth: 17,
3632 \ zindex: 50,
3633 \ })
3634 call popup_create([
3635 \ 'popup on top',
3636 \ 'popup on top',
3637 \ 'popup on top',
3638 \ ], #{
3639 \ line: 'cursor+3',
3640 \ col: 'cursor-10',
3641 \ highlight: 'Search',
3642 \ minwidth: 10,
3643 \ zindex: 200,
3644 \ })
3645 endfunc
3646
Bram Moolenaar2dfae042020-11-15 14:09:37 +01003647 " Check that no autocommands are triggered for the info popup
3648 au WinEnter * if win_gettype() == 'popup' | call setline(2, 'WinEnter') | endif
3649 au WinLeave * if win_gettype() == 'popup' | call setline(2, 'WinLeave') | endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003650 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003651 return lines
3652endfunc
3653
3654func Test_popupmenu_info_border()
3655 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003656 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003657
3658 let lines = Get_popupmenu_lines()
3659 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003660 call writefile(lines, 'XtestInfoPopup', 'D')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003661
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003662 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003663 call TermWait(buf, 25)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003664
3665 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3666 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
3667
3668 call term_sendkeys(buf, "\<C-N>")
3669 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
3670
3671 call term_sendkeys(buf, "\<C-N>")
3672 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
3673
3674 call term_sendkeys(buf, "\<C-N>\<C-N>")
3675 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
3676
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02003677 " info on the left with scrollbar
3678 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
3679 call term_sendkeys(buf, "\<C-N>\<C-N>")
3680 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
3681
Bram Moolenaar202c3f72019-11-21 12:12:35 +01003682 " Test that the popupmenu's scrollbar and infopopup do not overlap
3683 call term_sendkeys(buf, "\<Esc>")
3684 call term_sendkeys(buf, ":set pumheight=3\<CR>")
3685 call term_sendkeys(buf, "cc\<C-X>\<C-U>")
3686 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_6', {})
3687
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003688 " Hide the info popup, cycle through buffers, make sure it didn't get
Bram Moolenaarca7c0782020-01-14 20:42:48 +01003689 " deleted.
3690 call term_sendkeys(buf, "\<Esc>")
3691 call term_sendkeys(buf, ":set hidden\<CR>")
3692 call term_sendkeys(buf, ":bn\<CR>")
3693 call term_sendkeys(buf, ":bn\<CR>")
3694 call term_sendkeys(buf, "otest text test text\<C-X>\<C-U>")
3695 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_7', {})
3696
Bram Moolenaar447bfba2020-07-18 16:07:16 +02003697 " Test that when the option is changed the popup changes.
3698 call term_sendkeys(buf, "\<Esc>")
3699 call term_sendkeys(buf, ":set completepopup=border:off\<CR>")
3700 call term_sendkeys(buf, "a\<C-X>\<C-U>")
3701 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_8', {})
3702
Bram Moolenaar6d585f42020-07-26 22:20:54 +02003703 call term_sendkeys(buf, " \<Esc>")
3704 call term_sendkeys(buf, ":set completepopup+=width:10\<CR>")
3705 call term_sendkeys(buf, "a\<C-X>\<C-U>")
3706 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_9', {})
3707
Bram Moolenaarca7c0782020-01-14 20:42:48 +01003708 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003709 call StopVimInTerminal(buf)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003710endfunc
3711
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003712func Test_popupmenu_info_noborder()
3713 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003714 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003715
3716 let lines = Get_popupmenu_lines()
3717 call add(lines, 'set completepopup=height:4,border:off')
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003718 call writefile(lines, 'XtestInfoPopupNb', 'D')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003719
3720 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003721 call TermWait(buf, 25)
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003722
3723 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3724 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
3725
3726 call StopVimInTerminal(buf)
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003727endfunc
3728
Bram Moolenaar258cef52019-08-21 17:29:29 +02003729func Test_popupmenu_info_align_menu()
3730 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003731 CheckFeature quickfix
Bram Moolenaar258cef52019-08-21 17:29:29 +02003732
3733 let lines = Get_popupmenu_lines()
3734 call add(lines, 'set completepopup=height:4,border:off,align:menu')
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003735 call writefile(lines, 'XtestInfoPopupNb', 'D')
Bram Moolenaar258cef52019-08-21 17:29:29 +02003736
3737 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003738 call TermWait(buf, 25)
Bram Moolenaar258cef52019-08-21 17:29:29 +02003739
3740 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3741 call term_sendkeys(buf, "\<C-N>")
3742 call term_sendkeys(buf, "\<C-N>")
3743 call term_sendkeys(buf, "\<C-N>")
3744 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
3745
3746 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
3747 call term_sendkeys(buf, "\<C-N>")
3748 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
3749
3750 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003751 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02003752 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
3753 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
3754 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
3755
3756 call StopVimInTerminal(buf)
Bram Moolenaar258cef52019-08-21 17:29:29 +02003757endfunc
3758
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003759func Test_popupmenu_info_hidden()
3760 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003761 CheckFeature quickfix
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003762
3763 let lines = Get_popupmenu_lines()
3764 call add(lines, 'call InfoHidden()')
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003765 call writefile(lines, 'XtestInfoPopupHidden', 'D')
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003766
3767 let buf = RunVimInTerminal('-S XtestInfoPopupHidden', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003768 call TermWait(buf, 25)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003769
3770 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3771 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_1', {})
3772
3773 call term_sendkeys(buf, "\<C-N>")
3774 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_2', {})
3775
3776 call term_sendkeys(buf, "\<C-N>")
3777 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_3', {})
3778
3779 call term_sendkeys(buf, "\<Esc>")
3780 call StopVimInTerminal(buf)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003781endfunc
3782
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003783func Test_popupmenu_info_too_wide()
3784 CheckScreendump
3785 CheckFeature quickfix
3786
3787 let lines =<< trim END
3788 call setline(1, range(10))
3789
3790 set completeopt+=preview,popup
3791 set completepopup=align:menu
3792 set omnifunc=OmniFunc
3793 hi InfoPopup ctermbg=lightgrey
3794
3795 func OmniFunc(findstart, base)
3796 if a:findstart
3797 return 0
3798 endif
3799
3800 let menuText = 'some long text to make sure the menu takes up all of the width of the window'
3801 return #{
Bram Moolenaar755bf2b2023-01-28 19:38:49 +00003802 \ words: [
3803 \ #{
3804 \ word: 'scrap',
3805 \ menu: menuText,
3806 \ info: "other words are\ncooler than this and some more text\nto make wrap",
3807 \ },
3808 \ #{
3809 \ word: 'scappier',
3810 \ menu: menuText,
3811 \ info: 'words are cool',
3812 \ },
3813 \ #{
3814 \ word: 'scrappier2',
3815 \ menu: menuText,
3816 \ info: 'words are cool',
3817 \ },
3818 \ ]
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003819 \ }
3820 endfunc
3821 END
3822
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003823 call writefile(lines, 'XtestInfoPopupWide', 'D')
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003824 let buf = RunVimInTerminal('-S XtestInfoPopupWide', #{rows: 8})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003825 call TermWait(buf, 25)
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003826
3827 call term_sendkeys(buf, "Ascr\<C-X>\<C-O>")
3828 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_wide_1', {})
3829
3830 call term_sendkeys(buf, "\<Esc>")
3831 call StopVimInTerminal(buf)
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003832endfunc
3833
Bakudankun65555002021-11-17 20:40:16 +00003834func Test_popupmenu_masking()
3835 " Test that popup windows that are opened while popup menu is open are
3836 " properly displayed.
3837 CheckScreendump
3838 CheckFeature quickfix
3839
3840 let lines = Get_popupmenu_lines()
3841 call add(lines, 'inoremap <C-A> <Cmd>call OpenOtherPopups()<CR>')
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003842 call writefile(lines, 'XtestPopupmenuMasking', 'D')
Bakudankun65555002021-11-17 20:40:16 +00003843
3844 let buf = RunVimInTerminal('-S XtestPopupmenuMasking', #{rows: 14})
3845 call TermWait(buf, 25)
3846
Bram Moolenaar9f145572022-11-27 12:45:41 +00003847 call term_sendkeys(buf, "A" .. GetEscCodeWithModifier('C', 'X')
3848 \ .. GetEscCodeWithModifier('C', 'U')
3849 \ .. GetEscCodeWithModifier('C', 'A'))
Bakudankun65555002021-11-17 20:40:16 +00003850 call VerifyScreenDump(buf, 'Test_popupwin_popupmenu_masking_1', {})
3851
3852 call term_sendkeys(buf, "\<Esc>")
3853 call VerifyScreenDump(buf, 'Test_popupwin_popupmenu_masking_2', {})
3854
3855 call StopVimInTerminal(buf)
Bakudankun65555002021-11-17 20:40:16 +00003856endfunc
3857
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003858func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02003859 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003860 call popup_clear()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003861 let winid = 'nothing wrong'->popup_notification({})
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003862 call assert_equal(bufnr, winbufnr(winid))
3863 call popup_clear()
3864endfunc
3865
Bram Moolenaar1824f452019-10-02 23:06:46 +02003866func Test_popupwin_getoptions_tablocal()
3867 topleft split
3868 let win1 = popup_create('nothing', #{maxheight: 8})
3869 let win2 = popup_create('something', #{maxheight: 10})
3870 let win3 = popup_create('something', #{maxheight: 15})
3871 call assert_equal(8, popup_getoptions(win1).maxheight)
3872 call assert_equal(10, popup_getoptions(win2).maxheight)
3873 call assert_equal(15, popup_getoptions(win3).maxheight)
3874 call popup_clear()
3875 quit
3876endfunc
3877
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02003878func Test_popupwin_cancel()
3879 let win1 = popup_create('one', #{line: 5, filter: {... -> 0}})
3880 let win2 = popup_create('two', #{line: 10, filter: {... -> 0}})
3881 let win3 = popup_create('three', #{line: 15, filter: {... -> 0}})
3882 call assert_equal(5, popup_getpos(win1).line)
3883 call assert_equal(10, popup_getpos(win2).line)
3884 call assert_equal(15, popup_getpos(win3).line)
3885 " TODO: this also works without patch 8.1.2110
3886 call feedkeys("\<C-C>", 'xt')
3887 call assert_equal(5, popup_getpos(win1).line)
3888 call assert_equal(10, popup_getpos(win2).line)
3889 call assert_equal({}, popup_getpos(win3))
3890 call feedkeys("\<C-C>", 'xt')
3891 call assert_equal(5, popup_getpos(win1).line)
3892 call assert_equal({}, popup_getpos(win2))
3893 call assert_equal({}, popup_getpos(win3))
3894 call feedkeys("\<C-C>", 'xt')
3895 call assert_equal({}, popup_getpos(win1))
3896 call assert_equal({}, popup_getpos(win2))
3897 call assert_equal({}, popup_getpos(win3))
3898endfunc
3899
glepnir8a635292025-03-21 18:12:32 +01003900func Test_popupwin_cancel_with_without_filter()
3901 let win1 = popup_create('with filter', #{line: 5, filter: {... -> 0}})
3902 let win2 = popup_create('no filter', #{line: 10})
3903
3904 call assert_equal(5, popup_getpos(win1).line)
3905 call assert_equal(10, popup_getpos(win2).line)
3906
3907 call feedkeys("\<C-C>", 'xt')
3908 call assert_equal({}, popup_getpos(win1))
3909 call assert_equal(10, popup_getpos(win2).line)
3910
3911 call feedkeys("\<C-C>", 'xt')
3912 call assert_equal({}, popup_getpos(win1))
3913 call assert_equal({}, popup_getpos(win2))
3914
3915 call popup_clear()
3916endfunc
3917
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003918func Test_popupwin_filter_redraw()
3919 " Create two popups with a filter that closes the popup when typing "0".
3920 " Both popups should close, even though the redraw also calls
3921 " popup_reset_handled()
3922
3923 func CloseFilter(winid, key)
3924 if a:key == '0'
3925 call popup_close(a:winid)
3926 redraw
3927 endif
3928 return 0 " pass the key
3929 endfunc
3930
3931 let id1 = popup_create('first one', #{
3932 \ line: 1,
3933 \ col: 1,
3934 \ filter: 'CloseFilter',
3935 \ })
3936 let id2 = popup_create('second one', #{
3937 \ line: 9,
3938 \ col: 1,
3939 \ filter: 'CloseFilter',
3940 \ })
3941 call assert_equal(1, popup_getpos(id1).line)
3942 call assert_equal(9, popup_getpos(id2).line)
3943
3944 call feedkeys('0', 'xt')
3945 call assert_equal({}, popup_getpos(id1))
3946 call assert_equal({}, popup_getpos(id2))
3947
3948 call popup_clear()
3949 delfunc CloseFilter
3950endfunc
3951
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003952func Test_popupwin_double_width()
3953 CheckScreendump
3954
3955 let lines =<< trim END
3956 call setline(1, 'x你好世界你好世你好世界你好')
3957 call setline(2, '你好世界你好世你好世界你好')
3958 call setline(3, 'x你好世界你好世你好世界你好')
3959 call popup_create('你好,世界 - 你好,世界xxxxx', #{line: 1, col: 3, maxwidth: 14})
3960 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003961 call writefile(lines, 'XtestPopupWide', 'D')
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003962
3963 let buf = RunVimInTerminal('-S XtestPopupWide', #{rows: 10})
3964 call VerifyScreenDump(buf, 'Test_popupwin_doublewidth_1', {})
3965
3966 call StopVimInTerminal(buf)
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003967endfunc
3968
3969func Test_popupwin_sign()
3970 CheckScreendump
3971
3972 let lines =<< trim END
3973 call setline(1, range(10))
3974 call sign_define('Current', {
3975 \ 'text': '>>',
3976 \ 'texthl': 'WarningMsg',
3977 \ 'linehl': 'Error',
3978 \ })
3979 call sign_define('Other', {
3980 \ 'text': '#!',
3981 \ 'texthl': 'Error',
3982 \ 'linehl': 'Search',
3983 \ })
3984 let winid = popup_create(['hello', 'bright', 'world'], {
3985 \ 'minwidth': 20,
3986 \ })
3987 call setwinvar(winid, "&signcolumn", "yes")
3988 let winbufnr = winbufnr(winid)
3989
3990 " add sign to current buffer, shows
3991 call sign_place(1, 'Selected', 'Current', bufnr('%'), {'lnum': 1})
3992 " add sign to current buffer, does not show
3993 call sign_place(2, 'PopUpSelected', 'Other', bufnr('%'), {'lnum': 2})
3994
3995 " add sign to popup buffer, shows
3996 call sign_place(3, 'PopUpSelected', 'Other', winbufnr, {'lnum': 1})
3997 " add sign to popup buffer, does not show
3998 call sign_place(4, 'Selected', 'Current', winbufnr, {'lnum': 2})
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02003999
4000 func SetOptions()
4001 call setwinvar(g:winid, '&number', 1)
4002 call setwinvar(g:winid, '&foldcolumn', 2)
4003 call popup_settext(g:winid, 'a longer line to check the width')
4004 endfunc
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01004005 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01004006 call writefile(lines, 'XtestPopupSign', 'D')
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01004007
4008 let buf = RunVimInTerminal('-S XtestPopupSign', #{rows: 10})
4009 call VerifyScreenDump(buf, 'Test_popupwin_sign_1', {})
4010
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02004011 " set more options to check the width is adjusted
4012 call term_sendkeys(buf, ":call SetOptions()\<CR>")
4013 call VerifyScreenDump(buf, 'Test_popupwin_sign_2', {})
4014
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01004015 call StopVimInTerminal(buf)
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01004016endfunc
4017
Bram Moolenaar99ebf222019-12-10 23:44:48 +01004018func Test_popupwin_bufnr()
4019 let popwin = popup_create(['blah'], #{})
4020 let popbuf = winbufnr(popwin)
4021 split asdfasdf
4022 let newbuf = bufnr()
4023 call assert_true(newbuf > popbuf, 'New buffer number is higher')
4024 call assert_equal(newbuf, bufnr('$'))
4025 call popup_clear()
4026 let popwin = popup_create(['blah'], #{})
4027 " reuses previous buffer number
4028 call assert_equal(popbuf, winbufnr(popwin))
4029 call assert_equal(newbuf, bufnr('$'))
4030
4031 call popup_clear()
4032 bwipe!
4033endfunc
4034
Bram Moolenaarec084d32020-02-28 22:44:47 +01004035func Test_popupwin_filter_input_multibyte()
4036 func MyPopupFilter(winid, c)
4037 let g:bytes = range(a:c->strlen())->map({i -> char2nr(a:c[i])})
4038 return 0
4039 endfunc
4040 let winid = popup_create('', #{mapping: 0, filter: 'MyPopupFilter'})
4041
4042 " UTF-8: E3 80 80, including K_SPECIAL(0x80)
4043 call feedkeys("\u3000", 'xt')
4044 call assert_equal([0xe3, 0x80, 0x80], g:bytes)
4045
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01004046 " UTF-8: E3 80 9B, including CSI(0x9B)
4047 call feedkeys("\u301b", 'xt')
4048 call assert_equal([0xe3, 0x80, 0x9b], g:bytes)
Bram Moolenaarec084d32020-02-28 22:44:47 +01004049
Bram Moolenaar20298ce2020-06-19 21:46:52 +02004050 if has('unix')
4051 " with modifyOtherKeys <M-S-a> does not include a modifier sequence
Bram Moolenaarb326edf2020-06-20 15:03:38 +02004052 if has('gui_running')
4053 call feedkeys("\x9b\xfc\x08A", 'Lx!')
4054 else
4055 call feedkeys("\<Esc>[27;4;65~", 'Lx!')
4056 endif
Bram Moolenaar20298ce2020-06-19 21:46:52 +02004057 call assert_equal([0xc3, 0x81], g:bytes)
4058 endif
4059
Bram Moolenaarec084d32020-02-28 22:44:47 +01004060 call popup_clear()
4061 delfunc MyPopupFilter
4062 unlet g:bytes
4063endfunc
4064
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02004065func Test_popupwin_filter_close_ctrl_c()
4066 CheckScreendump
4067
4068 let lines =<< trim END
4069 vsplit
4070 set laststatus=2
4071 set statusline=%!Statusline()
4072
4073 function Statusline() abort
4074 return '%<%f %h%m%r%=%-14.(%l,%c%V%) %P'
4075 endfunction
4076
4077 call popup_create('test test test test...', {'filter': {-> 0}})
4078 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01004079 call writefile(lines, 'XtestPopupCtrlC', 'D')
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02004080
4081 let buf = RunVimInTerminal('-S XtestPopupCtrlC', #{rows: 10})
4082
4083 call term_sendkeys(buf, "\<C-C>")
4084 call VerifyScreenDump(buf, 'Test_popupwin_ctrl_c', {})
4085
4086 call StopVimInTerminal(buf)
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02004087endfunc
4088
4089func Test_popupwin_filter_close_wrong_name()
4090 CheckScreendump
4091
4092 let lines =<< trim END
4093 call popup_create('one two three...', {'filter': 'NoSuchFunc'})
4094 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01004095 call writefile(lines, 'XtestPopupWrongName', 'D')
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02004096
4097 let buf = RunVimInTerminal('-S XtestPopupWrongName', #{rows: 10})
4098
4099 call term_sendkeys(buf, "j")
4100 call VerifyScreenDump(buf, 'Test_popupwin_wrong_name', {})
4101
4102 call StopVimInTerminal(buf)
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02004103endfunc
4104
4105func Test_popupwin_filter_close_three_errors()
4106 CheckScreendump
4107
4108 let lines =<< trim END
4109 set cmdheight=2
4110 call popup_create('one two three...', {'filter': 'filter'})
4111 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01004112 call writefile(lines, 'XtestPopupThreeErrors', 'D')
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02004113
4114 let buf = RunVimInTerminal('-S XtestPopupThreeErrors', #{rows: 10})
4115
4116 call term_sendkeys(buf, "jj")
4117 call VerifyScreenDump(buf, 'Test_popupwin_three_errors_1', {})
4118 call term_sendkeys(buf, "j")
4119 call VerifyScreenDump(buf, 'Test_popupwin_three_errors_2', {})
4120
4121 call StopVimInTerminal(buf)
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02004122endfunc
4123
Bram Moolenaar927495b2020-11-06 17:58:35 +01004124func Test_popupwin_latin1_encoding()
4125 CheckScreendump
4126 CheckUnix
4127
4128 " When 'encoding' is a single-byte encoding a terminal window will mess up
4129 " the display. Check that showing a popup on top of that doesn't crash.
4130 let lines =<< trim END
4131 set encoding=latin1
4132 terminal cat Xmultibyte
4133 call popup_create(['one', 'two', 'three', 'four'], #{line: 1, col: 10})
Bram Moolenaar4c5bdb92020-12-17 17:45:59 +01004134 redraw
Bram Moolenaarb125b532020-12-17 21:56:09 +01004135 " wait for "cat" to finish
4136 while execute('ls!') !~ 'finished'
4137 sleep 10m
4138 endwhile
Bram Moolenaar4c5bdb92020-12-17 17:45:59 +01004139 echo "Done"
Bram Moolenaar927495b2020-11-06 17:58:35 +01004140 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01004141 call writefile(lines, 'XtestPopupLatin', 'D')
4142 call writefile([repeat("\u3042 ", 120)], 'Xmultibyte', 'D')
Bram Moolenaar927495b2020-11-06 17:58:35 +01004143
4144 let buf = RunVimInTerminal('-S XtestPopupLatin', #{rows: 10})
Bram Moolenaar4c5bdb92020-12-17 17:45:59 +01004145 call WaitForAssert({-> assert_match('Done', term_getline(buf, 10))})
Bram Moolenaar927495b2020-11-06 17:58:35 +01004146
4147 call term_sendkeys(buf, ":q\<CR>")
4148 call StopVimInTerminal(buf)
Bram Moolenaar927495b2020-11-06 17:58:35 +01004149endfunc
4150
Bram Moolenaarba2920f2020-03-06 21:43:17 +01004151func Test_popupwin_atcursor_far_right()
4152 new
4153
4154 " this was getting stuck
4155 set signcolumn=yes
4156 call setline(1, repeat('=', &columns))
4157 normal! ggg$
Bram Moolenaaref6b9792020-05-13 16:34:15 +02004158 let winid = popup_atcursor(repeat('x', 500), #{moved: 'any', border: []})
Bram Moolenaarba2920f2020-03-06 21:43:17 +01004159
Bram Moolenaar6d585f42020-07-26 22:20:54 +02004160 " 'signcolumn' was getting reset
4161 call setwinvar(winid, '&signcolumn', 'yes')
4162 call popup_setoptions(winid, {'zindex': 1000})
4163 call assert_equal('yes', getwinvar(winid, '&signcolumn'))
4164
Bram Moolenaaref6b9792020-05-13 16:34:15 +02004165 call popup_close(winid)
Bram Moolenaarba2920f2020-03-06 21:43:17 +01004166 bwipe!
4167 set signcolumn&
4168endfunc
4169
Bram Moolenaar0f1563f2020-03-20 21:15:51 +01004170func Test_popupwin_splitmove()
4171 vsplit
4172 let win2 = win_getid()
4173 let popup_winid = popup_dialog('hello', {})
4174 call assert_fails('call win_splitmove(popup_winid, win2)', 'E957:')
4175 call assert_fails('call win_splitmove(win2, popup_winid)', 'E957:')
4176
4177 call popup_clear()
4178 bwipe
4179endfunc
4180
Bram Moolenaarcbcd9cb2020-11-07 16:58:59 +01004181func Test_popupwin_exiting_terminal()
4182 CheckFeature terminal
4183
4184 " Tests that when creating a popup right after closing a terminal window does
4185 " not make the popup the current window.
4186 let winid = win_getid()
4187 try
4188 augroup Test_popupwin_exiting_terminal
4189 autocmd!
4190 autocmd WinEnter * :call popup_create('test', {})
4191 augroup END
4192 let bnr = term_start(&shell, #{term_finish: 'close'})
4193 call term_sendkeys(bnr, "exit\r\n")
4194 call WaitForAssert({-> assert_equal(winid, win_getid())})
4195 finally
4196 call popup_clear(1)
4197 augroup Test_popupwin_exiting_terminal
4198 autocmd!
4199 augroup END
4200 endtry
4201endfunc
Bram Moolenaar0f1563f2020-03-20 21:15:51 +01004202
Bram Moolenaar014f6982021-01-04 13:18:30 +01004203func Test_popup_filter_menu()
4204 let colors = ['red', 'green', 'blue']
4205 call popup_menu(colors, #{callback: {_, result -> assert_equal('green', colors[result - 1])}})
4206 call feedkeys("\<c-n>\<c-n>\<c-p>\<cr>", 'xt')
4207endfunc
4208
Bram Moolenaar65026432021-02-06 14:22:32 +01004209func Test_popup_getoptions_other_tab()
4210 new
4211 call setline(1, 'some text')
4212 call prop_type_add('textprop', {})
4213 call prop_add(1, 1, #{type: 'textprop', length: 1})
4214 let id = popup_create('TEST', #{textprop: 'textprop', highlight: 'ErrorMsg', tabpage: 1})
4215 tab sp
4216 call assert_equal(['textprop', 'textpropid', 'textpropwin'], popup_getoptions(id)->keys()->filter({_, v -> v =~ 'textprop'}))
4217
4218 tabclose
Bram Moolenaarefa19232021-02-06 14:59:27 +01004219 call popup_close(id)
Bram Moolenaar65026432021-02-06 14:22:32 +01004220 bwipe!
4221 call prop_type_delete('textprop')
4222endfunc
4223
Bram Moolenaarefa19232021-02-06 14:59:27 +01004224
4225func Test_popup_setoptions_other_tab()
Bram Moolenaarb18b4962022-09-02 21:55:50 +01004226 new Xpotfile
Bram Moolenaarefa19232021-02-06 14:59:27 +01004227 let winid = win_getid()
4228 call setline(1, 'some text')
4229 call prop_type_add('textprop', {})
4230 call prop_add(1, 1, #{type: 'textprop', length: 1})
4231 let id = popup_create('TEST', #{textprop: 'textprop'})
4232 tab sp
4233 call popup_setoptions(id, #{textprop: 'textprop', textpropwin: winid})
4234 call assert_equal(winid, popup_getoptions(id).textpropwin)
4235
4236 tabclose
4237 call popup_close(id)
Bram Moolenaarb18b4962022-09-02 21:55:50 +01004238 bwipe! Xpotfile
Bram Moolenaarefa19232021-02-06 14:59:27 +01004239 call prop_type_delete('textprop')
4240endfunc
4241
Bram Moolenaar82db31c2021-02-10 14:56:11 +01004242func Test_popup_prop_not_visible()
4243 CheckScreendump
4244
4245 let lines =<< trim END
4246 vim9script
4247 set nowrap stal=2
4248 rightbelow :31vnew
4249 setline(1, ['', 'some text', '', 'other text'])
4250 prop_type_add('someprop', {})
4251 prop_add(2, 9, {type: 'someprop', length: 5})
Bram Moolenaar27724252022-05-08 15:00:04 +01004252 g:some_id = popup_create('attached to "some"', {
Bram Moolenaar82db31c2021-02-10 14:56:11 +01004253 textprop: 'someprop',
4254 highlight: 'ErrorMsg',
4255 line: -1,
4256 wrap: false,
4257 fixed: true,
4258 })
4259 prop_type_add('otherprop', {})
4260 prop_add(4, 10, {type: 'otherprop', length: 5})
4261 popup_create('attached to "other"', {
4262 textprop: 'otherprop',
4263 highlight: 'ErrorMsg',
4264 line: -1,
4265 wrap: false,
4266 fixed: false,
4267 })
4268 END
dundargocc57b5bc2022-11-02 13:30:51 +00004269 call writefile(lines, 'XtestPropNotVisible', 'D')
4270 let buf = RunVimInTerminal('-S XtestPropNotVisible', #{rows: 10})
Bram Moolenaar82db31c2021-02-10 14:56:11 +01004271 call VerifyScreenDump(buf, 'Test_popup_prop_not_visible_01', {})
4272
Bram Moolenaar27724252022-05-08 15:00:04 +01004273 " check that hiding and unhiding the popup works
4274 call term_sendkeys(buf, ":call popup_hide(g:some_id)\<CR>")
4275 call VerifyScreenDump(buf, 'Test_popup_prop_not_visible_01a', {})
4276 call term_sendkeys(buf, ":call popup_show(g:some_id)\<CR>")
4277 call VerifyScreenDump(buf, 'Test_popup_prop_not_visible_01b', {})
4278
Bram Moolenaar82db31c2021-02-10 14:56:11 +01004279 call term_sendkeys(buf, ":vert resize -14\<CR>")
4280 call VerifyScreenDump(buf, 'Test_popup_prop_not_visible_02', {})
4281
4282 call term_sendkeys(buf, ":vert resize -8\<CR>")
4283 call VerifyScreenDump(buf, 'Test_popup_prop_not_visible_03', {})
4284
4285 " clean up
4286 call StopVimInTerminal(buf)
Bram Moolenaar82db31c2021-02-10 14:56:11 +01004287endfunction
4288
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02004289func Test_bufdel_skips_popupwin_buffer()
4290 let id = popup_create("Some text", {})
4291 %bd
4292 call popup_close(id)
4293endfunc
4294
Bram Moolenaar58a3cae2022-09-08 13:43:10 +01004295func Test_term_popup_bufline()
4296 " very specific situation where a non-existing buffer line is used, leading
4297 " to an ml_get error
4298 CheckScreendump
4299
4300 let lines =<< trim END
4301 vim9script
4302 &scrolloff = 5
4303 term_start('seq 1 5', {term_finish: 'open'})
4304 timer_start(50, (_) => {
4305 set cpoptions&vim
4306 var buf = popup_create([], {})->winbufnr()
4307 appendbufline(buf, 0, range(5))
4308 })
4309 END
4310 call writefile(lines, 'XtestTermPopup', 'D')
4311 let buf = RunVimInTerminal('-S XtestTermPopup', #{rows: 15})
4312 call VerifyScreenDump(buf, 'Test_term_popup_bufline', {})
4313
4314 " clean up
4315 call StopVimInTerminal(buf)
4316endfunc
4317
Christian Brabandtf6cdab32023-08-11 23:42:02 +02004318func Test_popupwin_with_error()
4319 CheckScreendump
4320
4321 let lines =<< trim END
4322 let options = {'border': 'ERROR', 'line': 1, 'col': 1, 'minwidth': &columns, 'title': 'TITLE'}
4323
4324 END
4325 "call popup_create('Hello world!', options)
4326 call writefile(lines, 'XtestPopupError', 'D')
4327 let buf = RunVimInTerminal('-S XtestPopupError', {})
4328 call term_sendkeys(buf, ":call popup_create('Hello world!', options)\<CR>")
4329 call VerifyScreenDump(buf, 'Test_popupwin_with_error_1', {})
4330
4331 " clean up
4332 call StopVimInTerminal(buf)
4333endfunc
4334
Christian Brabandt47510f32023-10-15 09:56:16 +02004335func Test_popup_close_callback_recursive()
zeertzjqfe583b12023-12-21 16:59:26 +01004336 set maxfuncdepth=20
Christian Brabandt47510f32023-10-15 09:56:16 +02004337 " this invokes the callback recursively
4338 let winid = popup_create('something', #{callback: 'popup_close'})
4339 redraw
zeertzjqfe583b12023-12-21 16:59:26 +01004340 call assert_fails('call popup_close(winid)', 'E169:')
4341
4342 set maxfuncdepth&
Christian Brabandt47510f32023-10-15 09:56:16 +02004343endfunc
Bram Moolenaar82db31c2021-02-10 14:56:11 +01004344
Christian Brabandtac4cffc2024-01-16 17:22:38 +01004345func Test_popupwin_setbufvar_changing_window_view()
4346 " Test for Github Issue https://github.com/vim/vim/issues/13863
4347 " using setbufvar(buf, '&option') should not scroll
4348 " the current window
4349 20new
4350 call append(0, range(1, 25))
4351 setlocal scrollbind
4352 norm! G
4353 let topline = winsaveview()['topline']
4354 call setbufvar(winbufnr(popup_atcursor(['foobar'], {})), '&syntax', 'python')
4355 " close popup
4356 call popup_clear()
4357 call assert_equal(topline, winsaveview()['topline'])
4358
4359 " clean up
4360 bw!
4361endfunc
4362
Christian Brabandtf00f4d92024-09-03 18:20:13 +02004363func Test_popupwin_clears_cmdline_on_hide()
4364 " Test that the command line is properly cleared for overlong
4365 " popup windows and using popup_hide()
4366 CheckScreendump
4367
4368 let lines =<< trim END
4369 vim9script
4370 var id: number
4371 def Filter(winid: number, key: string): bool
4372 if key == 'q'
4373 popup_hide(winid)
4374 else
4375 return false
4376 endif
4377 return true
4378 enddef
4379 setline(1, repeat(['foobar one two three'], &lines))
4380 id = popup_create(1, {
4381 col: 1,
4382 minwidth: &columns,
4383 maxwidth: &columns,
4384 minheight: &lines,
4385 maxheight: &lines,
4386 filter: Filter,
4387 })
4388 END
4389 call writefile(lines, 'XtestPopup_win', 'D')
4390 let buf = RunVimInTerminal('-S XtestPopup_win', #{rows: 10})
4391 call term_sendkeys(buf, "q")
4392 call term_wait(buf)
4393 call VerifyScreenDump(buf, 'Test_popupwin_hide_clear_cmdline_01', {})
4394 call StopVimInTerminal(buf)
4395 let lines =<< trim END
4396 vim9script
4397 var id: number
4398 def Filter(winid: number, key: string): bool
4399 if key == 'q'
4400 popup_close(winid)
4401 else
4402 return false
4403 endif
4404 return true
4405 enddef
4406 setline(1, repeat(['foobar one two three'], &lines))
4407 id = popup_create(1, {
4408 col: 1,
4409 minwidth: &columns,
4410 maxwidth: &columns,
4411 minheight: &lines,
4412 maxheight: &lines,
4413 filter: Filter,
4414 })
4415 END
4416 call writefile(lines, 'XtestPopup_win2', 'D')
4417 let buf = RunVimInTerminal('-S XtestPopup_win2', #{rows: 10})
4418 call term_sendkeys(buf, "q")
4419 call term_wait(buf)
4420 call VerifyScreenDump(buf, 'Test_popupwin_hide_clear_cmdline_01', {})
4421
4422 " clean up
4423 call StopVimInTerminal(buf)
glepnirff159252025-03-01 16:17:00 +01004424
4425 let lines =<< trim END
4426 set completeopt=menuone,noinsert,noselect,popup,fuzzy
4427 set completepopup=border:off
4428 let g:list = [
4429 \ {"word": "one", "info": "one\ntwo\nthree\nfour\nfive"},
4430 \ {"word": "two", "info": "one\ntwo\nthree\nfour\nfive"},
4431 \ {"word": "three", "info": "one\ntwo\nthree\nfour\nfive"},
4432 \ {"word": "four", "info": "one\ntwo\nthree\nfour\nfive"},
4433 \ {"word": "five", "info": "one\ntwo\nthree\nfour\nfive"}
4434 \ ]
4435 call setline(1, repeat(['foobar'], &lines))
4436 inoremap <f5> <cmd>call complete(1, g:list)<cr>
4437 END
4438 call writefile(lines, 'XtestPopup_win3', 'D')
4439 let buf = RunVimInTerminal('-S XtestPopup_win3', #{rows: 15})
4440 call term_sendkeys(buf, "Go\<F5>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>")
4441 call term_wait(buf, 100)
4442 call VerifyScreenDump(buf, 'Test_info_popupwin_clears_cmdline_on_hide_01', {})
4443
4444 call term_sendkeys(buf, "\<Esc>")
4445 call term_wait(buf, 500)
4446 call VerifyScreenDump(buf, 'Test_info_popupwin_clears_cmdline_on_hide_02', {})
4447
4448 call StopVimInTerminal(buf)
Christian Brabandtf00f4d92024-09-03 18:20:13 +02004449endfunc
4450
Christian Brabandt8e831052025-06-18 18:33:31 +02004451func Test_popupwin_callback_closes_popupwin()
4452 " Test that the command line is properly cleared for overlong
4453 " popup windows and using popup_hide()
4454 CheckRunVimInTerminal
4455
4456 let lines =<< trim END
4457 vim9script
4458
4459 def Filter(winid: number, keyCode: string): bool
4460 popup_close(winid)
4461 colorscheme missing
4462 return true
4463 enddef
4464
4465 def Popup(): number
4466 return popup_create('', {
4467 border: [2, 2, 2, 2],
4468 close: 'button',
4469 filter: Filter,
4470 })
4471 enddef
4472 nnoremap gs <scriptcmd>Popup()<cr>
4473 END
4474 call writefile(lines, 'XtestPopup1_win', 'D')
4475 let buf = RunVimInTerminal('-S XtestPopup1_win', #{rows: 10})
4476 let i = 0
4477 while i <= 10
4478 call term_sendkeys(buf, "gs")
4479 call term_wait(buf)
4480 " this was causing a use-after-free
4481 call term_sendkeys(buf, "q")
4482 " clear the hit-enter prompt
4483 call term_sendkeys(buf, "\<cr>")
4484 call term_wait(buf)
4485 let i += 1
4486 endwhile
4487 call term_sendkeys(buf, ":echo 'Done'\<cr>")
4488 call WaitForAssert({-> assert_match('Done', term_getline(buf, 10))})
4489
4490 " clean up
4491 call StopVimInTerminal(buf)
4492endfunc
4493
Bram Moolenaar331bafd2019-07-20 17:46:05 +02004494" vim: shiftwidth=2 sts=2