blob: 954086cae4d0a9bbe942890c57797f5cb390f518 [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 Moolenaare2e40752020-09-04 21:18:46 +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', {})
Bram Moolenaare2e40752020-09-04 21:18:46 +0200983 call assert_fails('call win_execute(winid, printf("normal! :\<C-u>call popup_close(%d)\<CR>", winid))', 'E994:')
Bram Moolenaar49540192019-12-11 19:34:54 +0100984 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
Bram Moolenaar1f42f5a2020-09-03 18:52:24 +02002242func Test_popup_settext_getline()
2243 let id = popup_create('', #{ tabpage: 0 })
2244 call popup_settext(id, ['a','b'])
2245 call assert_equal(2, line('$', id)) " OK :)
2246 call popup_close(id)
2247
2248 let id = popup_create('', #{ tabpage: -1 })
2249 call popup_settext(id, ['a','b'])
2250 call assert_equal(2, line('$', id)) " Fails :(
2251 call popup_close(id)
2252endfunc
2253
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002254func Test_popup_hidden()
2255 new
2256
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002257 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002258 redraw
2259 call assert_equal(0, popup_getpos(winid).visible)
2260 call popup_close(winid)
2261
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002262 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002263 redraw
2264 call assert_equal(0, popup_getpos(winid).visible)
2265 call popup_close(winid)
2266
2267 func QuitCallback(id, res)
2268 let s:cb_winid = a:id
2269 let s:cb_res = a:res
2270 endfunc
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002271 let winid = 'make a choice'->popup_dialog(#{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002272 \ filter: 'popup_filter_yesno',
2273 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002274 \ })
2275 redraw
2276 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02002277 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
2278 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002279 exe "normal anot used by filter\<Esc>"
2280 call assert_equal('not used by filter', getline(1))
2281
2282 call popup_show(winid)
2283 call feedkeys('y', "xt")
2284 call assert_equal(1, s:cb_res)
2285
2286 bwipe!
2287 delfunc QuitCallback
2288endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02002289
2290" Test options not checked elsewhere
2291func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002292 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002293 let options = popup_getoptions(winid)
2294 call assert_equal(1, options.wrap)
2295 call assert_equal(0, options.drag)
2296 call assert_equal('Beautiful', options.highlight)
2297
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002298 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002299 let options = popup_getoptions(winid)
2300 call assert_equal(0, options.wrap)
2301 call assert_equal(1, options.drag)
2302 call assert_equal('Another', options.highlight)
2303
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002304 call assert_fails('call popup_setoptions(winid, [])', 'E715:')
2305 call assert_fails('call popup_setoptions(winid, test_null_dict())', 'E715:')
2306
Bram Moolenaarae943152019-06-16 22:54:14 +02002307 call popup_close(winid)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002308 call assert_equal(0, popup_setoptions(winid, options.wrap))
Bram Moolenaarae943152019-06-16 22:54:14 +02002309endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002310
2311func Test_popupwin_garbage_collect()
2312 func MyPopupFilter(x, winid, c)
2313 " NOP
2314 endfunc
2315
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002316 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002317 call test_garbagecollect_now()
2318 redraw
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02002319 " Must not crash caused by invalid memory access
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002320 call feedkeys('j', 'xt')
2321 call assert_true(v:true)
2322
2323 call popup_close(winid)
2324 delfunc MyPopupFilter
2325endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002326
Bram Moolenaar581ba392019-09-03 22:08:33 +02002327func Test_popupwin_filter_mode()
2328 func MyPopupFilter(winid, c)
2329 let s:typed = a:c
2330 if a:c == ':' || a:c == "\r" || a:c == 'v'
2331 " can start cmdline mode, get out, and start/stop Visual mode
2332 return 0
2333 endif
2334 return 1
2335 endfunc
2336
2337 " Normal, Visual and Insert mode
2338 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'nvi'})
2339 redraw
2340 call feedkeys('x', 'xt')
2341 call assert_equal('x', s:typed)
2342
2343 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2344 call assert_equal(':', s:typed)
2345 call assert_equal('foo', g:foo)
2346
2347 let @x = 'something'
2348 call feedkeys('v$"xy', 'xt')
2349 call assert_equal('y', s:typed)
2350 call assert_equal('something', @x) " yank command is filtered out
2351 call feedkeys('v', 'xt') " end Visual mode
2352
2353 call popup_close(winid)
2354
2355 " only Normal mode
2356 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'n'})
2357 redraw
2358 call feedkeys('x', 'xt')
2359 call assert_equal('x', s:typed)
2360
2361 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2362 call assert_equal(':', s:typed)
2363 call assert_equal('foo', g:foo)
2364
2365 let @x = 'something'
2366 call feedkeys('v$"xy', 'xt')
2367 call assert_equal('v', s:typed)
2368 call assert_notequal('something', @x)
2369
2370 call popup_close(winid)
2371
2372 " default: all modes
2373 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
2374 redraw
2375 call feedkeys('x', 'xt')
2376 call assert_equal('x', s:typed)
2377
2378 let g:foo = 'bar'
2379 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2380 call assert_equal("\r", s:typed)
2381 call assert_equal('bar', g:foo)
2382
2383 let @x = 'something'
2384 call feedkeys('v$"xy', 'xt')
2385 call assert_equal('y', s:typed)
2386 call assert_equal('something', @x) " yank command is filtered out
2387 call feedkeys('v', 'xt') " end Visual mode
2388
2389 call popup_close(winid)
2390 delfunc MyPopupFilter
2391endfunc
2392
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002393func Test_popupwin_filter_mouse()
2394 func MyPopupFilter(winid, c)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002395 let g:got_mousepos = getmousepos()
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002396 return 0
2397 endfunc
2398
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002399 call setline(1, ['.'->repeat(25)]->repeat(10))
2400 let winid = popup_create(['short', 'long line that will wrap', 'other'], #{
2401 \ line: 2,
2402 \ col: 4,
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002403 \ maxwidth: 12,
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002404 \ padding: [],
2405 \ border: [],
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002406 \ filter: 'MyPopupFilter',
2407 \ })
2408 redraw
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002409 " 123456789012345678901
2410 " 1 .....................
2411 " 2 ...+--------------+..
2412 " 3 ...| |..
2413 " 4 ...| short |..
2414 " 5 ...| long line th |..
2415 " 6 ...| at will wrap |..
2416 " 7 ...| other |..
2417 " 8 ...| |..
2418 " 9 ...+--------------+..
2419 " 10 .....................
2420 let tests = []
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002421
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002422 func AddItemOutsidePopup(tests, row, col)
2423 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2424 \ screenrow: a:row, screencol: a:col,
2425 \ winid: win_getid(), winrow: a:row, wincol: a:col,
2426 \ line: a:row, column: a:col,
2427 \ }})
2428 endfunc
2429 func AddItemInPopupBorder(tests, winid, row, col)
2430 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2431 \ screenrow: a:row, screencol: a:col,
2432 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2433 \ line: 0, column: 0,
2434 \ }})
2435 endfunc
2436 func AddItemInPopupText(tests, winid, row, col, textline, textcol)
2437 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2438 \ screenrow: a:row, screencol: a:col,
2439 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2440 \ line: a:textline, column: a:textcol,
2441 \ }})
2442 endfunc
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002443
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002444 " above and below popup
2445 for c in range(1, 21)
2446 call AddItemOutsidePopup(tests, 1, c)
2447 call AddItemOutsidePopup(tests, 10, c)
2448 endfor
2449 " left and right of popup
2450 for r in range(1, 10)
2451 call AddItemOutsidePopup(tests, r, 3)
2452 call AddItemOutsidePopup(tests, r, 20)
2453 endfor
2454 " top and bottom in popup
2455 for c in range(4, 19)
2456 call AddItemInPopupBorder(tests, winid, 2, c)
2457 call AddItemInPopupBorder(tests, winid, 3, c)
2458 call AddItemInPopupBorder(tests, winid, 8, c)
2459 call AddItemInPopupBorder(tests, winid, 9, c)
2460 endfor
2461 " left and right margin in popup
2462 for r in range(2, 9)
2463 call AddItemInPopupBorder(tests, winid, r, 4)
2464 call AddItemInPopupBorder(tests, winid, r, 5)
2465 call AddItemInPopupBorder(tests, winid, r, 18)
2466 call AddItemInPopupBorder(tests, winid, r, 19)
2467 endfor
2468 " text "short"
2469 call AddItemInPopupText(tests, winid, 4, 6, 1, 1)
2470 call AddItemInPopupText(tests, winid, 4, 10, 1, 5)
2471 call AddItemInPopupText(tests, winid, 4, 11, 1, 6)
2472 call AddItemInPopupText(tests, winid, 4, 17, 1, 6)
2473 " text "long line th"
2474 call AddItemInPopupText(tests, winid, 5, 6, 2, 1)
2475 call AddItemInPopupText(tests, winid, 5, 10, 2, 5)
2476 call AddItemInPopupText(tests, winid, 5, 17, 2, 12)
2477 " text "at will wrap"
2478 call AddItemInPopupText(tests, winid, 6, 6, 2, 13)
2479 call AddItemInPopupText(tests, winid, 6, 10, 2, 17)
2480 call AddItemInPopupText(tests, winid, 6, 17, 2, 24)
2481 " text "other"
2482 call AddItemInPopupText(tests, winid, 7, 6, 3, 1)
2483 call AddItemInPopupText(tests, winid, 7, 10, 3, 5)
2484 call AddItemInPopupText(tests, winid, 7, 11, 3, 6)
2485 call AddItemInPopupText(tests, winid, 7, 17, 3, 6)
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002486
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002487 for item in tests
2488 call test_setmouse(item.clickrow, item.clickcol)
2489 call feedkeys("\<LeftMouse>", 'xt')
2490 call assert_equal(item.result, g:got_mousepos)
2491 endfor
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002492
2493 call popup_close(winid)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002494 enew!
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002495 delfunc MyPopupFilter
2496endfunc
2497
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002498func Test_popupwin_with_buffer()
2499 call writefile(['some text', 'in a buffer'], 'XsomeFile')
2500 let buf = bufadd('XsomeFile')
2501 call assert_equal(0, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002502
2503 setlocal number
2504 call setbufvar(buf, "&wrapmargin", 13)
2505
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002506 let winid = popup_create(buf, {})
2507 call assert_notequal(0, winid)
2508 let pos = popup_getpos(winid)
2509 call assert_equal(2, pos.height)
2510 call assert_equal(1, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002511
2512 " window-local option is set to default, buffer-local is not
2513 call assert_equal(0, getwinvar(winid, '&number'))
2514 call assert_equal(13, getbufvar(buf, '&wrapmargin'))
2515
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002516 call popup_close(winid)
2517 call assert_equal({}, popup_getpos(winid))
2518 call assert_equal(1, bufloaded(buf))
2519 exe 'bwipe! ' .. buf
Bram Moolenaar46451042019-08-24 15:50:46 +02002520 setlocal nonumber
Bram Moolenaar7866b872019-07-01 22:21:01 +02002521
2522 edit test_popupwin.vim
2523 let winid = popup_create(bufnr(''), {})
2524 redraw
2525 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002526 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002527endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02002528
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002529func Test_popupwin_terminal_buffer()
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002530 CheckFeature terminal
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002531 CheckUnix
Bram Moolenaare06a28f2020-05-13 23:24:12 +02002532 " Starting a terminal to run a shell in is considered flaky.
2533 let g:test_is_flaky = 1
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002534
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002535 let origwin = win_getid()
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002536 let termbuf = term_start(&shell, #{hidden: 1})
2537 let winid = popup_create(termbuf, #{minwidth: 40, minheight: 10})
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002538 " Wait for shell to start
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002539 call WaitForAssert({-> assert_equal("run", job_status(term_getjob(termbuf)))})
Bram Moolenaare06a28f2020-05-13 23:24:12 +02002540 sleep 100m
Bram Moolenaar631ebc42020-02-03 22:15:26 +01002541 " Check this doesn't crash
2542 call assert_equal(winnr(), winnr('j'))
2543 call assert_equal(winnr(), winnr('k'))
2544 call assert_equal(winnr(), winnr('h'))
2545 call assert_equal(winnr(), winnr('l'))
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002546
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002547 " Cannot quit while job is running
2548 call assert_fails('call feedkeys("\<C-W>:quit\<CR>", "xt")', 'E948:')
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002549
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002550 " Cannot enter Terminal-Normal mode. (TODO: but it works...)
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002551 call feedkeys("xxx\<C-W>N", 'xt')
2552 call assert_fails('call feedkeys("gf", "xt")', 'E863:')
2553 call feedkeys("a\<C-U>", 'xt')
2554
Bram Moolenaar3f65c662020-05-27 23:15:16 +02002555 " Cannot escape from terminal window
2556 call assert_fails('tab drop xxx', 'E863:')
2557
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002558 " Cannot open a second one.
2559 let termbuf2 = term_start(&shell, #{hidden: 1})
2560 call assert_fails('call popup_create(termbuf2, #{})', 'E861:')
2561 call term_sendkeys(termbuf2, "exit\<CR>")
2562
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002563 " Exiting shell closes popup window
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002564 call feedkeys("exit\<CR>", 'xt')
2565 " Wait for shell to exit
Bram Moolenaarb2b218d2020-06-22 20:22:19 +02002566 call WaitForAssert({-> assert_equal("dead", job_status(term_getjob(termbuf)))})
Bram Moolenaar373c6512020-06-22 19:24:23 +02002567
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002568 call feedkeys(":quit\<CR>", 'xt')
2569 call assert_equal(origwin, win_getid())
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002570endfunc
2571
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002572func Test_popupwin_close_prevwin()
2573 CheckFeature terminal
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002574 call Popupwin_close_prevwin()
2575endfunc
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002576
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002577def Popupwin_close_prevwin()
2578 assert_equal(1, winnr('$'))
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002579 split
2580 wincmd b
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002581 assert_equal(2, winnr())
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002582 let buf = term_start(&shell, #{hidden: 1})
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002583 popup_create(buf, {})
2584 TermWait(buf, 100)
2585 popup_clear(true)
2586 assert_equal(2, winnr())
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002587
2588 quit
2589 exe 'bwipe! ' .. buf
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002590enddef
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002591
Bram Moolenaar934470e2019-09-01 23:27:05 +02002592func Test_popupwin_with_buffer_and_filter()
2593 new Xwithfilter
2594 call setline(1, range(100))
2595 let bufnr = bufnr()
2596 hide
2597
2598 func BufferFilter(win, key)
2599 if a:key == 'G'
2600 " recursive use of "G" does not cause problems.
2601 call win_execute(a:win, 'normal! G')
2602 return 1
2603 endif
2604 return 0
2605 endfunc
2606
2607 let winid = popup_create(bufnr, #{maxheight: 5, filter: 'BufferFilter'})
2608 call assert_equal(1, popup_getpos(winid).firstline)
2609 redraw
2610 call feedkeys("G", 'xt')
2611 call assert_equal(99, popup_getpos(winid).firstline)
2612
2613 call popup_close(winid)
2614 exe 'bwipe! ' .. bufnr
2615endfunc
2616
Bram Moolenaare296e312019-07-03 23:20:18 +02002617func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002618 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002619 \ maxwidth: 40,
2620 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02002621 \ })
2622 for top in range(1, 20)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002623 eval winid->popup_setoptions(#{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02002624 redraw
2625 call assert_equal(19, popup_getpos(winid).width)
2626 endfor
2627 call popup_clear()
2628endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02002629
2630func Test_popupwin_buf_close()
2631 let buf = bufadd('Xtestbuf')
2632 call bufload(buf)
2633 call setbufline(buf, 1, ['just', 'some', 'lines'])
2634 let winid = popup_create(buf, {})
2635 redraw
2636 call assert_equal(3, popup_getpos(winid).height)
2637 let bufinfo = getbufinfo(buf)[0]
2638 call assert_equal(1, bufinfo.changed)
2639 call assert_equal(0, bufinfo.hidden)
2640 call assert_equal(0, bufinfo.listed)
2641 call assert_equal(1, bufinfo.loaded)
2642 call assert_equal([], bufinfo.windows)
2643 call assert_equal([winid], bufinfo.popups)
2644
2645 call popup_close(winid)
2646 call assert_equal({}, popup_getpos(winid))
2647 let bufinfo = getbufinfo(buf)[0]
2648 call assert_equal(1, bufinfo.changed)
2649 call assert_equal(1, bufinfo.hidden)
2650 call assert_equal(0, bufinfo.listed)
2651 call assert_equal(1, bufinfo.loaded)
2652 call assert_equal([], bufinfo.windows)
2653 call assert_equal([], bufinfo.popups)
2654 exe 'bwipe! ' .. buf
2655endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02002656
2657func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002658 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02002659
2660 let lines =<< trim END
2661 call setline(1, range(1, 10))
2662 hi ScrollThumb ctermbg=blue
2663 hi ScrollBar ctermbg=red
2664 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002665 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002666 \ maxwidth: 10,
2667 \ maxheight: 3,
2668 \ pos : 'topleft',
2669 \ col : a:col,
2670 \ line : a:line,
2671 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02002672 \ })
2673 endfunc
2674 call PopupMenu(['x'], 1, 1)
2675 call PopupMenu(['123456789|'], 1, 16)
2676 call PopupMenu(['123456789|' .. ' '], 7, 1)
2677 call PopupMenu([repeat('123456789|', 100)], 7, 16)
2678 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
2679 END
2680 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002681 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02002682 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
2683
2684 " close the menu popupwin.
2685 call term_sendkeys(buf, " ")
2686 call term_sendkeys(buf, " ")
2687 call term_sendkeys(buf, " ")
2688 call term_sendkeys(buf, " ")
2689 call term_sendkeys(buf, " ")
2690
2691 " clean up
2692 call StopVimInTerminal(buf)
2693 call delete('XtestPopupMenuMaxWidth')
2694endfunc
2695
Bram Moolenaara901a372019-07-13 16:38:50 +02002696func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002697 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02002698
2699 let lines =<< trim END
2700 call setline(1, range(1, 20))
2701 hi ScrollThumb ctermbg=blue
2702 hi ScrollBar ctermbg=red
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002703 eval ['one', 'two', 'three', 'four', 'five',
2704 \ 'six', 'seven', 'eight', 'nine']
2705 \ ->popup_menu(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002706 \ minwidth: 8,
2707 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02002708 \ })
2709 END
2710 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002711 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02002712
2713 call term_sendkeys(buf, "j")
2714 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
2715
2716 call term_sendkeys(buf, "jjj")
2717 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
2718
2719 " if the cursor is the bottom line, it stays at the bottom line.
2720 call term_sendkeys(buf, repeat("j", 20))
2721 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
2722
2723 call term_sendkeys(buf, "kk")
2724 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
2725
2726 call term_sendkeys(buf, "k")
2727 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
2728
2729 " if the cursor is in the top line, it stays in the top line.
2730 call term_sendkeys(buf, repeat("k", 20))
2731 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
2732
2733 " close the menu popupwin.
2734 call term_sendkeys(buf, " ")
2735
2736 " clean up
2737 call StopVimInTerminal(buf)
2738 call delete('XtestPopupMenuScroll')
2739endfunc
2740
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002741func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002742 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002743
2744 let lines =<< trim END
2745 function! MyFilter(winid, key) abort
2746 if a:key == "0"
2747 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
2748 return 1
2749 endif
2750 if a:key == "G"
2751 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
2752 return 1
2753 endif
2754 if a:key == "j"
2755 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
2756 return 1
2757 endif
2758 if a:key == "k"
2759 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
2760 return 1
2761 endif
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002762 if a:key == ':'
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002763 call popup_close(a:winid)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002764 return 0
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002765 endif
2766 return 0
2767 endfunction
2768 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
2769 \ maxheight : 3,
2770 \ filter : 'MyFilter'
2771 \ })
2772 END
2773 call writefile(lines, 'XtestPopupMenuFilter')
2774 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
2775
2776 call term_sendkeys(buf, "j")
2777 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
2778
2779 call term_sendkeys(buf, "k")
2780 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
2781
2782 call term_sendkeys(buf, "G")
2783 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
2784
2785 call term_sendkeys(buf, "0")
2786 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
2787
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002788 " check that when the popup is closed in the filter the screen is redrawn
2789 call term_sendkeys(buf, ":")
2790 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_5', {})
2791 call term_sendkeys(buf, "\<CR>")
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002792
2793 " clean up
2794 call StopVimInTerminal(buf)
2795 call delete('XtestPopupMenuFilter')
2796endfunc
2797
2798func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002799 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002800
2801 let winid = popup_create('some text', {})
2802 call assert_equal(0, popup_getoptions(winid).cursorline)
2803 call popup_close(winid)
2804
2805 let winid = popup_create('some text', #{ cursorline: 1, })
2806 call assert_equal(1, popup_getoptions(winid).cursorline)
2807 call popup_close(winid)
2808
2809 let winid = popup_create('some text', #{ cursorline: 0, })
2810 call assert_equal(0, popup_getoptions(winid).cursorline)
2811 call popup_close(winid)
2812
2813 let winid = popup_menu('some text', {})
2814 call assert_equal(1, popup_getoptions(winid).cursorline)
2815 call popup_close(winid)
2816
2817 let winid = popup_menu('some text', #{ cursorline: 1, })
2818 call assert_equal(1, popup_getoptions(winid).cursorline)
2819 call popup_close(winid)
2820
2821 let winid = popup_menu('some text', #{ cursorline: 0, })
2822 call assert_equal(0, popup_getoptions(winid).cursorline)
2823 call popup_close(winid)
2824
2825 " ---------
2826 " Pattern 1
2827 " ---------
2828 let lines =<< trim END
2829 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
2830 END
2831 call writefile(lines, 'XtestPopupCursorLine')
2832 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2833 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
2834 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2835 call StopVimInTerminal(buf)
2836
2837 " ---------
2838 " Pattern 2
2839 " ---------
2840 let lines =<< trim END
2841 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
2842 END
2843 call writefile(lines, 'XtestPopupCursorLine')
2844 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2845 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
2846 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2847 call StopVimInTerminal(buf)
2848
2849 " ---------
2850 " Pattern 3
2851 " ---------
2852 let lines =<< trim END
2853 function! MyFilter(winid, key) abort
2854 if a:key == "j"
2855 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2856 return 1
2857 endif
2858 if a:key == 'x'
2859 call popup_close(a:winid)
2860 return 1
2861 endif
2862 return 0
2863 endfunction
2864 call popup_menu(['111', '222', '333'], #{
2865 \ cursorline : 0,
2866 \ maxheight : 2,
2867 \ filter : 'MyFilter',
2868 \ })
2869 END
2870 call writefile(lines, 'XtestPopupCursorLine')
2871 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2872 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
2873 call term_sendkeys(buf, "j")
2874 call term_sendkeys(buf, "j")
2875 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
2876 call term_sendkeys(buf, "x")
2877 call StopVimInTerminal(buf)
2878
2879 " ---------
2880 " Pattern 4
2881 " ---------
2882 let lines =<< trim END
2883 function! MyFilter(winid, key) abort
2884 if a:key == "j"
2885 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2886 return 1
2887 endif
2888 if a:key == 'x'
2889 call popup_close(a:winid)
2890 return 1
2891 endif
2892 return 0
2893 endfunction
2894 call popup_menu(['111', '222', '333'], #{
2895 \ cursorline : 1,
2896 \ maxheight : 2,
2897 \ filter : 'MyFilter',
2898 \ })
2899 END
2900 call writefile(lines, 'XtestPopupCursorLine')
2901 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2902 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
2903 call term_sendkeys(buf, "j")
2904 call term_sendkeys(buf, "j")
2905 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
2906 call term_sendkeys(buf, "x")
2907 call StopVimInTerminal(buf)
2908
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02002909 " ---------
2910 " Cursor in second line when creating the popup
2911 " ---------
2912 let lines =<< trim END
2913 let winid = popup_create(['111', '222', '333'], #{
2914 \ cursorline : 1,
2915 \ })
2916 call win_execute(winid, "2")
2917 END
2918 call writefile(lines, 'XtestPopupCursorLine')
2919 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2920 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_7', {})
2921 call StopVimInTerminal(buf)
2922
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002923 call delete('XtestPopupCursorLine')
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01002924
2925 " ---------
2926 " Use current buffer for popupmenu
2927 " ---------
2928 let lines =<< trim END
2929 call setline(1, ['one', 'two', 'three'])
2930 let winid = popup_create(bufnr('%'), #{
2931 \ cursorline : 1,
2932 \ })
2933 call win_execute(winid, "2")
2934 END
2935 call writefile(lines, 'XtestPopupCursorLine')
2936 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2937 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_8', {})
2938 call StopVimInTerminal(buf)
2939
2940 call delete('XtestPopupCursorLine')
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002941endfunc
2942
Bram Moolenaarf914a332019-07-20 15:09:56 +02002943func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002944 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002945 CheckFeature quickfix
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002946
Bram Moolenaarf914a332019-07-20 15:09:56 +02002947 call writefile([
2948 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
2949 \ "another\tXtagfile\t/^this is another",
2950 \ "theword\tXtagfile\t/^theword"],
2951 \ 'Xtags')
2952 call writefile(range(1,20)
2953 \ + ['theword is here']
2954 \ + range(22, 27)
2955 \ + ['this is another place']
2956 \ + range(29, 40),
2957 \ "Xtagfile")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002958 call writefile(range(1,10)
2959 \ + ['searched word is here']
2960 \ + range(12, 20),
2961 \ "Xheader.h")
Bram Moolenaarf914a332019-07-20 15:09:56 +02002962 let lines =<< trim END
2963 set tags=Xtags
2964 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002965 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002966 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002967 \ 'three',
2968 \ 'four',
2969 \ 'five',
2970 \ 'six',
2971 \ 'seven',
2972 \ 'find theword somewhere',
2973 \ 'nine',
2974 \ 'this is another word',
2975 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02002976 set previewpopup=height:4,width:40
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002977 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02002978 END
2979 call writefile(lines, 'XtestPreviewPopup')
2980 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
2981
2982 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
2983 call term_sendkeys(buf, ":\<CR>")
2984 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
2985
2986 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2987 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
2988
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002989 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002990 call term_sendkeys(buf, ":\<CR>")
2991 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
2992
2993 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2994 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
2995
Bram Moolenaar799439a2020-02-11 21:44:17 +01002996 call term_sendkeys(buf, ":silent cd ..\<CR>:\<CR>")
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002997 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar799439a2020-02-11 21:44:17 +01002998 call term_sendkeys(buf, ":silent cd testdir\<CR>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002999
3000 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02003001 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003002 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
3003
3004 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
3005 call term_sendkeys(buf, ":\<CR>")
3006 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
3007
3008 call term_sendkeys(buf, ":pclose\<CR>")
3009 call term_sendkeys(buf, ":psearch searched\<CR>")
3010 call term_sendkeys(buf, ":\<CR>")
3011 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003012
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01003013 call term_sendkeys(buf, "\<C-W>p")
3014 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_9', {})
3015
3016 call term_sendkeys(buf, ":call win_execute(popup_findpreview(), 'call popup_clear()')\<CR>")
3017 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_10', {})
3018
Bram Moolenaarf914a332019-07-20 15:09:56 +02003019 call StopVimInTerminal(buf)
3020 call delete('Xtags')
3021 call delete('Xtagfile')
3022 call delete('XtestPreviewPopup')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003023 call delete('Xheader.h')
Bram Moolenaarf914a332019-07-20 15:09:56 +02003024endfunc
3025
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003026func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003027 let lines =<< trim END
3028 set completeopt+=preview,popup
3029 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02003030 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003031
3032 func CompleteFuncDict(findstart, base)
3033 if a:findstart
3034 if col('.') > 10
3035 return col('.') - 10
3036 endif
3037 return 0
3038 endif
3039
3040 return {
3041 \ 'words': [
3042 \ {
3043 \ 'word': 'aword',
3044 \ 'abbr': 'wrd',
3045 \ 'menu': 'extra text',
3046 \ 'info': 'words are cool',
3047 \ 'kind': 'W',
3048 \ 'user_data': 'test'
3049 \ },
3050 \ {
3051 \ 'word': 'anotherword',
3052 \ 'abbr': 'anotwrd',
3053 \ 'menu': 'extra text',
3054 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
3055 \ 'kind': 'W',
3056 \ 'user_data': 'notest'
3057 \ },
3058 \ {
3059 \ 'word': 'noinfo',
3060 \ 'abbr': 'noawrd',
3061 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02003062 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003063 \ 'kind': 'W',
3064 \ 'user_data': 'notest'
3065 \ },
3066 \ {
3067 \ 'word': 'thatword',
3068 \ 'abbr': 'thatwrd',
3069 \ 'menu': 'extra text',
3070 \ 'info': 'that word is cool',
3071 \ 'kind': 'W',
3072 \ 'user_data': 'notest'
3073 \ },
3074 \ ]
3075 \ }
3076 endfunc
3077 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003078 func ChangeColor()
3079 let id = popup_findinfo()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003080 eval id->popup_setoptions(#{highlight: 'InfoPopup'})
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003081 endfunc
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003082
3083 func InfoHidden()
3084 set completepopup=height:4,border:off,align:menu
3085 set completeopt-=popup completeopt+=popuphidden
3086 au CompleteChanged * call HandleChange()
3087 endfunc
3088
3089 let s:counter = 0
3090 func HandleChange()
3091 let s:counter += 1
3092 let selected = complete_info(['selected']).selected
3093 if selected <= 0
3094 " First time: do nothing, info remains hidden
3095 return
3096 endif
3097 if selected == 1
3098 " Second time: show info right away
3099 let id = popup_findinfo()
3100 if id
3101 call popup_settext(id, 'immediate info ' .. s:counter)
3102 call popup_show(id)
3103 endif
3104 else
3105 " Third time: show info after a short delay
3106 call timer_start(100, 'ShowInfo')
3107 endif
3108 endfunc
3109
3110 func ShowInfo(...)
3111 let id = popup_findinfo()
3112 if id
3113 call popup_settext(id, 'async info ' .. s:counter)
3114 call popup_show(id)
3115 endif
3116 endfunc
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003117 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003118 return lines
3119endfunc
3120
3121func Test_popupmenu_info_border()
3122 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003123 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003124
3125 let lines = Get_popupmenu_lines()
3126 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003127 call writefile(lines, 'XtestInfoPopup')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003128
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003129 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003130 call TermWait(buf, 25)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003131
3132 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3133 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
3134
3135 call term_sendkeys(buf, "\<C-N>")
3136 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
3137
3138 call term_sendkeys(buf, "\<C-N>")
3139 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
3140
3141 call term_sendkeys(buf, "\<C-N>\<C-N>")
3142 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
3143
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02003144 " info on the left with scrollbar
3145 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
3146 call term_sendkeys(buf, "\<C-N>\<C-N>")
3147 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
3148
Bram Moolenaar202c3f72019-11-21 12:12:35 +01003149 " Test that the popupmenu's scrollbar and infopopup do not overlap
3150 call term_sendkeys(buf, "\<Esc>")
3151 call term_sendkeys(buf, ":set pumheight=3\<CR>")
3152 call term_sendkeys(buf, "cc\<C-X>\<C-U>")
3153 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_6', {})
3154
Bram Moolenaarca7c0782020-01-14 20:42:48 +01003155 " Hide the info popup, cycle trough buffers, make sure it didn't get
3156 " deleted.
3157 call term_sendkeys(buf, "\<Esc>")
3158 call term_sendkeys(buf, ":set hidden\<CR>")
3159 call term_sendkeys(buf, ":bn\<CR>")
3160 call term_sendkeys(buf, ":bn\<CR>")
3161 call term_sendkeys(buf, "otest text test text\<C-X>\<C-U>")
3162 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_7', {})
3163
Bram Moolenaar447bfba2020-07-18 16:07:16 +02003164 " Test that when the option is changed the popup changes.
3165 call term_sendkeys(buf, "\<Esc>")
3166 call term_sendkeys(buf, ":set completepopup=border:off\<CR>")
3167 call term_sendkeys(buf, "a\<C-X>\<C-U>")
3168 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_8', {})
3169
Bram Moolenaar6d585f42020-07-26 22:20:54 +02003170 call term_sendkeys(buf, " \<Esc>")
3171 call term_sendkeys(buf, ":set completepopup+=width:10\<CR>")
3172 call term_sendkeys(buf, "a\<C-X>\<C-U>")
3173 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_9', {})
3174
Bram Moolenaarca7c0782020-01-14 20:42:48 +01003175 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003176 call StopVimInTerminal(buf)
3177 call delete('XtestInfoPopup')
3178endfunc
3179
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003180func Test_popupmenu_info_noborder()
3181 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003182 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003183
3184 let lines = Get_popupmenu_lines()
3185 call add(lines, 'set completepopup=height:4,border:off')
3186 call writefile(lines, 'XtestInfoPopupNb')
3187
3188 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003189 call TermWait(buf, 25)
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003190
3191 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3192 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
3193
3194 call StopVimInTerminal(buf)
3195 call delete('XtestInfoPopupNb')
3196endfunc
3197
Bram Moolenaar258cef52019-08-21 17:29:29 +02003198func Test_popupmenu_info_align_menu()
3199 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003200 CheckFeature quickfix
Bram Moolenaar258cef52019-08-21 17:29:29 +02003201
3202 let lines = Get_popupmenu_lines()
3203 call add(lines, 'set completepopup=height:4,border:off,align:menu')
3204 call writefile(lines, 'XtestInfoPopupNb')
3205
3206 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003207 call TermWait(buf, 25)
Bram Moolenaar258cef52019-08-21 17:29:29 +02003208
3209 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3210 call term_sendkeys(buf, "\<C-N>")
3211 call term_sendkeys(buf, "\<C-N>")
3212 call term_sendkeys(buf, "\<C-N>")
3213 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
3214
3215 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
3216 call term_sendkeys(buf, "\<C-N>")
3217 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
3218
3219 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003220 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02003221 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
3222 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
3223 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
3224
3225 call StopVimInTerminal(buf)
3226 call delete('XtestInfoPopupNb')
3227endfunc
3228
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003229func Test_popupmenu_info_hidden()
3230 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003231 CheckFeature quickfix
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003232
3233 let lines = Get_popupmenu_lines()
3234 call add(lines, 'call InfoHidden()')
3235 call writefile(lines, 'XtestInfoPopupHidden')
3236
3237 let buf = RunVimInTerminal('-S XtestInfoPopupHidden', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003238 call TermWait(buf, 25)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003239
3240 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3241 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_1', {})
3242
3243 call term_sendkeys(buf, "\<C-N>")
3244 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_2', {})
3245
3246 call term_sendkeys(buf, "\<C-N>")
3247 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_3', {})
3248
3249 call term_sendkeys(buf, "\<Esc>")
3250 call StopVimInTerminal(buf)
3251 call delete('XtestInfoPopupHidden')
3252endfunc
3253
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003254func Test_popupmenu_info_too_wide()
3255 CheckScreendump
3256 CheckFeature quickfix
3257
3258 let lines =<< trim END
3259 call setline(1, range(10))
3260
3261 set completeopt+=preview,popup
3262 set completepopup=align:menu
3263 set omnifunc=OmniFunc
3264 hi InfoPopup ctermbg=lightgrey
3265
3266 func OmniFunc(findstart, base)
3267 if a:findstart
3268 return 0
3269 endif
3270
3271 let menuText = 'some long text to make sure the menu takes up all of the width of the window'
3272 return #{
3273 \ words: [
3274 \ #{
3275 \ word: 'scrap',
3276 \ menu: menuText,
3277 \ info: "other words are\ncooler than this and some more text\nto make wrap",
3278 \ },
3279 \ #{
3280 \ word: 'scappier',
3281 \ menu: menuText,
3282 \ info: 'words are cool',
3283 \ },
3284 \ #{
3285 \ word: 'scrappier2',
3286 \ menu: menuText,
3287 \ info: 'words are cool',
3288 \ },
3289 \ ]
3290 \ }
3291 endfunc
3292 END
3293
3294 call writefile(lines, 'XtestInfoPopupWide')
3295 let buf = RunVimInTerminal('-S XtestInfoPopupWide', #{rows: 8})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003296 call TermWait(buf, 25)
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003297
3298 call term_sendkeys(buf, "Ascr\<C-X>\<C-O>")
3299 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_wide_1', {})
3300
3301 call term_sendkeys(buf, "\<Esc>")
3302 call StopVimInTerminal(buf)
3303 call delete('XtestInfoPopupWide')
3304endfunc
3305
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003306func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02003307 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003308 call popup_clear()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003309 let winid = 'nothing wrong'->popup_notification({})
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003310 call assert_equal(bufnr, winbufnr(winid))
3311 call popup_clear()
3312endfunc
3313
Bram Moolenaar1824f452019-10-02 23:06:46 +02003314func Test_popupwin_getoptions_tablocal()
3315 topleft split
3316 let win1 = popup_create('nothing', #{maxheight: 8})
3317 let win2 = popup_create('something', #{maxheight: 10})
3318 let win3 = popup_create('something', #{maxheight: 15})
3319 call assert_equal(8, popup_getoptions(win1).maxheight)
3320 call assert_equal(10, popup_getoptions(win2).maxheight)
3321 call assert_equal(15, popup_getoptions(win3).maxheight)
3322 call popup_clear()
3323 quit
3324endfunc
3325
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02003326func Test_popupwin_cancel()
3327 let win1 = popup_create('one', #{line: 5, filter: {... -> 0}})
3328 let win2 = popup_create('two', #{line: 10, filter: {... -> 0}})
3329 let win3 = popup_create('three', #{line: 15, filter: {... -> 0}})
3330 call assert_equal(5, popup_getpos(win1).line)
3331 call assert_equal(10, popup_getpos(win2).line)
3332 call assert_equal(15, popup_getpos(win3).line)
3333 " TODO: this also works without patch 8.1.2110
3334 call feedkeys("\<C-C>", 'xt')
3335 call assert_equal(5, popup_getpos(win1).line)
3336 call assert_equal(10, popup_getpos(win2).line)
3337 call assert_equal({}, popup_getpos(win3))
3338 call feedkeys("\<C-C>", 'xt')
3339 call assert_equal(5, popup_getpos(win1).line)
3340 call assert_equal({}, popup_getpos(win2))
3341 call assert_equal({}, popup_getpos(win3))
3342 call feedkeys("\<C-C>", 'xt')
3343 call assert_equal({}, popup_getpos(win1))
3344 call assert_equal({}, popup_getpos(win2))
3345 call assert_equal({}, popup_getpos(win3))
3346endfunc
3347
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003348func Test_popupwin_filter_redraw()
3349 " Create two popups with a filter that closes the popup when typing "0".
3350 " Both popups should close, even though the redraw also calls
3351 " popup_reset_handled()
3352
3353 func CloseFilter(winid, key)
3354 if a:key == '0'
3355 call popup_close(a:winid)
3356 redraw
3357 endif
3358 return 0 " pass the key
3359 endfunc
3360
3361 let id1 = popup_create('first one', #{
3362 \ line: 1,
3363 \ col: 1,
3364 \ filter: 'CloseFilter',
3365 \ })
3366 let id2 = popup_create('second one', #{
3367 \ line: 9,
3368 \ col: 1,
3369 \ filter: 'CloseFilter',
3370 \ })
3371 call assert_equal(1, popup_getpos(id1).line)
3372 call assert_equal(9, popup_getpos(id2).line)
3373
3374 call feedkeys('0', 'xt')
3375 call assert_equal({}, popup_getpos(id1))
3376 call assert_equal({}, popup_getpos(id2))
3377
3378 call popup_clear()
3379 delfunc CloseFilter
3380endfunc
3381
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003382func Test_popupwin_double_width()
3383 CheckScreendump
3384
3385 let lines =<< trim END
3386 call setline(1, 'x你好世界你好世你好世界你好')
3387 call setline(2, '你好世界你好世你好世界你好')
3388 call setline(3, 'x你好世界你好世你好世界你好')
3389 call popup_create('你好,世界 - 你好,世界xxxxx', #{line: 1, col: 3, maxwidth: 14})
3390 END
3391 call writefile(lines, 'XtestPopupWide')
3392
3393 let buf = RunVimInTerminal('-S XtestPopupWide', #{rows: 10})
3394 call VerifyScreenDump(buf, 'Test_popupwin_doublewidth_1', {})
3395
3396 call StopVimInTerminal(buf)
3397 call delete('XtestPopupWide')
3398endfunc
3399
3400func Test_popupwin_sign()
3401 CheckScreendump
3402
3403 let lines =<< trim END
3404 call setline(1, range(10))
3405 call sign_define('Current', {
3406 \ 'text': '>>',
3407 \ 'texthl': 'WarningMsg',
3408 \ 'linehl': 'Error',
3409 \ })
3410 call sign_define('Other', {
3411 \ 'text': '#!',
3412 \ 'texthl': 'Error',
3413 \ 'linehl': 'Search',
3414 \ })
3415 let winid = popup_create(['hello', 'bright', 'world'], {
3416 \ 'minwidth': 20,
3417 \ })
3418 call setwinvar(winid, "&signcolumn", "yes")
3419 let winbufnr = winbufnr(winid)
3420
3421 " add sign to current buffer, shows
3422 call sign_place(1, 'Selected', 'Current', bufnr('%'), {'lnum': 1})
3423 " add sign to current buffer, does not show
3424 call sign_place(2, 'PopUpSelected', 'Other', bufnr('%'), {'lnum': 2})
3425
3426 " add sign to popup buffer, shows
3427 call sign_place(3, 'PopUpSelected', 'Other', winbufnr, {'lnum': 1})
3428 " add sign to popup buffer, does not show
3429 call sign_place(4, 'Selected', 'Current', winbufnr, {'lnum': 2})
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02003430
3431 func SetOptions()
3432 call setwinvar(g:winid, '&number', 1)
3433 call setwinvar(g:winid, '&foldcolumn', 2)
3434 call popup_settext(g:winid, 'a longer line to check the width')
3435 endfunc
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003436 END
3437 call writefile(lines, 'XtestPopupSign')
3438
3439 let buf = RunVimInTerminal('-S XtestPopupSign', #{rows: 10})
3440 call VerifyScreenDump(buf, 'Test_popupwin_sign_1', {})
3441
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02003442 " set more options to check the width is adjusted
3443 call term_sendkeys(buf, ":call SetOptions()\<CR>")
3444 call VerifyScreenDump(buf, 'Test_popupwin_sign_2', {})
3445
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003446 call StopVimInTerminal(buf)
3447 call delete('XtestPopupSign')
3448endfunc
3449
Bram Moolenaar99ebf222019-12-10 23:44:48 +01003450func Test_popupwin_bufnr()
3451 let popwin = popup_create(['blah'], #{})
3452 let popbuf = winbufnr(popwin)
3453 split asdfasdf
3454 let newbuf = bufnr()
3455 call assert_true(newbuf > popbuf, 'New buffer number is higher')
3456 call assert_equal(newbuf, bufnr('$'))
3457 call popup_clear()
3458 let popwin = popup_create(['blah'], #{})
3459 " reuses previous buffer number
3460 call assert_equal(popbuf, winbufnr(popwin))
3461 call assert_equal(newbuf, bufnr('$'))
3462
3463 call popup_clear()
3464 bwipe!
3465endfunc
3466
Bram Moolenaarec084d32020-02-28 22:44:47 +01003467func Test_popupwin_filter_input_multibyte()
3468 func MyPopupFilter(winid, c)
3469 let g:bytes = range(a:c->strlen())->map({i -> char2nr(a:c[i])})
3470 return 0
3471 endfunc
3472 let winid = popup_create('', #{mapping: 0, filter: 'MyPopupFilter'})
3473
3474 " UTF-8: E3 80 80, including K_SPECIAL(0x80)
3475 call feedkeys("\u3000", 'xt')
3476 call assert_equal([0xe3, 0x80, 0x80], g:bytes)
3477
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01003478 " UTF-8: E3 80 9B, including CSI(0x9B)
3479 call feedkeys("\u301b", 'xt')
3480 call assert_equal([0xe3, 0x80, 0x9b], g:bytes)
Bram Moolenaarec084d32020-02-28 22:44:47 +01003481
Bram Moolenaar20298ce2020-06-19 21:46:52 +02003482 if has('unix')
3483 " with modifyOtherKeys <M-S-a> does not include a modifier sequence
Bram Moolenaarb326edf2020-06-20 15:03:38 +02003484 if has('gui_running')
3485 call feedkeys("\x9b\xfc\x08A", 'Lx!')
3486 else
3487 call feedkeys("\<Esc>[27;4;65~", 'Lx!')
3488 endif
Bram Moolenaar20298ce2020-06-19 21:46:52 +02003489 call assert_equal([0xc3, 0x81], g:bytes)
3490 endif
3491
Bram Moolenaarec084d32020-02-28 22:44:47 +01003492 call popup_clear()
3493 delfunc MyPopupFilter
3494 unlet g:bytes
3495endfunc
3496
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003497func Test_popupwin_filter_close_ctrl_c()
3498 CheckScreendump
3499
3500 let lines =<< trim END
3501 vsplit
3502 set laststatus=2
3503 set statusline=%!Statusline()
3504
3505 function Statusline() abort
3506 return '%<%f %h%m%r%=%-14.(%l,%c%V%) %P'
3507 endfunction
3508
3509 call popup_create('test test test test...', {'filter': {-> 0}})
3510 END
3511 call writefile(lines, 'XtestPopupCtrlC')
3512
3513 let buf = RunVimInTerminal('-S XtestPopupCtrlC', #{rows: 10})
3514
3515 call term_sendkeys(buf, "\<C-C>")
3516 call VerifyScreenDump(buf, 'Test_popupwin_ctrl_c', {})
3517
3518 call StopVimInTerminal(buf)
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003519 call delete('XtestPopupCtrlC')
3520endfunc
3521
3522func Test_popupwin_filter_close_wrong_name()
3523 CheckScreendump
3524
3525 let lines =<< trim END
3526 call popup_create('one two three...', {'filter': 'NoSuchFunc'})
3527 END
3528 call writefile(lines, 'XtestPopupWrongName')
3529
3530 let buf = RunVimInTerminal('-S XtestPopupWrongName', #{rows: 10})
3531
3532 call term_sendkeys(buf, "j")
3533 call VerifyScreenDump(buf, 'Test_popupwin_wrong_name', {})
3534
3535 call StopVimInTerminal(buf)
3536 call delete('XtestPopupWrongName')
3537endfunc
3538
3539func Test_popupwin_filter_close_three_errors()
3540 CheckScreendump
3541
3542 let lines =<< trim END
3543 set cmdheight=2
3544 call popup_create('one two three...', {'filter': 'filter'})
3545 END
3546 call writefile(lines, 'XtestPopupThreeErrors')
3547
3548 let buf = RunVimInTerminal('-S XtestPopupThreeErrors', #{rows: 10})
3549
3550 call term_sendkeys(buf, "jj")
3551 call VerifyScreenDump(buf, 'Test_popupwin_three_errors_1', {})
3552 call term_sendkeys(buf, "j")
3553 call VerifyScreenDump(buf, 'Test_popupwin_three_errors_2', {})
3554
3555 call StopVimInTerminal(buf)
3556 call delete('XtestPopupThreeErrors')
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003557endfunc
3558
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003559func Test_popupwin_atcursor_far_right()
3560 new
3561
3562 " this was getting stuck
3563 set signcolumn=yes
3564 call setline(1, repeat('=', &columns))
3565 normal! ggg$
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003566 let winid = popup_atcursor(repeat('x', 500), #{moved: 'any', border: []})
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003567
Bram Moolenaar6d585f42020-07-26 22:20:54 +02003568 " 'signcolumn' was getting reset
3569 call setwinvar(winid, '&signcolumn', 'yes')
3570 call popup_setoptions(winid, {'zindex': 1000})
3571 call assert_equal('yes', getwinvar(winid, '&signcolumn'))
3572
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003573 call popup_close(winid)
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003574 bwipe!
3575 set signcolumn&
3576endfunc
3577
Bram Moolenaar0f1563f2020-03-20 21:15:51 +01003578func Test_popupwin_splitmove()
3579 vsplit
3580 let win2 = win_getid()
3581 let popup_winid = popup_dialog('hello', {})
3582 call assert_fails('call win_splitmove(popup_winid, win2)', 'E957:')
3583 call assert_fails('call win_splitmove(win2, popup_winid)', 'E957:')
3584
3585 call popup_clear()
3586 bwipe
3587endfunc
3588
3589
Bram Moolenaar331bafd2019-07-20 17:46:05 +02003590" vim: shiftwidth=2 sts=2