blob: d89824df0db2a0f8fb0c4ba97c0dbf9bed045ed6 [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
22 call writefile(lines, 'XtestPopup')
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>")
68 call term_wait(buf)
69 call term_sendkeys(buf, "0")
70 call term_wait(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)
76 call delete('XtestPopup')
77endfunc
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020078
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020079func Test_popup_with_border_and_padding()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +020080 CheckScreendump
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020081
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020082 for iter in range(0, 1)
Bram Moolenaare7eb9272019-06-24 00:58:07 +020083 let lines =<< trim END
84 call setline(1, range(1, 100))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020085 call popup_create('hello border', #{line: 2, col: 3, border: []})
86 call popup_create('hello padding', #{line: 2, col: 23, padding: []})
Bram Moolenaarc363fe12019-08-04 18:13:46 +020087 call popup_create('hello both', #{line: 2, col: 43, border: [], padding: [], highlight: 'Normal'})
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020088 call popup_create('border TL', #{line: 6, col: 3, border: [1, 0, 0, 4]})
Bram Moolenaarb0992022020-01-30 14:55:42 +010089 call popup_create('paddings', #{line: 6, col: 23, padding: range(1, 4)})
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020090 call popup_create('wrapped longer text', #{line: 8, col: 55, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
91 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 +010092 call popup_create('X', #{line: 2, col: 73})
93 call popup_create('X', #{line: 3, col: 74})
94 call popup_create('X', #{line: 4, col: 75})
95 call popup_create('X', #{line: 5, col: 76})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020096 END
97 call insert(lines, iter == 1 ? '' : 'set enc=latin1')
98 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020099 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 15})
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +0200100 call VerifyScreenDump(buf, 'Test_popupwin_2' .. iter, {})
101
102 call StopVimInTerminal(buf)
103 call delete('XtestPopupBorder')
104 endfor
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200105
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200106 let lines =<< trim END
107 call setline(1, range(1, 100))
108 hi BlueColor ctermbg=lightblue
109 hi TopColor ctermbg=253
110 hi RightColor ctermbg=245
111 hi BottomColor ctermbg=240
112 hi LeftColor ctermbg=248
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200113 call popup_create('hello border', #{line: 2, col: 3, border: [], borderhighlight: ['BlueColor']})
114 call popup_create(['hello border', 'and more'], #{line: 2, col: 23, border: [], borderhighlight: ['TopColor', 'RightColor', 'BottomColor', 'LeftColor']})
115 call popup_create(['hello border', 'lines only'], #{line: 2, col: 43, border: [], borderhighlight: ['BlueColor'], borderchars: ['x']})
116 call popup_create(['hello border', 'with corners'], #{line: 2, col: 60, border: [], borderhighlight: ['BlueColor'], borderchars: ['x', '#']})
117 let winid = popup_create(['hello border', 'with numbers'], #{line: 6, col: 3, border: [], borderhighlight: ['BlueColor'], borderchars: ['0', '1', '2', '3', '4', '5', '6', '7']})
118 call popup_create(['hello border', 'just blanks'], #{line: 7, col: 23, border: [], borderhighlight: ['BlueColor'], borderchars: [' ']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200119 func MultiByte()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200120 call popup_create(['hello'], #{line: 8, col: 43, border: [], borderchars: ['─', '│', '─', '│', '┌', '┐', '┘', '└']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200121 endfunc
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200122 END
123 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200124 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 12})
Bram Moolenaar790498b2019-06-01 22:15:29 +0200125 call VerifyScreenDump(buf, 'Test_popupwin_22', {})
126
Bram Moolenaarad24a712019-06-17 20:05:45 +0200127 " check that changing borderchars triggers a redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200128 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 +0200129 call VerifyScreenDump(buf, 'Test_popupwin_23', {})
130
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200131 " check multi-byte border only with 'ambiwidth' single
132 if &ambiwidth == 'single'
133 call term_sendkeys(buf, ":call MultiByte()\<CR>")
134 call VerifyScreenDump(buf, 'Test_popupwin_24', {})
135 endif
136
Bram Moolenaar790498b2019-06-01 22:15:29 +0200137 call StopVimInTerminal(buf)
138 call delete('XtestPopupBorder')
139
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200140 let with_border_or_padding = #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200141 \ line: 2,
142 \ core_line: 3,
143 \ col: 3,
144 \ core_col: 4,
145 \ width: 14,
146 \ core_width: 12,
147 \ height: 3,
148 \ core_height: 1,
149 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100150 \ lastline: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200151 \ scrollbar: 0,
152 \ visible: 1}
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200153 let winid = popup_create('hello border', #{line: 2, col: 3, border: []})",
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200154 call assert_equal(with_border_or_padding, winid->popup_getpos())
Bram Moolenaarae943152019-06-16 22:54:14 +0200155 let options = popup_getoptions(winid)
156 call assert_equal([], options.border)
157 call assert_false(has_key(options, "padding"))
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200158
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200159 let winid = popup_create('hello padding', #{line: 2, col: 3, padding: []})
Bram Moolenaarae943152019-06-16 22:54:14 +0200160 let with_border_or_padding.width = 15
161 let with_border_or_padding.core_width = 13
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200162 call assert_equal(with_border_or_padding, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200163 let options = popup_getoptions(winid)
164 call assert_false(has_key(options, "border"))
165 call assert_equal([], options.padding)
166
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200167 call popup_setoptions(winid, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200168 \ padding: [1, 2, 3, 4],
169 \ border: [4, 0, 7, 8],
170 \ borderhighlight: ['Top', 'Right', 'Bottom', 'Left'],
171 \ borderchars: ['1', '^', '2', '>', '3', 'v', '4', '<'],
Bram Moolenaarae943152019-06-16 22:54:14 +0200172 \ })
173 let options = popup_getoptions(winid)
174 call assert_equal([1, 0, 1, 1], options.border)
175 call assert_equal([1, 2, 3, 4], options.padding)
176 call assert_equal(['Top', 'Right', 'Bottom', 'Left'], options.borderhighlight)
177 call assert_equal(['1', '^', '2', '>', '3', 'v', '4', '<'], options.borderchars)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200178
Bram Moolenaara1396152019-10-20 18:46:05 +0200179 " Check that popup_setoptions() takes the output of popup_getoptions()
180 call popup_setoptions(winid, options)
181 call assert_equal(options, popup_getoptions(winid))
182
Bram Moolenaarb0992022020-01-30 14:55:42 +0100183 " Check that range() doesn't crash
184 call popup_setoptions(winid, #{
185 \ padding: range(1, 4),
186 \ border: range(5, 8),
187 \ borderhighlight: range(4),
188 \ borderchars: range(8),
189 \ })
190
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200191 let winid = popup_create('hello both', #{line: 3, col: 8, border: [], padding: []})
192 call assert_equal(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200193 \ line: 3,
194 \ core_line: 5,
195 \ col: 8,
196 \ core_col: 10,
197 \ width: 14,
198 \ core_width: 10,
199 \ height: 5,
200 \ scrollbar: 0,
201 \ core_height: 1,
202 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100203 \ lastline: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200204 \ visible: 1}, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200205
206 call popup_clear()
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200207endfunc
208
Bram Moolenaarb4230122019-05-30 18:40:53 +0200209func Test_popup_with_syntax_win_execute()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200210 CheckScreendump
211
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200212 let lines =<< trim END
213 call setline(1, range(1, 100))
214 hi PopupColor ctermbg=lightblue
215 let winid = popup_create([
216 \ '#include <stdio.h>',
217 \ 'int main(void)',
218 \ '{',
219 \ ' printf(123);',
220 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200221 \], #{line: 3, col: 25, highlight: 'PopupColor'})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200222 call win_execute(winid, 'set syntax=cpp')
223 END
224 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200225 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200226 call VerifyScreenDump(buf, 'Test_popupwin_10', {})
227
228 " clean up
229 call StopVimInTerminal(buf)
230 call delete('XtestPopup')
231endfunc
232
233func Test_popup_with_syntax_setbufvar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200234 CheckScreendump
235
Bram Moolenaar402502d2019-05-30 22:07:36 +0200236 let lines =<< trim END
237 call setline(1, range(1, 100))
238 hi PopupColor ctermbg=lightgrey
239 let winid = popup_create([
240 \ '#include <stdio.h>',
241 \ 'int main(void)',
242 \ '{',
Bram Moolenaare089c3f2019-07-09 20:25:25 +0200243 \ "\tprintf(567);",
Bram Moolenaar402502d2019-05-30 22:07:36 +0200244 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200245 \], #{line: 3, col: 21, highlight: 'PopupColor'})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200246 call setbufvar(winbufnr(winid), '&syntax', 'cpp')
247 END
248 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200249 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200250 call VerifyScreenDump(buf, 'Test_popupwin_11', {})
251
252 " clean up
253 call StopVimInTerminal(buf)
254 call delete('XtestPopup')
255endfunc
256
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200257func Test_popup_with_matches()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200258 CheckScreendump
259
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200260 let lines =<< trim END
261 call setline(1, ['111 222 333', '444 555 666'])
262 let winid = popup_create([
263 \ '111 222 333',
264 \ '444 555 666',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200265 \], #{line: 3, col: 10, border: []})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200266 set hlsearch
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100267 hi VeryBlue ctermfg=blue guifg=blue
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200268 /666
269 call matchadd('ErrorMsg', '111')
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100270 call matchadd('VeryBlue', '444')
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200271 call win_execute(winid, "call matchadd('ErrorMsg', '111')")
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100272 call win_execute(winid, "call matchadd('VeryBlue', '555')")
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200273 END
274 call writefile(lines, 'XtestPopupMatches')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200275 let buf = RunVimInTerminal('-S XtestPopupMatches', #{rows: 10})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200276 call VerifyScreenDump(buf, 'Test_popupwin_matches', {})
277
278 " clean up
279 call StopVimInTerminal(buf)
280 call delete('XtestPopupMatches')
281endfunc
282
Bram Moolenaar399d8982019-06-02 15:34:29 +0200283func Test_popup_all_corners()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200284 CheckScreendump
285
Bram Moolenaar399d8982019-06-02 15:34:29 +0200286 let lines =<< trim END
287 call setline(1, repeat([repeat('-', 60)], 15))
288 set so=0
289 normal 2G3|r#
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200290 let winid1 = popup_create(['first', 'second'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200291 \ line: 'cursor+1',
292 \ col: 'cursor',
293 \ pos: 'topleft',
294 \ border: [],
295 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200296 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200297 normal 24|r@
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200298 let winid1 = popup_create(['First', 'SeconD'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200299 \ line: 'cursor+1',
300 \ col: 'cursor',
301 \ pos: 'topright',
302 \ border: [],
303 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200304 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200305 normal 9G27|r%
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200306 let winid1 = popup_create(['fiRSt', 'seCOnd'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200307 \ line: 'cursor-1',
308 \ col: 'cursor',
309 \ pos: 'botleft',
310 \ border: [],
311 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200312 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200313 normal 48|r&
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200314 let winid1 = popup_create(['FIrsT', 'SEcoND'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200315 \ line: 'cursor-1',
316 \ col: 'cursor',
317 \ pos: 'botright',
318 \ border: [],
319 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200320 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200321 normal 1G51|r*
322 let winid1 = popup_create(['one', 'two'], #{
323 \ line: 'cursor-1',
324 \ col: 'cursor',
325 \ pos: 'botleft',
326 \ border: [],
327 \ padding: [],
328 \ })
Bram Moolenaar399d8982019-06-02 15:34:29 +0200329 END
330 call writefile(lines, 'XtestPopupCorners')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200331 let buf = RunVimInTerminal('-S XtestPopupCorners', #{rows: 12})
Bram Moolenaar399d8982019-06-02 15:34:29 +0200332 call VerifyScreenDump(buf, 'Test_popupwin_corners', {})
333
334 " clean up
335 call StopVimInTerminal(buf)
336 call delete('XtestPopupCorners')
337endfunc
338
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100339func Test_popup_nospace()
340 CheckScreendump
341
342 let lines =<< trim END
343 call setline(1, repeat([repeat('-', 60)], 15))
344 set so=0
345
346 " cursor in a line in top half, using "botleft" with popup that
347 " does fit
348 normal 5G2|r@
349 let winid1 = popup_create(['one', 'two'], #{
350 \ line: 'cursor-1',
351 \ col: 'cursor',
352 \ pos: 'botleft',
353 \ border: [],
354 \ })
355 " cursor in a line in top half, using "botleft" with popup that
356 " doesn't fit: gets truncated
357 normal 5G9|r#
358 let winid1 = popup_create(['one', 'two', 'tee'], #{
359 \ line: 'cursor-1',
360 \ col: 'cursor',
361 \ pos: 'botleft',
362 \ posinvert: 0,
363 \ border: [],
364 \ })
365 " cursor in a line in top half, using "botleft" with popup that
366 " doesn't fit and 'posinvert' set: flips to below.
367 normal 5G16|r%
368 let winid1 = popup_create(['one', 'two', 'tee'], #{
369 \ line: 'cursor-1',
370 \ col: 'cursor',
371 \ pos: 'botleft',
372 \ border: [],
373 \ })
374 " cursor in a line in bottom half, using "botleft" with popup that
375 " doesn't fit: does not flip.
376 normal 8G23|r*
377 let winid1 = popup_create(['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff'], #{
378 \ line: 'cursor-1',
379 \ col: 'cursor',
380 \ pos: 'botleft',
381 \ border: [],
382 \ })
383
384 " cursor in a line in bottom half, using "topleft" with popup that
385 " does fit
386 normal 8G30|r@
387 let winid1 = popup_create(['one', 'two'], #{
388 \ line: 'cursor+1',
389 \ col: 'cursor',
390 \ pos: 'topleft',
391 \ border: [],
392 \ })
393 " cursor in a line in top half, using "topleft" with popup that
394 " doesn't fit: truncated
395 normal 8G37|r#
396 let winid1 = popup_create(['one', 'two', 'tee'], #{
397 \ line: 'cursor+1',
398 \ col: 'cursor',
399 \ pos: 'topleft',
400 \ posinvert: 0,
401 \ border: [],
402 \ })
403 " cursor in a line in top half, using "topleft" with popup that
Bram Moolenaar5c6b6182019-11-10 17:51:38 +0100404 " doesn't fit and "posinvert" set: flips to above.
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100405 normal 8G44|r%
Bram Moolenaar5c6b6182019-11-10 17:51:38 +0100406 let winid1 = popup_create(['one', 'two', 'tee', 'fou', 'fiv'], #{
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100407 \ line: 'cursor+1',
408 \ col: 'cursor',
409 \ pos: 'topleft',
410 \ border: [],
411 \ })
412 " cursor in a line in top half, using "topleft" with popup that
413 " doesn't fit: does not flip.
414 normal 5G51|r*
415 let winid1 = popup_create(['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff'], #{
416 \ line: 'cursor+1',
417 \ col: 'cursor',
418 \ pos: 'topleft',
419 \ border: [],
420 \ })
421 END
422 call writefile(lines, 'XtestPopupNospace')
423 let buf = RunVimInTerminal('-S XtestPopupNospace', #{rows: 12})
424 call VerifyScreenDump(buf, 'Test_popupwin_nospace', {})
425
426 " clean up
427 call StopVimInTerminal(buf)
428 call delete('XtestPopupNospace')
429endfunc
430
Bram Moolenaar8d241042019-06-12 23:40:01 +0200431func Test_popup_firstline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200432 CheckScreendump
433
Bram Moolenaar8d241042019-06-12 23:40:01 +0200434 let lines =<< trim END
435 call setline(1, range(1, 20))
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200436 let winid = popup_create(['1111', '222222', '33333', '44', '5', '666666', '77777', '888', '9999999999999999'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200437 \ maxheight: 4,
438 \ firstline: 3,
Bram Moolenaar8d241042019-06-12 23:40:01 +0200439 \ })
440 END
441 call writefile(lines, 'XtestPopupFirstline')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200442 let buf = RunVimInTerminal('-S XtestPopupFirstline', #{rows: 10})
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200443 call VerifyScreenDump(buf, 'Test_popupwin_firstline_1', {})
444
445 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: -1})\<CR>")
446 call term_sendkeys(buf, ":\<CR>")
447 call VerifyScreenDump(buf, 'Test_popupwin_firstline_2', {})
Bram Moolenaar8d241042019-06-12 23:40:01 +0200448
449 " clean up
450 call StopVimInTerminal(buf)
451 call delete('XtestPopupFirstline')
Bram Moolenaarae943152019-06-16 22:54:14 +0200452
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200453 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200454 \ maxheight: 2,
455 \ firstline: 3,
Bram Moolenaarae943152019-06-16 22:54:14 +0200456 \ })
457 call assert_equal(3, popup_getoptions(winid).firstline)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200458 call popup_setoptions(winid, #{firstline: 1})
Bram Moolenaarae943152019-06-16 22:54:14 +0200459 call assert_equal(1, popup_getoptions(winid).firstline)
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200460 eval winid->popup_close()
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200461
462 let winid = popup_create(['xxx']->repeat(50), #{
463 \ maxheight: 3,
464 \ firstline: 11,
465 \ })
466 redraw
467 call assert_equal(11, popup_getoptions(winid).firstline)
468 call assert_equal(11, popup_getpos(winid).firstline)
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +0200469 " check line() works with popup window
470 call assert_equal(11, line('.', winid))
471 call assert_equal(50, line('$', winid))
472 call assert_equal(0, line('$', 123456))
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200473
474 " Normal command changes what is displayed but not "firstline"
475 call win_execute(winid, "normal! \<c-y>")
476 call assert_equal(11, popup_getoptions(winid).firstline)
477 call assert_equal(10, popup_getpos(winid).firstline)
478
479 " Making some property change applies "firstline" again
480 call popup_setoptions(winid, #{line: 4})
481 call assert_equal(11, popup_getoptions(winid).firstline)
482 call assert_equal(11, popup_getpos(winid).firstline)
483
484 " Remove "firstline" property and scroll
485 call popup_setoptions(winid, #{firstline: 0})
486 call win_execute(winid, "normal! \<c-y>")
487 call assert_equal(0, popup_getoptions(winid).firstline)
488 call assert_equal(10, popup_getpos(winid).firstline)
489
490 " Making some property change has no side effect
491 call popup_setoptions(winid, #{line: 3})
492 call assert_equal(0, popup_getoptions(winid).firstline)
493 call assert_equal(10, popup_getpos(winid).firstline)
Bram Moolenaarae943152019-06-16 22:54:14 +0200494
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100495 " CTRL-D scrolls down half a page
496 let winid = popup_create(['xxx']->repeat(50), #{
497 \ maxheight: 8,
498 \ })
499 redraw
500 call assert_equal(1, popup_getpos(winid).firstline)
501 call win_execute(winid, "normal! \<C-D>")
502 call assert_equal(5, popup_getpos(winid).firstline)
503 call win_execute(winid, "normal! \<C-D>")
504 call assert_equal(9, popup_getpos(winid).firstline)
505 call win_execute(winid, "normal! \<C-U>")
506 call assert_equal(5, popup_getpos(winid).firstline)
507
508 call win_execute(winid, "normal! \<C-F>")
509 call assert_equal(11, popup_getpos(winid).firstline)
510 call win_execute(winid, "normal! \<C-B>")
511 call assert_equal(5, popup_getpos(winid).firstline)
512
Bram Moolenaarae943152019-06-16 22:54:14 +0200513 call popup_close(winid)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200514endfunc
515
Bram Moolenaara112f2d2019-09-01 17:38:09 +0200516func Test_popup_noscrolloff()
517 set scrolloff=5
518 let winid = popup_create(['xxx']->repeat(50), #{
519 \ maxheight: 5,
520 \ firstline: 11,
521 \ })
522 redraw
523 call assert_equal(11, popup_getoptions(winid).firstline)
524 call assert_equal(11, popup_getpos(winid).firstline)
525
526 call popup_setoptions(winid, #{firstline: 0})
527 call win_execute(winid, "normal! \<c-y>")
528 call assert_equal(0, popup_getoptions(winid).firstline)
529 call assert_equal(10, popup_getpos(winid).firstline)
530
531 call popup_close(winid)
532endfunc
533
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200534func Test_popup_drag()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200535 CheckScreendump
536
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200537 " create a popup that covers the command line
538 let lines =<< trim END
539 call setline(1, range(1, 20))
Bram Moolenaar356375f2019-08-24 14:46:29 +0200540 split
541 vsplit
542 $wincmd w
543 vsplit
544 1wincmd w
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200545 let winid = popup_create(['1111', '222222', '33333'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200546 \ drag: 1,
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200547 \ resize: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200548 \ border: [],
549 \ line: &lines - 4,
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200550 \ })
551 func Dragit()
552 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
553 endfunc
554 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
Bram Moolenaar356375f2019-08-24 14:46:29 +0200555 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2 - 20)<CR>
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200556 func Resize()
557 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
558 endfunc
Bram Moolenaar356375f2019-08-24 14:46:29 +0200559 map <silent> <F5> :call test_setmouse(6, 21)<CR>
560 map <silent> <F6> :call test_setmouse(7, 25)<CR>
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200561 END
562 call writefile(lines, 'XtestPopupDrag')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200563 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200564 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
565
566 call term_sendkeys(buf, ":call Dragit()\<CR>")
567 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
568
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200569 call term_sendkeys(buf, ":call Resize()\<CR>")
570 call VerifyScreenDump(buf, 'Test_popupwin_drag_03', {})
571
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200572 " clean up
573 call StopVimInTerminal(buf)
574 call delete('XtestPopupDrag')
575endfunc
576
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200577func Test_popup_close_with_mouse()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200578 CheckScreendump
579
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200580 let lines =<< trim END
581 call setline(1, range(1, 20))
582 " With border, can click on X
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200583 let winid = popup_create('foobar', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200584 \ close: 'button',
585 \ border: [],
586 \ line: 1,
587 \ col: 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200588 \ })
589 func CloseMsg(id, result)
590 echomsg 'Popup closed with ' .. a:result
591 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200592 let winid = popup_create('notification', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200593 \ close: 'click',
594 \ line: 3,
595 \ col: 15,
596 \ callback: 'CloseMsg',
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200597 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200598 let winid = popup_create('no border here', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200599 \ close: 'button',
600 \ line: 5,
601 \ col: 3,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200602 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200603 let winid = popup_create('only padding', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200604 \ close: 'button',
605 \ padding: [],
606 \ line: 5,
607 \ col: 23,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200608 \ })
609 func CloseWithX()
610 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
611 endfunc
612 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
613 func CloseWithClick()
614 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
615 endfunc
616 map <silent> <F4> :call test_setmouse(3, 17)<CR>
Bram Moolenaarf6396232019-08-24 19:36:00 +0200617 func CreateWithMenuFilter()
618 let winid = popup_create('barfoo', #{
619 \ close: 'button',
620 \ filter: 'popup_filter_menu',
621 \ border: [],
622 \ line: 1,
623 \ col: 40,
624 \ })
625 endfunc
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200626 END
627 call writefile(lines, 'XtestPopupClose')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200628 let buf = RunVimInTerminal('-S XtestPopupClose', #{rows: 10})
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200629 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
630
631 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
632 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
633
634 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
635 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
636
Bram Moolenaarf6396232019-08-24 19:36:00 +0200637 call term_sendkeys(buf, ":call CreateWithMenuFilter()\<CR>")
638 call VerifyScreenDump(buf, 'Test_popupwin_close_04', {})
639
640 " We have to send the actual mouse code, feedkeys() would be caught the
641 " filter.
642 call term_sendkeys(buf, "\<Esc>[<0;47;1M")
643 call VerifyScreenDump(buf, 'Test_popupwin_close_05', {})
644
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200645 " clean up
646 call StopVimInTerminal(buf)
647 call delete('XtestPopupClose')
648endfunction
649
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200650func Test_popup_menu_wrap()
651 CheckScreendump
652
653 let lines =<< trim END
654 call setline(1, range(1, 20))
655 call popup_create([
656 \ 'one',
657 \ 'asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfas',
658 \ 'three',
659 \ 'four',
660 \ ], #{
661 \ pos: "botleft",
662 \ border: [],
663 \ padding: [0,1,0,1],
664 \ maxheight: 3,
665 \ cursorline: 1,
666 \ filter: 'popup_filter_menu',
667 \ })
668 END
669 call writefile(lines, 'XtestPopupWrap')
670 let buf = RunVimInTerminal('-S XtestPopupWrap', #{rows: 10})
671 call VerifyScreenDump(buf, 'Test_popupwin_wrap_1', {})
672
673 call term_sendkeys(buf, "jj")
674 call VerifyScreenDump(buf, 'Test_popupwin_wrap_2', {})
675
676 " clean up
677 call term_sendkeys(buf, "\<Esc>")
678 call StopVimInTerminal(buf)
679 call delete('XtestPopupWrap')
680endfunction
681
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200682func Test_popup_with_mask()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200683 CheckScreendump
684
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200685 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200686 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200687 hi PopupColor ctermbg=lightgrey
688 let winid = popup_create([
689 \ 'some text',
690 \ 'another line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200691 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200692 \ line: 1,
693 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100694 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200695 \ wrap: 0,
696 \ fixed: 1,
697 \ zindex: 90,
698 \ padding: [],
699 \ highlight: 'PopupColor',
700 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200701 call popup_create([
702 \ 'xxxxxxxxx',
703 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200704 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200705 \ line: 3,
706 \ col: 18,
707 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200708 let winidb = popup_create([
709 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200710 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200711 \ line: 7,
712 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100713 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200714 \ wrap: 0,
715 \ fixed: 1,
716 \ close: 'button',
717 \ zindex: 90,
718 \ padding: [],
719 \ border: [],
720 \ 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 +0200721 END
722 call writefile(lines, 'XtestPopupMask')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200723 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200724 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
725
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200726 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
727 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200728 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200729 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
730
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200731 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
732 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200733 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200734 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
735
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200736 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
737 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200738 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200739 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
740
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200741 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
742 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200743 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200744 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
745
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200746 " clean up
747 call StopVimInTerminal(buf)
748 call delete('XtestPopupMask')
749endfunc
750
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200751func Test_popup_select()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200752 CheckScreendump
753 CheckFeature clipboard_working
754
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200755 " create a popup with some text to be selected
756 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200757 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200758 call setline(1, range(1, 20))
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200759 let winid = popup_create(['the word', 'some more', 'several words here', 'invisible', '5', '6', '7'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200760 \ drag: 1,
761 \ border: [],
762 \ line: 3,
763 \ col: 10,
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200764 \ maxheight: 3,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200765 \ })
766 func Select1()
767 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
768 endfunc
769 map <silent> <F3> :call test_setmouse(4, 15)<CR>
770 map <silent> <F4> :call test_setmouse(6, 23)<CR>
771 END
772 call writefile(lines, 'XtestPopupSelect')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200773 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200774 call term_sendkeys(buf, ":call Select1()\<CR>")
775 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
776
777 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
778 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200779 " clean the command line, sometimes it still shows a command
780 call term_sendkeys(buf, ":\<esc>")
781
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200782 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
783
784 " clean up
785 call StopVimInTerminal(buf)
786 call delete('XtestPopupSelect')
787endfunc
788
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200789func Test_popup_in_tab()
790 " default popup is local to tab, not visible when in other tab
791 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200792 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200793 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200794 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200795 tabnew
796 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200797 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200798 quit
799 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200800
801 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200802 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200803 " buffer is gone now
804 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200805
806 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200807 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200808 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200809 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200810 tabnew
811 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200812 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200813 quit
814 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200815 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200816
817 " create popup in other tab
818 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200819 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200820 call assert_equal(0, popup_getpos(winid).visible)
821 call assert_equal(1, popup_getoptions(winid).tabpage)
822 quit
823 call assert_equal(1, popup_getpos(winid).visible)
824 call assert_equal(0, popup_getoptions(winid).tabpage)
825 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200826endfunc
827
828func Test_popup_valid_arguments()
829 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200830 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200831 let pos = popup_getpos(winid)
832 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200833 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200834
835 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200836 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200837 let pos = popup_getpos(winid)
838 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200839 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200840
841 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200842 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200843 let pos = popup_getpos(winid)
844 let around = (&columns - pos.width) / 2
845 call assert_inrange(around - 1, around + 1, pos.col)
846 let around = (&lines - pos.height) / 2
847 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200848 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200849endfunc
850
851func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200852 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200853 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200854 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200855 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200856
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200857 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200858 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200859 call assert_fails('call popup_create("text", #{col: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200860 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200861 call assert_fails('call popup_create("text", #{col: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200862 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200863 call assert_fails('call popup_create("text", #{col: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200864 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200865
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200866 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200867 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200868 call assert_fails('call popup_create("text", #{line: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200869 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200870 call assert_fails('call popup_create("text", #{line: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200871 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200872 call assert_fails('call popup_create("text", #{line: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200873 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200874
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200875 call assert_fails('call popup_create("text", #{pos: "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200876 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200877 call assert_fails('call popup_create("text", #{padding: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200878 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200879 call assert_fails('call popup_create("text", #{border: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200880 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200881 call assert_fails('call popup_create("text", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200882 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +0200883 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
884 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200885 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200886 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200887
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200888 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200889 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200890 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200891 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200892 call assert_fails('call popup_create([#{text: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200893 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +0100894 call assert_fails('call popup_create([#{text: "text", props: range(3)}], {})', 'E715:')
895 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200896 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
897 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +0100898 call assert_fails('call popup_create("text", #{mask: range(5)})', 'E475:')
899 call popup_clear()
900 call popup_create("text", #{mask: [range(4)]})
901 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200902 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200903 call assert_fails('call popup_create("text", #{mapping: []})', 'E745:')
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200904 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200905endfunc
906
Bram Moolenaareea16992019-05-31 17:34:48 +0200907func Test_win_execute_closing_curwin()
908 split
909 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200910 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200911 call popup_clear()
Bram Moolenaar49540192019-12-11 19:34:54 +0100912
913 let winid = popup_create('some text', {})
914 call assert_fails('call win_execute(winid, printf("normal! :\<C-u>call popup_close(%d)\<CR>", winid))', 'E994')
915 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200916endfunc
917
918func Test_win_execute_not_allowed()
919 let winid = popup_create('some text', {})
920 call assert_fails('call win_execute(winid, "split")', 'E994:')
921 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
922 call assert_fails('call win_execute(winid, "close")', 'E994:')
923 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200924 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200925 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
926 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
927 call assert_fails('call win_execute(winid, "next")', 'E994:')
928 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
929 call assert_fails('call win_execute(winid, "buf")', 'E994:')
Bram Moolenaar3029bcc2020-01-18 15:06:19 +0100930 call assert_fails('call win_execute(winid, "bnext")', 'E994:')
931 call assert_fails('call win_execute(winid, "bprev")', 'E994:')
932 call assert_fails('call win_execute(winid, "bfirst")', 'E994:')
933 call assert_fails('call win_execute(winid, "blast")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200934 call assert_fails('call win_execute(winid, "edit")', 'E994:')
935 call assert_fails('call win_execute(winid, "enew")', 'E994:')
936 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
937 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
938 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
939 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200940 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200941endfunc
942
Bram Moolenaar402502d2019-05-30 22:07:36 +0200943func Test_popup_with_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200944 CheckScreendump
945
Bram Moolenaar402502d2019-05-30 22:07:36 +0200946 let lines =<< trim END
947 call setline(1, range(1, 100))
948 let winid = popup_create(
949 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200950 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200951 END
952 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200953 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200954 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
955
956 " clean up
957 call StopVimInTerminal(buf)
958 call delete('XtestPopup')
959endfunc
960
961func Test_popup_without_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200962 CheckScreendump
963
Bram Moolenaar402502d2019-05-30 22:07:36 +0200964 let lines =<< trim END
965 call setline(1, range(1, 100))
966 let winid = popup_create(
967 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200968 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200969 END
970 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200971 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200972 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
973
974 " clean up
975 call StopVimInTerminal(buf)
976 call delete('XtestPopup')
977endfunc
978
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200979func Test_popup_with_showbreak()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200980 CheckScreendump
981
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200982 let lines =<< trim END
983 set showbreak=>>\
984 call setline(1, range(1, 20))
985 let winid = popup_dialog(
Bram Moolenaar8ae54372019-09-15 18:11:16 +0200986 \ 'a long line here that wraps',
987 \ #{filter: 'popup_filter_yesno',
988 \ maxwidth: 12})
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200989 END
990 call writefile(lines, 'XtestPopupShowbreak')
991 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
992 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
993
994 " clean up
995 call term_sendkeys(buf, "y")
996 call StopVimInTerminal(buf)
997 call delete('XtestPopupShowbreak')
998endfunc
999
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001000func Test_popup_time()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001001 CheckFeature timers
1002
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001003 topleft vnew
1004 call setline(1, 'hello')
1005
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001006 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001007 \ line: 1,
1008 \ col: 1,
1009 \ minwidth: 20,
1010 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001011 \})
1012 redraw
1013 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1014 call assert_equal('world', line)
1015
Bram Moolenaarb4f06282019-07-12 21:07:54 +02001016 call assert_equal(winid, popup_locate(1, 1))
1017 call assert_equal(winid, popup_locate(1, 20))
1018 call assert_equal(0, popup_locate(1, 21))
1019 call assert_equal(0, popup_locate(2, 1))
1020
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001021 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +02001022 redraw
Bram Moolenaar196b4662019-09-06 21:34:30 +02001023 let line = join(map(range(1, 5), '1->screenstring(v:val)'), '')
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001024 call assert_equal('hello', line)
1025
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001026 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001027 \ line: &lines,
1028 \ col: 10,
1029 \ minwidth: 20,
1030 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001031 \})
1032 redraw
1033 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1034 call assert_match('.*on the command line.*', line)
1035
1036 sleep 700m
1037 redraw
1038 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1039 call assert_notmatch('.*on the command line.*', line)
1040
1041 bwipe!
1042endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001043
1044func Test_popup_hide()
1045 topleft vnew
1046 call setline(1, 'hello')
1047
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001048 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001049 \ line: 1,
1050 \ col: 1,
1051 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001052 \})
1053 redraw
1054 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1055 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001056 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001057 " buffer is still listed and active
1058 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001059
1060 call popup_hide(winid)
1061 redraw
1062 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1063 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001064 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001065 " buffer is still listed but hidden
Bram Moolenaarca7c0782020-01-14 20:42:48 +01001066 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001067
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001068 eval winid->popup_show()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001069 redraw
1070 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1071 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001072 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001073
1074
1075 call popup_close(winid)
1076 redraw
1077 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1078 call assert_equal('hello', line)
1079
1080 " error is given for existing non-popup window
1081 call assert_fails('call popup_hide(win_getid())', 'E993:')
1082
1083 " no error non-existing window
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001084 eval 1234234->popup_hide()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001085 call popup_show(41234234)
1086
1087 bwipe!
1088endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001089
1090func Test_popup_move()
1091 topleft vnew
1092 call setline(1, 'hello')
1093
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001094 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001095 \ line: 1,
1096 \ col: 1,
1097 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001098 \})
1099 redraw
1100 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1101 call assert_equal('world ', line)
1102
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001103 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001104 redraw
1105 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1106 call assert_equal('hello ', line)
1107 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
1108 call assert_equal('~world', line)
1109
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001110 eval winid->popup_move(#{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001111 redraw
1112 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1113 call assert_equal('hworld', line)
1114
1115 call popup_close(winid)
1116
1117 bwipe!
1118endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +02001119
Bram Moolenaar402502d2019-05-30 22:07:36 +02001120func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001121 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001122 \ line: 2,
1123 \ col: 3,
1124 \ minwidth: 10,
1125 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +02001126 \})
1127 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001128 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001129 call assert_equal(2, res.line)
1130 call assert_equal(3, res.col)
1131 call assert_equal(10, res.width)
1132 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001133 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001134
1135 call popup_close(winid)
1136endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001137
1138func Test_popup_width_longest()
1139 let tests = [
1140 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
1141 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
1142 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
1143 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
1144 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
1145 \ ]
1146
1147 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001148 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001149 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001150 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001151 call assert_equal(test[1], position.width)
1152 call popup_close(winid)
1153 endfor
1154endfunc
1155
1156func Test_popup_wraps()
1157 let tests = [
1158 \ ['nowrap', 6, 1],
1159 \ ['a line that wraps once', 12, 2],
1160 \ ['a line that wraps two times', 12, 3],
1161 \ ]
1162 for test in tests
1163 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001164 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001165 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001166 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001167 call assert_equal(test[1], position.width)
1168 call assert_equal(test[2], position.height)
1169
1170 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001171 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001172 endfor
1173endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001174
1175func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001176 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001177 \ line: 2,
1178 \ col: 3,
1179 \ minwidth: 10,
1180 \ minheight: 11,
1181 \ maxwidth: 20,
1182 \ maxheight: 21,
1183 \ zindex: 100,
1184 \ time: 5000,
1185 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001186 \})
1187 redraw
1188 let res = popup_getoptions(winid)
1189 call assert_equal(2, res.line)
1190 call assert_equal(3, res.col)
1191 call assert_equal(10, res.minwidth)
1192 call assert_equal(11, res.minheight)
1193 call assert_equal(20, res.maxwidth)
1194 call assert_equal(21, res.maxheight)
1195 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001196 call assert_equal(1, res.fixed)
Bram Moolenaarb8350ab2019-08-04 17:59:49 +02001197 call assert_equal(1, res.mapping)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001198 if has('timers')
1199 call assert_equal(5000, res.time)
1200 endif
1201 call popup_close(winid)
1202
1203 let winid = popup_create('hello', {})
1204 redraw
1205 let res = popup_getoptions(winid)
1206 call assert_equal(0, res.line)
1207 call assert_equal(0, res.col)
1208 call assert_equal(0, res.minwidth)
1209 call assert_equal(0, res.minheight)
1210 call assert_equal(0, res.maxwidth)
1211 call assert_equal(0, res.maxheight)
1212 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001213 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001214 if has('timers')
1215 call assert_equal(0, res.time)
1216 endif
1217 call popup_close(winid)
1218 call assert_equal({}, popup_getoptions(winid))
1219endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001220
1221func Test_popup_option_values()
1222 new
1223 " window-local
1224 setlocal number
1225 setlocal nowrap
1226 " buffer-local
1227 setlocal omnifunc=Something
1228 " global/buffer-local
1229 setlocal path=/there
1230 " global/window-local
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001231 setlocal statusline=2
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001232
1233 let winid = popup_create('hello', {})
1234 call assert_equal(0, getwinvar(winid, '&number'))
1235 call assert_equal(1, getwinvar(winid, '&wrap'))
1236 call assert_equal('', getwinvar(winid, '&omnifunc'))
1237 call assert_equal(&g:path, getwinvar(winid, '&path'))
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001238 call assert_equal(&g:statusline, getwinvar(winid, '&statusline'))
1239
1240 " 'scrolloff' is reset to zero
1241 call assert_equal(5, &scrolloff)
1242 call assert_equal(0, getwinvar(winid, '&scrolloff'))
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001243
1244 call popup_close(winid)
1245 bwipe
1246endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001247
1248func Test_popup_atcursor()
1249 topleft vnew
1250 call setline(1, [
1251 \ 'xxxxxxxxxxxxxxxxx',
1252 \ 'xxxxxxxxxxxxxxxxx',
1253 \ 'xxxxxxxxxxxxxxxxx',
1254 \])
1255
1256 call cursor(2, 2)
1257 redraw
1258 let winid = popup_atcursor('vim', {})
1259 redraw
1260 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1261 call assert_equal('xvimxxxxxxxxxxxxx', line)
1262 call popup_close(winid)
1263
1264 call cursor(3, 4)
1265 redraw
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001266 let winid = 'vim'->popup_atcursor({})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001267 redraw
1268 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
1269 call assert_equal('xxxvimxxxxxxxxxxx', line)
1270 call popup_close(winid)
1271
1272 call cursor(1, 1)
1273 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001274 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001275 \ line: 'cursor+2',
1276 \ col: 'cursor+1',
1277 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001278 redraw
1279 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1280 call assert_equal('xvimxxxxxxxxxxxxx', line)
1281 call popup_close(winid)
1282
1283 call cursor(3, 3)
1284 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001285 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001286 \ line: 'cursor-2',
1287 \ col: 'cursor-1',
1288 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001289 redraw
1290 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1291 call assert_equal('xvimxxxxxxxxxxxxx', line)
1292 call popup_close(winid)
1293
Bram Moolenaar402502d2019-05-30 22:07:36 +02001294 " just enough room above
1295 call cursor(3, 3)
1296 redraw
1297 let winid = popup_atcursor(['vim', 'is great'], {})
1298 redraw
1299 let pos = popup_getpos(winid)
1300 call assert_equal(1, pos.line)
1301 call popup_close(winid)
1302
1303 " not enough room above, popup goes below the cursor
1304 call cursor(3, 3)
1305 redraw
1306 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1307 redraw
1308 let pos = popup_getpos(winid)
1309 call assert_equal(4, pos.line)
1310 call popup_close(winid)
1311
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001312 " cursor in first line, popup in line 2
1313 call cursor(1, 1)
1314 redraw
1315 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1316 redraw
1317 let pos = popup_getpos(winid)
1318 call assert_equal(2, pos.line)
1319 call popup_close(winid)
1320
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001321 bwipe!
1322endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001323
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001324func Test_popup_atcursor_pos()
1325 CheckScreendump
1326
1327 let lines =<< trim END
1328 call setline(1, repeat([repeat('-', 60)], 15))
1329 set so=0
1330
1331 normal 9G3|r#
1332 let winid1 = popup_atcursor(['first', 'second'], #{
1333 \ moved: [0, 0, 0],
1334 \ })
1335 normal 9G21|r&
1336 let winid1 = popup_atcursor(['FIrsT', 'SEcoND'], #{
1337 \ pos: 'botright',
1338 \ moved: [0, 0, 0],
1339 \ })
1340 normal 3G27|r%
1341 let winid1 = popup_atcursor(['fiRSt', 'seCOnd'], #{
1342 \ pos: 'topleft',
1343 \ moved: [0, 0, 0],
1344 \ })
1345 normal 3G45|r@
1346 let winid1 = popup_atcursor(['First', 'SeconD'], #{
1347 \ pos: 'topright',
Bram Moolenaarb0992022020-01-30 14:55:42 +01001348 \ moved: range(3),
1349 \ mousemoved: range(3),
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001350 \ })
1351 END
1352 call writefile(lines, 'XtestPopupAtcursorPos')
1353 let buf = RunVimInTerminal('-S XtestPopupAtcursorPos', #{rows: 12})
1354 call VerifyScreenDump(buf, 'Test_popupwin_atcursor_pos', {})
1355
1356 " clean up
1357 call StopVimInTerminal(buf)
1358 call delete('XtestPopupAtcursorPos')
1359endfunc
1360
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001361func Test_popup_beval()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001362 CheckScreendump
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001363 CheckFeature balloon_eval_term
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001364
1365 let lines =<< trim END
1366 call setline(1, range(1, 20))
1367 call setline(5, 'here is some text to hover over')
1368 set balloonevalterm
1369 set balloonexpr=BalloonExpr()
1370 set balloondelay=100
1371 func BalloonExpr()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001372 let s:winid = [v:beval_text]->popup_beval({})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001373 return ''
1374 endfunc
1375 func Hover()
1376 call test_setmouse(5, 15)
1377 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1378 sleep 100m
1379 endfunc
1380 func MoveOntoPopup()
1381 call test_setmouse(4, 17)
1382 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1383 endfunc
1384 func MoveAway()
1385 call test_setmouse(5, 13)
1386 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1387 endfunc
1388 END
1389 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001390 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001391 call term_wait(buf, 100)
1392 call term_sendkeys(buf, 'j')
1393 call term_sendkeys(buf, ":call Hover()\<CR>")
1394 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1395
1396 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1397 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1398
1399 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1400 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1401
1402 " clean up
1403 call StopVimInTerminal(buf)
1404 call delete('XtestPopupBeval')
1405endfunc
1406
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001407func Test_popup_filter()
1408 new
1409 call setline(1, 'some text')
1410
1411 func MyPopupFilter(winid, c)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001412 if a:c == 'e' || a:c == "\<F9>"
1413 let g:eaten = a:c
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001414 return 1
1415 endif
1416 if a:c == '0'
1417 let g:ignored = '0'
1418 return 0
1419 endif
1420 if a:c == 'x'
1421 call popup_close(a:winid)
1422 return 1
1423 endif
1424 return 0
1425 endfunc
1426
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001427 let winid = 'something'->popup_create(#{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001428 redraw
1429
1430 " e is consumed by the filter
1431 call feedkeys('e', 'xt')
1432 call assert_equal('e', g:eaten)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001433 call feedkeys("\<F9>", 'xt')
1434 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001435
1436 " 0 is ignored by the filter
1437 normal $
1438 call assert_equal(9, getcurpos()[2])
1439 call feedkeys('0', 'xt')
1440 call assert_equal('0', g:ignored)
1441 call assert_equal(1, getcurpos()[2])
1442
1443 " x closes the popup
1444 call feedkeys('x', 'xt')
Bram Moolenaarec084d32020-02-28 22:44:47 +01001445 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001446 call assert_equal(-1, winbufnr(winid))
1447
1448 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001449 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001450endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001451
Bram Moolenaara42d9452019-06-15 21:46:30 +02001452func ShowDialog(key, result)
1453 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001454 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001455 \ filter: 'popup_filter_yesno',
1456 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001457 \ })
1458 redraw
1459 call feedkeys(a:key, "xt")
1460 call assert_equal(winid, s:cb_winid)
1461 call assert_equal(a:result, s:cb_res)
1462endfunc
1463
1464func Test_popup_dialog()
1465 func QuitCallback(id, res)
1466 let s:cb_winid = a:id
1467 let s:cb_res = a:res
1468 endfunc
1469
1470 let winid = ShowDialog("y", 1)
1471 let winid = ShowDialog("Y", 1)
1472 let winid = ShowDialog("n", 0)
1473 let winid = ShowDialog("N", 0)
1474 let winid = ShowDialog("x", 0)
1475 let winid = ShowDialog("X", 0)
1476 let winid = ShowDialog("\<Esc>", 0)
1477 let winid = ShowDialog("\<C-C>", -1)
1478
1479 delfunc QuitCallback
1480endfunc
1481
Bram Moolenaara730e552019-06-16 19:05:31 +02001482func ShowMenu(key, result)
1483 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001484 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001485 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001486 \ })
1487 redraw
1488 call feedkeys(a:key, "xt")
1489 call assert_equal(winid, s:cb_winid)
1490 call assert_equal(a:result, s:cb_res)
1491endfunc
1492
1493func Test_popup_menu()
1494 func QuitCallback(id, res)
1495 let s:cb_winid = a:id
1496 let s:cb_res = a:res
1497 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001498 " mapping won't be used in popup
1499 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001500
1501 let winid = ShowMenu(" ", 1)
1502 let winid = ShowMenu("j \<CR>", 2)
1503 let winid = ShowMenu("JjK \<CR>", 2)
1504 let winid = ShowMenu("jjjjjj ", 3)
1505 let winid = ShowMenu("kkk ", 1)
1506 let winid = ShowMenu("x", -1)
1507 let winid = ShowMenu("X", -1)
1508 let winid = ShowMenu("\<Esc>", -1)
1509 let winid = ShowMenu("\<C-C>", -1)
1510
1511 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001512 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001513endfunc
1514
1515func Test_popup_menu_screenshot()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001516 CheckScreendump
Bram Moolenaara730e552019-06-16 19:05:31 +02001517
1518 let lines =<< trim END
1519 call setline(1, range(1, 20))
1520 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001521 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001522 func MenuDone(id, res)
1523 echomsg "selected " .. a:res
1524 endfunc
1525 END
1526 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001527 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001528 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1529
1530 call term_sendkeys(buf, "jj")
1531 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1532
1533 call term_sendkeys(buf, " ")
1534 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1535
1536 " clean up
1537 call StopVimInTerminal(buf)
1538 call delete('XtestPopupMenu')
1539endfunc
1540
Bram Moolenaarf914a332019-07-20 15:09:56 +02001541func Test_popup_menu_narrow()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001542 CheckScreendump
Bram Moolenaarf914a332019-07-20 15:09:56 +02001543
1544 let lines =<< trim END
1545 call setline(1, range(1, 20))
1546 hi PopupSelected ctermbg=green
1547 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1548 func MenuDone(id, res)
1549 echomsg "selected " .. a:res
1550 endfunc
1551 END
1552 call writefile(lines, 'XtestPopupNarrowMenu')
1553 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1554 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1555
1556 " clean up
1557 call term_sendkeys(buf, "x")
1558 call StopVimInTerminal(buf)
1559 call delete('XtestPopupNarrowMenu')
1560endfunc
1561
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001562func Test_popup_title()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001563 CheckScreendump
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001564
1565 " Create a popup without title or border, a line of padding will be added to
1566 " put the title on.
1567 let lines =<< trim END
1568 call setline(1, range(1, 20))
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001569 let winid = popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001570 END
1571 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001572 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001573 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1574
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001575 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxwidth: 20, title: 'a very long title that is not going to fit'})\<CR>")
1576 call term_sendkeys(buf, ":\<CR>")
1577 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_1', {})
1578
1579 call term_sendkeys(buf, ":call popup_setoptions(winid, #{border: []})\<CR>")
1580 call term_sendkeys(buf, ":\<CR>")
1581 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_2', {})
1582
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001583 " clean up
1584 call StopVimInTerminal(buf)
1585 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001586
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001587 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001588 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001589 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001590 call assert_equal('Another Title', popup_getoptions(winid).title)
1591
1592 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001593endfunc
1594
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001595func Test_popup_close_callback()
1596 func PopupDone(id, result)
1597 let g:result = a:result
1598 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001599 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001600 redraw
1601 call popup_close(winid, 'done')
1602 call assert_equal('done', g:result)
1603endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001604
1605func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001606 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001607 redraw
1608 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001609 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001610 call assert_equal(5, pos.height)
1611
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001612 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001613 redraw
1614 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001615 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001616 call assert_equal(3, pos.height)
1617endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001618
1619func Test_popup_never_behind()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001620 CheckScreendump
1621
Bram Moolenaar988c4332019-06-02 14:12:11 +02001622 " +-----------------------------+
1623 " | | |
1624 " | | |
1625 " | | |
1626 " | line1 |
1627 " |------------line2------------|
1628 " | line3 |
1629 " | line4 |
1630 " | |
1631 " | |
1632 " +-----------------------------+
1633 let lines =<< trim END
Bram Moolenaar988c4332019-06-02 14:12:11 +02001634 split
1635 vsplit
1636 let info_window1 = getwininfo()[0]
1637 let line = info_window1['height']
1638 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001639 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001640 \ line : line,
1641 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001642 \ })
1643 END
1644 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001645 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001646 call term_sendkeys(buf, "\<C-W>w")
1647 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1648
1649 " clean up
1650 call StopVimInTerminal(buf)
1651 call delete('XtestPopupBehind')
1652endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001653
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001654func s:VerifyPosition(p, msg, line, col, width, height)
1655 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1656 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1657 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1658 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001659endfunc
1660
1661func Test_popup_position_adjust()
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001662 " Anything placed past the last cell on the right of the screen is moved to
1663 " the left.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001664 "
1665 " When wrapping is disabled, we also shift to the left to display on the
1666 " screen, unless fixed is set.
1667
1668 " Entries for cases which don't vary based on wrapping.
1669 " Format is per tests described below
1670 let both_wrap_tests = [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001671 \ ['a', 5, &columns, 5, &columns, 1, 1],
1672 \ ['b', 5, &columns + 1, 5, &columns, 1, 1],
1673 \ ['c', 5, &columns - 1, 5, &columns - 1, 1, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001674 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001675 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1]]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001676
1677 " these test groups are dicts with:
1678 " - comment: something to identify the group of tests by
1679 " - options: dict of options to merge with the row/col in tests
1680 " - tests: list of cases. Each one is a list with elements:
1681 " - text
1682 " - row
1683 " - col
1684 " - expected row
1685 " - expected col
1686 " - expected width
1687 " - expected height
1688 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001689 \ #{
1690 \ comment: 'left-aligned with wrapping',
1691 \ options: #{
1692 \ wrap: 1,
1693 \ pos: 'botleft',
1694 \ },
1695 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001696 \ ['aa', 5, &columns, 4, &columns, 1, 2],
1697 \ ['bb', 5, &columns + 1, 4, &columns, 1, 2],
1698 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1699 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1700 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1701 \
1702 \ ['aaa', 5, &columns, 3, &columns, 1, 3],
1703 \ ['bbb', 5, &columns + 1, 3, &columns, 1, 3],
1704 \ ['ccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
1705 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1706 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1707 \
1708 \ ['aaaa', 5, &columns, 2, &columns, 1, 4],
1709 \ ['bbbb', 5, &columns + 1, 2, &columns, 1, 4],
1710 \ ['cccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001711 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
1712 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001713 \ ['eeee', 5, &columns - 4, 5, &columns - 4, 4, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001714 \ ],
1715 \ },
1716 \ #{
1717 \ comment: 'left aligned without wrapping',
1718 \ options: #{
1719 \ wrap: 0,
1720 \ pos: 'botleft',
1721 \ },
1722 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001723 \ ['aa', 5, &columns, 5, &columns - 1, 2, 1],
1724 \ ['bb', 5, &columns + 1, 5, &columns - 1, 2, 1],
1725 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1726 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1727 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1728 \
1729 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
1730 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1731 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1732 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1733 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1734 \
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001735 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
1736 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
1737 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
1738 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
1739 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1740 \ ],
1741 \ },
1742 \ #{
1743 \ comment: 'left aligned with fixed position',
1744 \ options: #{
1745 \ wrap: 0,
1746 \ fixed: 1,
1747 \ pos: 'botleft',
1748 \ },
1749 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001750 \ ['aa', 5, &columns, 5, &columns, 1, 1],
1751 \ ['bb', 5, &columns + 1, 5, &columns, 1, 1],
1752 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1753 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1754 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1755 \
1756 \ ['aaa', 5, &columns, 5, &columns, 1, 1],
1757 \ ['bbb', 5, &columns + 1, 5, &columns, 1, 1],
1758 \ ['ccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1759 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1760 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1761 \
1762 \ ['aaaa', 5, &columns, 5, &columns, 1, 1],
1763 \ ['bbbb', 5, &columns + 1, 5, &columns, 1, 1],
1764 \ ['cccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001765 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1766 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1767 \ ],
1768 \ },
1769 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001770
1771 for test_group in tests
1772 for test in test_group.tests
1773 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001774 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001775 \ line: line,
1776 \ col: col,
1777 \ }
1778 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001779
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001780 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001781
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001782 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001783 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
1784 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001785 endfor
1786 endfor
1787
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001788 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001789 %bwipe!
1790endfunc
1791
Bram Moolenaar3397f742019-06-02 18:40:06 +02001792func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001793 " width of screen
1794 let X = join(map(range(&columns), {->'X'}), '')
1795
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001796 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1797 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001798
1799 redraw
1800 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1801 call assert_equal(X, line)
1802
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001803 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001804 redraw
1805
1806 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001807 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
1808 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001809
1810 redraw
1811 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1812 call assert_equal(X, line)
1813
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001814 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001815 redraw
1816
1817 " Extend so > window width
1818 let X .= 'x'
1819
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001820 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1821 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001822
1823 redraw
1824 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1825 call assert_equal(X[ : -2 ], line)
1826
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001827 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001828 redraw
1829
1830 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001831 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
1832 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001833
1834 redraw
1835 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1836 call assert_equal(X[ : -2 ], line)
1837
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001838 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001839 redraw
1840
1841 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001842 let p = popup_create(X,
1843 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
1844 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001845
1846 redraw
1847 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1848 let e_line = ' ' . X[ 1 : -2 ]
1849 call assert_equal(e_line, line)
1850
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001851 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001852 redraw
1853
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001854 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001855 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001856endfunc
1857
1858func Test_popup_moved()
1859 new
1860 call test_override('char_avail', 1)
1861 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1862
1863 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001864 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001865 redraw
1866 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001867 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001868 " trigger the check for last_cursormoved by going into insert mode
1869 call feedkeys("li\<Esc>", 'xt')
1870 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001871 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001872
1873 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001874 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001875 redraw
1876 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001877 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001878 call feedkeys("hi\<Esc>", 'xt')
1879 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001880 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001881
1882 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001883 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001884 redraw
1885 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001886 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001887 call feedkeys("li\<Esc>", 'xt')
1888 call assert_equal(1, popup_getpos(winid).visible)
1889 call feedkeys("ei\<Esc>", 'xt')
1890 call assert_equal(1, popup_getpos(winid).visible)
1891 call feedkeys("eli\<Esc>", 'xt')
1892 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001893 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001894
Bram Moolenaar17627312019-06-02 19:53:44 +02001895 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001896 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001897 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001898 redraw
1899 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001900 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001901 call feedkeys("eli\<Esc>", 'xt')
1902 call assert_equal(1, popup_getpos(winid).visible)
1903 call feedkeys("wi\<Esc>", 'xt')
1904 call assert_equal(1, popup_getpos(winid).visible)
1905 call feedkeys("Eli\<Esc>", 'xt')
1906 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001907 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001908
1909 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001910 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001911 redraw
1912 call assert_equal(1, popup_getpos(winid).visible)
1913 call feedkeys("eli\<Esc>", 'xt')
1914 call feedkeys("ei\<Esc>", 'xt')
1915 call assert_equal(1, popup_getpos(winid).visible)
1916 call feedkeys("eli\<Esc>", 'xt')
1917 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001918 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001919
1920 bwipe!
1921 call test_override('ALL', 0)
1922endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001923
1924func Test_notifications()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001925 CheckFeature timers
1926 CheckScreendump
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001927
Bram Moolenaar0fdddee2019-09-01 15:26:23 +02001928 let lines =<< trim END
1929 call setline(1, range(1, 20))
1930 hi Notification ctermbg=lightblue
1931 call popup_notification('first notification', {})
1932 END
1933 call writefile(lines, 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001934 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001935 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1936
1937 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001938 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1939 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001940 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1941
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001942 " clean up
1943 call StopVimInTerminal(buf)
1944 call delete('XtestNotifications')
1945endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001946
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001947func Test_popup_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001948 CheckScreendump
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001949
1950 let lines =<< trim END
1951 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02001952 hi ScrollThumb ctermbg=blue
1953 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001954 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001955 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001956 \ minwidth: 8,
1957 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001958 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001959 func ScrollUp()
1960 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
1961 endfunc
1962 func ScrollDown()
1963 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
1964 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001965 func ClickTop()
1966 call feedkeys("\<F4>\<LeftMouse>", "xt")
1967 endfunc
1968 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001969 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001970 call feedkeys("\<F5>\<LeftMouse>", "xt")
1971 endfunc
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001972 func Popup_filter(winid, key)
1973 if a:key == 'j'
1974 let line = popup_getoptions(a:winid).firstline
1975 let nlines = line('$', a:winid)
1976 let newline = line < nlines ? (line + 1) : nlines
1977 call popup_setoptions(a:winid, #{firstline: newline})
1978 return v:true
1979 elseif a:key == 'x'
1980 call popup_close(a:winid)
1981 return v:true
1982 endif
1983 endfunc
1984
1985 func PopupScroll()
1986 call popup_clear()
1987 let text =<< trim END
1988 1
1989 2
1990 3
1991 4
1992 long line long line long line long line long line long line
1993 long line long line long line long line long line long line
1994 long line long line long line long line long line long line
1995 END
1996 call popup_create(text, #{
1997 \ minwidth: 30,
1998 \ maxwidth: 30,
1999 \ minheight: 4,
2000 \ maxheight: 4,
2001 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +01002002 \ lastline: 4,
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002003 \ wrap: v:true,
2004 \ scrollbar: v:true,
2005 \ mapping: v:false,
2006 \ filter: funcref('Popup_filter')
2007 \ })
2008 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002009 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002010 map <silent> <F4> :call test_setmouse(4, 42)<CR>
2011 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002012 END
2013 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002014 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002015 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
2016
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002017 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002018 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002019 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
2020
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002021 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002022 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002023 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
2024
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002025 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002026 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002027 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
2028
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02002029 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb', firstline: 5})\<CR>")
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002030 " this scrolls two lines (half the window height)
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002031 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
2032 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
2033
2034 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2035 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
2036
2037 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02002038 " wait a bit, otherwise it fails sometimes (double click recognized?)
2039 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002040 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2041 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
2042
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002043 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2044 sleep 100m
2045 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2046 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
2047
2048 call term_sendkeys(buf, ":call ClickBot()\<CR>")
2049 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
2050
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002051 " remove the minwidth and maxheight
2052 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxheight: 0, minwidth: 0})\<CR>")
Bram Moolenaar7e0f4622019-09-17 21:23:39 +02002053 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002054 call VerifyScreenDump(buf, 'Test_popupwin_scroll_10', {})
2055
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002056 " check size with non-wrapping lines
2057 call term_sendkeys(buf, ":call PopupScroll()\<CR>")
2058 call VerifyScreenDump(buf, 'Test_popupwin_scroll_11', {})
2059
2060 " check size with wrapping lines
2061 call term_sendkeys(buf, "j")
2062 call VerifyScreenDump(buf, 'Test_popupwin_scroll_12', {})
2063 call term_sendkeys(buf, "x")
2064
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002065 " clean up
2066 call StopVimInTerminal(buf)
2067 call delete('XtestPopupScroll')
2068endfunc
2069
Bram Moolenaar437a7462019-07-05 20:17:22 +02002070func Test_popup_fitting_scrollbar()
2071 " this was causing a crash, divide by zero
2072 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002073 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002074 \ scrollbar: 1,
2075 \ maxwidth: 10,
2076 \ maxheight: 5,
2077 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02002078 redraw
2079 call popup_clear()
2080endfunc
2081
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002082func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002083 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002084
2085 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002086 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002087 let p = popup_create('test', opts)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002088 eval p->popup_settext('this is a text')
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002089 END
2090
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002091 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002092 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002093 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
2094
2095 " Setting to empty string clears it
2096 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
2097 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
2098
2099 " Setting a list
2100 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2101 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2102
2103 " Shrinking with a list
2104 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
2105 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
2106
2107 " Growing with a list
2108 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2109 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2110
2111 " Empty list clears
2112 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
2113 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
2114
2115 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002116 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002117 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
2118
Bram Moolenaarb0992022020-01-30 14:55:42 +01002119 " range() (doesn't work)
2120 call term_sendkeys(buf, ":call popup_settext(p, range(4, 8))\<CR>")
2121 call VerifyScreenDump(buf, 'Test_popup_settext_07', {})
2122
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002123 " clean up
2124 call StopVimInTerminal(buf)
2125 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002126endfunc
2127
2128func Test_popup_hidden()
2129 new
2130
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002131 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002132 redraw
2133 call assert_equal(0, popup_getpos(winid).visible)
2134 call popup_close(winid)
2135
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002136 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002137 redraw
2138 call assert_equal(0, popup_getpos(winid).visible)
2139 call popup_close(winid)
2140
2141 func QuitCallback(id, res)
2142 let s:cb_winid = a:id
2143 let s:cb_res = a:res
2144 endfunc
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002145 let winid = 'make a choice'->popup_dialog(#{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002146 \ filter: 'popup_filter_yesno',
2147 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002148 \ })
2149 redraw
2150 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02002151 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
2152 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002153 exe "normal anot used by filter\<Esc>"
2154 call assert_equal('not used by filter', getline(1))
2155
2156 call popup_show(winid)
2157 call feedkeys('y', "xt")
2158 call assert_equal(1, s:cb_res)
2159
2160 bwipe!
2161 delfunc QuitCallback
2162endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02002163
2164" Test options not checked elsewhere
2165func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002166 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002167 let options = popup_getoptions(winid)
2168 call assert_equal(1, options.wrap)
2169 call assert_equal(0, options.drag)
2170 call assert_equal('Beautiful', options.highlight)
2171
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002172 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002173 let options = popup_getoptions(winid)
2174 call assert_equal(0, options.wrap)
2175 call assert_equal(1, options.drag)
2176 call assert_equal('Another', options.highlight)
2177
2178 call popup_close(winid)
2179endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002180
2181func Test_popupwin_garbage_collect()
2182 func MyPopupFilter(x, winid, c)
2183 " NOP
2184 endfunc
2185
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002186 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002187 call test_garbagecollect_now()
2188 redraw
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02002189 " Must not crash caused by invalid memory access
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002190 call feedkeys('j', 'xt')
2191 call assert_true(v:true)
2192
2193 call popup_close(winid)
2194 delfunc MyPopupFilter
2195endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002196
Bram Moolenaar581ba392019-09-03 22:08:33 +02002197func Test_popupwin_filter_mode()
2198 func MyPopupFilter(winid, c)
2199 let s:typed = a:c
2200 if a:c == ':' || a:c == "\r" || a:c == 'v'
2201 " can start cmdline mode, get out, and start/stop Visual mode
2202 return 0
2203 endif
2204 return 1
2205 endfunc
2206
2207 " Normal, Visual and Insert mode
2208 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'nvi'})
2209 redraw
2210 call feedkeys('x', 'xt')
2211 call assert_equal('x', s:typed)
2212
2213 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2214 call assert_equal(':', s:typed)
2215 call assert_equal('foo', g:foo)
2216
2217 let @x = 'something'
2218 call feedkeys('v$"xy', 'xt')
2219 call assert_equal('y', s:typed)
2220 call assert_equal('something', @x) " yank command is filtered out
2221 call feedkeys('v', 'xt') " end Visual mode
2222
2223 call popup_close(winid)
2224
2225 " only Normal mode
2226 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'n'})
2227 redraw
2228 call feedkeys('x', 'xt')
2229 call assert_equal('x', s:typed)
2230
2231 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2232 call assert_equal(':', s:typed)
2233 call assert_equal('foo', g:foo)
2234
2235 let @x = 'something'
2236 call feedkeys('v$"xy', 'xt')
2237 call assert_equal('v', s:typed)
2238 call assert_notequal('something', @x)
2239
2240 call popup_close(winid)
2241
2242 " default: all modes
2243 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
2244 redraw
2245 call feedkeys('x', 'xt')
2246 call assert_equal('x', s:typed)
2247
2248 let g:foo = 'bar'
2249 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2250 call assert_equal("\r", s:typed)
2251 call assert_equal('bar', g:foo)
2252
2253 let @x = 'something'
2254 call feedkeys('v$"xy', 'xt')
2255 call assert_equal('y', s:typed)
2256 call assert_equal('something', @x) " yank command is filtered out
2257 call feedkeys('v', 'xt') " end Visual mode
2258
2259 call popup_close(winid)
2260 delfunc MyPopupFilter
2261endfunc
2262
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002263func Test_popupwin_filter_mouse()
2264 func MyPopupFilter(winid, c)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002265 let g:got_mousepos = getmousepos()
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002266 return 0
2267 endfunc
2268
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002269 call setline(1, ['.'->repeat(25)]->repeat(10))
2270 let winid = popup_create(['short', 'long line that will wrap', 'other'], #{
2271 \ line: 2,
2272 \ col: 4,
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002273 \ maxwidth: 12,
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002274 \ padding: [],
2275 \ border: [],
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002276 \ filter: 'MyPopupFilter',
2277 \ })
2278 redraw
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002279 " 123456789012345678901
2280 " 1 .....................
2281 " 2 ...+--------------+..
2282 " 3 ...| |..
2283 " 4 ...| short |..
2284 " 5 ...| long line th |..
2285 " 6 ...| at will wrap |..
2286 " 7 ...| other |..
2287 " 8 ...| |..
2288 " 9 ...+--------------+..
2289 " 10 .....................
2290 let tests = []
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002291
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002292 func AddItemOutsidePopup(tests, row, col)
2293 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2294 \ screenrow: a:row, screencol: a:col,
2295 \ winid: win_getid(), winrow: a:row, wincol: a:col,
2296 \ line: a:row, column: a:col,
2297 \ }})
2298 endfunc
2299 func AddItemInPopupBorder(tests, winid, row, col)
2300 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2301 \ screenrow: a:row, screencol: a:col,
2302 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2303 \ line: 0, column: 0,
2304 \ }})
2305 endfunc
2306 func AddItemInPopupText(tests, winid, row, col, textline, textcol)
2307 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2308 \ screenrow: a:row, screencol: a:col,
2309 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2310 \ line: a:textline, column: a:textcol,
2311 \ }})
2312 endfunc
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002313
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002314 " above and below popup
2315 for c in range(1, 21)
2316 call AddItemOutsidePopup(tests, 1, c)
2317 call AddItemOutsidePopup(tests, 10, c)
2318 endfor
2319 " left and right of popup
2320 for r in range(1, 10)
2321 call AddItemOutsidePopup(tests, r, 3)
2322 call AddItemOutsidePopup(tests, r, 20)
2323 endfor
2324 " top and bottom in popup
2325 for c in range(4, 19)
2326 call AddItemInPopupBorder(tests, winid, 2, c)
2327 call AddItemInPopupBorder(tests, winid, 3, c)
2328 call AddItemInPopupBorder(tests, winid, 8, c)
2329 call AddItemInPopupBorder(tests, winid, 9, c)
2330 endfor
2331 " left and right margin in popup
2332 for r in range(2, 9)
2333 call AddItemInPopupBorder(tests, winid, r, 4)
2334 call AddItemInPopupBorder(tests, winid, r, 5)
2335 call AddItemInPopupBorder(tests, winid, r, 18)
2336 call AddItemInPopupBorder(tests, winid, r, 19)
2337 endfor
2338 " text "short"
2339 call AddItemInPopupText(tests, winid, 4, 6, 1, 1)
2340 call AddItemInPopupText(tests, winid, 4, 10, 1, 5)
2341 call AddItemInPopupText(tests, winid, 4, 11, 1, 6)
2342 call AddItemInPopupText(tests, winid, 4, 17, 1, 6)
2343 " text "long line th"
2344 call AddItemInPopupText(tests, winid, 5, 6, 2, 1)
2345 call AddItemInPopupText(tests, winid, 5, 10, 2, 5)
2346 call AddItemInPopupText(tests, winid, 5, 17, 2, 12)
2347 " text "at will wrap"
2348 call AddItemInPopupText(tests, winid, 6, 6, 2, 13)
2349 call AddItemInPopupText(tests, winid, 6, 10, 2, 17)
2350 call AddItemInPopupText(tests, winid, 6, 17, 2, 24)
2351 " text "other"
2352 call AddItemInPopupText(tests, winid, 7, 6, 3, 1)
2353 call AddItemInPopupText(tests, winid, 7, 10, 3, 5)
2354 call AddItemInPopupText(tests, winid, 7, 11, 3, 6)
2355 call AddItemInPopupText(tests, winid, 7, 17, 3, 6)
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002356
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002357 for item in tests
2358 call test_setmouse(item.clickrow, item.clickcol)
2359 call feedkeys("\<LeftMouse>", 'xt')
2360 call assert_equal(item.result, g:got_mousepos)
2361 endfor
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002362
2363 call popup_close(winid)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002364 enew!
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002365 delfunc MyPopupFilter
2366endfunc
2367
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002368func Test_popupwin_with_buffer()
2369 call writefile(['some text', 'in a buffer'], 'XsomeFile')
2370 let buf = bufadd('XsomeFile')
2371 call assert_equal(0, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002372
2373 setlocal number
2374 call setbufvar(buf, "&wrapmargin", 13)
2375
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002376 let winid = popup_create(buf, {})
2377 call assert_notequal(0, winid)
2378 let pos = popup_getpos(winid)
2379 call assert_equal(2, pos.height)
2380 call assert_equal(1, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002381
2382 " window-local option is set to default, buffer-local is not
2383 call assert_equal(0, getwinvar(winid, '&number'))
2384 call assert_equal(13, getbufvar(buf, '&wrapmargin'))
2385
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002386 call popup_close(winid)
2387 call assert_equal({}, popup_getpos(winid))
2388 call assert_equal(1, bufloaded(buf))
2389 exe 'bwipe! ' .. buf
Bram Moolenaar46451042019-08-24 15:50:46 +02002390 setlocal nonumber
Bram Moolenaar7866b872019-07-01 22:21:01 +02002391
2392 edit test_popupwin.vim
2393 let winid = popup_create(bufnr(''), {})
2394 redraw
2395 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002396 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002397endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02002398
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002399func Test_popupwin_terminal_buffer()
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002400 CheckFeature terminal
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002401 CheckUnix
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002402
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002403 let origwin = win_getid()
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002404 let ptybuf = term_start(&shell, #{hidden: 1})
Bram Moolenaar631ebc42020-02-03 22:15:26 +01002405 let winid = popup_create(ptybuf, #{minwidth: 40, minheight: 10})
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002406 " Wait for shell to start
2407 sleep 200m
Bram Moolenaar631ebc42020-02-03 22:15:26 +01002408 " Check this doesn't crash
2409 call assert_equal(winnr(), winnr('j'))
2410 call assert_equal(winnr(), winnr('k'))
2411 call assert_equal(winnr(), winnr('h'))
2412 call assert_equal(winnr(), winnr('l'))
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002413 " Cannot quit while job is running
2414 call assert_fails('call feedkeys("\<C-W>:quit\<CR>", "xt")', 'E948:')
2415 call feedkeys("exit\<CR>", 'xt')
2416 " Wait for shell to exit
2417 sleep 100m
2418 call feedkeys(":quit\<CR>", 'xt')
2419 call assert_equal(origwin, win_getid())
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002420endfunc
2421
Bram Moolenaar934470e2019-09-01 23:27:05 +02002422func Test_popupwin_with_buffer_and_filter()
2423 new Xwithfilter
2424 call setline(1, range(100))
2425 let bufnr = bufnr()
2426 hide
2427
2428 func BufferFilter(win, key)
2429 if a:key == 'G'
2430 " recursive use of "G" does not cause problems.
2431 call win_execute(a:win, 'normal! G')
2432 return 1
2433 endif
2434 return 0
2435 endfunc
2436
2437 let winid = popup_create(bufnr, #{maxheight: 5, filter: 'BufferFilter'})
2438 call assert_equal(1, popup_getpos(winid).firstline)
2439 redraw
2440 call feedkeys("G", 'xt')
2441 call assert_equal(99, popup_getpos(winid).firstline)
2442
2443 call popup_close(winid)
2444 exe 'bwipe! ' .. bufnr
2445endfunc
2446
Bram Moolenaare296e312019-07-03 23:20:18 +02002447func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002448 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002449 \ maxwidth: 40,
2450 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02002451 \ })
2452 for top in range(1, 20)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002453 eval winid->popup_setoptions(#{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02002454 redraw
2455 call assert_equal(19, popup_getpos(winid).width)
2456 endfor
2457 call popup_clear()
2458endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02002459
2460func Test_popupwin_buf_close()
2461 let buf = bufadd('Xtestbuf')
2462 call bufload(buf)
2463 call setbufline(buf, 1, ['just', 'some', 'lines'])
2464 let winid = popup_create(buf, {})
2465 redraw
2466 call assert_equal(3, popup_getpos(winid).height)
2467 let bufinfo = getbufinfo(buf)[0]
2468 call assert_equal(1, bufinfo.changed)
2469 call assert_equal(0, bufinfo.hidden)
2470 call assert_equal(0, bufinfo.listed)
2471 call assert_equal(1, bufinfo.loaded)
2472 call assert_equal([], bufinfo.windows)
2473 call assert_equal([winid], bufinfo.popups)
2474
2475 call popup_close(winid)
2476 call assert_equal({}, popup_getpos(winid))
2477 let bufinfo = getbufinfo(buf)[0]
2478 call assert_equal(1, bufinfo.changed)
2479 call assert_equal(1, bufinfo.hidden)
2480 call assert_equal(0, bufinfo.listed)
2481 call assert_equal(1, bufinfo.loaded)
2482 call assert_equal([], bufinfo.windows)
2483 call assert_equal([], bufinfo.popups)
2484 exe 'bwipe! ' .. buf
2485endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02002486
2487func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002488 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02002489
2490 let lines =<< trim END
2491 call setline(1, range(1, 10))
2492 hi ScrollThumb ctermbg=blue
2493 hi ScrollBar ctermbg=red
2494 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002495 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002496 \ maxwidth: 10,
2497 \ maxheight: 3,
2498 \ pos : 'topleft',
2499 \ col : a:col,
2500 \ line : a:line,
2501 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02002502 \ })
2503 endfunc
2504 call PopupMenu(['x'], 1, 1)
2505 call PopupMenu(['123456789|'], 1, 16)
2506 call PopupMenu(['123456789|' .. ' '], 7, 1)
2507 call PopupMenu([repeat('123456789|', 100)], 7, 16)
2508 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
2509 END
2510 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002511 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02002512 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
2513
2514 " close the menu popupwin.
2515 call term_sendkeys(buf, " ")
2516 call term_sendkeys(buf, " ")
2517 call term_sendkeys(buf, " ")
2518 call term_sendkeys(buf, " ")
2519 call term_sendkeys(buf, " ")
2520
2521 " clean up
2522 call StopVimInTerminal(buf)
2523 call delete('XtestPopupMenuMaxWidth')
2524endfunc
2525
Bram Moolenaara901a372019-07-13 16:38:50 +02002526func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002527 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02002528
2529 let lines =<< trim END
2530 call setline(1, range(1, 20))
2531 hi ScrollThumb ctermbg=blue
2532 hi ScrollBar ctermbg=red
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002533 eval ['one', 'two', 'three', 'four', 'five',
2534 \ 'six', 'seven', 'eight', 'nine']
2535 \ ->popup_menu(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002536 \ minwidth: 8,
2537 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02002538 \ })
2539 END
2540 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002541 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02002542
2543 call term_sendkeys(buf, "j")
2544 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
2545
2546 call term_sendkeys(buf, "jjj")
2547 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
2548
2549 " if the cursor is the bottom line, it stays at the bottom line.
2550 call term_sendkeys(buf, repeat("j", 20))
2551 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
2552
2553 call term_sendkeys(buf, "kk")
2554 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
2555
2556 call term_sendkeys(buf, "k")
2557 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
2558
2559 " if the cursor is in the top line, it stays in the top line.
2560 call term_sendkeys(buf, repeat("k", 20))
2561 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
2562
2563 " close the menu popupwin.
2564 call term_sendkeys(buf, " ")
2565
2566 " clean up
2567 call StopVimInTerminal(buf)
2568 call delete('XtestPopupMenuScroll')
2569endfunc
2570
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002571func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002572 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002573
2574 let lines =<< trim END
2575 function! MyFilter(winid, key) abort
2576 if a:key == "0"
2577 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
2578 return 1
2579 endif
2580 if a:key == "G"
2581 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
2582 return 1
2583 endif
2584 if a:key == "j"
2585 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
2586 return 1
2587 endif
2588 if a:key == "k"
2589 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
2590 return 1
2591 endif
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002592 if a:key == ':'
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002593 call popup_close(a:winid)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002594 return 0
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002595 endif
2596 return 0
2597 endfunction
2598 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
2599 \ maxheight : 3,
2600 \ filter : 'MyFilter'
2601 \ })
2602 END
2603 call writefile(lines, 'XtestPopupMenuFilter')
2604 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
2605
2606 call term_sendkeys(buf, "j")
2607 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
2608
2609 call term_sendkeys(buf, "k")
2610 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
2611
2612 call term_sendkeys(buf, "G")
2613 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
2614
2615 call term_sendkeys(buf, "0")
2616 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
2617
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002618 " check that when the popup is closed in the filter the screen is redrawn
2619 call term_sendkeys(buf, ":")
2620 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_5', {})
2621 call term_sendkeys(buf, "\<CR>")
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002622
2623 " clean up
2624 call StopVimInTerminal(buf)
2625 call delete('XtestPopupMenuFilter')
2626endfunc
2627
2628func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002629 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002630
2631 let winid = popup_create('some text', {})
2632 call assert_equal(0, popup_getoptions(winid).cursorline)
2633 call popup_close(winid)
2634
2635 let winid = popup_create('some text', #{ cursorline: 1, })
2636 call assert_equal(1, popup_getoptions(winid).cursorline)
2637 call popup_close(winid)
2638
2639 let winid = popup_create('some text', #{ cursorline: 0, })
2640 call assert_equal(0, popup_getoptions(winid).cursorline)
2641 call popup_close(winid)
2642
2643 let winid = popup_menu('some text', {})
2644 call assert_equal(1, popup_getoptions(winid).cursorline)
2645 call popup_close(winid)
2646
2647 let winid = popup_menu('some text', #{ cursorline: 1, })
2648 call assert_equal(1, popup_getoptions(winid).cursorline)
2649 call popup_close(winid)
2650
2651 let winid = popup_menu('some text', #{ cursorline: 0, })
2652 call assert_equal(0, popup_getoptions(winid).cursorline)
2653 call popup_close(winid)
2654
2655 " ---------
2656 " Pattern 1
2657 " ---------
2658 let lines =<< trim END
2659 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
2660 END
2661 call writefile(lines, 'XtestPopupCursorLine')
2662 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2663 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
2664 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2665 call StopVimInTerminal(buf)
2666
2667 " ---------
2668 " Pattern 2
2669 " ---------
2670 let lines =<< trim END
2671 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
2672 END
2673 call writefile(lines, 'XtestPopupCursorLine')
2674 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2675 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
2676 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2677 call StopVimInTerminal(buf)
2678
2679 " ---------
2680 " Pattern 3
2681 " ---------
2682 let lines =<< trim END
2683 function! MyFilter(winid, key) abort
2684 if a:key == "j"
2685 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2686 return 1
2687 endif
2688 if a:key == 'x'
2689 call popup_close(a:winid)
2690 return 1
2691 endif
2692 return 0
2693 endfunction
2694 call popup_menu(['111', '222', '333'], #{
2695 \ cursorline : 0,
2696 \ maxheight : 2,
2697 \ filter : 'MyFilter',
2698 \ })
2699 END
2700 call writefile(lines, 'XtestPopupCursorLine')
2701 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2702 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
2703 call term_sendkeys(buf, "j")
2704 call term_sendkeys(buf, "j")
2705 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
2706 call term_sendkeys(buf, "x")
2707 call StopVimInTerminal(buf)
2708
2709 " ---------
2710 " Pattern 4
2711 " ---------
2712 let lines =<< trim END
2713 function! MyFilter(winid, key) abort
2714 if a:key == "j"
2715 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2716 return 1
2717 endif
2718 if a:key == 'x'
2719 call popup_close(a:winid)
2720 return 1
2721 endif
2722 return 0
2723 endfunction
2724 call popup_menu(['111', '222', '333'], #{
2725 \ cursorline : 1,
2726 \ maxheight : 2,
2727 \ filter : 'MyFilter',
2728 \ })
2729 END
2730 call writefile(lines, 'XtestPopupCursorLine')
2731 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2732 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
2733 call term_sendkeys(buf, "j")
2734 call term_sendkeys(buf, "j")
2735 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
2736 call term_sendkeys(buf, "x")
2737 call StopVimInTerminal(buf)
2738
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02002739 " ---------
2740 " Cursor in second line when creating the popup
2741 " ---------
2742 let lines =<< trim END
2743 let winid = popup_create(['111', '222', '333'], #{
2744 \ cursorline : 1,
2745 \ })
2746 call win_execute(winid, "2")
2747 END
2748 call writefile(lines, 'XtestPopupCursorLine')
2749 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2750 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_7', {})
2751 call StopVimInTerminal(buf)
2752
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002753 call delete('XtestPopupCursorLine')
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01002754
2755 " ---------
2756 " Use current buffer for popupmenu
2757 " ---------
2758 let lines =<< trim END
2759 call setline(1, ['one', 'two', 'three'])
2760 let winid = popup_create(bufnr('%'), #{
2761 \ cursorline : 1,
2762 \ })
2763 call win_execute(winid, "2")
2764 END
2765 call writefile(lines, 'XtestPopupCursorLine')
2766 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2767 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_8', {})
2768 call StopVimInTerminal(buf)
2769
2770 call delete('XtestPopupCursorLine')
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002771endfunc
2772
Bram Moolenaarf914a332019-07-20 15:09:56 +02002773func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002774 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002775 CheckFeature quickfix
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002776
Bram Moolenaarf914a332019-07-20 15:09:56 +02002777 call writefile([
2778 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
2779 \ "another\tXtagfile\t/^this is another",
2780 \ "theword\tXtagfile\t/^theword"],
2781 \ 'Xtags')
2782 call writefile(range(1,20)
2783 \ + ['theword is here']
2784 \ + range(22, 27)
2785 \ + ['this is another place']
2786 \ + range(29, 40),
2787 \ "Xtagfile")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002788 call writefile(range(1,10)
2789 \ + ['searched word is here']
2790 \ + range(12, 20),
2791 \ "Xheader.h")
Bram Moolenaarf914a332019-07-20 15:09:56 +02002792 let lines =<< trim END
2793 set tags=Xtags
2794 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002795 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002796 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002797 \ 'three',
2798 \ 'four',
2799 \ 'five',
2800 \ 'six',
2801 \ 'seven',
2802 \ 'find theword somewhere',
2803 \ 'nine',
2804 \ 'this is another word',
2805 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02002806 set previewpopup=height:4,width:40
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002807 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02002808 END
2809 call writefile(lines, 'XtestPreviewPopup')
2810 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
2811
2812 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
2813 call term_sendkeys(buf, ":\<CR>")
2814 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
2815
2816 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2817 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
2818
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002819 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002820 call term_sendkeys(buf, ":\<CR>")
2821 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
2822
2823 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2824 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
2825
Bram Moolenaar799439a2020-02-11 21:44:17 +01002826 call term_sendkeys(buf, ":silent cd ..\<CR>:\<CR>")
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002827 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar799439a2020-02-11 21:44:17 +01002828 call term_sendkeys(buf, ":silent cd testdir\<CR>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002829
2830 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02002831 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002832 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
2833
2834 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
2835 call term_sendkeys(buf, ":\<CR>")
2836 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
2837
2838 call term_sendkeys(buf, ":pclose\<CR>")
2839 call term_sendkeys(buf, ":psearch searched\<CR>")
2840 call term_sendkeys(buf, ":\<CR>")
2841 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002842
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01002843 call term_sendkeys(buf, "\<C-W>p")
2844 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_9', {})
2845
2846 call term_sendkeys(buf, ":call win_execute(popup_findpreview(), 'call popup_clear()')\<CR>")
2847 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_10', {})
2848
Bram Moolenaarf914a332019-07-20 15:09:56 +02002849 call StopVimInTerminal(buf)
2850 call delete('Xtags')
2851 call delete('Xtagfile')
2852 call delete('XtestPreviewPopup')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002853 call delete('Xheader.h')
Bram Moolenaarf914a332019-07-20 15:09:56 +02002854endfunc
2855
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002856func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002857 let lines =<< trim END
2858 set completeopt+=preview,popup
2859 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002860 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002861
2862 func CompleteFuncDict(findstart, base)
2863 if a:findstart
2864 if col('.') > 10
2865 return col('.') - 10
2866 endif
2867 return 0
2868 endif
2869
2870 return {
2871 \ 'words': [
2872 \ {
2873 \ 'word': 'aword',
2874 \ 'abbr': 'wrd',
2875 \ 'menu': 'extra text',
2876 \ 'info': 'words are cool',
2877 \ 'kind': 'W',
2878 \ 'user_data': 'test'
2879 \ },
2880 \ {
2881 \ 'word': 'anotherword',
2882 \ 'abbr': 'anotwrd',
2883 \ 'menu': 'extra text',
2884 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
2885 \ 'kind': 'W',
2886 \ 'user_data': 'notest'
2887 \ },
2888 \ {
2889 \ 'word': 'noinfo',
2890 \ 'abbr': 'noawrd',
2891 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002892 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002893 \ 'kind': 'W',
2894 \ 'user_data': 'notest'
2895 \ },
2896 \ {
2897 \ 'word': 'thatword',
2898 \ 'abbr': 'thatwrd',
2899 \ 'menu': 'extra text',
2900 \ 'info': 'that word is cool',
2901 \ 'kind': 'W',
2902 \ 'user_data': 'notest'
2903 \ },
2904 \ ]
2905 \ }
2906 endfunc
2907 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002908 func ChangeColor()
2909 let id = popup_findinfo()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002910 eval id->popup_setoptions(#{highlight: 'InfoPopup'})
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002911 endfunc
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002912
2913 func InfoHidden()
2914 set completepopup=height:4,border:off,align:menu
2915 set completeopt-=popup completeopt+=popuphidden
2916 au CompleteChanged * call HandleChange()
2917 endfunc
2918
2919 let s:counter = 0
2920 func HandleChange()
2921 let s:counter += 1
2922 let selected = complete_info(['selected']).selected
2923 if selected <= 0
2924 " First time: do nothing, info remains hidden
2925 return
2926 endif
2927 if selected == 1
2928 " Second time: show info right away
2929 let id = popup_findinfo()
2930 if id
2931 call popup_settext(id, 'immediate info ' .. s:counter)
2932 call popup_show(id)
2933 endif
2934 else
2935 " Third time: show info after a short delay
2936 call timer_start(100, 'ShowInfo')
2937 endif
2938 endfunc
2939
2940 func ShowInfo(...)
2941 let id = popup_findinfo()
2942 if id
2943 call popup_settext(id, 'async info ' .. s:counter)
2944 call popup_show(id)
2945 endif
2946 endfunc
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002947 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002948 return lines
2949endfunc
2950
2951func Test_popupmenu_info_border()
2952 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002953 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002954
2955 let lines = Get_popupmenu_lines()
2956 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002957 call writefile(lines, 'XtestInfoPopup')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002958
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002959 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
2960 call term_wait(buf, 50)
2961
2962 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2963 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
2964
2965 call term_sendkeys(buf, "\<C-N>")
2966 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
2967
2968 call term_sendkeys(buf, "\<C-N>")
2969 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
2970
2971 call term_sendkeys(buf, "\<C-N>\<C-N>")
2972 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
2973
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02002974 " info on the left with scrollbar
2975 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
2976 call term_sendkeys(buf, "\<C-N>\<C-N>")
2977 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
2978
Bram Moolenaar202c3f72019-11-21 12:12:35 +01002979 " Test that the popupmenu's scrollbar and infopopup do not overlap
2980 call term_sendkeys(buf, "\<Esc>")
2981 call term_sendkeys(buf, ":set pumheight=3\<CR>")
2982 call term_sendkeys(buf, "cc\<C-X>\<C-U>")
2983 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_6', {})
2984
Bram Moolenaarca7c0782020-01-14 20:42:48 +01002985 " Hide the info popup, cycle trough buffers, make sure it didn't get
2986 " deleted.
2987 call term_sendkeys(buf, "\<Esc>")
2988 call term_sendkeys(buf, ":set hidden\<CR>")
2989 call term_sendkeys(buf, ":bn\<CR>")
2990 call term_sendkeys(buf, ":bn\<CR>")
2991 call term_sendkeys(buf, "otest text test text\<C-X>\<C-U>")
2992 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_7', {})
2993
2994 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002995 call StopVimInTerminal(buf)
2996 call delete('XtestInfoPopup')
2997endfunc
2998
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002999func Test_popupmenu_info_noborder()
3000 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003001 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003002
3003 let lines = Get_popupmenu_lines()
3004 call add(lines, 'set completepopup=height:4,border:off')
3005 call writefile(lines, 'XtestInfoPopupNb')
3006
3007 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
3008 call term_wait(buf, 50)
3009
3010 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3011 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
3012
3013 call StopVimInTerminal(buf)
3014 call delete('XtestInfoPopupNb')
3015endfunc
3016
Bram Moolenaar258cef52019-08-21 17:29:29 +02003017func Test_popupmenu_info_align_menu()
3018 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003019 CheckFeature quickfix
Bram Moolenaar258cef52019-08-21 17:29:29 +02003020
3021 let lines = Get_popupmenu_lines()
3022 call add(lines, 'set completepopup=height:4,border:off,align:menu')
3023 call writefile(lines, 'XtestInfoPopupNb')
3024
3025 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
3026 call term_wait(buf, 50)
3027
3028 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3029 call term_sendkeys(buf, "\<C-N>")
3030 call term_sendkeys(buf, "\<C-N>")
3031 call term_sendkeys(buf, "\<C-N>")
3032 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
3033
3034 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
3035 call term_sendkeys(buf, "\<C-N>")
3036 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
3037
3038 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003039 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02003040 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
3041 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
3042 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
3043
3044 call StopVimInTerminal(buf)
3045 call delete('XtestInfoPopupNb')
3046endfunc
3047
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003048func Test_popupmenu_info_hidden()
3049 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003050 CheckFeature quickfix
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003051
3052 let lines = Get_popupmenu_lines()
3053 call add(lines, 'call InfoHidden()')
3054 call writefile(lines, 'XtestInfoPopupHidden')
3055
3056 let buf = RunVimInTerminal('-S XtestInfoPopupHidden', #{rows: 14})
3057 call term_wait(buf, 50)
3058
3059 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3060 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_1', {})
3061
3062 call term_sendkeys(buf, "\<C-N>")
3063 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_2', {})
3064
3065 call term_sendkeys(buf, "\<C-N>")
3066 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_3', {})
3067
3068 call term_sendkeys(buf, "\<Esc>")
3069 call StopVimInTerminal(buf)
3070 call delete('XtestInfoPopupHidden')
3071endfunc
3072
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003073func Test_popupmenu_info_too_wide()
3074 CheckScreendump
3075 CheckFeature quickfix
3076
3077 let lines =<< trim END
3078 call setline(1, range(10))
3079
3080 set completeopt+=preview,popup
3081 set completepopup=align:menu
3082 set omnifunc=OmniFunc
3083 hi InfoPopup ctermbg=lightgrey
3084
3085 func OmniFunc(findstart, base)
3086 if a:findstart
3087 return 0
3088 endif
3089
3090 let menuText = 'some long text to make sure the menu takes up all of the width of the window'
3091 return #{
3092 \ words: [
3093 \ #{
3094 \ word: 'scrap',
3095 \ menu: menuText,
3096 \ info: "other words are\ncooler than this and some more text\nto make wrap",
3097 \ },
3098 \ #{
3099 \ word: 'scappier',
3100 \ menu: menuText,
3101 \ info: 'words are cool',
3102 \ },
3103 \ #{
3104 \ word: 'scrappier2',
3105 \ menu: menuText,
3106 \ info: 'words are cool',
3107 \ },
3108 \ ]
3109 \ }
3110 endfunc
3111 END
3112
3113 call writefile(lines, 'XtestInfoPopupWide')
3114 let buf = RunVimInTerminal('-S XtestInfoPopupWide', #{rows: 8})
3115 call term_wait(buf, 50)
3116
3117 call term_sendkeys(buf, "Ascr\<C-X>\<C-O>")
3118 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_wide_1', {})
3119
3120 call term_sendkeys(buf, "\<Esc>")
3121 call StopVimInTerminal(buf)
3122 call delete('XtestInfoPopupWide')
3123endfunc
3124
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003125func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02003126 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003127 call popup_clear()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003128 let winid = 'nothing wrong'->popup_notification({})
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003129 call assert_equal(bufnr, winbufnr(winid))
3130 call popup_clear()
3131endfunc
3132
Bram Moolenaar1824f452019-10-02 23:06:46 +02003133func Test_popupwin_getoptions_tablocal()
3134 topleft split
3135 let win1 = popup_create('nothing', #{maxheight: 8})
3136 let win2 = popup_create('something', #{maxheight: 10})
3137 let win3 = popup_create('something', #{maxheight: 15})
3138 call assert_equal(8, popup_getoptions(win1).maxheight)
3139 call assert_equal(10, popup_getoptions(win2).maxheight)
3140 call assert_equal(15, popup_getoptions(win3).maxheight)
3141 call popup_clear()
3142 quit
3143endfunc
3144
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02003145func Test_popupwin_cancel()
3146 let win1 = popup_create('one', #{line: 5, filter: {... -> 0}})
3147 let win2 = popup_create('two', #{line: 10, filter: {... -> 0}})
3148 let win3 = popup_create('three', #{line: 15, filter: {... -> 0}})
3149 call assert_equal(5, popup_getpos(win1).line)
3150 call assert_equal(10, popup_getpos(win2).line)
3151 call assert_equal(15, popup_getpos(win3).line)
3152 " TODO: this also works without patch 8.1.2110
3153 call feedkeys("\<C-C>", 'xt')
3154 call assert_equal(5, popup_getpos(win1).line)
3155 call assert_equal(10, popup_getpos(win2).line)
3156 call assert_equal({}, popup_getpos(win3))
3157 call feedkeys("\<C-C>", 'xt')
3158 call assert_equal(5, popup_getpos(win1).line)
3159 call assert_equal({}, popup_getpos(win2))
3160 call assert_equal({}, popup_getpos(win3))
3161 call feedkeys("\<C-C>", 'xt')
3162 call assert_equal({}, popup_getpos(win1))
3163 call assert_equal({}, popup_getpos(win2))
3164 call assert_equal({}, popup_getpos(win3))
3165endfunc
3166
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003167func Test_popupwin_filter_redraw()
3168 " Create two popups with a filter that closes the popup when typing "0".
3169 " Both popups should close, even though the redraw also calls
3170 " popup_reset_handled()
3171
3172 func CloseFilter(winid, key)
3173 if a:key == '0'
3174 call popup_close(a:winid)
3175 redraw
3176 endif
3177 return 0 " pass the key
3178 endfunc
3179
3180 let id1 = popup_create('first one', #{
3181 \ line: 1,
3182 \ col: 1,
3183 \ filter: 'CloseFilter',
3184 \ })
3185 let id2 = popup_create('second one', #{
3186 \ line: 9,
3187 \ col: 1,
3188 \ filter: 'CloseFilter',
3189 \ })
3190 call assert_equal(1, popup_getpos(id1).line)
3191 call assert_equal(9, popup_getpos(id2).line)
3192
3193 call feedkeys('0', 'xt')
3194 call assert_equal({}, popup_getpos(id1))
3195 call assert_equal({}, popup_getpos(id2))
3196
3197 call popup_clear()
3198 delfunc CloseFilter
3199endfunc
3200
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003201func Test_popupwin_double_width()
3202 CheckScreendump
3203
3204 let lines =<< trim END
3205 call setline(1, 'x你好世界你好世你好世界你好')
3206 call setline(2, '你好世界你好世你好世界你好')
3207 call setline(3, 'x你好世界你好世你好世界你好')
3208 call popup_create('你好,世界 - 你好,世界xxxxx', #{line: 1, col: 3, maxwidth: 14})
3209 END
3210 call writefile(lines, 'XtestPopupWide')
3211
3212 let buf = RunVimInTerminal('-S XtestPopupWide', #{rows: 10})
3213 call VerifyScreenDump(buf, 'Test_popupwin_doublewidth_1', {})
3214
3215 call StopVimInTerminal(buf)
3216 call delete('XtestPopupWide')
3217endfunc
3218
3219func Test_popupwin_sign()
3220 CheckScreendump
3221
3222 let lines =<< trim END
3223 call setline(1, range(10))
3224 call sign_define('Current', {
3225 \ 'text': '>>',
3226 \ 'texthl': 'WarningMsg',
3227 \ 'linehl': 'Error',
3228 \ })
3229 call sign_define('Other', {
3230 \ 'text': '#!',
3231 \ 'texthl': 'Error',
3232 \ 'linehl': 'Search',
3233 \ })
3234 let winid = popup_create(['hello', 'bright', 'world'], {
3235 \ 'minwidth': 20,
3236 \ })
3237 call setwinvar(winid, "&signcolumn", "yes")
3238 let winbufnr = winbufnr(winid)
3239
3240 " add sign to current buffer, shows
3241 call sign_place(1, 'Selected', 'Current', bufnr('%'), {'lnum': 1})
3242 " add sign to current buffer, does not show
3243 call sign_place(2, 'PopUpSelected', 'Other', bufnr('%'), {'lnum': 2})
3244
3245 " add sign to popup buffer, shows
3246 call sign_place(3, 'PopUpSelected', 'Other', winbufnr, {'lnum': 1})
3247 " add sign to popup buffer, does not show
3248 call sign_place(4, 'Selected', 'Current', winbufnr, {'lnum': 2})
3249 END
3250 call writefile(lines, 'XtestPopupSign')
3251
3252 let buf = RunVimInTerminal('-S XtestPopupSign', #{rows: 10})
3253 call VerifyScreenDump(buf, 'Test_popupwin_sign_1', {})
3254
3255 call StopVimInTerminal(buf)
3256 call delete('XtestPopupSign')
3257endfunc
3258
Bram Moolenaar99ebf222019-12-10 23:44:48 +01003259func Test_popupwin_bufnr()
3260 let popwin = popup_create(['blah'], #{})
3261 let popbuf = winbufnr(popwin)
3262 split asdfasdf
3263 let newbuf = bufnr()
3264 call assert_true(newbuf > popbuf, 'New buffer number is higher')
3265 call assert_equal(newbuf, bufnr('$'))
3266 call popup_clear()
3267 let popwin = popup_create(['blah'], #{})
3268 " reuses previous buffer number
3269 call assert_equal(popbuf, winbufnr(popwin))
3270 call assert_equal(newbuf, bufnr('$'))
3271
3272 call popup_clear()
3273 bwipe!
3274endfunc
3275
Bram Moolenaarec084d32020-02-28 22:44:47 +01003276func Test_popupwin_filter_input_multibyte()
3277 func MyPopupFilter(winid, c)
3278 let g:bytes = range(a:c->strlen())->map({i -> char2nr(a:c[i])})
3279 return 0
3280 endfunc
3281 let winid = popup_create('', #{mapping: 0, filter: 'MyPopupFilter'})
3282
3283 " UTF-8: E3 80 80, including K_SPECIAL(0x80)
3284 call feedkeys("\u3000", 'xt')
3285 call assert_equal([0xe3, 0x80, 0x80], g:bytes)
3286
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01003287 " UTF-8: E3 80 9B, including CSI(0x9B)
3288 call feedkeys("\u301b", 'xt')
3289 call assert_equal([0xe3, 0x80, 0x9b], g:bytes)
Bram Moolenaarec084d32020-02-28 22:44:47 +01003290
3291 call popup_clear()
3292 delfunc MyPopupFilter
3293 unlet g:bytes
3294endfunc
3295
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003296func Test_popupwin_atcursor_far_right()
3297 new
3298
3299 " this was getting stuck
3300 set signcolumn=yes
3301 call setline(1, repeat('=', &columns))
3302 normal! ggg$
3303 call popup_atcursor(repeat('x', 500), #{moved: 'any', border: []})
3304
3305 bwipe!
3306 set signcolumn&
3307endfunc
3308
Bram Moolenaar331bafd2019-07-20 17:46:05 +02003309" vim: shiftwidth=2 sts=2