blob: 6ec4ef252967f88c90e6a3ff59a839c054565396 [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>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +020068 call TermWait(buf)
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020069 call term_sendkeys(buf, "0")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +020070 call TermWait(buf)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +020071 call term_sendkeys(buf, ":call popup_clear()\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020072 call VerifyScreenDump(buf, 'Test_popupwin_08', {})
73
Bram Moolenaar4d784b22019-05-25 19:51:39 +020074 " clean up
75 call StopVimInTerminal(buf)
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 Moolenaaref6b9792020-05-13 16:34:15 +0200431func Test_popup_firstline_dump()
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 Moolenaaref6b9792020-05-13 16:34:15 +0200452endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +0200453
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200454func Test_popup_firstline()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200455 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200456 \ maxheight: 2,
457 \ firstline: 3,
Bram Moolenaarae943152019-06-16 22:54:14 +0200458 \ })
459 call assert_equal(3, popup_getoptions(winid).firstline)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200460 call popup_setoptions(winid, #{firstline: 1})
Bram Moolenaarae943152019-06-16 22:54:14 +0200461 call assert_equal(1, popup_getoptions(winid).firstline)
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200462 eval winid->popup_close()
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200463
464 let winid = popup_create(['xxx']->repeat(50), #{
465 \ maxheight: 3,
466 \ firstline: 11,
467 \ })
468 redraw
469 call assert_equal(11, popup_getoptions(winid).firstline)
470 call assert_equal(11, popup_getpos(winid).firstline)
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +0200471 " check line() works with popup window
472 call assert_equal(11, line('.', winid))
473 call assert_equal(50, line('$', winid))
474 call assert_equal(0, line('$', 123456))
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200475
476 " Normal command changes what is displayed but not "firstline"
477 call win_execute(winid, "normal! \<c-y>")
478 call assert_equal(11, popup_getoptions(winid).firstline)
479 call assert_equal(10, popup_getpos(winid).firstline)
480
481 " Making some property change applies "firstline" again
482 call popup_setoptions(winid, #{line: 4})
483 call assert_equal(11, popup_getoptions(winid).firstline)
484 call assert_equal(11, popup_getpos(winid).firstline)
485
486 " Remove "firstline" property and scroll
487 call popup_setoptions(winid, #{firstline: 0})
488 call win_execute(winid, "normal! \<c-y>")
489 call assert_equal(0, popup_getoptions(winid).firstline)
490 call assert_equal(10, popup_getpos(winid).firstline)
491
492 " Making some property change has no side effect
493 call popup_setoptions(winid, #{line: 3})
494 call assert_equal(0, popup_getoptions(winid).firstline)
495 call assert_equal(10, popup_getpos(winid).firstline)
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200496 call popup_close(winid)
Bram Moolenaarae943152019-06-16 22:54:14 +0200497
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100498 " CTRL-D scrolls down half a page
499 let winid = popup_create(['xxx']->repeat(50), #{
500 \ maxheight: 8,
501 \ })
502 redraw
503 call assert_equal(1, popup_getpos(winid).firstline)
504 call win_execute(winid, "normal! \<C-D>")
505 call assert_equal(5, popup_getpos(winid).firstline)
506 call win_execute(winid, "normal! \<C-D>")
507 call assert_equal(9, popup_getpos(winid).firstline)
508 call win_execute(winid, "normal! \<C-U>")
509 call assert_equal(5, popup_getpos(winid).firstline)
510
511 call win_execute(winid, "normal! \<C-F>")
512 call assert_equal(11, popup_getpos(winid).firstline)
513 call win_execute(winid, "normal! \<C-B>")
514 call assert_equal(5, popup_getpos(winid).firstline)
515
Bram Moolenaarae943152019-06-16 22:54:14 +0200516 call popup_close(winid)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200517endfunc
518
Bram Moolenaara112f2d2019-09-01 17:38:09 +0200519func Test_popup_noscrolloff()
520 set scrolloff=5
521 let winid = popup_create(['xxx']->repeat(50), #{
522 \ maxheight: 5,
523 \ firstline: 11,
524 \ })
525 redraw
526 call assert_equal(11, popup_getoptions(winid).firstline)
527 call assert_equal(11, popup_getpos(winid).firstline)
528
529 call popup_setoptions(winid, #{firstline: 0})
530 call win_execute(winid, "normal! \<c-y>")
531 call assert_equal(0, popup_getoptions(winid).firstline)
532 call assert_equal(10, popup_getpos(winid).firstline)
533
534 call popup_close(winid)
535endfunc
536
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200537func Test_popup_drag()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200538 CheckScreendump
539
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200540 " create a popup that covers the command line
541 let lines =<< trim END
542 call setline(1, range(1, 20))
Bram Moolenaar356375f2019-08-24 14:46:29 +0200543 split
544 vsplit
545 $wincmd w
546 vsplit
547 1wincmd w
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200548 let winid = popup_create(['1111', '222222', '33333'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200549 \ drag: 1,
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200550 \ resize: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200551 \ border: [],
552 \ line: &lines - 4,
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200553 \ })
554 func Dragit()
555 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
556 endfunc
557 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
Bram Moolenaar356375f2019-08-24 14:46:29 +0200558 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2 - 20)<CR>
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200559 func Resize()
560 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
561 endfunc
Bram Moolenaar356375f2019-08-24 14:46:29 +0200562 map <silent> <F5> :call test_setmouse(6, 21)<CR>
563 map <silent> <F6> :call test_setmouse(7, 25)<CR>
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200564 END
565 call writefile(lines, 'XtestPopupDrag')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200566 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200567 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
568
569 call term_sendkeys(buf, ":call Dragit()\<CR>")
570 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
571
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200572 call term_sendkeys(buf, ":call Resize()\<CR>")
573 call VerifyScreenDump(buf, 'Test_popupwin_drag_03', {})
574
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200575 " clean up
576 call StopVimInTerminal(buf)
577 call delete('XtestPopupDrag')
578endfunc
579
Bram Moolenaar35910f22020-07-12 19:24:10 +0200580func Test_popup_drag_termwin()
581 CheckUnix
582 CheckScreendump
583 CheckFeature terminal
584
585 " create a popup that covers the terminal window
586 let lines =<< trim END
Bram Moolenaar452143c2020-07-15 17:38:21 +0200587 set foldmethod=marker
588 call setline(1, range(100))
589 for nr in range(7)
590 call setline(nr * 12 + 1, "fold {{{")
Bram Moolenaar9d489562020-07-30 20:08:50 +0200591 call setline(nr * 12 + 11, "end }}}")
Bram Moolenaar452143c2020-07-15 17:38:21 +0200592 endfor
593 %foldclose
Bram Moolenaar35910f22020-07-12 19:24:10 +0200594 set shell=/bin/sh noruler
Bram Moolenaara4dc6f92020-07-12 19:52:36 +0200595 let $PS1 = 'vim> '
Bram Moolenaar452143c2020-07-15 17:38:21 +0200596 terminal ++rows=4
Bram Moolenaar35910f22020-07-12 19:24:10 +0200597 $wincmd w
598 let winid = popup_create(['1111', '2222'], #{
599 \ drag: 1,
600 \ resize: 1,
601 \ border: [],
602 \ line: 3,
603 \ })
Bram Moolenaar452143c2020-07-15 17:38:21 +0200604 func DragitLeft()
Bram Moolenaar35910f22020-07-12 19:24:10 +0200605 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
606 endfunc
Bram Moolenaar452143c2020-07-15 17:38:21 +0200607 func DragitDown()
608 call feedkeys("\<F4>\<LeftMouse>\<F5>\<LeftDrag>\<LeftRelease>", "xt")
609 endfunc
610 func DragitDownLeft()
611 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
612 endfunc
Bram Moolenaar35910f22020-07-12 19:24:10 +0200613 map <silent> <F3> :call test_setmouse(3, &columns / 2)<CR>
614 map <silent> <F4> :call test_setmouse(3, &columns / 2 - 20)<CR>
Bram Moolenaar452143c2020-07-15 17:38:21 +0200615 map <silent> <F5> :call test_setmouse(12, &columns / 2)<CR>
616 map <silent> <F6> :call test_setmouse(12, &columns / 2 - 20)<CR>
Bram Moolenaar35910f22020-07-12 19:24:10 +0200617 END
618 call writefile(lines, 'XtestPopupTerm')
Bram Moolenaar452143c2020-07-15 17:38:21 +0200619 let buf = RunVimInTerminal('-S XtestPopupTerm', #{rows: 16})
Bram Moolenaar35910f22020-07-12 19:24:10 +0200620 call VerifyScreenDump(buf, 'Test_popupwin_term_01', {})
621
Bram Moolenaar452143c2020-07-15 17:38:21 +0200622 call term_sendkeys(buf, ":call DragitLeft()\<CR>")
Bram Moolenaar35910f22020-07-12 19:24:10 +0200623 call VerifyScreenDump(buf, 'Test_popupwin_term_02', {})
624
Bram Moolenaar452143c2020-07-15 17:38:21 +0200625 call term_sendkeys(buf, ":call DragitDown()\<CR>")
626 call VerifyScreenDump(buf, 'Test_popupwin_term_03', {})
627
628 call term_sendkeys(buf, ":call DragitDownLeft()\<CR>")
629 call VerifyScreenDump(buf, 'Test_popupwin_term_04', {})
630
Bram Moolenaar35910f22020-07-12 19:24:10 +0200631 " clean up
632 call StopVimInTerminal(buf)
633 call delete('XtestPopupTerm')
634endfunc
635
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200636func Test_popup_close_with_mouse()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200637 CheckScreendump
638
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200639 let lines =<< trim END
640 call setline(1, range(1, 20))
641 " With border, can click on X
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200642 let winid = popup_create('foobar', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200643 \ close: 'button',
644 \ border: [],
645 \ line: 1,
646 \ col: 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200647 \ })
648 func CloseMsg(id, result)
649 echomsg 'Popup closed with ' .. a:result
650 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200651 let winid = popup_create('notification', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200652 \ close: 'click',
653 \ line: 3,
654 \ col: 15,
655 \ callback: 'CloseMsg',
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200656 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200657 let winid = popup_create('no border here', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200658 \ close: 'button',
659 \ line: 5,
660 \ col: 3,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200661 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200662 let winid = popup_create('only padding', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200663 \ close: 'button',
664 \ padding: [],
665 \ line: 5,
666 \ col: 23,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200667 \ })
668 func CloseWithX()
669 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
670 endfunc
671 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
672 func CloseWithClick()
673 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
674 endfunc
675 map <silent> <F4> :call test_setmouse(3, 17)<CR>
Bram Moolenaarf6396232019-08-24 19:36:00 +0200676 func CreateWithMenuFilter()
677 let winid = popup_create('barfoo', #{
678 \ close: 'button',
679 \ filter: 'popup_filter_menu',
680 \ border: [],
681 \ line: 1,
682 \ col: 40,
683 \ })
684 endfunc
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200685 END
686 call writefile(lines, 'XtestPopupClose')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200687 let buf = RunVimInTerminal('-S XtestPopupClose', #{rows: 10})
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200688 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
689
690 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
691 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
692
693 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
694 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
695
Bram Moolenaarf6396232019-08-24 19:36:00 +0200696 call term_sendkeys(buf, ":call CreateWithMenuFilter()\<CR>")
697 call VerifyScreenDump(buf, 'Test_popupwin_close_04', {})
698
699 " We have to send the actual mouse code, feedkeys() would be caught the
700 " filter.
701 call term_sendkeys(buf, "\<Esc>[<0;47;1M")
702 call VerifyScreenDump(buf, 'Test_popupwin_close_05', {})
703
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200704 " clean up
705 call StopVimInTerminal(buf)
706 call delete('XtestPopupClose')
707endfunction
708
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200709func Test_popup_menu_wrap()
710 CheckScreendump
711
712 let lines =<< trim END
713 call setline(1, range(1, 20))
714 call popup_create([
715 \ 'one',
716 \ 'asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfas',
717 \ 'three',
718 \ 'four',
719 \ ], #{
720 \ pos: "botleft",
721 \ border: [],
722 \ padding: [0,1,0,1],
723 \ maxheight: 3,
724 \ cursorline: 1,
725 \ filter: 'popup_filter_menu',
726 \ })
727 END
728 call writefile(lines, 'XtestPopupWrap')
729 let buf = RunVimInTerminal('-S XtestPopupWrap', #{rows: 10})
730 call VerifyScreenDump(buf, 'Test_popupwin_wrap_1', {})
731
732 call term_sendkeys(buf, "jj")
733 call VerifyScreenDump(buf, 'Test_popupwin_wrap_2', {})
734
735 " clean up
736 call term_sendkeys(buf, "\<Esc>")
737 call StopVimInTerminal(buf)
738 call delete('XtestPopupWrap')
739endfunction
740
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200741func Test_popup_with_mask()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200742 CheckScreendump
743
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200744 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200745 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200746 hi PopupColor ctermbg=lightgrey
747 let winid = popup_create([
748 \ 'some text',
749 \ 'another line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200750 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200751 \ line: 1,
752 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100753 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200754 \ wrap: 0,
755 \ fixed: 1,
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +0200756 \ scrollbar: v:false,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200757 \ zindex: 90,
758 \ padding: [],
759 \ highlight: 'PopupColor',
760 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200761 call popup_create([
762 \ 'xxxxxxxxx',
763 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200764 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200765 \ line: 3,
766 \ col: 18,
767 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200768 let winidb = popup_create([
769 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200770 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200771 \ line: 7,
772 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100773 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200774 \ wrap: 0,
775 \ fixed: 1,
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +0200776 \ scrollbar: v:false,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200777 \ close: 'button',
778 \ zindex: 90,
779 \ padding: [],
780 \ border: [],
781 \ 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 +0200782 END
783 call writefile(lines, 'XtestPopupMask')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200784 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200785 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
786
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200787 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
788 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200789 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200790 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
791
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200792 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
793 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200794 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200795 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
796
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200797 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
798 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200799 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200800 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
801
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200802 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
803 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200804 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200805 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
806
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200807 " clean up
808 call StopVimInTerminal(buf)
809 call delete('XtestPopupMask')
810endfunc
811
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200812func Test_popup_select()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200813 CheckScreendump
814 CheckFeature clipboard_working
815
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200816 " create a popup with some text to be selected
817 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200818 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200819 call setline(1, range(1, 20))
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200820 let winid = popup_create(['the word', 'some more', 'several words here', 'invisible', '5', '6', '7'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200821 \ drag: 1,
822 \ border: [],
823 \ line: 3,
824 \ col: 10,
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200825 \ maxheight: 3,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200826 \ })
827 func Select1()
828 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
829 endfunc
830 map <silent> <F3> :call test_setmouse(4, 15)<CR>
831 map <silent> <F4> :call test_setmouse(6, 23)<CR>
832 END
833 call writefile(lines, 'XtestPopupSelect')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200834 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200835 call term_sendkeys(buf, ":call Select1()\<CR>")
836 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
837
838 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
839 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200840 " clean the command line, sometimes it still shows a command
841 call term_sendkeys(buf, ":\<esc>")
842
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200843 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
844
845 " clean up
846 call StopVimInTerminal(buf)
847 call delete('XtestPopupSelect')
848endfunc
849
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200850func Test_popup_in_tab()
851 " default popup is local to tab, not visible when in other tab
852 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200853 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200854 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200855 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200856 tabnew
857 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200858 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200859 quit
860 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200861
862 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200863 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200864 " buffer is gone now
865 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200866
867 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200868 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200869 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200870 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200871 tabnew
872 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200873 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200874 quit
875 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200876 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200877
878 " create popup in other tab
879 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200880 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200881 call assert_equal(0, popup_getpos(winid).visible)
882 call assert_equal(1, popup_getoptions(winid).tabpage)
883 quit
884 call assert_equal(1, popup_getpos(winid).visible)
885 call assert_equal(0, popup_getoptions(winid).tabpage)
886 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200887endfunc
888
889func Test_popup_valid_arguments()
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200890 call assert_equal(0, len(popup_list()))
891
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200892 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200893 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200894 let pos = popup_getpos(winid)
895 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200896 call assert_equal([winid], popup_list())
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200897 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200898
899 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200900 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200901 let pos = popup_getpos(winid)
902 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200903 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200904
905 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200906 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200907 let pos = popup_getpos(winid)
908 let around = (&columns - pos.width) / 2
909 call assert_inrange(around - 1, around + 1, pos.col)
910 let around = (&lines - pos.height) / 2
911 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200912 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200913endfunc
914
915func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200916 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200917 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200918 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200919 call popup_clear()
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200920 call assert_fails('call popup_create(test_null_string(), {})', 'E450:')
921 call assert_fails('call popup_create(test_null_list(), {})', 'E450:')
922 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200923
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200924 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200925 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200926 call assert_fails('call popup_create("text", #{col: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200927 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200928 call assert_fails('call popup_create("text", #{col: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200929 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200930 call assert_fails('call popup_create("text", #{col: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200931 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200932
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200933 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200934 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200935 call assert_fails('call popup_create("text", #{line: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200936 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200937 call assert_fails('call popup_create("text", #{line: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200938 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200939 call assert_fails('call popup_create("text", #{line: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200940 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200941
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200942 call assert_fails('call popup_create("text", #{pos: "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200943 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200944 call assert_fails('call popup_create("text", #{padding: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200945 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200946 call assert_fails('call popup_create("text", #{border: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200947 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200948 call assert_fails('call popup_create("text", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200949 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +0200950 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
951 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200952 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200953 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200954
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200955 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200956 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200957 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200958 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200959 call assert_fails('call popup_create([#{text: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200960 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +0100961 call assert_fails('call popup_create([#{text: "text", props: range(3)}], {})', 'E715:')
962 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200963 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
964 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +0100965 call assert_fails('call popup_create("text", #{mask: range(5)})', 'E475:')
966 call popup_clear()
967 call popup_create("text", #{mask: [range(4)]})
968 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200969 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200970 call assert_fails('call popup_create("text", #{mapping: []})', 'E745:')
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200971 call popup_clear()
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200972 call assert_fails('call popup_create("text", #{tabpage : 4})', 'E997:')
973 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200974endfunc
975
Bram Moolenaareea16992019-05-31 17:34:48 +0200976func Test_win_execute_closing_curwin()
977 split
978 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200979 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200980 call popup_clear()
Bram Moolenaar49540192019-12-11 19:34:54 +0100981
982 let winid = popup_create('some text', {})
983 call assert_fails('call win_execute(winid, printf("normal! :\<C-u>call popup_close(%d)\<CR>", winid))', 'E994')
984 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200985endfunc
986
987func Test_win_execute_not_allowed()
988 let winid = popup_create('some text', {})
989 call assert_fails('call win_execute(winid, "split")', 'E994:')
990 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
991 call assert_fails('call win_execute(winid, "close")', 'E994:')
992 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200993 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200994 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
995 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
996 call assert_fails('call win_execute(winid, "next")', 'E994:')
997 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
Bram Moolenaar3a2505c2020-03-09 16:40:41 +0100998 call assert_fails('call win_execute(winid, "pedit filename")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200999 call assert_fails('call win_execute(winid, "buf")', 'E994:')
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01001000 call assert_fails('call win_execute(winid, "bnext")', 'E994:')
1001 call assert_fails('call win_execute(winid, "bprev")', 'E994:')
1002 call assert_fails('call win_execute(winid, "bfirst")', 'E994:')
1003 call assert_fails('call win_execute(winid, "blast")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001004 call assert_fails('call win_execute(winid, "edit")', 'E994:')
1005 call assert_fails('call win_execute(winid, "enew")', 'E994:')
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001006 call assert_fails('call win_execute(winid, "help")', 'E994:')
1007 call assert_fails('call win_execute(winid, "1only")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001008 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
1009 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
1010 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
1011 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001012 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +02001013endfunc
1014
Bram Moolenaar402502d2019-05-30 22:07:36 +02001015func Test_popup_with_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001016 CheckScreendump
1017
Bram Moolenaar402502d2019-05-30 22:07:36 +02001018 let lines =<< trim END
1019 call setline(1, range(1, 100))
1020 let winid = popup_create(
1021 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001022 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001023 END
1024 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001025 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001026 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
1027
1028 " clean up
1029 call StopVimInTerminal(buf)
1030 call delete('XtestPopup')
1031endfunc
1032
1033func Test_popup_without_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001034 CheckScreendump
1035
Bram Moolenaar402502d2019-05-30 22:07:36 +02001036 let lines =<< trim END
1037 call setline(1, range(1, 100))
1038 let winid = popup_create(
1039 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001040 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001041 END
1042 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001043 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001044 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
1045
1046 " clean up
1047 call StopVimInTerminal(buf)
1048 call delete('XtestPopup')
1049endfunc
1050
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001051func Test_popup_with_showbreak()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001052 CheckScreendump
1053
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001054 let lines =<< trim END
1055 set showbreak=>>\
1056 call setline(1, range(1, 20))
1057 let winid = popup_dialog(
Bram Moolenaar8ae54372019-09-15 18:11:16 +02001058 \ 'a long line here that wraps',
1059 \ #{filter: 'popup_filter_yesno',
1060 \ maxwidth: 12})
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001061 END
1062 call writefile(lines, 'XtestPopupShowbreak')
1063 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
1064 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
1065
1066 " clean up
1067 call term_sendkeys(buf, "y")
1068 call StopVimInTerminal(buf)
1069 call delete('XtestPopupShowbreak')
1070endfunc
1071
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001072func Test_popup_time()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001073 CheckFeature timers
1074
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001075 topleft vnew
1076 call setline(1, 'hello')
1077
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001078 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001079 \ line: 1,
1080 \ col: 1,
1081 \ minwidth: 20,
1082 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001083 \})
1084 redraw
1085 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1086 call assert_equal('world', line)
1087
Bram Moolenaarb4f06282019-07-12 21:07:54 +02001088 call assert_equal(winid, popup_locate(1, 1))
1089 call assert_equal(winid, popup_locate(1, 20))
1090 call assert_equal(0, popup_locate(1, 21))
1091 call assert_equal(0, popup_locate(2, 1))
1092
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001093 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +02001094 redraw
Bram Moolenaar196b4662019-09-06 21:34:30 +02001095 let line = join(map(range(1, 5), '1->screenstring(v:val)'), '')
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001096 call assert_equal('hello', line)
1097
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001098 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001099 \ line: &lines,
1100 \ col: 10,
1101 \ minwidth: 20,
1102 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001103 \})
1104 redraw
1105 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1106 call assert_match('.*on the command line.*', line)
1107
1108 sleep 700m
1109 redraw
1110 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1111 call assert_notmatch('.*on the command line.*', line)
1112
1113 bwipe!
1114endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001115
1116func Test_popup_hide()
1117 topleft vnew
1118 call setline(1, 'hello')
1119
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001120 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001121 \ line: 1,
1122 \ col: 1,
1123 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001124 \})
1125 redraw
1126 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1127 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001128 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001129 " buffer is still listed and active
1130 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001131
1132 call popup_hide(winid)
1133 redraw
1134 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1135 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001136 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001137 " buffer is still listed but hidden
Bram Moolenaarca7c0782020-01-14 20:42:48 +01001138 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001139
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001140 eval winid->popup_show()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001141 redraw
1142 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1143 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001144 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001145
1146
1147 call popup_close(winid)
1148 redraw
1149 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1150 call assert_equal('hello', line)
1151
1152 " error is given for existing non-popup window
1153 call assert_fails('call popup_hide(win_getid())', 'E993:')
1154
1155 " no error non-existing window
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001156 eval 1234234->popup_hide()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001157 call popup_show(41234234)
1158
1159 bwipe!
1160endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001161
1162func Test_popup_move()
1163 topleft vnew
1164 call setline(1, 'hello')
1165
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001166 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001167 \ line: 1,
1168 \ col: 1,
1169 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001170 \})
1171 redraw
1172 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1173 call assert_equal('world ', line)
1174
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001175 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001176 redraw
1177 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1178 call assert_equal('hello ', line)
1179 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
1180 call assert_equal('~world', line)
1181
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001182 eval winid->popup_move(#{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001183 redraw
1184 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1185 call assert_equal('hworld', line)
1186
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001187 call assert_fails('call popup_move(winid, [])', 'E715:')
1188 call assert_fails('call popup_move(winid, test_null_dict())', 'E715:')
1189
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001190 call popup_close(winid)
1191
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001192 call assert_equal(0, popup_move(-1, {}))
1193
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001194 bwipe!
1195endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +02001196
Bram Moolenaar402502d2019-05-30 22:07:36 +02001197func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001198 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001199 \ line: 2,
1200 \ col: 3,
1201 \ minwidth: 10,
1202 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +02001203 \})
1204 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001205 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001206 call assert_equal(2, res.line)
1207 call assert_equal(3, res.col)
1208 call assert_equal(10, res.width)
1209 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001210 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001211
1212 call popup_close(winid)
1213endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001214
1215func Test_popup_width_longest()
1216 let tests = [
1217 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
1218 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
1219 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
1220 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
1221 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
1222 \ ]
1223
1224 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001225 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001226 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001227 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001228 call assert_equal(test[1], position.width)
1229 call popup_close(winid)
1230 endfor
1231endfunc
1232
1233func Test_popup_wraps()
1234 let tests = [
1235 \ ['nowrap', 6, 1],
1236 \ ['a line that wraps once', 12, 2],
1237 \ ['a line that wraps two times', 12, 3],
1238 \ ]
1239 for test in tests
1240 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001241 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001242 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001243 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001244 call assert_equal(test[1], position.width)
1245 call assert_equal(test[2], position.height)
1246
1247 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001248 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001249 endfor
1250endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001251
1252func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001253 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001254 \ line: 2,
1255 \ col: 3,
1256 \ minwidth: 10,
1257 \ minheight: 11,
1258 \ maxwidth: 20,
1259 \ maxheight: 21,
1260 \ zindex: 100,
1261 \ time: 5000,
1262 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001263 \})
1264 redraw
1265 let res = popup_getoptions(winid)
1266 call assert_equal(2, res.line)
1267 call assert_equal(3, res.col)
1268 call assert_equal(10, res.minwidth)
1269 call assert_equal(11, res.minheight)
1270 call assert_equal(20, res.maxwidth)
1271 call assert_equal(21, res.maxheight)
1272 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001273 call assert_equal(1, res.fixed)
Bram Moolenaarb8350ab2019-08-04 17:59:49 +02001274 call assert_equal(1, res.mapping)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001275 if has('timers')
1276 call assert_equal(5000, res.time)
1277 endif
1278 call popup_close(winid)
1279
1280 let winid = popup_create('hello', {})
1281 redraw
1282 let res = popup_getoptions(winid)
1283 call assert_equal(0, res.line)
1284 call assert_equal(0, res.col)
1285 call assert_equal(0, res.minwidth)
1286 call assert_equal(0, res.minheight)
1287 call assert_equal(0, res.maxwidth)
1288 call assert_equal(0, res.maxheight)
1289 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001290 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001291 if has('timers')
1292 call assert_equal(0, res.time)
1293 endif
1294 call popup_close(winid)
1295 call assert_equal({}, popup_getoptions(winid))
1296endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001297
1298func Test_popup_option_values()
1299 new
1300 " window-local
1301 setlocal number
1302 setlocal nowrap
1303 " buffer-local
1304 setlocal omnifunc=Something
1305 " global/buffer-local
1306 setlocal path=/there
1307 " global/window-local
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001308 setlocal statusline=2
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001309
1310 let winid = popup_create('hello', {})
1311 call assert_equal(0, getwinvar(winid, '&number'))
1312 call assert_equal(1, getwinvar(winid, '&wrap'))
1313 call assert_equal('', getwinvar(winid, '&omnifunc'))
1314 call assert_equal(&g:path, getwinvar(winid, '&path'))
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001315 call assert_equal(&g:statusline, getwinvar(winid, '&statusline'))
1316
1317 " 'scrolloff' is reset to zero
1318 call assert_equal(5, &scrolloff)
1319 call assert_equal(0, getwinvar(winid, '&scrolloff'))
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001320
1321 call popup_close(winid)
1322 bwipe
1323endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001324
1325func Test_popup_atcursor()
1326 topleft vnew
1327 call setline(1, [
1328 \ 'xxxxxxxxxxxxxxxxx',
1329 \ 'xxxxxxxxxxxxxxxxx',
1330 \ 'xxxxxxxxxxxxxxxxx',
1331 \])
1332
1333 call cursor(2, 2)
1334 redraw
1335 let winid = popup_atcursor('vim', {})
1336 redraw
1337 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1338 call assert_equal('xvimxxxxxxxxxxxxx', line)
1339 call popup_close(winid)
1340
1341 call cursor(3, 4)
1342 redraw
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001343 let winid = 'vim'->popup_atcursor({})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001344 redraw
1345 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
1346 call assert_equal('xxxvimxxxxxxxxxxx', line)
1347 call popup_close(winid)
1348
1349 call cursor(1, 1)
1350 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001351 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001352 \ line: 'cursor+2',
1353 \ col: 'cursor+1',
1354 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001355 redraw
1356 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1357 call assert_equal('xvimxxxxxxxxxxxxx', line)
1358 call popup_close(winid)
1359
1360 call cursor(3, 3)
1361 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001362 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001363 \ line: 'cursor-2',
1364 \ col: 'cursor-1',
1365 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001366 redraw
1367 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1368 call assert_equal('xvimxxxxxxxxxxxxx', line)
1369 call popup_close(winid)
1370
Bram Moolenaar402502d2019-05-30 22:07:36 +02001371 " just enough room above
1372 call cursor(3, 3)
1373 redraw
1374 let winid = popup_atcursor(['vim', 'is great'], {})
1375 redraw
1376 let pos = popup_getpos(winid)
1377 call assert_equal(1, pos.line)
1378 call popup_close(winid)
1379
1380 " not enough room above, popup goes below the cursor
1381 call cursor(3, 3)
1382 redraw
1383 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1384 redraw
1385 let pos = popup_getpos(winid)
1386 call assert_equal(4, pos.line)
1387 call popup_close(winid)
1388
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001389 " cursor in first line, popup in line 2
1390 call cursor(1, 1)
1391 redraw
1392 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1393 redraw
1394 let pos = popup_getpos(winid)
1395 call assert_equal(2, pos.line)
1396 call popup_close(winid)
1397
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001398 bwipe!
1399endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001400
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001401func Test_popup_atcursor_pos()
1402 CheckScreendump
1403
1404 let lines =<< trim END
1405 call setline(1, repeat([repeat('-', 60)], 15))
1406 set so=0
1407
1408 normal 9G3|r#
1409 let winid1 = popup_atcursor(['first', 'second'], #{
1410 \ moved: [0, 0, 0],
1411 \ })
1412 normal 9G21|r&
1413 let winid1 = popup_atcursor(['FIrsT', 'SEcoND'], #{
1414 \ pos: 'botright',
1415 \ moved: [0, 0, 0],
1416 \ })
1417 normal 3G27|r%
1418 let winid1 = popup_atcursor(['fiRSt', 'seCOnd'], #{
1419 \ pos: 'topleft',
1420 \ moved: [0, 0, 0],
1421 \ })
1422 normal 3G45|r@
1423 let winid1 = popup_atcursor(['First', 'SeconD'], #{
1424 \ pos: 'topright',
Bram Moolenaarb0992022020-01-30 14:55:42 +01001425 \ moved: range(3),
1426 \ mousemoved: range(3),
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001427 \ })
1428 END
1429 call writefile(lines, 'XtestPopupAtcursorPos')
1430 let buf = RunVimInTerminal('-S XtestPopupAtcursorPos', #{rows: 12})
1431 call VerifyScreenDump(buf, 'Test_popupwin_atcursor_pos', {})
1432
1433 " clean up
1434 call StopVimInTerminal(buf)
1435 call delete('XtestPopupAtcursorPos')
1436endfunc
1437
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001438func Test_popup_beval()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001439 CheckScreendump
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001440 CheckFeature balloon_eval_term
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001441
1442 let lines =<< trim END
1443 call setline(1, range(1, 20))
1444 call setline(5, 'here is some text to hover over')
1445 set balloonevalterm
1446 set balloonexpr=BalloonExpr()
1447 set balloondelay=100
1448 func BalloonExpr()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001449 let s:winid = [v:beval_text]->popup_beval({})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001450 return ''
1451 endfunc
1452 func Hover()
1453 call test_setmouse(5, 15)
1454 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1455 sleep 100m
1456 endfunc
1457 func MoveOntoPopup()
1458 call test_setmouse(4, 17)
1459 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1460 endfunc
1461 func MoveAway()
1462 call test_setmouse(5, 13)
1463 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1464 endfunc
1465 END
1466 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001467 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001468 call TermWait(buf, 50)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001469 call term_sendkeys(buf, 'j')
1470 call term_sendkeys(buf, ":call Hover()\<CR>")
1471 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1472
1473 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1474 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1475
1476 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1477 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1478
1479 " clean up
1480 call StopVimInTerminal(buf)
1481 call delete('XtestPopupBeval')
1482endfunc
1483
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001484func Test_popup_filter()
1485 new
1486 call setline(1, 'some text')
1487
1488 func MyPopupFilter(winid, c)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001489 if a:c == 'e' || a:c == "\<F9>"
1490 let g:eaten = a:c
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001491 return 1
1492 endif
1493 if a:c == '0'
1494 let g:ignored = '0'
1495 return 0
1496 endif
1497 if a:c == 'x'
1498 call popup_close(a:winid)
1499 return 1
1500 endif
1501 return 0
1502 endfunc
1503
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001504 let winid = 'something'->popup_create(#{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001505 redraw
1506
1507 " e is consumed by the filter
1508 call feedkeys('e', 'xt')
1509 call assert_equal('e', g:eaten)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001510 call feedkeys("\<F9>", 'xt')
1511 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001512
1513 " 0 is ignored by the filter
1514 normal $
1515 call assert_equal(9, getcurpos()[2])
1516 call feedkeys('0', 'xt')
1517 call assert_equal('0', g:ignored)
1518 call assert_equal(1, getcurpos()[2])
1519
1520 " x closes the popup
1521 call feedkeys('x', 'xt')
Bram Moolenaarec084d32020-02-28 22:44:47 +01001522 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001523 call assert_equal(-1, winbufnr(winid))
1524
1525 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001526 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001527endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001528
Bram Moolenaara42d9452019-06-15 21:46:30 +02001529func ShowDialog(key, result)
1530 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001531 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001532 \ filter: 'popup_filter_yesno',
1533 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001534 \ })
1535 redraw
1536 call feedkeys(a:key, "xt")
1537 call assert_equal(winid, s:cb_winid)
1538 call assert_equal(a:result, s:cb_res)
1539endfunc
1540
1541func Test_popup_dialog()
1542 func QuitCallback(id, res)
1543 let s:cb_winid = a:id
1544 let s:cb_res = a:res
1545 endfunc
1546
1547 let winid = ShowDialog("y", 1)
1548 let winid = ShowDialog("Y", 1)
1549 let winid = ShowDialog("n", 0)
1550 let winid = ShowDialog("N", 0)
1551 let winid = ShowDialog("x", 0)
1552 let winid = ShowDialog("X", 0)
1553 let winid = ShowDialog("\<Esc>", 0)
1554 let winid = ShowDialog("\<C-C>", -1)
1555
1556 delfunc QuitCallback
1557endfunc
1558
Bram Moolenaara730e552019-06-16 19:05:31 +02001559func ShowMenu(key, result)
1560 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001561 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001562 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001563 \ })
1564 redraw
1565 call feedkeys(a:key, "xt")
1566 call assert_equal(winid, s:cb_winid)
1567 call assert_equal(a:result, s:cb_res)
1568endfunc
1569
1570func Test_popup_menu()
1571 func QuitCallback(id, res)
1572 let s:cb_winid = a:id
1573 let s:cb_res = a:res
1574 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001575 " mapping won't be used in popup
1576 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001577
1578 let winid = ShowMenu(" ", 1)
1579 let winid = ShowMenu("j \<CR>", 2)
1580 let winid = ShowMenu("JjK \<CR>", 2)
1581 let winid = ShowMenu("jjjjjj ", 3)
1582 let winid = ShowMenu("kkk ", 1)
1583 let winid = ShowMenu("x", -1)
1584 let winid = ShowMenu("X", -1)
1585 let winid = ShowMenu("\<Esc>", -1)
1586 let winid = ShowMenu("\<C-C>", -1)
1587
1588 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001589 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001590endfunc
1591
1592func Test_popup_menu_screenshot()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001593 CheckScreendump
Bram Moolenaara730e552019-06-16 19:05:31 +02001594
1595 let lines =<< trim END
1596 call setline(1, range(1, 20))
1597 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001598 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001599 func MenuDone(id, res)
1600 echomsg "selected " .. a:res
1601 endfunc
1602 END
1603 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001604 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001605 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1606
1607 call term_sendkeys(buf, "jj")
1608 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1609
1610 call term_sendkeys(buf, " ")
1611 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1612
1613 " clean up
1614 call StopVimInTerminal(buf)
1615 call delete('XtestPopupMenu')
1616endfunc
1617
Bram Moolenaarf914a332019-07-20 15:09:56 +02001618func Test_popup_menu_narrow()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001619 CheckScreendump
Bram Moolenaarf914a332019-07-20 15:09:56 +02001620
1621 let lines =<< trim END
1622 call setline(1, range(1, 20))
1623 hi PopupSelected ctermbg=green
1624 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1625 func MenuDone(id, res)
1626 echomsg "selected " .. a:res
1627 endfunc
1628 END
1629 call writefile(lines, 'XtestPopupNarrowMenu')
1630 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1631 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1632
1633 " clean up
1634 call term_sendkeys(buf, "x")
1635 call StopVimInTerminal(buf)
1636 call delete('XtestPopupNarrowMenu')
1637endfunc
1638
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001639func Test_popup_title()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001640 CheckScreendump
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001641
1642 " Create a popup without title or border, a line of padding will be added to
1643 " put the title on.
1644 let lines =<< trim END
1645 call setline(1, range(1, 20))
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001646 let winid = popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001647 END
1648 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001649 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001650 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1651
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001652 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxwidth: 20, title: 'a very long title that is not going to fit'})\<CR>")
1653 call term_sendkeys(buf, ":\<CR>")
1654 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_1', {})
1655
1656 call term_sendkeys(buf, ":call popup_setoptions(winid, #{border: []})\<CR>")
1657 call term_sendkeys(buf, ":\<CR>")
1658 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_2', {})
1659
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001660 " clean up
1661 call StopVimInTerminal(buf)
1662 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001663
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001664 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001665 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001666 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001667 call assert_equal('Another Title', popup_getoptions(winid).title)
1668
1669 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001670endfunc
1671
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001672func Test_popup_close_callback()
1673 func PopupDone(id, result)
1674 let g:result = a:result
1675 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001676 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001677 redraw
1678 call popup_close(winid, 'done')
1679 call assert_equal('done', g:result)
1680endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001681
1682func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001683 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001684 redraw
1685 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001686 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001687 call assert_equal(5, pos.height)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001688 call popup_close(winid)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001689
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001690 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001691 redraw
1692 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001693 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001694 call assert_equal(3, pos.height)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001695 call popup_close(winid)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001696endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001697
1698func Test_popup_never_behind()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001699 CheckScreendump
1700
Bram Moolenaar988c4332019-06-02 14:12:11 +02001701 " +-----------------------------+
1702 " | | |
1703 " | | |
1704 " | | |
1705 " | line1 |
1706 " |------------line2------------|
1707 " | line3 |
1708 " | line4 |
1709 " | |
1710 " | |
1711 " +-----------------------------+
1712 let lines =<< trim END
Bram Moolenaar988c4332019-06-02 14:12:11 +02001713 split
1714 vsplit
1715 let info_window1 = getwininfo()[0]
1716 let line = info_window1['height']
1717 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001718 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001719 \ line : line,
1720 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001721 \ })
1722 END
1723 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001724 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001725 call term_sendkeys(buf, "\<C-W>w")
1726 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1727
1728 " clean up
1729 call StopVimInTerminal(buf)
1730 call delete('XtestPopupBehind')
1731endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001732
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001733func s:VerifyPosition(p, msg, line, col, width, height)
1734 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1735 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1736 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1737 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001738endfunc
1739
1740func Test_popup_position_adjust()
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001741 " Anything placed past the last cell on the right of the screen is moved to
1742 " the left.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001743 "
1744 " When wrapping is disabled, we also shift to the left to display on the
1745 " screen, unless fixed is set.
1746
1747 " Entries for cases which don't vary based on wrapping.
1748 " Format is per tests described below
1749 let both_wrap_tests = [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001750 \ ['a', 5, &columns, 5, &columns, 1, 1],
1751 \ ['b', 5, &columns + 1, 5, &columns, 1, 1],
1752 \ ['c', 5, &columns - 1, 5, &columns - 1, 1, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001753 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001754 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1]]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001755
1756 " these test groups are dicts with:
1757 " - comment: something to identify the group of tests by
1758 " - options: dict of options to merge with the row/col in tests
1759 " - tests: list of cases. Each one is a list with elements:
1760 " - text
1761 " - row
1762 " - col
1763 " - expected row
1764 " - expected col
1765 " - expected width
1766 " - expected height
1767 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001768 \ #{
1769 \ comment: 'left-aligned with wrapping',
1770 \ options: #{
1771 \ wrap: 1,
1772 \ pos: 'botleft',
1773 \ },
1774 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001775 \ ['aa', 5, &columns, 4, &columns, 1, 2],
1776 \ ['bb', 5, &columns + 1, 4, &columns, 1, 2],
1777 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1778 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1779 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1780 \
1781 \ ['aaa', 5, &columns, 3, &columns, 1, 3],
1782 \ ['bbb', 5, &columns + 1, 3, &columns, 1, 3],
1783 \ ['ccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
1784 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1785 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1786 \
1787 \ ['aaaa', 5, &columns, 2, &columns, 1, 4],
1788 \ ['bbbb', 5, &columns + 1, 2, &columns, 1, 4],
1789 \ ['cccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001790 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
1791 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001792 \ ['eeee', 5, &columns - 4, 5, &columns - 4, 4, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001793 \ ],
1794 \ },
1795 \ #{
1796 \ comment: 'left aligned without wrapping',
1797 \ options: #{
1798 \ wrap: 0,
1799 \ pos: 'botleft',
1800 \ },
1801 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001802 \ ['aa', 5, &columns, 5, &columns - 1, 2, 1],
1803 \ ['bb', 5, &columns + 1, 5, &columns - 1, 2, 1],
1804 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1805 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1806 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1807 \
1808 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
1809 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1810 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1811 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1812 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1813 \
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001814 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
1815 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
1816 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
1817 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
1818 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1819 \ ],
1820 \ },
1821 \ #{
1822 \ comment: 'left aligned with fixed position',
1823 \ options: #{
1824 \ wrap: 0,
1825 \ fixed: 1,
1826 \ pos: 'botleft',
1827 \ },
1828 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001829 \ ['aa', 5, &columns, 5, &columns, 1, 1],
1830 \ ['bb', 5, &columns + 1, 5, &columns, 1, 1],
1831 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1832 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1833 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1834 \
1835 \ ['aaa', 5, &columns, 5, &columns, 1, 1],
1836 \ ['bbb', 5, &columns + 1, 5, &columns, 1, 1],
1837 \ ['ccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1838 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1839 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1840 \
1841 \ ['aaaa', 5, &columns, 5, &columns, 1, 1],
1842 \ ['bbbb', 5, &columns + 1, 5, &columns, 1, 1],
1843 \ ['cccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001844 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1845 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1846 \ ],
1847 \ },
1848 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001849
1850 for test_group in tests
1851 for test in test_group.tests
1852 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001853 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001854 \ line: line,
1855 \ col: col,
1856 \ }
1857 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001858
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001859 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001860
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001861 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001862 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
1863 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001864 endfor
1865 endfor
1866
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001867 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001868 %bwipe!
1869endfunc
1870
Bram Moolenaar3397f742019-06-02 18:40:06 +02001871func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001872 " width of screen
1873 let X = join(map(range(&columns), {->'X'}), '')
1874
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001875 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1876 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001877
1878 redraw
1879 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1880 call assert_equal(X, line)
1881
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001882 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001883 redraw
1884
1885 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001886 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
1887 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001888
1889 redraw
1890 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1891 call assert_equal(X, line)
1892
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001893 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001894 redraw
1895
1896 " Extend so > window width
1897 let X .= 'x'
1898
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001899 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1900 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001901
1902 redraw
1903 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1904 call assert_equal(X[ : -2 ], line)
1905
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001906 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001907 redraw
1908
1909 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001910 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
1911 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001912
1913 redraw
1914 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1915 call assert_equal(X[ : -2 ], line)
1916
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001917 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001918 redraw
1919
1920 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001921 let p = popup_create(X,
1922 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
1923 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001924
1925 redraw
1926 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1927 let e_line = ' ' . X[ 1 : -2 ]
1928 call assert_equal(e_line, line)
1929
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001930 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001931 redraw
1932
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001933 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001934 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001935endfunc
1936
1937func Test_popup_moved()
1938 new
1939 call test_override('char_avail', 1)
1940 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1941
1942 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001943 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001944 redraw
1945 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001946 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001947 " trigger the check for last_cursormoved by going into insert mode
1948 call feedkeys("li\<Esc>", 'xt')
1949 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001950 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001951
1952 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001953 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001954 redraw
1955 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001956 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001957 call feedkeys("hi\<Esc>", 'xt')
1958 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001959 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001960
1961 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001962 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001963 redraw
1964 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001965 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001966 call feedkeys("li\<Esc>", 'xt')
1967 call assert_equal(1, popup_getpos(winid).visible)
1968 call feedkeys("ei\<Esc>", 'xt')
1969 call assert_equal(1, popup_getpos(winid).visible)
1970 call feedkeys("eli\<Esc>", 'xt')
1971 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001972 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001973
Bram Moolenaar17627312019-06-02 19:53:44 +02001974 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001975 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001976 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001977 redraw
1978 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001979 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001980 call feedkeys("eli\<Esc>", 'xt')
1981 call assert_equal(1, popup_getpos(winid).visible)
1982 call feedkeys("wi\<Esc>", 'xt')
1983 call assert_equal(1, popup_getpos(winid).visible)
1984 call feedkeys("Eli\<Esc>", 'xt')
1985 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001986 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001987
1988 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001989 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001990 redraw
1991 call assert_equal(1, popup_getpos(winid).visible)
1992 call feedkeys("eli\<Esc>", 'xt')
1993 call feedkeys("ei\<Esc>", 'xt')
1994 call assert_equal(1, popup_getpos(winid).visible)
1995 call feedkeys("eli\<Esc>", 'xt')
1996 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001997 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001998
1999 bwipe!
2000 call test_override('ALL', 0)
2001endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002002
2003func Test_notifications()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002004 CheckFeature timers
2005 CheckScreendump
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002006
Bram Moolenaar0fdddee2019-09-01 15:26:23 +02002007 let lines =<< trim END
2008 call setline(1, range(1, 20))
2009 hi Notification ctermbg=lightblue
2010 call popup_notification('first notification', {})
2011 END
2012 call writefile(lines, 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002013 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002014 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
2015
2016 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002017 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
2018 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002019 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
2020
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002021 " clean up
2022 call StopVimInTerminal(buf)
2023 call delete('XtestNotifications')
2024endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002025
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002026func Test_popup_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002027 CheckScreendump
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002028
2029 let lines =<< trim END
2030 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02002031 hi ScrollThumb ctermbg=blue
2032 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002033 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002034 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002035 \ minwidth: 8,
2036 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002037 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002038 func ScrollUp()
2039 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
2040 endfunc
2041 func ScrollDown()
2042 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
2043 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002044 func ClickTop()
2045 call feedkeys("\<F4>\<LeftMouse>", "xt")
2046 endfunc
2047 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002048 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002049 call feedkeys("\<F5>\<LeftMouse>", "xt")
2050 endfunc
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002051 func Popup_filter(winid, key)
2052 if a:key == 'j'
2053 let line = popup_getoptions(a:winid).firstline
2054 let nlines = line('$', a:winid)
2055 let newline = line < nlines ? (line + 1) : nlines
2056 call popup_setoptions(a:winid, #{firstline: newline})
2057 return v:true
2058 elseif a:key == 'x'
2059 call popup_close(a:winid)
2060 return v:true
2061 endif
2062 endfunc
2063
2064 func PopupScroll()
2065 call popup_clear()
2066 let text =<< trim END
2067 1
2068 2
2069 3
2070 4
2071 long line long line long line long line long line long line
2072 long line long line long line long line long line long line
2073 long line long line long line long line long line long line
2074 END
2075 call popup_create(text, #{
2076 \ minwidth: 30,
2077 \ maxwidth: 30,
2078 \ minheight: 4,
2079 \ maxheight: 4,
2080 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +01002081 \ lastline: 4,
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002082 \ wrap: v:true,
2083 \ scrollbar: v:true,
2084 \ mapping: v:false,
2085 \ filter: funcref('Popup_filter')
2086 \ })
2087 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002088 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002089 map <silent> <F4> :call test_setmouse(4, 42)<CR>
2090 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002091 END
2092 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002093 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002094 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
2095
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002096 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002097 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002098 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
2099
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002100 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002101 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002102 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
2103
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002104 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002105 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002106 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
2107
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02002108 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb', firstline: 5})\<CR>")
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002109 " this scrolls two lines (half the window height)
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002110 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
2111 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
2112
2113 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2114 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
2115
2116 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02002117 " wait a bit, otherwise it fails sometimes (double click recognized?)
2118 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002119 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2120 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
2121
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002122 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2123 sleep 100m
2124 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2125 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
2126
2127 call term_sendkeys(buf, ":call ClickBot()\<CR>")
2128 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
2129
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002130 " remove the minwidth and maxheight
2131 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxheight: 0, minwidth: 0})\<CR>")
Bram Moolenaar7e0f4622019-09-17 21:23:39 +02002132 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002133 call VerifyScreenDump(buf, 'Test_popupwin_scroll_10', {})
2134
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002135 " check size with non-wrapping lines
2136 call term_sendkeys(buf, ":call PopupScroll()\<CR>")
2137 call VerifyScreenDump(buf, 'Test_popupwin_scroll_11', {})
2138
2139 " check size with wrapping lines
2140 call term_sendkeys(buf, "j")
2141 call VerifyScreenDump(buf, 'Test_popupwin_scroll_12', {})
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002142
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002143 " clean up
Bram Moolenaar20298ce2020-06-19 21:46:52 +02002144 call term_sendkeys(buf, "x")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002145 call StopVimInTerminal(buf)
2146 call delete('XtestPopupScroll')
2147endfunc
2148
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02002149func Test_popup_too_high_scrollbar()
2150 CheckScreendump
2151
2152 let lines =<< trim END
2153 call setline(1, range(1, 20)->map({i, v -> repeat(v, 10)}))
2154 set scrolloff=0
2155 func ShowPopup()
2156 let winid = popup_atcursor(['one', 'two', 'three', 'four', 'five',
2157 \ 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve'], #{
2158 \ minwidth: 8,
2159 \ border: [],
2160 \ })
2161 endfunc
2162 normal 3G$
2163 call ShowPopup()
2164 END
2165 call writefile(lines, 'XtestPopupToohigh')
2166 let buf = RunVimInTerminal('-S XtestPopupToohigh', #{rows: 10})
2167 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_1', {})
2168
2169 call term_sendkeys(buf, ":call popup_clear()\<CR>")
2170 call term_sendkeys(buf, "8G$")
2171 call term_sendkeys(buf, ":call ShowPopup()\<CR>")
2172 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_2', {})
2173
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02002174 call term_sendkeys(buf, ":call popup_clear()\<CR>")
2175 call term_sendkeys(buf, "gg$")
2176 call term_sendkeys(buf, ":call ShowPopup()\<CR>")
2177 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_3', {})
2178
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02002179 " clean up
2180 call StopVimInTerminal(buf)
2181 call delete('XtestPopupToohigh')
2182endfunc
2183
Bram Moolenaar437a7462019-07-05 20:17:22 +02002184func Test_popup_fitting_scrollbar()
2185 " this was causing a crash, divide by zero
2186 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002187 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002188 \ scrollbar: 1,
2189 \ maxwidth: 10,
2190 \ maxheight: 5,
2191 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02002192 redraw
2193 call popup_clear()
2194endfunc
2195
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002196func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002197 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002198
2199 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002200 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002201 let p = popup_create('test', opts)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002202 eval p->popup_settext('this is a text')
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002203 END
2204
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002205 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002206 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002207 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
2208
2209 " Setting to empty string clears it
2210 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
2211 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
2212
2213 " Setting a list
2214 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2215 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2216
2217 " Shrinking with a list
2218 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
2219 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
2220
2221 " Growing with a list
2222 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2223 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2224
2225 " Empty list clears
2226 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
2227 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
2228
2229 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002230 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002231 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
2232
Bram Moolenaarb0992022020-01-30 14:55:42 +01002233 " range() (doesn't work)
2234 call term_sendkeys(buf, ":call popup_settext(p, range(4, 8))\<CR>")
2235 call VerifyScreenDump(buf, 'Test_popup_settext_07', {})
2236
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002237 " clean up
2238 call StopVimInTerminal(buf)
2239 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002240endfunc
2241
2242func Test_popup_hidden()
2243 new
2244
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002245 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002246 redraw
2247 call assert_equal(0, popup_getpos(winid).visible)
2248 call popup_close(winid)
2249
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002250 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002251 redraw
2252 call assert_equal(0, popup_getpos(winid).visible)
2253 call popup_close(winid)
2254
2255 func QuitCallback(id, res)
2256 let s:cb_winid = a:id
2257 let s:cb_res = a:res
2258 endfunc
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002259 let winid = 'make a choice'->popup_dialog(#{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002260 \ filter: 'popup_filter_yesno',
2261 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002262 \ })
2263 redraw
2264 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02002265 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
2266 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002267 exe "normal anot used by filter\<Esc>"
2268 call assert_equal('not used by filter', getline(1))
2269
2270 call popup_show(winid)
2271 call feedkeys('y', "xt")
2272 call assert_equal(1, s:cb_res)
2273
2274 bwipe!
2275 delfunc QuitCallback
2276endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02002277
2278" Test options not checked elsewhere
2279func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002280 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002281 let options = popup_getoptions(winid)
2282 call assert_equal(1, options.wrap)
2283 call assert_equal(0, options.drag)
2284 call assert_equal('Beautiful', options.highlight)
2285
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002286 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002287 let options = popup_getoptions(winid)
2288 call assert_equal(0, options.wrap)
2289 call assert_equal(1, options.drag)
2290 call assert_equal('Another', options.highlight)
2291
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002292 call assert_fails('call popup_setoptions(winid, [])', 'E715:')
2293 call assert_fails('call popup_setoptions(winid, test_null_dict())', 'E715:')
2294
Bram Moolenaarae943152019-06-16 22:54:14 +02002295 call popup_close(winid)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002296 call assert_equal(0, popup_setoptions(winid, options.wrap))
Bram Moolenaarae943152019-06-16 22:54:14 +02002297endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002298
2299func Test_popupwin_garbage_collect()
2300 func MyPopupFilter(x, winid, c)
2301 " NOP
2302 endfunc
2303
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002304 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002305 call test_garbagecollect_now()
2306 redraw
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02002307 " Must not crash caused by invalid memory access
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002308 call feedkeys('j', 'xt')
2309 call assert_true(v:true)
2310
2311 call popup_close(winid)
2312 delfunc MyPopupFilter
2313endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002314
Bram Moolenaar581ba392019-09-03 22:08:33 +02002315func Test_popupwin_filter_mode()
2316 func MyPopupFilter(winid, c)
2317 let s:typed = a:c
2318 if a:c == ':' || a:c == "\r" || a:c == 'v'
2319 " can start cmdline mode, get out, and start/stop Visual mode
2320 return 0
2321 endif
2322 return 1
2323 endfunc
2324
2325 " Normal, Visual and Insert mode
2326 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'nvi'})
2327 redraw
2328 call feedkeys('x', 'xt')
2329 call assert_equal('x', s:typed)
2330
2331 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2332 call assert_equal(':', s:typed)
2333 call assert_equal('foo', g:foo)
2334
2335 let @x = 'something'
2336 call feedkeys('v$"xy', 'xt')
2337 call assert_equal('y', s:typed)
2338 call assert_equal('something', @x) " yank command is filtered out
2339 call feedkeys('v', 'xt') " end Visual mode
2340
2341 call popup_close(winid)
2342
2343 " only Normal mode
2344 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'n'})
2345 redraw
2346 call feedkeys('x', 'xt')
2347 call assert_equal('x', s:typed)
2348
2349 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2350 call assert_equal(':', s:typed)
2351 call assert_equal('foo', g:foo)
2352
2353 let @x = 'something'
2354 call feedkeys('v$"xy', 'xt')
2355 call assert_equal('v', s:typed)
2356 call assert_notequal('something', @x)
2357
2358 call popup_close(winid)
2359
2360 " default: all modes
2361 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
2362 redraw
2363 call feedkeys('x', 'xt')
2364 call assert_equal('x', s:typed)
2365
2366 let g:foo = 'bar'
2367 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2368 call assert_equal("\r", s:typed)
2369 call assert_equal('bar', g:foo)
2370
2371 let @x = 'something'
2372 call feedkeys('v$"xy', 'xt')
2373 call assert_equal('y', s:typed)
2374 call assert_equal('something', @x) " yank command is filtered out
2375 call feedkeys('v', 'xt') " end Visual mode
2376
2377 call popup_close(winid)
2378 delfunc MyPopupFilter
2379endfunc
2380
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002381func Test_popupwin_filter_mouse()
2382 func MyPopupFilter(winid, c)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002383 let g:got_mousepos = getmousepos()
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002384 return 0
2385 endfunc
2386
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002387 call setline(1, ['.'->repeat(25)]->repeat(10))
2388 let winid = popup_create(['short', 'long line that will wrap', 'other'], #{
2389 \ line: 2,
2390 \ col: 4,
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002391 \ maxwidth: 12,
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002392 \ padding: [],
2393 \ border: [],
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002394 \ filter: 'MyPopupFilter',
2395 \ })
2396 redraw
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002397 " 123456789012345678901
2398 " 1 .....................
2399 " 2 ...+--------------+..
2400 " 3 ...| |..
2401 " 4 ...| short |..
2402 " 5 ...| long line th |..
2403 " 6 ...| at will wrap |..
2404 " 7 ...| other |..
2405 " 8 ...| |..
2406 " 9 ...+--------------+..
2407 " 10 .....................
2408 let tests = []
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002409
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002410 func AddItemOutsidePopup(tests, row, col)
2411 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2412 \ screenrow: a:row, screencol: a:col,
2413 \ winid: win_getid(), winrow: a:row, wincol: a:col,
2414 \ line: a:row, column: a:col,
2415 \ }})
2416 endfunc
2417 func AddItemInPopupBorder(tests, winid, row, col)
2418 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2419 \ screenrow: a:row, screencol: a:col,
2420 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2421 \ line: 0, column: 0,
2422 \ }})
2423 endfunc
2424 func AddItemInPopupText(tests, winid, row, col, textline, textcol)
2425 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2426 \ screenrow: a:row, screencol: a:col,
2427 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2428 \ line: a:textline, column: a:textcol,
2429 \ }})
2430 endfunc
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002431
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002432 " above and below popup
2433 for c in range(1, 21)
2434 call AddItemOutsidePopup(tests, 1, c)
2435 call AddItemOutsidePopup(tests, 10, c)
2436 endfor
2437 " left and right of popup
2438 for r in range(1, 10)
2439 call AddItemOutsidePopup(tests, r, 3)
2440 call AddItemOutsidePopup(tests, r, 20)
2441 endfor
2442 " top and bottom in popup
2443 for c in range(4, 19)
2444 call AddItemInPopupBorder(tests, winid, 2, c)
2445 call AddItemInPopupBorder(tests, winid, 3, c)
2446 call AddItemInPopupBorder(tests, winid, 8, c)
2447 call AddItemInPopupBorder(tests, winid, 9, c)
2448 endfor
2449 " left and right margin in popup
2450 for r in range(2, 9)
2451 call AddItemInPopupBorder(tests, winid, r, 4)
2452 call AddItemInPopupBorder(tests, winid, r, 5)
2453 call AddItemInPopupBorder(tests, winid, r, 18)
2454 call AddItemInPopupBorder(tests, winid, r, 19)
2455 endfor
2456 " text "short"
2457 call AddItemInPopupText(tests, winid, 4, 6, 1, 1)
2458 call AddItemInPopupText(tests, winid, 4, 10, 1, 5)
2459 call AddItemInPopupText(tests, winid, 4, 11, 1, 6)
2460 call AddItemInPopupText(tests, winid, 4, 17, 1, 6)
2461 " text "long line th"
2462 call AddItemInPopupText(tests, winid, 5, 6, 2, 1)
2463 call AddItemInPopupText(tests, winid, 5, 10, 2, 5)
2464 call AddItemInPopupText(tests, winid, 5, 17, 2, 12)
2465 " text "at will wrap"
2466 call AddItemInPopupText(tests, winid, 6, 6, 2, 13)
2467 call AddItemInPopupText(tests, winid, 6, 10, 2, 17)
2468 call AddItemInPopupText(tests, winid, 6, 17, 2, 24)
2469 " text "other"
2470 call AddItemInPopupText(tests, winid, 7, 6, 3, 1)
2471 call AddItemInPopupText(tests, winid, 7, 10, 3, 5)
2472 call AddItemInPopupText(tests, winid, 7, 11, 3, 6)
2473 call AddItemInPopupText(tests, winid, 7, 17, 3, 6)
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002474
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002475 for item in tests
2476 call test_setmouse(item.clickrow, item.clickcol)
2477 call feedkeys("\<LeftMouse>", 'xt')
2478 call assert_equal(item.result, g:got_mousepos)
2479 endfor
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002480
2481 call popup_close(winid)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002482 enew!
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002483 delfunc MyPopupFilter
2484endfunc
2485
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002486func Test_popupwin_with_buffer()
2487 call writefile(['some text', 'in a buffer'], 'XsomeFile')
2488 let buf = bufadd('XsomeFile')
2489 call assert_equal(0, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002490
2491 setlocal number
2492 call setbufvar(buf, "&wrapmargin", 13)
2493
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002494 let winid = popup_create(buf, {})
2495 call assert_notequal(0, winid)
2496 let pos = popup_getpos(winid)
2497 call assert_equal(2, pos.height)
2498 call assert_equal(1, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002499
2500 " window-local option is set to default, buffer-local is not
2501 call assert_equal(0, getwinvar(winid, '&number'))
2502 call assert_equal(13, getbufvar(buf, '&wrapmargin'))
2503
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002504 call popup_close(winid)
2505 call assert_equal({}, popup_getpos(winid))
2506 call assert_equal(1, bufloaded(buf))
2507 exe 'bwipe! ' .. buf
Bram Moolenaar46451042019-08-24 15:50:46 +02002508 setlocal nonumber
Bram Moolenaar7866b872019-07-01 22:21:01 +02002509
2510 edit test_popupwin.vim
2511 let winid = popup_create(bufnr(''), {})
2512 redraw
2513 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002514 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002515endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02002516
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002517func Test_popupwin_terminal_buffer()
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002518 CheckFeature terminal
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002519 CheckUnix
Bram Moolenaare06a28f2020-05-13 23:24:12 +02002520 " Starting a terminal to run a shell in is considered flaky.
2521 let g:test_is_flaky = 1
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002522
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002523 let origwin = win_getid()
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002524 let termbuf = term_start(&shell, #{hidden: 1})
2525 let winid = popup_create(termbuf, #{minwidth: 40, minheight: 10})
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002526 " Wait for shell to start
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002527 call WaitForAssert({-> assert_equal("run", job_status(term_getjob(termbuf)))})
Bram Moolenaare06a28f2020-05-13 23:24:12 +02002528 sleep 100m
Bram Moolenaar631ebc42020-02-03 22:15:26 +01002529 " Check this doesn't crash
2530 call assert_equal(winnr(), winnr('j'))
2531 call assert_equal(winnr(), winnr('k'))
2532 call assert_equal(winnr(), winnr('h'))
2533 call assert_equal(winnr(), winnr('l'))
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002534
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002535 " Cannot quit while job is running
2536 call assert_fails('call feedkeys("\<C-W>:quit\<CR>", "xt")', 'E948:')
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002537
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002538 " Cannot enter Terminal-Normal mode. (TODO: but it works...)
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002539 call feedkeys("xxx\<C-W>N", 'xt')
2540 call assert_fails('call feedkeys("gf", "xt")', 'E863:')
2541 call feedkeys("a\<C-U>", 'xt')
2542
Bram Moolenaar3f65c662020-05-27 23:15:16 +02002543 " Cannot escape from terminal window
2544 call assert_fails('tab drop xxx', 'E863:')
2545
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002546 " Cannot open a second one.
2547 let termbuf2 = term_start(&shell, #{hidden: 1})
2548 call assert_fails('call popup_create(termbuf2, #{})', 'E861:')
2549 call term_sendkeys(termbuf2, "exit\<CR>")
2550
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002551 " Exiting shell closes popup window
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002552 call feedkeys("exit\<CR>", 'xt')
2553 " Wait for shell to exit
Bram Moolenaarb2b218d2020-06-22 20:22:19 +02002554 call WaitForAssert({-> assert_equal("dead", job_status(term_getjob(termbuf)))})
Bram Moolenaar373c6512020-06-22 19:24:23 +02002555
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002556 call feedkeys(":quit\<CR>", 'xt')
2557 call assert_equal(origwin, win_getid())
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002558endfunc
2559
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002560func Test_popupwin_close_prevwin()
2561 CheckFeature terminal
2562
2563 call assert_equal(1, winnr('$'))
2564 split
2565 wincmd b
2566 call assert_equal(2, winnr())
2567 let buf = term_start(&shell, #{hidden: 1})
2568 call popup_create(buf, {})
2569 call term_wait(buf, 100)
2570 call popup_clear(1)
2571 call assert_equal(2, winnr())
2572
2573 quit
2574 exe 'bwipe! ' .. buf
2575endfunc
2576
Bram Moolenaar934470e2019-09-01 23:27:05 +02002577func Test_popupwin_with_buffer_and_filter()
2578 new Xwithfilter
2579 call setline(1, range(100))
2580 let bufnr = bufnr()
2581 hide
2582
2583 func BufferFilter(win, key)
2584 if a:key == 'G'
2585 " recursive use of "G" does not cause problems.
2586 call win_execute(a:win, 'normal! G')
2587 return 1
2588 endif
2589 return 0
2590 endfunc
2591
2592 let winid = popup_create(bufnr, #{maxheight: 5, filter: 'BufferFilter'})
2593 call assert_equal(1, popup_getpos(winid).firstline)
2594 redraw
2595 call feedkeys("G", 'xt')
2596 call assert_equal(99, popup_getpos(winid).firstline)
2597
2598 call popup_close(winid)
2599 exe 'bwipe! ' .. bufnr
2600endfunc
2601
Bram Moolenaare296e312019-07-03 23:20:18 +02002602func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002603 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002604 \ maxwidth: 40,
2605 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02002606 \ })
2607 for top in range(1, 20)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002608 eval winid->popup_setoptions(#{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02002609 redraw
2610 call assert_equal(19, popup_getpos(winid).width)
2611 endfor
2612 call popup_clear()
2613endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02002614
2615func Test_popupwin_buf_close()
2616 let buf = bufadd('Xtestbuf')
2617 call bufload(buf)
2618 call setbufline(buf, 1, ['just', 'some', 'lines'])
2619 let winid = popup_create(buf, {})
2620 redraw
2621 call assert_equal(3, popup_getpos(winid).height)
2622 let bufinfo = getbufinfo(buf)[0]
2623 call assert_equal(1, bufinfo.changed)
2624 call assert_equal(0, bufinfo.hidden)
2625 call assert_equal(0, bufinfo.listed)
2626 call assert_equal(1, bufinfo.loaded)
2627 call assert_equal([], bufinfo.windows)
2628 call assert_equal([winid], bufinfo.popups)
2629
2630 call popup_close(winid)
2631 call assert_equal({}, popup_getpos(winid))
2632 let bufinfo = getbufinfo(buf)[0]
2633 call assert_equal(1, bufinfo.changed)
2634 call assert_equal(1, bufinfo.hidden)
2635 call assert_equal(0, bufinfo.listed)
2636 call assert_equal(1, bufinfo.loaded)
2637 call assert_equal([], bufinfo.windows)
2638 call assert_equal([], bufinfo.popups)
2639 exe 'bwipe! ' .. buf
2640endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02002641
2642func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002643 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02002644
2645 let lines =<< trim END
2646 call setline(1, range(1, 10))
2647 hi ScrollThumb ctermbg=blue
2648 hi ScrollBar ctermbg=red
2649 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002650 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002651 \ maxwidth: 10,
2652 \ maxheight: 3,
2653 \ pos : 'topleft',
2654 \ col : a:col,
2655 \ line : a:line,
2656 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02002657 \ })
2658 endfunc
2659 call PopupMenu(['x'], 1, 1)
2660 call PopupMenu(['123456789|'], 1, 16)
2661 call PopupMenu(['123456789|' .. ' '], 7, 1)
2662 call PopupMenu([repeat('123456789|', 100)], 7, 16)
2663 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
2664 END
2665 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002666 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02002667 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
2668
2669 " close the menu popupwin.
2670 call term_sendkeys(buf, " ")
2671 call term_sendkeys(buf, " ")
2672 call term_sendkeys(buf, " ")
2673 call term_sendkeys(buf, " ")
2674 call term_sendkeys(buf, " ")
2675
2676 " clean up
2677 call StopVimInTerminal(buf)
2678 call delete('XtestPopupMenuMaxWidth')
2679endfunc
2680
Bram Moolenaara901a372019-07-13 16:38:50 +02002681func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002682 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02002683
2684 let lines =<< trim END
2685 call setline(1, range(1, 20))
2686 hi ScrollThumb ctermbg=blue
2687 hi ScrollBar ctermbg=red
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002688 eval ['one', 'two', 'three', 'four', 'five',
2689 \ 'six', 'seven', 'eight', 'nine']
2690 \ ->popup_menu(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002691 \ minwidth: 8,
2692 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02002693 \ })
2694 END
2695 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002696 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02002697
2698 call term_sendkeys(buf, "j")
2699 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
2700
2701 call term_sendkeys(buf, "jjj")
2702 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
2703
2704 " if the cursor is the bottom line, it stays at the bottom line.
2705 call term_sendkeys(buf, repeat("j", 20))
2706 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
2707
2708 call term_sendkeys(buf, "kk")
2709 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
2710
2711 call term_sendkeys(buf, "k")
2712 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
2713
2714 " if the cursor is in the top line, it stays in the top line.
2715 call term_sendkeys(buf, repeat("k", 20))
2716 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
2717
2718 " close the menu popupwin.
2719 call term_sendkeys(buf, " ")
2720
2721 " clean up
2722 call StopVimInTerminal(buf)
2723 call delete('XtestPopupMenuScroll')
2724endfunc
2725
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002726func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002727 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002728
2729 let lines =<< trim END
2730 function! MyFilter(winid, key) abort
2731 if a:key == "0"
2732 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
2733 return 1
2734 endif
2735 if a:key == "G"
2736 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
2737 return 1
2738 endif
2739 if a:key == "j"
2740 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
2741 return 1
2742 endif
2743 if a:key == "k"
2744 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
2745 return 1
2746 endif
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002747 if a:key == ':'
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002748 call popup_close(a:winid)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002749 return 0
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002750 endif
2751 return 0
2752 endfunction
2753 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
2754 \ maxheight : 3,
2755 \ filter : 'MyFilter'
2756 \ })
2757 END
2758 call writefile(lines, 'XtestPopupMenuFilter')
2759 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
2760
2761 call term_sendkeys(buf, "j")
2762 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
2763
2764 call term_sendkeys(buf, "k")
2765 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
2766
2767 call term_sendkeys(buf, "G")
2768 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
2769
2770 call term_sendkeys(buf, "0")
2771 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
2772
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002773 " check that when the popup is closed in the filter the screen is redrawn
2774 call term_sendkeys(buf, ":")
2775 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_5', {})
2776 call term_sendkeys(buf, "\<CR>")
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002777
2778 " clean up
2779 call StopVimInTerminal(buf)
2780 call delete('XtestPopupMenuFilter')
2781endfunc
2782
2783func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002784 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002785
2786 let winid = popup_create('some text', {})
2787 call assert_equal(0, popup_getoptions(winid).cursorline)
2788 call popup_close(winid)
2789
2790 let winid = popup_create('some text', #{ cursorline: 1, })
2791 call assert_equal(1, popup_getoptions(winid).cursorline)
2792 call popup_close(winid)
2793
2794 let winid = popup_create('some text', #{ cursorline: 0, })
2795 call assert_equal(0, popup_getoptions(winid).cursorline)
2796 call popup_close(winid)
2797
2798 let winid = popup_menu('some text', {})
2799 call assert_equal(1, popup_getoptions(winid).cursorline)
2800 call popup_close(winid)
2801
2802 let winid = popup_menu('some text', #{ cursorline: 1, })
2803 call assert_equal(1, popup_getoptions(winid).cursorline)
2804 call popup_close(winid)
2805
2806 let winid = popup_menu('some text', #{ cursorline: 0, })
2807 call assert_equal(0, popup_getoptions(winid).cursorline)
2808 call popup_close(winid)
2809
2810 " ---------
2811 " Pattern 1
2812 " ---------
2813 let lines =<< trim END
2814 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
2815 END
2816 call writefile(lines, 'XtestPopupCursorLine')
2817 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2818 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
2819 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2820 call StopVimInTerminal(buf)
2821
2822 " ---------
2823 " Pattern 2
2824 " ---------
2825 let lines =<< trim END
2826 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
2827 END
2828 call writefile(lines, 'XtestPopupCursorLine')
2829 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2830 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
2831 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2832 call StopVimInTerminal(buf)
2833
2834 " ---------
2835 " Pattern 3
2836 " ---------
2837 let lines =<< trim END
2838 function! MyFilter(winid, key) abort
2839 if a:key == "j"
2840 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2841 return 1
2842 endif
2843 if a:key == 'x'
2844 call popup_close(a:winid)
2845 return 1
2846 endif
2847 return 0
2848 endfunction
2849 call popup_menu(['111', '222', '333'], #{
2850 \ cursorline : 0,
2851 \ maxheight : 2,
2852 \ filter : 'MyFilter',
2853 \ })
2854 END
2855 call writefile(lines, 'XtestPopupCursorLine')
2856 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2857 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
2858 call term_sendkeys(buf, "j")
2859 call term_sendkeys(buf, "j")
2860 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
2861 call term_sendkeys(buf, "x")
2862 call StopVimInTerminal(buf)
2863
2864 " ---------
2865 " Pattern 4
2866 " ---------
2867 let lines =<< trim END
2868 function! MyFilter(winid, key) abort
2869 if a:key == "j"
2870 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2871 return 1
2872 endif
2873 if a:key == 'x'
2874 call popup_close(a:winid)
2875 return 1
2876 endif
2877 return 0
2878 endfunction
2879 call popup_menu(['111', '222', '333'], #{
2880 \ cursorline : 1,
2881 \ maxheight : 2,
2882 \ filter : 'MyFilter',
2883 \ })
2884 END
2885 call writefile(lines, 'XtestPopupCursorLine')
2886 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2887 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
2888 call term_sendkeys(buf, "j")
2889 call term_sendkeys(buf, "j")
2890 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
2891 call term_sendkeys(buf, "x")
2892 call StopVimInTerminal(buf)
2893
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02002894 " ---------
2895 " Cursor in second line when creating the popup
2896 " ---------
2897 let lines =<< trim END
2898 let winid = popup_create(['111', '222', '333'], #{
2899 \ cursorline : 1,
2900 \ })
2901 call win_execute(winid, "2")
2902 END
2903 call writefile(lines, 'XtestPopupCursorLine')
2904 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2905 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_7', {})
2906 call StopVimInTerminal(buf)
2907
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002908 call delete('XtestPopupCursorLine')
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01002909
2910 " ---------
2911 " Use current buffer for popupmenu
2912 " ---------
2913 let lines =<< trim END
2914 call setline(1, ['one', 'two', 'three'])
2915 let winid = popup_create(bufnr('%'), #{
2916 \ cursorline : 1,
2917 \ })
2918 call win_execute(winid, "2")
2919 END
2920 call writefile(lines, 'XtestPopupCursorLine')
2921 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2922 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_8', {})
2923 call StopVimInTerminal(buf)
2924
2925 call delete('XtestPopupCursorLine')
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002926endfunc
2927
Bram Moolenaarf914a332019-07-20 15:09:56 +02002928func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002929 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002930 CheckFeature quickfix
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002931
Bram Moolenaarf914a332019-07-20 15:09:56 +02002932 call writefile([
2933 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
2934 \ "another\tXtagfile\t/^this is another",
2935 \ "theword\tXtagfile\t/^theword"],
2936 \ 'Xtags')
2937 call writefile(range(1,20)
2938 \ + ['theword is here']
2939 \ + range(22, 27)
2940 \ + ['this is another place']
2941 \ + range(29, 40),
2942 \ "Xtagfile")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002943 call writefile(range(1,10)
2944 \ + ['searched word is here']
2945 \ + range(12, 20),
2946 \ "Xheader.h")
Bram Moolenaarf914a332019-07-20 15:09:56 +02002947 let lines =<< trim END
2948 set tags=Xtags
2949 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002950 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002951 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002952 \ 'three',
2953 \ 'four',
2954 \ 'five',
2955 \ 'six',
2956 \ 'seven',
2957 \ 'find theword somewhere',
2958 \ 'nine',
2959 \ 'this is another word',
2960 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02002961 set previewpopup=height:4,width:40
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002962 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02002963 END
2964 call writefile(lines, 'XtestPreviewPopup')
2965 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
2966
2967 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
2968 call term_sendkeys(buf, ":\<CR>")
2969 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
2970
2971 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2972 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
2973
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002974 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002975 call term_sendkeys(buf, ":\<CR>")
2976 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
2977
2978 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2979 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
2980
Bram Moolenaar799439a2020-02-11 21:44:17 +01002981 call term_sendkeys(buf, ":silent cd ..\<CR>:\<CR>")
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002982 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar799439a2020-02-11 21:44:17 +01002983 call term_sendkeys(buf, ":silent cd testdir\<CR>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002984
2985 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02002986 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002987 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
2988
2989 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
2990 call term_sendkeys(buf, ":\<CR>")
2991 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
2992
2993 call term_sendkeys(buf, ":pclose\<CR>")
2994 call term_sendkeys(buf, ":psearch searched\<CR>")
2995 call term_sendkeys(buf, ":\<CR>")
2996 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002997
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01002998 call term_sendkeys(buf, "\<C-W>p")
2999 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_9', {})
3000
3001 call term_sendkeys(buf, ":call win_execute(popup_findpreview(), 'call popup_clear()')\<CR>")
3002 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_10', {})
3003
Bram Moolenaarf914a332019-07-20 15:09:56 +02003004 call StopVimInTerminal(buf)
3005 call delete('Xtags')
3006 call delete('Xtagfile')
3007 call delete('XtestPreviewPopup')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003008 call delete('Xheader.h')
Bram Moolenaarf914a332019-07-20 15:09:56 +02003009endfunc
3010
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003011func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003012 let lines =<< trim END
3013 set completeopt+=preview,popup
3014 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02003015 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003016
3017 func CompleteFuncDict(findstart, base)
3018 if a:findstart
3019 if col('.') > 10
3020 return col('.') - 10
3021 endif
3022 return 0
3023 endif
3024
3025 return {
3026 \ 'words': [
3027 \ {
3028 \ 'word': 'aword',
3029 \ 'abbr': 'wrd',
3030 \ 'menu': 'extra text',
3031 \ 'info': 'words are cool',
3032 \ 'kind': 'W',
3033 \ 'user_data': 'test'
3034 \ },
3035 \ {
3036 \ 'word': 'anotherword',
3037 \ 'abbr': 'anotwrd',
3038 \ 'menu': 'extra text',
3039 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
3040 \ 'kind': 'W',
3041 \ 'user_data': 'notest'
3042 \ },
3043 \ {
3044 \ 'word': 'noinfo',
3045 \ 'abbr': 'noawrd',
3046 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02003047 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003048 \ 'kind': 'W',
3049 \ 'user_data': 'notest'
3050 \ },
3051 \ {
3052 \ 'word': 'thatword',
3053 \ 'abbr': 'thatwrd',
3054 \ 'menu': 'extra text',
3055 \ 'info': 'that word is cool',
3056 \ 'kind': 'W',
3057 \ 'user_data': 'notest'
3058 \ },
3059 \ ]
3060 \ }
3061 endfunc
3062 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003063 func ChangeColor()
3064 let id = popup_findinfo()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003065 eval id->popup_setoptions(#{highlight: 'InfoPopup'})
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003066 endfunc
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003067
3068 func InfoHidden()
3069 set completepopup=height:4,border:off,align:menu
3070 set completeopt-=popup completeopt+=popuphidden
3071 au CompleteChanged * call HandleChange()
3072 endfunc
3073
3074 let s:counter = 0
3075 func HandleChange()
3076 let s:counter += 1
3077 let selected = complete_info(['selected']).selected
3078 if selected <= 0
3079 " First time: do nothing, info remains hidden
3080 return
3081 endif
3082 if selected == 1
3083 " Second time: show info right away
3084 let id = popup_findinfo()
3085 if id
3086 call popup_settext(id, 'immediate info ' .. s:counter)
3087 call popup_show(id)
3088 endif
3089 else
3090 " Third time: show info after a short delay
3091 call timer_start(100, 'ShowInfo')
3092 endif
3093 endfunc
3094
3095 func ShowInfo(...)
3096 let id = popup_findinfo()
3097 if id
3098 call popup_settext(id, 'async info ' .. s:counter)
3099 call popup_show(id)
3100 endif
3101 endfunc
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003102 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003103 return lines
3104endfunc
3105
3106func Test_popupmenu_info_border()
3107 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003108 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003109
3110 let lines = Get_popupmenu_lines()
3111 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003112 call writefile(lines, 'XtestInfoPopup')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003113
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003114 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003115 call TermWait(buf, 25)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003116
3117 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3118 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
3119
3120 call term_sendkeys(buf, "\<C-N>")
3121 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
3122
3123 call term_sendkeys(buf, "\<C-N>")
3124 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
3125
3126 call term_sendkeys(buf, "\<C-N>\<C-N>")
3127 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
3128
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02003129 " info on the left with scrollbar
3130 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
3131 call term_sendkeys(buf, "\<C-N>\<C-N>")
3132 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
3133
Bram Moolenaar202c3f72019-11-21 12:12:35 +01003134 " Test that the popupmenu's scrollbar and infopopup do not overlap
3135 call term_sendkeys(buf, "\<Esc>")
3136 call term_sendkeys(buf, ":set pumheight=3\<CR>")
3137 call term_sendkeys(buf, "cc\<C-X>\<C-U>")
3138 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_6', {})
3139
Bram Moolenaarca7c0782020-01-14 20:42:48 +01003140 " Hide the info popup, cycle trough buffers, make sure it didn't get
3141 " deleted.
3142 call term_sendkeys(buf, "\<Esc>")
3143 call term_sendkeys(buf, ":set hidden\<CR>")
3144 call term_sendkeys(buf, ":bn\<CR>")
3145 call term_sendkeys(buf, ":bn\<CR>")
3146 call term_sendkeys(buf, "otest text test text\<C-X>\<C-U>")
3147 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_7', {})
3148
Bram Moolenaar447bfba2020-07-18 16:07:16 +02003149 " Test that when the option is changed the popup changes.
3150 call term_sendkeys(buf, "\<Esc>")
3151 call term_sendkeys(buf, ":set completepopup=border:off\<CR>")
3152 call term_sendkeys(buf, "a\<C-X>\<C-U>")
3153 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_8', {})
3154
Bram Moolenaar6d585f42020-07-26 22:20:54 +02003155 call term_sendkeys(buf, " \<Esc>")
3156 call term_sendkeys(buf, ":set completepopup+=width:10\<CR>")
3157 call term_sendkeys(buf, "a\<C-X>\<C-U>")
3158 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_9', {})
3159
Bram Moolenaarca7c0782020-01-14 20:42:48 +01003160 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003161 call StopVimInTerminal(buf)
3162 call delete('XtestInfoPopup')
3163endfunc
3164
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003165func Test_popupmenu_info_noborder()
3166 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003167 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003168
3169 let lines = Get_popupmenu_lines()
3170 call add(lines, 'set completepopup=height:4,border:off')
3171 call writefile(lines, 'XtestInfoPopupNb')
3172
3173 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003174 call TermWait(buf, 25)
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003175
3176 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3177 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
3178
3179 call StopVimInTerminal(buf)
3180 call delete('XtestInfoPopupNb')
3181endfunc
3182
Bram Moolenaar258cef52019-08-21 17:29:29 +02003183func Test_popupmenu_info_align_menu()
3184 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003185 CheckFeature quickfix
Bram Moolenaar258cef52019-08-21 17:29:29 +02003186
3187 let lines = Get_popupmenu_lines()
3188 call add(lines, 'set completepopup=height:4,border:off,align:menu')
3189 call writefile(lines, 'XtestInfoPopupNb')
3190
3191 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003192 call TermWait(buf, 25)
Bram Moolenaar258cef52019-08-21 17:29:29 +02003193
3194 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3195 call term_sendkeys(buf, "\<C-N>")
3196 call term_sendkeys(buf, "\<C-N>")
3197 call term_sendkeys(buf, "\<C-N>")
3198 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
3199
3200 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
3201 call term_sendkeys(buf, "\<C-N>")
3202 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
3203
3204 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003205 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02003206 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
3207 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
3208 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
3209
3210 call StopVimInTerminal(buf)
3211 call delete('XtestInfoPopupNb')
3212endfunc
3213
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003214func Test_popupmenu_info_hidden()
3215 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003216 CheckFeature quickfix
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003217
3218 let lines = Get_popupmenu_lines()
3219 call add(lines, 'call InfoHidden()')
3220 call writefile(lines, 'XtestInfoPopupHidden')
3221
3222 let buf = RunVimInTerminal('-S XtestInfoPopupHidden', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003223 call TermWait(buf, 25)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003224
3225 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3226 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_1', {})
3227
3228 call term_sendkeys(buf, "\<C-N>")
3229 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_2', {})
3230
3231 call term_sendkeys(buf, "\<C-N>")
3232 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_3', {})
3233
3234 call term_sendkeys(buf, "\<Esc>")
3235 call StopVimInTerminal(buf)
3236 call delete('XtestInfoPopupHidden')
3237endfunc
3238
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003239func Test_popupmenu_info_too_wide()
3240 CheckScreendump
3241 CheckFeature quickfix
3242
3243 let lines =<< trim END
3244 call setline(1, range(10))
3245
3246 set completeopt+=preview,popup
3247 set completepopup=align:menu
3248 set omnifunc=OmniFunc
3249 hi InfoPopup ctermbg=lightgrey
3250
3251 func OmniFunc(findstart, base)
3252 if a:findstart
3253 return 0
3254 endif
3255
3256 let menuText = 'some long text to make sure the menu takes up all of the width of the window'
3257 return #{
3258 \ words: [
3259 \ #{
3260 \ word: 'scrap',
3261 \ menu: menuText,
3262 \ info: "other words are\ncooler than this and some more text\nto make wrap",
3263 \ },
3264 \ #{
3265 \ word: 'scappier',
3266 \ menu: menuText,
3267 \ info: 'words are cool',
3268 \ },
3269 \ #{
3270 \ word: 'scrappier2',
3271 \ menu: menuText,
3272 \ info: 'words are cool',
3273 \ },
3274 \ ]
3275 \ }
3276 endfunc
3277 END
3278
3279 call writefile(lines, 'XtestInfoPopupWide')
3280 let buf = RunVimInTerminal('-S XtestInfoPopupWide', #{rows: 8})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003281 call TermWait(buf, 25)
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003282
3283 call term_sendkeys(buf, "Ascr\<C-X>\<C-O>")
3284 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_wide_1', {})
3285
3286 call term_sendkeys(buf, "\<Esc>")
3287 call StopVimInTerminal(buf)
3288 call delete('XtestInfoPopupWide')
3289endfunc
3290
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003291func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02003292 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003293 call popup_clear()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003294 let winid = 'nothing wrong'->popup_notification({})
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003295 call assert_equal(bufnr, winbufnr(winid))
3296 call popup_clear()
3297endfunc
3298
Bram Moolenaar1824f452019-10-02 23:06:46 +02003299func Test_popupwin_getoptions_tablocal()
3300 topleft split
3301 let win1 = popup_create('nothing', #{maxheight: 8})
3302 let win2 = popup_create('something', #{maxheight: 10})
3303 let win3 = popup_create('something', #{maxheight: 15})
3304 call assert_equal(8, popup_getoptions(win1).maxheight)
3305 call assert_equal(10, popup_getoptions(win2).maxheight)
3306 call assert_equal(15, popup_getoptions(win3).maxheight)
3307 call popup_clear()
3308 quit
3309endfunc
3310
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02003311func Test_popupwin_cancel()
3312 let win1 = popup_create('one', #{line: 5, filter: {... -> 0}})
3313 let win2 = popup_create('two', #{line: 10, filter: {... -> 0}})
3314 let win3 = popup_create('three', #{line: 15, filter: {... -> 0}})
3315 call assert_equal(5, popup_getpos(win1).line)
3316 call assert_equal(10, popup_getpos(win2).line)
3317 call assert_equal(15, popup_getpos(win3).line)
3318 " TODO: this also works without patch 8.1.2110
3319 call feedkeys("\<C-C>", 'xt')
3320 call assert_equal(5, popup_getpos(win1).line)
3321 call assert_equal(10, popup_getpos(win2).line)
3322 call assert_equal({}, popup_getpos(win3))
3323 call feedkeys("\<C-C>", 'xt')
3324 call assert_equal(5, popup_getpos(win1).line)
3325 call assert_equal({}, popup_getpos(win2))
3326 call assert_equal({}, popup_getpos(win3))
3327 call feedkeys("\<C-C>", 'xt')
3328 call assert_equal({}, popup_getpos(win1))
3329 call assert_equal({}, popup_getpos(win2))
3330 call assert_equal({}, popup_getpos(win3))
3331endfunc
3332
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003333func Test_popupwin_filter_redraw()
3334 " Create two popups with a filter that closes the popup when typing "0".
3335 " Both popups should close, even though the redraw also calls
3336 " popup_reset_handled()
3337
3338 func CloseFilter(winid, key)
3339 if a:key == '0'
3340 call popup_close(a:winid)
3341 redraw
3342 endif
3343 return 0 " pass the key
3344 endfunc
3345
3346 let id1 = popup_create('first one', #{
3347 \ line: 1,
3348 \ col: 1,
3349 \ filter: 'CloseFilter',
3350 \ })
3351 let id2 = popup_create('second one', #{
3352 \ line: 9,
3353 \ col: 1,
3354 \ filter: 'CloseFilter',
3355 \ })
3356 call assert_equal(1, popup_getpos(id1).line)
3357 call assert_equal(9, popup_getpos(id2).line)
3358
3359 call feedkeys('0', 'xt')
3360 call assert_equal({}, popup_getpos(id1))
3361 call assert_equal({}, popup_getpos(id2))
3362
3363 call popup_clear()
3364 delfunc CloseFilter
3365endfunc
3366
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003367func Test_popupwin_double_width()
3368 CheckScreendump
3369
3370 let lines =<< trim END
3371 call setline(1, 'x你好世界你好世你好世界你好')
3372 call setline(2, '你好世界你好世你好世界你好')
3373 call setline(3, 'x你好世界你好世你好世界你好')
3374 call popup_create('你好,世界 - 你好,世界xxxxx', #{line: 1, col: 3, maxwidth: 14})
3375 END
3376 call writefile(lines, 'XtestPopupWide')
3377
3378 let buf = RunVimInTerminal('-S XtestPopupWide', #{rows: 10})
3379 call VerifyScreenDump(buf, 'Test_popupwin_doublewidth_1', {})
3380
3381 call StopVimInTerminal(buf)
3382 call delete('XtestPopupWide')
3383endfunc
3384
3385func Test_popupwin_sign()
3386 CheckScreendump
3387
3388 let lines =<< trim END
3389 call setline(1, range(10))
3390 call sign_define('Current', {
3391 \ 'text': '>>',
3392 \ 'texthl': 'WarningMsg',
3393 \ 'linehl': 'Error',
3394 \ })
3395 call sign_define('Other', {
3396 \ 'text': '#!',
3397 \ 'texthl': 'Error',
3398 \ 'linehl': 'Search',
3399 \ })
3400 let winid = popup_create(['hello', 'bright', 'world'], {
3401 \ 'minwidth': 20,
3402 \ })
3403 call setwinvar(winid, "&signcolumn", "yes")
3404 let winbufnr = winbufnr(winid)
3405
3406 " add sign to current buffer, shows
3407 call sign_place(1, 'Selected', 'Current', bufnr('%'), {'lnum': 1})
3408 " add sign to current buffer, does not show
3409 call sign_place(2, 'PopUpSelected', 'Other', bufnr('%'), {'lnum': 2})
3410
3411 " add sign to popup buffer, shows
3412 call sign_place(3, 'PopUpSelected', 'Other', winbufnr, {'lnum': 1})
3413 " add sign to popup buffer, does not show
3414 call sign_place(4, 'Selected', 'Current', winbufnr, {'lnum': 2})
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02003415
3416 func SetOptions()
3417 call setwinvar(g:winid, '&number', 1)
3418 call setwinvar(g:winid, '&foldcolumn', 2)
3419 call popup_settext(g:winid, 'a longer line to check the width')
3420 endfunc
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003421 END
3422 call writefile(lines, 'XtestPopupSign')
3423
3424 let buf = RunVimInTerminal('-S XtestPopupSign', #{rows: 10})
3425 call VerifyScreenDump(buf, 'Test_popupwin_sign_1', {})
3426
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02003427 " set more options to check the width is adjusted
3428 call term_sendkeys(buf, ":call SetOptions()\<CR>")
3429 call VerifyScreenDump(buf, 'Test_popupwin_sign_2', {})
3430
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003431 call StopVimInTerminal(buf)
3432 call delete('XtestPopupSign')
3433endfunc
3434
Bram Moolenaar99ebf222019-12-10 23:44:48 +01003435func Test_popupwin_bufnr()
3436 let popwin = popup_create(['blah'], #{})
3437 let popbuf = winbufnr(popwin)
3438 split asdfasdf
3439 let newbuf = bufnr()
3440 call assert_true(newbuf > popbuf, 'New buffer number is higher')
3441 call assert_equal(newbuf, bufnr('$'))
3442 call popup_clear()
3443 let popwin = popup_create(['blah'], #{})
3444 " reuses previous buffer number
3445 call assert_equal(popbuf, winbufnr(popwin))
3446 call assert_equal(newbuf, bufnr('$'))
3447
3448 call popup_clear()
3449 bwipe!
3450endfunc
3451
Bram Moolenaarec084d32020-02-28 22:44:47 +01003452func Test_popupwin_filter_input_multibyte()
3453 func MyPopupFilter(winid, c)
3454 let g:bytes = range(a:c->strlen())->map({i -> char2nr(a:c[i])})
3455 return 0
3456 endfunc
3457 let winid = popup_create('', #{mapping: 0, filter: 'MyPopupFilter'})
3458
3459 " UTF-8: E3 80 80, including K_SPECIAL(0x80)
3460 call feedkeys("\u3000", 'xt')
3461 call assert_equal([0xe3, 0x80, 0x80], g:bytes)
3462
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01003463 " UTF-8: E3 80 9B, including CSI(0x9B)
3464 call feedkeys("\u301b", 'xt')
3465 call assert_equal([0xe3, 0x80, 0x9b], g:bytes)
Bram Moolenaarec084d32020-02-28 22:44:47 +01003466
Bram Moolenaar20298ce2020-06-19 21:46:52 +02003467 if has('unix')
3468 " with modifyOtherKeys <M-S-a> does not include a modifier sequence
Bram Moolenaarb326edf2020-06-20 15:03:38 +02003469 if has('gui_running')
3470 call feedkeys("\x9b\xfc\x08A", 'Lx!')
3471 else
3472 call feedkeys("\<Esc>[27;4;65~", 'Lx!')
3473 endif
Bram Moolenaar20298ce2020-06-19 21:46:52 +02003474 call assert_equal([0xc3, 0x81], g:bytes)
3475 endif
3476
Bram Moolenaarec084d32020-02-28 22:44:47 +01003477 call popup_clear()
3478 delfunc MyPopupFilter
3479 unlet g:bytes
3480endfunc
3481
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003482func Test_popupwin_filter_close_ctrl_c()
3483 CheckScreendump
3484
3485 let lines =<< trim END
3486 vsplit
3487 set laststatus=2
3488 set statusline=%!Statusline()
3489
3490 function Statusline() abort
3491 return '%<%f %h%m%r%=%-14.(%l,%c%V%) %P'
3492 endfunction
3493
3494 call popup_create('test test test test...', {'filter': {-> 0}})
3495 END
3496 call writefile(lines, 'XtestPopupCtrlC')
3497
3498 let buf = RunVimInTerminal('-S XtestPopupCtrlC', #{rows: 10})
3499
3500 call term_sendkeys(buf, "\<C-C>")
3501 call VerifyScreenDump(buf, 'Test_popupwin_ctrl_c', {})
3502
3503 call StopVimInTerminal(buf)
3504 call delete('XtestPopupCorners')
3505endfunc
3506
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003507func Test_popupwin_atcursor_far_right()
3508 new
3509
3510 " this was getting stuck
3511 set signcolumn=yes
3512 call setline(1, repeat('=', &columns))
3513 normal! ggg$
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003514 let winid = popup_atcursor(repeat('x', 500), #{moved: 'any', border: []})
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003515
Bram Moolenaar6d585f42020-07-26 22:20:54 +02003516 " 'signcolumn' was getting reset
3517 call setwinvar(winid, '&signcolumn', 'yes')
3518 call popup_setoptions(winid, {'zindex': 1000})
3519 call assert_equal('yes', getwinvar(winid, '&signcolumn'))
3520
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003521 call popup_close(winid)
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003522 bwipe!
3523 set signcolumn&
3524endfunc
3525
Bram Moolenaar0f1563f2020-03-20 21:15:51 +01003526func Test_popupwin_splitmove()
3527 vsplit
3528 let win2 = win_getid()
3529 let popup_winid = popup_dialog('hello', {})
3530 call assert_fails('call win_splitmove(popup_winid, win2)', 'E957:')
3531 call assert_fails('call win_splitmove(win2, popup_winid)', 'E957:')
3532
3533 call popup_clear()
3534 bwipe
3535endfunc
3536
3537
Bram Moolenaar331bafd2019-07-20 17:46:05 +02003538" vim: shiftwidth=2 sts=2