blob: ebcc1f41490d5c6c86ddc5c41c13d392293cf648 [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]})
89 call popup_create('paddings', #{line: 6, col: 23, padding: [1, 3, 2, 4]})
90 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 Moolenaar4c6d9042019-07-16 22:04:02 +0200183 let winid = popup_create('hello both', #{line: 3, col: 8, border: [], padding: []})
184 call assert_equal(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200185 \ line: 3,
186 \ core_line: 5,
187 \ col: 8,
188 \ core_col: 10,
189 \ width: 14,
190 \ core_width: 10,
191 \ height: 5,
192 \ scrollbar: 0,
193 \ core_height: 1,
194 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100195 \ lastline: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200196 \ visible: 1}, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200197
198 call popup_clear()
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200199endfunc
200
Bram Moolenaarb4230122019-05-30 18:40:53 +0200201func Test_popup_with_syntax_win_execute()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200202 CheckScreendump
203
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200204 let lines =<< trim END
205 call setline(1, range(1, 100))
206 hi PopupColor ctermbg=lightblue
207 let winid = popup_create([
208 \ '#include <stdio.h>',
209 \ 'int main(void)',
210 \ '{',
211 \ ' printf(123);',
212 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200213 \], #{line: 3, col: 25, highlight: 'PopupColor'})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200214 call win_execute(winid, 'set syntax=cpp')
215 END
216 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200217 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200218 call VerifyScreenDump(buf, 'Test_popupwin_10', {})
219
220 " clean up
221 call StopVimInTerminal(buf)
222 call delete('XtestPopup')
223endfunc
224
225func Test_popup_with_syntax_setbufvar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200226 CheckScreendump
227
Bram Moolenaar402502d2019-05-30 22:07:36 +0200228 let lines =<< trim END
229 call setline(1, range(1, 100))
230 hi PopupColor ctermbg=lightgrey
231 let winid = popup_create([
232 \ '#include <stdio.h>',
233 \ 'int main(void)',
234 \ '{',
Bram Moolenaare089c3f2019-07-09 20:25:25 +0200235 \ "\tprintf(567);",
Bram Moolenaar402502d2019-05-30 22:07:36 +0200236 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200237 \], #{line: 3, col: 21, highlight: 'PopupColor'})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200238 call setbufvar(winbufnr(winid), '&syntax', 'cpp')
239 END
240 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200241 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200242 call VerifyScreenDump(buf, 'Test_popupwin_11', {})
243
244 " clean up
245 call StopVimInTerminal(buf)
246 call delete('XtestPopup')
247endfunc
248
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200249func Test_popup_with_matches()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200250 CheckScreendump
251
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200252 let lines =<< trim END
253 call setline(1, ['111 222 333', '444 555 666'])
254 let winid = popup_create([
255 \ '111 222 333',
256 \ '444 555 666',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200257 \], #{line: 3, col: 10, border: []})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200258 set hlsearch
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100259 hi VeryBlue ctermfg=blue guifg=blue
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200260 /666
261 call matchadd('ErrorMsg', '111')
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100262 call matchadd('VeryBlue', '444')
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200263 call win_execute(winid, "call matchadd('ErrorMsg', '111')")
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100264 call win_execute(winid, "call matchadd('VeryBlue', '555')")
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200265 END
266 call writefile(lines, 'XtestPopupMatches')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200267 let buf = RunVimInTerminal('-S XtestPopupMatches', #{rows: 10})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200268 call VerifyScreenDump(buf, 'Test_popupwin_matches', {})
269
270 " clean up
271 call StopVimInTerminal(buf)
272 call delete('XtestPopupMatches')
273endfunc
274
Bram Moolenaar399d8982019-06-02 15:34:29 +0200275func Test_popup_all_corners()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200276 CheckScreendump
277
Bram Moolenaar399d8982019-06-02 15:34:29 +0200278 let lines =<< trim END
279 call setline(1, repeat([repeat('-', 60)], 15))
280 set so=0
281 normal 2G3|r#
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200282 let winid1 = popup_create(['first', 'second'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200283 \ line: 'cursor+1',
284 \ col: 'cursor',
285 \ pos: 'topleft',
286 \ border: [],
287 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200288 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200289 normal 24|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: 'topright',
294 \ border: [],
295 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200296 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200297 normal 9G27|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: 'botleft',
302 \ border: [],
303 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200304 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200305 normal 48|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: 'botright',
310 \ border: [],
311 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200312 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200313 normal 1G51|r*
314 let winid1 = popup_create(['one', 'two'], #{
315 \ line: 'cursor-1',
316 \ col: 'cursor',
317 \ pos: 'botleft',
318 \ border: [],
319 \ padding: [],
320 \ })
Bram Moolenaar399d8982019-06-02 15:34:29 +0200321 END
322 call writefile(lines, 'XtestPopupCorners')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200323 let buf = RunVimInTerminal('-S XtestPopupCorners', #{rows: 12})
Bram Moolenaar399d8982019-06-02 15:34:29 +0200324 call VerifyScreenDump(buf, 'Test_popupwin_corners', {})
325
326 " clean up
327 call StopVimInTerminal(buf)
328 call delete('XtestPopupCorners')
329endfunc
330
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100331func Test_popup_nospace()
332 CheckScreendump
333
334 let lines =<< trim END
335 call setline(1, repeat([repeat('-', 60)], 15))
336 set so=0
337
338 " cursor in a line in top half, using "botleft" with popup that
339 " does fit
340 normal 5G2|r@
341 let winid1 = popup_create(['one', 'two'], #{
342 \ line: 'cursor-1',
343 \ col: 'cursor',
344 \ pos: 'botleft',
345 \ border: [],
346 \ })
347 " cursor in a line in top half, using "botleft" with popup that
348 " doesn't fit: gets truncated
349 normal 5G9|r#
350 let winid1 = popup_create(['one', 'two', 'tee'], #{
351 \ line: 'cursor-1',
352 \ col: 'cursor',
353 \ pos: 'botleft',
354 \ posinvert: 0,
355 \ border: [],
356 \ })
357 " cursor in a line in top half, using "botleft" with popup that
358 " doesn't fit and 'posinvert' set: flips to below.
359 normal 5G16|r%
360 let winid1 = popup_create(['one', 'two', 'tee'], #{
361 \ line: 'cursor-1',
362 \ col: 'cursor',
363 \ pos: 'botleft',
364 \ border: [],
365 \ })
366 " cursor in a line in bottom half, using "botleft" with popup that
367 " doesn't fit: does not flip.
368 normal 8G23|r*
369 let winid1 = popup_create(['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff'], #{
370 \ line: 'cursor-1',
371 \ col: 'cursor',
372 \ pos: 'botleft',
373 \ border: [],
374 \ })
375
376 " cursor in a line in bottom half, using "topleft" with popup that
377 " does fit
378 normal 8G30|r@
379 let winid1 = popup_create(['one', 'two'], #{
380 \ line: 'cursor+1',
381 \ col: 'cursor',
382 \ pos: 'topleft',
383 \ border: [],
384 \ })
385 " cursor in a line in top half, using "topleft" with popup that
386 " doesn't fit: truncated
387 normal 8G37|r#
388 let winid1 = popup_create(['one', 'two', 'tee'], #{
389 \ line: 'cursor+1',
390 \ col: 'cursor',
391 \ pos: 'topleft',
392 \ posinvert: 0,
393 \ border: [],
394 \ })
395 " cursor in a line in top half, using "topleft" with popup that
Bram Moolenaar5c6b6182019-11-10 17:51:38 +0100396 " doesn't fit and "posinvert" set: flips to above.
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100397 normal 8G44|r%
Bram Moolenaar5c6b6182019-11-10 17:51:38 +0100398 let winid1 = popup_create(['one', 'two', 'tee', 'fou', 'fiv'], #{
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100399 \ line: 'cursor+1',
400 \ col: 'cursor',
401 \ pos: 'topleft',
402 \ border: [],
403 \ })
404 " cursor in a line in top half, using "topleft" with popup that
405 " doesn't fit: does not flip.
406 normal 5G51|r*
407 let winid1 = popup_create(['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff'], #{
408 \ line: 'cursor+1',
409 \ col: 'cursor',
410 \ pos: 'topleft',
411 \ border: [],
412 \ })
413 END
414 call writefile(lines, 'XtestPopupNospace')
415 let buf = RunVimInTerminal('-S XtestPopupNospace', #{rows: 12})
416 call VerifyScreenDump(buf, 'Test_popupwin_nospace', {})
417
418 " clean up
419 call StopVimInTerminal(buf)
420 call delete('XtestPopupNospace')
421endfunc
422
Bram Moolenaar8d241042019-06-12 23:40:01 +0200423func Test_popup_firstline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200424 CheckScreendump
425
Bram Moolenaar8d241042019-06-12 23:40:01 +0200426 let lines =<< trim END
427 call setline(1, range(1, 20))
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200428 let winid = popup_create(['1111', '222222', '33333', '44', '5', '666666', '77777', '888', '9999999999999999'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200429 \ maxheight: 4,
430 \ firstline: 3,
Bram Moolenaar8d241042019-06-12 23:40:01 +0200431 \ })
432 END
433 call writefile(lines, 'XtestPopupFirstline')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200434 let buf = RunVimInTerminal('-S XtestPopupFirstline', #{rows: 10})
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200435 call VerifyScreenDump(buf, 'Test_popupwin_firstline_1', {})
436
437 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: -1})\<CR>")
438 call term_sendkeys(buf, ":\<CR>")
439 call VerifyScreenDump(buf, 'Test_popupwin_firstline_2', {})
Bram Moolenaar8d241042019-06-12 23:40:01 +0200440
441 " clean up
442 call StopVimInTerminal(buf)
443 call delete('XtestPopupFirstline')
Bram Moolenaarae943152019-06-16 22:54:14 +0200444
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200445 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200446 \ maxheight: 2,
447 \ firstline: 3,
Bram Moolenaarae943152019-06-16 22:54:14 +0200448 \ })
449 call assert_equal(3, popup_getoptions(winid).firstline)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200450 call popup_setoptions(winid, #{firstline: 1})
Bram Moolenaarae943152019-06-16 22:54:14 +0200451 call assert_equal(1, popup_getoptions(winid).firstline)
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200452 eval winid->popup_close()
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200453
454 let winid = popup_create(['xxx']->repeat(50), #{
455 \ maxheight: 3,
456 \ firstline: 11,
457 \ })
458 redraw
459 call assert_equal(11, popup_getoptions(winid).firstline)
460 call assert_equal(11, popup_getpos(winid).firstline)
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +0200461 " check line() works with popup window
462 call assert_equal(11, line('.', winid))
463 call assert_equal(50, line('$', winid))
464 call assert_equal(0, line('$', 123456))
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200465
466 " Normal command changes what is displayed but not "firstline"
467 call win_execute(winid, "normal! \<c-y>")
468 call assert_equal(11, popup_getoptions(winid).firstline)
469 call assert_equal(10, popup_getpos(winid).firstline)
470
471 " Making some property change applies "firstline" again
472 call popup_setoptions(winid, #{line: 4})
473 call assert_equal(11, popup_getoptions(winid).firstline)
474 call assert_equal(11, popup_getpos(winid).firstline)
475
476 " Remove "firstline" property and scroll
477 call popup_setoptions(winid, #{firstline: 0})
478 call win_execute(winid, "normal! \<c-y>")
479 call assert_equal(0, popup_getoptions(winid).firstline)
480 call assert_equal(10, popup_getpos(winid).firstline)
481
482 " Making some property change has no side effect
483 call popup_setoptions(winid, #{line: 3})
484 call assert_equal(0, popup_getoptions(winid).firstline)
485 call assert_equal(10, popup_getpos(winid).firstline)
Bram Moolenaarae943152019-06-16 22:54:14 +0200486
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100487 " CTRL-D scrolls down half a page
488 let winid = popup_create(['xxx']->repeat(50), #{
489 \ maxheight: 8,
490 \ })
491 redraw
492 call assert_equal(1, popup_getpos(winid).firstline)
493 call win_execute(winid, "normal! \<C-D>")
494 call assert_equal(5, popup_getpos(winid).firstline)
495 call win_execute(winid, "normal! \<C-D>")
496 call assert_equal(9, popup_getpos(winid).firstline)
497 call win_execute(winid, "normal! \<C-U>")
498 call assert_equal(5, popup_getpos(winid).firstline)
499
500 call win_execute(winid, "normal! \<C-F>")
501 call assert_equal(11, popup_getpos(winid).firstline)
502 call win_execute(winid, "normal! \<C-B>")
503 call assert_equal(5, popup_getpos(winid).firstline)
504
Bram Moolenaarae943152019-06-16 22:54:14 +0200505 call popup_close(winid)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200506endfunc
507
Bram Moolenaara112f2d2019-09-01 17:38:09 +0200508func Test_popup_noscrolloff()
509 set scrolloff=5
510 let winid = popup_create(['xxx']->repeat(50), #{
511 \ maxheight: 5,
512 \ firstline: 11,
513 \ })
514 redraw
515 call assert_equal(11, popup_getoptions(winid).firstline)
516 call assert_equal(11, popup_getpos(winid).firstline)
517
518 call popup_setoptions(winid, #{firstline: 0})
519 call win_execute(winid, "normal! \<c-y>")
520 call assert_equal(0, popup_getoptions(winid).firstline)
521 call assert_equal(10, popup_getpos(winid).firstline)
522
523 call popup_close(winid)
524endfunc
525
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200526func Test_popup_drag()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200527 CheckScreendump
528
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200529 " create a popup that covers the command line
530 let lines =<< trim END
531 call setline(1, range(1, 20))
Bram Moolenaar356375f2019-08-24 14:46:29 +0200532 split
533 vsplit
534 $wincmd w
535 vsplit
536 1wincmd w
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200537 let winid = popup_create(['1111', '222222', '33333'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200538 \ drag: 1,
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200539 \ resize: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200540 \ border: [],
541 \ line: &lines - 4,
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200542 \ })
543 func Dragit()
544 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
545 endfunc
546 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
Bram Moolenaar356375f2019-08-24 14:46:29 +0200547 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2 - 20)<CR>
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200548 func Resize()
549 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
550 endfunc
Bram Moolenaar356375f2019-08-24 14:46:29 +0200551 map <silent> <F5> :call test_setmouse(6, 21)<CR>
552 map <silent> <F6> :call test_setmouse(7, 25)<CR>
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200553 END
554 call writefile(lines, 'XtestPopupDrag')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200555 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200556 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
557
558 call term_sendkeys(buf, ":call Dragit()\<CR>")
559 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
560
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200561 call term_sendkeys(buf, ":call Resize()\<CR>")
562 call VerifyScreenDump(buf, 'Test_popupwin_drag_03', {})
563
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200564 " clean up
565 call StopVimInTerminal(buf)
566 call delete('XtestPopupDrag')
567endfunc
568
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200569func Test_popup_close_with_mouse()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200570 CheckScreendump
571
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200572 let lines =<< trim END
573 call setline(1, range(1, 20))
574 " With border, can click on X
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200575 let winid = popup_create('foobar', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200576 \ close: 'button',
577 \ border: [],
578 \ line: 1,
579 \ col: 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200580 \ })
581 func CloseMsg(id, result)
582 echomsg 'Popup closed with ' .. a:result
583 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200584 let winid = popup_create('notification', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200585 \ close: 'click',
586 \ line: 3,
587 \ col: 15,
588 \ callback: 'CloseMsg',
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200589 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200590 let winid = popup_create('no border here', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200591 \ close: 'button',
592 \ line: 5,
593 \ col: 3,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200594 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200595 let winid = popup_create('only padding', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200596 \ close: 'button',
597 \ padding: [],
598 \ line: 5,
599 \ col: 23,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200600 \ })
601 func CloseWithX()
602 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
603 endfunc
604 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
605 func CloseWithClick()
606 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
607 endfunc
608 map <silent> <F4> :call test_setmouse(3, 17)<CR>
Bram Moolenaarf6396232019-08-24 19:36:00 +0200609 func CreateWithMenuFilter()
610 let winid = popup_create('barfoo', #{
611 \ close: 'button',
612 \ filter: 'popup_filter_menu',
613 \ border: [],
614 \ line: 1,
615 \ col: 40,
616 \ })
617 endfunc
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200618 END
619 call writefile(lines, 'XtestPopupClose')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200620 let buf = RunVimInTerminal('-S XtestPopupClose', #{rows: 10})
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200621 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
622
623 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
624 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
625
626 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
627 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
628
Bram Moolenaarf6396232019-08-24 19:36:00 +0200629 call term_sendkeys(buf, ":call CreateWithMenuFilter()\<CR>")
630 call VerifyScreenDump(buf, 'Test_popupwin_close_04', {})
631
632 " We have to send the actual mouse code, feedkeys() would be caught the
633 " filter.
634 call term_sendkeys(buf, "\<Esc>[<0;47;1M")
635 call VerifyScreenDump(buf, 'Test_popupwin_close_05', {})
636
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200637 " clean up
638 call StopVimInTerminal(buf)
639 call delete('XtestPopupClose')
640endfunction
641
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200642func Test_popup_menu_wrap()
643 CheckScreendump
644
645 let lines =<< trim END
646 call setline(1, range(1, 20))
647 call popup_create([
648 \ 'one',
649 \ 'asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfas',
650 \ 'three',
651 \ 'four',
652 \ ], #{
653 \ pos: "botleft",
654 \ border: [],
655 \ padding: [0,1,0,1],
656 \ maxheight: 3,
657 \ cursorline: 1,
658 \ filter: 'popup_filter_menu',
659 \ })
660 END
661 call writefile(lines, 'XtestPopupWrap')
662 let buf = RunVimInTerminal('-S XtestPopupWrap', #{rows: 10})
663 call VerifyScreenDump(buf, 'Test_popupwin_wrap_1', {})
664
665 call term_sendkeys(buf, "jj")
666 call VerifyScreenDump(buf, 'Test_popupwin_wrap_2', {})
667
668 " clean up
669 call term_sendkeys(buf, "\<Esc>")
670 call StopVimInTerminal(buf)
671 call delete('XtestPopupWrap')
672endfunction
673
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200674func Test_popup_with_mask()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200675 CheckScreendump
676
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200677 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200678 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200679 hi PopupColor ctermbg=lightgrey
680 let winid = popup_create([
681 \ 'some text',
682 \ 'another line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200683 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200684 \ line: 1,
685 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100686 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200687 \ wrap: 0,
688 \ fixed: 1,
689 \ zindex: 90,
690 \ padding: [],
691 \ highlight: 'PopupColor',
692 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200693 call popup_create([
694 \ 'xxxxxxxxx',
695 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200696 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200697 \ line: 3,
698 \ col: 18,
699 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200700 let winidb = popup_create([
701 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200702 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200703 \ line: 7,
704 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100705 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200706 \ wrap: 0,
707 \ fixed: 1,
708 \ close: 'button',
709 \ zindex: 90,
710 \ padding: [],
711 \ border: [],
712 \ 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 +0200713 END
714 call writefile(lines, 'XtestPopupMask')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200715 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200716 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
717
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200718 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
719 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200720 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200721 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
722
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200723 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
724 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200725 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200726 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
727
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200728 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
729 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200730 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200731 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
732
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200733 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
734 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200735 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200736 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
737
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200738 " clean up
739 call StopVimInTerminal(buf)
740 call delete('XtestPopupMask')
741endfunc
742
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200743func Test_popup_select()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200744 CheckScreendump
745 CheckFeature clipboard_working
746
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200747 " create a popup with some text to be selected
748 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200749 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200750 call setline(1, range(1, 20))
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200751 let winid = popup_create(['the word', 'some more', 'several words here', 'invisible', '5', '6', '7'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200752 \ drag: 1,
753 \ border: [],
754 \ line: 3,
755 \ col: 10,
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200756 \ maxheight: 3,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200757 \ })
758 func Select1()
759 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
760 endfunc
761 map <silent> <F3> :call test_setmouse(4, 15)<CR>
762 map <silent> <F4> :call test_setmouse(6, 23)<CR>
763 END
764 call writefile(lines, 'XtestPopupSelect')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200765 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200766 call term_sendkeys(buf, ":call Select1()\<CR>")
767 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
768
769 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
770 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200771 " clean the command line, sometimes it still shows a command
772 call term_sendkeys(buf, ":\<esc>")
773
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200774 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
775
776 " clean up
777 call StopVimInTerminal(buf)
778 call delete('XtestPopupSelect')
779endfunc
780
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200781func Test_popup_in_tab()
782 " default popup is local to tab, not visible when in other tab
783 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200784 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200785 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200786 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200787 tabnew
788 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200789 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200790 quit
791 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200792
793 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200794 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200795 " buffer is gone now
796 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200797
798 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200799 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200800 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200801 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200802 tabnew
803 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200804 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200805 quit
806 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200807 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200808
809 " create popup in other tab
810 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200811 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200812 call assert_equal(0, popup_getpos(winid).visible)
813 call assert_equal(1, popup_getoptions(winid).tabpage)
814 quit
815 call assert_equal(1, popup_getpos(winid).visible)
816 call assert_equal(0, popup_getoptions(winid).tabpage)
817 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200818endfunc
819
820func Test_popup_valid_arguments()
821 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200822 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200823 let pos = popup_getpos(winid)
824 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200825 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200826
827 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200828 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200829 let pos = popup_getpos(winid)
830 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200831 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200832
833 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200834 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200835 let pos = popup_getpos(winid)
836 let around = (&columns - pos.width) / 2
837 call assert_inrange(around - 1, around + 1, pos.col)
838 let around = (&lines - pos.height) / 2
839 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200840 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200841endfunc
842
843func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200844 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200845 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200846 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200847 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200848
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200849 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200850 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200851 call assert_fails('call popup_create("text", #{col: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200852 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200853 call assert_fails('call popup_create("text", #{col: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200854 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200855 call assert_fails('call popup_create("text", #{col: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200856 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200857
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200858 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200859 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200860 call assert_fails('call popup_create("text", #{line: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200861 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200862 call assert_fails('call popup_create("text", #{line: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200863 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200864 call assert_fails('call popup_create("text", #{line: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200865 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200866
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200867 call assert_fails('call popup_create("text", #{pos: "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200868 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200869 call assert_fails('call popup_create("text", #{padding: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200870 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200871 call assert_fails('call popup_create("text", #{border: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200872 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200873 call assert_fails('call popup_create("text", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200874 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +0200875 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
876 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200877 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200878 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200879
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200880 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200881 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200882 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200883 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200884 call assert_fails('call popup_create([#{text: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200885 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200886 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
887 call popup_clear()
888 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200889 call assert_fails('call popup_create("text", #{mapping: []})', 'E745:')
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200890 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200891endfunc
892
Bram Moolenaareea16992019-05-31 17:34:48 +0200893func Test_win_execute_closing_curwin()
894 split
895 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200896 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200897 call popup_clear()
Bram Moolenaar49540192019-12-11 19:34:54 +0100898
899 let winid = popup_create('some text', {})
900 call assert_fails('call win_execute(winid, printf("normal! :\<C-u>call popup_close(%d)\<CR>", winid))', 'E994')
901 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200902endfunc
903
904func Test_win_execute_not_allowed()
905 let winid = popup_create('some text', {})
906 call assert_fails('call win_execute(winid, "split")', 'E994:')
907 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
908 call assert_fails('call win_execute(winid, "close")', 'E994:')
909 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200910 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200911 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
912 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
913 call assert_fails('call win_execute(winid, "next")', 'E994:')
914 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
915 call assert_fails('call win_execute(winid, "buf")', 'E994:')
Bram Moolenaar3029bcc2020-01-18 15:06:19 +0100916 call assert_fails('call win_execute(winid, "bnext")', 'E994:')
917 call assert_fails('call win_execute(winid, "bprev")', 'E994:')
918 call assert_fails('call win_execute(winid, "bfirst")', 'E994:')
919 call assert_fails('call win_execute(winid, "blast")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200920 call assert_fails('call win_execute(winid, "edit")', 'E994:')
921 call assert_fails('call win_execute(winid, "enew")', 'E994:')
922 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
923 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
924 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
925 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200926 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200927endfunc
928
Bram Moolenaar402502d2019-05-30 22:07:36 +0200929func Test_popup_with_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200930 CheckScreendump
931
Bram Moolenaar402502d2019-05-30 22:07:36 +0200932 let lines =<< trim END
933 call setline(1, range(1, 100))
934 let winid = popup_create(
935 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200936 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200937 END
938 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200939 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200940 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
941
942 " clean up
943 call StopVimInTerminal(buf)
944 call delete('XtestPopup')
945endfunc
946
947func Test_popup_without_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200948 CheckScreendump
949
Bram Moolenaar402502d2019-05-30 22:07:36 +0200950 let lines =<< trim END
951 call setline(1, range(1, 100))
952 let winid = popup_create(
953 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200954 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200955 END
956 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200957 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200958 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
959
960 " clean up
961 call StopVimInTerminal(buf)
962 call delete('XtestPopup')
963endfunc
964
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200965func Test_popup_with_showbreak()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200966 CheckScreendump
967
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200968 let lines =<< trim END
969 set showbreak=>>\
970 call setline(1, range(1, 20))
971 let winid = popup_dialog(
Bram Moolenaar8ae54372019-09-15 18:11:16 +0200972 \ 'a long line here that wraps',
973 \ #{filter: 'popup_filter_yesno',
974 \ maxwidth: 12})
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200975 END
976 call writefile(lines, 'XtestPopupShowbreak')
977 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
978 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
979
980 " clean up
981 call term_sendkeys(buf, "y")
982 call StopVimInTerminal(buf)
983 call delete('XtestPopupShowbreak')
984endfunc
985
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200986func Test_popup_time()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200987 CheckFeature timers
988
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200989 topleft vnew
990 call setline(1, 'hello')
991
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200992 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200993 \ line: 1,
994 \ col: 1,
995 \ minwidth: 20,
996 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200997 \})
998 redraw
999 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1000 call assert_equal('world', line)
1001
Bram Moolenaarb4f06282019-07-12 21:07:54 +02001002 call assert_equal(winid, popup_locate(1, 1))
1003 call assert_equal(winid, popup_locate(1, 20))
1004 call assert_equal(0, popup_locate(1, 21))
1005 call assert_equal(0, popup_locate(2, 1))
1006
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001007 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +02001008 redraw
Bram Moolenaar196b4662019-09-06 21:34:30 +02001009 let line = join(map(range(1, 5), '1->screenstring(v:val)'), '')
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001010 call assert_equal('hello', line)
1011
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001012 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001013 \ line: &lines,
1014 \ col: 10,
1015 \ minwidth: 20,
1016 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001017 \})
1018 redraw
1019 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1020 call assert_match('.*on the command line.*', line)
1021
1022 sleep 700m
1023 redraw
1024 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1025 call assert_notmatch('.*on the command line.*', line)
1026
1027 bwipe!
1028endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001029
1030func Test_popup_hide()
1031 topleft vnew
1032 call setline(1, 'hello')
1033
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001034 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001035 \ line: 1,
1036 \ col: 1,
1037 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001038 \})
1039 redraw
1040 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1041 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001042 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001043 " buffer is still listed and active
1044 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001045
1046 call popup_hide(winid)
1047 redraw
1048 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1049 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001050 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001051 " buffer is still listed but hidden
Bram Moolenaarca7c0782020-01-14 20:42:48 +01001052 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001053
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001054 eval winid->popup_show()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001055 redraw
1056 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1057 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001058 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001059
1060
1061 call popup_close(winid)
1062 redraw
1063 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1064 call assert_equal('hello', line)
1065
1066 " error is given for existing non-popup window
1067 call assert_fails('call popup_hide(win_getid())', 'E993:')
1068
1069 " no error non-existing window
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001070 eval 1234234->popup_hide()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001071 call popup_show(41234234)
1072
1073 bwipe!
1074endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001075
1076func Test_popup_move()
1077 topleft vnew
1078 call setline(1, 'hello')
1079
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001080 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001081 \ line: 1,
1082 \ col: 1,
1083 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001084 \})
1085 redraw
1086 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1087 call assert_equal('world ', line)
1088
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001089 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001090 redraw
1091 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1092 call assert_equal('hello ', line)
1093 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
1094 call assert_equal('~world', line)
1095
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001096 eval winid->popup_move(#{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001097 redraw
1098 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1099 call assert_equal('hworld', line)
1100
1101 call popup_close(winid)
1102
1103 bwipe!
1104endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +02001105
Bram Moolenaar402502d2019-05-30 22:07:36 +02001106func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001107 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001108 \ line: 2,
1109 \ col: 3,
1110 \ minwidth: 10,
1111 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +02001112 \})
1113 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001114 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001115 call assert_equal(2, res.line)
1116 call assert_equal(3, res.col)
1117 call assert_equal(10, res.width)
1118 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001119 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001120
1121 call popup_close(winid)
1122endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001123
1124func Test_popup_width_longest()
1125 let tests = [
1126 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
1127 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
1128 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
1129 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
1130 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
1131 \ ]
1132
1133 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001134 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001135 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001136 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001137 call assert_equal(test[1], position.width)
1138 call popup_close(winid)
1139 endfor
1140endfunc
1141
1142func Test_popup_wraps()
1143 let tests = [
1144 \ ['nowrap', 6, 1],
1145 \ ['a line that wraps once', 12, 2],
1146 \ ['a line that wraps two times', 12, 3],
1147 \ ]
1148 for test in tests
1149 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001150 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001151 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001152 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001153 call assert_equal(test[1], position.width)
1154 call assert_equal(test[2], position.height)
1155
1156 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001157 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001158 endfor
1159endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001160
1161func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001162 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001163 \ line: 2,
1164 \ col: 3,
1165 \ minwidth: 10,
1166 \ minheight: 11,
1167 \ maxwidth: 20,
1168 \ maxheight: 21,
1169 \ zindex: 100,
1170 \ time: 5000,
1171 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001172 \})
1173 redraw
1174 let res = popup_getoptions(winid)
1175 call assert_equal(2, res.line)
1176 call assert_equal(3, res.col)
1177 call assert_equal(10, res.minwidth)
1178 call assert_equal(11, res.minheight)
1179 call assert_equal(20, res.maxwidth)
1180 call assert_equal(21, res.maxheight)
1181 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001182 call assert_equal(1, res.fixed)
Bram Moolenaarb8350ab2019-08-04 17:59:49 +02001183 call assert_equal(1, res.mapping)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001184 if has('timers')
1185 call assert_equal(5000, res.time)
1186 endif
1187 call popup_close(winid)
1188
1189 let winid = popup_create('hello', {})
1190 redraw
1191 let res = popup_getoptions(winid)
1192 call assert_equal(0, res.line)
1193 call assert_equal(0, res.col)
1194 call assert_equal(0, res.minwidth)
1195 call assert_equal(0, res.minheight)
1196 call assert_equal(0, res.maxwidth)
1197 call assert_equal(0, res.maxheight)
1198 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001199 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001200 if has('timers')
1201 call assert_equal(0, res.time)
1202 endif
1203 call popup_close(winid)
1204 call assert_equal({}, popup_getoptions(winid))
1205endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001206
1207func Test_popup_option_values()
1208 new
1209 " window-local
1210 setlocal number
1211 setlocal nowrap
1212 " buffer-local
1213 setlocal omnifunc=Something
1214 " global/buffer-local
1215 setlocal path=/there
1216 " global/window-local
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001217 setlocal statusline=2
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001218
1219 let winid = popup_create('hello', {})
1220 call assert_equal(0, getwinvar(winid, '&number'))
1221 call assert_equal(1, getwinvar(winid, '&wrap'))
1222 call assert_equal('', getwinvar(winid, '&omnifunc'))
1223 call assert_equal(&g:path, getwinvar(winid, '&path'))
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001224 call assert_equal(&g:statusline, getwinvar(winid, '&statusline'))
1225
1226 " 'scrolloff' is reset to zero
1227 call assert_equal(5, &scrolloff)
1228 call assert_equal(0, getwinvar(winid, '&scrolloff'))
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001229
1230 call popup_close(winid)
1231 bwipe
1232endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001233
1234func Test_popup_atcursor()
1235 topleft vnew
1236 call setline(1, [
1237 \ 'xxxxxxxxxxxxxxxxx',
1238 \ 'xxxxxxxxxxxxxxxxx',
1239 \ 'xxxxxxxxxxxxxxxxx',
1240 \])
1241
1242 call cursor(2, 2)
1243 redraw
1244 let winid = popup_atcursor('vim', {})
1245 redraw
1246 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1247 call assert_equal('xvimxxxxxxxxxxxxx', line)
1248 call popup_close(winid)
1249
1250 call cursor(3, 4)
1251 redraw
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001252 let winid = 'vim'->popup_atcursor({})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001253 redraw
1254 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
1255 call assert_equal('xxxvimxxxxxxxxxxx', line)
1256 call popup_close(winid)
1257
1258 call cursor(1, 1)
1259 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001260 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001261 \ line: 'cursor+2',
1262 \ col: 'cursor+1',
1263 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001264 redraw
1265 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1266 call assert_equal('xvimxxxxxxxxxxxxx', line)
1267 call popup_close(winid)
1268
1269 call cursor(3, 3)
1270 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001271 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001272 \ line: 'cursor-2',
1273 \ col: 'cursor-1',
1274 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001275 redraw
1276 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1277 call assert_equal('xvimxxxxxxxxxxxxx', line)
1278 call popup_close(winid)
1279
Bram Moolenaar402502d2019-05-30 22:07:36 +02001280 " just enough room above
1281 call cursor(3, 3)
1282 redraw
1283 let winid = popup_atcursor(['vim', 'is great'], {})
1284 redraw
1285 let pos = popup_getpos(winid)
1286 call assert_equal(1, pos.line)
1287 call popup_close(winid)
1288
1289 " not enough room above, popup goes below the cursor
1290 call cursor(3, 3)
1291 redraw
1292 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1293 redraw
1294 let pos = popup_getpos(winid)
1295 call assert_equal(4, pos.line)
1296 call popup_close(winid)
1297
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001298 " cursor in first line, popup in line 2
1299 call cursor(1, 1)
1300 redraw
1301 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1302 redraw
1303 let pos = popup_getpos(winid)
1304 call assert_equal(2, pos.line)
1305 call popup_close(winid)
1306
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001307 bwipe!
1308endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001309
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001310func Test_popup_atcursor_pos()
1311 CheckScreendump
1312
1313 let lines =<< trim END
1314 call setline(1, repeat([repeat('-', 60)], 15))
1315 set so=0
1316
1317 normal 9G3|r#
1318 let winid1 = popup_atcursor(['first', 'second'], #{
1319 \ moved: [0, 0, 0],
1320 \ })
1321 normal 9G21|r&
1322 let winid1 = popup_atcursor(['FIrsT', 'SEcoND'], #{
1323 \ pos: 'botright',
1324 \ moved: [0, 0, 0],
1325 \ })
1326 normal 3G27|r%
1327 let winid1 = popup_atcursor(['fiRSt', 'seCOnd'], #{
1328 \ pos: 'topleft',
1329 \ moved: [0, 0, 0],
1330 \ })
1331 normal 3G45|r@
1332 let winid1 = popup_atcursor(['First', 'SeconD'], #{
1333 \ pos: 'topright',
1334 \ moved: [0, 0, 0],
1335 \ })
1336 END
1337 call writefile(lines, 'XtestPopupAtcursorPos')
1338 let buf = RunVimInTerminal('-S XtestPopupAtcursorPos', #{rows: 12})
1339 call VerifyScreenDump(buf, 'Test_popupwin_atcursor_pos', {})
1340
1341 " clean up
1342 call StopVimInTerminal(buf)
1343 call delete('XtestPopupAtcursorPos')
1344endfunc
1345
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001346func Test_popup_beval()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001347 CheckScreendump
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001348 CheckFeature balloon_eval_term
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001349
1350 let lines =<< trim END
1351 call setline(1, range(1, 20))
1352 call setline(5, 'here is some text to hover over')
1353 set balloonevalterm
1354 set balloonexpr=BalloonExpr()
1355 set balloondelay=100
1356 func BalloonExpr()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001357 let s:winid = [v:beval_text]->popup_beval({})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001358 return ''
1359 endfunc
1360 func Hover()
1361 call test_setmouse(5, 15)
1362 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1363 sleep 100m
1364 endfunc
1365 func MoveOntoPopup()
1366 call test_setmouse(4, 17)
1367 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1368 endfunc
1369 func MoveAway()
1370 call test_setmouse(5, 13)
1371 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1372 endfunc
1373 END
1374 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001375 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001376 call term_wait(buf, 100)
1377 call term_sendkeys(buf, 'j')
1378 call term_sendkeys(buf, ":call Hover()\<CR>")
1379 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1380
1381 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1382 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1383
1384 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1385 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1386
1387 " clean up
1388 call StopVimInTerminal(buf)
1389 call delete('XtestPopupBeval')
1390endfunc
1391
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001392func Test_popup_filter()
1393 new
1394 call setline(1, 'some text')
1395
1396 func MyPopupFilter(winid, c)
1397 if a:c == 'e'
1398 let g:eaten = 'e'
1399 return 1
1400 endif
1401 if a:c == '0'
1402 let g:ignored = '0'
1403 return 0
1404 endif
1405 if a:c == 'x'
1406 call popup_close(a:winid)
1407 return 1
1408 endif
1409 return 0
1410 endfunc
1411
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001412 let winid = 'something'->popup_create(#{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001413 redraw
1414
1415 " e is consumed by the filter
1416 call feedkeys('e', 'xt')
1417 call assert_equal('e', g:eaten)
1418
1419 " 0 is ignored by the filter
1420 normal $
1421 call assert_equal(9, getcurpos()[2])
1422 call feedkeys('0', 'xt')
1423 call assert_equal('0', g:ignored)
1424 call assert_equal(1, getcurpos()[2])
1425
1426 " x closes the popup
1427 call feedkeys('x', 'xt')
1428 call assert_equal('e', g:eaten)
1429 call assert_equal(-1, winbufnr(winid))
1430
1431 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001432 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001433endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001434
Bram Moolenaara42d9452019-06-15 21:46:30 +02001435func ShowDialog(key, result)
1436 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001437 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001438 \ filter: 'popup_filter_yesno',
1439 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001440 \ })
1441 redraw
1442 call feedkeys(a:key, "xt")
1443 call assert_equal(winid, s:cb_winid)
1444 call assert_equal(a:result, s:cb_res)
1445endfunc
1446
1447func Test_popup_dialog()
1448 func QuitCallback(id, res)
1449 let s:cb_winid = a:id
1450 let s:cb_res = a:res
1451 endfunc
1452
1453 let winid = ShowDialog("y", 1)
1454 let winid = ShowDialog("Y", 1)
1455 let winid = ShowDialog("n", 0)
1456 let winid = ShowDialog("N", 0)
1457 let winid = ShowDialog("x", 0)
1458 let winid = ShowDialog("X", 0)
1459 let winid = ShowDialog("\<Esc>", 0)
1460 let winid = ShowDialog("\<C-C>", -1)
1461
1462 delfunc QuitCallback
1463endfunc
1464
Bram Moolenaara730e552019-06-16 19:05:31 +02001465func ShowMenu(key, result)
1466 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001467 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001468 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001469 \ })
1470 redraw
1471 call feedkeys(a:key, "xt")
1472 call assert_equal(winid, s:cb_winid)
1473 call assert_equal(a:result, s:cb_res)
1474endfunc
1475
1476func Test_popup_menu()
1477 func QuitCallback(id, res)
1478 let s:cb_winid = a:id
1479 let s:cb_res = a:res
1480 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001481 " mapping won't be used in popup
1482 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001483
1484 let winid = ShowMenu(" ", 1)
1485 let winid = ShowMenu("j \<CR>", 2)
1486 let winid = ShowMenu("JjK \<CR>", 2)
1487 let winid = ShowMenu("jjjjjj ", 3)
1488 let winid = ShowMenu("kkk ", 1)
1489 let winid = ShowMenu("x", -1)
1490 let winid = ShowMenu("X", -1)
1491 let winid = ShowMenu("\<Esc>", -1)
1492 let winid = ShowMenu("\<C-C>", -1)
1493
1494 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001495 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001496endfunc
1497
1498func Test_popup_menu_screenshot()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001499 CheckScreendump
Bram Moolenaara730e552019-06-16 19:05:31 +02001500
1501 let lines =<< trim END
1502 call setline(1, range(1, 20))
1503 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001504 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001505 func MenuDone(id, res)
1506 echomsg "selected " .. a:res
1507 endfunc
1508 END
1509 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001510 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001511 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1512
1513 call term_sendkeys(buf, "jj")
1514 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1515
1516 call term_sendkeys(buf, " ")
1517 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1518
1519 " clean up
1520 call StopVimInTerminal(buf)
1521 call delete('XtestPopupMenu')
1522endfunc
1523
Bram Moolenaarf914a332019-07-20 15:09:56 +02001524func Test_popup_menu_narrow()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001525 CheckScreendump
Bram Moolenaarf914a332019-07-20 15:09:56 +02001526
1527 let lines =<< trim END
1528 call setline(1, range(1, 20))
1529 hi PopupSelected ctermbg=green
1530 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1531 func MenuDone(id, res)
1532 echomsg "selected " .. a:res
1533 endfunc
1534 END
1535 call writefile(lines, 'XtestPopupNarrowMenu')
1536 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1537 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1538
1539 " clean up
1540 call term_sendkeys(buf, "x")
1541 call StopVimInTerminal(buf)
1542 call delete('XtestPopupNarrowMenu')
1543endfunc
1544
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001545func Test_popup_title()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001546 CheckScreendump
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001547
1548 " Create a popup without title or border, a line of padding will be added to
1549 " put the title on.
1550 let lines =<< trim END
1551 call setline(1, range(1, 20))
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001552 let winid = popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001553 END
1554 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001555 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001556 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1557
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001558 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxwidth: 20, title: 'a very long title that is not going to fit'})\<CR>")
1559 call term_sendkeys(buf, ":\<CR>")
1560 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_1', {})
1561
1562 call term_sendkeys(buf, ":call popup_setoptions(winid, #{border: []})\<CR>")
1563 call term_sendkeys(buf, ":\<CR>")
1564 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_2', {})
1565
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001566 " clean up
1567 call StopVimInTerminal(buf)
1568 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001569
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001570 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001571 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001572 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001573 call assert_equal('Another Title', popup_getoptions(winid).title)
1574
1575 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001576endfunc
1577
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001578func Test_popup_close_callback()
1579 func PopupDone(id, result)
1580 let g:result = a:result
1581 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001582 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001583 redraw
1584 call popup_close(winid, 'done')
1585 call assert_equal('done', g:result)
1586endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001587
1588func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001589 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001590 redraw
1591 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001592 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001593 call assert_equal(5, pos.height)
1594
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001595 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001596 redraw
1597 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001598 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001599 call assert_equal(3, pos.height)
1600endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001601
1602func Test_popup_never_behind()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001603 CheckScreendump
1604
Bram Moolenaar988c4332019-06-02 14:12:11 +02001605 " +-----------------------------+
1606 " | | |
1607 " | | |
1608 " | | |
1609 " | line1 |
1610 " |------------line2------------|
1611 " | line3 |
1612 " | line4 |
1613 " | |
1614 " | |
1615 " +-----------------------------+
1616 let lines =<< trim END
Bram Moolenaar988c4332019-06-02 14:12:11 +02001617 split
1618 vsplit
1619 let info_window1 = getwininfo()[0]
1620 let line = info_window1['height']
1621 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001622 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001623 \ line : line,
1624 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001625 \ })
1626 END
1627 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001628 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001629 call term_sendkeys(buf, "\<C-W>w")
1630 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1631
1632 " clean up
1633 call StopVimInTerminal(buf)
1634 call delete('XtestPopupBehind')
1635endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001636
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001637func s:VerifyPosition(p, msg, line, col, width, height)
1638 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1639 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1640 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1641 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001642endfunc
1643
1644func Test_popup_position_adjust()
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001645 " Anything placed past the last cell on the right of the screen is moved to
1646 " the left.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001647 "
1648 " When wrapping is disabled, we also shift to the left to display on the
1649 " screen, unless fixed is set.
1650
1651 " Entries for cases which don't vary based on wrapping.
1652 " Format is per tests described below
1653 let both_wrap_tests = [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001654 \ ['a', 5, &columns, 5, &columns, 1, 1],
1655 \ ['b', 5, &columns + 1, 5, &columns, 1, 1],
1656 \ ['c', 5, &columns - 1, 5, &columns - 1, 1, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001657 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001658 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1]]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001659
1660 " these test groups are dicts with:
1661 " - comment: something to identify the group of tests by
1662 " - options: dict of options to merge with the row/col in tests
1663 " - tests: list of cases. Each one is a list with elements:
1664 " - text
1665 " - row
1666 " - col
1667 " - expected row
1668 " - expected col
1669 " - expected width
1670 " - expected height
1671 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001672 \ #{
1673 \ comment: 'left-aligned with wrapping',
1674 \ options: #{
1675 \ wrap: 1,
1676 \ pos: 'botleft',
1677 \ },
1678 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001679 \ ['aa', 5, &columns, 4, &columns, 1, 2],
1680 \ ['bb', 5, &columns + 1, 4, &columns, 1, 2],
1681 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1682 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1683 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1684 \
1685 \ ['aaa', 5, &columns, 3, &columns, 1, 3],
1686 \ ['bbb', 5, &columns + 1, 3, &columns, 1, 3],
1687 \ ['ccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
1688 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1689 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1690 \
1691 \ ['aaaa', 5, &columns, 2, &columns, 1, 4],
1692 \ ['bbbb', 5, &columns + 1, 2, &columns, 1, 4],
1693 \ ['cccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001694 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
1695 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001696 \ ['eeee', 5, &columns - 4, 5, &columns - 4, 4, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001697 \ ],
1698 \ },
1699 \ #{
1700 \ comment: 'left aligned without wrapping',
1701 \ options: #{
1702 \ wrap: 0,
1703 \ pos: 'botleft',
1704 \ },
1705 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001706 \ ['aa', 5, &columns, 5, &columns - 1, 2, 1],
1707 \ ['bb', 5, &columns + 1, 5, &columns - 1, 2, 1],
1708 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1709 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1710 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1711 \
1712 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
1713 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1714 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1715 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1716 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1717 \
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001718 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
1719 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
1720 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
1721 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
1722 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1723 \ ],
1724 \ },
1725 \ #{
1726 \ comment: 'left aligned with fixed position',
1727 \ options: #{
1728 \ wrap: 0,
1729 \ fixed: 1,
1730 \ pos: 'botleft',
1731 \ },
1732 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001733 \ ['aa', 5, &columns, 5, &columns, 1, 1],
1734 \ ['bb', 5, &columns + 1, 5, &columns, 1, 1],
1735 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1736 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1737 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1738 \
1739 \ ['aaa', 5, &columns, 5, &columns, 1, 1],
1740 \ ['bbb', 5, &columns + 1, 5, &columns, 1, 1],
1741 \ ['ccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1742 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1743 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1744 \
1745 \ ['aaaa', 5, &columns, 5, &columns, 1, 1],
1746 \ ['bbbb', 5, &columns + 1, 5, &columns, 1, 1],
1747 \ ['cccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001748 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1749 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1750 \ ],
1751 \ },
1752 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001753
1754 for test_group in tests
1755 for test in test_group.tests
1756 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001757 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001758 \ line: line,
1759 \ col: col,
1760 \ }
1761 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001762
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001763 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001764
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001765 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001766 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
1767 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001768 endfor
1769 endfor
1770
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001771 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001772 %bwipe!
1773endfunc
1774
Bram Moolenaar3397f742019-06-02 18:40:06 +02001775func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001776 " width of screen
1777 let X = join(map(range(&columns), {->'X'}), '')
1778
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001779 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1780 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001781
1782 redraw
1783 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1784 call assert_equal(X, line)
1785
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001786 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001787 redraw
1788
1789 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001790 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
1791 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001792
1793 redraw
1794 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1795 call assert_equal(X, line)
1796
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001797 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001798 redraw
1799
1800 " Extend so > window width
1801 let X .= 'x'
1802
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001803 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1804 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001805
1806 redraw
1807 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1808 call assert_equal(X[ : -2 ], line)
1809
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001810 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001811 redraw
1812
1813 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001814 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
1815 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001816
1817 redraw
1818 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1819 call assert_equal(X[ : -2 ], line)
1820
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001821 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001822 redraw
1823
1824 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001825 let p = popup_create(X,
1826 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
1827 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001828
1829 redraw
1830 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1831 let e_line = ' ' . X[ 1 : -2 ]
1832 call assert_equal(e_line, line)
1833
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001834 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001835 redraw
1836
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001837 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001838 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001839endfunc
1840
1841func Test_popup_moved()
1842 new
1843 call test_override('char_avail', 1)
1844 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1845
1846 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001847 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001848 redraw
1849 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001850 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001851 " trigger the check for last_cursormoved by going into insert mode
1852 call feedkeys("li\<Esc>", 'xt')
1853 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001854 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001855
1856 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001857 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001858 redraw
1859 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001860 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001861 call feedkeys("hi\<Esc>", 'xt')
1862 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001863 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001864
1865 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001866 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001867 redraw
1868 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001869 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001870 call feedkeys("li\<Esc>", 'xt')
1871 call assert_equal(1, popup_getpos(winid).visible)
1872 call feedkeys("ei\<Esc>", 'xt')
1873 call assert_equal(1, popup_getpos(winid).visible)
1874 call feedkeys("eli\<Esc>", 'xt')
1875 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001876 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001877
Bram Moolenaar17627312019-06-02 19:53:44 +02001878 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001879 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001880 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001881 redraw
1882 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001883 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001884 call feedkeys("eli\<Esc>", 'xt')
1885 call assert_equal(1, popup_getpos(winid).visible)
1886 call feedkeys("wi\<Esc>", 'xt')
1887 call assert_equal(1, popup_getpos(winid).visible)
1888 call feedkeys("Eli\<Esc>", 'xt')
1889 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001890 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001891
1892 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001893 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001894 redraw
1895 call assert_equal(1, popup_getpos(winid).visible)
1896 call feedkeys("eli\<Esc>", 'xt')
1897 call feedkeys("ei\<Esc>", 'xt')
1898 call assert_equal(1, popup_getpos(winid).visible)
1899 call feedkeys("eli\<Esc>", 'xt')
1900 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001901 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001902
1903 bwipe!
1904 call test_override('ALL', 0)
1905endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001906
1907func Test_notifications()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001908 CheckFeature timers
1909 CheckScreendump
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001910
Bram Moolenaar0fdddee2019-09-01 15:26:23 +02001911 let lines =<< trim END
1912 call setline(1, range(1, 20))
1913 hi Notification ctermbg=lightblue
1914 call popup_notification('first notification', {})
1915 END
1916 call writefile(lines, 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001917 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001918 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1919
1920 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001921 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1922 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001923 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1924
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001925 " clean up
1926 call StopVimInTerminal(buf)
1927 call delete('XtestNotifications')
1928endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001929
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001930func Test_popup_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001931 CheckScreendump
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001932
1933 let lines =<< trim END
1934 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02001935 hi ScrollThumb ctermbg=blue
1936 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001937 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001938 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001939 \ minwidth: 8,
1940 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001941 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001942 func ScrollUp()
1943 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
1944 endfunc
1945 func ScrollDown()
1946 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
1947 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001948 func ClickTop()
1949 call feedkeys("\<F4>\<LeftMouse>", "xt")
1950 endfunc
1951 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001952 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001953 call feedkeys("\<F5>\<LeftMouse>", "xt")
1954 endfunc
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001955 func Popup_filter(winid, key)
1956 if a:key == 'j'
1957 let line = popup_getoptions(a:winid).firstline
1958 let nlines = line('$', a:winid)
1959 let newline = line < nlines ? (line + 1) : nlines
1960 call popup_setoptions(a:winid, #{firstline: newline})
1961 return v:true
1962 elseif a:key == 'x'
1963 call popup_close(a:winid)
1964 return v:true
1965 endif
1966 endfunc
1967
1968 func PopupScroll()
1969 call popup_clear()
1970 let text =<< trim END
1971 1
1972 2
1973 3
1974 4
1975 long line long line long line long line long line long line
1976 long line long line long line long line long line long line
1977 long line long line long line long line long line long line
1978 END
1979 call popup_create(text, #{
1980 \ minwidth: 30,
1981 \ maxwidth: 30,
1982 \ minheight: 4,
1983 \ maxheight: 4,
1984 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +01001985 \ lastline: 4,
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001986 \ wrap: v:true,
1987 \ scrollbar: v:true,
1988 \ mapping: v:false,
1989 \ filter: funcref('Popup_filter')
1990 \ })
1991 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001992 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001993 map <silent> <F4> :call test_setmouse(4, 42)<CR>
1994 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001995 END
1996 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001997 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001998 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
1999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002000 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002001 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002002 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
2003
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002004 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002005 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002006 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
2007
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002008 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002009 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002010 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
2011
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02002012 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb', firstline: 5})\<CR>")
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002013 " this scrolls two lines (half the window height)
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002014 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
2015 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
2016
2017 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2018 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
2019
2020 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02002021 " wait a bit, otherwise it fails sometimes (double click recognized?)
2022 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002023 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2024 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
2025
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002026 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2027 sleep 100m
2028 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2029 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
2030
2031 call term_sendkeys(buf, ":call ClickBot()\<CR>")
2032 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
2033
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002034 " remove the minwidth and maxheight
2035 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxheight: 0, minwidth: 0})\<CR>")
Bram Moolenaar7e0f4622019-09-17 21:23:39 +02002036 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002037 call VerifyScreenDump(buf, 'Test_popupwin_scroll_10', {})
2038
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002039 " check size with non-wrapping lines
2040 call term_sendkeys(buf, ":call PopupScroll()\<CR>")
2041 call VerifyScreenDump(buf, 'Test_popupwin_scroll_11', {})
2042
2043 " check size with wrapping lines
2044 call term_sendkeys(buf, "j")
2045 call VerifyScreenDump(buf, 'Test_popupwin_scroll_12', {})
2046 call term_sendkeys(buf, "x")
2047
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002048 " clean up
2049 call StopVimInTerminal(buf)
2050 call delete('XtestPopupScroll')
2051endfunc
2052
Bram Moolenaar437a7462019-07-05 20:17:22 +02002053func Test_popup_fitting_scrollbar()
2054 " this was causing a crash, divide by zero
2055 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002056 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002057 \ scrollbar: 1,
2058 \ maxwidth: 10,
2059 \ maxheight: 5,
2060 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02002061 redraw
2062 call popup_clear()
2063endfunc
2064
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002065func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002066 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002067
2068 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002069 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002070 let p = popup_create('test', opts)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002071 eval p->popup_settext('this is a text')
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002072 END
2073
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002074 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002075 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002076 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
2077
2078 " Setting to empty string clears it
2079 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
2080 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
2081
2082 " Setting a list
2083 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2084 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2085
2086 " Shrinking with a list
2087 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
2088 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
2089
2090 " Growing with a list
2091 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2092 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2093
2094 " Empty list clears
2095 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
2096 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
2097
2098 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002099 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002100 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
2101
2102 " clean up
2103 call StopVimInTerminal(buf)
2104 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002105endfunc
2106
2107func Test_popup_hidden()
2108 new
2109
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002110 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002111 redraw
2112 call assert_equal(0, popup_getpos(winid).visible)
2113 call popup_close(winid)
2114
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002115 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002116 redraw
2117 call assert_equal(0, popup_getpos(winid).visible)
2118 call popup_close(winid)
2119
2120 func QuitCallback(id, res)
2121 let s:cb_winid = a:id
2122 let s:cb_res = a:res
2123 endfunc
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002124 let winid = 'make a choice'->popup_dialog(#{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002125 \ filter: 'popup_filter_yesno',
2126 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002127 \ })
2128 redraw
2129 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02002130 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
2131 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002132 exe "normal anot used by filter\<Esc>"
2133 call assert_equal('not used by filter', getline(1))
2134
2135 call popup_show(winid)
2136 call feedkeys('y', "xt")
2137 call assert_equal(1, s:cb_res)
2138
2139 bwipe!
2140 delfunc QuitCallback
2141endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02002142
2143" Test options not checked elsewhere
2144func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002145 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002146 let options = popup_getoptions(winid)
2147 call assert_equal(1, options.wrap)
2148 call assert_equal(0, options.drag)
2149 call assert_equal('Beautiful', options.highlight)
2150
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002151 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002152 let options = popup_getoptions(winid)
2153 call assert_equal(0, options.wrap)
2154 call assert_equal(1, options.drag)
2155 call assert_equal('Another', options.highlight)
2156
2157 call popup_close(winid)
2158endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002159
2160func Test_popupwin_garbage_collect()
2161 func MyPopupFilter(x, winid, c)
2162 " NOP
2163 endfunc
2164
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002165 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002166 call test_garbagecollect_now()
2167 redraw
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02002168 " Must not crash caused by invalid memory access
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002169 call feedkeys('j', 'xt')
2170 call assert_true(v:true)
2171
2172 call popup_close(winid)
2173 delfunc MyPopupFilter
2174endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002175
Bram Moolenaar581ba392019-09-03 22:08:33 +02002176func Test_popupwin_filter_mode()
2177 func MyPopupFilter(winid, c)
2178 let s:typed = a:c
2179 if a:c == ':' || a:c == "\r" || a:c == 'v'
2180 " can start cmdline mode, get out, and start/stop Visual mode
2181 return 0
2182 endif
2183 return 1
2184 endfunc
2185
2186 " Normal, Visual and Insert mode
2187 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'nvi'})
2188 redraw
2189 call feedkeys('x', 'xt')
2190 call assert_equal('x', s:typed)
2191
2192 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2193 call assert_equal(':', s:typed)
2194 call assert_equal('foo', g:foo)
2195
2196 let @x = 'something'
2197 call feedkeys('v$"xy', 'xt')
2198 call assert_equal('y', s:typed)
2199 call assert_equal('something', @x) " yank command is filtered out
2200 call feedkeys('v', 'xt') " end Visual mode
2201
2202 call popup_close(winid)
2203
2204 " only Normal mode
2205 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'n'})
2206 redraw
2207 call feedkeys('x', 'xt')
2208 call assert_equal('x', s:typed)
2209
2210 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2211 call assert_equal(':', s:typed)
2212 call assert_equal('foo', g:foo)
2213
2214 let @x = 'something'
2215 call feedkeys('v$"xy', 'xt')
2216 call assert_equal('v', s:typed)
2217 call assert_notequal('something', @x)
2218
2219 call popup_close(winid)
2220
2221 " default: all modes
2222 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
2223 redraw
2224 call feedkeys('x', 'xt')
2225 call assert_equal('x', s:typed)
2226
2227 let g:foo = 'bar'
2228 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2229 call assert_equal("\r", s:typed)
2230 call assert_equal('bar', g:foo)
2231
2232 let @x = 'something'
2233 call feedkeys('v$"xy', 'xt')
2234 call assert_equal('y', s:typed)
2235 call assert_equal('something', @x) " yank command is filtered out
2236 call feedkeys('v', 'xt') " end Visual mode
2237
2238 call popup_close(winid)
2239 delfunc MyPopupFilter
2240endfunc
2241
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002242func Test_popupwin_filter_mouse()
2243 func MyPopupFilter(winid, c)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002244 let g:got_mousepos = getmousepos()
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002245 return 0
2246 endfunc
2247
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002248 call setline(1, ['.'->repeat(25)]->repeat(10))
2249 let winid = popup_create(['short', 'long line that will wrap', 'other'], #{
2250 \ line: 2,
2251 \ col: 4,
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002252 \ maxwidth: 12,
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002253 \ padding: [],
2254 \ border: [],
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002255 \ filter: 'MyPopupFilter',
2256 \ })
2257 redraw
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002258 " 123456789012345678901
2259 " 1 .....................
2260 " 2 ...+--------------+..
2261 " 3 ...| |..
2262 " 4 ...| short |..
2263 " 5 ...| long line th |..
2264 " 6 ...| at will wrap |..
2265 " 7 ...| other |..
2266 " 8 ...| |..
2267 " 9 ...+--------------+..
2268 " 10 .....................
2269 let tests = []
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002270
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002271 func AddItemOutsidePopup(tests, row, col)
2272 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2273 \ screenrow: a:row, screencol: a:col,
2274 \ winid: win_getid(), winrow: a:row, wincol: a:col,
2275 \ line: a:row, column: a:col,
2276 \ }})
2277 endfunc
2278 func AddItemInPopupBorder(tests, winid, row, col)
2279 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2280 \ screenrow: a:row, screencol: a:col,
2281 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2282 \ line: 0, column: 0,
2283 \ }})
2284 endfunc
2285 func AddItemInPopupText(tests, winid, row, col, textline, textcol)
2286 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2287 \ screenrow: a:row, screencol: a:col,
2288 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2289 \ line: a:textline, column: a:textcol,
2290 \ }})
2291 endfunc
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002292
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002293 " above and below popup
2294 for c in range(1, 21)
2295 call AddItemOutsidePopup(tests, 1, c)
2296 call AddItemOutsidePopup(tests, 10, c)
2297 endfor
2298 " left and right of popup
2299 for r in range(1, 10)
2300 call AddItemOutsidePopup(tests, r, 3)
2301 call AddItemOutsidePopup(tests, r, 20)
2302 endfor
2303 " top and bottom in popup
2304 for c in range(4, 19)
2305 call AddItemInPopupBorder(tests, winid, 2, c)
2306 call AddItemInPopupBorder(tests, winid, 3, c)
2307 call AddItemInPopupBorder(tests, winid, 8, c)
2308 call AddItemInPopupBorder(tests, winid, 9, c)
2309 endfor
2310 " left and right margin in popup
2311 for r in range(2, 9)
2312 call AddItemInPopupBorder(tests, winid, r, 4)
2313 call AddItemInPopupBorder(tests, winid, r, 5)
2314 call AddItemInPopupBorder(tests, winid, r, 18)
2315 call AddItemInPopupBorder(tests, winid, r, 19)
2316 endfor
2317 " text "short"
2318 call AddItemInPopupText(tests, winid, 4, 6, 1, 1)
2319 call AddItemInPopupText(tests, winid, 4, 10, 1, 5)
2320 call AddItemInPopupText(tests, winid, 4, 11, 1, 6)
2321 call AddItemInPopupText(tests, winid, 4, 17, 1, 6)
2322 " text "long line th"
2323 call AddItemInPopupText(tests, winid, 5, 6, 2, 1)
2324 call AddItemInPopupText(tests, winid, 5, 10, 2, 5)
2325 call AddItemInPopupText(tests, winid, 5, 17, 2, 12)
2326 " text "at will wrap"
2327 call AddItemInPopupText(tests, winid, 6, 6, 2, 13)
2328 call AddItemInPopupText(tests, winid, 6, 10, 2, 17)
2329 call AddItemInPopupText(tests, winid, 6, 17, 2, 24)
2330 " text "other"
2331 call AddItemInPopupText(tests, winid, 7, 6, 3, 1)
2332 call AddItemInPopupText(tests, winid, 7, 10, 3, 5)
2333 call AddItemInPopupText(tests, winid, 7, 11, 3, 6)
2334 call AddItemInPopupText(tests, winid, 7, 17, 3, 6)
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002335
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002336 for item in tests
2337 call test_setmouse(item.clickrow, item.clickcol)
2338 call feedkeys("\<LeftMouse>", 'xt')
2339 call assert_equal(item.result, g:got_mousepos)
2340 endfor
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002341
2342 call popup_close(winid)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002343 enew!
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002344 delfunc MyPopupFilter
2345endfunc
2346
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002347func Test_popupwin_with_buffer()
2348 call writefile(['some text', 'in a buffer'], 'XsomeFile')
2349 let buf = bufadd('XsomeFile')
2350 call assert_equal(0, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002351
2352 setlocal number
2353 call setbufvar(buf, "&wrapmargin", 13)
2354
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002355 let winid = popup_create(buf, {})
2356 call assert_notequal(0, winid)
2357 let pos = popup_getpos(winid)
2358 call assert_equal(2, pos.height)
2359 call assert_equal(1, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002360
2361 " window-local option is set to default, buffer-local is not
2362 call assert_equal(0, getwinvar(winid, '&number'))
2363 call assert_equal(13, getbufvar(buf, '&wrapmargin'))
2364
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002365 call popup_close(winid)
2366 call assert_equal({}, popup_getpos(winid))
2367 call assert_equal(1, bufloaded(buf))
2368 exe 'bwipe! ' .. buf
Bram Moolenaar46451042019-08-24 15:50:46 +02002369 setlocal nonumber
Bram Moolenaar7866b872019-07-01 22:21:01 +02002370
2371 edit test_popupwin.vim
2372 let winid = popup_create(bufnr(''), {})
2373 redraw
2374 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002375 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002376endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02002377
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002378func Test_popupwin_terminal_buffer()
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002379 CheckFeature terminal
2380
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002381 let ptybuf = term_start(&shell, #{hidden: 1})
2382 call assert_fails('let winnr = popup_create(ptybuf, #{})', 'E278:')
2383 exe 'bwipe! ' .. ptybuf
2384endfunc
2385
Bram Moolenaar934470e2019-09-01 23:27:05 +02002386func Test_popupwin_with_buffer_and_filter()
2387 new Xwithfilter
2388 call setline(1, range(100))
2389 let bufnr = bufnr()
2390 hide
2391
2392 func BufferFilter(win, key)
2393 if a:key == 'G'
2394 " recursive use of "G" does not cause problems.
2395 call win_execute(a:win, 'normal! G')
2396 return 1
2397 endif
2398 return 0
2399 endfunc
2400
2401 let winid = popup_create(bufnr, #{maxheight: 5, filter: 'BufferFilter'})
2402 call assert_equal(1, popup_getpos(winid).firstline)
2403 redraw
2404 call feedkeys("G", 'xt')
2405 call assert_equal(99, popup_getpos(winid).firstline)
2406
2407 call popup_close(winid)
2408 exe 'bwipe! ' .. bufnr
2409endfunc
2410
Bram Moolenaare296e312019-07-03 23:20:18 +02002411func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002412 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002413 \ maxwidth: 40,
2414 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02002415 \ })
2416 for top in range(1, 20)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002417 eval winid->popup_setoptions(#{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02002418 redraw
2419 call assert_equal(19, popup_getpos(winid).width)
2420 endfor
2421 call popup_clear()
2422endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02002423
2424func Test_popupwin_buf_close()
2425 let buf = bufadd('Xtestbuf')
2426 call bufload(buf)
2427 call setbufline(buf, 1, ['just', 'some', 'lines'])
2428 let winid = popup_create(buf, {})
2429 redraw
2430 call assert_equal(3, popup_getpos(winid).height)
2431 let bufinfo = getbufinfo(buf)[0]
2432 call assert_equal(1, bufinfo.changed)
2433 call assert_equal(0, bufinfo.hidden)
2434 call assert_equal(0, bufinfo.listed)
2435 call assert_equal(1, bufinfo.loaded)
2436 call assert_equal([], bufinfo.windows)
2437 call assert_equal([winid], bufinfo.popups)
2438
2439 call popup_close(winid)
2440 call assert_equal({}, popup_getpos(winid))
2441 let bufinfo = getbufinfo(buf)[0]
2442 call assert_equal(1, bufinfo.changed)
2443 call assert_equal(1, bufinfo.hidden)
2444 call assert_equal(0, bufinfo.listed)
2445 call assert_equal(1, bufinfo.loaded)
2446 call assert_equal([], bufinfo.windows)
2447 call assert_equal([], bufinfo.popups)
2448 exe 'bwipe! ' .. buf
2449endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02002450
2451func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002452 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02002453
2454 let lines =<< trim END
2455 call setline(1, range(1, 10))
2456 hi ScrollThumb ctermbg=blue
2457 hi ScrollBar ctermbg=red
2458 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002459 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002460 \ maxwidth: 10,
2461 \ maxheight: 3,
2462 \ pos : 'topleft',
2463 \ col : a:col,
2464 \ line : a:line,
2465 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02002466 \ })
2467 endfunc
2468 call PopupMenu(['x'], 1, 1)
2469 call PopupMenu(['123456789|'], 1, 16)
2470 call PopupMenu(['123456789|' .. ' '], 7, 1)
2471 call PopupMenu([repeat('123456789|', 100)], 7, 16)
2472 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
2473 END
2474 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002475 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02002476 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
2477
2478 " close the menu popupwin.
2479 call term_sendkeys(buf, " ")
2480 call term_sendkeys(buf, " ")
2481 call term_sendkeys(buf, " ")
2482 call term_sendkeys(buf, " ")
2483 call term_sendkeys(buf, " ")
2484
2485 " clean up
2486 call StopVimInTerminal(buf)
2487 call delete('XtestPopupMenuMaxWidth')
2488endfunc
2489
Bram Moolenaara901a372019-07-13 16:38:50 +02002490func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002491 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02002492
2493 let lines =<< trim END
2494 call setline(1, range(1, 20))
2495 hi ScrollThumb ctermbg=blue
2496 hi ScrollBar ctermbg=red
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002497 eval ['one', 'two', 'three', 'four', 'five',
2498 \ 'six', 'seven', 'eight', 'nine']
2499 \ ->popup_menu(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002500 \ minwidth: 8,
2501 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02002502 \ })
2503 END
2504 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002505 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02002506
2507 call term_sendkeys(buf, "j")
2508 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
2509
2510 call term_sendkeys(buf, "jjj")
2511 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
2512
2513 " if the cursor is the bottom line, it stays at the bottom line.
2514 call term_sendkeys(buf, repeat("j", 20))
2515 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
2516
2517 call term_sendkeys(buf, "kk")
2518 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
2519
2520 call term_sendkeys(buf, "k")
2521 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
2522
2523 " if the cursor is in the top line, it stays in the top line.
2524 call term_sendkeys(buf, repeat("k", 20))
2525 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
2526
2527 " close the menu popupwin.
2528 call term_sendkeys(buf, " ")
2529
2530 " clean up
2531 call StopVimInTerminal(buf)
2532 call delete('XtestPopupMenuScroll')
2533endfunc
2534
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002535func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002536 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002537
2538 let lines =<< trim END
2539 function! MyFilter(winid, key) abort
2540 if a:key == "0"
2541 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
2542 return 1
2543 endif
2544 if a:key == "G"
2545 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
2546 return 1
2547 endif
2548 if a:key == "j"
2549 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
2550 return 1
2551 endif
2552 if a:key == "k"
2553 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
2554 return 1
2555 endif
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002556 if a:key == ':'
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002557 call popup_close(a:winid)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002558 return 0
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002559 endif
2560 return 0
2561 endfunction
2562 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
2563 \ maxheight : 3,
2564 \ filter : 'MyFilter'
2565 \ })
2566 END
2567 call writefile(lines, 'XtestPopupMenuFilter')
2568 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
2569
2570 call term_sendkeys(buf, "j")
2571 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
2572
2573 call term_sendkeys(buf, "k")
2574 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
2575
2576 call term_sendkeys(buf, "G")
2577 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
2578
2579 call term_sendkeys(buf, "0")
2580 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
2581
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002582 " check that when the popup is closed in the filter the screen is redrawn
2583 call term_sendkeys(buf, ":")
2584 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_5', {})
2585 call term_sendkeys(buf, "\<CR>")
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002586
2587 " clean up
2588 call StopVimInTerminal(buf)
2589 call delete('XtestPopupMenuFilter')
2590endfunc
2591
2592func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002593 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002594
2595 let winid = popup_create('some text', {})
2596 call assert_equal(0, popup_getoptions(winid).cursorline)
2597 call popup_close(winid)
2598
2599 let winid = popup_create('some text', #{ cursorline: 1, })
2600 call assert_equal(1, popup_getoptions(winid).cursorline)
2601 call popup_close(winid)
2602
2603 let winid = popup_create('some text', #{ cursorline: 0, })
2604 call assert_equal(0, popup_getoptions(winid).cursorline)
2605 call popup_close(winid)
2606
2607 let winid = popup_menu('some text', {})
2608 call assert_equal(1, popup_getoptions(winid).cursorline)
2609 call popup_close(winid)
2610
2611 let winid = popup_menu('some text', #{ cursorline: 1, })
2612 call assert_equal(1, popup_getoptions(winid).cursorline)
2613 call popup_close(winid)
2614
2615 let winid = popup_menu('some text', #{ cursorline: 0, })
2616 call assert_equal(0, popup_getoptions(winid).cursorline)
2617 call popup_close(winid)
2618
2619 " ---------
2620 " Pattern 1
2621 " ---------
2622 let lines =<< trim END
2623 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
2624 END
2625 call writefile(lines, 'XtestPopupCursorLine')
2626 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2627 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
2628 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2629 call StopVimInTerminal(buf)
2630
2631 " ---------
2632 " Pattern 2
2633 " ---------
2634 let lines =<< trim END
2635 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
2636 END
2637 call writefile(lines, 'XtestPopupCursorLine')
2638 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2639 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
2640 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2641 call StopVimInTerminal(buf)
2642
2643 " ---------
2644 " Pattern 3
2645 " ---------
2646 let lines =<< trim END
2647 function! MyFilter(winid, key) abort
2648 if a:key == "j"
2649 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2650 return 1
2651 endif
2652 if a:key == 'x'
2653 call popup_close(a:winid)
2654 return 1
2655 endif
2656 return 0
2657 endfunction
2658 call popup_menu(['111', '222', '333'], #{
2659 \ cursorline : 0,
2660 \ maxheight : 2,
2661 \ filter : 'MyFilter',
2662 \ })
2663 END
2664 call writefile(lines, 'XtestPopupCursorLine')
2665 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2666 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
2667 call term_sendkeys(buf, "j")
2668 call term_sendkeys(buf, "j")
2669 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
2670 call term_sendkeys(buf, "x")
2671 call StopVimInTerminal(buf)
2672
2673 " ---------
2674 " Pattern 4
2675 " ---------
2676 let lines =<< trim END
2677 function! MyFilter(winid, key) abort
2678 if a:key == "j"
2679 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2680 return 1
2681 endif
2682 if a:key == 'x'
2683 call popup_close(a:winid)
2684 return 1
2685 endif
2686 return 0
2687 endfunction
2688 call popup_menu(['111', '222', '333'], #{
2689 \ cursorline : 1,
2690 \ maxheight : 2,
2691 \ filter : 'MyFilter',
2692 \ })
2693 END
2694 call writefile(lines, 'XtestPopupCursorLine')
2695 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2696 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
2697 call term_sendkeys(buf, "j")
2698 call term_sendkeys(buf, "j")
2699 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
2700 call term_sendkeys(buf, "x")
2701 call StopVimInTerminal(buf)
2702
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02002703 " ---------
2704 " Cursor in second line when creating the popup
2705 " ---------
2706 let lines =<< trim END
2707 let winid = popup_create(['111', '222', '333'], #{
2708 \ cursorline : 1,
2709 \ })
2710 call win_execute(winid, "2")
2711 END
2712 call writefile(lines, 'XtestPopupCursorLine')
2713 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2714 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_7', {})
2715 call StopVimInTerminal(buf)
2716
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002717 call delete('XtestPopupCursorLine')
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01002718
2719 " ---------
2720 " Use current buffer for popupmenu
2721 " ---------
2722 let lines =<< trim END
2723 call setline(1, ['one', 'two', 'three'])
2724 let winid = popup_create(bufnr('%'), #{
2725 \ cursorline : 1,
2726 \ })
2727 call win_execute(winid, "2")
2728 END
2729 call writefile(lines, 'XtestPopupCursorLine')
2730 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2731 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_8', {})
2732 call StopVimInTerminal(buf)
2733
2734 call delete('XtestPopupCursorLine')
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002735endfunc
2736
Bram Moolenaarf914a332019-07-20 15:09:56 +02002737func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002738 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002739 CheckFeature quickfix
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002740
Bram Moolenaarf914a332019-07-20 15:09:56 +02002741 call writefile([
2742 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
2743 \ "another\tXtagfile\t/^this is another",
2744 \ "theword\tXtagfile\t/^theword"],
2745 \ 'Xtags')
2746 call writefile(range(1,20)
2747 \ + ['theword is here']
2748 \ + range(22, 27)
2749 \ + ['this is another place']
2750 \ + range(29, 40),
2751 \ "Xtagfile")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002752 call writefile(range(1,10)
2753 \ + ['searched word is here']
2754 \ + range(12, 20),
2755 \ "Xheader.h")
Bram Moolenaarf914a332019-07-20 15:09:56 +02002756 let lines =<< trim END
2757 set tags=Xtags
2758 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002759 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002760 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002761 \ 'three',
2762 \ 'four',
2763 \ 'five',
2764 \ 'six',
2765 \ 'seven',
2766 \ 'find theword somewhere',
2767 \ 'nine',
2768 \ 'this is another word',
2769 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02002770 set previewpopup=height:4,width:40
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002771 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02002772 END
2773 call writefile(lines, 'XtestPreviewPopup')
2774 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
2775
2776 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
2777 call term_sendkeys(buf, ":\<CR>")
2778 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
2779
2780 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2781 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
2782
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002783 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002784 call term_sendkeys(buf, ":\<CR>")
2785 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
2786
2787 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2788 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
2789
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002790 call term_sendkeys(buf, ":cd ..\<CR>:\<CR>")
2791 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002792 call term_sendkeys(buf, ":cd testdir\<CR>")
2793
2794 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02002795 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002796 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
2797
2798 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
2799 call term_sendkeys(buf, ":\<CR>")
2800 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
2801
2802 call term_sendkeys(buf, ":pclose\<CR>")
2803 call term_sendkeys(buf, ":psearch searched\<CR>")
2804 call term_sendkeys(buf, ":\<CR>")
2805 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002806
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01002807 call term_sendkeys(buf, "\<C-W>p")
2808 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_9', {})
2809
2810 call term_sendkeys(buf, ":call win_execute(popup_findpreview(), 'call popup_clear()')\<CR>")
2811 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_10', {})
2812
Bram Moolenaarf914a332019-07-20 15:09:56 +02002813 call StopVimInTerminal(buf)
2814 call delete('Xtags')
2815 call delete('Xtagfile')
2816 call delete('XtestPreviewPopup')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002817 call delete('Xheader.h')
Bram Moolenaarf914a332019-07-20 15:09:56 +02002818endfunc
2819
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002820func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002821 let lines =<< trim END
2822 set completeopt+=preview,popup
2823 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002824 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002825
2826 func CompleteFuncDict(findstart, base)
2827 if a:findstart
2828 if col('.') > 10
2829 return col('.') - 10
2830 endif
2831 return 0
2832 endif
2833
2834 return {
2835 \ 'words': [
2836 \ {
2837 \ 'word': 'aword',
2838 \ 'abbr': 'wrd',
2839 \ 'menu': 'extra text',
2840 \ 'info': 'words are cool',
2841 \ 'kind': 'W',
2842 \ 'user_data': 'test'
2843 \ },
2844 \ {
2845 \ 'word': 'anotherword',
2846 \ 'abbr': 'anotwrd',
2847 \ 'menu': 'extra text',
2848 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
2849 \ 'kind': 'W',
2850 \ 'user_data': 'notest'
2851 \ },
2852 \ {
2853 \ 'word': 'noinfo',
2854 \ 'abbr': 'noawrd',
2855 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002856 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002857 \ 'kind': 'W',
2858 \ 'user_data': 'notest'
2859 \ },
2860 \ {
2861 \ 'word': 'thatword',
2862 \ 'abbr': 'thatwrd',
2863 \ 'menu': 'extra text',
2864 \ 'info': 'that word is cool',
2865 \ 'kind': 'W',
2866 \ 'user_data': 'notest'
2867 \ },
2868 \ ]
2869 \ }
2870 endfunc
2871 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002872 func ChangeColor()
2873 let id = popup_findinfo()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002874 eval id->popup_setoptions(#{highlight: 'InfoPopup'})
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002875 endfunc
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002876
2877 func InfoHidden()
2878 set completepopup=height:4,border:off,align:menu
2879 set completeopt-=popup completeopt+=popuphidden
2880 au CompleteChanged * call HandleChange()
2881 endfunc
2882
2883 let s:counter = 0
2884 func HandleChange()
2885 let s:counter += 1
2886 let selected = complete_info(['selected']).selected
2887 if selected <= 0
2888 " First time: do nothing, info remains hidden
2889 return
2890 endif
2891 if selected == 1
2892 " Second time: show info right away
2893 let id = popup_findinfo()
2894 if id
2895 call popup_settext(id, 'immediate info ' .. s:counter)
2896 call popup_show(id)
2897 endif
2898 else
2899 " Third time: show info after a short delay
2900 call timer_start(100, 'ShowInfo')
2901 endif
2902 endfunc
2903
2904 func ShowInfo(...)
2905 let id = popup_findinfo()
2906 if id
2907 call popup_settext(id, 'async info ' .. s:counter)
2908 call popup_show(id)
2909 endif
2910 endfunc
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002911 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002912 return lines
2913endfunc
2914
2915func Test_popupmenu_info_border()
2916 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002917 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002918
2919 let lines = Get_popupmenu_lines()
2920 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002921 call writefile(lines, 'XtestInfoPopup')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002922
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002923 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
2924 call term_wait(buf, 50)
2925
2926 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2927 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
2928
2929 call term_sendkeys(buf, "\<C-N>")
2930 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
2931
2932 call term_sendkeys(buf, "\<C-N>")
2933 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
2934
2935 call term_sendkeys(buf, "\<C-N>\<C-N>")
2936 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
2937
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02002938 " info on the left with scrollbar
2939 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
2940 call term_sendkeys(buf, "\<C-N>\<C-N>")
2941 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
2942
Bram Moolenaar202c3f72019-11-21 12:12:35 +01002943 " Test that the popupmenu's scrollbar and infopopup do not overlap
2944 call term_sendkeys(buf, "\<Esc>")
2945 call term_sendkeys(buf, ":set pumheight=3\<CR>")
2946 call term_sendkeys(buf, "cc\<C-X>\<C-U>")
2947 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_6', {})
2948
Bram Moolenaarca7c0782020-01-14 20:42:48 +01002949 " Hide the info popup, cycle trough buffers, make sure it didn't get
2950 " deleted.
2951 call term_sendkeys(buf, "\<Esc>")
2952 call term_sendkeys(buf, ":set hidden\<CR>")
2953 call term_sendkeys(buf, ":bn\<CR>")
2954 call term_sendkeys(buf, ":bn\<CR>")
2955 call term_sendkeys(buf, "otest text test text\<C-X>\<C-U>")
2956 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_7', {})
2957
2958 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002959 call StopVimInTerminal(buf)
2960 call delete('XtestInfoPopup')
2961endfunc
2962
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002963func Test_popupmenu_info_noborder()
2964 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002965 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002966
2967 let lines = Get_popupmenu_lines()
2968 call add(lines, 'set completepopup=height:4,border:off')
2969 call writefile(lines, 'XtestInfoPopupNb')
2970
2971 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
2972 call term_wait(buf, 50)
2973
2974 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2975 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
2976
2977 call StopVimInTerminal(buf)
2978 call delete('XtestInfoPopupNb')
2979endfunc
2980
Bram Moolenaar258cef52019-08-21 17:29:29 +02002981func Test_popupmenu_info_align_menu()
2982 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002983 CheckFeature quickfix
Bram Moolenaar258cef52019-08-21 17:29:29 +02002984
2985 let lines = Get_popupmenu_lines()
2986 call add(lines, 'set completepopup=height:4,border:off,align:menu')
2987 call writefile(lines, 'XtestInfoPopupNb')
2988
2989 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
2990 call term_wait(buf, 50)
2991
2992 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2993 call term_sendkeys(buf, "\<C-N>")
2994 call term_sendkeys(buf, "\<C-N>")
2995 call term_sendkeys(buf, "\<C-N>")
2996 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
2997
2998 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
2999 call term_sendkeys(buf, "\<C-N>")
3000 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
3001
3002 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003003 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02003004 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
3005 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
3006 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
3007
3008 call StopVimInTerminal(buf)
3009 call delete('XtestInfoPopupNb')
3010endfunc
3011
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003012func Test_popupmenu_info_hidden()
3013 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003014 CheckFeature quickfix
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003015
3016 let lines = Get_popupmenu_lines()
3017 call add(lines, 'call InfoHidden()')
3018 call writefile(lines, 'XtestInfoPopupHidden')
3019
3020 let buf = RunVimInTerminal('-S XtestInfoPopupHidden', #{rows: 14})
3021 call term_wait(buf, 50)
3022
3023 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3024 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_1', {})
3025
3026 call term_sendkeys(buf, "\<C-N>")
3027 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_2', {})
3028
3029 call term_sendkeys(buf, "\<C-N>")
3030 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_3', {})
3031
3032 call term_sendkeys(buf, "\<Esc>")
3033 call StopVimInTerminal(buf)
3034 call delete('XtestInfoPopupHidden')
3035endfunc
3036
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003037func Test_popupmenu_info_too_wide()
3038 CheckScreendump
3039 CheckFeature quickfix
3040
3041 let lines =<< trim END
3042 call setline(1, range(10))
3043
3044 set completeopt+=preview,popup
3045 set completepopup=align:menu
3046 set omnifunc=OmniFunc
3047 hi InfoPopup ctermbg=lightgrey
3048
3049 func OmniFunc(findstart, base)
3050 if a:findstart
3051 return 0
3052 endif
3053
3054 let menuText = 'some long text to make sure the menu takes up all of the width of the window'
3055 return #{
3056 \ words: [
3057 \ #{
3058 \ word: 'scrap',
3059 \ menu: menuText,
3060 \ info: "other words are\ncooler than this and some more text\nto make wrap",
3061 \ },
3062 \ #{
3063 \ word: 'scappier',
3064 \ menu: menuText,
3065 \ info: 'words are cool',
3066 \ },
3067 \ #{
3068 \ word: 'scrappier2',
3069 \ menu: menuText,
3070 \ info: 'words are cool',
3071 \ },
3072 \ ]
3073 \ }
3074 endfunc
3075 END
3076
3077 call writefile(lines, 'XtestInfoPopupWide')
3078 let buf = RunVimInTerminal('-S XtestInfoPopupWide', #{rows: 8})
3079 call term_wait(buf, 50)
3080
3081 call term_sendkeys(buf, "Ascr\<C-X>\<C-O>")
3082 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_wide_1', {})
3083
3084 call term_sendkeys(buf, "\<Esc>")
3085 call StopVimInTerminal(buf)
3086 call delete('XtestInfoPopupWide')
3087endfunc
3088
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003089func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02003090 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003091 call popup_clear()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003092 let winid = 'nothing wrong'->popup_notification({})
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003093 call assert_equal(bufnr, winbufnr(winid))
3094 call popup_clear()
3095endfunc
3096
Bram Moolenaar1824f452019-10-02 23:06:46 +02003097func Test_popupwin_getoptions_tablocal()
3098 topleft split
3099 let win1 = popup_create('nothing', #{maxheight: 8})
3100 let win2 = popup_create('something', #{maxheight: 10})
3101 let win3 = popup_create('something', #{maxheight: 15})
3102 call assert_equal(8, popup_getoptions(win1).maxheight)
3103 call assert_equal(10, popup_getoptions(win2).maxheight)
3104 call assert_equal(15, popup_getoptions(win3).maxheight)
3105 call popup_clear()
3106 quit
3107endfunc
3108
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02003109func Test_popupwin_cancel()
3110 let win1 = popup_create('one', #{line: 5, filter: {... -> 0}})
3111 let win2 = popup_create('two', #{line: 10, filter: {... -> 0}})
3112 let win3 = popup_create('three', #{line: 15, filter: {... -> 0}})
3113 call assert_equal(5, popup_getpos(win1).line)
3114 call assert_equal(10, popup_getpos(win2).line)
3115 call assert_equal(15, popup_getpos(win3).line)
3116 " TODO: this also works without patch 8.1.2110
3117 call feedkeys("\<C-C>", 'xt')
3118 call assert_equal(5, popup_getpos(win1).line)
3119 call assert_equal(10, popup_getpos(win2).line)
3120 call assert_equal({}, popup_getpos(win3))
3121 call feedkeys("\<C-C>", 'xt')
3122 call assert_equal(5, popup_getpos(win1).line)
3123 call assert_equal({}, popup_getpos(win2))
3124 call assert_equal({}, popup_getpos(win3))
3125 call feedkeys("\<C-C>", 'xt')
3126 call assert_equal({}, popup_getpos(win1))
3127 call assert_equal({}, popup_getpos(win2))
3128 call assert_equal({}, popup_getpos(win3))
3129endfunc
3130
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003131func Test_popupwin_filter_redraw()
3132 " Create two popups with a filter that closes the popup when typing "0".
3133 " Both popups should close, even though the redraw also calls
3134 " popup_reset_handled()
3135
3136 func CloseFilter(winid, key)
3137 if a:key == '0'
3138 call popup_close(a:winid)
3139 redraw
3140 endif
3141 return 0 " pass the key
3142 endfunc
3143
3144 let id1 = popup_create('first one', #{
3145 \ line: 1,
3146 \ col: 1,
3147 \ filter: 'CloseFilter',
3148 \ })
3149 let id2 = popup_create('second one', #{
3150 \ line: 9,
3151 \ col: 1,
3152 \ filter: 'CloseFilter',
3153 \ })
3154 call assert_equal(1, popup_getpos(id1).line)
3155 call assert_equal(9, popup_getpos(id2).line)
3156
3157 call feedkeys('0', 'xt')
3158 call assert_equal({}, popup_getpos(id1))
3159 call assert_equal({}, popup_getpos(id2))
3160
3161 call popup_clear()
3162 delfunc CloseFilter
3163endfunc
3164
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003165func Test_popupwin_double_width()
3166 CheckScreendump
3167
3168 let lines =<< trim END
3169 call setline(1, 'x你好世界你好世你好世界你好')
3170 call setline(2, '你好世界你好世你好世界你好')
3171 call setline(3, 'x你好世界你好世你好世界你好')
3172 call popup_create('你好,世界 - 你好,世界xxxxx', #{line: 1, col: 3, maxwidth: 14})
3173 END
3174 call writefile(lines, 'XtestPopupWide')
3175
3176 let buf = RunVimInTerminal('-S XtestPopupWide', #{rows: 10})
3177 call VerifyScreenDump(buf, 'Test_popupwin_doublewidth_1', {})
3178
3179 call StopVimInTerminal(buf)
3180 call delete('XtestPopupWide')
3181endfunc
3182
3183func Test_popupwin_sign()
3184 CheckScreendump
3185
3186 let lines =<< trim END
3187 call setline(1, range(10))
3188 call sign_define('Current', {
3189 \ 'text': '>>',
3190 \ 'texthl': 'WarningMsg',
3191 \ 'linehl': 'Error',
3192 \ })
3193 call sign_define('Other', {
3194 \ 'text': '#!',
3195 \ 'texthl': 'Error',
3196 \ 'linehl': 'Search',
3197 \ })
3198 let winid = popup_create(['hello', 'bright', 'world'], {
3199 \ 'minwidth': 20,
3200 \ })
3201 call setwinvar(winid, "&signcolumn", "yes")
3202 let winbufnr = winbufnr(winid)
3203
3204 " add sign to current buffer, shows
3205 call sign_place(1, 'Selected', 'Current', bufnr('%'), {'lnum': 1})
3206 " add sign to current buffer, does not show
3207 call sign_place(2, 'PopUpSelected', 'Other', bufnr('%'), {'lnum': 2})
3208
3209 " add sign to popup buffer, shows
3210 call sign_place(3, 'PopUpSelected', 'Other', winbufnr, {'lnum': 1})
3211 " add sign to popup buffer, does not show
3212 call sign_place(4, 'Selected', 'Current', winbufnr, {'lnum': 2})
3213 END
3214 call writefile(lines, 'XtestPopupSign')
3215
3216 let buf = RunVimInTerminal('-S XtestPopupSign', #{rows: 10})
3217 call VerifyScreenDump(buf, 'Test_popupwin_sign_1', {})
3218
3219 call StopVimInTerminal(buf)
3220 call delete('XtestPopupSign')
3221endfunc
3222
Bram Moolenaar99ebf222019-12-10 23:44:48 +01003223func Test_popupwin_bufnr()
3224 let popwin = popup_create(['blah'], #{})
3225 let popbuf = winbufnr(popwin)
3226 split asdfasdf
3227 let newbuf = bufnr()
3228 call assert_true(newbuf > popbuf, 'New buffer number is higher')
3229 call assert_equal(newbuf, bufnr('$'))
3230 call popup_clear()
3231 let popwin = popup_create(['blah'], #{})
3232 " reuses previous buffer number
3233 call assert_equal(popbuf, winbufnr(popwin))
3234 call assert_equal(newbuf, bufnr('$'))
3235
3236 call popup_clear()
3237 bwipe!
3238endfunc
3239
Bram Moolenaar331bafd2019-07-20 17:46:05 +02003240" vim: shiftwidth=2 sts=2