blob: 75247b5334545cfcf17df384f8d9d7c5ffb19b3c [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)
1412 if a:c == 'e'
1413 let g:eaten = 'e'
1414 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)
1433
1434 " 0 is ignored by the filter
1435 normal $
1436 call assert_equal(9, getcurpos()[2])
1437 call feedkeys('0', 'xt')
1438 call assert_equal('0', g:ignored)
1439 call assert_equal(1, getcurpos()[2])
1440
1441 " x closes the popup
1442 call feedkeys('x', 'xt')
1443 call assert_equal('e', g:eaten)
1444 call assert_equal(-1, winbufnr(winid))
1445
1446 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001447 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001448endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001449
Bram Moolenaara42d9452019-06-15 21:46:30 +02001450func ShowDialog(key, result)
1451 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001452 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001453 \ filter: 'popup_filter_yesno',
1454 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001455 \ })
1456 redraw
1457 call feedkeys(a:key, "xt")
1458 call assert_equal(winid, s:cb_winid)
1459 call assert_equal(a:result, s:cb_res)
1460endfunc
1461
1462func Test_popup_dialog()
1463 func QuitCallback(id, res)
1464 let s:cb_winid = a:id
1465 let s:cb_res = a:res
1466 endfunc
1467
1468 let winid = ShowDialog("y", 1)
1469 let winid = ShowDialog("Y", 1)
1470 let winid = ShowDialog("n", 0)
1471 let winid = ShowDialog("N", 0)
1472 let winid = ShowDialog("x", 0)
1473 let winid = ShowDialog("X", 0)
1474 let winid = ShowDialog("\<Esc>", 0)
1475 let winid = ShowDialog("\<C-C>", -1)
1476
1477 delfunc QuitCallback
1478endfunc
1479
Bram Moolenaara730e552019-06-16 19:05:31 +02001480func ShowMenu(key, result)
1481 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001482 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001483 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001484 \ })
1485 redraw
1486 call feedkeys(a:key, "xt")
1487 call assert_equal(winid, s:cb_winid)
1488 call assert_equal(a:result, s:cb_res)
1489endfunc
1490
1491func Test_popup_menu()
1492 func QuitCallback(id, res)
1493 let s:cb_winid = a:id
1494 let s:cb_res = a:res
1495 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001496 " mapping won't be used in popup
1497 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001498
1499 let winid = ShowMenu(" ", 1)
1500 let winid = ShowMenu("j \<CR>", 2)
1501 let winid = ShowMenu("JjK \<CR>", 2)
1502 let winid = ShowMenu("jjjjjj ", 3)
1503 let winid = ShowMenu("kkk ", 1)
1504 let winid = ShowMenu("x", -1)
1505 let winid = ShowMenu("X", -1)
1506 let winid = ShowMenu("\<Esc>", -1)
1507 let winid = ShowMenu("\<C-C>", -1)
1508
1509 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001510 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001511endfunc
1512
1513func Test_popup_menu_screenshot()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001514 CheckScreendump
Bram Moolenaara730e552019-06-16 19:05:31 +02001515
1516 let lines =<< trim END
1517 call setline(1, range(1, 20))
1518 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001519 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001520 func MenuDone(id, res)
1521 echomsg "selected " .. a:res
1522 endfunc
1523 END
1524 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001525 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001526 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1527
1528 call term_sendkeys(buf, "jj")
1529 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1530
1531 call term_sendkeys(buf, " ")
1532 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1533
1534 " clean up
1535 call StopVimInTerminal(buf)
1536 call delete('XtestPopupMenu')
1537endfunc
1538
Bram Moolenaarf914a332019-07-20 15:09:56 +02001539func Test_popup_menu_narrow()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001540 CheckScreendump
Bram Moolenaarf914a332019-07-20 15:09:56 +02001541
1542 let lines =<< trim END
1543 call setline(1, range(1, 20))
1544 hi PopupSelected ctermbg=green
1545 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1546 func MenuDone(id, res)
1547 echomsg "selected " .. a:res
1548 endfunc
1549 END
1550 call writefile(lines, 'XtestPopupNarrowMenu')
1551 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1552 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1553
1554 " clean up
1555 call term_sendkeys(buf, "x")
1556 call StopVimInTerminal(buf)
1557 call delete('XtestPopupNarrowMenu')
1558endfunc
1559
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001560func Test_popup_title()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001561 CheckScreendump
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001562
1563 " Create a popup without title or border, a line of padding will be added to
1564 " put the title on.
1565 let lines =<< trim END
1566 call setline(1, range(1, 20))
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001567 let winid = popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001568 END
1569 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001570 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001571 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1572
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001573 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxwidth: 20, title: 'a very long title that is not going to fit'})\<CR>")
1574 call term_sendkeys(buf, ":\<CR>")
1575 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_1', {})
1576
1577 call term_sendkeys(buf, ":call popup_setoptions(winid, #{border: []})\<CR>")
1578 call term_sendkeys(buf, ":\<CR>")
1579 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_2', {})
1580
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001581 " clean up
1582 call StopVimInTerminal(buf)
1583 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001584
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001585 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001586 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001587 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001588 call assert_equal('Another Title', popup_getoptions(winid).title)
1589
1590 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001591endfunc
1592
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001593func Test_popup_close_callback()
1594 func PopupDone(id, result)
1595 let g:result = a:result
1596 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001597 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001598 redraw
1599 call popup_close(winid, 'done')
1600 call assert_equal('done', g:result)
1601endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001602
1603func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001604 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001605 redraw
1606 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001607 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001608 call assert_equal(5, pos.height)
1609
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001610 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001611 redraw
1612 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001613 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001614 call assert_equal(3, pos.height)
1615endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001616
1617func Test_popup_never_behind()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001618 CheckScreendump
1619
Bram Moolenaar988c4332019-06-02 14:12:11 +02001620 " +-----------------------------+
1621 " | | |
1622 " | | |
1623 " | | |
1624 " | line1 |
1625 " |------------line2------------|
1626 " | line3 |
1627 " | line4 |
1628 " | |
1629 " | |
1630 " +-----------------------------+
1631 let lines =<< trim END
Bram Moolenaar988c4332019-06-02 14:12:11 +02001632 split
1633 vsplit
1634 let info_window1 = getwininfo()[0]
1635 let line = info_window1['height']
1636 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001637 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001638 \ line : line,
1639 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001640 \ })
1641 END
1642 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001643 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001644 call term_sendkeys(buf, "\<C-W>w")
1645 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1646
1647 " clean up
1648 call StopVimInTerminal(buf)
1649 call delete('XtestPopupBehind')
1650endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001651
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001652func s:VerifyPosition(p, msg, line, col, width, height)
1653 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1654 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1655 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1656 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001657endfunc
1658
1659func Test_popup_position_adjust()
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001660 " Anything placed past the last cell on the right of the screen is moved to
1661 " the left.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001662 "
1663 " When wrapping is disabled, we also shift to the left to display on the
1664 " screen, unless fixed is set.
1665
1666 " Entries for cases which don't vary based on wrapping.
1667 " Format is per tests described below
1668 let both_wrap_tests = [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001669 \ ['a', 5, &columns, 5, &columns, 1, 1],
1670 \ ['b', 5, &columns + 1, 5, &columns, 1, 1],
1671 \ ['c', 5, &columns - 1, 5, &columns - 1, 1, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001672 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001673 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1]]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001674
1675 " these test groups are dicts with:
1676 " - comment: something to identify the group of tests by
1677 " - options: dict of options to merge with the row/col in tests
1678 " - tests: list of cases. Each one is a list with elements:
1679 " - text
1680 " - row
1681 " - col
1682 " - expected row
1683 " - expected col
1684 " - expected width
1685 " - expected height
1686 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001687 \ #{
1688 \ comment: 'left-aligned with wrapping',
1689 \ options: #{
1690 \ wrap: 1,
1691 \ pos: 'botleft',
1692 \ },
1693 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001694 \ ['aa', 5, &columns, 4, &columns, 1, 2],
1695 \ ['bb', 5, &columns + 1, 4, &columns, 1, 2],
1696 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1697 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1698 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1699 \
1700 \ ['aaa', 5, &columns, 3, &columns, 1, 3],
1701 \ ['bbb', 5, &columns + 1, 3, &columns, 1, 3],
1702 \ ['ccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
1703 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1704 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1705 \
1706 \ ['aaaa', 5, &columns, 2, &columns, 1, 4],
1707 \ ['bbbb', 5, &columns + 1, 2, &columns, 1, 4],
1708 \ ['cccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001709 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
1710 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001711 \ ['eeee', 5, &columns - 4, 5, &columns - 4, 4, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001712 \ ],
1713 \ },
1714 \ #{
1715 \ comment: 'left aligned without wrapping',
1716 \ options: #{
1717 \ wrap: 0,
1718 \ pos: 'botleft',
1719 \ },
1720 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001721 \ ['aa', 5, &columns, 5, &columns - 1, 2, 1],
1722 \ ['bb', 5, &columns + 1, 5, &columns - 1, 2, 1],
1723 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1724 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1725 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1726 \
1727 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
1728 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1729 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1730 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1731 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1732 \
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001733 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
1734 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
1735 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
1736 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
1737 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1738 \ ],
1739 \ },
1740 \ #{
1741 \ comment: 'left aligned with fixed position',
1742 \ options: #{
1743 \ wrap: 0,
1744 \ fixed: 1,
1745 \ pos: 'botleft',
1746 \ },
1747 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001748 \ ['aa', 5, &columns, 5, &columns, 1, 1],
1749 \ ['bb', 5, &columns + 1, 5, &columns, 1, 1],
1750 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1751 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1752 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1753 \
1754 \ ['aaa', 5, &columns, 5, &columns, 1, 1],
1755 \ ['bbb', 5, &columns + 1, 5, &columns, 1, 1],
1756 \ ['ccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1757 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1758 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1759 \
1760 \ ['aaaa', 5, &columns, 5, &columns, 1, 1],
1761 \ ['bbbb', 5, &columns + 1, 5, &columns, 1, 1],
1762 \ ['cccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001763 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1764 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1765 \ ],
1766 \ },
1767 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001768
1769 for test_group in tests
1770 for test in test_group.tests
1771 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001772 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001773 \ line: line,
1774 \ col: col,
1775 \ }
1776 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001777
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001778 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001779
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001780 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001781 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
1782 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001783 endfor
1784 endfor
1785
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001786 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001787 %bwipe!
1788endfunc
1789
Bram Moolenaar3397f742019-06-02 18:40:06 +02001790func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001791 " width of screen
1792 let X = join(map(range(&columns), {->'X'}), '')
1793
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001794 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1795 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001796
1797 redraw
1798 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1799 call assert_equal(X, line)
1800
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001801 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001802 redraw
1803
1804 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001805 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
1806 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001807
1808 redraw
1809 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1810 call assert_equal(X, line)
1811
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001812 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001813 redraw
1814
1815 " Extend so > window width
1816 let X .= 'x'
1817
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001818 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1819 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001820
1821 redraw
1822 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1823 call assert_equal(X[ : -2 ], line)
1824
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001825 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001826 redraw
1827
1828 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001829 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
1830 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001831
1832 redraw
1833 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1834 call assert_equal(X[ : -2 ], line)
1835
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001836 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001837 redraw
1838
1839 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001840 let p = popup_create(X,
1841 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
1842 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001843
1844 redraw
1845 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1846 let e_line = ' ' . X[ 1 : -2 ]
1847 call assert_equal(e_line, line)
1848
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001849 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001850 redraw
1851
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001852 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001853 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001854endfunc
1855
1856func Test_popup_moved()
1857 new
1858 call test_override('char_avail', 1)
1859 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1860
1861 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001862 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001863 redraw
1864 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001865 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001866 " trigger the check for last_cursormoved by going into insert mode
1867 call feedkeys("li\<Esc>", 'xt')
1868 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001869 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001870
1871 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001872 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001873 redraw
1874 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001875 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001876 call feedkeys("hi\<Esc>", 'xt')
1877 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001878 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001879
1880 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001881 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001882 redraw
1883 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001884 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001885 call feedkeys("li\<Esc>", 'xt')
1886 call assert_equal(1, popup_getpos(winid).visible)
1887 call feedkeys("ei\<Esc>", 'xt')
1888 call assert_equal(1, popup_getpos(winid).visible)
1889 call feedkeys("eli\<Esc>", 'xt')
1890 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001891 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001892
Bram Moolenaar17627312019-06-02 19:53:44 +02001893 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001894 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001895 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001896 redraw
1897 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001898 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001899 call feedkeys("eli\<Esc>", 'xt')
1900 call assert_equal(1, popup_getpos(winid).visible)
1901 call feedkeys("wi\<Esc>", 'xt')
1902 call assert_equal(1, popup_getpos(winid).visible)
1903 call feedkeys("Eli\<Esc>", 'xt')
1904 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001905 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001906
1907 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001908 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001909 redraw
1910 call assert_equal(1, popup_getpos(winid).visible)
1911 call feedkeys("eli\<Esc>", 'xt')
1912 call feedkeys("ei\<Esc>", 'xt')
1913 call assert_equal(1, popup_getpos(winid).visible)
1914 call feedkeys("eli\<Esc>", 'xt')
1915 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001916 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001917
1918 bwipe!
1919 call test_override('ALL', 0)
1920endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001921
1922func Test_notifications()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001923 CheckFeature timers
1924 CheckScreendump
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001925
Bram Moolenaar0fdddee2019-09-01 15:26:23 +02001926 let lines =<< trim END
1927 call setline(1, range(1, 20))
1928 hi Notification ctermbg=lightblue
1929 call popup_notification('first notification', {})
1930 END
1931 call writefile(lines, 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001932 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001933 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1934
1935 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001936 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1937 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001938 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1939
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001940 " clean up
1941 call StopVimInTerminal(buf)
1942 call delete('XtestNotifications')
1943endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001944
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001945func Test_popup_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001946 CheckScreendump
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001947
1948 let lines =<< trim END
1949 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02001950 hi ScrollThumb ctermbg=blue
1951 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001952 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001953 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001954 \ minwidth: 8,
1955 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001956 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001957 func ScrollUp()
1958 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
1959 endfunc
1960 func ScrollDown()
1961 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
1962 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001963 func ClickTop()
1964 call feedkeys("\<F4>\<LeftMouse>", "xt")
1965 endfunc
1966 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001967 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001968 call feedkeys("\<F5>\<LeftMouse>", "xt")
1969 endfunc
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001970 func Popup_filter(winid, key)
1971 if a:key == 'j'
1972 let line = popup_getoptions(a:winid).firstline
1973 let nlines = line('$', a:winid)
1974 let newline = line < nlines ? (line + 1) : nlines
1975 call popup_setoptions(a:winid, #{firstline: newline})
1976 return v:true
1977 elseif a:key == 'x'
1978 call popup_close(a:winid)
1979 return v:true
1980 endif
1981 endfunc
1982
1983 func PopupScroll()
1984 call popup_clear()
1985 let text =<< trim END
1986 1
1987 2
1988 3
1989 4
1990 long line long line long line long line long line long line
1991 long line long line long line long line long line long line
1992 long line long line long line long line long line long line
1993 END
1994 call popup_create(text, #{
1995 \ minwidth: 30,
1996 \ maxwidth: 30,
1997 \ minheight: 4,
1998 \ maxheight: 4,
1999 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +01002000 \ lastline: 4,
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002001 \ wrap: v:true,
2002 \ scrollbar: v:true,
2003 \ mapping: v:false,
2004 \ filter: funcref('Popup_filter')
2005 \ })
2006 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002007 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002008 map <silent> <F4> :call test_setmouse(4, 42)<CR>
2009 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002010 END
2011 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002012 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002013 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
2014
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002015 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002016 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002017 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
2018
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002019 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002020 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002021 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
2022
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002023 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002024 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002025 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
2026
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02002027 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb', firstline: 5})\<CR>")
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002028 " this scrolls two lines (half the window height)
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002029 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
2030 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
2031
2032 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2033 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
2034
2035 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02002036 " wait a bit, otherwise it fails sometimes (double click recognized?)
2037 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002038 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2039 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
2040
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002041 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2042 sleep 100m
2043 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2044 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
2045
2046 call term_sendkeys(buf, ":call ClickBot()\<CR>")
2047 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
2048
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002049 " remove the minwidth and maxheight
2050 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxheight: 0, minwidth: 0})\<CR>")
Bram Moolenaar7e0f4622019-09-17 21:23:39 +02002051 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002052 call VerifyScreenDump(buf, 'Test_popupwin_scroll_10', {})
2053
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002054 " check size with non-wrapping lines
2055 call term_sendkeys(buf, ":call PopupScroll()\<CR>")
2056 call VerifyScreenDump(buf, 'Test_popupwin_scroll_11', {})
2057
2058 " check size with wrapping lines
2059 call term_sendkeys(buf, "j")
2060 call VerifyScreenDump(buf, 'Test_popupwin_scroll_12', {})
2061 call term_sendkeys(buf, "x")
2062
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002063 " clean up
2064 call StopVimInTerminal(buf)
2065 call delete('XtestPopupScroll')
2066endfunc
2067
Bram Moolenaar437a7462019-07-05 20:17:22 +02002068func Test_popup_fitting_scrollbar()
2069 " this was causing a crash, divide by zero
2070 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002071 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002072 \ scrollbar: 1,
2073 \ maxwidth: 10,
2074 \ maxheight: 5,
2075 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02002076 redraw
2077 call popup_clear()
2078endfunc
2079
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002080func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002081 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002082
2083 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002084 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002085 let p = popup_create('test', opts)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002086 eval p->popup_settext('this is a text')
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002087 END
2088
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002089 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002090 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002091 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
2092
2093 " Setting to empty string clears it
2094 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
2095 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
2096
2097 " Setting a list
2098 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2099 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2100
2101 " Shrinking with a list
2102 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
2103 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
2104
2105 " Growing with a list
2106 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2107 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2108
2109 " Empty list clears
2110 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
2111 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
2112
2113 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002114 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002115 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
2116
Bram Moolenaarb0992022020-01-30 14:55:42 +01002117 " range() (doesn't work)
2118 call term_sendkeys(buf, ":call popup_settext(p, range(4, 8))\<CR>")
2119 call VerifyScreenDump(buf, 'Test_popup_settext_07', {})
2120
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002121 " clean up
2122 call StopVimInTerminal(buf)
2123 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002124endfunc
2125
2126func Test_popup_hidden()
2127 new
2128
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002129 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002130 redraw
2131 call assert_equal(0, popup_getpos(winid).visible)
2132 call popup_close(winid)
2133
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002134 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002135 redraw
2136 call assert_equal(0, popup_getpos(winid).visible)
2137 call popup_close(winid)
2138
2139 func QuitCallback(id, res)
2140 let s:cb_winid = a:id
2141 let s:cb_res = a:res
2142 endfunc
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002143 let winid = 'make a choice'->popup_dialog(#{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002144 \ filter: 'popup_filter_yesno',
2145 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002146 \ })
2147 redraw
2148 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02002149 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
2150 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002151 exe "normal anot used by filter\<Esc>"
2152 call assert_equal('not used by filter', getline(1))
2153
2154 call popup_show(winid)
2155 call feedkeys('y', "xt")
2156 call assert_equal(1, s:cb_res)
2157
2158 bwipe!
2159 delfunc QuitCallback
2160endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02002161
2162" Test options not checked elsewhere
2163func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002164 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002165 let options = popup_getoptions(winid)
2166 call assert_equal(1, options.wrap)
2167 call assert_equal(0, options.drag)
2168 call assert_equal('Beautiful', options.highlight)
2169
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002170 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002171 let options = popup_getoptions(winid)
2172 call assert_equal(0, options.wrap)
2173 call assert_equal(1, options.drag)
2174 call assert_equal('Another', options.highlight)
2175
2176 call popup_close(winid)
2177endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002178
2179func Test_popupwin_garbage_collect()
2180 func MyPopupFilter(x, winid, c)
2181 " NOP
2182 endfunc
2183
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002184 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002185 call test_garbagecollect_now()
2186 redraw
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02002187 " Must not crash caused by invalid memory access
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002188 call feedkeys('j', 'xt')
2189 call assert_true(v:true)
2190
2191 call popup_close(winid)
2192 delfunc MyPopupFilter
2193endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002194
Bram Moolenaar581ba392019-09-03 22:08:33 +02002195func Test_popupwin_filter_mode()
2196 func MyPopupFilter(winid, c)
2197 let s:typed = a:c
2198 if a:c == ':' || a:c == "\r" || a:c == 'v'
2199 " can start cmdline mode, get out, and start/stop Visual mode
2200 return 0
2201 endif
2202 return 1
2203 endfunc
2204
2205 " Normal, Visual and Insert mode
2206 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'nvi'})
2207 redraw
2208 call feedkeys('x', 'xt')
2209 call assert_equal('x', s:typed)
2210
2211 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2212 call assert_equal(':', s:typed)
2213 call assert_equal('foo', g:foo)
2214
2215 let @x = 'something'
2216 call feedkeys('v$"xy', 'xt')
2217 call assert_equal('y', s:typed)
2218 call assert_equal('something', @x) " yank command is filtered out
2219 call feedkeys('v', 'xt') " end Visual mode
2220
2221 call popup_close(winid)
2222
2223 " only Normal mode
2224 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'n'})
2225 redraw
2226 call feedkeys('x', 'xt')
2227 call assert_equal('x', s:typed)
2228
2229 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2230 call assert_equal(':', s:typed)
2231 call assert_equal('foo', g:foo)
2232
2233 let @x = 'something'
2234 call feedkeys('v$"xy', 'xt')
2235 call assert_equal('v', s:typed)
2236 call assert_notequal('something', @x)
2237
2238 call popup_close(winid)
2239
2240 " default: all modes
2241 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
2242 redraw
2243 call feedkeys('x', 'xt')
2244 call assert_equal('x', s:typed)
2245
2246 let g:foo = 'bar'
2247 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2248 call assert_equal("\r", s:typed)
2249 call assert_equal('bar', g:foo)
2250
2251 let @x = 'something'
2252 call feedkeys('v$"xy', 'xt')
2253 call assert_equal('y', s:typed)
2254 call assert_equal('something', @x) " yank command is filtered out
2255 call feedkeys('v', 'xt') " end Visual mode
2256
2257 call popup_close(winid)
2258 delfunc MyPopupFilter
2259endfunc
2260
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002261func Test_popupwin_filter_mouse()
2262 func MyPopupFilter(winid, c)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002263 let g:got_mousepos = getmousepos()
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002264 return 0
2265 endfunc
2266
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002267 call setline(1, ['.'->repeat(25)]->repeat(10))
2268 let winid = popup_create(['short', 'long line that will wrap', 'other'], #{
2269 \ line: 2,
2270 \ col: 4,
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002271 \ maxwidth: 12,
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002272 \ padding: [],
2273 \ border: [],
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002274 \ filter: 'MyPopupFilter',
2275 \ })
2276 redraw
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002277 " 123456789012345678901
2278 " 1 .....................
2279 " 2 ...+--------------+..
2280 " 3 ...| |..
2281 " 4 ...| short |..
2282 " 5 ...| long line th |..
2283 " 6 ...| at will wrap |..
2284 " 7 ...| other |..
2285 " 8 ...| |..
2286 " 9 ...+--------------+..
2287 " 10 .....................
2288 let tests = []
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002289
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002290 func AddItemOutsidePopup(tests, row, col)
2291 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2292 \ screenrow: a:row, screencol: a:col,
2293 \ winid: win_getid(), winrow: a:row, wincol: a:col,
2294 \ line: a:row, column: a:col,
2295 \ }})
2296 endfunc
2297 func AddItemInPopupBorder(tests, winid, row, col)
2298 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2299 \ screenrow: a:row, screencol: a:col,
2300 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2301 \ line: 0, column: 0,
2302 \ }})
2303 endfunc
2304 func AddItemInPopupText(tests, winid, row, col, textline, textcol)
2305 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2306 \ screenrow: a:row, screencol: a:col,
2307 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2308 \ line: a:textline, column: a:textcol,
2309 \ }})
2310 endfunc
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002311
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002312 " above and below popup
2313 for c in range(1, 21)
2314 call AddItemOutsidePopup(tests, 1, c)
2315 call AddItemOutsidePopup(tests, 10, c)
2316 endfor
2317 " left and right of popup
2318 for r in range(1, 10)
2319 call AddItemOutsidePopup(tests, r, 3)
2320 call AddItemOutsidePopup(tests, r, 20)
2321 endfor
2322 " top and bottom in popup
2323 for c in range(4, 19)
2324 call AddItemInPopupBorder(tests, winid, 2, c)
2325 call AddItemInPopupBorder(tests, winid, 3, c)
2326 call AddItemInPopupBorder(tests, winid, 8, c)
2327 call AddItemInPopupBorder(tests, winid, 9, c)
2328 endfor
2329 " left and right margin in popup
2330 for r in range(2, 9)
2331 call AddItemInPopupBorder(tests, winid, r, 4)
2332 call AddItemInPopupBorder(tests, winid, r, 5)
2333 call AddItemInPopupBorder(tests, winid, r, 18)
2334 call AddItemInPopupBorder(tests, winid, r, 19)
2335 endfor
2336 " text "short"
2337 call AddItemInPopupText(tests, winid, 4, 6, 1, 1)
2338 call AddItemInPopupText(tests, winid, 4, 10, 1, 5)
2339 call AddItemInPopupText(tests, winid, 4, 11, 1, 6)
2340 call AddItemInPopupText(tests, winid, 4, 17, 1, 6)
2341 " text "long line th"
2342 call AddItemInPopupText(tests, winid, 5, 6, 2, 1)
2343 call AddItemInPopupText(tests, winid, 5, 10, 2, 5)
2344 call AddItemInPopupText(tests, winid, 5, 17, 2, 12)
2345 " text "at will wrap"
2346 call AddItemInPopupText(tests, winid, 6, 6, 2, 13)
2347 call AddItemInPopupText(tests, winid, 6, 10, 2, 17)
2348 call AddItemInPopupText(tests, winid, 6, 17, 2, 24)
2349 " text "other"
2350 call AddItemInPopupText(tests, winid, 7, 6, 3, 1)
2351 call AddItemInPopupText(tests, winid, 7, 10, 3, 5)
2352 call AddItemInPopupText(tests, winid, 7, 11, 3, 6)
2353 call AddItemInPopupText(tests, winid, 7, 17, 3, 6)
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002354
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002355 for item in tests
2356 call test_setmouse(item.clickrow, item.clickcol)
2357 call feedkeys("\<LeftMouse>", 'xt')
2358 call assert_equal(item.result, g:got_mousepos)
2359 endfor
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002360
2361 call popup_close(winid)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002362 enew!
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002363 delfunc MyPopupFilter
2364endfunc
2365
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002366func Test_popupwin_with_buffer()
2367 call writefile(['some text', 'in a buffer'], 'XsomeFile')
2368 let buf = bufadd('XsomeFile')
2369 call assert_equal(0, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002370
2371 setlocal number
2372 call setbufvar(buf, "&wrapmargin", 13)
2373
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002374 let winid = popup_create(buf, {})
2375 call assert_notequal(0, winid)
2376 let pos = popup_getpos(winid)
2377 call assert_equal(2, pos.height)
2378 call assert_equal(1, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002379
2380 " window-local option is set to default, buffer-local is not
2381 call assert_equal(0, getwinvar(winid, '&number'))
2382 call assert_equal(13, getbufvar(buf, '&wrapmargin'))
2383
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002384 call popup_close(winid)
2385 call assert_equal({}, popup_getpos(winid))
2386 call assert_equal(1, bufloaded(buf))
2387 exe 'bwipe! ' .. buf
Bram Moolenaar46451042019-08-24 15:50:46 +02002388 setlocal nonumber
Bram Moolenaar7866b872019-07-01 22:21:01 +02002389
2390 edit test_popupwin.vim
2391 let winid = popup_create(bufnr(''), {})
2392 redraw
2393 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002394 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002395endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02002396
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002397func Test_popupwin_terminal_buffer()
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002398 CheckFeature terminal
2399
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002400 let ptybuf = term_start(&shell, #{hidden: 1})
2401 call assert_fails('let winnr = popup_create(ptybuf, #{})', 'E278:')
2402 exe 'bwipe! ' .. ptybuf
2403endfunc
2404
Bram Moolenaar934470e2019-09-01 23:27:05 +02002405func Test_popupwin_with_buffer_and_filter()
2406 new Xwithfilter
2407 call setline(1, range(100))
2408 let bufnr = bufnr()
2409 hide
2410
2411 func BufferFilter(win, key)
2412 if a:key == 'G'
2413 " recursive use of "G" does not cause problems.
2414 call win_execute(a:win, 'normal! G')
2415 return 1
2416 endif
2417 return 0
2418 endfunc
2419
2420 let winid = popup_create(bufnr, #{maxheight: 5, filter: 'BufferFilter'})
2421 call assert_equal(1, popup_getpos(winid).firstline)
2422 redraw
2423 call feedkeys("G", 'xt')
2424 call assert_equal(99, popup_getpos(winid).firstline)
2425
2426 call popup_close(winid)
2427 exe 'bwipe! ' .. bufnr
2428endfunc
2429
Bram Moolenaare296e312019-07-03 23:20:18 +02002430func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002431 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002432 \ maxwidth: 40,
2433 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02002434 \ })
2435 for top in range(1, 20)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002436 eval winid->popup_setoptions(#{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02002437 redraw
2438 call assert_equal(19, popup_getpos(winid).width)
2439 endfor
2440 call popup_clear()
2441endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02002442
2443func Test_popupwin_buf_close()
2444 let buf = bufadd('Xtestbuf')
2445 call bufload(buf)
2446 call setbufline(buf, 1, ['just', 'some', 'lines'])
2447 let winid = popup_create(buf, {})
2448 redraw
2449 call assert_equal(3, popup_getpos(winid).height)
2450 let bufinfo = getbufinfo(buf)[0]
2451 call assert_equal(1, bufinfo.changed)
2452 call assert_equal(0, bufinfo.hidden)
2453 call assert_equal(0, bufinfo.listed)
2454 call assert_equal(1, bufinfo.loaded)
2455 call assert_equal([], bufinfo.windows)
2456 call assert_equal([winid], bufinfo.popups)
2457
2458 call popup_close(winid)
2459 call assert_equal({}, popup_getpos(winid))
2460 let bufinfo = getbufinfo(buf)[0]
2461 call assert_equal(1, bufinfo.changed)
2462 call assert_equal(1, bufinfo.hidden)
2463 call assert_equal(0, bufinfo.listed)
2464 call assert_equal(1, bufinfo.loaded)
2465 call assert_equal([], bufinfo.windows)
2466 call assert_equal([], bufinfo.popups)
2467 exe 'bwipe! ' .. buf
2468endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02002469
2470func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002471 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02002472
2473 let lines =<< trim END
2474 call setline(1, range(1, 10))
2475 hi ScrollThumb ctermbg=blue
2476 hi ScrollBar ctermbg=red
2477 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002478 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002479 \ maxwidth: 10,
2480 \ maxheight: 3,
2481 \ pos : 'topleft',
2482 \ col : a:col,
2483 \ line : a:line,
2484 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02002485 \ })
2486 endfunc
2487 call PopupMenu(['x'], 1, 1)
2488 call PopupMenu(['123456789|'], 1, 16)
2489 call PopupMenu(['123456789|' .. ' '], 7, 1)
2490 call PopupMenu([repeat('123456789|', 100)], 7, 16)
2491 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
2492 END
2493 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002494 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02002495 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
2496
2497 " close the menu popupwin.
2498 call term_sendkeys(buf, " ")
2499 call term_sendkeys(buf, " ")
2500 call term_sendkeys(buf, " ")
2501 call term_sendkeys(buf, " ")
2502 call term_sendkeys(buf, " ")
2503
2504 " clean up
2505 call StopVimInTerminal(buf)
2506 call delete('XtestPopupMenuMaxWidth')
2507endfunc
2508
Bram Moolenaara901a372019-07-13 16:38:50 +02002509func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002510 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02002511
2512 let lines =<< trim END
2513 call setline(1, range(1, 20))
2514 hi ScrollThumb ctermbg=blue
2515 hi ScrollBar ctermbg=red
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002516 eval ['one', 'two', 'three', 'four', 'five',
2517 \ 'six', 'seven', 'eight', 'nine']
2518 \ ->popup_menu(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002519 \ minwidth: 8,
2520 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02002521 \ })
2522 END
2523 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002524 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02002525
2526 call term_sendkeys(buf, "j")
2527 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
2528
2529 call term_sendkeys(buf, "jjj")
2530 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
2531
2532 " if the cursor is the bottom line, it stays at the bottom line.
2533 call term_sendkeys(buf, repeat("j", 20))
2534 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
2535
2536 call term_sendkeys(buf, "kk")
2537 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
2538
2539 call term_sendkeys(buf, "k")
2540 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
2541
2542 " if the cursor is in the top line, it stays in the top line.
2543 call term_sendkeys(buf, repeat("k", 20))
2544 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
2545
2546 " close the menu popupwin.
2547 call term_sendkeys(buf, " ")
2548
2549 " clean up
2550 call StopVimInTerminal(buf)
2551 call delete('XtestPopupMenuScroll')
2552endfunc
2553
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002554func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002555 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002556
2557 let lines =<< trim END
2558 function! MyFilter(winid, key) abort
2559 if a:key == "0"
2560 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
2561 return 1
2562 endif
2563 if a:key == "G"
2564 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
2565 return 1
2566 endif
2567 if a:key == "j"
2568 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
2569 return 1
2570 endif
2571 if a:key == "k"
2572 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
2573 return 1
2574 endif
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002575 if a:key == ':'
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002576 call popup_close(a:winid)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002577 return 0
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002578 endif
2579 return 0
2580 endfunction
2581 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
2582 \ maxheight : 3,
2583 \ filter : 'MyFilter'
2584 \ })
2585 END
2586 call writefile(lines, 'XtestPopupMenuFilter')
2587 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
2588
2589 call term_sendkeys(buf, "j")
2590 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
2591
2592 call term_sendkeys(buf, "k")
2593 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
2594
2595 call term_sendkeys(buf, "G")
2596 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
2597
2598 call term_sendkeys(buf, "0")
2599 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
2600
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002601 " check that when the popup is closed in the filter the screen is redrawn
2602 call term_sendkeys(buf, ":")
2603 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_5', {})
2604 call term_sendkeys(buf, "\<CR>")
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002605
2606 " clean up
2607 call StopVimInTerminal(buf)
2608 call delete('XtestPopupMenuFilter')
2609endfunc
2610
2611func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002612 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002613
2614 let winid = popup_create('some text', {})
2615 call assert_equal(0, popup_getoptions(winid).cursorline)
2616 call popup_close(winid)
2617
2618 let winid = popup_create('some text', #{ cursorline: 1, })
2619 call assert_equal(1, popup_getoptions(winid).cursorline)
2620 call popup_close(winid)
2621
2622 let winid = popup_create('some text', #{ cursorline: 0, })
2623 call assert_equal(0, popup_getoptions(winid).cursorline)
2624 call popup_close(winid)
2625
2626 let winid = popup_menu('some text', {})
2627 call assert_equal(1, popup_getoptions(winid).cursorline)
2628 call popup_close(winid)
2629
2630 let winid = popup_menu('some text', #{ cursorline: 1, })
2631 call assert_equal(1, popup_getoptions(winid).cursorline)
2632 call popup_close(winid)
2633
2634 let winid = popup_menu('some text', #{ cursorline: 0, })
2635 call assert_equal(0, popup_getoptions(winid).cursorline)
2636 call popup_close(winid)
2637
2638 " ---------
2639 " Pattern 1
2640 " ---------
2641 let lines =<< trim END
2642 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
2643 END
2644 call writefile(lines, 'XtestPopupCursorLine')
2645 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2646 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
2647 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2648 call StopVimInTerminal(buf)
2649
2650 " ---------
2651 " Pattern 2
2652 " ---------
2653 let lines =<< trim END
2654 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
2655 END
2656 call writefile(lines, 'XtestPopupCursorLine')
2657 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2658 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
2659 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2660 call StopVimInTerminal(buf)
2661
2662 " ---------
2663 " Pattern 3
2664 " ---------
2665 let lines =<< trim END
2666 function! MyFilter(winid, key) abort
2667 if a:key == "j"
2668 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2669 return 1
2670 endif
2671 if a:key == 'x'
2672 call popup_close(a:winid)
2673 return 1
2674 endif
2675 return 0
2676 endfunction
2677 call popup_menu(['111', '222', '333'], #{
2678 \ cursorline : 0,
2679 \ maxheight : 2,
2680 \ filter : 'MyFilter',
2681 \ })
2682 END
2683 call writefile(lines, 'XtestPopupCursorLine')
2684 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2685 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
2686 call term_sendkeys(buf, "j")
2687 call term_sendkeys(buf, "j")
2688 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
2689 call term_sendkeys(buf, "x")
2690 call StopVimInTerminal(buf)
2691
2692 " ---------
2693 " Pattern 4
2694 " ---------
2695 let lines =<< trim END
2696 function! MyFilter(winid, key) abort
2697 if a:key == "j"
2698 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2699 return 1
2700 endif
2701 if a:key == 'x'
2702 call popup_close(a:winid)
2703 return 1
2704 endif
2705 return 0
2706 endfunction
2707 call popup_menu(['111', '222', '333'], #{
2708 \ cursorline : 1,
2709 \ maxheight : 2,
2710 \ filter : 'MyFilter',
2711 \ })
2712 END
2713 call writefile(lines, 'XtestPopupCursorLine')
2714 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2715 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
2716 call term_sendkeys(buf, "j")
2717 call term_sendkeys(buf, "j")
2718 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
2719 call term_sendkeys(buf, "x")
2720 call StopVimInTerminal(buf)
2721
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02002722 " ---------
2723 " Cursor in second line when creating the popup
2724 " ---------
2725 let lines =<< trim END
2726 let winid = popup_create(['111', '222', '333'], #{
2727 \ cursorline : 1,
2728 \ })
2729 call win_execute(winid, "2")
2730 END
2731 call writefile(lines, 'XtestPopupCursorLine')
2732 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2733 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_7', {})
2734 call StopVimInTerminal(buf)
2735
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002736 call delete('XtestPopupCursorLine')
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01002737
2738 " ---------
2739 " Use current buffer for popupmenu
2740 " ---------
2741 let lines =<< trim END
2742 call setline(1, ['one', 'two', 'three'])
2743 let winid = popup_create(bufnr('%'), #{
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_8', {})
2751 call StopVimInTerminal(buf)
2752
2753 call delete('XtestPopupCursorLine')
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002754endfunc
2755
Bram Moolenaarf914a332019-07-20 15:09:56 +02002756func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002757 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002758 CheckFeature quickfix
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002759
Bram Moolenaarf914a332019-07-20 15:09:56 +02002760 call writefile([
2761 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
2762 \ "another\tXtagfile\t/^this is another",
2763 \ "theword\tXtagfile\t/^theword"],
2764 \ 'Xtags')
2765 call writefile(range(1,20)
2766 \ + ['theword is here']
2767 \ + range(22, 27)
2768 \ + ['this is another place']
2769 \ + range(29, 40),
2770 \ "Xtagfile")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002771 call writefile(range(1,10)
2772 \ + ['searched word is here']
2773 \ + range(12, 20),
2774 \ "Xheader.h")
Bram Moolenaarf914a332019-07-20 15:09:56 +02002775 let lines =<< trim END
2776 set tags=Xtags
2777 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002778 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002779 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002780 \ 'three',
2781 \ 'four',
2782 \ 'five',
2783 \ 'six',
2784 \ 'seven',
2785 \ 'find theword somewhere',
2786 \ 'nine',
2787 \ 'this is another word',
2788 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02002789 set previewpopup=height:4,width:40
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002790 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02002791 END
2792 call writefile(lines, 'XtestPreviewPopup')
2793 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
2794
2795 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
2796 call term_sendkeys(buf, ":\<CR>")
2797 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
2798
2799 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2800 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
2801
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002802 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002803 call term_sendkeys(buf, ":\<CR>")
2804 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
2805
2806 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2807 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
2808
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002809 call term_sendkeys(buf, ":cd ..\<CR>:\<CR>")
2810 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002811 call term_sendkeys(buf, ":cd testdir\<CR>")
2812
2813 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02002814 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002815 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
2816
2817 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
2818 call term_sendkeys(buf, ":\<CR>")
2819 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
2820
2821 call term_sendkeys(buf, ":pclose\<CR>")
2822 call term_sendkeys(buf, ":psearch searched\<CR>")
2823 call term_sendkeys(buf, ":\<CR>")
2824 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002825
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01002826 call term_sendkeys(buf, "\<C-W>p")
2827 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_9', {})
2828
2829 call term_sendkeys(buf, ":call win_execute(popup_findpreview(), 'call popup_clear()')\<CR>")
2830 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_10', {})
2831
Bram Moolenaarf914a332019-07-20 15:09:56 +02002832 call StopVimInTerminal(buf)
2833 call delete('Xtags')
2834 call delete('Xtagfile')
2835 call delete('XtestPreviewPopup')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002836 call delete('Xheader.h')
Bram Moolenaarf914a332019-07-20 15:09:56 +02002837endfunc
2838
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002839func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002840 let lines =<< trim END
2841 set completeopt+=preview,popup
2842 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002843 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002844
2845 func CompleteFuncDict(findstart, base)
2846 if a:findstart
2847 if col('.') > 10
2848 return col('.') - 10
2849 endif
2850 return 0
2851 endif
2852
2853 return {
2854 \ 'words': [
2855 \ {
2856 \ 'word': 'aword',
2857 \ 'abbr': 'wrd',
2858 \ 'menu': 'extra text',
2859 \ 'info': 'words are cool',
2860 \ 'kind': 'W',
2861 \ 'user_data': 'test'
2862 \ },
2863 \ {
2864 \ 'word': 'anotherword',
2865 \ 'abbr': 'anotwrd',
2866 \ 'menu': 'extra text',
2867 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
2868 \ 'kind': 'W',
2869 \ 'user_data': 'notest'
2870 \ },
2871 \ {
2872 \ 'word': 'noinfo',
2873 \ 'abbr': 'noawrd',
2874 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002875 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002876 \ 'kind': 'W',
2877 \ 'user_data': 'notest'
2878 \ },
2879 \ {
2880 \ 'word': 'thatword',
2881 \ 'abbr': 'thatwrd',
2882 \ 'menu': 'extra text',
2883 \ 'info': 'that word is cool',
2884 \ 'kind': 'W',
2885 \ 'user_data': 'notest'
2886 \ },
2887 \ ]
2888 \ }
2889 endfunc
2890 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002891 func ChangeColor()
2892 let id = popup_findinfo()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002893 eval id->popup_setoptions(#{highlight: 'InfoPopup'})
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002894 endfunc
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002895
2896 func InfoHidden()
2897 set completepopup=height:4,border:off,align:menu
2898 set completeopt-=popup completeopt+=popuphidden
2899 au CompleteChanged * call HandleChange()
2900 endfunc
2901
2902 let s:counter = 0
2903 func HandleChange()
2904 let s:counter += 1
2905 let selected = complete_info(['selected']).selected
2906 if selected <= 0
2907 " First time: do nothing, info remains hidden
2908 return
2909 endif
2910 if selected == 1
2911 " Second time: show info right away
2912 let id = popup_findinfo()
2913 if id
2914 call popup_settext(id, 'immediate info ' .. s:counter)
2915 call popup_show(id)
2916 endif
2917 else
2918 " Third time: show info after a short delay
2919 call timer_start(100, 'ShowInfo')
2920 endif
2921 endfunc
2922
2923 func ShowInfo(...)
2924 let id = popup_findinfo()
2925 if id
2926 call popup_settext(id, 'async info ' .. s:counter)
2927 call popup_show(id)
2928 endif
2929 endfunc
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002930 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002931 return lines
2932endfunc
2933
2934func Test_popupmenu_info_border()
2935 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002936 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002937
2938 let lines = Get_popupmenu_lines()
2939 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002940 call writefile(lines, 'XtestInfoPopup')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002941
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002942 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
2943 call term_wait(buf, 50)
2944
2945 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2946 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
2947
2948 call term_sendkeys(buf, "\<C-N>")
2949 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
2950
2951 call term_sendkeys(buf, "\<C-N>")
2952 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
2953
2954 call term_sendkeys(buf, "\<C-N>\<C-N>")
2955 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
2956
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02002957 " info on the left with scrollbar
2958 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
2959 call term_sendkeys(buf, "\<C-N>\<C-N>")
2960 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
2961
Bram Moolenaar202c3f72019-11-21 12:12:35 +01002962 " Test that the popupmenu's scrollbar and infopopup do not overlap
2963 call term_sendkeys(buf, "\<Esc>")
2964 call term_sendkeys(buf, ":set pumheight=3\<CR>")
2965 call term_sendkeys(buf, "cc\<C-X>\<C-U>")
2966 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_6', {})
2967
Bram Moolenaarca7c0782020-01-14 20:42:48 +01002968 " Hide the info popup, cycle trough buffers, make sure it didn't get
2969 " deleted.
2970 call term_sendkeys(buf, "\<Esc>")
2971 call term_sendkeys(buf, ":set hidden\<CR>")
2972 call term_sendkeys(buf, ":bn\<CR>")
2973 call term_sendkeys(buf, ":bn\<CR>")
2974 call term_sendkeys(buf, "otest text test text\<C-X>\<C-U>")
2975 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_7', {})
2976
2977 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002978 call StopVimInTerminal(buf)
2979 call delete('XtestInfoPopup')
2980endfunc
2981
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002982func Test_popupmenu_info_noborder()
2983 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002984 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002985
2986 let lines = Get_popupmenu_lines()
2987 call add(lines, 'set completepopup=height:4,border:off')
2988 call writefile(lines, 'XtestInfoPopupNb')
2989
2990 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
2991 call term_wait(buf, 50)
2992
2993 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2994 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
2995
2996 call StopVimInTerminal(buf)
2997 call delete('XtestInfoPopupNb')
2998endfunc
2999
Bram Moolenaar258cef52019-08-21 17:29:29 +02003000func Test_popupmenu_info_align_menu()
3001 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003002 CheckFeature quickfix
Bram Moolenaar258cef52019-08-21 17:29:29 +02003003
3004 let lines = Get_popupmenu_lines()
3005 call add(lines, 'set completepopup=height:4,border:off,align:menu')
3006 call writefile(lines, 'XtestInfoPopupNb')
3007
3008 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
3009 call term_wait(buf, 50)
3010
3011 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3012 call term_sendkeys(buf, "\<C-N>")
3013 call term_sendkeys(buf, "\<C-N>")
3014 call term_sendkeys(buf, "\<C-N>")
3015 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
3016
3017 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
3018 call term_sendkeys(buf, "\<C-N>")
3019 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
3020
3021 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003022 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02003023 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
3024 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
3025 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
3026
3027 call StopVimInTerminal(buf)
3028 call delete('XtestInfoPopupNb')
3029endfunc
3030
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003031func Test_popupmenu_info_hidden()
3032 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003033 CheckFeature quickfix
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003034
3035 let lines = Get_popupmenu_lines()
3036 call add(lines, 'call InfoHidden()')
3037 call writefile(lines, 'XtestInfoPopupHidden')
3038
3039 let buf = RunVimInTerminal('-S XtestInfoPopupHidden', #{rows: 14})
3040 call term_wait(buf, 50)
3041
3042 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3043 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_1', {})
3044
3045 call term_sendkeys(buf, "\<C-N>")
3046 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_2', {})
3047
3048 call term_sendkeys(buf, "\<C-N>")
3049 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_3', {})
3050
3051 call term_sendkeys(buf, "\<Esc>")
3052 call StopVimInTerminal(buf)
3053 call delete('XtestInfoPopupHidden')
3054endfunc
3055
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003056func Test_popupmenu_info_too_wide()
3057 CheckScreendump
3058 CheckFeature quickfix
3059
3060 let lines =<< trim END
3061 call setline(1, range(10))
3062
3063 set completeopt+=preview,popup
3064 set completepopup=align:menu
3065 set omnifunc=OmniFunc
3066 hi InfoPopup ctermbg=lightgrey
3067
3068 func OmniFunc(findstart, base)
3069 if a:findstart
3070 return 0
3071 endif
3072
3073 let menuText = 'some long text to make sure the menu takes up all of the width of the window'
3074 return #{
3075 \ words: [
3076 \ #{
3077 \ word: 'scrap',
3078 \ menu: menuText,
3079 \ info: "other words are\ncooler than this and some more text\nto make wrap",
3080 \ },
3081 \ #{
3082 \ word: 'scappier',
3083 \ menu: menuText,
3084 \ info: 'words are cool',
3085 \ },
3086 \ #{
3087 \ word: 'scrappier2',
3088 \ menu: menuText,
3089 \ info: 'words are cool',
3090 \ },
3091 \ ]
3092 \ }
3093 endfunc
3094 END
3095
3096 call writefile(lines, 'XtestInfoPopupWide')
3097 let buf = RunVimInTerminal('-S XtestInfoPopupWide', #{rows: 8})
3098 call term_wait(buf, 50)
3099
3100 call term_sendkeys(buf, "Ascr\<C-X>\<C-O>")
3101 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_wide_1', {})
3102
3103 call term_sendkeys(buf, "\<Esc>")
3104 call StopVimInTerminal(buf)
3105 call delete('XtestInfoPopupWide')
3106endfunc
3107
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003108func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02003109 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003110 call popup_clear()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003111 let winid = 'nothing wrong'->popup_notification({})
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003112 call assert_equal(bufnr, winbufnr(winid))
3113 call popup_clear()
3114endfunc
3115
Bram Moolenaar1824f452019-10-02 23:06:46 +02003116func Test_popupwin_getoptions_tablocal()
3117 topleft split
3118 let win1 = popup_create('nothing', #{maxheight: 8})
3119 let win2 = popup_create('something', #{maxheight: 10})
3120 let win3 = popup_create('something', #{maxheight: 15})
3121 call assert_equal(8, popup_getoptions(win1).maxheight)
3122 call assert_equal(10, popup_getoptions(win2).maxheight)
3123 call assert_equal(15, popup_getoptions(win3).maxheight)
3124 call popup_clear()
3125 quit
3126endfunc
3127
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02003128func Test_popupwin_cancel()
3129 let win1 = popup_create('one', #{line: 5, filter: {... -> 0}})
3130 let win2 = popup_create('two', #{line: 10, filter: {... -> 0}})
3131 let win3 = popup_create('three', #{line: 15, filter: {... -> 0}})
3132 call assert_equal(5, popup_getpos(win1).line)
3133 call assert_equal(10, popup_getpos(win2).line)
3134 call assert_equal(15, popup_getpos(win3).line)
3135 " TODO: this also works without patch 8.1.2110
3136 call feedkeys("\<C-C>", 'xt')
3137 call assert_equal(5, popup_getpos(win1).line)
3138 call assert_equal(10, popup_getpos(win2).line)
3139 call assert_equal({}, popup_getpos(win3))
3140 call feedkeys("\<C-C>", 'xt')
3141 call assert_equal(5, popup_getpos(win1).line)
3142 call assert_equal({}, popup_getpos(win2))
3143 call assert_equal({}, popup_getpos(win3))
3144 call feedkeys("\<C-C>", 'xt')
3145 call assert_equal({}, popup_getpos(win1))
3146 call assert_equal({}, popup_getpos(win2))
3147 call assert_equal({}, popup_getpos(win3))
3148endfunc
3149
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003150func Test_popupwin_filter_redraw()
3151 " Create two popups with a filter that closes the popup when typing "0".
3152 " Both popups should close, even though the redraw also calls
3153 " popup_reset_handled()
3154
3155 func CloseFilter(winid, key)
3156 if a:key == '0'
3157 call popup_close(a:winid)
3158 redraw
3159 endif
3160 return 0 " pass the key
3161 endfunc
3162
3163 let id1 = popup_create('first one', #{
3164 \ line: 1,
3165 \ col: 1,
3166 \ filter: 'CloseFilter',
3167 \ })
3168 let id2 = popup_create('second one', #{
3169 \ line: 9,
3170 \ col: 1,
3171 \ filter: 'CloseFilter',
3172 \ })
3173 call assert_equal(1, popup_getpos(id1).line)
3174 call assert_equal(9, popup_getpos(id2).line)
3175
3176 call feedkeys('0', 'xt')
3177 call assert_equal({}, popup_getpos(id1))
3178 call assert_equal({}, popup_getpos(id2))
3179
3180 call popup_clear()
3181 delfunc CloseFilter
3182endfunc
3183
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003184func Test_popupwin_double_width()
3185 CheckScreendump
3186
3187 let lines =<< trim END
3188 call setline(1, 'x你好世界你好世你好世界你好')
3189 call setline(2, '你好世界你好世你好世界你好')
3190 call setline(3, 'x你好世界你好世你好世界你好')
3191 call popup_create('你好,世界 - 你好,世界xxxxx', #{line: 1, col: 3, maxwidth: 14})
3192 END
3193 call writefile(lines, 'XtestPopupWide')
3194
3195 let buf = RunVimInTerminal('-S XtestPopupWide', #{rows: 10})
3196 call VerifyScreenDump(buf, 'Test_popupwin_doublewidth_1', {})
3197
3198 call StopVimInTerminal(buf)
3199 call delete('XtestPopupWide')
3200endfunc
3201
3202func Test_popupwin_sign()
3203 CheckScreendump
3204
3205 let lines =<< trim END
3206 call setline(1, range(10))
3207 call sign_define('Current', {
3208 \ 'text': '>>',
3209 \ 'texthl': 'WarningMsg',
3210 \ 'linehl': 'Error',
3211 \ })
3212 call sign_define('Other', {
3213 \ 'text': '#!',
3214 \ 'texthl': 'Error',
3215 \ 'linehl': 'Search',
3216 \ })
3217 let winid = popup_create(['hello', 'bright', 'world'], {
3218 \ 'minwidth': 20,
3219 \ })
3220 call setwinvar(winid, "&signcolumn", "yes")
3221 let winbufnr = winbufnr(winid)
3222
3223 " add sign to current buffer, shows
3224 call sign_place(1, 'Selected', 'Current', bufnr('%'), {'lnum': 1})
3225 " add sign to current buffer, does not show
3226 call sign_place(2, 'PopUpSelected', 'Other', bufnr('%'), {'lnum': 2})
3227
3228 " add sign to popup buffer, shows
3229 call sign_place(3, 'PopUpSelected', 'Other', winbufnr, {'lnum': 1})
3230 " add sign to popup buffer, does not show
3231 call sign_place(4, 'Selected', 'Current', winbufnr, {'lnum': 2})
3232 END
3233 call writefile(lines, 'XtestPopupSign')
3234
3235 let buf = RunVimInTerminal('-S XtestPopupSign', #{rows: 10})
3236 call VerifyScreenDump(buf, 'Test_popupwin_sign_1', {})
3237
3238 call StopVimInTerminal(buf)
3239 call delete('XtestPopupSign')
3240endfunc
3241
Bram Moolenaar99ebf222019-12-10 23:44:48 +01003242func Test_popupwin_bufnr()
3243 let popwin = popup_create(['blah'], #{})
3244 let popbuf = winbufnr(popwin)
3245 split asdfasdf
3246 let newbuf = bufnr()
3247 call assert_true(newbuf > popbuf, 'New buffer number is higher')
3248 call assert_equal(newbuf, bufnr('$'))
3249 call popup_clear()
3250 let popwin = popup_create(['blah'], #{})
3251 " reuses previous buffer number
3252 call assert_equal(popbuf, winbufnr(popwin))
3253 call assert_equal(newbuf, bufnr('$'))
3254
3255 call popup_clear()
3256 bwipe!
3257endfunc
3258
Bram Moolenaar331bafd2019-07-20 17:46:05 +02003259" vim: shiftwidth=2 sts=2