blob: 29b7b7df9cc3c2295d88b09b8e148134796d7c4d [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001" Tests for popup windows
2
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004CheckFeature popupwin
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005
6source screendump.vim
7
8func Test_simple_popup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02009 CheckScreendump
10
Bram Moolenaare7eb9272019-06-24 00:58:07 +020011 let lines =<< trim END
12 call setline(1, range(1, 100))
13 hi PopupColor1 ctermbg=lightblue
14 hi PopupColor2 ctermbg=lightcyan
Bram Moolenaar1666ac92019-11-10 17:22:31 +010015 hi EndOfBuffer ctermbg=lightgrey
Bram Moolenaare7eb9272019-06-24 00:58:07 +020016 hi Comment ctermfg=red
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020017 call prop_type_add('comment', #{highlight: 'Comment'})
18 let winid = popup_create('hello there', #{line: 3, col: 11, minwidth: 20, highlight: 'PopupColor1'})
19 let winid2 = popup_create(['another one', 'another two', 'another three'], #{line: 3, col: 25, minwidth: 20})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020020 call setwinvar(winid2, '&wincolor', 'PopupColor2')
21 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +010022 call writefile(lines, 'XtestPopup', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020023 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar4d784b22019-05-25 19:51:39 +020024 call VerifyScreenDump(buf, 'Test_popupwin_01', {})
25
Bram Moolenaarec583842019-05-26 14:11:23 +020026 " Add a tabpage
27 call term_sendkeys(buf, ":tabnew\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020028 call term_sendkeys(buf, ":let popupwin = popup_create(["
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020029 \ .. "#{text: 'other tab'},"
30 \ .. "#{text: 'a comment line', props: [#{"
Bram Moolenaard5abb4c2019-07-13 22:46:10 +020031 \ .. "col: 3, length: 7, minwidth: 20, type: 'comment'"
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020032 \ .. "}]},"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020033 \ .. "], #{line: 4, col: 9, minwidth: 20})\<CR>")
Bram Moolenaarec583842019-05-26 14:11:23 +020034 call VerifyScreenDump(buf, 'Test_popupwin_02', {})
35
36 " switch back to first tabpage
37 call term_sendkeys(buf, "gt")
38 call VerifyScreenDump(buf, 'Test_popupwin_03', {})
39
40 " close that tabpage
41 call term_sendkeys(buf, ":quit!\<CR>")
42 call VerifyScreenDump(buf, 'Test_popupwin_04', {})
43
Bram Moolenaar202d9822019-06-11 21:56:30 +020044 " set 'columns' to a small value, size must be recomputed
45 call term_sendkeys(buf, ":let cols = &columns\<CR>")
46 call term_sendkeys(buf, ":set columns=12\<CR>")
47 call VerifyScreenDump(buf, 'Test_popupwin_04a', {})
48 call term_sendkeys(buf, ":let &columns = cols\<CR>")
49
Bram Moolenaar17146962019-05-30 00:12:11 +020050 " resize popup, show empty line at bottom
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020051 call term_sendkeys(buf, ":call popup_move(popupwin, #{minwidth: 15, maxwidth: 25, minheight: 3, maxheight: 5})\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020052 call term_sendkeys(buf, ":redraw\<CR>")
53 call VerifyScreenDump(buf, 'Test_popupwin_05', {})
54
Bram Moolenaar17146962019-05-30 00:12:11 +020055 " show not fitting line at bottom
56 call term_sendkeys(buf, ":call setbufline(winbufnr(popupwin), 3, 'this line will not fit here')\<CR>")
57 call term_sendkeys(buf, ":redraw\<CR>")
58 call VerifyScreenDump(buf, 'Test_popupwin_06', {})
59
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020060 " move popup over ruler
61 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020062 call term_sendkeys(buf, ":call popup_move(popupwin, #{line: 7, col: 55})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +020063 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020064 call VerifyScreenDump(buf, 'Test_popupwin_07', {})
65
66 " clear all popups after moving the cursor a bit, so that ruler is updated
67 call term_sendkeys(buf, "axxx\<Esc>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +020068 call TermWait(buf)
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020069 call term_sendkeys(buf, "0")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +020070 call TermWait(buf)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +020071 call term_sendkeys(buf, ":call popup_clear()\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020072 call VerifyScreenDump(buf, 'Test_popupwin_08', {})
73
Bram Moolenaar4d784b22019-05-25 19:51:39 +020074 " clean up
75 call StopVimInTerminal(buf)
Bram Moolenaar4d784b22019-05-25 19:51:39 +020076endfunc
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020077
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020078func Test_popup_with_border_and_padding()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +020079 CheckScreendump
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020080
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020081 for iter in range(0, 1)
Bram Moolenaare7eb9272019-06-24 00:58:07 +020082 let lines =<< trim END
83 call setline(1, range(1, 100))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020084 call popup_create('hello border', #{line: 2, col: 3, border: []})
85 call popup_create('hello padding', #{line: 2, col: 23, padding: []})
Bram Moolenaarc363fe12019-08-04 18:13:46 +020086 call popup_create('hello both', #{line: 2, col: 43, border: [], padding: [], highlight: 'Normal'})
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020087 call popup_create('border TL', #{line: 6, col: 3, border: [1, 0, 0, 4]})
Bram Moolenaarb0992022020-01-30 14:55:42 +010088 call popup_create('paddings', #{line: 6, col: 23, padding: range(1, 4)})
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020089 call popup_create('wrapped longer text', #{line: 8, col: 55, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
90 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 +010091 call popup_create('X', #{line: 2, col: 73})
92 call popup_create('X', #{line: 3, col: 74})
93 call popup_create('X', #{line: 4, col: 75})
94 call popup_create('X', #{line: 5, col: 76})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020095 END
96 call insert(lines, iter == 1 ? '' : 'set enc=latin1')
Bram Moolenaar145d1fd2022-09-30 21:57:11 +010097 call writefile(lines, 'XtestPopupBorder', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020098 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 15})
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020099 call VerifyScreenDump(buf, 'Test_popupwin_2' .. iter, {})
100
101 call StopVimInTerminal(buf)
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +0200102 endfor
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200103
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200104 let lines =<< trim END
105 call setline(1, range(1, 100))
106 hi BlueColor ctermbg=lightblue
107 hi TopColor ctermbg=253
108 hi RightColor ctermbg=245
109 hi BottomColor ctermbg=240
110 hi LeftColor ctermbg=248
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200111 call popup_create('hello border', #{line: 2, col: 3, border: [], borderhighlight: ['BlueColor']})
112 call popup_create(['hello border', 'and more'], #{line: 2, col: 23, border: [], borderhighlight: ['TopColor', 'RightColor', 'BottomColor', 'LeftColor']})
113 call popup_create(['hello border', 'lines only'], #{line: 2, col: 43, border: [], borderhighlight: ['BlueColor'], borderchars: ['x']})
114 call popup_create(['hello border', 'with corners'], #{line: 2, col: 60, border: [], borderhighlight: ['BlueColor'], borderchars: ['x', '#']})
115 let winid = popup_create(['hello border', 'with numbers'], #{line: 6, col: 3, border: [], borderhighlight: ['BlueColor'], borderchars: ['0', '1', '2', '3', '4', '5', '6', '7']})
116 call popup_create(['hello border', 'just blanks'], #{line: 7, col: 23, border: [], borderhighlight: ['BlueColor'], borderchars: [' ']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200117 func MultiByte()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200118 call popup_create(['hello'], #{line: 8, col: 43, border: [], borderchars: ['─', '│', '─', '│', '┌', '┐', '┘', '└']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200119 endfunc
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200120 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100121 call writefile(lines, 'XtestPopupBorder', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200122 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 12})
Bram Moolenaar790498b2019-06-01 22:15:29 +0200123 call VerifyScreenDump(buf, 'Test_popupwin_22', {})
124
Bram Moolenaarad24a712019-06-17 20:05:45 +0200125 " check that changing borderchars triggers a redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200126 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 +0200127 call VerifyScreenDump(buf, 'Test_popupwin_23', {})
128
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200129 " check multi-byte border only with 'ambiwidth' single
130 if &ambiwidth == 'single'
131 call term_sendkeys(buf, ":call MultiByte()\<CR>")
132 call VerifyScreenDump(buf, 'Test_popupwin_24', {})
133 endif
134
Bram Moolenaar790498b2019-06-01 22:15:29 +0200135 call StopVimInTerminal(buf)
Bram Moolenaar790498b2019-06-01 22:15:29 +0200136
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200137 let with_border_or_padding = #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200138 \ line: 2,
139 \ core_line: 3,
140 \ col: 3,
141 \ core_col: 4,
142 \ width: 14,
143 \ core_width: 12,
144 \ height: 3,
145 \ core_height: 1,
146 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100147 \ lastline: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200148 \ scrollbar: 0,
149 \ visible: 1}
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200150 let winid = popup_create('hello border', #{line: 2, col: 3, border: []})",
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200151 call assert_equal(with_border_or_padding, winid->popup_getpos())
Bram Moolenaarae943152019-06-16 22:54:14 +0200152 let options = popup_getoptions(winid)
153 call assert_equal([], options.border)
154 call assert_false(has_key(options, "padding"))
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200155
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200156 let winid = popup_create('hello padding', #{line: 2, col: 3, padding: []})
Bram Moolenaarae943152019-06-16 22:54:14 +0200157 let with_border_or_padding.width = 15
158 let with_border_or_padding.core_width = 13
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200159 call assert_equal(with_border_or_padding, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200160 let options = popup_getoptions(winid)
161 call assert_false(has_key(options, "border"))
162 call assert_equal([], options.padding)
163
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200164 call popup_setoptions(winid, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200165 \ padding: [1, 2, 3, 4],
166 \ border: [4, 0, 7, 8],
167 \ borderhighlight: ['Top', 'Right', 'Bottom', 'Left'],
168 \ borderchars: ['1', '^', '2', '>', '3', 'v', '4', '<'],
Bram Moolenaarae943152019-06-16 22:54:14 +0200169 \ })
170 let options = popup_getoptions(winid)
171 call assert_equal([1, 0, 1, 1], options.border)
172 call assert_equal([1, 2, 3, 4], options.padding)
173 call assert_equal(['Top', 'Right', 'Bottom', 'Left'], options.borderhighlight)
174 call assert_equal(['1', '^', '2', '>', '3', 'v', '4', '<'], options.borderchars)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200175
Bram Moolenaara1396152019-10-20 18:46:05 +0200176 " Check that popup_setoptions() takes the output of popup_getoptions()
177 call popup_setoptions(winid, options)
178 call assert_equal(options, popup_getoptions(winid))
179
Bram Moolenaarb0992022020-01-30 14:55:42 +0100180 " Check that range() doesn't crash
181 call popup_setoptions(winid, #{
182 \ padding: range(1, 4),
183 \ border: range(5, 8),
184 \ borderhighlight: range(4),
185 \ borderchars: range(8),
186 \ })
187
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200188 let winid = popup_create('hello both', #{line: 3, col: 8, border: [], padding: []})
189 call assert_equal(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200190 \ line: 3,
191 \ core_line: 5,
192 \ col: 8,
193 \ core_col: 10,
194 \ width: 14,
195 \ core_width: 10,
196 \ height: 5,
197 \ scrollbar: 0,
198 \ core_height: 1,
199 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100200 \ lastline: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200201 \ visible: 1}, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200202
203 call popup_clear()
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200204endfunc
205
Bram Moolenaarb4230122019-05-30 18:40:53 +0200206func Test_popup_with_syntax_win_execute()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200207 CheckScreendump
208
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200209 let lines =<< trim END
210 call setline(1, range(1, 100))
211 hi PopupColor ctermbg=lightblue
212 let winid = popup_create([
213 \ '#include <stdio.h>',
214 \ 'int main(void)',
215 \ '{',
216 \ ' printf(123);',
217 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200218 \], #{line: 3, col: 25, highlight: 'PopupColor'})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200219 call win_execute(winid, 'set syntax=cpp')
220 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100221 call writefile(lines, 'XtestPopup', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200222 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200223 call VerifyScreenDump(buf, 'Test_popupwin_10', {})
224
225 " clean up
226 call StopVimInTerminal(buf)
Bram Moolenaarb4230122019-05-30 18:40:53 +0200227endfunc
228
229func Test_popup_with_syntax_setbufvar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200230 CheckScreendump
231
Bram Moolenaar402502d2019-05-30 22:07:36 +0200232 let lines =<< trim END
233 call setline(1, range(1, 100))
234 hi PopupColor ctermbg=lightgrey
235 let winid = popup_create([
236 \ '#include <stdio.h>',
237 \ 'int main(void)',
238 \ '{',
Bram Moolenaare089c3f2019-07-09 20:25:25 +0200239 \ "\tprintf(567);",
Bram Moolenaar402502d2019-05-30 22:07:36 +0200240 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200241 \], #{line: 3, col: 21, highlight: 'PopupColor'})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200242 call setbufvar(winbufnr(winid), '&syntax', 'cpp')
243 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100244 call writefile(lines, 'XtestPopup', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200245 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200246 call VerifyScreenDump(buf, 'Test_popupwin_11', {})
247
248 " clean up
249 call StopVimInTerminal(buf)
Bram Moolenaarb4230122019-05-30 18:40:53 +0200250endfunc
251
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200252func Test_popup_with_matches()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200253 CheckScreendump
254
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200255 let lines =<< trim END
256 call setline(1, ['111 222 333', '444 555 666'])
257 let winid = popup_create([
258 \ '111 222 333',
259 \ '444 555 666',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200260 \], #{line: 3, col: 10, border: []})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200261 set hlsearch
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100262 hi VeryBlue ctermfg=blue guifg=blue
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200263 /666
264 call matchadd('ErrorMsg', '111')
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100265 call matchadd('VeryBlue', '444')
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200266 call win_execute(winid, "call matchadd('ErrorMsg', '111')")
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100267 call win_execute(winid, "call matchadd('VeryBlue', '555')")
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200268 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100269 call writefile(lines, 'XtestPopupMatches', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200270 let buf = RunVimInTerminal('-S XtestPopupMatches', #{rows: 10})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200271 call VerifyScreenDump(buf, 'Test_popupwin_matches', {})
272
273 " clean up
274 call StopVimInTerminal(buf)
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200275endfunc
276
Bram Moolenaar399d8982019-06-02 15:34:29 +0200277func Test_popup_all_corners()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200278 CheckScreendump
279
Bram Moolenaar399d8982019-06-02 15:34:29 +0200280 let lines =<< trim END
281 call setline(1, repeat([repeat('-', 60)], 15))
282 set so=0
283 normal 2G3|r#
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200284 let winid1 = popup_create(['first', 'second'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200285 \ line: 'cursor+1',
286 \ col: 'cursor',
287 \ pos: 'topleft',
288 \ border: [],
289 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200290 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200291 normal 24|r@
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200292 let winid1 = popup_create(['First', 'SeconD'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200293 \ line: 'cursor+1',
294 \ col: 'cursor',
295 \ pos: 'topright',
296 \ border: [],
297 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200298 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200299 normal 9G27|r%
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200300 let winid1 = popup_create(['fiRSt', 'seCOnd'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200301 \ line: 'cursor-1',
302 \ col: 'cursor',
303 \ pos: 'botleft',
304 \ border: [],
305 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200306 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200307 normal 48|r&
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200308 let winid1 = popup_create(['FIrsT', 'SEcoND'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200309 \ line: 'cursor-1',
310 \ col: 'cursor',
311 \ pos: 'botright',
312 \ border: [],
313 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200314 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200315 normal 1G51|r*
316 let winid1 = popup_create(['one', 'two'], #{
317 \ line: 'cursor-1',
318 \ col: 'cursor',
319 \ pos: 'botleft',
320 \ border: [],
321 \ padding: [],
322 \ })
Bram Moolenaar399d8982019-06-02 15:34:29 +0200323 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100324 call writefile(lines, 'XtestPopupCorners', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200325 let buf = RunVimInTerminal('-S XtestPopupCorners', #{rows: 12})
Bram Moolenaar399d8982019-06-02 15:34:29 +0200326 call VerifyScreenDump(buf, 'Test_popupwin_corners', {})
327
328 " clean up
329 call StopVimInTerminal(buf)
Bram Moolenaar399d8982019-06-02 15:34:29 +0200330endfunc
331
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100332func Test_popup_nospace()
333 CheckScreendump
334
335 let lines =<< trim END
336 call setline(1, repeat([repeat('-', 60)], 15))
337 set so=0
338
339 " cursor in a line in top half, using "botleft" with popup that
340 " does fit
341 normal 5G2|r@
342 let winid1 = popup_create(['one', 'two'], #{
343 \ line: 'cursor-1',
344 \ col: 'cursor',
345 \ pos: 'botleft',
346 \ border: [],
347 \ })
348 " cursor in a line in top half, using "botleft" with popup that
349 " doesn't fit: gets truncated
350 normal 5G9|r#
351 let winid1 = popup_create(['one', 'two', 'tee'], #{
352 \ line: 'cursor-1',
353 \ col: 'cursor',
354 \ pos: 'botleft',
355 \ posinvert: 0,
356 \ border: [],
357 \ })
358 " cursor in a line in top half, using "botleft" with popup that
359 " doesn't fit and 'posinvert' set: flips to below.
360 normal 5G16|r%
361 let winid1 = popup_create(['one', 'two', 'tee'], #{
362 \ line: 'cursor-1',
363 \ col: 'cursor',
364 \ pos: 'botleft',
365 \ border: [],
366 \ })
367 " cursor in a line in bottom half, using "botleft" with popup that
368 " doesn't fit: does not flip.
369 normal 8G23|r*
370 let winid1 = popup_create(['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff'], #{
371 \ line: 'cursor-1',
372 \ col: 'cursor',
373 \ pos: 'botleft',
374 \ border: [],
375 \ })
376
377 " cursor in a line in bottom half, using "topleft" with popup that
378 " does fit
379 normal 8G30|r@
380 let winid1 = popup_create(['one', 'two'], #{
381 \ line: 'cursor+1',
382 \ col: 'cursor',
383 \ pos: 'topleft',
384 \ border: [],
385 \ })
386 " cursor in a line in top half, using "topleft" with popup that
387 " doesn't fit: truncated
388 normal 8G37|r#
389 let winid1 = popup_create(['one', 'two', 'tee'], #{
390 \ line: 'cursor+1',
391 \ col: 'cursor',
392 \ pos: 'topleft',
393 \ posinvert: 0,
394 \ border: [],
395 \ })
396 " cursor in a line in top half, using "topleft" with popup that
Bram Moolenaar5c6b6182019-11-10 17:51:38 +0100397 " doesn't fit and "posinvert" set: flips to above.
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100398 normal 8G44|r%
Bram Moolenaar5c6b6182019-11-10 17:51:38 +0100399 let winid1 = popup_create(['one', 'two', 'tee', 'fou', 'fiv'], #{
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100400 \ line: 'cursor+1',
401 \ col: 'cursor',
402 \ pos: 'topleft',
403 \ border: [],
404 \ })
405 " cursor in a line in top half, using "topleft" with popup that
406 " doesn't fit: does not flip.
407 normal 5G51|r*
408 let winid1 = popup_create(['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff'], #{
409 \ line: 'cursor+1',
410 \ col: 'cursor',
411 \ pos: 'topleft',
412 \ border: [],
413 \ })
414 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100415 call writefile(lines, 'XtestPopupNospace', 'D')
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100416 let buf = RunVimInTerminal('-S XtestPopupNospace', #{rows: 12})
417 call VerifyScreenDump(buf, 'Test_popupwin_nospace', {})
418
419 " clean up
420 call StopVimInTerminal(buf)
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100421endfunc
422
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200423func Test_popup_firstline_dump()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200424 CheckScreendump
425
Bram Moolenaar8d241042019-06-12 23:40:01 +0200426 let lines =<< trim END
427 call setline(1, range(1, 20))
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200428 let winid = popup_create(['1111', '222222', '33333', '44', '5', '666666', '77777', '888', '9999999999999999'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200429 \ maxheight: 4,
430 \ firstline: 3,
Bram Moolenaar8d241042019-06-12 23:40:01 +0200431 \ })
432 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100433 call writefile(lines, 'XtestPopupFirstline', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200434 let buf = RunVimInTerminal('-S XtestPopupFirstline', #{rows: 10})
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200435 call VerifyScreenDump(buf, 'Test_popupwin_firstline_1', {})
436
437 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: -1})\<CR>")
438 call term_sendkeys(buf, ":\<CR>")
439 call VerifyScreenDump(buf, 'Test_popupwin_firstline_2', {})
Bram Moolenaar8d241042019-06-12 23:40:01 +0200440
441 " clean up
442 call StopVimInTerminal(buf)
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200443endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +0200444
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200445func Test_popup_firstline()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200446 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200447 \ maxheight: 2,
448 \ firstline: 3,
Bram Moolenaarae943152019-06-16 22:54:14 +0200449 \ })
450 call assert_equal(3, popup_getoptions(winid).firstline)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200451 call popup_setoptions(winid, #{firstline: 1})
Bram Moolenaarae943152019-06-16 22:54:14 +0200452 call assert_equal(1, popup_getoptions(winid).firstline)
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200453 eval winid->popup_close()
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200454
455 let winid = popup_create(['xxx']->repeat(50), #{
456 \ maxheight: 3,
457 \ firstline: 11,
458 \ })
459 redraw
460 call assert_equal(11, popup_getoptions(winid).firstline)
461 call assert_equal(11, popup_getpos(winid).firstline)
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +0200462 " check line() works with popup window
463 call assert_equal(11, line('.', winid))
464 call assert_equal(50, line('$', winid))
465 call assert_equal(0, line('$', 123456))
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200466
467 " Normal command changes what is displayed but not "firstline"
468 call win_execute(winid, "normal! \<c-y>")
469 call assert_equal(11, popup_getoptions(winid).firstline)
470 call assert_equal(10, popup_getpos(winid).firstline)
471
472 " Making some property change applies "firstline" again
473 call popup_setoptions(winid, #{line: 4})
474 call assert_equal(11, popup_getoptions(winid).firstline)
475 call assert_equal(11, popup_getpos(winid).firstline)
476
477 " Remove "firstline" property and scroll
478 call popup_setoptions(winid, #{firstline: 0})
479 call win_execute(winid, "normal! \<c-y>")
480 call assert_equal(0, popup_getoptions(winid).firstline)
481 call assert_equal(10, popup_getpos(winid).firstline)
482
483 " Making some property change has no side effect
484 call popup_setoptions(winid, #{line: 3})
485 call assert_equal(0, popup_getoptions(winid).firstline)
486 call assert_equal(10, popup_getpos(winid).firstline)
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200487 call popup_close(winid)
Bram Moolenaarae943152019-06-16 22:54:14 +0200488
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100489 " CTRL-D scrolls down half a page
490 let winid = popup_create(['xxx']->repeat(50), #{
491 \ maxheight: 8,
492 \ })
493 redraw
494 call assert_equal(1, popup_getpos(winid).firstline)
495 call win_execute(winid, "normal! \<C-D>")
496 call assert_equal(5, popup_getpos(winid).firstline)
497 call win_execute(winid, "normal! \<C-D>")
498 call assert_equal(9, popup_getpos(winid).firstline)
499 call win_execute(winid, "normal! \<C-U>")
500 call assert_equal(5, popup_getpos(winid).firstline)
501
502 call win_execute(winid, "normal! \<C-F>")
503 call assert_equal(11, popup_getpos(winid).firstline)
504 call win_execute(winid, "normal! \<C-B>")
505 call assert_equal(5, popup_getpos(winid).firstline)
506
Bram Moolenaarae943152019-06-16 22:54:14 +0200507 call popup_close(winid)
LemonBoy0044e512022-04-20 19:47:37 +0100508
509 " Popup with less elements than the maximum height and negative firstline:
510 " check that the popup height is correctly computed.
511 let winid = popup_create(['xxx']->repeat(4), #{
512 \ firstline: -1,
513 \ maxheight: 6,
514 \ })
515
516 let pos = popup_getpos(winid)
517 call assert_equal(3, pos.width)
518 call assert_equal(4, pos.height)
519
520 call popup_close(winid)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200521endfunc
522
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200523func Test_popup_firstline_cursorline()
524 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
525 \ maxheight: 2,
526 \ firstline: 3,
527 \ cursorline: 1,
528 \ })
529 call assert_equal(3, popup_getoptions(winid).firstline)
530 call assert_equal(3, getwininfo(winid)[0].topline)
531 call assert_equal(3, getcurpos(winid)[1])
532
533 call popup_close(winid)
534endfunc
535
Bram Moolenaara112f2d2019-09-01 17:38:09 +0200536func Test_popup_noscrolloff()
537 set scrolloff=5
538 let winid = popup_create(['xxx']->repeat(50), #{
539 \ maxheight: 5,
540 \ firstline: 11,
541 \ })
542 redraw
543 call assert_equal(11, popup_getoptions(winid).firstline)
544 call assert_equal(11, popup_getpos(winid).firstline)
545
546 call popup_setoptions(winid, #{firstline: 0})
547 call win_execute(winid, "normal! \<c-y>")
548 call assert_equal(0, popup_getoptions(winid).firstline)
549 call assert_equal(10, popup_getpos(winid).firstline)
550
551 call popup_close(winid)
552endfunc
553
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200554func Test_popup_drag()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200555 CheckScreendump
556
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200557 " create a popup that covers the command line
558 let lines =<< trim END
559 call setline(1, range(1, 20))
Bram Moolenaar356375f2019-08-24 14:46:29 +0200560 split
561 vsplit
562 $wincmd w
563 vsplit
564 1wincmd w
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200565 let winid = popup_create(['1111', '222222', '33333'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200566 \ drag: 1,
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200567 \ resize: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200568 \ border: [],
569 \ line: &lines - 4,
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200570 \ })
571 func Dragit()
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000572 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
573 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2 - 20)<CR>
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200574 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
575 endfunc
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200576 func Resize()
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000577 map <silent> <F5> :call test_setmouse(6, 21)<CR>
578 map <silent> <F6> :call test_setmouse(7, 25)<CR>
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200579 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
580 endfunc
Bram Moolenaarbfc57862021-11-26 15:57:40 +0000581 func ClickAndDrag()
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000582 map <silent> <F3> :call test_setmouse(5, 2)<CR>
583 map <silent> <F4> :call test_setmouse(3, 14)<CR>
584 map <silent> <F5> :call test_setmouse(3, 18)<CR>
585 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
586 call feedkeys("\<F4>\<LeftMouse>\<F5>\<LeftDrag>\<LeftRelease>", "xt")
Bram Moolenaarbfc57862021-11-26 15:57:40 +0000587 endfunc
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000588 func DragAllStart()
589 call popup_clear()
590 call popup_create('hello', #{line: 3, col: 5, dragall: 1})
591 endfunc
592 func DragAllDrag()
593 map <silent> <F3> :call test_setmouse(3, 5)<CR>
594 map <silent> <F4> :call test_setmouse(5, 36)<CR>
595 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
596 endfunc
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200597 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100598 call writefile(lines, 'XtestPopupDrag', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200599 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200600 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
601
602 call term_sendkeys(buf, ":call Dragit()\<CR>")
603 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
604
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200605 call term_sendkeys(buf, ":call Resize()\<CR>")
606 call VerifyScreenDump(buf, 'Test_popupwin_drag_03', {})
607
Bram Moolenaarbfc57862021-11-26 15:57:40 +0000608 " dragging works after click on a status line
609 call term_sendkeys(buf, ":call ClickAndDrag()\<CR>")
610 call VerifyScreenDump(buf, 'Test_popupwin_drag_04', {})
611
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000612 " dragging without border
613 call term_sendkeys(buf, ":call DragAllStart()\<CR>")
614 call VerifyScreenDump(buf, 'Test_popupwin_drag_05', {})
615 call term_sendkeys(buf, ":call DragAllDrag()\<CR>")
616 call VerifyScreenDump(buf, 'Test_popupwin_drag_06', {})
617
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200618 " clean up
619 call StopVimInTerminal(buf)
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200620endfunc
621
Bram Moolenaareabddc42022-04-02 15:32:16 +0100622func Test_popup_drag_minwidth()
623 CheckScreendump
624
625 " create a popup that does not fit
626 let lines =<< trim END
627 call range(40)
628 \ ->map({_,i -> string(i)})
629 \ ->popup_create({
630 \ 'drag': 1,
631 \ 'wrap': 0,
632 \ 'border': [],
633 \ 'scrollbar': 1,
634 \ 'minwidth': 100,
635 \ 'filter': {w, k -> k ==# 'q' ? len([popup_close(w)]) : 0},
636 \ })
637 func DragitDown()
638 map <silent> <F3> :call test_setmouse(1, 10)<CR>
639 map <silent> <F4> :call test_setmouse(5, 40)<CR>
640 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
641 endfunc
642 func DragitUp()
643 map <silent> <F3> :call test_setmouse(5, 40)<CR>
644 map <silent> <F4> :call test_setmouse(4, 40)<CR>
645 map <silent> <F5> :call test_setmouse(3, 40)<CR>
646 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<F5>\<LeftDrag>\<LeftRelease>", "xt")
647 endfunc
648 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100649 call writefile(lines, 'XtestPopupDrag', 'D')
Bram Moolenaareabddc42022-04-02 15:32:16 +0100650 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
651 call VerifyScreenDump(buf, 'Test_popupwin_drag_minwidth_1', {})
652
653 call term_sendkeys(buf, ":call DragitDown()\<CR>")
654 call VerifyScreenDump(buf, 'Test_popupwin_drag_minwidth_2', {})
655
656 call term_sendkeys(buf, ":call DragitUp()\<CR>")
657 call VerifyScreenDump(buf, 'Test_popupwin_drag_minwidth_3', {})
658
659 call term_sendkeys(buf, 'q')
660
661 " clean up
662 call StopVimInTerminal(buf)
Bram Moolenaareabddc42022-04-02 15:32:16 +0100663endfunc
664
Bram Moolenaar35910f22020-07-12 19:24:10 +0200665func Test_popup_drag_termwin()
666 CheckUnix
667 CheckScreendump
668 CheckFeature terminal
669
670 " create a popup that covers the terminal window
671 let lines =<< trim END
Bram Moolenaar452143c2020-07-15 17:38:21 +0200672 set foldmethod=marker
673 call setline(1, range(100))
674 for nr in range(7)
675 call setline(nr * 12 + 1, "fold {{{")
Bram Moolenaar9d489562020-07-30 20:08:50 +0200676 call setline(nr * 12 + 11, "end }}}")
Bram Moolenaar452143c2020-07-15 17:38:21 +0200677 endfor
678 %foldclose
Bram Moolenaar35910f22020-07-12 19:24:10 +0200679 set shell=/bin/sh noruler
Bram Moolenaar4b2ce122020-11-21 21:41:41 +0100680 unlet $PROMPT_COMMAND
Bram Moolenaara4dc6f92020-07-12 19:52:36 +0200681 let $PS1 = 'vim> '
Bram Moolenaar452143c2020-07-15 17:38:21 +0200682 terminal ++rows=4
Bram Moolenaar35910f22020-07-12 19:24:10 +0200683 $wincmd w
684 let winid = popup_create(['1111', '2222'], #{
685 \ drag: 1,
686 \ resize: 1,
687 \ border: [],
688 \ line: 3,
689 \ })
Bram Moolenaar452143c2020-07-15 17:38:21 +0200690 func DragitLeft()
Bram Moolenaar35910f22020-07-12 19:24:10 +0200691 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
692 endfunc
Bram Moolenaar452143c2020-07-15 17:38:21 +0200693 func DragitDown()
694 call feedkeys("\<F4>\<LeftMouse>\<F5>\<LeftDrag>\<LeftRelease>", "xt")
695 endfunc
696 func DragitDownLeft()
697 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
698 endfunc
Bram Moolenaar35910f22020-07-12 19:24:10 +0200699 map <silent> <F3> :call test_setmouse(3, &columns / 2)<CR>
700 map <silent> <F4> :call test_setmouse(3, &columns / 2 - 20)<CR>
Bram Moolenaar452143c2020-07-15 17:38:21 +0200701 map <silent> <F5> :call test_setmouse(12, &columns / 2)<CR>
702 map <silent> <F6> :call test_setmouse(12, &columns / 2 - 20)<CR>
Bram Moolenaar35910f22020-07-12 19:24:10 +0200703 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100704 call writefile(lines, 'XtestPopupTerm', 'D')
Bram Moolenaar452143c2020-07-15 17:38:21 +0200705 let buf = RunVimInTerminal('-S XtestPopupTerm', #{rows: 16})
Bram Moolenaar35910f22020-07-12 19:24:10 +0200706 call VerifyScreenDump(buf, 'Test_popupwin_term_01', {})
707
Bram Moolenaar452143c2020-07-15 17:38:21 +0200708 call term_sendkeys(buf, ":call DragitLeft()\<CR>")
Bram Moolenaar35910f22020-07-12 19:24:10 +0200709 call VerifyScreenDump(buf, 'Test_popupwin_term_02', {})
710
Bram Moolenaar452143c2020-07-15 17:38:21 +0200711 call term_sendkeys(buf, ":call DragitDown()\<CR>")
712 call VerifyScreenDump(buf, 'Test_popupwin_term_03', {})
713
714 call term_sendkeys(buf, ":call DragitDownLeft()\<CR>")
715 call VerifyScreenDump(buf, 'Test_popupwin_term_04', {})
716
Bram Moolenaar35910f22020-07-12 19:24:10 +0200717 " clean up
718 call StopVimInTerminal(buf)
Bram Moolenaar35910f22020-07-12 19:24:10 +0200719endfunc
720
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200721func Test_popup_close_with_mouse()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200722 CheckScreendump
723
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200724 let lines =<< trim END
725 call setline(1, range(1, 20))
726 " With border, can click on X
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200727 let winid = popup_create('foobar', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200728 \ close: 'button',
729 \ border: [],
730 \ line: 1,
731 \ col: 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200732 \ })
733 func CloseMsg(id, result)
734 echomsg 'Popup closed with ' .. a:result
735 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200736 let winid = popup_create('notification', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200737 \ close: 'click',
738 \ line: 3,
739 \ col: 15,
740 \ callback: 'CloseMsg',
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200741 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200742 let winid = popup_create('no border here', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200743 \ close: 'button',
744 \ line: 5,
745 \ col: 3,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200746 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200747 let winid = popup_create('only padding', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200748 \ close: 'button',
749 \ padding: [],
750 \ line: 5,
751 \ col: 23,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200752 \ })
753 func CloseWithX()
754 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
755 endfunc
756 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
757 func CloseWithClick()
758 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
759 endfunc
760 map <silent> <F4> :call test_setmouse(3, 17)<CR>
Bram Moolenaarf6396232019-08-24 19:36:00 +0200761 func CreateWithMenuFilter()
762 let winid = popup_create('barfoo', #{
763 \ close: 'button',
764 \ filter: 'popup_filter_menu',
765 \ border: [],
766 \ line: 1,
767 \ col: 40,
768 \ })
769 endfunc
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200770 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100771 call writefile(lines, 'XtestPopupClose', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200772 let buf = RunVimInTerminal('-S XtestPopupClose', #{rows: 10})
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200773 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
774
775 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
776 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
777
778 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
779 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
780
Bram Moolenaarf6396232019-08-24 19:36:00 +0200781 call term_sendkeys(buf, ":call CreateWithMenuFilter()\<CR>")
782 call VerifyScreenDump(buf, 'Test_popupwin_close_04', {})
783
784 " We have to send the actual mouse code, feedkeys() would be caught the
785 " filter.
786 call term_sendkeys(buf, "\<Esc>[<0;47;1M")
787 call VerifyScreenDump(buf, 'Test_popupwin_close_05', {})
788
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200789 " clean up
790 call StopVimInTerminal(buf)
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200791endfunction
792
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200793func Test_popup_menu_wrap()
794 CheckScreendump
795
796 let lines =<< trim END
797 call setline(1, range(1, 20))
798 call popup_create([
799 \ 'one',
800 \ 'asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfas',
801 \ 'three',
802 \ 'four',
803 \ ], #{
804 \ pos: "botleft",
805 \ border: [],
806 \ padding: [0,1,0,1],
807 \ maxheight: 3,
808 \ cursorline: 1,
809 \ filter: 'popup_filter_menu',
810 \ })
811 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100812 call writefile(lines, 'XtestPopupWrap', 'D')
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200813 let buf = RunVimInTerminal('-S XtestPopupWrap', #{rows: 10})
814 call VerifyScreenDump(buf, 'Test_popupwin_wrap_1', {})
815
816 call term_sendkeys(buf, "jj")
817 call VerifyScreenDump(buf, 'Test_popupwin_wrap_2', {})
818
819 " clean up
820 call term_sendkeys(buf, "\<Esc>")
821 call StopVimInTerminal(buf)
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200822endfunction
823
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200824func Test_popup_with_mask()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200825 CheckScreendump
826
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200827 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200828 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200829 hi PopupColor ctermbg=lightgrey
830 let winid = popup_create([
831 \ 'some text',
832 \ 'another line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200833 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200834 \ line: 1,
835 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100836 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200837 \ wrap: 0,
838 \ fixed: 1,
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +0200839 \ scrollbar: v:false,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200840 \ zindex: 90,
841 \ padding: [],
842 \ highlight: 'PopupColor',
843 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200844 call popup_create([
845 \ 'xxxxxxxxx',
846 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200847 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200848 \ line: 3,
849 \ col: 18,
850 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200851 let winidb = popup_create([
852 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200853 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200854 \ line: 7,
855 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100856 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200857 \ wrap: 0,
858 \ fixed: 1,
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +0200859 \ scrollbar: v:false,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200860 \ close: 'button',
861 \ zindex: 90,
862 \ padding: [],
863 \ border: [],
864 \ 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 +0200865 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100866 call writefile(lines, 'XtestPopupMask', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200867 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200868 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
869
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200870 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
871 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200872 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200873 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
874
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200875 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
876 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200877 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200878 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
879
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200880 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
881 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200882 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200883 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
884
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200885 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
886 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200887 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200888 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
889
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200890 " clean up
891 call StopVimInTerminal(buf)
Bram Moolenaar4012d262020-12-29 11:57:46 +0100892
893 " this was causing a crash
894 call popup_create('test', #{mask: [[0, 0, 0, 0]]})
895 call popup_clear()
Bram Moolenaar10ccfb22021-02-13 21:31:18 +0100896
897 " this was causing an internal error
898 enew
899 set nowrap
900 call repeat('x', &columns)->setline(1)
901 call prop_type_add('textprop', {})
902 call prop_add(1, 1, #{length: &columns, type: 'textprop'})
903 vsplit
904 let opts = popup_create('', #{textprop: 'textprop'})
905 \ ->popup_getoptions()
906 \ ->extend(#{mask: [[1, 1, 1, 1]]})
907 call popup_create('', opts)
908 redraw
909
910 close!
911 bwipe!
912 call prop_type_delete('textprop')
913 call popup_clear()
914 set wrap&
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200915endfunc
916
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200917func Test_popup_select()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200918 CheckScreendump
919 CheckFeature clipboard_working
920
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200921 " create a popup with some text to be selected
922 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200923 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200924 call setline(1, range(1, 20))
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200925 let winid = popup_create(['the word', 'some more', 'several words here', 'invisible', '5', '6', '7'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200926 \ drag: 1,
927 \ border: [],
928 \ line: 3,
929 \ col: 10,
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200930 \ maxheight: 3,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200931 \ })
932 func Select1()
933 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
934 endfunc
935 map <silent> <F3> :call test_setmouse(4, 15)<CR>
936 map <silent> <F4> :call test_setmouse(6, 23)<CR>
937 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100938 call writefile(lines, 'XtestPopupSelect', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200939 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200940 call term_sendkeys(buf, ":call Select1()\<CR>")
941 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
942
943 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
944 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200945 " clean the command line, sometimes it still shows a command
946 call term_sendkeys(buf, ":\<esc>")
947
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200948 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
949
950 " clean up
951 call StopVimInTerminal(buf)
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200952endfunc
953
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200954func Test_popup_in_tab()
955 " default popup is local to tab, not visible when in other tab
956 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200957 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200958 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200959 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200960 tabnew
961 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200962 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200963 quit
964 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200965
966 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200967 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200968 " buffer is gone now
969 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200970
971 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200972 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200973 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200974 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200975 tabnew
976 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200977 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200978 quit
979 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200980 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200981
982 " create popup in other tab
983 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200984 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200985 call assert_equal(0, popup_getpos(winid).visible)
986 call assert_equal(1, popup_getoptions(winid).tabpage)
987 quit
988 call assert_equal(1, popup_getpos(winid).visible)
989 call assert_equal(0, popup_getoptions(winid).tabpage)
990 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200991endfunc
992
993func Test_popup_valid_arguments()
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200994 call assert_equal(0, len(popup_list()))
995
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200996 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200997 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200998 let pos = popup_getpos(winid)
999 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001000 call assert_equal([winid], popup_list())
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001001 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001002
1003 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001004 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001005 let pos = popup_getpos(winid)
1006 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001007 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001008
1009 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001010 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001011 let pos = popup_getpos(winid)
1012 let around = (&columns - pos.width) / 2
1013 call assert_inrange(around - 1, around + 1, pos.col)
1014 let around = (&lines - pos.height) / 2
1015 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001016 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001017endfunc
1018
1019func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001020 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001021 call popup_clear()
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001022 call assert_fails('call popup_create("text", "none")', 'E1206:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001023 call popup_clear()
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001024 call assert_fails('call popup_create(test_null_string(), {})', 'E450:')
1025 call assert_fails('call popup_create(test_null_list(), {})', 'E450:')
1026 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001027
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001028 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001029 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001030 call assert_fails('call popup_create("text", #{col: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001031 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001032 call assert_fails('call popup_create("text", #{col: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001033 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001034 call assert_fails('call popup_create("text", #{col: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001035 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001036
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001037 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001038 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001039 call assert_fails('call popup_create("text", #{line: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001040 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001041 call assert_fails('call popup_create("text", #{line: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001042 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001043 call assert_fails('call popup_create("text", #{line: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001044 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001045
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001046 call assert_fails('call popup_create("text", #{pos: "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001047 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001048 call assert_fails('call popup_create("text", #{padding: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001049 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001050 call assert_fails('call popup_create("text", #{border: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001051 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001052 call assert_fails('call popup_create("text", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001053 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +02001054 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
1055 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001056 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001057 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001058
Bram Moolenaar83bd7a92022-05-29 17:13:24 +01001059 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E1284: Argument 1, list item 2: Dictionary required')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001060 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001061 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001062 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001063 call assert_fails('call popup_create([#{text: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001064 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +01001065 call assert_fails('call popup_create([#{text: "text", props: range(3)}], {})', 'E715:')
1066 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +02001067 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
1068 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +01001069 call assert_fails('call popup_create("text", #{mask: range(5)})', 'E475:')
1070 call popup_clear()
1071 call popup_create("text", #{mask: [range(4)]})
1072 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +02001073 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001074 call assert_fails('call popup_create("text", #{mapping: []})', 'E745:')
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +02001075 call popup_clear()
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001076 call assert_fails('call popup_create("text", #{tabpage : 4})', 'E997:')
1077 call popup_clear()
Bram Moolenaar83bd7a92022-05-29 17:13:24 +01001078
1079 call assert_fails('call popup_create(range(10), {})', 'E1024:')
1080 call popup_clear()
1081 call assert_fails('call popup_create([1, 2], {})', 'E1284: Argument 1, list item 1: Dictionary required')
1082 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001083endfunc
1084
Bram Moolenaareea16992019-05-31 17:34:48 +02001085func Test_win_execute_closing_curwin()
1086 split
1087 let winid = popup_create('some text', {})
Bram Moolenaare2e40752020-09-04 21:18:46 +02001088 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001089 call popup_clear()
Bram Moolenaar49540192019-12-11 19:34:54 +01001090
1091 let winid = popup_create('some text', {})
Bram Moolenaare2e40752020-09-04 21:18:46 +02001092 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 +01001093 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001094endfunc
1095
1096func Test_win_execute_not_allowed()
1097 let winid = popup_create('some text', {})
1098 call assert_fails('call win_execute(winid, "split")', 'E994:')
1099 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
1100 call assert_fails('call win_execute(winid, "close")', 'E994:')
1101 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +02001102 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001103 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
1104 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
1105 call assert_fails('call win_execute(winid, "next")', 'E994:')
1106 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01001107 call assert_fails('call win_execute(winid, "pedit 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()
Bram Moolenaareea16992019-05-31 17:34:48 +02001122endfunc
1123
Bram Moolenaar402502d2019-05-30 22:07:36 +02001124func Test_popup_with_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001125 CheckScreendump
1126
Bram Moolenaar402502d2019-05-30 22:07:36 +02001127 let lines =<< trim END
1128 call setline(1, range(1, 100))
1129 let winid = popup_create(
1130 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001131 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001132 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001133 call writefile(lines, 'XtestPopup', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001134 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001135 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
1136
1137 " clean up
1138 call StopVimInTerminal(buf)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001139endfunc
1140
1141func Test_popup_without_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001142 CheckScreendump
1143
Bram Moolenaar402502d2019-05-30 22:07:36 +02001144 let lines =<< trim END
1145 call setline(1, range(1, 100))
1146 let winid = popup_create(
1147 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001148 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001149 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001150 call writefile(lines, 'XtestPopup', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001151 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001152 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
1153
1154 " clean up
1155 call StopVimInTerminal(buf)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001156endfunc
1157
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001158func Test_popup_with_showbreak()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001159 CheckScreendump
1160
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001161 let lines =<< trim END
1162 set showbreak=>>\
1163 call setline(1, range(1, 20))
1164 let winid = popup_dialog(
Bram Moolenaar8ae54372019-09-15 18:11:16 +02001165 \ 'a long line here that wraps',
1166 \ #{filter: 'popup_filter_yesno',
1167 \ maxwidth: 12})
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001168 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001169 call writefile(lines, 'XtestPopupShowbreak', 'D')
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001170 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
1171 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
1172
1173 " clean up
1174 call term_sendkeys(buf, "y")
1175 call StopVimInTerminal(buf)
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001176endfunc
1177
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001178func Test_popup_time()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001179 CheckFeature timers
1180
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001181 topleft vnew
1182 call setline(1, 'hello')
1183
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001184 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001185 \ line: 1,
1186 \ col: 1,
1187 \ minwidth: 20,
1188 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001189 \})
1190 redraw
1191 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1192 call assert_equal('world', line)
1193
Bram Moolenaarb4f06282019-07-12 21:07:54 +02001194 call assert_equal(winid, popup_locate(1, 1))
1195 call assert_equal(winid, popup_locate(1, 20))
1196 call assert_equal(0, popup_locate(1, 21))
1197 call assert_equal(0, popup_locate(2, 1))
1198
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001199 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +02001200 redraw
Bram Moolenaar196b4662019-09-06 21:34:30 +02001201 let line = join(map(range(1, 5), '1->screenstring(v:val)'), '')
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001202 call assert_equal('hello', line)
1203
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001204 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001205 \ line: &lines,
1206 \ col: 10,
1207 \ minwidth: 20,
1208 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001209 \})
1210 redraw
1211 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1212 call assert_match('.*on the command line.*', line)
1213
1214 sleep 700m
1215 redraw
1216 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1217 call assert_notmatch('.*on the command line.*', line)
1218
1219 bwipe!
1220endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001221
1222func Test_popup_hide()
1223 topleft vnew
1224 call setline(1, 'hello')
1225
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001226 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001227 \ line: 1,
1228 \ col: 1,
1229 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001230 \})
1231 redraw
1232 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1233 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001234 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001235 " buffer is still listed and active
1236 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001237
1238 call popup_hide(winid)
1239 redraw
1240 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1241 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001242 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001243 " buffer is still listed but hidden
Bram Moolenaarca7c0782020-01-14 20:42:48 +01001244 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001245
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001246 eval winid->popup_show()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001247 redraw
1248 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1249 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001250 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001251
1252
1253 call popup_close(winid)
1254 redraw
1255 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1256 call assert_equal('hello', line)
1257
1258 " error is given for existing non-popup window
1259 call assert_fails('call popup_hide(win_getid())', 'E993:')
1260
1261 " no error non-existing window
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001262 eval 1234234->popup_hide()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001263 call popup_show(41234234)
1264
1265 bwipe!
1266endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001267
1268func Test_popup_move()
1269 topleft vnew
1270 call setline(1, 'hello')
1271
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001272 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001273 \ line: 1,
1274 \ col: 1,
1275 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001276 \})
1277 redraw
1278 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1279 call assert_equal('world ', line)
1280
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001281 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001282 redraw
1283 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1284 call assert_equal('hello ', line)
1285 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
1286 call assert_equal('~world', line)
1287
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001288 eval winid->popup_move(#{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001289 redraw
1290 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1291 call assert_equal('hworld', line)
1292
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001293 call assert_fails('call popup_move(winid, [])', 'E1206:')
1294 call assert_fails('call popup_move(winid, test_null_dict())', 'E1297:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001295
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001296 call popup_close(winid)
1297
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001298 call assert_equal(0, popup_move(-1, {}))
1299
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001300 bwipe!
1301endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +02001302
Bram Moolenaar402502d2019-05-30 22:07:36 +02001303func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001304 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001305 \ line: 2,
1306 \ col: 3,
1307 \ minwidth: 10,
1308 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +02001309 \})
1310 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001311 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001312 call assert_equal(2, res.line)
1313 call assert_equal(3, res.col)
1314 call assert_equal(10, res.width)
1315 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001316 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001317
1318 call popup_close(winid)
1319endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001320
1321func Test_popup_width_longest()
1322 let tests = [
1323 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
1324 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
1325 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
1326 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
1327 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
1328 \ ]
1329
1330 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001331 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001332 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001333 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001334 call assert_equal(test[1], position.width)
1335 call popup_close(winid)
1336 endfor
1337endfunc
1338
1339func Test_popup_wraps()
1340 let tests = [
1341 \ ['nowrap', 6, 1],
1342 \ ['a line that wraps once', 12, 2],
1343 \ ['a line that wraps two times', 12, 3],
1344 \ ]
1345 for test in tests
1346 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001347 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001348 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001349 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001350 call assert_equal(test[1], position.width)
1351 call assert_equal(test[2], position.height)
1352
1353 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001354 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001355 endfor
1356endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001357
1358func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001359 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001360 \ line: 2,
1361 \ col: 3,
1362 \ minwidth: 10,
1363 \ minheight: 11,
1364 \ maxwidth: 20,
1365 \ maxheight: 21,
1366 \ zindex: 100,
1367 \ time: 5000,
1368 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001369 \})
1370 redraw
1371 let res = popup_getoptions(winid)
1372 call assert_equal(2, res.line)
1373 call assert_equal(3, res.col)
1374 call assert_equal(10, res.minwidth)
1375 call assert_equal(11, res.minheight)
1376 call assert_equal(20, res.maxwidth)
1377 call assert_equal(21, res.maxheight)
1378 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001379 call assert_equal(1, res.fixed)
Bram Moolenaarb8350ab2019-08-04 17:59:49 +02001380 call assert_equal(1, res.mapping)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001381 if has('timers')
1382 call assert_equal(5000, res.time)
1383 endif
1384 call popup_close(winid)
1385
1386 let winid = popup_create('hello', {})
1387 redraw
1388 let res = popup_getoptions(winid)
1389 call assert_equal(0, res.line)
1390 call assert_equal(0, res.col)
1391 call assert_equal(0, res.minwidth)
1392 call assert_equal(0, res.minheight)
1393 call assert_equal(0, res.maxwidth)
1394 call assert_equal(0, res.maxheight)
1395 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001396 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001397 if has('timers')
1398 call assert_equal(0, res.time)
1399 endif
1400 call popup_close(winid)
1401 call assert_equal({}, popup_getoptions(winid))
1402endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001403
1404func Test_popup_option_values()
1405 new
1406 " window-local
1407 setlocal number
1408 setlocal nowrap
1409 " buffer-local
1410 setlocal omnifunc=Something
1411 " global/buffer-local
1412 setlocal path=/there
1413 " global/window-local
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001414 setlocal statusline=2
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001415
1416 let winid = popup_create('hello', {})
1417 call assert_equal(0, getwinvar(winid, '&number'))
1418 call assert_equal(1, getwinvar(winid, '&wrap'))
1419 call assert_equal('', getwinvar(winid, '&omnifunc'))
1420 call assert_equal(&g:path, getwinvar(winid, '&path'))
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001421 call assert_equal(&g:statusline, getwinvar(winid, '&statusline'))
1422
1423 " 'scrolloff' is reset to zero
1424 call assert_equal(5, &scrolloff)
1425 call assert_equal(0, getwinvar(winid, '&scrolloff'))
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001426
1427 call popup_close(winid)
1428 bwipe
1429endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001430
1431func Test_popup_atcursor()
1432 topleft vnew
1433 call setline(1, [
1434 \ 'xxxxxxxxxxxxxxxxx',
1435 \ 'xxxxxxxxxxxxxxxxx',
1436 \ 'xxxxxxxxxxxxxxxxx',
1437 \])
1438
1439 call cursor(2, 2)
1440 redraw
1441 let winid = popup_atcursor('vim', {})
1442 redraw
1443 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1444 call assert_equal('xvimxxxxxxxxxxxxx', line)
1445 call popup_close(winid)
1446
1447 call cursor(3, 4)
1448 redraw
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001449 let winid = 'vim'->popup_atcursor({})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001450 redraw
1451 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
1452 call assert_equal('xxxvimxxxxxxxxxxx', line)
1453 call popup_close(winid)
1454
1455 call cursor(1, 1)
1456 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001457 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001458 \ line: 'cursor+2',
1459 \ col: 'cursor+1',
1460 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001461 redraw
1462 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1463 call assert_equal('xvimxxxxxxxxxxxxx', line)
1464 call popup_close(winid)
1465
1466 call cursor(3, 3)
1467 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001468 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001469 \ line: 'cursor-2',
1470 \ col: 'cursor-1',
1471 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001472 redraw
1473 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1474 call assert_equal('xvimxxxxxxxxxxxxx', line)
1475 call popup_close(winid)
1476
Bram Moolenaar402502d2019-05-30 22:07:36 +02001477 " just enough room above
1478 call cursor(3, 3)
1479 redraw
1480 let winid = popup_atcursor(['vim', 'is great'], {})
1481 redraw
1482 let pos = popup_getpos(winid)
1483 call assert_equal(1, pos.line)
1484 call popup_close(winid)
1485
1486 " not enough room above, popup goes below the cursor
1487 call cursor(3, 3)
1488 redraw
1489 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1490 redraw
1491 let pos = popup_getpos(winid)
1492 call assert_equal(4, pos.line)
1493 call popup_close(winid)
1494
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001495 " cursor in first line, popup in line 2
1496 call cursor(1, 1)
1497 redraw
1498 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1499 redraw
1500 let pos = popup_getpos(winid)
1501 call assert_equal(2, pos.line)
1502 call popup_close(winid)
1503
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001504 bwipe!
1505endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001506
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001507func Test_popup_atcursor_pos()
1508 CheckScreendump
Dominique Pellec60e9592021-07-03 21:41:38 +02001509 CheckFeature conceal
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001510
1511 let lines =<< trim END
1512 call setline(1, repeat([repeat('-', 60)], 15))
1513 set so=0
1514
1515 normal 9G3|r#
1516 let winid1 = popup_atcursor(['first', 'second'], #{
1517 \ moved: [0, 0, 0],
1518 \ })
1519 normal 9G21|r&
1520 let winid1 = popup_atcursor(['FIrsT', 'SEcoND'], #{
1521 \ pos: 'botright',
1522 \ moved: [0, 0, 0],
1523 \ })
1524 normal 3G27|r%
1525 let winid1 = popup_atcursor(['fiRSt', 'seCOnd'], #{
1526 \ pos: 'topleft',
1527 \ moved: [0, 0, 0],
1528 \ })
1529 normal 3G45|r@
1530 let winid1 = popup_atcursor(['First', 'SeconD'], #{
1531 \ pos: 'topright',
Bram Moolenaarb0992022020-01-30 14:55:42 +01001532 \ moved: range(3),
1533 \ mousemoved: range(3),
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001534 \ })
Bram Moolenaarea042672021-06-29 20:22:32 +02001535
1536 normal 9G27|Rconcealed X
1537 syn match Hidden /concealed/ conceal
1538 set conceallevel=2 concealcursor=n
1539 redraw
1540 normal 0fX
1541 call popup_atcursor('mark', {})
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001542 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001543 call writefile(lines, 'XtestPopupAtcursorPos', 'D')
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001544 let buf = RunVimInTerminal('-S XtestPopupAtcursorPos', #{rows: 12})
1545 call VerifyScreenDump(buf, 'Test_popupwin_atcursor_pos', {})
1546
1547 " clean up
1548 call StopVimInTerminal(buf)
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001549endfunc
1550
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001551func Test_popup_beval()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001552 CheckScreendump
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001553 CheckFeature balloon_eval_term
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001554
1555 let lines =<< trim END
1556 call setline(1, range(1, 20))
1557 call setline(5, 'here is some text to hover over')
1558 set balloonevalterm
1559 set balloonexpr=BalloonExpr()
1560 set balloondelay=100
1561 func BalloonExpr()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001562 let s:winid = [v:beval_text]->popup_beval({})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001563 return ''
1564 endfunc
1565 func Hover()
1566 call test_setmouse(5, 15)
1567 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1568 sleep 100m
1569 endfunc
1570 func MoveOntoPopup()
1571 call test_setmouse(4, 17)
1572 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1573 endfunc
1574 func MoveAway()
1575 call test_setmouse(5, 13)
1576 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1577 endfunc
1578 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001579 call writefile(lines, 'XtestPopupBeval', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001580 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001581 call TermWait(buf, 50)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001582 call term_sendkeys(buf, 'j')
1583 call term_sendkeys(buf, ":call Hover()\<CR>")
1584 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1585
1586 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1587 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1588
1589 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1590 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1591
1592 " clean up
1593 call StopVimInTerminal(buf)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001594endfunc
1595
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001596func Test_popup_filter()
1597 new
1598 call setline(1, 'some text')
1599
1600 func MyPopupFilter(winid, c)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001601 if a:c == 'e' || a:c == "\<F9>"
1602 let g:eaten = a:c
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001603 return 1
1604 endif
1605 if a:c == '0'
1606 let g:ignored = '0'
1607 return 0
1608 endif
1609 if a:c == 'x'
1610 call popup_close(a:winid)
1611 return 1
1612 endif
1613 return 0
1614 endfunc
1615
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001616 let winid = 'something'->popup_create(#{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001617 redraw
1618
1619 " e is consumed by the filter
Bram Moolenaar999db232021-07-04 14:00:55 +02001620 let g:eaten = ''
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001621 call feedkeys('e', 'xt')
1622 call assert_equal('e', g:eaten)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001623 call feedkeys("\<F9>", 'xt')
1624 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001625
1626 " 0 is ignored by the filter
Bram Moolenaar999db232021-07-04 14:00:55 +02001627 let g:ignored = ''
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001628 normal $
1629 call assert_equal(9, getcurpos()[2])
1630 call feedkeys('0', 'xt')
1631 call assert_equal('0', g:ignored)
Bram Moolenaar999db232021-07-04 14:00:55 +02001632
1633 if has('win32') && has('gui_running')
1634 echo "FIXME: this check is very flaky on MS-Windows GUI, the cursor doesn't move"
1635 else
1636 call assert_equal(1, getcurpos()[2])
1637 endif
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001638
1639 " x closes the popup
1640 call feedkeys('x', 'xt')
Bram Moolenaarec084d32020-02-28 22:44:47 +01001641 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001642 call assert_equal(-1, winbufnr(winid))
1643
Bram Moolenaar999db232021-07-04 14:00:55 +02001644 unlet g:eaten
1645 unlet g:ignored
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001646 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001647 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001648endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001649
Bram Moolenaar4934ad02020-09-28 22:29:58 +02001650" this tests that the filter is not used for :normal command
Bram Moolenaar189832b2020-09-23 12:29:11 +02001651func Test_popup_filter_normal_cmd()
1652 CheckScreendump
1653
1654 let lines =<< trim END
Bram Moolenaar4934ad02020-09-28 22:29:58 +02001655 let text = range(1, 20)->map({_, v -> string(v)})
1656 let g:winid = popup_create(text, #{maxheight: 5, minwidth: 3, filter: 'invalidfilter'})
1657 call timer_start(0, {-> win_execute(g:winid, 'norm! 10Gzz')})
Bram Moolenaar189832b2020-09-23 12:29:11 +02001658 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001659 call writefile(lines, 'XtestPopupNormal', 'D')
Bram Moolenaar189832b2020-09-23 12:29:11 +02001660 let buf = RunVimInTerminal('-S XtestPopupNormal', #{rows: 10})
1661 call TermWait(buf, 100)
1662 call VerifyScreenDump(buf, 'Test_popupwin_normal_cmd', {})
1663
1664 call StopVimInTerminal(buf)
Bram Moolenaar189832b2020-09-23 12:29:11 +02001665endfunc
1666
Bram Moolenaar10476522020-09-24 22:57:31 +02001667" test that cursor line highlight is updated after using win_execute()
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001668func Test_popup_filter_win_execute()
1669 CheckScreendump
1670
1671 let lines =<< trim END
Bram Moolenaar10476522020-09-24 22:57:31 +02001672 let lines = range(1, &lines * 2)->map({_, v -> string(v)})
1673 let g:id = popup_create(lines, #{
1674 \ minheight: &lines - 5,
1675 \ maxheight: &lines - 5,
1676 \ cursorline: 1,
1677 \ })
1678 redraw
1679 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001680 call writefile(lines, 'XtestPopupWinExecute', 'D')
Bram Moolenaar10476522020-09-24 22:57:31 +02001681 let buf = RunVimInTerminal('-S XtestPopupWinExecute', #{rows: 14})
1682
1683 call term_sendkeys(buf, ":call win_execute(g:id, ['normal 17Gzz'])\<CR>")
1684 call term_sendkeys(buf, ":\<CR>")
1685
1686 call VerifyScreenDump(buf, 'Test_popupwin_win_execute_cursorline', {})
1687
1688 call StopVimInTerminal(buf)
Bram Moolenaar10476522020-09-24 22:57:31 +02001689endfunc
1690
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001691func Test_popup_set_firstline()
1692 CheckScreendump
1693
1694 let lines =<< trim END
1695 let lines = range(1, 50)->map({_, v -> string(v)})
1696 let g:id = popup_create(lines, #{
1697 \ minwidth: 20,
1698 \ maxwidth: 20,
1699 \ minheight: &lines - 5,
1700 \ maxheight: &lines - 5,
1701 \ cursorline: 1,
1702 \ })
1703 call popup_setoptions(g:id, #{firstline: 10})
1704 redraw
1705 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001706 call writefile(lines, 'XtestPopupWinSetFirstline', 'D')
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001707 let buf = RunVimInTerminal('-S XtestPopupWinSetFirstline', #{rows: 16})
1708
1709 call VerifyScreenDump(buf, 'Test_popupwin_set_firstline_1', {})
1710
1711 call term_sendkeys(buf, ":call popup_setoptions(g:id, #{firstline: 5})\<CR>")
1712 call term_sendkeys(buf, ":\<CR>")
1713 call VerifyScreenDump(buf, 'Test_popupwin_set_firstline_2', {})
1714
1715 call StopVimInTerminal(buf)
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001716endfunc
1717
Bram Moolenaar10476522020-09-24 22:57:31 +02001718" this tests that we don't get stuck with an error in "win_execute()"
1719func Test_popup_filter_win_execute_error()
1720 CheckScreendump
1721
1722 let lines =<< trim END
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001723 let g:winid = popup_create('some text', {'filter': 'invalidfilter'})
1724 call timer_start(0, {-> win_execute(g:winid, 'invalidCommand')})
1725 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001726 call writefile(lines, 'XtestPopupWinExecuteError', 'D')
Bram Moolenaar10476522020-09-24 22:57:31 +02001727 let buf = RunVimInTerminal('-S XtestPopupWinExecuteError', #{rows: 10, wait_for_ruler: 0})
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001728
1729 call WaitFor({-> term_getline(buf, 9) =~ 'Not an editor command: invalidCommand'})
1730 call term_sendkeys(buf, "\<CR>")
1731 call WaitFor({-> term_getline(buf, 9) =~ 'Unknown function: invalidfilter'})
1732 call term_sendkeys(buf, "\<CR>")
1733 call WaitFor({-> term_getline(buf, 9) =~ 'Not allowed in a popup window'})
1734 call term_sendkeys(buf, "\<CR>")
1735 call term_sendkeys(buf, "\<CR>")
1736 call VerifyScreenDump(buf, 'Test_popupwin_win_execute', {})
1737
1738 call StopVimInTerminal(buf)
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001739endfunc
1740
Bram Moolenaara42d9452019-06-15 21:46:30 +02001741func ShowDialog(key, result)
1742 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001743 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001744 \ filter: 'popup_filter_yesno',
1745 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001746 \ })
1747 redraw
1748 call feedkeys(a:key, "xt")
1749 call assert_equal(winid, s:cb_winid)
1750 call assert_equal(a:result, s:cb_res)
1751endfunc
1752
1753func Test_popup_dialog()
1754 func QuitCallback(id, res)
1755 let s:cb_winid = a:id
1756 let s:cb_res = a:res
1757 endfunc
1758
1759 let winid = ShowDialog("y", 1)
1760 let winid = ShowDialog("Y", 1)
1761 let winid = ShowDialog("n", 0)
1762 let winid = ShowDialog("N", 0)
1763 let winid = ShowDialog("x", 0)
1764 let winid = ShowDialog("X", 0)
1765 let winid = ShowDialog("\<Esc>", 0)
1766 let winid = ShowDialog("\<C-C>", -1)
1767
1768 delfunc QuitCallback
1769endfunc
1770
Bram Moolenaara730e552019-06-16 19:05:31 +02001771func ShowMenu(key, result)
1772 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001773 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001774 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001775 \ })
1776 redraw
1777 call feedkeys(a:key, "xt")
1778 call assert_equal(winid, s:cb_winid)
1779 call assert_equal(a:result, s:cb_res)
1780endfunc
1781
1782func Test_popup_menu()
1783 func QuitCallback(id, res)
1784 let s:cb_winid = a:id
1785 let s:cb_res = a:res
1786 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001787 " mapping won't be used in popup
1788 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001789
1790 let winid = ShowMenu(" ", 1)
1791 let winid = ShowMenu("j \<CR>", 2)
1792 let winid = ShowMenu("JjK \<CR>", 2)
1793 let winid = ShowMenu("jjjjjj ", 3)
1794 let winid = ShowMenu("kkk ", 1)
1795 let winid = ShowMenu("x", -1)
1796 let winid = ShowMenu("X", -1)
1797 let winid = ShowMenu("\<Esc>", -1)
1798 let winid = ShowMenu("\<C-C>", -1)
1799
1800 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001801 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001802endfunc
1803
1804func Test_popup_menu_screenshot()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001805 CheckScreendump
Bram Moolenaara730e552019-06-16 19:05:31 +02001806
1807 let lines =<< trim END
1808 call setline(1, range(1, 20))
1809 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001810 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001811 func MenuDone(id, res)
1812 echomsg "selected " .. a:res
1813 endfunc
1814 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001815 call writefile(lines, 'XtestPopupMenu', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001816 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001817 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1818
1819 call term_sendkeys(buf, "jj")
1820 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1821
1822 call term_sendkeys(buf, " ")
1823 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1824
1825 " clean up
1826 call StopVimInTerminal(buf)
Bram Moolenaara730e552019-06-16 19:05:31 +02001827endfunc
1828
Bram Moolenaarf914a332019-07-20 15:09:56 +02001829func Test_popup_menu_narrow()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001830 CheckScreendump
Bram Moolenaarf914a332019-07-20 15:09:56 +02001831
1832 let lines =<< trim END
1833 call setline(1, range(1, 20))
1834 hi PopupSelected ctermbg=green
1835 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1836 func MenuDone(id, res)
1837 echomsg "selected " .. a:res
1838 endfunc
1839 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001840 call writefile(lines, 'XtestPopupNarrowMenu', 'D')
Bram Moolenaarf914a332019-07-20 15:09:56 +02001841 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1842 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1843
1844 " clean up
1845 call term_sendkeys(buf, "x")
1846 call StopVimInTerminal(buf)
Bram Moolenaarf914a332019-07-20 15:09:56 +02001847endfunc
1848
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001849func Test_popup_title()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001850 CheckScreendump
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001851
1852 " Create a popup without title or border, a line of padding will be added to
1853 " put the title on.
1854 let lines =<< trim END
1855 call setline(1, range(1, 20))
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001856 let winid = popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001857 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001858 call writefile(lines, 'XtestPopupTitle', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001859 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001860 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1861
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001862 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxwidth: 20, title: 'a very long title that is not going to fit'})\<CR>")
1863 call term_sendkeys(buf, ":\<CR>")
1864 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_1', {})
1865
1866 call term_sendkeys(buf, ":call popup_setoptions(winid, #{border: []})\<CR>")
1867 call term_sendkeys(buf, ":\<CR>")
1868 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_2', {})
1869
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01001870 call term_sendkeys(buf, ":call popup_clear()\<CR>")
1871 call term_sendkeys(buf, ":call popup_create(['aaa', 'bbb'], #{title: 'Title', minwidth: 12, padding: [2, 2, 2, 2]})\<CR>")
1872 call term_sendkeys(buf, ":\<CR>")
1873 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_3', {})
1874
1875 call term_sendkeys(buf, ":call popup_clear()\<CR>")
1876 call term_sendkeys(buf, ":call popup_create(['aaa', 'bbb'], #{title: 'Title', minwidth: 12, border: [], padding: [2, 2, 2, 2]})\<CR>")
1877 call term_sendkeys(buf, ":\<CR>")
1878 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_4', {})
1879
Ralf Schandlbc869872021-05-28 14:12:14 +02001880 call term_sendkeys(buf, ":call popup_clear()\<CR>")
rbtnnc6119412021-08-07 13:08:45 +02001881 call term_sendkeys(buf, ":call popup_menu(['This is a line', 'and another line'], #{title: '▶Äあいうえお◀', })\<CR>")
Ralf Schandlbc869872021-05-28 14:12:14 +02001882 call VerifyScreenDump(buf, 'Test_popupwin_multibytetitle', {})
1883 call term_sendkeys(buf, "x")
1884
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001885 " clean up
1886 call StopVimInTerminal(buf)
Bram Moolenaarae943152019-06-16 22:54:14 +02001887
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001888 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001889 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001890 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001891 call assert_equal('Another Title', popup_getoptions(winid).title)
1892
1893 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001894endfunc
1895
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001896func Test_popup_close_callback()
1897 func PopupDone(id, result)
1898 let g:result = a:result
1899 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001900 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001901 redraw
1902 call popup_close(winid, 'done')
1903 call assert_equal('done', g:result)
1904endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001905
1906func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001907 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001908 redraw
1909 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001910 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001911 call assert_equal(5, pos.height)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001912 call popup_close(winid)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001913
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001914 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001915 redraw
1916 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001917 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001918 call assert_equal(3, pos.height)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001919 call popup_close(winid)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001920endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001921
1922func Test_popup_never_behind()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001923 CheckScreendump
1924
Bram Moolenaar988c4332019-06-02 14:12:11 +02001925 " +-----------------------------+
1926 " | | |
1927 " | | |
1928 " | | |
1929 " | line1 |
1930 " |------------line2------------|
1931 " | line3 |
1932 " | line4 |
1933 " | |
1934 " | |
1935 " +-----------------------------+
1936 let lines =<< trim END
Bram Moolenaar988c4332019-06-02 14:12:11 +02001937 split
1938 vsplit
1939 let info_window1 = getwininfo()[0]
1940 let line = info_window1['height']
1941 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001942 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001943 \ line : line,
1944 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001945 \ })
1946 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001947 call writefile(lines, 'XtestPopupBehind', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001948 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001949 call term_sendkeys(buf, "\<C-W>w")
1950 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1951
1952 " clean up
1953 call StopVimInTerminal(buf)
Bram Moolenaar988c4332019-06-02 14:12:11 +02001954endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001955
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001956func s:VerifyPosition(p, msg, line, col, width, height)
1957 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1958 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1959 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1960 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001961endfunc
1962
1963func Test_popup_position_adjust()
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001964 " Anything placed past the last cell on the right of the screen is moved to
1965 " the left.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001966 "
1967 " When wrapping is disabled, we also shift to the left to display on the
1968 " screen, unless fixed is set.
1969
1970 " Entries for cases which don't vary based on wrapping.
1971 " Format is per tests described below
1972 let both_wrap_tests = [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001973 \ ['a', 5, &columns, 5, &columns, 1, 1],
1974 \ ['b', 5, &columns + 1, 5, &columns, 1, 1],
1975 \ ['c', 5, &columns - 1, 5, &columns - 1, 1, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001976 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001977 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1]]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001978
1979 " these test groups are dicts with:
1980 " - comment: something to identify the group of tests by
1981 " - options: dict of options to merge with the row/col in tests
1982 " - tests: list of cases. Each one is a list with elements:
1983 " - text
1984 " - row
1985 " - col
1986 " - expected row
1987 " - expected col
1988 " - expected width
1989 " - expected height
1990 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001991 \ #{
1992 \ comment: 'left-aligned with wrapping',
1993 \ options: #{
1994 \ wrap: 1,
1995 \ pos: 'botleft',
1996 \ },
1997 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001998 \ ['aa', 5, &columns, 4, &columns, 1, 2],
1999 \ ['bb', 5, &columns + 1, 4, &columns, 1, 2],
2000 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
2001 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
2002 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
2003 \
2004 \ ['aaa', 5, &columns, 3, &columns, 1, 3],
2005 \ ['bbb', 5, &columns + 1, 3, &columns, 1, 3],
2006 \ ['ccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
2007 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
2008 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
2009 \
2010 \ ['aaaa', 5, &columns, 2, &columns, 1, 4],
2011 \ ['bbbb', 5, &columns + 1, 2, &columns, 1, 4],
2012 \ ['cccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002013 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
2014 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01002015 \ ['eeee', 5, &columns - 4, 5, &columns - 4, 4, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002016 \ ],
2017 \ },
2018 \ #{
2019 \ comment: 'left aligned without wrapping',
2020 \ options: #{
2021 \ wrap: 0,
2022 \ pos: 'botleft',
2023 \ },
2024 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01002025 \ ['aa', 5, &columns, 5, &columns - 1, 2, 1],
2026 \ ['bb', 5, &columns + 1, 5, &columns - 1, 2, 1],
2027 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
2028 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
2029 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
2030 \
2031 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
2032 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
2033 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
2034 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
2035 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
2036 \
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002037 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
2038 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
2039 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
2040 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
2041 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
2042 \ ],
2043 \ },
2044 \ #{
2045 \ comment: 'left aligned with fixed position',
2046 \ options: #{
2047 \ wrap: 0,
2048 \ fixed: 1,
2049 \ pos: 'botleft',
2050 \ },
2051 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01002052 \ ['aa', 5, &columns, 5, &columns, 1, 1],
2053 \ ['bb', 5, &columns + 1, 5, &columns, 1, 1],
2054 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
2055 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
2056 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
2057 \
2058 \ ['aaa', 5, &columns, 5, &columns, 1, 1],
2059 \ ['bbb', 5, &columns + 1, 5, &columns, 1, 1],
2060 \ ['ccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
2061 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
2062 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
2063 \
2064 \ ['aaaa', 5, &columns, 5, &columns, 1, 1],
2065 \ ['bbbb', 5, &columns + 1, 5, &columns, 1, 1],
2066 \ ['cccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002067 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
2068 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
2069 \ ],
2070 \ },
2071 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002072
2073 for test_group in tests
2074 for test in test_group.tests
2075 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002076 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002077 \ line: line,
2078 \ col: col,
2079 \ }
2080 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002081
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002082 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002083
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002084 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002085 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
2086 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002087 endfor
2088 endfor
2089
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002090 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002091 %bwipe!
2092endfunc
2093
Bram Moolenaar3397f742019-06-02 18:40:06 +02002094func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002095 " width of screen
2096 let X = join(map(range(&columns), {->'X'}), '')
2097
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002098 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
2099 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002100
2101 redraw
2102 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2103 call assert_equal(X, line)
2104
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002105 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002106 redraw
2107
2108 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002109 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
2110 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002111
2112 redraw
2113 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2114 call assert_equal(X, line)
2115
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002116 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002117 redraw
2118
2119 " Extend so > window width
2120 let X .= 'x'
2121
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002122 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
2123 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002124
2125 redraw
2126 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2127 call assert_equal(X[ : -2 ], line)
2128
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002129 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002130 redraw
2131
2132 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002133 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
2134 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002135
2136 redraw
2137 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2138 call assert_equal(X[ : -2 ], line)
2139
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002140 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002141 redraw
2142
2143 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002144 let p = popup_create(X,
2145 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
2146 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002147
2148 redraw
2149 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2150 let e_line = ' ' . X[ 1 : -2 ]
2151 call assert_equal(e_line, line)
2152
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002153 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002154 redraw
2155
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002156 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002157 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02002158endfunc
2159
2160func Test_popup_moved()
2161 new
2162 call test_override('char_avail', 1)
2163 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
2164
2165 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002166 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002167 redraw
2168 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002169 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002170 " trigger the check for last_cursormoved by going into insert mode
2171 call feedkeys("li\<Esc>", 'xt')
2172 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002173 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002174
2175 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002176 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002177 redraw
2178 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002179 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002180 call feedkeys("hi\<Esc>", 'xt')
2181 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002182 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002183
2184 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002185 let winid = popup_atcursor('text', #{moved: 'word'})
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, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002189 call feedkeys("li\<Esc>", 'xt')
2190 call assert_equal(1, popup_getpos(winid).visible)
2191 call feedkeys("ei\<Esc>", 'xt')
2192 call assert_equal(1, popup_getpos(winid).visible)
2193 call feedkeys("eli\<Esc>", 'xt')
2194 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002195 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002196
Bram Moolenaar17627312019-06-02 19:53:44 +02002197 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02002198 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02002199 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002200 redraw
2201 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002202 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002203 call feedkeys("eli\<Esc>", 'xt')
2204 call assert_equal(1, popup_getpos(winid).visible)
2205 call feedkeys("wi\<Esc>", 'xt')
2206 call assert_equal(1, popup_getpos(winid).visible)
2207 call feedkeys("Eli\<Esc>", 'xt')
2208 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002209 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002210
2211 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002212 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002213 redraw
2214 call assert_equal(1, popup_getpos(winid).visible)
2215 call feedkeys("eli\<Esc>", 'xt')
2216 call feedkeys("ei\<Esc>", 'xt')
2217 call assert_equal(1, popup_getpos(winid).visible)
2218 call feedkeys("eli\<Esc>", 'xt')
2219 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002220 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002221
2222 bwipe!
2223 call test_override('ALL', 0)
2224endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002225
2226func Test_notifications()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002227 CheckFeature timers
2228 CheckScreendump
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002229
Bram Moolenaar0fdddee2019-09-01 15:26:23 +02002230 let lines =<< trim END
2231 call setline(1, range(1, 20))
2232 hi Notification ctermbg=lightblue
2233 call popup_notification('first notification', {})
2234 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01002235 call writefile(lines, 'XtestNotifications', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002236 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002237 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
2238
2239 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002240 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
2241 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002242 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
2243
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002244 " clean up
2245 call StopVimInTerminal(buf)
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002246endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002247
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002248func Test_popup_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002249 CheckScreendump
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002250
2251 let lines =<< trim END
2252 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02002253 hi ScrollThumb ctermbg=blue
2254 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002255 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002256 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002257 \ minwidth: 8,
2258 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002259 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002260 func ScrollUp()
2261 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
2262 endfunc
2263 func ScrollDown()
2264 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
2265 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002266 func ClickTop()
2267 call feedkeys("\<F4>\<LeftMouse>", "xt")
2268 endfunc
2269 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002270 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002271 call feedkeys("\<F5>\<LeftMouse>", "xt")
2272 endfunc
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002273 func Popup_filter(winid, key)
2274 if a:key == 'j'
Bram Moolenaar371806e2020-10-22 13:44:54 +02002275 silent! this_throws_an_error_but_is_ignored
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002276 let line = popup_getoptions(a:winid).firstline
2277 let nlines = line('$', a:winid)
2278 let newline = line < nlines ? (line + 1) : nlines
2279 call popup_setoptions(a:winid, #{firstline: newline})
2280 return v:true
2281 elseif a:key == 'x'
2282 call popup_close(a:winid)
2283 return v:true
2284 endif
2285 endfunc
2286
Bram Moolenaarfc376e02022-05-29 18:18:18 +01002287 def CreatePopup(text: list<string>): number
2288 return popup_create(text, {
Bram Moolenaar6c542f72020-09-27 21:16:45 +02002289 \ minwidth: 30,
2290 \ maxwidth: 30,
2291 \ minheight: 4,
2292 \ maxheight: 4,
2293 \ firstline: 1,
2294 \ lastline: 4,
2295 \ wrap: true,
2296 \ scrollbar: true,
2297 \ mapping: false,
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002298 \ filter: g:Popup_filter,
Bram Moolenaar6c542f72020-09-27 21:16:45 +02002299 \ })
2300 enddef
2301
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002302 func PopupScroll()
2303 call popup_clear()
2304 let text =<< trim END
2305 1
2306 2
2307 3
2308 4
2309 long line long line long line long line long line long line
2310 long line long line long line long line long line long line
2311 long line long line long line long line long line long line
2312 END
Bram Moolenaar6c542f72020-09-27 21:16:45 +02002313 call CreatePopup(text)
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002314 endfunc
Bram Moolenaarfc376e02022-05-29 18:18:18 +01002315 func ScrollBottom()
2316 call popup_clear()
Bram Moolenaar89b25582022-05-30 13:20:56 +01002317 let id = CreatePopup(range(100)->map({k, v -> string(v)}))
2318 call popup_setoptions(id, #{firstline: 100, minheight: 9, maxheight: 9})
Bram Moolenaarfc376e02022-05-29 18:18:18 +01002319 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002320 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002321 map <silent> <F4> :call test_setmouse(4, 42)<CR>
2322 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002323 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01002324 call writefile(lines, 'XtestPopupScroll', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002325 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002326 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
2327
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002328 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002329 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002330 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
2331
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002332 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002333 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002334 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
2335
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002336 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002337 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002338 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
2339
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02002340 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb', firstline: 5})\<CR>")
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002341 " this scrolls two lines (half the window height)
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002342 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
2343 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
2344
2345 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2346 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
2347
2348 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02002349 " wait a bit, otherwise it fails sometimes (double click recognized?)
2350 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002351 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2352 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
2353
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002354 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2355 sleep 100m
2356 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2357 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
2358
2359 call term_sendkeys(buf, ":call ClickBot()\<CR>")
2360 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
2361
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002362 " remove the minwidth and maxheight
2363 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxheight: 0, minwidth: 0})\<CR>")
Bram Moolenaar7e0f4622019-09-17 21:23:39 +02002364 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002365 call VerifyScreenDump(buf, 'Test_popupwin_scroll_10', {})
2366
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002367 " check size with non-wrapping lines
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002368 call term_sendkeys(buf, ":call g:PopupScroll()\<CR>")
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002369 call VerifyScreenDump(buf, 'Test_popupwin_scroll_11', {})
2370
2371 " check size with wrapping lines
2372 call term_sendkeys(buf, "j")
2373 call VerifyScreenDump(buf, 'Test_popupwin_scroll_12', {})
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002374
Bram Moolenaarfc376e02022-05-29 18:18:18 +01002375 " check thumb when scrolled all the way down
2376 call term_sendkeys(buf, ":call ScrollBottom()\<CR>")
2377 call VerifyScreenDump(buf, 'Test_popupwin_scroll_13', {})
2378
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002379 " clean up
Bram Moolenaar20298ce2020-06-19 21:46:52 +02002380 call term_sendkeys(buf, "x")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002381 call StopVimInTerminal(buf)
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002382endfunc
2383
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02002384func Test_popup_too_high_scrollbar()
2385 CheckScreendump
2386
2387 let lines =<< trim END
2388 call setline(1, range(1, 20)->map({i, v -> repeat(v, 10)}))
2389 set scrolloff=0
2390 func ShowPopup()
2391 let winid = popup_atcursor(['one', 'two', 'three', 'four', 'five',
2392 \ 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve'], #{
2393 \ minwidth: 8,
2394 \ border: [],
2395 \ })
2396 endfunc
2397 normal 3G$
2398 call ShowPopup()
2399 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01002400 call writefile(lines, 'XtestPopupToohigh', 'D')
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02002401 let buf = RunVimInTerminal('-S XtestPopupToohigh', #{rows: 10})
2402 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_1', {})
2403
2404 call term_sendkeys(buf, ":call popup_clear()\<CR>")
2405 call term_sendkeys(buf, "8G$")
2406 call term_sendkeys(buf, ":call ShowPopup()\<CR>")
2407 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_2', {})
2408
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02002409 call term_sendkeys(buf, ":call popup_clear()\<CR>")
2410 call term_sendkeys(buf, "gg$")
2411 call term_sendkeys(buf, ":call ShowPopup()\<CR>")
2412 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_3', {})
2413
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02002414 " clean up
2415 call StopVimInTerminal(buf)
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02002416endfunc
2417
Bram Moolenaar437a7462019-07-05 20:17:22 +02002418func Test_popup_fitting_scrollbar()
2419 " this was causing a crash, divide by zero
2420 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002421 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002422 \ scrollbar: 1,
2423 \ maxwidth: 10,
2424 \ maxheight: 5,
2425 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02002426 redraw
2427 call popup_clear()
2428endfunc
2429
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002430func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002431 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002432
2433 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002434 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002435 let p = popup_create('test', opts)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002436 eval p->popup_settext('this is a text')
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002437 END
2438
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01002439 call writefile(lines, 'XtestPopupSetText', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002440 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002441 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
2442
2443 " Setting to empty string clears it
2444 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
2445 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
2446
2447 " Setting a list
2448 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2449 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2450
2451 " Shrinking with a list
2452 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
2453 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
2454
2455 " Growing with a list
2456 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2457 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2458
2459 " Empty list clears
2460 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
2461 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
2462
2463 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002464 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002465 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
2466
Bram Moolenaarb0992022020-01-30 14:55:42 +01002467 " range() (doesn't work)
2468 call term_sendkeys(buf, ":call popup_settext(p, range(4, 8))\<CR>")
2469 call VerifyScreenDump(buf, 'Test_popup_settext_07', {})
2470
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002471 " clean up
2472 call StopVimInTerminal(buf)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002473endfunc
2474
Bram Moolenaar1f42f5a2020-09-03 18:52:24 +02002475func Test_popup_settext_getline()
2476 let id = popup_create('', #{ tabpage: 0 })
2477 call popup_settext(id, ['a','b'])
2478 call assert_equal(2, line('$', id)) " OK :)
2479 call popup_close(id)
2480
2481 let id = popup_create('', #{ tabpage: -1 })
2482 call popup_settext(id, ['a','b'])
2483 call assert_equal(2, line('$', id)) " Fails :(
2484 call popup_close(id)
2485endfunc
2486
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02002487func Test_popup_settext_null()
2488 let id = popup_create('', #{ tabpage: 0 })
2489 call popup_settext(id, test_null_list())
2490 call popup_close(id)
2491
2492 let id = popup_create('', #{ tabpage: 0 })
2493 call popup_settext(id, test_null_string())
2494 call popup_close(id)
2495endfunc
2496
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002497func Test_popup_hidden()
2498 new
2499
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002500 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002501 redraw
2502 call assert_equal(0, popup_getpos(winid).visible)
2503 call popup_close(winid)
2504
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002505 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002506 redraw
2507 call assert_equal(0, popup_getpos(winid).visible)
2508 call popup_close(winid)
2509
2510 func QuitCallback(id, res)
2511 let s:cb_winid = a:id
2512 let s:cb_res = a:res
2513 endfunc
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002514 let winid = 'make a choice'->popup_dialog(#{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002515 \ filter: 'popup_filter_yesno',
2516 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002517 \ })
2518 redraw
2519 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02002520 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
2521 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002522 exe "normal anot used by filter\<Esc>"
2523 call assert_equal('not used by filter', getline(1))
2524
2525 call popup_show(winid)
2526 call feedkeys('y', "xt")
2527 call assert_equal(1, s:cb_res)
2528
2529 bwipe!
2530 delfunc QuitCallback
2531endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02002532
2533" Test options not checked elsewhere
2534func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002535 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002536 let options = popup_getoptions(winid)
2537 call assert_equal(1, options.wrap)
2538 call assert_equal(0, options.drag)
2539 call assert_equal('Beautiful', options.highlight)
2540
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002541 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002542 let options = popup_getoptions(winid)
2543 call assert_equal(0, options.wrap)
2544 call assert_equal(1, options.drag)
2545 call assert_equal('Another', options.highlight)
2546
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01002547 call assert_fails('call popup_setoptions(winid, [])', 'E1206:')
2548 call assert_fails('call popup_setoptions(winid, test_null_dict())', 'E1297:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002549
Bram Moolenaarae943152019-06-16 22:54:14 +02002550 call popup_close(winid)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002551 call assert_equal(0, popup_setoptions(winid, options.wrap))
Bram Moolenaarae943152019-06-16 22:54:14 +02002552endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002553
2554func Test_popupwin_garbage_collect()
2555 func MyPopupFilter(x, winid, c)
2556 " NOP
2557 endfunc
2558
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002559 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002560 call test_garbagecollect_now()
2561 redraw
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02002562 " Must not crash caused by invalid memory access
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002563 call feedkeys('j', 'xt')
2564 call assert_true(v:true)
2565
2566 call popup_close(winid)
2567 delfunc MyPopupFilter
2568endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002569
Bram Moolenaar581ba392019-09-03 22:08:33 +02002570func Test_popupwin_filter_mode()
2571 func MyPopupFilter(winid, c)
2572 let s:typed = a:c
2573 if a:c == ':' || a:c == "\r" || a:c == 'v'
2574 " can start cmdline mode, get out, and start/stop Visual mode
2575 return 0
2576 endif
2577 return 1
2578 endfunc
2579
2580 " Normal, Visual and Insert mode
2581 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'nvi'})
2582 redraw
2583 call feedkeys('x', 'xt')
2584 call assert_equal('x', s:typed)
2585
2586 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2587 call assert_equal(':', s:typed)
2588 call assert_equal('foo', g:foo)
2589
2590 let @x = 'something'
2591 call feedkeys('v$"xy', 'xt')
2592 call assert_equal('y', s:typed)
2593 call assert_equal('something', @x) " yank command is filtered out
2594 call feedkeys('v', 'xt') " end Visual mode
2595
2596 call popup_close(winid)
2597
2598 " only Normal mode
2599 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'n'})
2600 redraw
2601 call feedkeys('x', 'xt')
2602 call assert_equal('x', s:typed)
2603
2604 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2605 call assert_equal(':', s:typed)
2606 call assert_equal('foo', g:foo)
2607
2608 let @x = 'something'
2609 call feedkeys('v$"xy', 'xt')
2610 call assert_equal('v', s:typed)
2611 call assert_notequal('something', @x)
2612
2613 call popup_close(winid)
2614
2615 " default: all modes
2616 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
2617 redraw
2618 call feedkeys('x', 'xt')
2619 call assert_equal('x', s:typed)
2620
2621 let g:foo = 'bar'
2622 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2623 call assert_equal("\r", s:typed)
2624 call assert_equal('bar', g:foo)
2625
2626 let @x = 'something'
2627 call feedkeys('v$"xy', 'xt')
2628 call assert_equal('y', s:typed)
2629 call assert_equal('something', @x) " yank command is filtered out
2630 call feedkeys('v', 'xt') " end Visual mode
2631
2632 call popup_close(winid)
2633 delfunc MyPopupFilter
2634endfunc
2635
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002636func Test_popupwin_filter_mouse()
2637 func MyPopupFilter(winid, c)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002638 let g:got_mousepos = getmousepos()
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002639 return 0
2640 endfunc
2641
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002642 call setline(1, ['.'->repeat(25)]->repeat(10))
2643 let winid = popup_create(['short', 'long line that will wrap', 'other'], #{
2644 \ line: 2,
2645 \ col: 4,
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002646 \ maxwidth: 12,
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002647 \ padding: [],
2648 \ border: [],
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002649 \ filter: 'MyPopupFilter',
2650 \ })
2651 redraw
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002652 " 123456789012345678901
2653 " 1 .....................
2654 " 2 ...+--------------+..
2655 " 3 ...| |..
2656 " 4 ...| short |..
2657 " 5 ...| long line th |..
2658 " 6 ...| at will wrap |..
2659 " 7 ...| other |..
2660 " 8 ...| |..
2661 " 9 ...+--------------+..
2662 " 10 .....................
2663 let tests = []
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002664
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002665 func AddItemOutsidePopup(tests, row, col)
2666 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2667 \ screenrow: a:row, screencol: a:col,
2668 \ winid: win_getid(), winrow: a:row, wincol: a:col,
2669 \ line: a:row, column: a:col,
2670 \ }})
2671 endfunc
2672 func AddItemInPopupBorder(tests, winid, row, col)
2673 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2674 \ screenrow: a:row, screencol: a:col,
2675 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2676 \ line: 0, column: 0,
2677 \ }})
2678 endfunc
2679 func AddItemInPopupText(tests, winid, row, col, textline, textcol)
2680 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2681 \ screenrow: a:row, screencol: a:col,
2682 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2683 \ line: a:textline, column: a:textcol,
2684 \ }})
2685 endfunc
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002686
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002687 " above and below popup
2688 for c in range(1, 21)
2689 call AddItemOutsidePopup(tests, 1, c)
2690 call AddItemOutsidePopup(tests, 10, c)
2691 endfor
2692 " left and right of popup
2693 for r in range(1, 10)
2694 call AddItemOutsidePopup(tests, r, 3)
2695 call AddItemOutsidePopup(tests, r, 20)
2696 endfor
2697 " top and bottom in popup
2698 for c in range(4, 19)
2699 call AddItemInPopupBorder(tests, winid, 2, c)
2700 call AddItemInPopupBorder(tests, winid, 3, c)
2701 call AddItemInPopupBorder(tests, winid, 8, c)
2702 call AddItemInPopupBorder(tests, winid, 9, c)
2703 endfor
2704 " left and right margin in popup
2705 for r in range(2, 9)
2706 call AddItemInPopupBorder(tests, winid, r, 4)
2707 call AddItemInPopupBorder(tests, winid, r, 5)
2708 call AddItemInPopupBorder(tests, winid, r, 18)
2709 call AddItemInPopupBorder(tests, winid, r, 19)
2710 endfor
2711 " text "short"
2712 call AddItemInPopupText(tests, winid, 4, 6, 1, 1)
2713 call AddItemInPopupText(tests, winid, 4, 10, 1, 5)
2714 call AddItemInPopupText(tests, winid, 4, 11, 1, 6)
2715 call AddItemInPopupText(tests, winid, 4, 17, 1, 6)
2716 " text "long line th"
2717 call AddItemInPopupText(tests, winid, 5, 6, 2, 1)
2718 call AddItemInPopupText(tests, winid, 5, 10, 2, 5)
2719 call AddItemInPopupText(tests, winid, 5, 17, 2, 12)
2720 " text "at will wrap"
2721 call AddItemInPopupText(tests, winid, 6, 6, 2, 13)
2722 call AddItemInPopupText(tests, winid, 6, 10, 2, 17)
2723 call AddItemInPopupText(tests, winid, 6, 17, 2, 24)
2724 " text "other"
2725 call AddItemInPopupText(tests, winid, 7, 6, 3, 1)
2726 call AddItemInPopupText(tests, winid, 7, 10, 3, 5)
2727 call AddItemInPopupText(tests, winid, 7, 11, 3, 6)
2728 call AddItemInPopupText(tests, winid, 7, 17, 3, 6)
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002729
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002730 for item in tests
2731 call test_setmouse(item.clickrow, item.clickcol)
2732 call feedkeys("\<LeftMouse>", 'xt')
2733 call assert_equal(item.result, g:got_mousepos)
2734 endfor
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002735
2736 call popup_close(winid)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002737 enew!
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002738 delfunc MyPopupFilter
2739endfunc
2740
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002741func Test_popupwin_with_buffer()
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01002742 call writefile(['some text', 'in a buffer'], 'XsomeFile', 'D')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002743 let buf = bufadd('XsomeFile')
2744 call assert_equal(0, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002745
2746 setlocal number
2747 call setbufvar(buf, "&wrapmargin", 13)
2748
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002749 let winid = popup_create(buf, {})
2750 call assert_notequal(0, winid)
2751 let pos = popup_getpos(winid)
2752 call assert_equal(2, pos.height)
2753 call assert_equal(1, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002754
2755 " window-local option is set to default, buffer-local is not
2756 call assert_equal(0, getwinvar(winid, '&number'))
2757 call assert_equal(13, getbufvar(buf, '&wrapmargin'))
2758
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002759 call popup_close(winid)
2760 call assert_equal({}, popup_getpos(winid))
2761 call assert_equal(1, bufloaded(buf))
2762 exe 'bwipe! ' .. buf
Bram Moolenaar46451042019-08-24 15:50:46 +02002763 setlocal nonumber
Bram Moolenaar7866b872019-07-01 22:21:01 +02002764
2765 edit test_popupwin.vim
2766 let winid = popup_create(bufnr(''), {})
2767 redraw
2768 call popup_close(winid)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002769endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02002770
Bram Moolenaar188639d2022-04-04 16:57:21 +01002771func Test_popupwin_buffer_with_swapfile()
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01002772 call writefile(['some text', 'in a buffer'], 'XopenFile', 'D')
2773 call writefile([''], '.XopenFile.swp', 'D')
Bram Moolenaar188639d2022-04-04 16:57:21 +01002774 let g:ignoreSwapExists = 1
2775
2776 let bufnr = bufadd('XopenFile')
2777 call assert_equal(0, bufloaded(bufnr))
2778 let winid = popup_create(bufnr, {'hidden': 1})
2779 call assert_equal(1, bufloaded(bufnr))
2780 call popup_close(winid)
2781
2782 exe 'buffer ' .. bufnr
2783 call assert_equal(1, &readonly)
2784 bwipe!
2785
Bram Moolenaar188639d2022-04-04 16:57:21 +01002786 unlet g:ignoreSwapExists
2787endfunc
2788
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002789func Test_popupwin_terminal_buffer()
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002790 CheckFeature terminal
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002791 CheckUnix
Bram Moolenaare06a28f2020-05-13 23:24:12 +02002792 " Starting a terminal to run a shell in is considered flaky.
2793 let g:test_is_flaky = 1
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002794
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002795 let origwin = win_getid()
Bram Moolenaar349f6092020-10-06 20:46:49 +02002796
2797 " open help window to test that :help below fails
2798 help
2799
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002800 let termbuf = term_start(&shell, #{hidden: 1})
Bram Moolenaarf5452692020-11-28 21:56:06 +01002801 let winid = popup_create(termbuf, #{minwidth: 40, minheight: 10, border: []})
Bram Moolenaar0353f562020-12-17 22:27:38 +01002802 " Wait for shell to start
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002803 call WaitForAssert({-> assert_equal("run", job_status(term_getjob(termbuf)))})
Bram Moolenaar0353f562020-12-17 22:27:38 +01002804 " Wait for a prompt (see border char first, then space after prompt)
2805 call WaitForAssert({ -> assert_equal(' ', screenstring(screenrow(), screencol() - 1))})
Bram Moolenaarf5452692020-11-28 21:56:06 +01002806
2807 " When typing a character, the cursor is after it.
2808 call feedkeys("x", 'xt')
Bram Moolenaar0353f562020-12-17 22:27:38 +01002809 call term_wait(termbuf)
Bram Moolenaarf5452692020-11-28 21:56:06 +01002810 redraw
2811 call WaitForAssert({ -> assert_equal('x', screenstring(screenrow(), screencol() - 1))})
2812 call feedkeys("\<BS>", 'xt')
2813
Bram Moolenaar631ebc42020-02-03 22:15:26 +01002814 " Check this doesn't crash
2815 call assert_equal(winnr(), winnr('j'))
2816 call assert_equal(winnr(), winnr('k'))
2817 call assert_equal(winnr(), winnr('h'))
2818 call assert_equal(winnr(), winnr('l'))
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002819
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002820 " Cannot quit while job is running
2821 call assert_fails('call feedkeys("\<C-W>:quit\<CR>", "xt")', 'E948:')
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002822
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002823 " Cannot enter Terminal-Normal mode. (TODO: but it works...)
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002824 call feedkeys("xxx\<C-W>N", 'xt')
2825 call assert_fails('call feedkeys("gf", "xt")', 'E863:')
2826 call feedkeys("a\<C-U>", 'xt')
2827
Bram Moolenaar3f65c662020-05-27 23:15:16 +02002828 " Cannot escape from terminal window
2829 call assert_fails('tab drop xxx', 'E863:')
Bram Moolenaar349f6092020-10-06 20:46:49 +02002830 call assert_fails('help', 'E994:')
Bram Moolenaar3f65c662020-05-27 23:15:16 +02002831
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002832 " Cannot open a second one.
2833 let termbuf2 = term_start(&shell, #{hidden: 1})
2834 call assert_fails('call popup_create(termbuf2, #{})', 'E861:')
2835 call term_sendkeys(termbuf2, "exit\<CR>")
2836
Bram Moolenaard28950f2022-05-29 14:13:04 +01002837 " Exiting shell puts popup window in Terminal-Normal mode.
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002838 call feedkeys("exit\<CR>", 'xt')
2839 " Wait for shell to exit
Bram Moolenaarb2b218d2020-06-22 20:22:19 +02002840 call WaitForAssert({-> assert_equal("dead", job_status(term_getjob(termbuf)))})
Bram Moolenaar373c6512020-06-22 19:24:23 +02002841
Bram Moolenaar349f6092020-10-06 20:46:49 +02002842 helpclose
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002843 call feedkeys(":quit\<CR>", 'xt')
2844 call assert_equal(origwin, win_getid())
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002845endfunc
2846
Bram Moolenaar9e636b92022-05-29 22:37:05 +01002847func Test_popupwin_terminal_buffer_none()
2848 CheckFeature terminal
2849 CheckUnix
2850
2851 " Starting a terminal to run a shell in is considered flaky.
2852 let g:test_is_flaky = 1
2853
2854 let origwin = win_getid()
2855 call term_start("NONE", {"hidden": 1})->popup_create({"border": []})
2856 sleep 50m
2857
2858 " since no actual job is running can close the window with :quit
2859 call feedkeys("\<C-W>:q\<CR>", 'xt')
2860 call assert_equal([], popup_list())
2861
2862 call assert_equal(origwin, win_getid())
2863endfunc
2864
Bram Moolenaard28950f2022-05-29 14:13:04 +01002865func Test_popupwin_terminal_scrollbar()
2866 CheckFeature terminal
2867 CheckScreendump
2868 CheckUnix
2869
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01002870 call writefile(range(50), 'Xtestfile', 'D')
Bram Moolenaard28950f2022-05-29 14:13:04 +01002871 let lines =<< trim END
2872 vim9script
2873
Bram Moolenaar10db31f2022-05-30 17:58:03 +01002874 # testing CTRL-W CTRL-W requires two windows
2875 split
2876
Bram Moolenaard28950f2022-05-29 14:13:04 +01002877 term_start(['cat', 'Xtestfile'], {hidden: true})
2878 ->popup_create({
2879 minwidth: 40,
2880 maxwidth: 40,
2881 minheight: 8,
2882 maxheight: 8,
2883 scrollbar: true,
2884 border: []
2885 })
2886 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01002887 call writefile(lines, 'Xpterm', 'D')
Bram Moolenaard28950f2022-05-29 14:13:04 +01002888 let buf = RunVimInTerminal('-S Xpterm', #{rows: 15})
2889 call VerifyScreenDump(buf, 'Test_popupwin_poptermscroll_1', {})
2890
2891 " scroll to the middle
2892 call term_sendkeys(buf, "50%")
2893 call VerifyScreenDump(buf, 'Test_popupwin_poptermscroll_2', {})
2894
Bram Moolenaar10db31f2022-05-30 17:58:03 +01002895 " get error if trying to escape the window
2896 call term_sendkeys(buf, "\<C-W>\<C-W>")
2897 call VerifyScreenDump(buf, 'Test_popupwin_poptermscroll_3', {})
2898
Bram Moolenaard28950f2022-05-29 14:13:04 +01002899 " close the popupwin.
2900 call term_sendkeys(buf, ":q\<CR>")
Bram Moolenaar10db31f2022-05-30 17:58:03 +01002901 call VerifyScreenDump(buf, 'Test_popupwin_poptermscroll_4', {})
Bram Moolenaard28950f2022-05-29 14:13:04 +01002902
2903 call StopVimInTerminal(buf)
Bram Moolenaard28950f2022-05-29 14:13:04 +01002904endfunc
2905
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002906func Test_popupwin_close_prevwin()
2907 CheckFeature terminal
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002908 call Popupwin_close_prevwin()
2909endfunc
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002910
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002911def Popupwin_close_prevwin()
2912 assert_equal(1, winnr('$'))
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002913 split
2914 wincmd b
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002915 assert_equal(2, winnr())
Bram Moolenaare0de1712020-12-02 17:36:54 +01002916 var buf = term_start(&shell, {hidden: 1})
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002917 popup_create(buf, {})
Bram Moolenaar62aec932022-01-29 21:45:34 +00002918 g:TermWait(buf, 100)
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002919 popup_clear(true)
2920 assert_equal(2, winnr())
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002921
2922 quit
2923 exe 'bwipe! ' .. buf
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002924enddef
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002925
Bram Moolenaar934470e2019-09-01 23:27:05 +02002926func Test_popupwin_with_buffer_and_filter()
2927 new Xwithfilter
2928 call setline(1, range(100))
2929 let bufnr = bufnr()
2930 hide
2931
2932 func BufferFilter(win, key)
2933 if a:key == 'G'
2934 " recursive use of "G" does not cause problems.
2935 call win_execute(a:win, 'normal! G')
2936 return 1
2937 endif
2938 return 0
2939 endfunc
2940
2941 let winid = popup_create(bufnr, #{maxheight: 5, filter: 'BufferFilter'})
2942 call assert_equal(1, popup_getpos(winid).firstline)
2943 redraw
2944 call feedkeys("G", 'xt')
2945 call assert_equal(99, popup_getpos(winid).firstline)
2946
2947 call popup_close(winid)
2948 exe 'bwipe! ' .. bufnr
2949endfunc
2950
Bram Moolenaare296e312019-07-03 23:20:18 +02002951func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002952 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002953 \ maxwidth: 40,
2954 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02002955 \ })
2956 for top in range(1, 20)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002957 eval winid->popup_setoptions(#{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02002958 redraw
2959 call assert_equal(19, popup_getpos(winid).width)
2960 endfor
2961 call popup_clear()
2962endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02002963
2964func Test_popupwin_buf_close()
2965 let buf = bufadd('Xtestbuf')
2966 call bufload(buf)
2967 call setbufline(buf, 1, ['just', 'some', 'lines'])
2968 let winid = popup_create(buf, {})
2969 redraw
2970 call assert_equal(3, popup_getpos(winid).height)
2971 let bufinfo = getbufinfo(buf)[0]
2972 call assert_equal(1, bufinfo.changed)
2973 call assert_equal(0, bufinfo.hidden)
2974 call assert_equal(0, bufinfo.listed)
2975 call assert_equal(1, bufinfo.loaded)
2976 call assert_equal([], bufinfo.windows)
2977 call assert_equal([winid], bufinfo.popups)
2978
2979 call popup_close(winid)
2980 call assert_equal({}, popup_getpos(winid))
2981 let bufinfo = getbufinfo(buf)[0]
2982 call assert_equal(1, bufinfo.changed)
2983 call assert_equal(1, bufinfo.hidden)
2984 call assert_equal(0, bufinfo.listed)
2985 call assert_equal(1, bufinfo.loaded)
2986 call assert_equal([], bufinfo.windows)
2987 call assert_equal([], bufinfo.popups)
2988 exe 'bwipe! ' .. buf
2989endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02002990
2991func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002992 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02002993
2994 let lines =<< trim END
2995 call setline(1, range(1, 10))
2996 hi ScrollThumb ctermbg=blue
2997 hi ScrollBar ctermbg=red
2998 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002999 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003000 \ maxwidth: 10,
3001 \ maxheight: 3,
3002 \ pos : 'topleft',
3003 \ col : a:col,
3004 \ line : a:line,
3005 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02003006 \ })
3007 endfunc
3008 call PopupMenu(['x'], 1, 1)
3009 call PopupMenu(['123456789|'], 1, 16)
3010 call PopupMenu(['123456789|' .. ' '], 7, 1)
3011 call PopupMenu([repeat('123456789|', 100)], 7, 16)
3012 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
3013 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003014 call writefile(lines, 'XtestPopupMenuMaxWidth', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003015 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02003016 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
3017
3018 " close the menu popupwin.
3019 call term_sendkeys(buf, " ")
3020 call term_sendkeys(buf, " ")
3021 call term_sendkeys(buf, " ")
3022 call term_sendkeys(buf, " ")
3023 call term_sendkeys(buf, " ")
3024
3025 " clean up
3026 call StopVimInTerminal(buf)
Bram Moolenaar017c2692019-07-13 14:17:51 +02003027endfunc
3028
Bram Moolenaara901a372019-07-13 16:38:50 +02003029func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003030 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02003031
3032 let lines =<< trim END
3033 call setline(1, range(1, 20))
3034 hi ScrollThumb ctermbg=blue
3035 hi ScrollBar ctermbg=red
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003036 eval ['one', 'two', 'three', 'four', 'five',
3037 \ 'six', 'seven', 'eight', 'nine']
3038 \ ->popup_menu(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003039 \ minwidth: 8,
3040 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02003041 \ })
3042 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003043 call writefile(lines, 'XtestPopupMenuScroll', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003044 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02003045
3046 call term_sendkeys(buf, "j")
3047 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
3048
3049 call term_sendkeys(buf, "jjj")
3050 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
3051
3052 " if the cursor is the bottom line, it stays at the bottom line.
3053 call term_sendkeys(buf, repeat("j", 20))
3054 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
3055
3056 call term_sendkeys(buf, "kk")
3057 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
3058
3059 call term_sendkeys(buf, "k")
3060 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
3061
3062 " if the cursor is in the top line, it stays in the top line.
3063 call term_sendkeys(buf, repeat("k", 20))
3064 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
3065
3066 " close the menu popupwin.
3067 call term_sendkeys(buf, " ")
3068
3069 " clean up
3070 call StopVimInTerminal(buf)
Bram Moolenaara901a372019-07-13 16:38:50 +02003071endfunc
3072
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003073func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003074 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003075
3076 let lines =<< trim END
3077 function! MyFilter(winid, key) abort
3078 if a:key == "0"
3079 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
3080 return 1
3081 endif
3082 if a:key == "G"
3083 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
3084 return 1
3085 endif
3086 if a:key == "j"
3087 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
3088 return 1
3089 endif
3090 if a:key == "k"
3091 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
3092 return 1
3093 endif
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02003094 if a:key == ':'
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003095 call popup_close(a:winid)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02003096 return 0
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003097 endif
3098 return 0
3099 endfunction
3100 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
3101 \ maxheight : 3,
3102 \ filter : 'MyFilter'
3103 \ })
3104 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003105 call writefile(lines, 'XtestPopupMenuFilter', 'D')
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003106 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
3107
3108 call term_sendkeys(buf, "j")
3109 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
3110
3111 call term_sendkeys(buf, "k")
3112 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
3113
3114 call term_sendkeys(buf, "G")
3115 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
3116
3117 call term_sendkeys(buf, "0")
3118 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
3119
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02003120 " check that when the popup is closed in the filter the screen is redrawn
3121 call term_sendkeys(buf, ":")
3122 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_5', {})
3123 call term_sendkeys(buf, "\<CR>")
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003124
3125 " clean up
3126 call StopVimInTerminal(buf)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003127endfunc
3128
3129func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003130 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003131
3132 let winid = popup_create('some text', {})
3133 call assert_equal(0, popup_getoptions(winid).cursorline)
3134 call popup_close(winid)
3135
3136 let winid = popup_create('some text', #{ cursorline: 1, })
3137 call assert_equal(1, popup_getoptions(winid).cursorline)
3138 call popup_close(winid)
3139
Bram Moolenaar6bfc4752021-02-21 23:12:18 +01003140 let winid = popup_create('some text', #{ cursorline: v:true, })
3141 call assert_equal(1, popup_getoptions(winid).cursorline)
3142 call popup_close(winid)
3143
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003144 let winid = popup_create('some text', #{ cursorline: 0, })
3145 call assert_equal(0, popup_getoptions(winid).cursorline)
3146 call popup_close(winid)
3147
3148 let winid = popup_menu('some text', {})
3149 call assert_equal(1, popup_getoptions(winid).cursorline)
3150 call popup_close(winid)
3151
3152 let winid = popup_menu('some text', #{ cursorline: 1, })
3153 call assert_equal(1, popup_getoptions(winid).cursorline)
3154 call popup_close(winid)
3155
3156 let winid = popup_menu('some text', #{ cursorline: 0, })
3157 call assert_equal(0, popup_getoptions(winid).cursorline)
3158 call popup_close(winid)
3159
3160 " ---------
3161 " Pattern 1
3162 " ---------
3163 let lines =<< trim END
3164 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
3165 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003166 call writefile(lines, 'XtestPopupCursorLine', 'D')
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003167 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3168 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
3169 call term_sendkeys(buf, ":call popup_clear()\<cr>")
3170 call StopVimInTerminal(buf)
3171
3172 " ---------
3173 " Pattern 2
3174 " ---------
3175 let lines =<< trim END
3176 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
3177 END
3178 call writefile(lines, 'XtestPopupCursorLine')
3179 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3180 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
3181 call term_sendkeys(buf, ":call popup_clear()\<cr>")
3182 call StopVimInTerminal(buf)
3183
3184 " ---------
3185 " Pattern 3
3186 " ---------
3187 let lines =<< trim END
3188 function! MyFilter(winid, key) abort
3189 if a:key == "j"
3190 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
3191 return 1
3192 endif
3193 if a:key == 'x'
3194 call popup_close(a:winid)
3195 return 1
3196 endif
3197 return 0
3198 endfunction
3199 call popup_menu(['111', '222', '333'], #{
3200 \ cursorline : 0,
3201 \ maxheight : 2,
3202 \ filter : 'MyFilter',
3203 \ })
3204 END
3205 call writefile(lines, 'XtestPopupCursorLine')
3206 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3207 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
3208 call term_sendkeys(buf, "j")
3209 call term_sendkeys(buf, "j")
3210 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
3211 call term_sendkeys(buf, "x")
3212 call StopVimInTerminal(buf)
3213
3214 " ---------
3215 " Pattern 4
3216 " ---------
3217 let lines =<< trim END
3218 function! MyFilter(winid, key) abort
3219 if a:key == "j"
3220 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
3221 return 1
3222 endif
3223 if a:key == 'x'
3224 call popup_close(a:winid)
3225 return 1
3226 endif
3227 return 0
3228 endfunction
3229 call popup_menu(['111', '222', '333'], #{
3230 \ cursorline : 1,
3231 \ maxheight : 2,
3232 \ filter : 'MyFilter',
3233 \ })
3234 END
3235 call writefile(lines, 'XtestPopupCursorLine')
3236 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3237 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
3238 call term_sendkeys(buf, "j")
3239 call term_sendkeys(buf, "j")
3240 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
3241 call term_sendkeys(buf, "x")
3242 call StopVimInTerminal(buf)
3243
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003244 " ---------
3245 " Cursor in second line when creating the popup
3246 " ---------
3247 let lines =<< trim END
3248 let winid = popup_create(['111', '222', '333'], #{
3249 \ cursorline : 1,
3250 \ })
3251 call win_execute(winid, "2")
3252 END
3253 call writefile(lines, 'XtestPopupCursorLine')
3254 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3255 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_7', {})
3256 call StopVimInTerminal(buf)
3257
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01003258 " ---------
3259 " Use current buffer for popupmenu
3260 " ---------
3261 let lines =<< trim END
3262 call setline(1, ['one', 'two', 'three'])
3263 let winid = popup_create(bufnr('%'), #{
3264 \ cursorline : 1,
3265 \ })
3266 call win_execute(winid, "2")
3267 END
3268 call writefile(lines, 'XtestPopupCursorLine')
3269 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3270 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_8', {})
3271 call StopVimInTerminal(buf)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003272endfunc
3273
Bram Moolenaar6bfc4752021-02-21 23:12:18 +01003274def Test_popup_cursorline_vim9()
3275 var winid = popup_create('some text', { cursorline: true, })
3276 assert_equal(1, popup_getoptions(winid).cursorline)
3277 popup_close(winid)
3278
3279 assert_fails("popup_create('some text', { cursorline: 2, })", 'E1023:')
3280 popup_clear()
3281enddef
3282
Bram Moolenaarf914a332019-07-20 15:09:56 +02003283func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003284 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003285 CheckFeature quickfix
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003286
Bram Moolenaarf914a332019-07-20 15:09:56 +02003287 call writefile([
3288 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
3289 \ "another\tXtagfile\t/^this is another",
3290 \ "theword\tXtagfile\t/^theword"],
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003291 \ 'Xtags', 'D')
Bram Moolenaarf914a332019-07-20 15:09:56 +02003292 call writefile(range(1,20)
3293 \ + ['theword is here']
3294 \ + range(22, 27)
3295 \ + ['this is another place']
3296 \ + range(29, 40),
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003297 \ "Xtagfile", 'D')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003298 call writefile(range(1,10)
3299 \ + ['searched word is here']
3300 \ + range(12, 20),
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003301 \ "Xheader.h", 'D')
Bram Moolenaarf914a332019-07-20 15:09:56 +02003302 let lines =<< trim END
3303 set tags=Xtags
3304 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003305 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003306 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003307 \ 'three',
3308 \ 'four',
3309 \ 'five',
3310 \ 'six',
3311 \ 'seven',
3312 \ 'find theword somewhere',
3313 \ 'nine',
3314 \ 'this is another word',
3315 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02003316 set previewpopup=height:4,width:40
Bram Moolenaar60577482021-03-04 21:35:07 +01003317 hi OtherColor ctermbg=lightcyan guibg=lightcyan
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003318 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02003319 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003320 call writefile(lines, 'XtestPreviewPopup', 'D')
Bram Moolenaarf914a332019-07-20 15:09:56 +02003321 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
3322
3323 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
3324 call term_sendkeys(buf, ":\<CR>")
3325 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
3326
Bram Moolenaar60577482021-03-04 21:35:07 +01003327 call term_sendkeys(buf, ":set previewpopup+=highlight:OtherColor\<CR>")
Bram Moolenaarf914a332019-07-20 15:09:56 +02003328 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
3329 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
3330
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003331 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003332 call term_sendkeys(buf, ":\<CR>")
3333 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
3334
3335 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
3336 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
3337
Bram Moolenaar799439a2020-02-11 21:44:17 +01003338 call term_sendkeys(buf, ":silent cd ..\<CR>:\<CR>")
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003339 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar799439a2020-02-11 21:44:17 +01003340 call term_sendkeys(buf, ":silent cd testdir\<CR>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003341
Bram Moolenaar60577482021-03-04 21:35:07 +01003342 call term_sendkeys(buf, ":set previewpopup-=highlight:OtherColor\<CR>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003343 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02003344 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003345 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
3346
3347 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
3348 call term_sendkeys(buf, ":\<CR>")
3349 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
3350
3351 call term_sendkeys(buf, ":pclose\<CR>")
3352 call term_sendkeys(buf, ":psearch searched\<CR>")
3353 call term_sendkeys(buf, ":\<CR>")
3354 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003355
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01003356 call term_sendkeys(buf, "\<C-W>p")
3357 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_9', {})
3358
3359 call term_sendkeys(buf, ":call win_execute(popup_findpreview(), 'call popup_clear()')\<CR>")
3360 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_10', {})
3361
Bram Moolenaarf914a332019-07-20 15:09:56 +02003362 call StopVimInTerminal(buf)
Bram Moolenaarf914a332019-07-20 15:09:56 +02003363endfunc
3364
Bram Moolenaar393f8d62022-10-02 14:28:30 +01003365func Test_previewpopup_pum()
3366 CheckScreendump
3367 CheckFeature quickfix
3368
3369 let lines =<< trim END
3370 let a = 3
3371 let b = 1
3372 echo a
3373 echo b
3374 call system('echo hello')
3375 " the end
3376 END
3377 call writefile(lines, 'XpreviewText.vim', 'D')
3378
3379 let lines =<< trim END
3380 call setline(1, ['one', 'two', 'three', 'other', 'once', 'only', 'off'])
3381 set previewpopup=height:6,width:40
3382 pedit XpreviewText.vim
3383 END
3384 call writefile(lines, 'XtestPreviewPum', 'D')
3385 let buf = RunVimInTerminal('-S XtestPreviewPum', #{rows: 12})
3386
3387 call term_sendkeys(buf, "A o\<C-N>")
3388 call VerifyScreenDump(buf, 'Test_pum_preview_1', {})
3389
3390 call term_sendkeys(buf, "\<C-N>")
3391 call VerifyScreenDump(buf, 'Test_pum_preview_2', {})
3392
3393 call term_sendkeys(buf, "\<C-N>")
3394 call VerifyScreenDump(buf, 'Test_pum_preview_3', {})
3395
3396 call term_sendkeys(buf, "\<C-N>")
3397 call VerifyScreenDump(buf, 'Test_pum_preview_4', {})
3398
3399 call term_sendkeys(buf, "\<Esc>")
3400 call StopVimInTerminal(buf)
3401endfunc
3402
3403
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003404func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003405 let lines =<< trim END
3406 set completeopt+=preview,popup
3407 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02003408 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003409
3410 func CompleteFuncDict(findstart, base)
3411 if a:findstart
3412 if col('.') > 10
3413 return col('.') - 10
3414 endif
3415 return 0
3416 endif
3417
3418 return {
3419 \ 'words': [
3420 \ {
3421 \ 'word': 'aword',
3422 \ 'abbr': 'wrd',
3423 \ 'menu': 'extra text',
3424 \ 'info': 'words are cool',
3425 \ 'kind': 'W',
3426 \ 'user_data': 'test'
3427 \ },
3428 \ {
3429 \ 'word': 'anotherword',
3430 \ 'abbr': 'anotwrd',
3431 \ 'menu': 'extra text',
3432 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
3433 \ 'kind': 'W',
3434 \ 'user_data': 'notest'
3435 \ },
3436 \ {
3437 \ 'word': 'noinfo',
3438 \ 'abbr': 'noawrd',
3439 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02003440 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003441 \ 'kind': 'W',
3442 \ 'user_data': 'notest'
3443 \ },
3444 \ {
3445 \ 'word': 'thatword',
3446 \ 'abbr': 'thatwrd',
3447 \ 'menu': 'extra text',
3448 \ 'info': 'that word is cool',
3449 \ 'kind': 'W',
3450 \ 'user_data': 'notest'
3451 \ },
3452 \ ]
3453 \ }
3454 endfunc
3455 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003456 func ChangeColor()
3457 let id = popup_findinfo()
Bram Moolenaard356fc62020-12-09 18:13:44 +01003458 if buflisted(winbufnr(id))
3459 call setline(1, 'buffer is listed')
3460 endif
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003461 eval id->popup_setoptions(#{highlight: 'InfoPopup'})
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003462 endfunc
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003463
3464 func InfoHidden()
3465 set completepopup=height:4,border:off,align:menu
3466 set completeopt-=popup completeopt+=popuphidden
3467 au CompleteChanged * call HandleChange()
3468 endfunc
3469
3470 let s:counter = 0
3471 func HandleChange()
3472 let s:counter += 1
3473 let selected = complete_info(['selected']).selected
3474 if selected <= 0
3475 " First time: do nothing, info remains hidden
3476 return
3477 endif
3478 if selected == 1
3479 " Second time: show info right away
3480 let id = popup_findinfo()
3481 if id
3482 call popup_settext(id, 'immediate info ' .. s:counter)
3483 call popup_show(id)
3484 endif
3485 else
3486 " Third time: show info after a short delay
3487 call timer_start(100, 'ShowInfo')
3488 endif
3489 endfunc
3490
3491 func ShowInfo(...)
3492 let id = popup_findinfo()
3493 if id
3494 call popup_settext(id, 'async info ' .. s:counter)
3495 call popup_show(id)
3496 endif
3497 endfunc
Bram Moolenaar2dfae042020-11-15 14:09:37 +01003498
Bakudankun65555002021-11-17 20:40:16 +00003499 func OpenOtherPopups()
3500 call popup_create([
3501 \ 'popup below',
3502 \ 'popup below',
3503 \ 'popup below',
3504 \ 'popup below',
3505 \ ], #{
3506 \ line: 'cursor',
3507 \ col: 'cursor+3',
3508 \ highlight: 'ErrorMsg',
3509 \ minwidth: 17,
3510 \ zindex: 50,
3511 \ })
3512 call popup_create([
3513 \ 'popup on top',
3514 \ 'popup on top',
3515 \ 'popup on top',
3516 \ ], #{
3517 \ line: 'cursor+3',
3518 \ col: 'cursor-10',
3519 \ highlight: 'Search',
3520 \ minwidth: 10,
3521 \ zindex: 200,
3522 \ })
3523 endfunc
3524
Bram Moolenaar2dfae042020-11-15 14:09:37 +01003525 " Check that no autocommands are triggered for the info popup
3526 au WinEnter * if win_gettype() == 'popup' | call setline(2, 'WinEnter') | endif
3527 au WinLeave * if win_gettype() == 'popup' | call setline(2, 'WinLeave') | endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003528 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003529 return lines
3530endfunc
3531
3532func Test_popupmenu_info_border()
3533 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003534 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003535
3536 let lines = Get_popupmenu_lines()
3537 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003538 call writefile(lines, 'XtestInfoPopup', 'D')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003539
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003540 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003541 call TermWait(buf, 25)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003542
3543 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3544 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
3545
3546 call term_sendkeys(buf, "\<C-N>")
3547 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
3548
3549 call term_sendkeys(buf, "\<C-N>")
3550 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
3551
3552 call term_sendkeys(buf, "\<C-N>\<C-N>")
3553 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
3554
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02003555 " info on the left with scrollbar
3556 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
3557 call term_sendkeys(buf, "\<C-N>\<C-N>")
3558 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
3559
Bram Moolenaar202c3f72019-11-21 12:12:35 +01003560 " Test that the popupmenu's scrollbar and infopopup do not overlap
3561 call term_sendkeys(buf, "\<Esc>")
3562 call term_sendkeys(buf, ":set pumheight=3\<CR>")
3563 call term_sendkeys(buf, "cc\<C-X>\<C-U>")
3564 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_6', {})
3565
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003566 " Hide the info popup, cycle through buffers, make sure it didn't get
Bram Moolenaarca7c0782020-01-14 20:42:48 +01003567 " deleted.
3568 call term_sendkeys(buf, "\<Esc>")
3569 call term_sendkeys(buf, ":set hidden\<CR>")
3570 call term_sendkeys(buf, ":bn\<CR>")
3571 call term_sendkeys(buf, ":bn\<CR>")
3572 call term_sendkeys(buf, "otest text test text\<C-X>\<C-U>")
3573 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_7', {})
3574
Bram Moolenaar447bfba2020-07-18 16:07:16 +02003575 " Test that when the option is changed the popup changes.
3576 call term_sendkeys(buf, "\<Esc>")
3577 call term_sendkeys(buf, ":set completepopup=border:off\<CR>")
3578 call term_sendkeys(buf, "a\<C-X>\<C-U>")
3579 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_8', {})
3580
Bram Moolenaar6d585f42020-07-26 22:20:54 +02003581 call term_sendkeys(buf, " \<Esc>")
3582 call term_sendkeys(buf, ":set completepopup+=width:10\<CR>")
3583 call term_sendkeys(buf, "a\<C-X>\<C-U>")
3584 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_9', {})
3585
Bram Moolenaarca7c0782020-01-14 20:42:48 +01003586 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003587 call StopVimInTerminal(buf)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003588endfunc
3589
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003590func Test_popupmenu_info_noborder()
3591 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003592 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003593
3594 let lines = Get_popupmenu_lines()
3595 call add(lines, 'set completepopup=height:4,border:off')
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003596 call writefile(lines, 'XtestInfoPopupNb', 'D')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003597
3598 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003599 call TermWait(buf, 25)
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003600
3601 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3602 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
3603
3604 call StopVimInTerminal(buf)
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003605endfunc
3606
Bram Moolenaar258cef52019-08-21 17:29:29 +02003607func Test_popupmenu_info_align_menu()
3608 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003609 CheckFeature quickfix
Bram Moolenaar258cef52019-08-21 17:29:29 +02003610
3611 let lines = Get_popupmenu_lines()
3612 call add(lines, 'set completepopup=height:4,border:off,align:menu')
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003613 call writefile(lines, 'XtestInfoPopupNb', 'D')
Bram Moolenaar258cef52019-08-21 17:29:29 +02003614
3615 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003616 call TermWait(buf, 25)
Bram Moolenaar258cef52019-08-21 17:29:29 +02003617
3618 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3619 call term_sendkeys(buf, "\<C-N>")
3620 call term_sendkeys(buf, "\<C-N>")
3621 call term_sendkeys(buf, "\<C-N>")
3622 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
3623
3624 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
3625 call term_sendkeys(buf, "\<C-N>")
3626 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
3627
3628 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003629 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02003630 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
3631 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
3632 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
3633
3634 call StopVimInTerminal(buf)
Bram Moolenaar258cef52019-08-21 17:29:29 +02003635endfunc
3636
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003637func Test_popupmenu_info_hidden()
3638 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003639 CheckFeature quickfix
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003640
3641 let lines = Get_popupmenu_lines()
3642 call add(lines, 'call InfoHidden()')
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003643 call writefile(lines, 'XtestInfoPopupHidden', 'D')
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003644
3645 let buf = RunVimInTerminal('-S XtestInfoPopupHidden', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003646 call TermWait(buf, 25)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003647
3648 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3649 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_1', {})
3650
3651 call term_sendkeys(buf, "\<C-N>")
3652 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_2', {})
3653
3654 call term_sendkeys(buf, "\<C-N>")
3655 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_3', {})
3656
3657 call term_sendkeys(buf, "\<Esc>")
3658 call StopVimInTerminal(buf)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003659endfunc
3660
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003661func Test_popupmenu_info_too_wide()
3662 CheckScreendump
3663 CheckFeature quickfix
3664
3665 let lines =<< trim END
3666 call setline(1, range(10))
3667
3668 set completeopt+=preview,popup
3669 set completepopup=align:menu
3670 set omnifunc=OmniFunc
3671 hi InfoPopup ctermbg=lightgrey
3672
3673 func OmniFunc(findstart, base)
3674 if a:findstart
3675 return 0
3676 endif
3677
3678 let menuText = 'some long text to make sure the menu takes up all of the width of the window'
3679 return #{
3680 \ words: [
3681 \ #{
3682 \ word: 'scrap',
3683 \ menu: menuText,
3684 \ info: "other words are\ncooler than this and some more text\nto make wrap",
3685 \ },
3686 \ #{
3687 \ word: 'scappier',
3688 \ menu: menuText,
3689 \ info: 'words are cool',
3690 \ },
3691 \ #{
3692 \ word: 'scrappier2',
3693 \ menu: menuText,
3694 \ info: 'words are cool',
3695 \ },
3696 \ ]
3697 \ }
3698 endfunc
3699 END
3700
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003701 call writefile(lines, 'XtestInfoPopupWide', 'D')
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003702 let buf = RunVimInTerminal('-S XtestInfoPopupWide', #{rows: 8})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003703 call TermWait(buf, 25)
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003704
3705 call term_sendkeys(buf, "Ascr\<C-X>\<C-O>")
3706 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_wide_1', {})
3707
3708 call term_sendkeys(buf, "\<Esc>")
3709 call StopVimInTerminal(buf)
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003710endfunc
3711
Bakudankun65555002021-11-17 20:40:16 +00003712func Test_popupmenu_masking()
3713 " Test that popup windows that are opened while popup menu is open are
3714 " properly displayed.
3715 CheckScreendump
3716 CheckFeature quickfix
3717
3718 let lines = Get_popupmenu_lines()
3719 call add(lines, 'inoremap <C-A> <Cmd>call OpenOtherPopups()<CR>')
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003720 call writefile(lines, 'XtestPopupmenuMasking', 'D')
Bakudankun65555002021-11-17 20:40:16 +00003721
3722 let buf = RunVimInTerminal('-S XtestPopupmenuMasking', #{rows: 14})
3723 call TermWait(buf, 25)
3724
3725 call term_sendkeys(buf, "A\<C-X>\<C-U>\<C-A>")
3726 call VerifyScreenDump(buf, 'Test_popupwin_popupmenu_masking_1', {})
3727
3728 call term_sendkeys(buf, "\<Esc>")
3729 call VerifyScreenDump(buf, 'Test_popupwin_popupmenu_masking_2', {})
3730
3731 call StopVimInTerminal(buf)
Bakudankun65555002021-11-17 20:40:16 +00003732endfunc
3733
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003734func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02003735 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003736 call popup_clear()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003737 let winid = 'nothing wrong'->popup_notification({})
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003738 call assert_equal(bufnr, winbufnr(winid))
3739 call popup_clear()
3740endfunc
3741
Bram Moolenaar1824f452019-10-02 23:06:46 +02003742func Test_popupwin_getoptions_tablocal()
3743 topleft split
3744 let win1 = popup_create('nothing', #{maxheight: 8})
3745 let win2 = popup_create('something', #{maxheight: 10})
3746 let win3 = popup_create('something', #{maxheight: 15})
3747 call assert_equal(8, popup_getoptions(win1).maxheight)
3748 call assert_equal(10, popup_getoptions(win2).maxheight)
3749 call assert_equal(15, popup_getoptions(win3).maxheight)
3750 call popup_clear()
3751 quit
3752endfunc
3753
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02003754func Test_popupwin_cancel()
3755 let win1 = popup_create('one', #{line: 5, filter: {... -> 0}})
3756 let win2 = popup_create('two', #{line: 10, filter: {... -> 0}})
3757 let win3 = popup_create('three', #{line: 15, filter: {... -> 0}})
3758 call assert_equal(5, popup_getpos(win1).line)
3759 call assert_equal(10, popup_getpos(win2).line)
3760 call assert_equal(15, popup_getpos(win3).line)
3761 " TODO: this also works without patch 8.1.2110
3762 call feedkeys("\<C-C>", 'xt')
3763 call assert_equal(5, popup_getpos(win1).line)
3764 call assert_equal(10, popup_getpos(win2).line)
3765 call assert_equal({}, popup_getpos(win3))
3766 call feedkeys("\<C-C>", 'xt')
3767 call assert_equal(5, popup_getpos(win1).line)
3768 call assert_equal({}, popup_getpos(win2))
3769 call assert_equal({}, popup_getpos(win3))
3770 call feedkeys("\<C-C>", 'xt')
3771 call assert_equal({}, popup_getpos(win1))
3772 call assert_equal({}, popup_getpos(win2))
3773 call assert_equal({}, popup_getpos(win3))
3774endfunc
3775
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003776func Test_popupwin_filter_redraw()
3777 " Create two popups with a filter that closes the popup when typing "0".
3778 " Both popups should close, even though the redraw also calls
3779 " popup_reset_handled()
3780
3781 func CloseFilter(winid, key)
3782 if a:key == '0'
3783 call popup_close(a:winid)
3784 redraw
3785 endif
3786 return 0 " pass the key
3787 endfunc
3788
3789 let id1 = popup_create('first one', #{
3790 \ line: 1,
3791 \ col: 1,
3792 \ filter: 'CloseFilter',
3793 \ })
3794 let id2 = popup_create('second one', #{
3795 \ line: 9,
3796 \ col: 1,
3797 \ filter: 'CloseFilter',
3798 \ })
3799 call assert_equal(1, popup_getpos(id1).line)
3800 call assert_equal(9, popup_getpos(id2).line)
3801
3802 call feedkeys('0', 'xt')
3803 call assert_equal({}, popup_getpos(id1))
3804 call assert_equal({}, popup_getpos(id2))
3805
3806 call popup_clear()
3807 delfunc CloseFilter
3808endfunc
3809
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003810func Test_popupwin_double_width()
3811 CheckScreendump
3812
3813 let lines =<< trim END
3814 call setline(1, 'x你好世界你好世你好世界你好')
3815 call setline(2, '你好世界你好世你好世界你好')
3816 call setline(3, 'x你好世界你好世你好世界你好')
3817 call popup_create('你好,世界 - 你好,世界xxxxx', #{line: 1, col: 3, maxwidth: 14})
3818 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003819 call writefile(lines, 'XtestPopupWide', 'D')
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003820
3821 let buf = RunVimInTerminal('-S XtestPopupWide', #{rows: 10})
3822 call VerifyScreenDump(buf, 'Test_popupwin_doublewidth_1', {})
3823
3824 call StopVimInTerminal(buf)
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003825endfunc
3826
3827func Test_popupwin_sign()
3828 CheckScreendump
3829
3830 let lines =<< trim END
3831 call setline(1, range(10))
3832 call sign_define('Current', {
3833 \ 'text': '>>',
3834 \ 'texthl': 'WarningMsg',
3835 \ 'linehl': 'Error',
3836 \ })
3837 call sign_define('Other', {
3838 \ 'text': '#!',
3839 \ 'texthl': 'Error',
3840 \ 'linehl': 'Search',
3841 \ })
3842 let winid = popup_create(['hello', 'bright', 'world'], {
3843 \ 'minwidth': 20,
3844 \ })
3845 call setwinvar(winid, "&signcolumn", "yes")
3846 let winbufnr = winbufnr(winid)
3847
3848 " add sign to current buffer, shows
3849 call sign_place(1, 'Selected', 'Current', bufnr('%'), {'lnum': 1})
3850 " add sign to current buffer, does not show
3851 call sign_place(2, 'PopUpSelected', 'Other', bufnr('%'), {'lnum': 2})
3852
3853 " add sign to popup buffer, shows
3854 call sign_place(3, 'PopUpSelected', 'Other', winbufnr, {'lnum': 1})
3855 " add sign to popup buffer, does not show
3856 call sign_place(4, 'Selected', 'Current', winbufnr, {'lnum': 2})
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02003857
3858 func SetOptions()
3859 call setwinvar(g:winid, '&number', 1)
3860 call setwinvar(g:winid, '&foldcolumn', 2)
3861 call popup_settext(g:winid, 'a longer line to check the width')
3862 endfunc
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003863 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003864 call writefile(lines, 'XtestPopupSign', 'D')
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003865
3866 let buf = RunVimInTerminal('-S XtestPopupSign', #{rows: 10})
3867 call VerifyScreenDump(buf, 'Test_popupwin_sign_1', {})
3868
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02003869 " set more options to check the width is adjusted
3870 call term_sendkeys(buf, ":call SetOptions()\<CR>")
3871 call VerifyScreenDump(buf, 'Test_popupwin_sign_2', {})
3872
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003873 call StopVimInTerminal(buf)
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003874endfunc
3875
Bram Moolenaar99ebf222019-12-10 23:44:48 +01003876func Test_popupwin_bufnr()
3877 let popwin = popup_create(['blah'], #{})
3878 let popbuf = winbufnr(popwin)
3879 split asdfasdf
3880 let newbuf = bufnr()
3881 call assert_true(newbuf > popbuf, 'New buffer number is higher')
3882 call assert_equal(newbuf, bufnr('$'))
3883 call popup_clear()
3884 let popwin = popup_create(['blah'], #{})
3885 " reuses previous buffer number
3886 call assert_equal(popbuf, winbufnr(popwin))
3887 call assert_equal(newbuf, bufnr('$'))
3888
3889 call popup_clear()
3890 bwipe!
3891endfunc
3892
Bram Moolenaarec084d32020-02-28 22:44:47 +01003893func Test_popupwin_filter_input_multibyte()
3894 func MyPopupFilter(winid, c)
3895 let g:bytes = range(a:c->strlen())->map({i -> char2nr(a:c[i])})
3896 return 0
3897 endfunc
3898 let winid = popup_create('', #{mapping: 0, filter: 'MyPopupFilter'})
3899
3900 " UTF-8: E3 80 80, including K_SPECIAL(0x80)
3901 call feedkeys("\u3000", 'xt')
3902 call assert_equal([0xe3, 0x80, 0x80], g:bytes)
3903
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01003904 " UTF-8: E3 80 9B, including CSI(0x9B)
3905 call feedkeys("\u301b", 'xt')
3906 call assert_equal([0xe3, 0x80, 0x9b], g:bytes)
Bram Moolenaarec084d32020-02-28 22:44:47 +01003907
Bram Moolenaar20298ce2020-06-19 21:46:52 +02003908 if has('unix')
3909 " with modifyOtherKeys <M-S-a> does not include a modifier sequence
Bram Moolenaarb326edf2020-06-20 15:03:38 +02003910 if has('gui_running')
3911 call feedkeys("\x9b\xfc\x08A", 'Lx!')
3912 else
3913 call feedkeys("\<Esc>[27;4;65~", 'Lx!')
3914 endif
Bram Moolenaar20298ce2020-06-19 21:46:52 +02003915 call assert_equal([0xc3, 0x81], g:bytes)
3916 endif
3917
Bram Moolenaarec084d32020-02-28 22:44:47 +01003918 call popup_clear()
3919 delfunc MyPopupFilter
3920 unlet g:bytes
3921endfunc
3922
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003923func Test_popupwin_filter_close_ctrl_c()
3924 CheckScreendump
3925
3926 let lines =<< trim END
3927 vsplit
3928 set laststatus=2
3929 set statusline=%!Statusline()
3930
3931 function Statusline() abort
3932 return '%<%f %h%m%r%=%-14.(%l,%c%V%) %P'
3933 endfunction
3934
3935 call popup_create('test test test test...', {'filter': {-> 0}})
3936 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003937 call writefile(lines, 'XtestPopupCtrlC', 'D')
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003938
3939 let buf = RunVimInTerminal('-S XtestPopupCtrlC', #{rows: 10})
3940
3941 call term_sendkeys(buf, "\<C-C>")
3942 call VerifyScreenDump(buf, 'Test_popupwin_ctrl_c', {})
3943
3944 call StopVimInTerminal(buf)
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003945endfunc
3946
3947func Test_popupwin_filter_close_wrong_name()
3948 CheckScreendump
3949
3950 let lines =<< trim END
3951 call popup_create('one two three...', {'filter': 'NoSuchFunc'})
3952 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003953 call writefile(lines, 'XtestPopupWrongName', 'D')
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003954
3955 let buf = RunVimInTerminal('-S XtestPopupWrongName', #{rows: 10})
3956
3957 call term_sendkeys(buf, "j")
3958 call VerifyScreenDump(buf, 'Test_popupwin_wrong_name', {})
3959
3960 call StopVimInTerminal(buf)
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003961endfunc
3962
3963func Test_popupwin_filter_close_three_errors()
3964 CheckScreendump
3965
3966 let lines =<< trim END
3967 set cmdheight=2
3968 call popup_create('one two three...', {'filter': 'filter'})
3969 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003970 call writefile(lines, 'XtestPopupThreeErrors', 'D')
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003971
3972 let buf = RunVimInTerminal('-S XtestPopupThreeErrors', #{rows: 10})
3973
3974 call term_sendkeys(buf, "jj")
3975 call VerifyScreenDump(buf, 'Test_popupwin_three_errors_1', {})
3976 call term_sendkeys(buf, "j")
3977 call VerifyScreenDump(buf, 'Test_popupwin_three_errors_2', {})
3978
3979 call StopVimInTerminal(buf)
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003980endfunc
3981
Bram Moolenaar927495b2020-11-06 17:58:35 +01003982func Test_popupwin_latin1_encoding()
3983 CheckScreendump
3984 CheckUnix
3985
3986 " When 'encoding' is a single-byte encoding a terminal window will mess up
3987 " the display. Check that showing a popup on top of that doesn't crash.
3988 let lines =<< trim END
3989 set encoding=latin1
3990 terminal cat Xmultibyte
3991 call popup_create(['one', 'two', 'three', 'four'], #{line: 1, col: 10})
Bram Moolenaar4c5bdb92020-12-17 17:45:59 +01003992 redraw
Bram Moolenaarb125b532020-12-17 21:56:09 +01003993 " wait for "cat" to finish
3994 while execute('ls!') !~ 'finished'
3995 sleep 10m
3996 endwhile
Bram Moolenaar4c5bdb92020-12-17 17:45:59 +01003997 echo "Done"
Bram Moolenaar927495b2020-11-06 17:58:35 +01003998 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003999 call writefile(lines, 'XtestPopupLatin', 'D')
4000 call writefile([repeat("\u3042 ", 120)], 'Xmultibyte', 'D')
Bram Moolenaar927495b2020-11-06 17:58:35 +01004001
4002 let buf = RunVimInTerminal('-S XtestPopupLatin', #{rows: 10})
Bram Moolenaar4c5bdb92020-12-17 17:45:59 +01004003 call WaitForAssert({-> assert_match('Done', term_getline(buf, 10))})
Bram Moolenaar927495b2020-11-06 17:58:35 +01004004
4005 call term_sendkeys(buf, ":q\<CR>")
4006 call StopVimInTerminal(buf)
Bram Moolenaar927495b2020-11-06 17:58:35 +01004007endfunc
4008
Bram Moolenaarba2920f2020-03-06 21:43:17 +01004009func Test_popupwin_atcursor_far_right()
4010 new
4011
4012 " this was getting stuck
4013 set signcolumn=yes
4014 call setline(1, repeat('=', &columns))
4015 normal! ggg$
Bram Moolenaaref6b9792020-05-13 16:34:15 +02004016 let winid = popup_atcursor(repeat('x', 500), #{moved: 'any', border: []})
Bram Moolenaarba2920f2020-03-06 21:43:17 +01004017
Bram Moolenaar6d585f42020-07-26 22:20:54 +02004018 " 'signcolumn' was getting reset
4019 call setwinvar(winid, '&signcolumn', 'yes')
4020 call popup_setoptions(winid, {'zindex': 1000})
4021 call assert_equal('yes', getwinvar(winid, '&signcolumn'))
4022
Bram Moolenaaref6b9792020-05-13 16:34:15 +02004023 call popup_close(winid)
Bram Moolenaarba2920f2020-03-06 21:43:17 +01004024 bwipe!
4025 set signcolumn&
4026endfunc
4027
Bram Moolenaar0f1563f2020-03-20 21:15:51 +01004028func Test_popupwin_splitmove()
4029 vsplit
4030 let win2 = win_getid()
4031 let popup_winid = popup_dialog('hello', {})
4032 call assert_fails('call win_splitmove(popup_winid, win2)', 'E957:')
4033 call assert_fails('call win_splitmove(win2, popup_winid)', 'E957:')
4034
4035 call popup_clear()
4036 bwipe
4037endfunc
4038
Bram Moolenaarcbcd9cb2020-11-07 16:58:59 +01004039func Test_popupwin_exiting_terminal()
4040 CheckFeature terminal
4041
4042 " Tests that when creating a popup right after closing a terminal window does
4043 " not make the popup the current window.
4044 let winid = win_getid()
4045 try
4046 augroup Test_popupwin_exiting_terminal
4047 autocmd!
4048 autocmd WinEnter * :call popup_create('test', {})
4049 augroup END
4050 let bnr = term_start(&shell, #{term_finish: 'close'})
4051 call term_sendkeys(bnr, "exit\r\n")
4052 call WaitForAssert({-> assert_equal(winid, win_getid())})
4053 finally
4054 call popup_clear(1)
4055 augroup Test_popupwin_exiting_terminal
4056 autocmd!
4057 augroup END
4058 endtry
4059endfunc
Bram Moolenaar0f1563f2020-03-20 21:15:51 +01004060
Bram Moolenaar014f6982021-01-04 13:18:30 +01004061func Test_popup_filter_menu()
4062 let colors = ['red', 'green', 'blue']
4063 call popup_menu(colors, #{callback: {_, result -> assert_equal('green', colors[result - 1])}})
4064 call feedkeys("\<c-n>\<c-n>\<c-p>\<cr>", 'xt')
4065endfunc
4066
Bram Moolenaar65026432021-02-06 14:22:32 +01004067func Test_popup_getoptions_other_tab()
4068 new
4069 call setline(1, 'some text')
4070 call prop_type_add('textprop', {})
4071 call prop_add(1, 1, #{type: 'textprop', length: 1})
4072 let id = popup_create('TEST', #{textprop: 'textprop', highlight: 'ErrorMsg', tabpage: 1})
4073 tab sp
4074 call assert_equal(['textprop', 'textpropid', 'textpropwin'], popup_getoptions(id)->keys()->filter({_, v -> v =~ 'textprop'}))
4075
4076 tabclose
Bram Moolenaarefa19232021-02-06 14:59:27 +01004077 call popup_close(id)
Bram Moolenaar65026432021-02-06 14:22:32 +01004078 bwipe!
4079 call prop_type_delete('textprop')
4080endfunc
4081
Bram Moolenaarefa19232021-02-06 14:59:27 +01004082
4083func Test_popup_setoptions_other_tab()
Bram Moolenaarb18b4962022-09-02 21:55:50 +01004084 new Xpotfile
Bram Moolenaarefa19232021-02-06 14:59:27 +01004085 let winid = win_getid()
4086 call setline(1, 'some text')
4087 call prop_type_add('textprop', {})
4088 call prop_add(1, 1, #{type: 'textprop', length: 1})
4089 let id = popup_create('TEST', #{textprop: 'textprop'})
4090 tab sp
4091 call popup_setoptions(id, #{textprop: 'textprop', textpropwin: winid})
4092 call assert_equal(winid, popup_getoptions(id).textpropwin)
4093
4094 tabclose
4095 call popup_close(id)
Bram Moolenaarb18b4962022-09-02 21:55:50 +01004096 bwipe! Xpotfile
Bram Moolenaarefa19232021-02-06 14:59:27 +01004097 call prop_type_delete('textprop')
4098endfunc
4099
Bram Moolenaar82db31c2021-02-10 14:56:11 +01004100func Test_popup_prop_not_visible()
4101 CheckScreendump
4102
4103 let lines =<< trim END
4104 vim9script
4105 set nowrap stal=2
4106 rightbelow :31vnew
4107 setline(1, ['', 'some text', '', 'other text'])
4108 prop_type_add('someprop', {})
4109 prop_add(2, 9, {type: 'someprop', length: 5})
Bram Moolenaar27724252022-05-08 15:00:04 +01004110 g:some_id = popup_create('attached to "some"', {
Bram Moolenaar82db31c2021-02-10 14:56:11 +01004111 textprop: 'someprop',
4112 highlight: 'ErrorMsg',
4113 line: -1,
4114 wrap: false,
4115 fixed: true,
4116 })
4117 prop_type_add('otherprop', {})
4118 prop_add(4, 10, {type: 'otherprop', length: 5})
4119 popup_create('attached to "other"', {
4120 textprop: 'otherprop',
4121 highlight: 'ErrorMsg',
4122 line: -1,
4123 wrap: false,
4124 fixed: false,
4125 })
4126 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01004127 call writefile(lines, 'XtestPropNotVisble', 'D')
Bram Moolenaar82db31c2021-02-10 14:56:11 +01004128 let buf = RunVimInTerminal('-S XtestPropNotVisble', #{rows: 10})
4129 call VerifyScreenDump(buf, 'Test_popup_prop_not_visible_01', {})
4130
Bram Moolenaar27724252022-05-08 15:00:04 +01004131 " check that hiding and unhiding the popup works
4132 call term_sendkeys(buf, ":call popup_hide(g:some_id)\<CR>")
4133 call VerifyScreenDump(buf, 'Test_popup_prop_not_visible_01a', {})
4134 call term_sendkeys(buf, ":call popup_show(g:some_id)\<CR>")
4135 call VerifyScreenDump(buf, 'Test_popup_prop_not_visible_01b', {})
4136
Bram Moolenaar82db31c2021-02-10 14:56:11 +01004137 call term_sendkeys(buf, ":vert resize -14\<CR>")
4138 call VerifyScreenDump(buf, 'Test_popup_prop_not_visible_02', {})
4139
4140 call term_sendkeys(buf, ":vert resize -8\<CR>")
4141 call VerifyScreenDump(buf, 'Test_popup_prop_not_visible_03', {})
4142
4143 " clean up
4144 call StopVimInTerminal(buf)
Bram Moolenaar82db31c2021-02-10 14:56:11 +01004145endfunction
4146
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02004147func Test_bufdel_skips_popupwin_buffer()
4148 let id = popup_create("Some text", {})
4149 %bd
4150 call popup_close(id)
4151endfunc
4152
Bram Moolenaar58a3cae2022-09-08 13:43:10 +01004153func Test_term_popup_bufline()
4154 " very specific situation where a non-existing buffer line is used, leading
4155 " to an ml_get error
4156 CheckScreendump
4157
4158 let lines =<< trim END
4159 vim9script
4160 &scrolloff = 5
4161 term_start('seq 1 5', {term_finish: 'open'})
4162 timer_start(50, (_) => {
4163 set cpoptions&vim
4164 var buf = popup_create([], {})->winbufnr()
4165 appendbufline(buf, 0, range(5))
4166 })
4167 END
4168 call writefile(lines, 'XtestTermPopup', 'D')
4169 let buf = RunVimInTerminal('-S XtestTermPopup', #{rows: 15})
4170 call VerifyScreenDump(buf, 'Test_term_popup_bufline', {})
4171
4172 " clean up
4173 call StopVimInTerminal(buf)
4174endfunc
4175
Bram Moolenaar82db31c2021-02-10 14:56:11 +01004176
Bram Moolenaar331bafd2019-07-20 17:46:05 +02004177" vim: shiftwidth=2 sts=2