blob: 478e9d49e95482005b60cfdef8e076f0ce8eea0b [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001" Tests for popup windows
2
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004CheckFeature popupwin
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005
6source screendump.vim
7
8func Test_simple_popup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02009 CheckScreendump
10
Bram Moolenaare7eb9272019-06-24 00:58:07 +020011 let lines =<< trim END
12 call setline(1, range(1, 100))
13 hi PopupColor1 ctermbg=lightblue
14 hi PopupColor2 ctermbg=lightcyan
Bram Moolenaar1666ac92019-11-10 17:22:31 +010015 hi EndOfBuffer ctermbg=lightgrey
Bram Moolenaare7eb9272019-06-24 00:58:07 +020016 hi Comment ctermfg=red
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020017 call prop_type_add('comment', #{highlight: 'Comment'})
18 let winid = popup_create('hello there', #{line: 3, col: 11, minwidth: 20, highlight: 'PopupColor1'})
19 let winid2 = popup_create(['another one', 'another two', 'another three'], #{line: 3, col: 25, minwidth: 20})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020020 call setwinvar(winid2, '&wincolor', 'PopupColor2')
21 END
22 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020023 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar4d784b22019-05-25 19:51:39 +020024 call VerifyScreenDump(buf, 'Test_popupwin_01', {})
25
Bram Moolenaarec583842019-05-26 14:11:23 +020026 " Add a tabpage
27 call term_sendkeys(buf, ":tabnew\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020028 call term_sendkeys(buf, ":let popupwin = popup_create(["
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020029 \ .. "#{text: 'other tab'},"
30 \ .. "#{text: 'a comment line', props: [#{"
Bram Moolenaard5abb4c2019-07-13 22:46:10 +020031 \ .. "col: 3, length: 7, minwidth: 20, type: 'comment'"
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020032 \ .. "}]},"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020033 \ .. "], #{line: 4, col: 9, minwidth: 20})\<CR>")
Bram Moolenaarec583842019-05-26 14:11:23 +020034 call VerifyScreenDump(buf, 'Test_popupwin_02', {})
35
36 " switch back to first tabpage
37 call term_sendkeys(buf, "gt")
38 call VerifyScreenDump(buf, 'Test_popupwin_03', {})
39
40 " close that tabpage
41 call term_sendkeys(buf, ":quit!\<CR>")
42 call VerifyScreenDump(buf, 'Test_popupwin_04', {})
43
Bram Moolenaar202d9822019-06-11 21:56:30 +020044 " set 'columns' to a small value, size must be recomputed
45 call term_sendkeys(buf, ":let cols = &columns\<CR>")
46 call term_sendkeys(buf, ":set columns=12\<CR>")
47 call VerifyScreenDump(buf, 'Test_popupwin_04a', {})
48 call term_sendkeys(buf, ":let &columns = cols\<CR>")
49
Bram Moolenaar17146962019-05-30 00:12:11 +020050 " resize popup, show empty line at bottom
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020051 call term_sendkeys(buf, ":call popup_move(popupwin, #{minwidth: 15, maxwidth: 25, minheight: 3, maxheight: 5})\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020052 call term_sendkeys(buf, ":redraw\<CR>")
53 call VerifyScreenDump(buf, 'Test_popupwin_05', {})
54
Bram Moolenaar17146962019-05-30 00:12:11 +020055 " show not fitting line at bottom
56 call term_sendkeys(buf, ":call setbufline(winbufnr(popupwin), 3, 'this line will not fit here')\<CR>")
57 call term_sendkeys(buf, ":redraw\<CR>")
58 call VerifyScreenDump(buf, 'Test_popupwin_06', {})
59
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020060 " move popup over ruler
61 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020062 call term_sendkeys(buf, ":call popup_move(popupwin, #{line: 7, col: 55})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +020063 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020064 call VerifyScreenDump(buf, 'Test_popupwin_07', {})
65
66 " clear all popups after moving the cursor a bit, so that ruler is updated
67 call term_sendkeys(buf, "axxx\<Esc>")
68 call term_wait(buf)
69 call term_sendkeys(buf, "0")
70 call term_wait(buf)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +020071 call term_sendkeys(buf, ":call popup_clear()\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020072 call VerifyScreenDump(buf, 'Test_popupwin_08', {})
73
Bram Moolenaar4d784b22019-05-25 19:51:39 +020074 " clean up
75 call StopVimInTerminal(buf)
76 call delete('XtestPopup')
77endfunc
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020078
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020079func Test_popup_with_border_and_padding()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +020080 CheckScreendump
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020081
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020082 for iter in range(0, 1)
Bram Moolenaare7eb9272019-06-24 00:58:07 +020083 let lines =<< trim END
84 call setline(1, range(1, 100))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020085 call popup_create('hello border', #{line: 2, col: 3, border: []})
86 call popup_create('hello padding', #{line: 2, col: 23, padding: []})
Bram Moolenaarc363fe12019-08-04 18:13:46 +020087 call popup_create('hello both', #{line: 2, col: 43, border: [], padding: [], highlight: 'Normal'})
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020088 call popup_create('border TL', #{line: 6, col: 3, border: [1, 0, 0, 4]})
Bram Moolenaarb0992022020-01-30 14:55:42 +010089 call popup_create('paddings', #{line: 6, col: 23, padding: range(1, 4)})
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020090 call popup_create('wrapped longer text', #{line: 8, col: 55, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
91 call popup_create('right aligned text', #{line: 11, col: 56, wrap: 0, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
Bram Moolenaarfbcdf672020-01-06 23:07:48 +010092 call popup_create('X', #{line: 2, col: 73})
93 call popup_create('X', #{line: 3, col: 74})
94 call popup_create('X', #{line: 4, col: 75})
95 call popup_create('X', #{line: 5, col: 76})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020096 END
97 call insert(lines, iter == 1 ? '' : 'set enc=latin1')
98 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020099 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 15})
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +0200100 call VerifyScreenDump(buf, 'Test_popupwin_2' .. iter, {})
101
102 call StopVimInTerminal(buf)
103 call delete('XtestPopupBorder')
104 endfor
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200105
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200106 let lines =<< trim END
107 call setline(1, range(1, 100))
108 hi BlueColor ctermbg=lightblue
109 hi TopColor ctermbg=253
110 hi RightColor ctermbg=245
111 hi BottomColor ctermbg=240
112 hi LeftColor ctermbg=248
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200113 call popup_create('hello border', #{line: 2, col: 3, border: [], borderhighlight: ['BlueColor']})
114 call popup_create(['hello border', 'and more'], #{line: 2, col: 23, border: [], borderhighlight: ['TopColor', 'RightColor', 'BottomColor', 'LeftColor']})
115 call popup_create(['hello border', 'lines only'], #{line: 2, col: 43, border: [], borderhighlight: ['BlueColor'], borderchars: ['x']})
116 call popup_create(['hello border', 'with corners'], #{line: 2, col: 60, border: [], borderhighlight: ['BlueColor'], borderchars: ['x', '#']})
117 let winid = popup_create(['hello border', 'with numbers'], #{line: 6, col: 3, border: [], borderhighlight: ['BlueColor'], borderchars: ['0', '1', '2', '3', '4', '5', '6', '7']})
118 call popup_create(['hello border', 'just blanks'], #{line: 7, col: 23, border: [], borderhighlight: ['BlueColor'], borderchars: [' ']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200119 func MultiByte()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200120 call popup_create(['hello'], #{line: 8, col: 43, border: [], borderchars: ['─', '│', '─', '│', '┌', '┐', '┘', '└']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200121 endfunc
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200122 END
123 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200124 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 12})
Bram Moolenaar790498b2019-06-01 22:15:29 +0200125 call VerifyScreenDump(buf, 'Test_popupwin_22', {})
126
Bram Moolenaarad24a712019-06-17 20:05:45 +0200127 " check that changing borderchars triggers a redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200128 call term_sendkeys(buf, ":call popup_setoptions(winid, #{borderchars: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']})\<CR>")
Bram Moolenaarad24a712019-06-17 20:05:45 +0200129 call VerifyScreenDump(buf, 'Test_popupwin_23', {})
130
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200131 " check multi-byte border only with 'ambiwidth' single
132 if &ambiwidth == 'single'
133 call term_sendkeys(buf, ":call MultiByte()\<CR>")
134 call VerifyScreenDump(buf, 'Test_popupwin_24', {})
135 endif
136
Bram Moolenaar790498b2019-06-01 22:15:29 +0200137 call StopVimInTerminal(buf)
138 call delete('XtestPopupBorder')
139
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200140 let with_border_or_padding = #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200141 \ line: 2,
142 \ core_line: 3,
143 \ col: 3,
144 \ core_col: 4,
145 \ width: 14,
146 \ core_width: 12,
147 \ height: 3,
148 \ core_height: 1,
149 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100150 \ lastline: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200151 \ scrollbar: 0,
152 \ visible: 1}
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200153 let winid = popup_create('hello border', #{line: 2, col: 3, border: []})",
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200154 call assert_equal(with_border_or_padding, winid->popup_getpos())
Bram Moolenaarae943152019-06-16 22:54:14 +0200155 let options = popup_getoptions(winid)
156 call assert_equal([], options.border)
157 call assert_false(has_key(options, "padding"))
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200158
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200159 let winid = popup_create('hello padding', #{line: 2, col: 3, padding: []})
Bram Moolenaarae943152019-06-16 22:54:14 +0200160 let with_border_or_padding.width = 15
161 let with_border_or_padding.core_width = 13
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200162 call assert_equal(with_border_or_padding, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200163 let options = popup_getoptions(winid)
164 call assert_false(has_key(options, "border"))
165 call assert_equal([], options.padding)
166
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200167 call popup_setoptions(winid, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200168 \ padding: [1, 2, 3, 4],
169 \ border: [4, 0, 7, 8],
170 \ borderhighlight: ['Top', 'Right', 'Bottom', 'Left'],
171 \ borderchars: ['1', '^', '2', '>', '3', 'v', '4', '<'],
Bram Moolenaarae943152019-06-16 22:54:14 +0200172 \ })
173 let options = popup_getoptions(winid)
174 call assert_equal([1, 0, 1, 1], options.border)
175 call assert_equal([1, 2, 3, 4], options.padding)
176 call assert_equal(['Top', 'Right', 'Bottom', 'Left'], options.borderhighlight)
177 call assert_equal(['1', '^', '2', '>', '3', 'v', '4', '<'], options.borderchars)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200178
Bram Moolenaara1396152019-10-20 18:46:05 +0200179 " Check that popup_setoptions() takes the output of popup_getoptions()
180 call popup_setoptions(winid, options)
181 call assert_equal(options, popup_getoptions(winid))
182
Bram Moolenaarb0992022020-01-30 14:55:42 +0100183 " Check that range() doesn't crash
184 call popup_setoptions(winid, #{
185 \ padding: range(1, 4),
186 \ border: range(5, 8),
187 \ borderhighlight: range(4),
188 \ borderchars: range(8),
189 \ })
190
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200191 let winid = popup_create('hello both', #{line: 3, col: 8, border: [], padding: []})
192 call assert_equal(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200193 \ line: 3,
194 \ core_line: 5,
195 \ col: 8,
196 \ core_col: 10,
197 \ width: 14,
198 \ core_width: 10,
199 \ height: 5,
200 \ scrollbar: 0,
201 \ core_height: 1,
202 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100203 \ lastline: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200204 \ visible: 1}, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200205
206 call popup_clear()
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200207endfunc
208
Bram Moolenaarb4230122019-05-30 18:40:53 +0200209func Test_popup_with_syntax_win_execute()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200210 CheckScreendump
211
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200212 let lines =<< trim END
213 call setline(1, range(1, 100))
214 hi PopupColor ctermbg=lightblue
215 let winid = popup_create([
216 \ '#include <stdio.h>',
217 \ 'int main(void)',
218 \ '{',
219 \ ' printf(123);',
220 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200221 \], #{line: 3, col: 25, highlight: 'PopupColor'})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200222 call win_execute(winid, 'set syntax=cpp')
223 END
224 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200225 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200226 call VerifyScreenDump(buf, 'Test_popupwin_10', {})
227
228 " clean up
229 call StopVimInTerminal(buf)
230 call delete('XtestPopup')
231endfunc
232
233func Test_popup_with_syntax_setbufvar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200234 CheckScreendump
235
Bram Moolenaar402502d2019-05-30 22:07:36 +0200236 let lines =<< trim END
237 call setline(1, range(1, 100))
238 hi PopupColor ctermbg=lightgrey
239 let winid = popup_create([
240 \ '#include <stdio.h>',
241 \ 'int main(void)',
242 \ '{',
Bram Moolenaare089c3f2019-07-09 20:25:25 +0200243 \ "\tprintf(567);",
Bram Moolenaar402502d2019-05-30 22:07:36 +0200244 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200245 \], #{line: 3, col: 21, highlight: 'PopupColor'})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200246 call setbufvar(winbufnr(winid), '&syntax', 'cpp')
247 END
248 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200249 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200250 call VerifyScreenDump(buf, 'Test_popupwin_11', {})
251
252 " clean up
253 call StopVimInTerminal(buf)
254 call delete('XtestPopup')
255endfunc
256
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200257func Test_popup_with_matches()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200258 CheckScreendump
259
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200260 let lines =<< trim END
261 call setline(1, ['111 222 333', '444 555 666'])
262 let winid = popup_create([
263 \ '111 222 333',
264 \ '444 555 666',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200265 \], #{line: 3, col: 10, border: []})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200266 set hlsearch
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100267 hi VeryBlue ctermfg=blue guifg=blue
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200268 /666
269 call matchadd('ErrorMsg', '111')
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100270 call matchadd('VeryBlue', '444')
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200271 call win_execute(winid, "call matchadd('ErrorMsg', '111')")
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100272 call win_execute(winid, "call matchadd('VeryBlue', '555')")
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200273 END
274 call writefile(lines, 'XtestPopupMatches')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200275 let buf = RunVimInTerminal('-S XtestPopupMatches', #{rows: 10})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200276 call VerifyScreenDump(buf, 'Test_popupwin_matches', {})
277
278 " clean up
279 call StopVimInTerminal(buf)
280 call delete('XtestPopupMatches')
281endfunc
282
Bram Moolenaar399d8982019-06-02 15:34:29 +0200283func Test_popup_all_corners()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200284 CheckScreendump
285
Bram Moolenaar399d8982019-06-02 15:34:29 +0200286 let lines =<< trim END
287 call setline(1, repeat([repeat('-', 60)], 15))
288 set so=0
289 normal 2G3|r#
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200290 let winid1 = popup_create(['first', 'second'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200291 \ line: 'cursor+1',
292 \ col: 'cursor',
293 \ pos: 'topleft',
294 \ border: [],
295 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200296 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200297 normal 24|r@
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200298 let winid1 = popup_create(['First', 'SeconD'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200299 \ line: 'cursor+1',
300 \ col: 'cursor',
301 \ pos: 'topright',
302 \ border: [],
303 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200304 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200305 normal 9G27|r%
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200306 let winid1 = popup_create(['fiRSt', 'seCOnd'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200307 \ line: 'cursor-1',
308 \ col: 'cursor',
309 \ pos: 'botleft',
310 \ border: [],
311 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200312 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200313 normal 48|r&
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200314 let winid1 = popup_create(['FIrsT', 'SEcoND'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200315 \ line: 'cursor-1',
316 \ col: 'cursor',
317 \ pos: 'botright',
318 \ border: [],
319 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200320 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200321 normal 1G51|r*
322 let winid1 = popup_create(['one', 'two'], #{
323 \ line: 'cursor-1',
324 \ col: 'cursor',
325 \ pos: 'botleft',
326 \ border: [],
327 \ padding: [],
328 \ })
Bram Moolenaar399d8982019-06-02 15:34:29 +0200329 END
330 call writefile(lines, 'XtestPopupCorners')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200331 let buf = RunVimInTerminal('-S XtestPopupCorners', #{rows: 12})
Bram Moolenaar399d8982019-06-02 15:34:29 +0200332 call VerifyScreenDump(buf, 'Test_popupwin_corners', {})
333
334 " clean up
335 call StopVimInTerminal(buf)
336 call delete('XtestPopupCorners')
337endfunc
338
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100339func Test_popup_nospace()
340 CheckScreendump
341
342 let lines =<< trim END
343 call setline(1, repeat([repeat('-', 60)], 15))
344 set so=0
345
346 " cursor in a line in top half, using "botleft" with popup that
347 " does fit
348 normal 5G2|r@
349 let winid1 = popup_create(['one', 'two'], #{
350 \ line: 'cursor-1',
351 \ col: 'cursor',
352 \ pos: 'botleft',
353 \ border: [],
354 \ })
355 " cursor in a line in top half, using "botleft" with popup that
356 " doesn't fit: gets truncated
357 normal 5G9|r#
358 let winid1 = popup_create(['one', 'two', 'tee'], #{
359 \ line: 'cursor-1',
360 \ col: 'cursor',
361 \ pos: 'botleft',
362 \ posinvert: 0,
363 \ border: [],
364 \ })
365 " cursor in a line in top half, using "botleft" with popup that
366 " doesn't fit and 'posinvert' set: flips to below.
367 normal 5G16|r%
368 let winid1 = popup_create(['one', 'two', 'tee'], #{
369 \ line: 'cursor-1',
370 \ col: 'cursor',
371 \ pos: 'botleft',
372 \ border: [],
373 \ })
374 " cursor in a line in bottom half, using "botleft" with popup that
375 " doesn't fit: does not flip.
376 normal 8G23|r*
377 let winid1 = popup_create(['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff'], #{
378 \ line: 'cursor-1',
379 \ col: 'cursor',
380 \ pos: 'botleft',
381 \ border: [],
382 \ })
383
384 " cursor in a line in bottom half, using "topleft" with popup that
385 " does fit
386 normal 8G30|r@
387 let winid1 = popup_create(['one', 'two'], #{
388 \ line: 'cursor+1',
389 \ col: 'cursor',
390 \ pos: 'topleft',
391 \ border: [],
392 \ })
393 " cursor in a line in top half, using "topleft" with popup that
394 " doesn't fit: truncated
395 normal 8G37|r#
396 let winid1 = popup_create(['one', 'two', 'tee'], #{
397 \ line: 'cursor+1',
398 \ col: 'cursor',
399 \ pos: 'topleft',
400 \ posinvert: 0,
401 \ border: [],
402 \ })
403 " cursor in a line in top half, using "topleft" with popup that
Bram Moolenaar5c6b6182019-11-10 17:51:38 +0100404 " doesn't fit and "posinvert" set: flips to above.
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100405 normal 8G44|r%
Bram Moolenaar5c6b6182019-11-10 17:51:38 +0100406 let winid1 = popup_create(['one', 'two', 'tee', 'fou', 'fiv'], #{
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100407 \ line: 'cursor+1',
408 \ col: 'cursor',
409 \ pos: 'topleft',
410 \ border: [],
411 \ })
412 " cursor in a line in top half, using "topleft" with popup that
413 " doesn't fit: does not flip.
414 normal 5G51|r*
415 let winid1 = popup_create(['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff'], #{
416 \ line: 'cursor+1',
417 \ col: 'cursor',
418 \ pos: 'topleft',
419 \ border: [],
420 \ })
421 END
422 call writefile(lines, 'XtestPopupNospace')
423 let buf = RunVimInTerminal('-S XtestPopupNospace', #{rows: 12})
424 call VerifyScreenDump(buf, 'Test_popupwin_nospace', {})
425
426 " clean up
427 call StopVimInTerminal(buf)
428 call delete('XtestPopupNospace')
429endfunc
430
Bram Moolenaar8d241042019-06-12 23:40:01 +0200431func Test_popup_firstline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200432 CheckScreendump
433
Bram Moolenaar8d241042019-06-12 23:40:01 +0200434 let lines =<< trim END
435 call setline(1, range(1, 20))
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200436 let winid = popup_create(['1111', '222222', '33333', '44', '5', '666666', '77777', '888', '9999999999999999'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200437 \ maxheight: 4,
438 \ firstline: 3,
Bram Moolenaar8d241042019-06-12 23:40:01 +0200439 \ })
440 END
441 call writefile(lines, 'XtestPopupFirstline')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200442 let buf = RunVimInTerminal('-S XtestPopupFirstline', #{rows: 10})
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200443 call VerifyScreenDump(buf, 'Test_popupwin_firstline_1', {})
444
445 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: -1})\<CR>")
446 call term_sendkeys(buf, ":\<CR>")
447 call VerifyScreenDump(buf, 'Test_popupwin_firstline_2', {})
Bram Moolenaar8d241042019-06-12 23:40:01 +0200448
449 " clean up
450 call StopVimInTerminal(buf)
451 call delete('XtestPopupFirstline')
Bram Moolenaarae943152019-06-16 22:54:14 +0200452
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200453 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200454 \ maxheight: 2,
455 \ firstline: 3,
Bram Moolenaarae943152019-06-16 22:54:14 +0200456 \ })
457 call assert_equal(3, popup_getoptions(winid).firstline)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200458 call popup_setoptions(winid, #{firstline: 1})
Bram Moolenaarae943152019-06-16 22:54:14 +0200459 call assert_equal(1, popup_getoptions(winid).firstline)
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200460 eval winid->popup_close()
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200461
462 let winid = popup_create(['xxx']->repeat(50), #{
463 \ maxheight: 3,
464 \ firstline: 11,
465 \ })
466 redraw
467 call assert_equal(11, popup_getoptions(winid).firstline)
468 call assert_equal(11, popup_getpos(winid).firstline)
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +0200469 " check line() works with popup window
470 call assert_equal(11, line('.', winid))
471 call assert_equal(50, line('$', winid))
472 call assert_equal(0, line('$', 123456))
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200473
474 " Normal command changes what is displayed but not "firstline"
475 call win_execute(winid, "normal! \<c-y>")
476 call assert_equal(11, popup_getoptions(winid).firstline)
477 call assert_equal(10, popup_getpos(winid).firstline)
478
479 " Making some property change applies "firstline" again
480 call popup_setoptions(winid, #{line: 4})
481 call assert_equal(11, popup_getoptions(winid).firstline)
482 call assert_equal(11, popup_getpos(winid).firstline)
483
484 " Remove "firstline" property and scroll
485 call popup_setoptions(winid, #{firstline: 0})
486 call win_execute(winid, "normal! \<c-y>")
487 call assert_equal(0, popup_getoptions(winid).firstline)
488 call assert_equal(10, popup_getpos(winid).firstline)
489
490 " Making some property change has no side effect
491 call popup_setoptions(winid, #{line: 3})
492 call assert_equal(0, popup_getoptions(winid).firstline)
493 call assert_equal(10, popup_getpos(winid).firstline)
Bram Moolenaarae943152019-06-16 22:54:14 +0200494
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100495 " CTRL-D scrolls down half a page
496 let winid = popup_create(['xxx']->repeat(50), #{
497 \ maxheight: 8,
498 \ })
499 redraw
500 call assert_equal(1, popup_getpos(winid).firstline)
501 call win_execute(winid, "normal! \<C-D>")
502 call assert_equal(5, popup_getpos(winid).firstline)
503 call win_execute(winid, "normal! \<C-D>")
504 call assert_equal(9, popup_getpos(winid).firstline)
505 call win_execute(winid, "normal! \<C-U>")
506 call assert_equal(5, popup_getpos(winid).firstline)
507
508 call win_execute(winid, "normal! \<C-F>")
509 call assert_equal(11, popup_getpos(winid).firstline)
510 call win_execute(winid, "normal! \<C-B>")
511 call assert_equal(5, popup_getpos(winid).firstline)
512
Bram Moolenaarae943152019-06-16 22:54:14 +0200513 call popup_close(winid)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200514endfunc
515
Bram Moolenaara112f2d2019-09-01 17:38:09 +0200516func Test_popup_noscrolloff()
517 set scrolloff=5
518 let winid = popup_create(['xxx']->repeat(50), #{
519 \ maxheight: 5,
520 \ firstline: 11,
521 \ })
522 redraw
523 call assert_equal(11, popup_getoptions(winid).firstline)
524 call assert_equal(11, popup_getpos(winid).firstline)
525
526 call popup_setoptions(winid, #{firstline: 0})
527 call win_execute(winid, "normal! \<c-y>")
528 call assert_equal(0, popup_getoptions(winid).firstline)
529 call assert_equal(10, popup_getpos(winid).firstline)
530
531 call popup_close(winid)
532endfunc
533
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200534func Test_popup_drag()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200535 CheckScreendump
536
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200537 " create a popup that covers the command line
538 let lines =<< trim END
539 call setline(1, range(1, 20))
Bram Moolenaar356375f2019-08-24 14:46:29 +0200540 split
541 vsplit
542 $wincmd w
543 vsplit
544 1wincmd w
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200545 let winid = popup_create(['1111', '222222', '33333'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200546 \ drag: 1,
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200547 \ resize: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200548 \ border: [],
549 \ line: &lines - 4,
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200550 \ })
551 func Dragit()
552 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
553 endfunc
554 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
Bram Moolenaar356375f2019-08-24 14:46:29 +0200555 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2 - 20)<CR>
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200556 func Resize()
557 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
558 endfunc
Bram Moolenaar356375f2019-08-24 14:46:29 +0200559 map <silent> <F5> :call test_setmouse(6, 21)<CR>
560 map <silent> <F6> :call test_setmouse(7, 25)<CR>
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200561 END
562 call writefile(lines, 'XtestPopupDrag')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200563 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200564 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
565
566 call term_sendkeys(buf, ":call Dragit()\<CR>")
567 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
568
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200569 call term_sendkeys(buf, ":call Resize()\<CR>")
570 call VerifyScreenDump(buf, 'Test_popupwin_drag_03', {})
571
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200572 " clean up
573 call StopVimInTerminal(buf)
574 call delete('XtestPopupDrag')
575endfunc
576
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200577func Test_popup_close_with_mouse()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200578 CheckScreendump
579
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200580 let lines =<< trim END
581 call setline(1, range(1, 20))
582 " With border, can click on X
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200583 let winid = popup_create('foobar', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200584 \ close: 'button',
585 \ border: [],
586 \ line: 1,
587 \ col: 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200588 \ })
589 func CloseMsg(id, result)
590 echomsg 'Popup closed with ' .. a:result
591 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200592 let winid = popup_create('notification', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200593 \ close: 'click',
594 \ line: 3,
595 \ col: 15,
596 \ callback: 'CloseMsg',
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200597 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200598 let winid = popup_create('no border here', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200599 \ close: 'button',
600 \ line: 5,
601 \ col: 3,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200602 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200603 let winid = popup_create('only padding', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200604 \ close: 'button',
605 \ padding: [],
606 \ line: 5,
607 \ col: 23,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200608 \ })
609 func CloseWithX()
610 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
611 endfunc
612 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
613 func CloseWithClick()
614 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
615 endfunc
616 map <silent> <F4> :call test_setmouse(3, 17)<CR>
Bram Moolenaarf6396232019-08-24 19:36:00 +0200617 func CreateWithMenuFilter()
618 let winid = popup_create('barfoo', #{
619 \ close: 'button',
620 \ filter: 'popup_filter_menu',
621 \ border: [],
622 \ line: 1,
623 \ col: 40,
624 \ })
625 endfunc
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200626 END
627 call writefile(lines, 'XtestPopupClose')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200628 let buf = RunVimInTerminal('-S XtestPopupClose', #{rows: 10})
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200629 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
630
631 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
632 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
633
634 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
635 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
636
Bram Moolenaarf6396232019-08-24 19:36:00 +0200637 call term_sendkeys(buf, ":call CreateWithMenuFilter()\<CR>")
638 call VerifyScreenDump(buf, 'Test_popupwin_close_04', {})
639
640 " We have to send the actual mouse code, feedkeys() would be caught the
641 " filter.
642 call term_sendkeys(buf, "\<Esc>[<0;47;1M")
643 call VerifyScreenDump(buf, 'Test_popupwin_close_05', {})
644
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200645 " clean up
646 call StopVimInTerminal(buf)
647 call delete('XtestPopupClose')
648endfunction
649
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200650func Test_popup_menu_wrap()
651 CheckScreendump
652
653 let lines =<< trim END
654 call setline(1, range(1, 20))
655 call popup_create([
656 \ 'one',
657 \ 'asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfas',
658 \ 'three',
659 \ 'four',
660 \ ], #{
661 \ pos: "botleft",
662 \ border: [],
663 \ padding: [0,1,0,1],
664 \ maxheight: 3,
665 \ cursorline: 1,
666 \ filter: 'popup_filter_menu',
667 \ })
668 END
669 call writefile(lines, 'XtestPopupWrap')
670 let buf = RunVimInTerminal('-S XtestPopupWrap', #{rows: 10})
671 call VerifyScreenDump(buf, 'Test_popupwin_wrap_1', {})
672
673 call term_sendkeys(buf, "jj")
674 call VerifyScreenDump(buf, 'Test_popupwin_wrap_2', {})
675
676 " clean up
677 call term_sendkeys(buf, "\<Esc>")
678 call StopVimInTerminal(buf)
679 call delete('XtestPopupWrap')
680endfunction
681
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200682func Test_popup_with_mask()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200683 CheckScreendump
684
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200685 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200686 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200687 hi PopupColor ctermbg=lightgrey
688 let winid = popup_create([
689 \ 'some text',
690 \ 'another line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200691 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200692 \ line: 1,
693 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100694 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200695 \ wrap: 0,
696 \ fixed: 1,
697 \ zindex: 90,
698 \ padding: [],
699 \ highlight: 'PopupColor',
700 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200701 call popup_create([
702 \ 'xxxxxxxxx',
703 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200704 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200705 \ line: 3,
706 \ col: 18,
707 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200708 let winidb = popup_create([
709 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200710 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200711 \ line: 7,
712 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100713 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200714 \ wrap: 0,
715 \ fixed: 1,
716 \ close: 'button',
717 \ zindex: 90,
718 \ padding: [],
719 \ border: [],
720 \ mask: [[1,2,1,1], [-5,-1,4,4], [7,9,2,3], [3,5,5,5],[-7,-4,5,5]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200721 END
722 call writefile(lines, 'XtestPopupMask')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200723 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200724 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
725
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200726 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
727 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200728 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200729 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
730
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200731 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
732 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200733 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200734 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
735
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200736 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
737 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200738 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200739 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
740
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200741 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
742 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200743 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200744 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
745
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200746 " clean up
747 call StopVimInTerminal(buf)
748 call delete('XtestPopupMask')
749endfunc
750
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200751func Test_popup_select()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200752 CheckScreendump
753 CheckFeature clipboard_working
754
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200755 " create a popup with some text to be selected
756 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200757 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200758 call setline(1, range(1, 20))
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200759 let winid = popup_create(['the word', 'some more', 'several words here', 'invisible', '5', '6', '7'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200760 \ drag: 1,
761 \ border: [],
762 \ line: 3,
763 \ col: 10,
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200764 \ maxheight: 3,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200765 \ })
766 func Select1()
767 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
768 endfunc
769 map <silent> <F3> :call test_setmouse(4, 15)<CR>
770 map <silent> <F4> :call test_setmouse(6, 23)<CR>
771 END
772 call writefile(lines, 'XtestPopupSelect')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200773 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200774 call term_sendkeys(buf, ":call Select1()\<CR>")
775 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
776
777 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
778 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200779 " clean the command line, sometimes it still shows a command
780 call term_sendkeys(buf, ":\<esc>")
781
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200782 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
783
784 " clean up
785 call StopVimInTerminal(buf)
786 call delete('XtestPopupSelect')
787endfunc
788
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200789func Test_popup_in_tab()
790 " default popup is local to tab, not visible when in other tab
791 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200792 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200793 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200794 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200795 tabnew
796 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200797 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200798 quit
799 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200800
801 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200802 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200803 " buffer is gone now
804 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200805
806 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200807 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200808 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200809 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200810 tabnew
811 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200812 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200813 quit
814 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200815 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200816
817 " create popup in other tab
818 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200819 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200820 call assert_equal(0, popup_getpos(winid).visible)
821 call assert_equal(1, popup_getoptions(winid).tabpage)
822 quit
823 call assert_equal(1, popup_getpos(winid).visible)
824 call assert_equal(0, popup_getoptions(winid).tabpage)
825 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200826endfunc
827
828func Test_popup_valid_arguments()
829 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200830 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200831 let pos = popup_getpos(winid)
832 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200833 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200834
835 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200836 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200837 let pos = popup_getpos(winid)
838 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200839 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200840
841 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200842 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200843 let pos = popup_getpos(winid)
844 let around = (&columns - pos.width) / 2
845 call assert_inrange(around - 1, around + 1, pos.col)
846 let around = (&lines - pos.height) / 2
847 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200848 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200849endfunc
850
851func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200852 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200853 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200854 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200855 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200856
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200857 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200858 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200859 call assert_fails('call popup_create("text", #{col: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200860 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200861 call assert_fails('call popup_create("text", #{col: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200862 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200863 call assert_fails('call popup_create("text", #{col: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200864 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200865
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200866 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200867 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200868 call assert_fails('call popup_create("text", #{line: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200869 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200870 call assert_fails('call popup_create("text", #{line: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200871 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200872 call assert_fails('call popup_create("text", #{line: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200873 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200874
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200875 call assert_fails('call popup_create("text", #{pos: "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200876 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200877 call assert_fails('call popup_create("text", #{padding: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200878 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200879 call assert_fails('call popup_create("text", #{border: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200880 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200881 call assert_fails('call popup_create("text", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200882 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +0200883 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
884 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200885 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200886 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200887
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200888 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200889 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200890 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200891 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200892 call assert_fails('call popup_create([#{text: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200893 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +0100894 call assert_fails('call popup_create([#{text: "text", props: range(3)}], {})', 'E715:')
895 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200896 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
897 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +0100898 call assert_fails('call popup_create("text", #{mask: range(5)})', 'E475:')
899 call popup_clear()
900 call popup_create("text", #{mask: [range(4)]})
901 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200902 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200903 call assert_fails('call popup_create("text", #{mapping: []})', 'E745:')
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200904 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200905endfunc
906
Bram Moolenaareea16992019-05-31 17:34:48 +0200907func Test_win_execute_closing_curwin()
908 split
909 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200910 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200911 call popup_clear()
Bram Moolenaar49540192019-12-11 19:34:54 +0100912
913 let winid = popup_create('some text', {})
914 call assert_fails('call win_execute(winid, printf("normal! :\<C-u>call popup_close(%d)\<CR>", winid))', 'E994')
915 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200916endfunc
917
918func Test_win_execute_not_allowed()
919 let winid = popup_create('some text', {})
920 call assert_fails('call win_execute(winid, "split")', 'E994:')
921 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
922 call assert_fails('call win_execute(winid, "close")', 'E994:')
923 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200924 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200925 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
926 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
927 call assert_fails('call win_execute(winid, "next")', 'E994:')
928 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
Bram Moolenaar3a2505c2020-03-09 16:40:41 +0100929 call assert_fails('call win_execute(winid, "pedit filename")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200930 call assert_fails('call win_execute(winid, "buf")', 'E994:')
Bram Moolenaar3029bcc2020-01-18 15:06:19 +0100931 call assert_fails('call win_execute(winid, "bnext")', 'E994:')
932 call assert_fails('call win_execute(winid, "bprev")', 'E994:')
933 call assert_fails('call win_execute(winid, "bfirst")', 'E994:')
934 call assert_fails('call win_execute(winid, "blast")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200935 call assert_fails('call win_execute(winid, "edit")', 'E994:')
936 call assert_fails('call win_execute(winid, "enew")', 'E994:')
937 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
938 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
939 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
940 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200941 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200942endfunc
943
Bram Moolenaar402502d2019-05-30 22:07:36 +0200944func Test_popup_with_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200945 CheckScreendump
946
Bram Moolenaar402502d2019-05-30 22:07:36 +0200947 let lines =<< trim END
948 call setline(1, range(1, 100))
949 let winid = popup_create(
950 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200951 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200952 END
953 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200954 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200955 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
956
957 " clean up
958 call StopVimInTerminal(buf)
959 call delete('XtestPopup')
960endfunc
961
962func Test_popup_without_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200963 CheckScreendump
964
Bram Moolenaar402502d2019-05-30 22:07:36 +0200965 let lines =<< trim END
966 call setline(1, range(1, 100))
967 let winid = popup_create(
968 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200969 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200970 END
971 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200972 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200973 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
974
975 " clean up
976 call StopVimInTerminal(buf)
977 call delete('XtestPopup')
978endfunc
979
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200980func Test_popup_with_showbreak()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200981 CheckScreendump
982
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200983 let lines =<< trim END
984 set showbreak=>>\
985 call setline(1, range(1, 20))
986 let winid = popup_dialog(
Bram Moolenaar8ae54372019-09-15 18:11:16 +0200987 \ 'a long line here that wraps',
988 \ #{filter: 'popup_filter_yesno',
989 \ maxwidth: 12})
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200990 END
991 call writefile(lines, 'XtestPopupShowbreak')
992 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
993 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
994
995 " clean up
996 call term_sendkeys(buf, "y")
997 call StopVimInTerminal(buf)
998 call delete('XtestPopupShowbreak')
999endfunc
1000
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001001func Test_popup_time()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001002 CheckFeature timers
1003
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001004 topleft vnew
1005 call setline(1, 'hello')
1006
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001007 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001008 \ line: 1,
1009 \ col: 1,
1010 \ minwidth: 20,
1011 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001012 \})
1013 redraw
1014 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1015 call assert_equal('world', line)
1016
Bram Moolenaarb4f06282019-07-12 21:07:54 +02001017 call assert_equal(winid, popup_locate(1, 1))
1018 call assert_equal(winid, popup_locate(1, 20))
1019 call assert_equal(0, popup_locate(1, 21))
1020 call assert_equal(0, popup_locate(2, 1))
1021
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001022 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +02001023 redraw
Bram Moolenaar196b4662019-09-06 21:34:30 +02001024 let line = join(map(range(1, 5), '1->screenstring(v:val)'), '')
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001025 call assert_equal('hello', line)
1026
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001027 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001028 \ line: &lines,
1029 \ col: 10,
1030 \ minwidth: 20,
1031 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001032 \})
1033 redraw
1034 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1035 call assert_match('.*on the command line.*', line)
1036
1037 sleep 700m
1038 redraw
1039 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1040 call assert_notmatch('.*on the command line.*', line)
1041
1042 bwipe!
1043endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001044
1045func Test_popup_hide()
1046 topleft vnew
1047 call setline(1, 'hello')
1048
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001049 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001050 \ line: 1,
1051 \ col: 1,
1052 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001053 \})
1054 redraw
1055 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1056 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001057 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001058 " buffer is still listed and active
1059 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001060
1061 call popup_hide(winid)
1062 redraw
1063 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1064 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001065 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001066 " buffer is still listed but hidden
Bram Moolenaarca7c0782020-01-14 20:42:48 +01001067 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001068
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001069 eval winid->popup_show()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001070 redraw
1071 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1072 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001073 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001074
1075
1076 call popup_close(winid)
1077 redraw
1078 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1079 call assert_equal('hello', line)
1080
1081 " error is given for existing non-popup window
1082 call assert_fails('call popup_hide(win_getid())', 'E993:')
1083
1084 " no error non-existing window
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001085 eval 1234234->popup_hide()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001086 call popup_show(41234234)
1087
1088 bwipe!
1089endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001090
1091func Test_popup_move()
1092 topleft vnew
1093 call setline(1, 'hello')
1094
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001095 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001096 \ line: 1,
1097 \ col: 1,
1098 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001099 \})
1100 redraw
1101 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1102 call assert_equal('world ', line)
1103
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001104 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001105 redraw
1106 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1107 call assert_equal('hello ', line)
1108 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
1109 call assert_equal('~world', line)
1110
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001111 eval winid->popup_move(#{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001112 redraw
1113 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1114 call assert_equal('hworld', line)
1115
1116 call popup_close(winid)
1117
1118 bwipe!
1119endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +02001120
Bram Moolenaar402502d2019-05-30 22:07:36 +02001121func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001122 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001123 \ line: 2,
1124 \ col: 3,
1125 \ minwidth: 10,
1126 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +02001127 \})
1128 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001129 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001130 call assert_equal(2, res.line)
1131 call assert_equal(3, res.col)
1132 call assert_equal(10, res.width)
1133 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001134 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001135
1136 call popup_close(winid)
1137endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001138
1139func Test_popup_width_longest()
1140 let tests = [
1141 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
1142 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
1143 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
1144 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
1145 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
1146 \ ]
1147
1148 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001149 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001150 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001151 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001152 call assert_equal(test[1], position.width)
1153 call popup_close(winid)
1154 endfor
1155endfunc
1156
1157func Test_popup_wraps()
1158 let tests = [
1159 \ ['nowrap', 6, 1],
1160 \ ['a line that wraps once', 12, 2],
1161 \ ['a line that wraps two times', 12, 3],
1162 \ ]
1163 for test in tests
1164 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001165 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001166 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001167 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001168 call assert_equal(test[1], position.width)
1169 call assert_equal(test[2], position.height)
1170
1171 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001172 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001173 endfor
1174endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001175
1176func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001177 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001178 \ line: 2,
1179 \ col: 3,
1180 \ minwidth: 10,
1181 \ minheight: 11,
1182 \ maxwidth: 20,
1183 \ maxheight: 21,
1184 \ zindex: 100,
1185 \ time: 5000,
1186 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001187 \})
1188 redraw
1189 let res = popup_getoptions(winid)
1190 call assert_equal(2, res.line)
1191 call assert_equal(3, res.col)
1192 call assert_equal(10, res.minwidth)
1193 call assert_equal(11, res.minheight)
1194 call assert_equal(20, res.maxwidth)
1195 call assert_equal(21, res.maxheight)
1196 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001197 call assert_equal(1, res.fixed)
Bram Moolenaarb8350ab2019-08-04 17:59:49 +02001198 call assert_equal(1, res.mapping)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001199 if has('timers')
1200 call assert_equal(5000, res.time)
1201 endif
1202 call popup_close(winid)
1203
1204 let winid = popup_create('hello', {})
1205 redraw
1206 let res = popup_getoptions(winid)
1207 call assert_equal(0, res.line)
1208 call assert_equal(0, res.col)
1209 call assert_equal(0, res.minwidth)
1210 call assert_equal(0, res.minheight)
1211 call assert_equal(0, res.maxwidth)
1212 call assert_equal(0, res.maxheight)
1213 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001214 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001215 if has('timers')
1216 call assert_equal(0, res.time)
1217 endif
1218 call popup_close(winid)
1219 call assert_equal({}, popup_getoptions(winid))
1220endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001221
1222func Test_popup_option_values()
1223 new
1224 " window-local
1225 setlocal number
1226 setlocal nowrap
1227 " buffer-local
1228 setlocal omnifunc=Something
1229 " global/buffer-local
1230 setlocal path=/there
1231 " global/window-local
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001232 setlocal statusline=2
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001233
1234 let winid = popup_create('hello', {})
1235 call assert_equal(0, getwinvar(winid, '&number'))
1236 call assert_equal(1, getwinvar(winid, '&wrap'))
1237 call assert_equal('', getwinvar(winid, '&omnifunc'))
1238 call assert_equal(&g:path, getwinvar(winid, '&path'))
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001239 call assert_equal(&g:statusline, getwinvar(winid, '&statusline'))
1240
1241 " 'scrolloff' is reset to zero
1242 call assert_equal(5, &scrolloff)
1243 call assert_equal(0, getwinvar(winid, '&scrolloff'))
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001244
1245 call popup_close(winid)
1246 bwipe
1247endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001248
1249func Test_popup_atcursor()
1250 topleft vnew
1251 call setline(1, [
1252 \ 'xxxxxxxxxxxxxxxxx',
1253 \ 'xxxxxxxxxxxxxxxxx',
1254 \ 'xxxxxxxxxxxxxxxxx',
1255 \])
1256
1257 call cursor(2, 2)
1258 redraw
1259 let winid = popup_atcursor('vim', {})
1260 redraw
1261 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1262 call assert_equal('xvimxxxxxxxxxxxxx', line)
1263 call popup_close(winid)
1264
1265 call cursor(3, 4)
1266 redraw
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001267 let winid = 'vim'->popup_atcursor({})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001268 redraw
1269 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
1270 call assert_equal('xxxvimxxxxxxxxxxx', line)
1271 call popup_close(winid)
1272
1273 call cursor(1, 1)
1274 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001275 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001276 \ line: 'cursor+2',
1277 \ col: 'cursor+1',
1278 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001279 redraw
1280 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1281 call assert_equal('xvimxxxxxxxxxxxxx', line)
1282 call popup_close(winid)
1283
1284 call cursor(3, 3)
1285 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001286 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001287 \ line: 'cursor-2',
1288 \ col: 'cursor-1',
1289 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001290 redraw
1291 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1292 call assert_equal('xvimxxxxxxxxxxxxx', line)
1293 call popup_close(winid)
1294
Bram Moolenaar402502d2019-05-30 22:07:36 +02001295 " just enough room above
1296 call cursor(3, 3)
1297 redraw
1298 let winid = popup_atcursor(['vim', 'is great'], {})
1299 redraw
1300 let pos = popup_getpos(winid)
1301 call assert_equal(1, pos.line)
1302 call popup_close(winid)
1303
1304 " not enough room above, popup goes below the cursor
1305 call cursor(3, 3)
1306 redraw
1307 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1308 redraw
1309 let pos = popup_getpos(winid)
1310 call assert_equal(4, pos.line)
1311 call popup_close(winid)
1312
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001313 " cursor in first line, popup in line 2
1314 call cursor(1, 1)
1315 redraw
1316 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1317 redraw
1318 let pos = popup_getpos(winid)
1319 call assert_equal(2, pos.line)
1320 call popup_close(winid)
1321
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001322 bwipe!
1323endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001324
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001325func Test_popup_atcursor_pos()
1326 CheckScreendump
1327
1328 let lines =<< trim END
1329 call setline(1, repeat([repeat('-', 60)], 15))
1330 set so=0
1331
1332 normal 9G3|r#
1333 let winid1 = popup_atcursor(['first', 'second'], #{
1334 \ moved: [0, 0, 0],
1335 \ })
1336 normal 9G21|r&
1337 let winid1 = popup_atcursor(['FIrsT', 'SEcoND'], #{
1338 \ pos: 'botright',
1339 \ moved: [0, 0, 0],
1340 \ })
1341 normal 3G27|r%
1342 let winid1 = popup_atcursor(['fiRSt', 'seCOnd'], #{
1343 \ pos: 'topleft',
1344 \ moved: [0, 0, 0],
1345 \ })
1346 normal 3G45|r@
1347 let winid1 = popup_atcursor(['First', 'SeconD'], #{
1348 \ pos: 'topright',
Bram Moolenaarb0992022020-01-30 14:55:42 +01001349 \ moved: range(3),
1350 \ mousemoved: range(3),
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001351 \ })
1352 END
1353 call writefile(lines, 'XtestPopupAtcursorPos')
1354 let buf = RunVimInTerminal('-S XtestPopupAtcursorPos', #{rows: 12})
1355 call VerifyScreenDump(buf, 'Test_popupwin_atcursor_pos', {})
1356
1357 " clean up
1358 call StopVimInTerminal(buf)
1359 call delete('XtestPopupAtcursorPos')
1360endfunc
1361
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001362func Test_popup_beval()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001363 CheckScreendump
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001364 CheckFeature balloon_eval_term
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001365
1366 let lines =<< trim END
1367 call setline(1, range(1, 20))
1368 call setline(5, 'here is some text to hover over')
1369 set balloonevalterm
1370 set balloonexpr=BalloonExpr()
1371 set balloondelay=100
1372 func BalloonExpr()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001373 let s:winid = [v:beval_text]->popup_beval({})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001374 return ''
1375 endfunc
1376 func Hover()
1377 call test_setmouse(5, 15)
1378 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1379 sleep 100m
1380 endfunc
1381 func MoveOntoPopup()
1382 call test_setmouse(4, 17)
1383 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1384 endfunc
1385 func MoveAway()
1386 call test_setmouse(5, 13)
1387 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1388 endfunc
1389 END
1390 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001391 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001392 call term_wait(buf, 100)
1393 call term_sendkeys(buf, 'j')
1394 call term_sendkeys(buf, ":call Hover()\<CR>")
1395 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1396
1397 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1398 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1399
1400 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1401 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1402
1403 " clean up
1404 call StopVimInTerminal(buf)
1405 call delete('XtestPopupBeval')
1406endfunc
1407
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001408func Test_popup_filter()
1409 new
1410 call setline(1, 'some text')
1411
1412 func MyPopupFilter(winid, c)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001413 if a:c == 'e' || a:c == "\<F9>"
1414 let g:eaten = a:c
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001415 return 1
1416 endif
1417 if a:c == '0'
1418 let g:ignored = '0'
1419 return 0
1420 endif
1421 if a:c == 'x'
1422 call popup_close(a:winid)
1423 return 1
1424 endif
1425 return 0
1426 endfunc
1427
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001428 let winid = 'something'->popup_create(#{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001429 redraw
1430
1431 " e is consumed by the filter
1432 call feedkeys('e', 'xt')
1433 call assert_equal('e', g:eaten)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001434 call feedkeys("\<F9>", 'xt')
1435 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001436
1437 " 0 is ignored by the filter
1438 normal $
1439 call assert_equal(9, getcurpos()[2])
1440 call feedkeys('0', 'xt')
1441 call assert_equal('0', g:ignored)
1442 call assert_equal(1, getcurpos()[2])
1443
1444 " x closes the popup
1445 call feedkeys('x', 'xt')
Bram Moolenaarec084d32020-02-28 22:44:47 +01001446 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001447 call assert_equal(-1, winbufnr(winid))
1448
1449 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001450 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001451endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001452
Bram Moolenaara42d9452019-06-15 21:46:30 +02001453func ShowDialog(key, result)
1454 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001455 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001456 \ filter: 'popup_filter_yesno',
1457 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001458 \ })
1459 redraw
1460 call feedkeys(a:key, "xt")
1461 call assert_equal(winid, s:cb_winid)
1462 call assert_equal(a:result, s:cb_res)
1463endfunc
1464
1465func Test_popup_dialog()
1466 func QuitCallback(id, res)
1467 let s:cb_winid = a:id
1468 let s:cb_res = a:res
1469 endfunc
1470
1471 let winid = ShowDialog("y", 1)
1472 let winid = ShowDialog("Y", 1)
1473 let winid = ShowDialog("n", 0)
1474 let winid = ShowDialog("N", 0)
1475 let winid = ShowDialog("x", 0)
1476 let winid = ShowDialog("X", 0)
1477 let winid = ShowDialog("\<Esc>", 0)
1478 let winid = ShowDialog("\<C-C>", -1)
1479
1480 delfunc QuitCallback
1481endfunc
1482
Bram Moolenaara730e552019-06-16 19:05:31 +02001483func ShowMenu(key, result)
1484 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001485 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001486 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001487 \ })
1488 redraw
1489 call feedkeys(a:key, "xt")
1490 call assert_equal(winid, s:cb_winid)
1491 call assert_equal(a:result, s:cb_res)
1492endfunc
1493
1494func Test_popup_menu()
1495 func QuitCallback(id, res)
1496 let s:cb_winid = a:id
1497 let s:cb_res = a:res
1498 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001499 " mapping won't be used in popup
1500 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001501
1502 let winid = ShowMenu(" ", 1)
1503 let winid = ShowMenu("j \<CR>", 2)
1504 let winid = ShowMenu("JjK \<CR>", 2)
1505 let winid = ShowMenu("jjjjjj ", 3)
1506 let winid = ShowMenu("kkk ", 1)
1507 let winid = ShowMenu("x", -1)
1508 let winid = ShowMenu("X", -1)
1509 let winid = ShowMenu("\<Esc>", -1)
1510 let winid = ShowMenu("\<C-C>", -1)
1511
1512 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001513 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001514endfunc
1515
1516func Test_popup_menu_screenshot()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001517 CheckScreendump
Bram Moolenaara730e552019-06-16 19:05:31 +02001518
1519 let lines =<< trim END
1520 call setline(1, range(1, 20))
1521 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001522 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001523 func MenuDone(id, res)
1524 echomsg "selected " .. a:res
1525 endfunc
1526 END
1527 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001528 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001529 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1530
1531 call term_sendkeys(buf, "jj")
1532 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1533
1534 call term_sendkeys(buf, " ")
1535 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1536
1537 " clean up
1538 call StopVimInTerminal(buf)
1539 call delete('XtestPopupMenu')
1540endfunc
1541
Bram Moolenaarf914a332019-07-20 15:09:56 +02001542func Test_popup_menu_narrow()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001543 CheckScreendump
Bram Moolenaarf914a332019-07-20 15:09:56 +02001544
1545 let lines =<< trim END
1546 call setline(1, range(1, 20))
1547 hi PopupSelected ctermbg=green
1548 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1549 func MenuDone(id, res)
1550 echomsg "selected " .. a:res
1551 endfunc
1552 END
1553 call writefile(lines, 'XtestPopupNarrowMenu')
1554 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1555 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1556
1557 " clean up
1558 call term_sendkeys(buf, "x")
1559 call StopVimInTerminal(buf)
1560 call delete('XtestPopupNarrowMenu')
1561endfunc
1562
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001563func Test_popup_title()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001564 CheckScreendump
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001565
1566 " Create a popup without title or border, a line of padding will be added to
1567 " put the title on.
1568 let lines =<< trim END
1569 call setline(1, range(1, 20))
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001570 let winid = popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001571 END
1572 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001573 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001574 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1575
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001576 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxwidth: 20, title: 'a very long title that is not going to fit'})\<CR>")
1577 call term_sendkeys(buf, ":\<CR>")
1578 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_1', {})
1579
1580 call term_sendkeys(buf, ":call popup_setoptions(winid, #{border: []})\<CR>")
1581 call term_sendkeys(buf, ":\<CR>")
1582 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_2', {})
1583
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001584 " clean up
1585 call StopVimInTerminal(buf)
1586 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001587
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001588 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001589 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001590 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001591 call assert_equal('Another Title', popup_getoptions(winid).title)
1592
1593 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001594endfunc
1595
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001596func Test_popup_close_callback()
1597 func PopupDone(id, result)
1598 let g:result = a:result
1599 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001600 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001601 redraw
1602 call popup_close(winid, 'done')
1603 call assert_equal('done', g:result)
1604endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001605
1606func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001607 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001608 redraw
1609 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001610 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001611 call assert_equal(5, pos.height)
1612
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001613 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001614 redraw
1615 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001616 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001617 call assert_equal(3, pos.height)
1618endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001619
1620func Test_popup_never_behind()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001621 CheckScreendump
1622
Bram Moolenaar988c4332019-06-02 14:12:11 +02001623 " +-----------------------------+
1624 " | | |
1625 " | | |
1626 " | | |
1627 " | line1 |
1628 " |------------line2------------|
1629 " | line3 |
1630 " | line4 |
1631 " | |
1632 " | |
1633 " +-----------------------------+
1634 let lines =<< trim END
Bram Moolenaar988c4332019-06-02 14:12:11 +02001635 split
1636 vsplit
1637 let info_window1 = getwininfo()[0]
1638 let line = info_window1['height']
1639 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001640 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001641 \ line : line,
1642 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001643 \ })
1644 END
1645 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001646 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001647 call term_sendkeys(buf, "\<C-W>w")
1648 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1649
1650 " clean up
1651 call StopVimInTerminal(buf)
1652 call delete('XtestPopupBehind')
1653endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001654
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001655func s:VerifyPosition(p, msg, line, col, width, height)
1656 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1657 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1658 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1659 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001660endfunc
1661
1662func Test_popup_position_adjust()
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001663 " Anything placed past the last cell on the right of the screen is moved to
1664 " the left.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001665 "
1666 " When wrapping is disabled, we also shift to the left to display on the
1667 " screen, unless fixed is set.
1668
1669 " Entries for cases which don't vary based on wrapping.
1670 " Format is per tests described below
1671 let both_wrap_tests = [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001672 \ ['a', 5, &columns, 5, &columns, 1, 1],
1673 \ ['b', 5, &columns + 1, 5, &columns, 1, 1],
1674 \ ['c', 5, &columns - 1, 5, &columns - 1, 1, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001675 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001676 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1]]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001677
1678 " these test groups are dicts with:
1679 " - comment: something to identify the group of tests by
1680 " - options: dict of options to merge with the row/col in tests
1681 " - tests: list of cases. Each one is a list with elements:
1682 " - text
1683 " - row
1684 " - col
1685 " - expected row
1686 " - expected col
1687 " - expected width
1688 " - expected height
1689 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001690 \ #{
1691 \ comment: 'left-aligned with wrapping',
1692 \ options: #{
1693 \ wrap: 1,
1694 \ pos: 'botleft',
1695 \ },
1696 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001697 \ ['aa', 5, &columns, 4, &columns, 1, 2],
1698 \ ['bb', 5, &columns + 1, 4, &columns, 1, 2],
1699 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1700 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1701 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1702 \
1703 \ ['aaa', 5, &columns, 3, &columns, 1, 3],
1704 \ ['bbb', 5, &columns + 1, 3, &columns, 1, 3],
1705 \ ['ccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
1706 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1707 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1708 \
1709 \ ['aaaa', 5, &columns, 2, &columns, 1, 4],
1710 \ ['bbbb', 5, &columns + 1, 2, &columns, 1, 4],
1711 \ ['cccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001712 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
1713 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001714 \ ['eeee', 5, &columns - 4, 5, &columns - 4, 4, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001715 \ ],
1716 \ },
1717 \ #{
1718 \ comment: 'left aligned without wrapping',
1719 \ options: #{
1720 \ wrap: 0,
1721 \ pos: 'botleft',
1722 \ },
1723 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001724 \ ['aa', 5, &columns, 5, &columns - 1, 2, 1],
1725 \ ['bb', 5, &columns + 1, 5, &columns - 1, 2, 1],
1726 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1727 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1728 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1729 \
1730 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
1731 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1732 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1733 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1734 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1735 \
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001736 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
1737 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
1738 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
1739 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
1740 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1741 \ ],
1742 \ },
1743 \ #{
1744 \ comment: 'left aligned with fixed position',
1745 \ options: #{
1746 \ wrap: 0,
1747 \ fixed: 1,
1748 \ pos: 'botleft',
1749 \ },
1750 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001751 \ ['aa', 5, &columns, 5, &columns, 1, 1],
1752 \ ['bb', 5, &columns + 1, 5, &columns, 1, 1],
1753 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1754 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1755 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1756 \
1757 \ ['aaa', 5, &columns, 5, &columns, 1, 1],
1758 \ ['bbb', 5, &columns + 1, 5, &columns, 1, 1],
1759 \ ['ccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1760 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1761 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1762 \
1763 \ ['aaaa', 5, &columns, 5, &columns, 1, 1],
1764 \ ['bbbb', 5, &columns + 1, 5, &columns, 1, 1],
1765 \ ['cccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001766 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1767 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1768 \ ],
1769 \ },
1770 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001771
1772 for test_group in tests
1773 for test in test_group.tests
1774 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001775 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001776 \ line: line,
1777 \ col: col,
1778 \ }
1779 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001780
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001781 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001782
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001783 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001784 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
1785 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001786 endfor
1787 endfor
1788
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001789 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001790 %bwipe!
1791endfunc
1792
Bram Moolenaar3397f742019-06-02 18:40:06 +02001793func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001794 " width of screen
1795 let X = join(map(range(&columns), {->'X'}), '')
1796
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001797 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1798 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001799
1800 redraw
1801 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1802 call assert_equal(X, line)
1803
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001804 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001805 redraw
1806
1807 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001808 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
1809 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001810
1811 redraw
1812 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1813 call assert_equal(X, line)
1814
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001815 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001816 redraw
1817
1818 " Extend so > window width
1819 let X .= 'x'
1820
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001821 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1822 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001823
1824 redraw
1825 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1826 call assert_equal(X[ : -2 ], line)
1827
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001828 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001829 redraw
1830
1831 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001832 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
1833 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001834
1835 redraw
1836 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1837 call assert_equal(X[ : -2 ], line)
1838
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001839 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001840 redraw
1841
1842 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001843 let p = popup_create(X,
1844 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
1845 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001846
1847 redraw
1848 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1849 let e_line = ' ' . X[ 1 : -2 ]
1850 call assert_equal(e_line, line)
1851
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001852 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001853 redraw
1854
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001855 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001856 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001857endfunc
1858
1859func Test_popup_moved()
1860 new
1861 call test_override('char_avail', 1)
1862 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1863
1864 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001865 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001866 redraw
1867 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001868 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001869 " trigger the check for last_cursormoved by going into insert mode
1870 call feedkeys("li\<Esc>", 'xt')
1871 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001872 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001873
1874 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001875 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001876 redraw
1877 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001878 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001879 call feedkeys("hi\<Esc>", 'xt')
1880 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001881 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001882
1883 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001884 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001885 redraw
1886 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001887 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001888 call feedkeys("li\<Esc>", 'xt')
1889 call assert_equal(1, popup_getpos(winid).visible)
1890 call feedkeys("ei\<Esc>", 'xt')
1891 call assert_equal(1, popup_getpos(winid).visible)
1892 call feedkeys("eli\<Esc>", 'xt')
1893 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001894 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001895
Bram Moolenaar17627312019-06-02 19:53:44 +02001896 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001897 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001898 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001899 redraw
1900 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001901 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001902 call feedkeys("eli\<Esc>", 'xt')
1903 call assert_equal(1, popup_getpos(winid).visible)
1904 call feedkeys("wi\<Esc>", 'xt')
1905 call assert_equal(1, popup_getpos(winid).visible)
1906 call feedkeys("Eli\<Esc>", 'xt')
1907 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001908 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001909
1910 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001911 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001912 redraw
1913 call assert_equal(1, popup_getpos(winid).visible)
1914 call feedkeys("eli\<Esc>", 'xt')
1915 call feedkeys("ei\<Esc>", 'xt')
1916 call assert_equal(1, popup_getpos(winid).visible)
1917 call feedkeys("eli\<Esc>", 'xt')
1918 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001919 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001920
1921 bwipe!
1922 call test_override('ALL', 0)
1923endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001924
1925func Test_notifications()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001926 CheckFeature timers
1927 CheckScreendump
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001928
Bram Moolenaar0fdddee2019-09-01 15:26:23 +02001929 let lines =<< trim END
1930 call setline(1, range(1, 20))
1931 hi Notification ctermbg=lightblue
1932 call popup_notification('first notification', {})
1933 END
1934 call writefile(lines, 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001935 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001936 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1937
1938 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001939 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1940 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001941 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1942
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001943 " clean up
1944 call StopVimInTerminal(buf)
1945 call delete('XtestNotifications')
1946endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001947
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001948func Test_popup_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001949 CheckScreendump
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001950
1951 let lines =<< trim END
1952 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02001953 hi ScrollThumb ctermbg=blue
1954 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001955 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001956 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001957 \ minwidth: 8,
1958 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001959 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001960 func ScrollUp()
1961 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
1962 endfunc
1963 func ScrollDown()
1964 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
1965 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001966 func ClickTop()
1967 call feedkeys("\<F4>\<LeftMouse>", "xt")
1968 endfunc
1969 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001970 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001971 call feedkeys("\<F5>\<LeftMouse>", "xt")
1972 endfunc
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001973 func Popup_filter(winid, key)
1974 if a:key == 'j'
1975 let line = popup_getoptions(a:winid).firstline
1976 let nlines = line('$', a:winid)
1977 let newline = line < nlines ? (line + 1) : nlines
1978 call popup_setoptions(a:winid, #{firstline: newline})
1979 return v:true
1980 elseif a:key == 'x'
1981 call popup_close(a:winid)
1982 return v:true
1983 endif
1984 endfunc
1985
1986 func PopupScroll()
1987 call popup_clear()
1988 let text =<< trim END
1989 1
1990 2
1991 3
1992 4
1993 long line long line long line long line long line long line
1994 long line long line long line long line long line long line
1995 long line long line long line long line long line long line
1996 END
1997 call popup_create(text, #{
1998 \ minwidth: 30,
1999 \ maxwidth: 30,
2000 \ minheight: 4,
2001 \ maxheight: 4,
2002 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +01002003 \ lastline: 4,
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002004 \ wrap: v:true,
2005 \ scrollbar: v:true,
2006 \ mapping: v:false,
2007 \ filter: funcref('Popup_filter')
2008 \ })
2009 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002010 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002011 map <silent> <F4> :call test_setmouse(4, 42)<CR>
2012 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002013 END
2014 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002015 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002016 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
2017
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002018 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002019 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002020 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
2021
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002022 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002023 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002024 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
2025
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002026 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002027 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002028 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
2029
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02002030 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb', firstline: 5})\<CR>")
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002031 " this scrolls two lines (half the window height)
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002032 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
2033 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
2034
2035 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2036 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
2037
2038 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02002039 " wait a bit, otherwise it fails sometimes (double click recognized?)
2040 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002041 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2042 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
2043
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002044 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2045 sleep 100m
2046 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2047 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
2048
2049 call term_sendkeys(buf, ":call ClickBot()\<CR>")
2050 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
2051
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002052 " remove the minwidth and maxheight
2053 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxheight: 0, minwidth: 0})\<CR>")
Bram Moolenaar7e0f4622019-09-17 21:23:39 +02002054 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002055 call VerifyScreenDump(buf, 'Test_popupwin_scroll_10', {})
2056
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002057 " check size with non-wrapping lines
2058 call term_sendkeys(buf, ":call PopupScroll()\<CR>")
2059 call VerifyScreenDump(buf, 'Test_popupwin_scroll_11', {})
2060
2061 " check size with wrapping lines
2062 call term_sendkeys(buf, "j")
2063 call VerifyScreenDump(buf, 'Test_popupwin_scroll_12', {})
2064 call term_sendkeys(buf, "x")
2065
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002066 " clean up
2067 call StopVimInTerminal(buf)
2068 call delete('XtestPopupScroll')
2069endfunc
2070
Bram Moolenaar437a7462019-07-05 20:17:22 +02002071func Test_popup_fitting_scrollbar()
2072 " this was causing a crash, divide by zero
2073 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002074 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002075 \ scrollbar: 1,
2076 \ maxwidth: 10,
2077 \ maxheight: 5,
2078 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02002079 redraw
2080 call popup_clear()
2081endfunc
2082
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002083func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002084 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002085
2086 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002087 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002088 let p = popup_create('test', opts)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002089 eval p->popup_settext('this is a text')
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002090 END
2091
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002092 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002093 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002094 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
2095
2096 " Setting to empty string clears it
2097 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
2098 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
2099
2100 " Setting a list
2101 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2102 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2103
2104 " Shrinking with a list
2105 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
2106 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
2107
2108 " Growing with a list
2109 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2110 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2111
2112 " Empty list clears
2113 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
2114 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
2115
2116 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002117 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002118 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
2119
Bram Moolenaarb0992022020-01-30 14:55:42 +01002120 " range() (doesn't work)
2121 call term_sendkeys(buf, ":call popup_settext(p, range(4, 8))\<CR>")
2122 call VerifyScreenDump(buf, 'Test_popup_settext_07', {})
2123
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002124 " clean up
2125 call StopVimInTerminal(buf)
2126 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002127endfunc
2128
2129func Test_popup_hidden()
2130 new
2131
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002132 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002133 redraw
2134 call assert_equal(0, popup_getpos(winid).visible)
2135 call popup_close(winid)
2136
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002137 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002138 redraw
2139 call assert_equal(0, popup_getpos(winid).visible)
2140 call popup_close(winid)
2141
2142 func QuitCallback(id, res)
2143 let s:cb_winid = a:id
2144 let s:cb_res = a:res
2145 endfunc
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002146 let winid = 'make a choice'->popup_dialog(#{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002147 \ filter: 'popup_filter_yesno',
2148 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002149 \ })
2150 redraw
2151 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02002152 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
2153 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002154 exe "normal anot used by filter\<Esc>"
2155 call assert_equal('not used by filter', getline(1))
2156
2157 call popup_show(winid)
2158 call feedkeys('y', "xt")
2159 call assert_equal(1, s:cb_res)
2160
2161 bwipe!
2162 delfunc QuitCallback
2163endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02002164
2165" Test options not checked elsewhere
2166func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002167 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002168 let options = popup_getoptions(winid)
2169 call assert_equal(1, options.wrap)
2170 call assert_equal(0, options.drag)
2171 call assert_equal('Beautiful', options.highlight)
2172
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002173 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002174 let options = popup_getoptions(winid)
2175 call assert_equal(0, options.wrap)
2176 call assert_equal(1, options.drag)
2177 call assert_equal('Another', options.highlight)
2178
2179 call popup_close(winid)
2180endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002181
2182func Test_popupwin_garbage_collect()
2183 func MyPopupFilter(x, winid, c)
2184 " NOP
2185 endfunc
2186
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002187 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002188 call test_garbagecollect_now()
2189 redraw
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02002190 " Must not crash caused by invalid memory access
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002191 call feedkeys('j', 'xt')
2192 call assert_true(v:true)
2193
2194 call popup_close(winid)
2195 delfunc MyPopupFilter
2196endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002197
Bram Moolenaar581ba392019-09-03 22:08:33 +02002198func Test_popupwin_filter_mode()
2199 func MyPopupFilter(winid, c)
2200 let s:typed = a:c
2201 if a:c == ':' || a:c == "\r" || a:c == 'v'
2202 " can start cmdline mode, get out, and start/stop Visual mode
2203 return 0
2204 endif
2205 return 1
2206 endfunc
2207
2208 " Normal, Visual and Insert mode
2209 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'nvi'})
2210 redraw
2211 call feedkeys('x', 'xt')
2212 call assert_equal('x', s:typed)
2213
2214 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2215 call assert_equal(':', s:typed)
2216 call assert_equal('foo', g:foo)
2217
2218 let @x = 'something'
2219 call feedkeys('v$"xy', 'xt')
2220 call assert_equal('y', s:typed)
2221 call assert_equal('something', @x) " yank command is filtered out
2222 call feedkeys('v', 'xt') " end Visual mode
2223
2224 call popup_close(winid)
2225
2226 " only Normal mode
2227 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'n'})
2228 redraw
2229 call feedkeys('x', 'xt')
2230 call assert_equal('x', s:typed)
2231
2232 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2233 call assert_equal(':', s:typed)
2234 call assert_equal('foo', g:foo)
2235
2236 let @x = 'something'
2237 call feedkeys('v$"xy', 'xt')
2238 call assert_equal('v', s:typed)
2239 call assert_notequal('something', @x)
2240
2241 call popup_close(winid)
2242
2243 " default: all modes
2244 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
2245 redraw
2246 call feedkeys('x', 'xt')
2247 call assert_equal('x', s:typed)
2248
2249 let g:foo = 'bar'
2250 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2251 call assert_equal("\r", s:typed)
2252 call assert_equal('bar', g:foo)
2253
2254 let @x = 'something'
2255 call feedkeys('v$"xy', 'xt')
2256 call assert_equal('y', s:typed)
2257 call assert_equal('something', @x) " yank command is filtered out
2258 call feedkeys('v', 'xt') " end Visual mode
2259
2260 call popup_close(winid)
2261 delfunc MyPopupFilter
2262endfunc
2263
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002264func Test_popupwin_filter_mouse()
2265 func MyPopupFilter(winid, c)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002266 let g:got_mousepos = getmousepos()
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002267 return 0
2268 endfunc
2269
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002270 call setline(1, ['.'->repeat(25)]->repeat(10))
2271 let winid = popup_create(['short', 'long line that will wrap', 'other'], #{
2272 \ line: 2,
2273 \ col: 4,
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002274 \ maxwidth: 12,
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002275 \ padding: [],
2276 \ border: [],
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002277 \ filter: 'MyPopupFilter',
2278 \ })
2279 redraw
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002280 " 123456789012345678901
2281 " 1 .....................
2282 " 2 ...+--------------+..
2283 " 3 ...| |..
2284 " 4 ...| short |..
2285 " 5 ...| long line th |..
2286 " 6 ...| at will wrap |..
2287 " 7 ...| other |..
2288 " 8 ...| |..
2289 " 9 ...+--------------+..
2290 " 10 .....................
2291 let tests = []
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002292
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002293 func AddItemOutsidePopup(tests, row, col)
2294 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2295 \ screenrow: a:row, screencol: a:col,
2296 \ winid: win_getid(), winrow: a:row, wincol: a:col,
2297 \ line: a:row, column: a:col,
2298 \ }})
2299 endfunc
2300 func AddItemInPopupBorder(tests, winid, row, col)
2301 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2302 \ screenrow: a:row, screencol: a:col,
2303 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2304 \ line: 0, column: 0,
2305 \ }})
2306 endfunc
2307 func AddItemInPopupText(tests, winid, row, col, textline, textcol)
2308 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2309 \ screenrow: a:row, screencol: a:col,
2310 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2311 \ line: a:textline, column: a:textcol,
2312 \ }})
2313 endfunc
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002314
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002315 " above and below popup
2316 for c in range(1, 21)
2317 call AddItemOutsidePopup(tests, 1, c)
2318 call AddItemOutsidePopup(tests, 10, c)
2319 endfor
2320 " left and right of popup
2321 for r in range(1, 10)
2322 call AddItemOutsidePopup(tests, r, 3)
2323 call AddItemOutsidePopup(tests, r, 20)
2324 endfor
2325 " top and bottom in popup
2326 for c in range(4, 19)
2327 call AddItemInPopupBorder(tests, winid, 2, c)
2328 call AddItemInPopupBorder(tests, winid, 3, c)
2329 call AddItemInPopupBorder(tests, winid, 8, c)
2330 call AddItemInPopupBorder(tests, winid, 9, c)
2331 endfor
2332 " left and right margin in popup
2333 for r in range(2, 9)
2334 call AddItemInPopupBorder(tests, winid, r, 4)
2335 call AddItemInPopupBorder(tests, winid, r, 5)
2336 call AddItemInPopupBorder(tests, winid, r, 18)
2337 call AddItemInPopupBorder(tests, winid, r, 19)
2338 endfor
2339 " text "short"
2340 call AddItemInPopupText(tests, winid, 4, 6, 1, 1)
2341 call AddItemInPopupText(tests, winid, 4, 10, 1, 5)
2342 call AddItemInPopupText(tests, winid, 4, 11, 1, 6)
2343 call AddItemInPopupText(tests, winid, 4, 17, 1, 6)
2344 " text "long line th"
2345 call AddItemInPopupText(tests, winid, 5, 6, 2, 1)
2346 call AddItemInPopupText(tests, winid, 5, 10, 2, 5)
2347 call AddItemInPopupText(tests, winid, 5, 17, 2, 12)
2348 " text "at will wrap"
2349 call AddItemInPopupText(tests, winid, 6, 6, 2, 13)
2350 call AddItemInPopupText(tests, winid, 6, 10, 2, 17)
2351 call AddItemInPopupText(tests, winid, 6, 17, 2, 24)
2352 " text "other"
2353 call AddItemInPopupText(tests, winid, 7, 6, 3, 1)
2354 call AddItemInPopupText(tests, winid, 7, 10, 3, 5)
2355 call AddItemInPopupText(tests, winid, 7, 11, 3, 6)
2356 call AddItemInPopupText(tests, winid, 7, 17, 3, 6)
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002357
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002358 for item in tests
2359 call test_setmouse(item.clickrow, item.clickcol)
2360 call feedkeys("\<LeftMouse>", 'xt')
2361 call assert_equal(item.result, g:got_mousepos)
2362 endfor
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002363
2364 call popup_close(winid)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002365 enew!
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002366 delfunc MyPopupFilter
2367endfunc
2368
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002369func Test_popupwin_with_buffer()
2370 call writefile(['some text', 'in a buffer'], 'XsomeFile')
2371 let buf = bufadd('XsomeFile')
2372 call assert_equal(0, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002373
2374 setlocal number
2375 call setbufvar(buf, "&wrapmargin", 13)
2376
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002377 let winid = popup_create(buf, {})
2378 call assert_notequal(0, winid)
2379 let pos = popup_getpos(winid)
2380 call assert_equal(2, pos.height)
2381 call assert_equal(1, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002382
2383 " window-local option is set to default, buffer-local is not
2384 call assert_equal(0, getwinvar(winid, '&number'))
2385 call assert_equal(13, getbufvar(buf, '&wrapmargin'))
2386
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002387 call popup_close(winid)
2388 call assert_equal({}, popup_getpos(winid))
2389 call assert_equal(1, bufloaded(buf))
2390 exe 'bwipe! ' .. buf
Bram Moolenaar46451042019-08-24 15:50:46 +02002391 setlocal nonumber
Bram Moolenaar7866b872019-07-01 22:21:01 +02002392
2393 edit test_popupwin.vim
2394 let winid = popup_create(bufnr(''), {})
2395 redraw
2396 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002397 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002398endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02002399
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002400func Test_popupwin_terminal_buffer()
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002401 CheckFeature terminal
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002402 CheckUnix
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002403
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002404 let origwin = win_getid()
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002405 let ptybuf = term_start(&shell, #{hidden: 1})
Bram Moolenaar631ebc42020-02-03 22:15:26 +01002406 let winid = popup_create(ptybuf, #{minwidth: 40, minheight: 10})
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002407 " Wait for shell to start
2408 sleep 200m
Bram Moolenaar631ebc42020-02-03 22:15:26 +01002409 " Check this doesn't crash
2410 call assert_equal(winnr(), winnr('j'))
2411 call assert_equal(winnr(), winnr('k'))
2412 call assert_equal(winnr(), winnr('h'))
2413 call assert_equal(winnr(), winnr('l'))
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002414 " Cannot quit while job is running
2415 call assert_fails('call feedkeys("\<C-W>:quit\<CR>", "xt")', 'E948:')
2416 call feedkeys("exit\<CR>", 'xt')
2417 " Wait for shell to exit
2418 sleep 100m
2419 call feedkeys(":quit\<CR>", 'xt')
2420 call assert_equal(origwin, win_getid())
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002421endfunc
2422
Bram Moolenaar934470e2019-09-01 23:27:05 +02002423func Test_popupwin_with_buffer_and_filter()
2424 new Xwithfilter
2425 call setline(1, range(100))
2426 let bufnr = bufnr()
2427 hide
2428
2429 func BufferFilter(win, key)
2430 if a:key == 'G'
2431 " recursive use of "G" does not cause problems.
2432 call win_execute(a:win, 'normal! G')
2433 return 1
2434 endif
2435 return 0
2436 endfunc
2437
2438 let winid = popup_create(bufnr, #{maxheight: 5, filter: 'BufferFilter'})
2439 call assert_equal(1, popup_getpos(winid).firstline)
2440 redraw
2441 call feedkeys("G", 'xt')
2442 call assert_equal(99, popup_getpos(winid).firstline)
2443
2444 call popup_close(winid)
2445 exe 'bwipe! ' .. bufnr
2446endfunc
2447
Bram Moolenaare296e312019-07-03 23:20:18 +02002448func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002449 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002450 \ maxwidth: 40,
2451 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02002452 \ })
2453 for top in range(1, 20)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002454 eval winid->popup_setoptions(#{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02002455 redraw
2456 call assert_equal(19, popup_getpos(winid).width)
2457 endfor
2458 call popup_clear()
2459endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02002460
2461func Test_popupwin_buf_close()
2462 let buf = bufadd('Xtestbuf')
2463 call bufload(buf)
2464 call setbufline(buf, 1, ['just', 'some', 'lines'])
2465 let winid = popup_create(buf, {})
2466 redraw
2467 call assert_equal(3, popup_getpos(winid).height)
2468 let bufinfo = getbufinfo(buf)[0]
2469 call assert_equal(1, bufinfo.changed)
2470 call assert_equal(0, bufinfo.hidden)
2471 call assert_equal(0, bufinfo.listed)
2472 call assert_equal(1, bufinfo.loaded)
2473 call assert_equal([], bufinfo.windows)
2474 call assert_equal([winid], bufinfo.popups)
2475
2476 call popup_close(winid)
2477 call assert_equal({}, popup_getpos(winid))
2478 let bufinfo = getbufinfo(buf)[0]
2479 call assert_equal(1, bufinfo.changed)
2480 call assert_equal(1, bufinfo.hidden)
2481 call assert_equal(0, bufinfo.listed)
2482 call assert_equal(1, bufinfo.loaded)
2483 call assert_equal([], bufinfo.windows)
2484 call assert_equal([], bufinfo.popups)
2485 exe 'bwipe! ' .. buf
2486endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02002487
2488func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002489 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02002490
2491 let lines =<< trim END
2492 call setline(1, range(1, 10))
2493 hi ScrollThumb ctermbg=blue
2494 hi ScrollBar ctermbg=red
2495 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002496 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002497 \ maxwidth: 10,
2498 \ maxheight: 3,
2499 \ pos : 'topleft',
2500 \ col : a:col,
2501 \ line : a:line,
2502 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02002503 \ })
2504 endfunc
2505 call PopupMenu(['x'], 1, 1)
2506 call PopupMenu(['123456789|'], 1, 16)
2507 call PopupMenu(['123456789|' .. ' '], 7, 1)
2508 call PopupMenu([repeat('123456789|', 100)], 7, 16)
2509 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
2510 END
2511 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002512 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02002513 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
2514
2515 " close the menu popupwin.
2516 call term_sendkeys(buf, " ")
2517 call term_sendkeys(buf, " ")
2518 call term_sendkeys(buf, " ")
2519 call term_sendkeys(buf, " ")
2520 call term_sendkeys(buf, " ")
2521
2522 " clean up
2523 call StopVimInTerminal(buf)
2524 call delete('XtestPopupMenuMaxWidth')
2525endfunc
2526
Bram Moolenaara901a372019-07-13 16:38:50 +02002527func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002528 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02002529
2530 let lines =<< trim END
2531 call setline(1, range(1, 20))
2532 hi ScrollThumb ctermbg=blue
2533 hi ScrollBar ctermbg=red
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002534 eval ['one', 'two', 'three', 'four', 'five',
2535 \ 'six', 'seven', 'eight', 'nine']
2536 \ ->popup_menu(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002537 \ minwidth: 8,
2538 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02002539 \ })
2540 END
2541 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002542 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02002543
2544 call term_sendkeys(buf, "j")
2545 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
2546
2547 call term_sendkeys(buf, "jjj")
2548 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
2549
2550 " if the cursor is the bottom line, it stays at the bottom line.
2551 call term_sendkeys(buf, repeat("j", 20))
2552 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
2553
2554 call term_sendkeys(buf, "kk")
2555 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
2556
2557 call term_sendkeys(buf, "k")
2558 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
2559
2560 " if the cursor is in the top line, it stays in the top line.
2561 call term_sendkeys(buf, repeat("k", 20))
2562 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
2563
2564 " close the menu popupwin.
2565 call term_sendkeys(buf, " ")
2566
2567 " clean up
2568 call StopVimInTerminal(buf)
2569 call delete('XtestPopupMenuScroll')
2570endfunc
2571
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002572func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002573 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002574
2575 let lines =<< trim END
2576 function! MyFilter(winid, key) abort
2577 if a:key == "0"
2578 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
2579 return 1
2580 endif
2581 if a:key == "G"
2582 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
2583 return 1
2584 endif
2585 if a:key == "j"
2586 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
2587 return 1
2588 endif
2589 if a:key == "k"
2590 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
2591 return 1
2592 endif
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002593 if a:key == ':'
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002594 call popup_close(a:winid)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002595 return 0
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002596 endif
2597 return 0
2598 endfunction
2599 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
2600 \ maxheight : 3,
2601 \ filter : 'MyFilter'
2602 \ })
2603 END
2604 call writefile(lines, 'XtestPopupMenuFilter')
2605 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
2606
2607 call term_sendkeys(buf, "j")
2608 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
2609
2610 call term_sendkeys(buf, "k")
2611 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
2612
2613 call term_sendkeys(buf, "G")
2614 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
2615
2616 call term_sendkeys(buf, "0")
2617 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
2618
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002619 " check that when the popup is closed in the filter the screen is redrawn
2620 call term_sendkeys(buf, ":")
2621 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_5', {})
2622 call term_sendkeys(buf, "\<CR>")
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002623
2624 " clean up
2625 call StopVimInTerminal(buf)
2626 call delete('XtestPopupMenuFilter')
2627endfunc
2628
2629func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002630 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002631
2632 let winid = popup_create('some text', {})
2633 call assert_equal(0, popup_getoptions(winid).cursorline)
2634 call popup_close(winid)
2635
2636 let winid = popup_create('some text', #{ cursorline: 1, })
2637 call assert_equal(1, popup_getoptions(winid).cursorline)
2638 call popup_close(winid)
2639
2640 let winid = popup_create('some text', #{ cursorline: 0, })
2641 call assert_equal(0, popup_getoptions(winid).cursorline)
2642 call popup_close(winid)
2643
2644 let winid = popup_menu('some text', {})
2645 call assert_equal(1, popup_getoptions(winid).cursorline)
2646 call popup_close(winid)
2647
2648 let winid = popup_menu('some text', #{ cursorline: 1, })
2649 call assert_equal(1, popup_getoptions(winid).cursorline)
2650 call popup_close(winid)
2651
2652 let winid = popup_menu('some text', #{ cursorline: 0, })
2653 call assert_equal(0, popup_getoptions(winid).cursorline)
2654 call popup_close(winid)
2655
2656 " ---------
2657 " Pattern 1
2658 " ---------
2659 let lines =<< trim END
2660 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
2661 END
2662 call writefile(lines, 'XtestPopupCursorLine')
2663 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2664 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
2665 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2666 call StopVimInTerminal(buf)
2667
2668 " ---------
2669 " Pattern 2
2670 " ---------
2671 let lines =<< trim END
2672 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
2673 END
2674 call writefile(lines, 'XtestPopupCursorLine')
2675 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2676 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
2677 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2678 call StopVimInTerminal(buf)
2679
2680 " ---------
2681 " Pattern 3
2682 " ---------
2683 let lines =<< trim END
2684 function! MyFilter(winid, key) abort
2685 if a:key == "j"
2686 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2687 return 1
2688 endif
2689 if a:key == 'x'
2690 call popup_close(a:winid)
2691 return 1
2692 endif
2693 return 0
2694 endfunction
2695 call popup_menu(['111', '222', '333'], #{
2696 \ cursorline : 0,
2697 \ maxheight : 2,
2698 \ filter : 'MyFilter',
2699 \ })
2700 END
2701 call writefile(lines, 'XtestPopupCursorLine')
2702 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2703 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
2704 call term_sendkeys(buf, "j")
2705 call term_sendkeys(buf, "j")
2706 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
2707 call term_sendkeys(buf, "x")
2708 call StopVimInTerminal(buf)
2709
2710 " ---------
2711 " Pattern 4
2712 " ---------
2713 let lines =<< trim END
2714 function! MyFilter(winid, key) abort
2715 if a:key == "j"
2716 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2717 return 1
2718 endif
2719 if a:key == 'x'
2720 call popup_close(a:winid)
2721 return 1
2722 endif
2723 return 0
2724 endfunction
2725 call popup_menu(['111', '222', '333'], #{
2726 \ cursorline : 1,
2727 \ maxheight : 2,
2728 \ filter : 'MyFilter',
2729 \ })
2730 END
2731 call writefile(lines, 'XtestPopupCursorLine')
2732 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2733 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
2734 call term_sendkeys(buf, "j")
2735 call term_sendkeys(buf, "j")
2736 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
2737 call term_sendkeys(buf, "x")
2738 call StopVimInTerminal(buf)
2739
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02002740 " ---------
2741 " Cursor in second line when creating the popup
2742 " ---------
2743 let lines =<< trim END
2744 let winid = popup_create(['111', '222', '333'], #{
2745 \ cursorline : 1,
2746 \ })
2747 call win_execute(winid, "2")
2748 END
2749 call writefile(lines, 'XtestPopupCursorLine')
2750 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2751 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_7', {})
2752 call StopVimInTerminal(buf)
2753
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002754 call delete('XtestPopupCursorLine')
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01002755
2756 " ---------
2757 " Use current buffer for popupmenu
2758 " ---------
2759 let lines =<< trim END
2760 call setline(1, ['one', 'two', 'three'])
2761 let winid = popup_create(bufnr('%'), #{
2762 \ cursorline : 1,
2763 \ })
2764 call win_execute(winid, "2")
2765 END
2766 call writefile(lines, 'XtestPopupCursorLine')
2767 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2768 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_8', {})
2769 call StopVimInTerminal(buf)
2770
2771 call delete('XtestPopupCursorLine')
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002772endfunc
2773
Bram Moolenaarf914a332019-07-20 15:09:56 +02002774func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002775 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002776 CheckFeature quickfix
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002777
Bram Moolenaarf914a332019-07-20 15:09:56 +02002778 call writefile([
2779 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
2780 \ "another\tXtagfile\t/^this is another",
2781 \ "theword\tXtagfile\t/^theword"],
2782 \ 'Xtags')
2783 call writefile(range(1,20)
2784 \ + ['theword is here']
2785 \ + range(22, 27)
2786 \ + ['this is another place']
2787 \ + range(29, 40),
2788 \ "Xtagfile")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002789 call writefile(range(1,10)
2790 \ + ['searched word is here']
2791 \ + range(12, 20),
2792 \ "Xheader.h")
Bram Moolenaarf914a332019-07-20 15:09:56 +02002793 let lines =<< trim END
2794 set tags=Xtags
2795 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002796 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002797 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002798 \ 'three',
2799 \ 'four',
2800 \ 'five',
2801 \ 'six',
2802 \ 'seven',
2803 \ 'find theword somewhere',
2804 \ 'nine',
2805 \ 'this is another word',
2806 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02002807 set previewpopup=height:4,width:40
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002808 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02002809 END
2810 call writefile(lines, 'XtestPreviewPopup')
2811 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
2812
2813 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
2814 call term_sendkeys(buf, ":\<CR>")
2815 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
2816
2817 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2818 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
2819
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002820 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002821 call term_sendkeys(buf, ":\<CR>")
2822 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
2823
2824 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2825 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
2826
Bram Moolenaar799439a2020-02-11 21:44:17 +01002827 call term_sendkeys(buf, ":silent cd ..\<CR>:\<CR>")
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002828 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar799439a2020-02-11 21:44:17 +01002829 call term_sendkeys(buf, ":silent cd testdir\<CR>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002830
2831 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02002832 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002833 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
2834
2835 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
2836 call term_sendkeys(buf, ":\<CR>")
2837 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
2838
2839 call term_sendkeys(buf, ":pclose\<CR>")
2840 call term_sendkeys(buf, ":psearch searched\<CR>")
2841 call term_sendkeys(buf, ":\<CR>")
2842 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002843
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01002844 call term_sendkeys(buf, "\<C-W>p")
2845 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_9', {})
2846
2847 call term_sendkeys(buf, ":call win_execute(popup_findpreview(), 'call popup_clear()')\<CR>")
2848 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_10', {})
2849
Bram Moolenaarf914a332019-07-20 15:09:56 +02002850 call StopVimInTerminal(buf)
2851 call delete('Xtags')
2852 call delete('Xtagfile')
2853 call delete('XtestPreviewPopup')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002854 call delete('Xheader.h')
Bram Moolenaarf914a332019-07-20 15:09:56 +02002855endfunc
2856
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002857func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002858 let lines =<< trim END
2859 set completeopt+=preview,popup
2860 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002861 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002862
2863 func CompleteFuncDict(findstart, base)
2864 if a:findstart
2865 if col('.') > 10
2866 return col('.') - 10
2867 endif
2868 return 0
2869 endif
2870
2871 return {
2872 \ 'words': [
2873 \ {
2874 \ 'word': 'aword',
2875 \ 'abbr': 'wrd',
2876 \ 'menu': 'extra text',
2877 \ 'info': 'words are cool',
2878 \ 'kind': 'W',
2879 \ 'user_data': 'test'
2880 \ },
2881 \ {
2882 \ 'word': 'anotherword',
2883 \ 'abbr': 'anotwrd',
2884 \ 'menu': 'extra text',
2885 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
2886 \ 'kind': 'W',
2887 \ 'user_data': 'notest'
2888 \ },
2889 \ {
2890 \ 'word': 'noinfo',
2891 \ 'abbr': 'noawrd',
2892 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002893 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002894 \ 'kind': 'W',
2895 \ 'user_data': 'notest'
2896 \ },
2897 \ {
2898 \ 'word': 'thatword',
2899 \ 'abbr': 'thatwrd',
2900 \ 'menu': 'extra text',
2901 \ 'info': 'that word is cool',
2902 \ 'kind': 'W',
2903 \ 'user_data': 'notest'
2904 \ },
2905 \ ]
2906 \ }
2907 endfunc
2908 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002909 func ChangeColor()
2910 let id = popup_findinfo()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002911 eval id->popup_setoptions(#{highlight: 'InfoPopup'})
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002912 endfunc
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002913
2914 func InfoHidden()
2915 set completepopup=height:4,border:off,align:menu
2916 set completeopt-=popup completeopt+=popuphidden
2917 au CompleteChanged * call HandleChange()
2918 endfunc
2919
2920 let s:counter = 0
2921 func HandleChange()
2922 let s:counter += 1
2923 let selected = complete_info(['selected']).selected
2924 if selected <= 0
2925 " First time: do nothing, info remains hidden
2926 return
2927 endif
2928 if selected == 1
2929 " Second time: show info right away
2930 let id = popup_findinfo()
2931 if id
2932 call popup_settext(id, 'immediate info ' .. s:counter)
2933 call popup_show(id)
2934 endif
2935 else
2936 " Third time: show info after a short delay
2937 call timer_start(100, 'ShowInfo')
2938 endif
2939 endfunc
2940
2941 func ShowInfo(...)
2942 let id = popup_findinfo()
2943 if id
2944 call popup_settext(id, 'async info ' .. s:counter)
2945 call popup_show(id)
2946 endif
2947 endfunc
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002948 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002949 return lines
2950endfunc
2951
2952func Test_popupmenu_info_border()
2953 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002954 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002955
2956 let lines = Get_popupmenu_lines()
2957 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002958 call writefile(lines, 'XtestInfoPopup')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002959
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002960 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
2961 call term_wait(buf, 50)
2962
2963 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2964 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
2965
2966 call term_sendkeys(buf, "\<C-N>")
2967 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
2968
2969 call term_sendkeys(buf, "\<C-N>")
2970 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
2971
2972 call term_sendkeys(buf, "\<C-N>\<C-N>")
2973 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
2974
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02002975 " info on the left with scrollbar
2976 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
2977 call term_sendkeys(buf, "\<C-N>\<C-N>")
2978 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
2979
Bram Moolenaar202c3f72019-11-21 12:12:35 +01002980 " Test that the popupmenu's scrollbar and infopopup do not overlap
2981 call term_sendkeys(buf, "\<Esc>")
2982 call term_sendkeys(buf, ":set pumheight=3\<CR>")
2983 call term_sendkeys(buf, "cc\<C-X>\<C-U>")
2984 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_6', {})
2985
Bram Moolenaarca7c0782020-01-14 20:42:48 +01002986 " Hide the info popup, cycle trough buffers, make sure it didn't get
2987 " deleted.
2988 call term_sendkeys(buf, "\<Esc>")
2989 call term_sendkeys(buf, ":set hidden\<CR>")
2990 call term_sendkeys(buf, ":bn\<CR>")
2991 call term_sendkeys(buf, ":bn\<CR>")
2992 call term_sendkeys(buf, "otest text test text\<C-X>\<C-U>")
2993 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_7', {})
2994
2995 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002996 call StopVimInTerminal(buf)
2997 call delete('XtestInfoPopup')
2998endfunc
2999
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003000func Test_popupmenu_info_noborder()
3001 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003002 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003003
3004 let lines = Get_popupmenu_lines()
3005 call add(lines, 'set completepopup=height:4,border:off')
3006 call writefile(lines, 'XtestInfoPopupNb')
3007
3008 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
3009 call term_wait(buf, 50)
3010
3011 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3012 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
3013
3014 call StopVimInTerminal(buf)
3015 call delete('XtestInfoPopupNb')
3016endfunc
3017
Bram Moolenaar258cef52019-08-21 17:29:29 +02003018func Test_popupmenu_info_align_menu()
3019 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003020 CheckFeature quickfix
Bram Moolenaar258cef52019-08-21 17:29:29 +02003021
3022 let lines = Get_popupmenu_lines()
3023 call add(lines, 'set completepopup=height:4,border:off,align:menu')
3024 call writefile(lines, 'XtestInfoPopupNb')
3025
3026 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
3027 call term_wait(buf, 50)
3028
3029 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3030 call term_sendkeys(buf, "\<C-N>")
3031 call term_sendkeys(buf, "\<C-N>")
3032 call term_sendkeys(buf, "\<C-N>")
3033 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
3034
3035 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
3036 call term_sendkeys(buf, "\<C-N>")
3037 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
3038
3039 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003040 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02003041 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
3042 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
3043 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
3044
3045 call StopVimInTerminal(buf)
3046 call delete('XtestInfoPopupNb')
3047endfunc
3048
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003049func Test_popupmenu_info_hidden()
3050 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003051 CheckFeature quickfix
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003052
3053 let lines = Get_popupmenu_lines()
3054 call add(lines, 'call InfoHidden()')
3055 call writefile(lines, 'XtestInfoPopupHidden')
3056
3057 let buf = RunVimInTerminal('-S XtestInfoPopupHidden', #{rows: 14})
3058 call term_wait(buf, 50)
3059
3060 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3061 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_1', {})
3062
3063 call term_sendkeys(buf, "\<C-N>")
3064 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_2', {})
3065
3066 call term_sendkeys(buf, "\<C-N>")
3067 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_3', {})
3068
3069 call term_sendkeys(buf, "\<Esc>")
3070 call StopVimInTerminal(buf)
3071 call delete('XtestInfoPopupHidden')
3072endfunc
3073
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003074func Test_popupmenu_info_too_wide()
3075 CheckScreendump
3076 CheckFeature quickfix
3077
3078 let lines =<< trim END
3079 call setline(1, range(10))
3080
3081 set completeopt+=preview,popup
3082 set completepopup=align:menu
3083 set omnifunc=OmniFunc
3084 hi InfoPopup ctermbg=lightgrey
3085
3086 func OmniFunc(findstart, base)
3087 if a:findstart
3088 return 0
3089 endif
3090
3091 let menuText = 'some long text to make sure the menu takes up all of the width of the window'
3092 return #{
3093 \ words: [
3094 \ #{
3095 \ word: 'scrap',
3096 \ menu: menuText,
3097 \ info: "other words are\ncooler than this and some more text\nto make wrap",
3098 \ },
3099 \ #{
3100 \ word: 'scappier',
3101 \ menu: menuText,
3102 \ info: 'words are cool',
3103 \ },
3104 \ #{
3105 \ word: 'scrappier2',
3106 \ menu: menuText,
3107 \ info: 'words are cool',
3108 \ },
3109 \ ]
3110 \ }
3111 endfunc
3112 END
3113
3114 call writefile(lines, 'XtestInfoPopupWide')
3115 let buf = RunVimInTerminal('-S XtestInfoPopupWide', #{rows: 8})
3116 call term_wait(buf, 50)
3117
3118 call term_sendkeys(buf, "Ascr\<C-X>\<C-O>")
3119 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_wide_1', {})
3120
3121 call term_sendkeys(buf, "\<Esc>")
3122 call StopVimInTerminal(buf)
3123 call delete('XtestInfoPopupWide')
3124endfunc
3125
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003126func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02003127 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003128 call popup_clear()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003129 let winid = 'nothing wrong'->popup_notification({})
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003130 call assert_equal(bufnr, winbufnr(winid))
3131 call popup_clear()
3132endfunc
3133
Bram Moolenaar1824f452019-10-02 23:06:46 +02003134func Test_popupwin_getoptions_tablocal()
3135 topleft split
3136 let win1 = popup_create('nothing', #{maxheight: 8})
3137 let win2 = popup_create('something', #{maxheight: 10})
3138 let win3 = popup_create('something', #{maxheight: 15})
3139 call assert_equal(8, popup_getoptions(win1).maxheight)
3140 call assert_equal(10, popup_getoptions(win2).maxheight)
3141 call assert_equal(15, popup_getoptions(win3).maxheight)
3142 call popup_clear()
3143 quit
3144endfunc
3145
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02003146func Test_popupwin_cancel()
3147 let win1 = popup_create('one', #{line: 5, filter: {... -> 0}})
3148 let win2 = popup_create('two', #{line: 10, filter: {... -> 0}})
3149 let win3 = popup_create('three', #{line: 15, filter: {... -> 0}})
3150 call assert_equal(5, popup_getpos(win1).line)
3151 call assert_equal(10, popup_getpos(win2).line)
3152 call assert_equal(15, popup_getpos(win3).line)
3153 " TODO: this also works without patch 8.1.2110
3154 call feedkeys("\<C-C>", 'xt')
3155 call assert_equal(5, popup_getpos(win1).line)
3156 call assert_equal(10, popup_getpos(win2).line)
3157 call assert_equal({}, popup_getpos(win3))
3158 call feedkeys("\<C-C>", 'xt')
3159 call assert_equal(5, popup_getpos(win1).line)
3160 call assert_equal({}, popup_getpos(win2))
3161 call assert_equal({}, popup_getpos(win3))
3162 call feedkeys("\<C-C>", 'xt')
3163 call assert_equal({}, popup_getpos(win1))
3164 call assert_equal({}, popup_getpos(win2))
3165 call assert_equal({}, popup_getpos(win3))
3166endfunc
3167
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003168func Test_popupwin_filter_redraw()
3169 " Create two popups with a filter that closes the popup when typing "0".
3170 " Both popups should close, even though the redraw also calls
3171 " popup_reset_handled()
3172
3173 func CloseFilter(winid, key)
3174 if a:key == '0'
3175 call popup_close(a:winid)
3176 redraw
3177 endif
3178 return 0 " pass the key
3179 endfunc
3180
3181 let id1 = popup_create('first one', #{
3182 \ line: 1,
3183 \ col: 1,
3184 \ filter: 'CloseFilter',
3185 \ })
3186 let id2 = popup_create('second one', #{
3187 \ line: 9,
3188 \ col: 1,
3189 \ filter: 'CloseFilter',
3190 \ })
3191 call assert_equal(1, popup_getpos(id1).line)
3192 call assert_equal(9, popup_getpos(id2).line)
3193
3194 call feedkeys('0', 'xt')
3195 call assert_equal({}, popup_getpos(id1))
3196 call assert_equal({}, popup_getpos(id2))
3197
3198 call popup_clear()
3199 delfunc CloseFilter
3200endfunc
3201
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003202func Test_popupwin_double_width()
3203 CheckScreendump
3204
3205 let lines =<< trim END
3206 call setline(1, 'x你好世界你好世你好世界你好')
3207 call setline(2, '你好世界你好世你好世界你好')
3208 call setline(3, 'x你好世界你好世你好世界你好')
3209 call popup_create('你好,世界 - 你好,世界xxxxx', #{line: 1, col: 3, maxwidth: 14})
3210 END
3211 call writefile(lines, 'XtestPopupWide')
3212
3213 let buf = RunVimInTerminal('-S XtestPopupWide', #{rows: 10})
3214 call VerifyScreenDump(buf, 'Test_popupwin_doublewidth_1', {})
3215
3216 call StopVimInTerminal(buf)
3217 call delete('XtestPopupWide')
3218endfunc
3219
3220func Test_popupwin_sign()
3221 CheckScreendump
3222
3223 let lines =<< trim END
3224 call setline(1, range(10))
3225 call sign_define('Current', {
3226 \ 'text': '>>',
3227 \ 'texthl': 'WarningMsg',
3228 \ 'linehl': 'Error',
3229 \ })
3230 call sign_define('Other', {
3231 \ 'text': '#!',
3232 \ 'texthl': 'Error',
3233 \ 'linehl': 'Search',
3234 \ })
3235 let winid = popup_create(['hello', 'bright', 'world'], {
3236 \ 'minwidth': 20,
3237 \ })
3238 call setwinvar(winid, "&signcolumn", "yes")
3239 let winbufnr = winbufnr(winid)
3240
3241 " add sign to current buffer, shows
3242 call sign_place(1, 'Selected', 'Current', bufnr('%'), {'lnum': 1})
3243 " add sign to current buffer, does not show
3244 call sign_place(2, 'PopUpSelected', 'Other', bufnr('%'), {'lnum': 2})
3245
3246 " add sign to popup buffer, shows
3247 call sign_place(3, 'PopUpSelected', 'Other', winbufnr, {'lnum': 1})
3248 " add sign to popup buffer, does not show
3249 call sign_place(4, 'Selected', 'Current', winbufnr, {'lnum': 2})
3250 END
3251 call writefile(lines, 'XtestPopupSign')
3252
3253 let buf = RunVimInTerminal('-S XtestPopupSign', #{rows: 10})
3254 call VerifyScreenDump(buf, 'Test_popupwin_sign_1', {})
3255
3256 call StopVimInTerminal(buf)
3257 call delete('XtestPopupSign')
3258endfunc
3259
Bram Moolenaar99ebf222019-12-10 23:44:48 +01003260func Test_popupwin_bufnr()
3261 let popwin = popup_create(['blah'], #{})
3262 let popbuf = winbufnr(popwin)
3263 split asdfasdf
3264 let newbuf = bufnr()
3265 call assert_true(newbuf > popbuf, 'New buffer number is higher')
3266 call assert_equal(newbuf, bufnr('$'))
3267 call popup_clear()
3268 let popwin = popup_create(['blah'], #{})
3269 " reuses previous buffer number
3270 call assert_equal(popbuf, winbufnr(popwin))
3271 call assert_equal(newbuf, bufnr('$'))
3272
3273 call popup_clear()
3274 bwipe!
3275endfunc
3276
Bram Moolenaarec084d32020-02-28 22:44:47 +01003277func Test_popupwin_filter_input_multibyte()
3278 func MyPopupFilter(winid, c)
3279 let g:bytes = range(a:c->strlen())->map({i -> char2nr(a:c[i])})
3280 return 0
3281 endfunc
3282 let winid = popup_create('', #{mapping: 0, filter: 'MyPopupFilter'})
3283
3284 " UTF-8: E3 80 80, including K_SPECIAL(0x80)
3285 call feedkeys("\u3000", 'xt')
3286 call assert_equal([0xe3, 0x80, 0x80], g:bytes)
3287
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01003288 " UTF-8: E3 80 9B, including CSI(0x9B)
3289 call feedkeys("\u301b", 'xt')
3290 call assert_equal([0xe3, 0x80, 0x9b], g:bytes)
Bram Moolenaarec084d32020-02-28 22:44:47 +01003291
3292 call popup_clear()
3293 delfunc MyPopupFilter
3294 unlet g:bytes
3295endfunc
3296
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003297func Test_popupwin_atcursor_far_right()
3298 new
3299
3300 " this was getting stuck
3301 set signcolumn=yes
3302 call setline(1, repeat('=', &columns))
3303 normal! ggg$
3304 call popup_atcursor(repeat('x', 500), #{moved: 'any', border: []})
3305
3306 bwipe!
3307 set signcolumn&
3308endfunc
3309
Bram Moolenaar0f1563f2020-03-20 21:15:51 +01003310func Test_popupwin_splitmove()
3311 vsplit
3312 let win2 = win_getid()
3313 let popup_winid = popup_dialog('hello', {})
3314 call assert_fails('call win_splitmove(popup_winid, win2)', 'E957:')
3315 call assert_fails('call win_splitmove(win2, popup_winid)', 'E957:')
3316
3317 call popup_clear()
3318 bwipe
3319endfunc
3320
3321
Bram Moolenaar331bafd2019-07-20 17:46:05 +02003322" vim: shiftwidth=2 sts=2