blob: 1524e7856321105869c5f9c4cef763e1fd439855 [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 Moolenaar99ca9c42020-09-22 21:55:41 +0200519func Test_popup_firstline_cursorline()
520 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
521 \ maxheight: 2,
522 \ firstline: 3,
523 \ cursorline: 1,
524 \ })
525 call assert_equal(3, popup_getoptions(winid).firstline)
526 call assert_equal(3, getwininfo(winid)[0].topline)
527 call assert_equal(3, getcurpos(winid)[1])
528
529 call popup_close(winid)
530endfunc
531
Bram Moolenaara112f2d2019-09-01 17:38:09 +0200532func Test_popup_noscrolloff()
533 set scrolloff=5
534 let winid = popup_create(['xxx']->repeat(50), #{
535 \ maxheight: 5,
536 \ firstline: 11,
537 \ })
538 redraw
539 call assert_equal(11, popup_getoptions(winid).firstline)
540 call assert_equal(11, popup_getpos(winid).firstline)
541
542 call popup_setoptions(winid, #{firstline: 0})
543 call win_execute(winid, "normal! \<c-y>")
544 call assert_equal(0, popup_getoptions(winid).firstline)
545 call assert_equal(10, popup_getpos(winid).firstline)
546
547 call popup_close(winid)
548endfunc
549
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200550func Test_popup_drag()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200551 CheckScreendump
552
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200553 " create a popup that covers the command line
554 let lines =<< trim END
555 call setline(1, range(1, 20))
Bram Moolenaar356375f2019-08-24 14:46:29 +0200556 split
557 vsplit
558 $wincmd w
559 vsplit
560 1wincmd w
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200561 let winid = popup_create(['1111', '222222', '33333'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200562 \ drag: 1,
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200563 \ resize: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200564 \ border: [],
565 \ line: &lines - 4,
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200566 \ })
567 func Dragit()
568 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
569 endfunc
570 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
Bram Moolenaar356375f2019-08-24 14:46:29 +0200571 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2 - 20)<CR>
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200572 func Resize()
573 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
574 endfunc
Bram Moolenaar356375f2019-08-24 14:46:29 +0200575 map <silent> <F5> :call test_setmouse(6, 21)<CR>
576 map <silent> <F6> :call test_setmouse(7, 25)<CR>
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200577 END
578 call writefile(lines, 'XtestPopupDrag')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200579 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200580 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
581
582 call term_sendkeys(buf, ":call Dragit()\<CR>")
583 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
584
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200585 call term_sendkeys(buf, ":call Resize()\<CR>")
586 call VerifyScreenDump(buf, 'Test_popupwin_drag_03', {})
587
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200588 " clean up
589 call StopVimInTerminal(buf)
590 call delete('XtestPopupDrag')
591endfunc
592
Bram Moolenaar35910f22020-07-12 19:24:10 +0200593func Test_popup_drag_termwin()
594 CheckUnix
595 CheckScreendump
596 CheckFeature terminal
597
598 " create a popup that covers the terminal window
599 let lines =<< trim END
Bram Moolenaar452143c2020-07-15 17:38:21 +0200600 set foldmethod=marker
601 call setline(1, range(100))
602 for nr in range(7)
603 call setline(nr * 12 + 1, "fold {{{")
Bram Moolenaar9d489562020-07-30 20:08:50 +0200604 call setline(nr * 12 + 11, "end }}}")
Bram Moolenaar452143c2020-07-15 17:38:21 +0200605 endfor
606 %foldclose
Bram Moolenaar35910f22020-07-12 19:24:10 +0200607 set shell=/bin/sh noruler
Bram Moolenaara4dc6f92020-07-12 19:52:36 +0200608 let $PS1 = 'vim> '
Bram Moolenaar452143c2020-07-15 17:38:21 +0200609 terminal ++rows=4
Bram Moolenaar35910f22020-07-12 19:24:10 +0200610 $wincmd w
611 let winid = popup_create(['1111', '2222'], #{
612 \ drag: 1,
613 \ resize: 1,
614 \ border: [],
615 \ line: 3,
616 \ })
Bram Moolenaar452143c2020-07-15 17:38:21 +0200617 func DragitLeft()
Bram Moolenaar35910f22020-07-12 19:24:10 +0200618 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
619 endfunc
Bram Moolenaar452143c2020-07-15 17:38:21 +0200620 func DragitDown()
621 call feedkeys("\<F4>\<LeftMouse>\<F5>\<LeftDrag>\<LeftRelease>", "xt")
622 endfunc
623 func DragitDownLeft()
624 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
625 endfunc
Bram Moolenaar35910f22020-07-12 19:24:10 +0200626 map <silent> <F3> :call test_setmouse(3, &columns / 2)<CR>
627 map <silent> <F4> :call test_setmouse(3, &columns / 2 - 20)<CR>
Bram Moolenaar452143c2020-07-15 17:38:21 +0200628 map <silent> <F5> :call test_setmouse(12, &columns / 2)<CR>
629 map <silent> <F6> :call test_setmouse(12, &columns / 2 - 20)<CR>
Bram Moolenaar35910f22020-07-12 19:24:10 +0200630 END
631 call writefile(lines, 'XtestPopupTerm')
Bram Moolenaar452143c2020-07-15 17:38:21 +0200632 let buf = RunVimInTerminal('-S XtestPopupTerm', #{rows: 16})
Bram Moolenaar35910f22020-07-12 19:24:10 +0200633 call VerifyScreenDump(buf, 'Test_popupwin_term_01', {})
634
Bram Moolenaar452143c2020-07-15 17:38:21 +0200635 call term_sendkeys(buf, ":call DragitLeft()\<CR>")
Bram Moolenaar35910f22020-07-12 19:24:10 +0200636 call VerifyScreenDump(buf, 'Test_popupwin_term_02', {})
637
Bram Moolenaar452143c2020-07-15 17:38:21 +0200638 call term_sendkeys(buf, ":call DragitDown()\<CR>")
639 call VerifyScreenDump(buf, 'Test_popupwin_term_03', {})
640
641 call term_sendkeys(buf, ":call DragitDownLeft()\<CR>")
642 call VerifyScreenDump(buf, 'Test_popupwin_term_04', {})
643
Bram Moolenaar35910f22020-07-12 19:24:10 +0200644 " clean up
645 call StopVimInTerminal(buf)
646 call delete('XtestPopupTerm')
647endfunc
648
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200649func Test_popup_close_with_mouse()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200650 CheckScreendump
651
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200652 let lines =<< trim END
653 call setline(1, range(1, 20))
654 " With border, can click on X
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200655 let winid = popup_create('foobar', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200656 \ close: 'button',
657 \ border: [],
658 \ line: 1,
659 \ col: 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200660 \ })
661 func CloseMsg(id, result)
662 echomsg 'Popup closed with ' .. a:result
663 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200664 let winid = popup_create('notification', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200665 \ close: 'click',
666 \ line: 3,
667 \ col: 15,
668 \ callback: 'CloseMsg',
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200669 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200670 let winid = popup_create('no border here', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200671 \ close: 'button',
672 \ line: 5,
673 \ col: 3,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200674 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200675 let winid = popup_create('only padding', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200676 \ close: 'button',
677 \ padding: [],
678 \ line: 5,
679 \ col: 23,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200680 \ })
681 func CloseWithX()
682 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
683 endfunc
684 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
685 func CloseWithClick()
686 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
687 endfunc
688 map <silent> <F4> :call test_setmouse(3, 17)<CR>
Bram Moolenaarf6396232019-08-24 19:36:00 +0200689 func CreateWithMenuFilter()
690 let winid = popup_create('barfoo', #{
691 \ close: 'button',
692 \ filter: 'popup_filter_menu',
693 \ border: [],
694 \ line: 1,
695 \ col: 40,
696 \ })
697 endfunc
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200698 END
699 call writefile(lines, 'XtestPopupClose')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200700 let buf = RunVimInTerminal('-S XtestPopupClose', #{rows: 10})
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200701 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
702
703 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
704 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
705
706 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
707 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
708
Bram Moolenaarf6396232019-08-24 19:36:00 +0200709 call term_sendkeys(buf, ":call CreateWithMenuFilter()\<CR>")
710 call VerifyScreenDump(buf, 'Test_popupwin_close_04', {})
711
712 " We have to send the actual mouse code, feedkeys() would be caught the
713 " filter.
714 call term_sendkeys(buf, "\<Esc>[<0;47;1M")
715 call VerifyScreenDump(buf, 'Test_popupwin_close_05', {})
716
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200717 " clean up
718 call StopVimInTerminal(buf)
719 call delete('XtestPopupClose')
720endfunction
721
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200722func Test_popup_menu_wrap()
723 CheckScreendump
724
725 let lines =<< trim END
726 call setline(1, range(1, 20))
727 call popup_create([
728 \ 'one',
729 \ 'asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfas',
730 \ 'three',
731 \ 'four',
732 \ ], #{
733 \ pos: "botleft",
734 \ border: [],
735 \ padding: [0,1,0,1],
736 \ maxheight: 3,
737 \ cursorline: 1,
738 \ filter: 'popup_filter_menu',
739 \ })
740 END
741 call writefile(lines, 'XtestPopupWrap')
742 let buf = RunVimInTerminal('-S XtestPopupWrap', #{rows: 10})
743 call VerifyScreenDump(buf, 'Test_popupwin_wrap_1', {})
744
745 call term_sendkeys(buf, "jj")
746 call VerifyScreenDump(buf, 'Test_popupwin_wrap_2', {})
747
748 " clean up
749 call term_sendkeys(buf, "\<Esc>")
750 call StopVimInTerminal(buf)
751 call delete('XtestPopupWrap')
752endfunction
753
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200754func Test_popup_with_mask()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200755 CheckScreendump
756
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200757 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200758 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200759 hi PopupColor ctermbg=lightgrey
760 let winid = popup_create([
761 \ 'some text',
762 \ 'another line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200763 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200764 \ line: 1,
765 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100766 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200767 \ wrap: 0,
768 \ fixed: 1,
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +0200769 \ scrollbar: v:false,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200770 \ zindex: 90,
771 \ padding: [],
772 \ highlight: 'PopupColor',
773 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200774 call popup_create([
775 \ 'xxxxxxxxx',
776 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200777 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200778 \ line: 3,
779 \ col: 18,
780 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200781 let winidb = popup_create([
782 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200783 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200784 \ line: 7,
785 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100786 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200787 \ wrap: 0,
788 \ fixed: 1,
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +0200789 \ scrollbar: v:false,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200790 \ close: 'button',
791 \ zindex: 90,
792 \ padding: [],
793 \ border: [],
794 \ 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 +0200795 END
796 call writefile(lines, 'XtestPopupMask')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200797 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200798 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
799
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200800 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
801 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200802 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200803 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
804
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200805 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
806 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200807 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200808 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
809
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200810 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
811 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200812 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200813 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
814
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200815 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
816 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200817 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200818 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
819
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200820 " clean up
821 call StopVimInTerminal(buf)
822 call delete('XtestPopupMask')
823endfunc
824
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200825func Test_popup_select()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200826 CheckScreendump
827 CheckFeature clipboard_working
828
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200829 " create a popup with some text to be selected
830 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200831 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200832 call setline(1, range(1, 20))
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200833 let winid = popup_create(['the word', 'some more', 'several words here', 'invisible', '5', '6', '7'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200834 \ drag: 1,
835 \ border: [],
836 \ line: 3,
837 \ col: 10,
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200838 \ maxheight: 3,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200839 \ })
840 func Select1()
841 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
842 endfunc
843 map <silent> <F3> :call test_setmouse(4, 15)<CR>
844 map <silent> <F4> :call test_setmouse(6, 23)<CR>
845 END
846 call writefile(lines, 'XtestPopupSelect')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200847 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200848 call term_sendkeys(buf, ":call Select1()\<CR>")
849 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
850
851 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
852 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200853 " clean the command line, sometimes it still shows a command
854 call term_sendkeys(buf, ":\<esc>")
855
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200856 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
857
858 " clean up
859 call StopVimInTerminal(buf)
860 call delete('XtestPopupSelect')
861endfunc
862
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200863func Test_popup_in_tab()
864 " default popup is local to tab, not visible when in other tab
865 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200866 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200867 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200868 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200869 tabnew
870 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200871 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200872 quit
873 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200874
875 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200876 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200877 " buffer is gone now
878 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200879
880 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200881 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200882 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200883 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200884 tabnew
885 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200886 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200887 quit
888 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200889 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200890
891 " create popup in other tab
892 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200893 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200894 call assert_equal(0, popup_getpos(winid).visible)
895 call assert_equal(1, popup_getoptions(winid).tabpage)
896 quit
897 call assert_equal(1, popup_getpos(winid).visible)
898 call assert_equal(0, popup_getoptions(winid).tabpage)
899 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200900endfunc
901
902func Test_popup_valid_arguments()
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200903 call assert_equal(0, len(popup_list()))
904
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200905 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200906 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200907 let pos = popup_getpos(winid)
908 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200909 call assert_equal([winid], popup_list())
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200910 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200911
912 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200913 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200914 let pos = popup_getpos(winid)
915 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200916 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200917
918 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200919 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200920 let pos = popup_getpos(winid)
921 let around = (&columns - pos.width) / 2
922 call assert_inrange(around - 1, around + 1, pos.col)
923 let around = (&lines - pos.height) / 2
924 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200925 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200926endfunc
927
928func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200929 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200930 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200931 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200932 call popup_clear()
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200933 call assert_fails('call popup_create(test_null_string(), {})', 'E450:')
934 call assert_fails('call popup_create(test_null_list(), {})', 'E450:')
935 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200936
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200937 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
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", #{col: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200940 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200941 call assert_fails('call popup_create("text", #{col: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200942 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200943 call assert_fails('call popup_create("text", #{col: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200944 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200945
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200946 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
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", #{line: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200949 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200950 call assert_fails('call popup_create("text", #{line: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200951 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200952 call assert_fails('call popup_create("text", #{line: "cursor+8x"})', 'E15:')
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", #{pos: "there"})', 'E475:')
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", #{padding: "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", #{border: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200960 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200961 call assert_fails('call popup_create("text", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200962 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +0200963 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
964 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200965 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200966 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200967
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200968 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200969 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200970 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200971 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200972 call assert_fails('call popup_create([#{text: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200973 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +0100974 call assert_fails('call popup_create([#{text: "text", props: range(3)}], {})', 'E715:')
975 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200976 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
977 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +0100978 call assert_fails('call popup_create("text", #{mask: range(5)})', 'E475:')
979 call popup_clear()
980 call popup_create("text", #{mask: [range(4)]})
981 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200982 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200983 call assert_fails('call popup_create("text", #{mapping: []})', 'E745:')
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200984 call popup_clear()
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200985 call assert_fails('call popup_create("text", #{tabpage : 4})', 'E997:')
986 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200987endfunc
988
Bram Moolenaareea16992019-05-31 17:34:48 +0200989func Test_win_execute_closing_curwin()
990 split
991 let winid = popup_create('some text', {})
Bram Moolenaare2e40752020-09-04 21:18:46 +0200992 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200993 call popup_clear()
Bram Moolenaar49540192019-12-11 19:34:54 +0100994
995 let winid = popup_create('some text', {})
Bram Moolenaare2e40752020-09-04 21:18:46 +0200996 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 +0100997 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200998endfunc
999
1000func Test_win_execute_not_allowed()
1001 let winid = popup_create('some text', {})
1002 call assert_fails('call win_execute(winid, "split")', 'E994:')
1003 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
1004 call assert_fails('call win_execute(winid, "close")', 'E994:')
1005 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +02001006 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001007 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
1008 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
1009 call assert_fails('call win_execute(winid, "next")', 'E994:')
1010 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01001011 call assert_fails('call win_execute(winid, "pedit filename")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001012 call assert_fails('call win_execute(winid, "buf")', 'E994:')
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01001013 call assert_fails('call win_execute(winid, "bnext")', 'E994:')
1014 call assert_fails('call win_execute(winid, "bprev")', 'E994:')
1015 call assert_fails('call win_execute(winid, "bfirst")', 'E994:')
1016 call assert_fails('call win_execute(winid, "blast")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001017 call assert_fails('call win_execute(winid, "edit")', 'E994:')
1018 call assert_fails('call win_execute(winid, "enew")', 'E994:')
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001019 call assert_fails('call win_execute(winid, "help")', 'E994:')
1020 call assert_fails('call win_execute(winid, "1only")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001021 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
1022 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
1023 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
1024 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001025 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +02001026endfunc
1027
Bram Moolenaar402502d2019-05-30 22:07:36 +02001028func Test_popup_with_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001029 CheckScreendump
1030
Bram Moolenaar402502d2019-05-30 22:07:36 +02001031 let lines =<< trim END
1032 call setline(1, range(1, 100))
1033 let winid = popup_create(
1034 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001035 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001036 END
1037 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001038 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001039 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
1040
1041 " clean up
1042 call StopVimInTerminal(buf)
1043 call delete('XtestPopup')
1044endfunc
1045
1046func Test_popup_without_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001047 CheckScreendump
1048
Bram Moolenaar402502d2019-05-30 22:07:36 +02001049 let lines =<< trim END
1050 call setline(1, range(1, 100))
1051 let winid = popup_create(
1052 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001053 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001054 END
1055 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001056 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001057 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
1058
1059 " clean up
1060 call StopVimInTerminal(buf)
1061 call delete('XtestPopup')
1062endfunc
1063
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001064func Test_popup_with_showbreak()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001065 CheckScreendump
1066
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001067 let lines =<< trim END
1068 set showbreak=>>\
1069 call setline(1, range(1, 20))
1070 let winid = popup_dialog(
Bram Moolenaar8ae54372019-09-15 18:11:16 +02001071 \ 'a long line here that wraps',
1072 \ #{filter: 'popup_filter_yesno',
1073 \ maxwidth: 12})
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001074 END
1075 call writefile(lines, 'XtestPopupShowbreak')
1076 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
1077 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
1078
1079 " clean up
1080 call term_sendkeys(buf, "y")
1081 call StopVimInTerminal(buf)
1082 call delete('XtestPopupShowbreak')
1083endfunc
1084
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001085func Test_popup_time()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001086 CheckFeature timers
1087
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001088 topleft vnew
1089 call setline(1, 'hello')
1090
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001091 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001092 \ line: 1,
1093 \ col: 1,
1094 \ minwidth: 20,
1095 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001096 \})
1097 redraw
1098 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1099 call assert_equal('world', line)
1100
Bram Moolenaarb4f06282019-07-12 21:07:54 +02001101 call assert_equal(winid, popup_locate(1, 1))
1102 call assert_equal(winid, popup_locate(1, 20))
1103 call assert_equal(0, popup_locate(1, 21))
1104 call assert_equal(0, popup_locate(2, 1))
1105
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001106 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +02001107 redraw
Bram Moolenaar196b4662019-09-06 21:34:30 +02001108 let line = join(map(range(1, 5), '1->screenstring(v:val)'), '')
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001109 call assert_equal('hello', line)
1110
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001111 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001112 \ line: &lines,
1113 \ col: 10,
1114 \ minwidth: 20,
1115 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001116 \})
1117 redraw
1118 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1119 call assert_match('.*on the command line.*', line)
1120
1121 sleep 700m
1122 redraw
1123 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1124 call assert_notmatch('.*on the command line.*', line)
1125
1126 bwipe!
1127endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001128
1129func Test_popup_hide()
1130 topleft vnew
1131 call setline(1, 'hello')
1132
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001133 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001134 \ line: 1,
1135 \ col: 1,
1136 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001137 \})
1138 redraw
1139 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1140 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001141 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001142 " buffer is still listed and active
1143 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001144
1145 call popup_hide(winid)
1146 redraw
1147 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1148 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001149 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001150 " buffer is still listed but hidden
Bram Moolenaarca7c0782020-01-14 20:42:48 +01001151 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001152
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001153 eval winid->popup_show()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001154 redraw
1155 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1156 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001157 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001158
1159
1160 call popup_close(winid)
1161 redraw
1162 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1163 call assert_equal('hello', line)
1164
1165 " error is given for existing non-popup window
1166 call assert_fails('call popup_hide(win_getid())', 'E993:')
1167
1168 " no error non-existing window
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001169 eval 1234234->popup_hide()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001170 call popup_show(41234234)
1171
1172 bwipe!
1173endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001174
1175func Test_popup_move()
1176 topleft vnew
1177 call setline(1, 'hello')
1178
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001179 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001180 \ line: 1,
1181 \ col: 1,
1182 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001183 \})
1184 redraw
1185 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1186 call assert_equal('world ', line)
1187
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001188 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001189 redraw
1190 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1191 call assert_equal('hello ', line)
1192 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
1193 call assert_equal('~world', line)
1194
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001195 eval winid->popup_move(#{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001196 redraw
1197 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1198 call assert_equal('hworld', line)
1199
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001200 call assert_fails('call popup_move(winid, [])', 'E715:')
1201 call assert_fails('call popup_move(winid, test_null_dict())', 'E715:')
1202
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001203 call popup_close(winid)
1204
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001205 call assert_equal(0, popup_move(-1, {}))
1206
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001207 bwipe!
1208endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +02001209
Bram Moolenaar402502d2019-05-30 22:07:36 +02001210func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001211 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001212 \ line: 2,
1213 \ col: 3,
1214 \ minwidth: 10,
1215 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +02001216 \})
1217 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001218 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001219 call assert_equal(2, res.line)
1220 call assert_equal(3, res.col)
1221 call assert_equal(10, res.width)
1222 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001223 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001224
1225 call popup_close(winid)
1226endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001227
1228func Test_popup_width_longest()
1229 let tests = [
1230 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
1231 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
1232 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
1233 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
1234 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
1235 \ ]
1236
1237 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001238 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001239 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001240 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001241 call assert_equal(test[1], position.width)
1242 call popup_close(winid)
1243 endfor
1244endfunc
1245
1246func Test_popup_wraps()
1247 let tests = [
1248 \ ['nowrap', 6, 1],
1249 \ ['a line that wraps once', 12, 2],
1250 \ ['a line that wraps two times', 12, 3],
1251 \ ]
1252 for test in tests
1253 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001254 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001255 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001256 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001257 call assert_equal(test[1], position.width)
1258 call assert_equal(test[2], position.height)
1259
1260 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001261 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001262 endfor
1263endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001264
1265func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001266 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001267 \ line: 2,
1268 \ col: 3,
1269 \ minwidth: 10,
1270 \ minheight: 11,
1271 \ maxwidth: 20,
1272 \ maxheight: 21,
1273 \ zindex: 100,
1274 \ time: 5000,
1275 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001276 \})
1277 redraw
1278 let res = popup_getoptions(winid)
1279 call assert_equal(2, res.line)
1280 call assert_equal(3, res.col)
1281 call assert_equal(10, res.minwidth)
1282 call assert_equal(11, res.minheight)
1283 call assert_equal(20, res.maxwidth)
1284 call assert_equal(21, res.maxheight)
1285 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001286 call assert_equal(1, res.fixed)
Bram Moolenaarb8350ab2019-08-04 17:59:49 +02001287 call assert_equal(1, res.mapping)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001288 if has('timers')
1289 call assert_equal(5000, res.time)
1290 endif
1291 call popup_close(winid)
1292
1293 let winid = popup_create('hello', {})
1294 redraw
1295 let res = popup_getoptions(winid)
1296 call assert_equal(0, res.line)
1297 call assert_equal(0, res.col)
1298 call assert_equal(0, res.minwidth)
1299 call assert_equal(0, res.minheight)
1300 call assert_equal(0, res.maxwidth)
1301 call assert_equal(0, res.maxheight)
1302 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001303 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001304 if has('timers')
1305 call assert_equal(0, res.time)
1306 endif
1307 call popup_close(winid)
1308 call assert_equal({}, popup_getoptions(winid))
1309endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001310
1311func Test_popup_option_values()
1312 new
1313 " window-local
1314 setlocal number
1315 setlocal nowrap
1316 " buffer-local
1317 setlocal omnifunc=Something
1318 " global/buffer-local
1319 setlocal path=/there
1320 " global/window-local
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001321 setlocal statusline=2
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001322
1323 let winid = popup_create('hello', {})
1324 call assert_equal(0, getwinvar(winid, '&number'))
1325 call assert_equal(1, getwinvar(winid, '&wrap'))
1326 call assert_equal('', getwinvar(winid, '&omnifunc'))
1327 call assert_equal(&g:path, getwinvar(winid, '&path'))
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001328 call assert_equal(&g:statusline, getwinvar(winid, '&statusline'))
1329
1330 " 'scrolloff' is reset to zero
1331 call assert_equal(5, &scrolloff)
1332 call assert_equal(0, getwinvar(winid, '&scrolloff'))
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001333
1334 call popup_close(winid)
1335 bwipe
1336endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001337
1338func Test_popup_atcursor()
1339 topleft vnew
1340 call setline(1, [
1341 \ 'xxxxxxxxxxxxxxxxx',
1342 \ 'xxxxxxxxxxxxxxxxx',
1343 \ 'xxxxxxxxxxxxxxxxx',
1344 \])
1345
1346 call cursor(2, 2)
1347 redraw
1348 let winid = popup_atcursor('vim', {})
1349 redraw
1350 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1351 call assert_equal('xvimxxxxxxxxxxxxx', line)
1352 call popup_close(winid)
1353
1354 call cursor(3, 4)
1355 redraw
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001356 let winid = 'vim'->popup_atcursor({})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001357 redraw
1358 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
1359 call assert_equal('xxxvimxxxxxxxxxxx', line)
1360 call popup_close(winid)
1361
1362 call cursor(1, 1)
1363 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001364 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001365 \ line: 'cursor+2',
1366 \ col: 'cursor+1',
1367 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001368 redraw
1369 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1370 call assert_equal('xvimxxxxxxxxxxxxx', line)
1371 call popup_close(winid)
1372
1373 call cursor(3, 3)
1374 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001375 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001376 \ line: 'cursor-2',
1377 \ col: 'cursor-1',
1378 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001379 redraw
1380 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1381 call assert_equal('xvimxxxxxxxxxxxxx', line)
1382 call popup_close(winid)
1383
Bram Moolenaar402502d2019-05-30 22:07:36 +02001384 " just enough room above
1385 call cursor(3, 3)
1386 redraw
1387 let winid = popup_atcursor(['vim', 'is great'], {})
1388 redraw
1389 let pos = popup_getpos(winid)
1390 call assert_equal(1, pos.line)
1391 call popup_close(winid)
1392
1393 " not enough room above, popup goes below the cursor
1394 call cursor(3, 3)
1395 redraw
1396 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1397 redraw
1398 let pos = popup_getpos(winid)
1399 call assert_equal(4, pos.line)
1400 call popup_close(winid)
1401
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001402 " cursor in first line, popup in line 2
1403 call cursor(1, 1)
1404 redraw
1405 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1406 redraw
1407 let pos = popup_getpos(winid)
1408 call assert_equal(2, pos.line)
1409 call popup_close(winid)
1410
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001411 bwipe!
1412endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001413
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001414func Test_popup_atcursor_pos()
1415 CheckScreendump
1416
1417 let lines =<< trim END
1418 call setline(1, repeat([repeat('-', 60)], 15))
1419 set so=0
1420
1421 normal 9G3|r#
1422 let winid1 = popup_atcursor(['first', 'second'], #{
1423 \ moved: [0, 0, 0],
1424 \ })
1425 normal 9G21|r&
1426 let winid1 = popup_atcursor(['FIrsT', 'SEcoND'], #{
1427 \ pos: 'botright',
1428 \ moved: [0, 0, 0],
1429 \ })
1430 normal 3G27|r%
1431 let winid1 = popup_atcursor(['fiRSt', 'seCOnd'], #{
1432 \ pos: 'topleft',
1433 \ moved: [0, 0, 0],
1434 \ })
1435 normal 3G45|r@
1436 let winid1 = popup_atcursor(['First', 'SeconD'], #{
1437 \ pos: 'topright',
Bram Moolenaarb0992022020-01-30 14:55:42 +01001438 \ moved: range(3),
1439 \ mousemoved: range(3),
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001440 \ })
1441 END
1442 call writefile(lines, 'XtestPopupAtcursorPos')
1443 let buf = RunVimInTerminal('-S XtestPopupAtcursorPos', #{rows: 12})
1444 call VerifyScreenDump(buf, 'Test_popupwin_atcursor_pos', {})
1445
1446 " clean up
1447 call StopVimInTerminal(buf)
1448 call delete('XtestPopupAtcursorPos')
1449endfunc
1450
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001451func Test_popup_beval()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001452 CheckScreendump
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001453 CheckFeature balloon_eval_term
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001454
1455 let lines =<< trim END
1456 call setline(1, range(1, 20))
1457 call setline(5, 'here is some text to hover over')
1458 set balloonevalterm
1459 set balloonexpr=BalloonExpr()
1460 set balloondelay=100
1461 func BalloonExpr()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001462 let s:winid = [v:beval_text]->popup_beval({})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001463 return ''
1464 endfunc
1465 func Hover()
1466 call test_setmouse(5, 15)
1467 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1468 sleep 100m
1469 endfunc
1470 func MoveOntoPopup()
1471 call test_setmouse(4, 17)
1472 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1473 endfunc
1474 func MoveAway()
1475 call test_setmouse(5, 13)
1476 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1477 endfunc
1478 END
1479 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001480 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001481 call TermWait(buf, 50)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001482 call term_sendkeys(buf, 'j')
1483 call term_sendkeys(buf, ":call Hover()\<CR>")
1484 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1485
1486 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1487 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1488
1489 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1490 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1491
1492 " clean up
1493 call StopVimInTerminal(buf)
1494 call delete('XtestPopupBeval')
1495endfunc
1496
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001497func Test_popup_filter()
1498 new
1499 call setline(1, 'some text')
1500
1501 func MyPopupFilter(winid, c)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001502 if a:c == 'e' || a:c == "\<F9>"
1503 let g:eaten = a:c
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001504 return 1
1505 endif
1506 if a:c == '0'
1507 let g:ignored = '0'
1508 return 0
1509 endif
1510 if a:c == 'x'
1511 call popup_close(a:winid)
1512 return 1
1513 endif
1514 return 0
1515 endfunc
1516
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001517 let winid = 'something'->popup_create(#{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001518 redraw
1519
1520 " e is consumed by the filter
1521 call feedkeys('e', 'xt')
1522 call assert_equal('e', g:eaten)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001523 call feedkeys("\<F9>", 'xt')
1524 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001525
1526 " 0 is ignored by the filter
1527 normal $
1528 call assert_equal(9, getcurpos()[2])
1529 call feedkeys('0', 'xt')
1530 call assert_equal('0', g:ignored)
1531 call assert_equal(1, getcurpos()[2])
1532
1533 " x closes the popup
1534 call feedkeys('x', 'xt')
Bram Moolenaarec084d32020-02-28 22:44:47 +01001535 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001536 call assert_equal(-1, winbufnr(winid))
1537
1538 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001539 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001540endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001541
Bram Moolenaar4934ad02020-09-28 22:29:58 +02001542" this tests that the filter is not used for :normal command
Bram Moolenaar189832b2020-09-23 12:29:11 +02001543func Test_popup_filter_normal_cmd()
1544 CheckScreendump
1545
1546 let lines =<< trim END
Bram Moolenaar4934ad02020-09-28 22:29:58 +02001547 let text = range(1, 20)->map({_, v -> string(v)})
1548 let g:winid = popup_create(text, #{maxheight: 5, minwidth: 3, filter: 'invalidfilter'})
1549 call timer_start(0, {-> win_execute(g:winid, 'norm! 10Gzz')})
Bram Moolenaar189832b2020-09-23 12:29:11 +02001550 END
1551 call writefile(lines, 'XtestPopupNormal')
1552 let buf = RunVimInTerminal('-S XtestPopupNormal', #{rows: 10})
1553 call TermWait(buf, 100)
1554 call VerifyScreenDump(buf, 'Test_popupwin_normal_cmd', {})
1555
1556 call StopVimInTerminal(buf)
1557 call delete('XtestPopupNormal')
1558endfunc
1559
Bram Moolenaar10476522020-09-24 22:57:31 +02001560" test that cursor line highlight is updated after using win_execute()
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001561func Test_popup_filter_win_execute()
1562 CheckScreendump
1563
1564 let lines =<< trim END
Bram Moolenaar10476522020-09-24 22:57:31 +02001565 let lines = range(1, &lines * 2)->map({_, v -> string(v)})
1566 let g:id = popup_create(lines, #{
1567 \ minheight: &lines - 5,
1568 \ maxheight: &lines - 5,
1569 \ cursorline: 1,
1570 \ })
1571 redraw
1572 END
1573 call writefile(lines, 'XtestPopupWinExecute')
1574 let buf = RunVimInTerminal('-S XtestPopupWinExecute', #{rows: 14})
1575
1576 call term_sendkeys(buf, ":call win_execute(g:id, ['normal 17Gzz'])\<CR>")
1577 call term_sendkeys(buf, ":\<CR>")
1578
1579 call VerifyScreenDump(buf, 'Test_popupwin_win_execute_cursorline', {})
1580
1581 call StopVimInTerminal(buf)
1582 call delete('XtestPopupWinExecute')
1583endfunc
1584
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001585func Test_popup_set_firstline()
1586 CheckScreendump
1587
1588 let lines =<< trim END
1589 let lines = range(1, 50)->map({_, v -> string(v)})
1590 let g:id = popup_create(lines, #{
1591 \ minwidth: 20,
1592 \ maxwidth: 20,
1593 \ minheight: &lines - 5,
1594 \ maxheight: &lines - 5,
1595 \ cursorline: 1,
1596 \ })
1597 call popup_setoptions(g:id, #{firstline: 10})
1598 redraw
1599 END
1600 call writefile(lines, 'XtestPopupWinSetFirstline')
1601 let buf = RunVimInTerminal('-S XtestPopupWinSetFirstline', #{rows: 16})
1602
1603 call VerifyScreenDump(buf, 'Test_popupwin_set_firstline_1', {})
1604
1605 call term_sendkeys(buf, ":call popup_setoptions(g:id, #{firstline: 5})\<CR>")
1606 call term_sendkeys(buf, ":\<CR>")
1607 call VerifyScreenDump(buf, 'Test_popupwin_set_firstline_2', {})
1608
1609 call StopVimInTerminal(buf)
1610 call delete('XtestPopupWinSetFirstline')
1611endfunc
1612
Bram Moolenaar10476522020-09-24 22:57:31 +02001613" this tests that we don't get stuck with an error in "win_execute()"
1614func Test_popup_filter_win_execute_error()
1615 CheckScreendump
1616
1617 let lines =<< trim END
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001618 let g:winid = popup_create('some text', {'filter': 'invalidfilter'})
1619 call timer_start(0, {-> win_execute(g:winid, 'invalidCommand')})
1620 END
Bram Moolenaar10476522020-09-24 22:57:31 +02001621 call writefile(lines, 'XtestPopupWinExecuteError')
1622 let buf = RunVimInTerminal('-S XtestPopupWinExecuteError', #{rows: 10, wait_for_ruler: 0})
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001623
1624 call WaitFor({-> term_getline(buf, 9) =~ 'Not an editor command: invalidCommand'})
1625 call term_sendkeys(buf, "\<CR>")
1626 call WaitFor({-> term_getline(buf, 9) =~ 'Unknown function: invalidfilter'})
1627 call term_sendkeys(buf, "\<CR>")
1628 call WaitFor({-> term_getline(buf, 9) =~ 'Not allowed in a popup window'})
1629 call term_sendkeys(buf, "\<CR>")
1630 call term_sendkeys(buf, "\<CR>")
1631 call VerifyScreenDump(buf, 'Test_popupwin_win_execute', {})
1632
1633 call StopVimInTerminal(buf)
Bram Moolenaar10476522020-09-24 22:57:31 +02001634 call delete('XtestPopupWinExecuteError')
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001635endfunc
1636
Bram Moolenaara42d9452019-06-15 21:46:30 +02001637func ShowDialog(key, result)
1638 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001639 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001640 \ filter: 'popup_filter_yesno',
1641 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001642 \ })
1643 redraw
1644 call feedkeys(a:key, "xt")
1645 call assert_equal(winid, s:cb_winid)
1646 call assert_equal(a:result, s:cb_res)
1647endfunc
1648
1649func Test_popup_dialog()
1650 func QuitCallback(id, res)
1651 let s:cb_winid = a:id
1652 let s:cb_res = a:res
1653 endfunc
1654
1655 let winid = ShowDialog("y", 1)
1656 let winid = ShowDialog("Y", 1)
1657 let winid = ShowDialog("n", 0)
1658 let winid = ShowDialog("N", 0)
1659 let winid = ShowDialog("x", 0)
1660 let winid = ShowDialog("X", 0)
1661 let winid = ShowDialog("\<Esc>", 0)
1662 let winid = ShowDialog("\<C-C>", -1)
1663
1664 delfunc QuitCallback
1665endfunc
1666
Bram Moolenaara730e552019-06-16 19:05:31 +02001667func ShowMenu(key, result)
1668 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001669 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001670 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001671 \ })
1672 redraw
1673 call feedkeys(a:key, "xt")
1674 call assert_equal(winid, s:cb_winid)
1675 call assert_equal(a:result, s:cb_res)
1676endfunc
1677
1678func Test_popup_menu()
1679 func QuitCallback(id, res)
1680 let s:cb_winid = a:id
1681 let s:cb_res = a:res
1682 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001683 " mapping won't be used in popup
1684 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001685
1686 let winid = ShowMenu(" ", 1)
1687 let winid = ShowMenu("j \<CR>", 2)
1688 let winid = ShowMenu("JjK \<CR>", 2)
1689 let winid = ShowMenu("jjjjjj ", 3)
1690 let winid = ShowMenu("kkk ", 1)
1691 let winid = ShowMenu("x", -1)
1692 let winid = ShowMenu("X", -1)
1693 let winid = ShowMenu("\<Esc>", -1)
1694 let winid = ShowMenu("\<C-C>", -1)
1695
1696 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001697 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001698endfunc
1699
1700func Test_popup_menu_screenshot()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001701 CheckScreendump
Bram Moolenaara730e552019-06-16 19:05:31 +02001702
1703 let lines =<< trim END
1704 call setline(1, range(1, 20))
1705 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001706 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001707 func MenuDone(id, res)
1708 echomsg "selected " .. a:res
1709 endfunc
1710 END
1711 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001712 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001713 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1714
1715 call term_sendkeys(buf, "jj")
1716 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1717
1718 call term_sendkeys(buf, " ")
1719 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1720
1721 " clean up
1722 call StopVimInTerminal(buf)
1723 call delete('XtestPopupMenu')
1724endfunc
1725
Bram Moolenaarf914a332019-07-20 15:09:56 +02001726func Test_popup_menu_narrow()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001727 CheckScreendump
Bram Moolenaarf914a332019-07-20 15:09:56 +02001728
1729 let lines =<< trim END
1730 call setline(1, range(1, 20))
1731 hi PopupSelected ctermbg=green
1732 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1733 func MenuDone(id, res)
1734 echomsg "selected " .. a:res
1735 endfunc
1736 END
1737 call writefile(lines, 'XtestPopupNarrowMenu')
1738 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1739 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1740
1741 " clean up
1742 call term_sendkeys(buf, "x")
1743 call StopVimInTerminal(buf)
1744 call delete('XtestPopupNarrowMenu')
1745endfunc
1746
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001747func Test_popup_title()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001748 CheckScreendump
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001749
1750 " Create a popup without title or border, a line of padding will be added to
1751 " put the title on.
1752 let lines =<< trim END
1753 call setline(1, range(1, 20))
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001754 let winid = popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001755 END
1756 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001757 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001758 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1759
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001760 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxwidth: 20, title: 'a very long title that is not going to fit'})\<CR>")
1761 call term_sendkeys(buf, ":\<CR>")
1762 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_1', {})
1763
1764 call term_sendkeys(buf, ":call popup_setoptions(winid, #{border: []})\<CR>")
1765 call term_sendkeys(buf, ":\<CR>")
1766 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_2', {})
1767
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001768 " clean up
1769 call StopVimInTerminal(buf)
1770 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001771
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001772 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001773 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001774 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001775 call assert_equal('Another Title', popup_getoptions(winid).title)
1776
1777 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001778endfunc
1779
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001780func Test_popup_close_callback()
1781 func PopupDone(id, result)
1782 let g:result = a:result
1783 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001784 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001785 redraw
1786 call popup_close(winid, 'done')
1787 call assert_equal('done', g:result)
1788endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001789
1790func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001791 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001792 redraw
1793 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001794 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001795 call assert_equal(5, pos.height)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001796 call popup_close(winid)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001797
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001798 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001799 redraw
1800 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001801 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001802 call assert_equal(3, pos.height)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001803 call popup_close(winid)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001804endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001805
1806func Test_popup_never_behind()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001807 CheckScreendump
1808
Bram Moolenaar988c4332019-06-02 14:12:11 +02001809 " +-----------------------------+
1810 " | | |
1811 " | | |
1812 " | | |
1813 " | line1 |
1814 " |------------line2------------|
1815 " | line3 |
1816 " | line4 |
1817 " | |
1818 " | |
1819 " +-----------------------------+
1820 let lines =<< trim END
Bram Moolenaar988c4332019-06-02 14:12:11 +02001821 split
1822 vsplit
1823 let info_window1 = getwininfo()[0]
1824 let line = info_window1['height']
1825 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001826 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001827 \ line : line,
1828 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001829 \ })
1830 END
1831 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001832 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001833 call term_sendkeys(buf, "\<C-W>w")
1834 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1835
1836 " clean up
1837 call StopVimInTerminal(buf)
1838 call delete('XtestPopupBehind')
1839endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001840
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001841func s:VerifyPosition(p, msg, line, col, width, height)
1842 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1843 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1844 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1845 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001846endfunc
1847
1848func Test_popup_position_adjust()
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001849 " Anything placed past the last cell on the right of the screen is moved to
1850 " the left.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001851 "
1852 " When wrapping is disabled, we also shift to the left to display on the
1853 " screen, unless fixed is set.
1854
1855 " Entries for cases which don't vary based on wrapping.
1856 " Format is per tests described below
1857 let both_wrap_tests = [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001858 \ ['a', 5, &columns, 5, &columns, 1, 1],
1859 \ ['b', 5, &columns + 1, 5, &columns, 1, 1],
1860 \ ['c', 5, &columns - 1, 5, &columns - 1, 1, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001861 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001862 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1]]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001863
1864 " these test groups are dicts with:
1865 " - comment: something to identify the group of tests by
1866 " - options: dict of options to merge with the row/col in tests
1867 " - tests: list of cases. Each one is a list with elements:
1868 " - text
1869 " - row
1870 " - col
1871 " - expected row
1872 " - expected col
1873 " - expected width
1874 " - expected height
1875 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001876 \ #{
1877 \ comment: 'left-aligned with wrapping',
1878 \ options: #{
1879 \ wrap: 1,
1880 \ pos: 'botleft',
1881 \ },
1882 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001883 \ ['aa', 5, &columns, 4, &columns, 1, 2],
1884 \ ['bb', 5, &columns + 1, 4, &columns, 1, 2],
1885 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1886 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1887 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1888 \
1889 \ ['aaa', 5, &columns, 3, &columns, 1, 3],
1890 \ ['bbb', 5, &columns + 1, 3, &columns, 1, 3],
1891 \ ['ccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
1892 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1893 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1894 \
1895 \ ['aaaa', 5, &columns, 2, &columns, 1, 4],
1896 \ ['bbbb', 5, &columns + 1, 2, &columns, 1, 4],
1897 \ ['cccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001898 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
1899 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001900 \ ['eeee', 5, &columns - 4, 5, &columns - 4, 4, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001901 \ ],
1902 \ },
1903 \ #{
1904 \ comment: 'left aligned without wrapping',
1905 \ options: #{
1906 \ wrap: 0,
1907 \ pos: 'botleft',
1908 \ },
1909 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001910 \ ['aa', 5, &columns, 5, &columns - 1, 2, 1],
1911 \ ['bb', 5, &columns + 1, 5, &columns - 1, 2, 1],
1912 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1913 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1914 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1915 \
1916 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
1917 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1918 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1919 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1920 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1921 \
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001922 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
1923 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
1924 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
1925 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
1926 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1927 \ ],
1928 \ },
1929 \ #{
1930 \ comment: 'left aligned with fixed position',
1931 \ options: #{
1932 \ wrap: 0,
1933 \ fixed: 1,
1934 \ pos: 'botleft',
1935 \ },
1936 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001937 \ ['aa', 5, &columns, 5, &columns, 1, 1],
1938 \ ['bb', 5, &columns + 1, 5, &columns, 1, 1],
1939 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1940 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1941 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1942 \
1943 \ ['aaa', 5, &columns, 5, &columns, 1, 1],
1944 \ ['bbb', 5, &columns + 1, 5, &columns, 1, 1],
1945 \ ['ccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1946 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1947 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1948 \
1949 \ ['aaaa', 5, &columns, 5, &columns, 1, 1],
1950 \ ['bbbb', 5, &columns + 1, 5, &columns, 1, 1],
1951 \ ['cccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001952 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1953 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1954 \ ],
1955 \ },
1956 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001957
1958 for test_group in tests
1959 for test in test_group.tests
1960 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001961 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001962 \ line: line,
1963 \ col: col,
1964 \ }
1965 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001966
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001967 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001968
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001969 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001970 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
1971 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001972 endfor
1973 endfor
1974
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001975 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001976 %bwipe!
1977endfunc
1978
Bram Moolenaar3397f742019-06-02 18:40:06 +02001979func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001980 " width of screen
1981 let X = join(map(range(&columns), {->'X'}), '')
1982
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001983 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1984 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001985
1986 redraw
1987 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1988 call assert_equal(X, line)
1989
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001990 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001991 redraw
1992
1993 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001994 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
1995 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001996
1997 redraw
1998 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1999 call assert_equal(X, line)
2000
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002001 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002002 redraw
2003
2004 " Extend so > window width
2005 let X .= 'x'
2006
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002007 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
2008 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002009
2010 redraw
2011 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2012 call assert_equal(X[ : -2 ], line)
2013
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002014 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002015 redraw
2016
2017 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002018 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
2019 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002020
2021 redraw
2022 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2023 call assert_equal(X[ : -2 ], line)
2024
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002025 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002026 redraw
2027
2028 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002029 let p = popup_create(X,
2030 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
2031 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002032
2033 redraw
2034 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2035 let e_line = ' ' . X[ 1 : -2 ]
2036 call assert_equal(e_line, line)
2037
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002038 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002039 redraw
2040
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002041 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002042 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02002043endfunc
2044
2045func Test_popup_moved()
2046 new
2047 call test_override('char_avail', 1)
2048 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
2049
2050 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002051 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002052 redraw
2053 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002054 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002055 " trigger the check for last_cursormoved by going into insert mode
2056 call feedkeys("li\<Esc>", 'xt')
2057 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002058 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002059
2060 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002061 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002062 redraw
2063 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002064 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002065 call feedkeys("hi\<Esc>", 'xt')
2066 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002067 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002068
2069 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002070 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002071 redraw
2072 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002073 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002074 call feedkeys("li\<Esc>", 'xt')
2075 call assert_equal(1, popup_getpos(winid).visible)
2076 call feedkeys("ei\<Esc>", 'xt')
2077 call assert_equal(1, popup_getpos(winid).visible)
2078 call feedkeys("eli\<Esc>", 'xt')
2079 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002080 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002081
Bram Moolenaar17627312019-06-02 19:53:44 +02002082 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02002083 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02002084 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002085 redraw
2086 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002087 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002088 call feedkeys("eli\<Esc>", 'xt')
2089 call assert_equal(1, popup_getpos(winid).visible)
2090 call feedkeys("wi\<Esc>", 'xt')
2091 call assert_equal(1, popup_getpos(winid).visible)
2092 call feedkeys("Eli\<Esc>", 'xt')
2093 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002094 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002095
2096 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002097 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002098 redraw
2099 call assert_equal(1, popup_getpos(winid).visible)
2100 call feedkeys("eli\<Esc>", 'xt')
2101 call feedkeys("ei\<Esc>", 'xt')
2102 call assert_equal(1, popup_getpos(winid).visible)
2103 call feedkeys("eli\<Esc>", 'xt')
2104 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002105 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002106
2107 bwipe!
2108 call test_override('ALL', 0)
2109endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002110
2111func Test_notifications()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002112 CheckFeature timers
2113 CheckScreendump
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002114
Bram Moolenaar0fdddee2019-09-01 15:26:23 +02002115 let lines =<< trim END
2116 call setline(1, range(1, 20))
2117 hi Notification ctermbg=lightblue
2118 call popup_notification('first notification', {})
2119 END
2120 call writefile(lines, 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002121 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002122 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
2123
2124 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002125 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
2126 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002127 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
2128
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002129 " clean up
2130 call StopVimInTerminal(buf)
2131 call delete('XtestNotifications')
2132endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002133
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002134func Test_popup_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002135 CheckScreendump
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002136
2137 let lines =<< trim END
2138 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02002139 hi ScrollThumb ctermbg=blue
2140 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002141 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002142 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002143 \ minwidth: 8,
2144 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002145 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002146 func ScrollUp()
2147 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
2148 endfunc
2149 func ScrollDown()
2150 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
2151 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002152 func ClickTop()
2153 call feedkeys("\<F4>\<LeftMouse>", "xt")
2154 endfunc
2155 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002156 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002157 call feedkeys("\<F5>\<LeftMouse>", "xt")
2158 endfunc
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002159 func Popup_filter(winid, key)
2160 if a:key == 'j'
2161 let line = popup_getoptions(a:winid).firstline
2162 let nlines = line('$', a:winid)
2163 let newline = line < nlines ? (line + 1) : nlines
2164 call popup_setoptions(a:winid, #{firstline: newline})
2165 return v:true
2166 elseif a:key == 'x'
2167 call popup_close(a:winid)
2168 return v:true
2169 endif
2170 endfunc
2171
Bram Moolenaar6c542f72020-09-27 21:16:45 +02002172 def CreatePopup(text: list<string>)
2173 popup_create(text, #{
2174 \ minwidth: 30,
2175 \ maxwidth: 30,
2176 \ minheight: 4,
2177 \ maxheight: 4,
2178 \ firstline: 1,
2179 \ lastline: 4,
2180 \ wrap: true,
2181 \ scrollbar: true,
2182 \ mapping: false,
2183 \ filter: Popup_filter,
2184 \ })
2185 enddef
2186
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002187 func PopupScroll()
2188 call popup_clear()
2189 let text =<< trim END
2190 1
2191 2
2192 3
2193 4
2194 long line long line long line long line long line long line
2195 long line long line long line long line long line long line
2196 long line long line long line long line long line long line
2197 END
Bram Moolenaar6c542f72020-09-27 21:16:45 +02002198 call CreatePopup(text)
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002199 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002200 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002201 map <silent> <F4> :call test_setmouse(4, 42)<CR>
2202 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002203 END
2204 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002205 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002206 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
2207
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002208 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002209 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002210 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
2211
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002212 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002213 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002214 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
2215
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002216 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002217 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002218 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
2219
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02002220 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb', firstline: 5})\<CR>")
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002221 " this scrolls two lines (half the window height)
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002222 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
2223 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
2224
2225 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2226 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
2227
2228 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02002229 " wait a bit, otherwise it fails sometimes (double click recognized?)
2230 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002231 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2232 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
2233
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002234 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2235 sleep 100m
2236 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2237 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
2238
2239 call term_sendkeys(buf, ":call ClickBot()\<CR>")
2240 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
2241
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002242 " remove the minwidth and maxheight
2243 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxheight: 0, minwidth: 0})\<CR>")
Bram Moolenaar7e0f4622019-09-17 21:23:39 +02002244 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002245 call VerifyScreenDump(buf, 'Test_popupwin_scroll_10', {})
2246
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002247 " check size with non-wrapping lines
2248 call term_sendkeys(buf, ":call PopupScroll()\<CR>")
2249 call VerifyScreenDump(buf, 'Test_popupwin_scroll_11', {})
2250
2251 " check size with wrapping lines
2252 call term_sendkeys(buf, "j")
2253 call VerifyScreenDump(buf, 'Test_popupwin_scroll_12', {})
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002254
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002255 " clean up
Bram Moolenaar20298ce2020-06-19 21:46:52 +02002256 call term_sendkeys(buf, "x")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002257 call StopVimInTerminal(buf)
2258 call delete('XtestPopupScroll')
2259endfunc
2260
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02002261func Test_popup_too_high_scrollbar()
2262 CheckScreendump
2263
2264 let lines =<< trim END
2265 call setline(1, range(1, 20)->map({i, v -> repeat(v, 10)}))
2266 set scrolloff=0
2267 func ShowPopup()
2268 let winid = popup_atcursor(['one', 'two', 'three', 'four', 'five',
2269 \ 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve'], #{
2270 \ minwidth: 8,
2271 \ border: [],
2272 \ })
2273 endfunc
2274 normal 3G$
2275 call ShowPopup()
2276 END
2277 call writefile(lines, 'XtestPopupToohigh')
2278 let buf = RunVimInTerminal('-S XtestPopupToohigh', #{rows: 10})
2279 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_1', {})
2280
2281 call term_sendkeys(buf, ":call popup_clear()\<CR>")
2282 call term_sendkeys(buf, "8G$")
2283 call term_sendkeys(buf, ":call ShowPopup()\<CR>")
2284 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_2', {})
2285
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02002286 call term_sendkeys(buf, ":call popup_clear()\<CR>")
2287 call term_sendkeys(buf, "gg$")
2288 call term_sendkeys(buf, ":call ShowPopup()\<CR>")
2289 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_3', {})
2290
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02002291 " clean up
2292 call StopVimInTerminal(buf)
2293 call delete('XtestPopupToohigh')
2294endfunc
2295
Bram Moolenaar437a7462019-07-05 20:17:22 +02002296func Test_popup_fitting_scrollbar()
2297 " this was causing a crash, divide by zero
2298 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002299 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002300 \ scrollbar: 1,
2301 \ maxwidth: 10,
2302 \ maxheight: 5,
2303 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02002304 redraw
2305 call popup_clear()
2306endfunc
2307
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002308func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002309 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002310
2311 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002312 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002313 let p = popup_create('test', opts)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002314 eval p->popup_settext('this is a text')
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002315 END
2316
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002317 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002318 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002319 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
2320
2321 " Setting to empty string clears it
2322 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
2323 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
2324
2325 " Setting a list
2326 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2327 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2328
2329 " Shrinking with a list
2330 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
2331 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
2332
2333 " Growing with a list
2334 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2335 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2336
2337 " Empty list clears
2338 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
2339 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
2340
2341 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002342 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002343 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
2344
Bram Moolenaarb0992022020-01-30 14:55:42 +01002345 " range() (doesn't work)
2346 call term_sendkeys(buf, ":call popup_settext(p, range(4, 8))\<CR>")
2347 call VerifyScreenDump(buf, 'Test_popup_settext_07', {})
2348
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002349 " clean up
2350 call StopVimInTerminal(buf)
2351 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002352endfunc
2353
Bram Moolenaar1f42f5a2020-09-03 18:52:24 +02002354func Test_popup_settext_getline()
2355 let id = popup_create('', #{ tabpage: 0 })
2356 call popup_settext(id, ['a','b'])
2357 call assert_equal(2, line('$', id)) " OK :)
2358 call popup_close(id)
2359
2360 let id = popup_create('', #{ tabpage: -1 })
2361 call popup_settext(id, ['a','b'])
2362 call assert_equal(2, line('$', id)) " Fails :(
2363 call popup_close(id)
2364endfunc
2365
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02002366func Test_popup_settext_null()
2367 let id = popup_create('', #{ tabpage: 0 })
2368 call popup_settext(id, test_null_list())
2369 call popup_close(id)
2370
2371 let id = popup_create('', #{ tabpage: 0 })
2372 call popup_settext(id, test_null_string())
2373 call popup_close(id)
2374endfunc
2375
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002376func Test_popup_hidden()
2377 new
2378
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002379 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002380 redraw
2381 call assert_equal(0, popup_getpos(winid).visible)
2382 call popup_close(winid)
2383
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002384 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002385 redraw
2386 call assert_equal(0, popup_getpos(winid).visible)
2387 call popup_close(winid)
2388
2389 func QuitCallback(id, res)
2390 let s:cb_winid = a:id
2391 let s:cb_res = a:res
2392 endfunc
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002393 let winid = 'make a choice'->popup_dialog(#{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002394 \ filter: 'popup_filter_yesno',
2395 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002396 \ })
2397 redraw
2398 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02002399 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
2400 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002401 exe "normal anot used by filter\<Esc>"
2402 call assert_equal('not used by filter', getline(1))
2403
2404 call popup_show(winid)
2405 call feedkeys('y', "xt")
2406 call assert_equal(1, s:cb_res)
2407
2408 bwipe!
2409 delfunc QuitCallback
2410endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02002411
2412" Test options not checked elsewhere
2413func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002414 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002415 let options = popup_getoptions(winid)
2416 call assert_equal(1, options.wrap)
2417 call assert_equal(0, options.drag)
2418 call assert_equal('Beautiful', options.highlight)
2419
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002420 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002421 let options = popup_getoptions(winid)
2422 call assert_equal(0, options.wrap)
2423 call assert_equal(1, options.drag)
2424 call assert_equal('Another', options.highlight)
2425
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002426 call assert_fails('call popup_setoptions(winid, [])', 'E715:')
2427 call assert_fails('call popup_setoptions(winid, test_null_dict())', 'E715:')
2428
Bram Moolenaarae943152019-06-16 22:54:14 +02002429 call popup_close(winid)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002430 call assert_equal(0, popup_setoptions(winid, options.wrap))
Bram Moolenaarae943152019-06-16 22:54:14 +02002431endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002432
2433func Test_popupwin_garbage_collect()
2434 func MyPopupFilter(x, winid, c)
2435 " NOP
2436 endfunc
2437
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002438 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002439 call test_garbagecollect_now()
2440 redraw
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02002441 " Must not crash caused by invalid memory access
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002442 call feedkeys('j', 'xt')
2443 call assert_true(v:true)
2444
2445 call popup_close(winid)
2446 delfunc MyPopupFilter
2447endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002448
Bram Moolenaar581ba392019-09-03 22:08:33 +02002449func Test_popupwin_filter_mode()
2450 func MyPopupFilter(winid, c)
2451 let s:typed = a:c
2452 if a:c == ':' || a:c == "\r" || a:c == 'v'
2453 " can start cmdline mode, get out, and start/stop Visual mode
2454 return 0
2455 endif
2456 return 1
2457 endfunc
2458
2459 " Normal, Visual and Insert mode
2460 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'nvi'})
2461 redraw
2462 call feedkeys('x', 'xt')
2463 call assert_equal('x', s:typed)
2464
2465 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2466 call assert_equal(':', s:typed)
2467 call assert_equal('foo', g:foo)
2468
2469 let @x = 'something'
2470 call feedkeys('v$"xy', 'xt')
2471 call assert_equal('y', s:typed)
2472 call assert_equal('something', @x) " yank command is filtered out
2473 call feedkeys('v', 'xt') " end Visual mode
2474
2475 call popup_close(winid)
2476
2477 " only Normal mode
2478 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'n'})
2479 redraw
2480 call feedkeys('x', 'xt')
2481 call assert_equal('x', s:typed)
2482
2483 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2484 call assert_equal(':', s:typed)
2485 call assert_equal('foo', g:foo)
2486
2487 let @x = 'something'
2488 call feedkeys('v$"xy', 'xt')
2489 call assert_equal('v', s:typed)
2490 call assert_notequal('something', @x)
2491
2492 call popup_close(winid)
2493
2494 " default: all modes
2495 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
2496 redraw
2497 call feedkeys('x', 'xt')
2498 call assert_equal('x', s:typed)
2499
2500 let g:foo = 'bar'
2501 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2502 call assert_equal("\r", s:typed)
2503 call assert_equal('bar', g:foo)
2504
2505 let @x = 'something'
2506 call feedkeys('v$"xy', 'xt')
2507 call assert_equal('y', s:typed)
2508 call assert_equal('something', @x) " yank command is filtered out
2509 call feedkeys('v', 'xt') " end Visual mode
2510
2511 call popup_close(winid)
2512 delfunc MyPopupFilter
2513endfunc
2514
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002515func Test_popupwin_filter_mouse()
2516 func MyPopupFilter(winid, c)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002517 let g:got_mousepos = getmousepos()
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002518 return 0
2519 endfunc
2520
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002521 call setline(1, ['.'->repeat(25)]->repeat(10))
2522 let winid = popup_create(['short', 'long line that will wrap', 'other'], #{
2523 \ line: 2,
2524 \ col: 4,
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002525 \ maxwidth: 12,
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002526 \ padding: [],
2527 \ border: [],
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002528 \ filter: 'MyPopupFilter',
2529 \ })
2530 redraw
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002531 " 123456789012345678901
2532 " 1 .....................
2533 " 2 ...+--------------+..
2534 " 3 ...| |..
2535 " 4 ...| short |..
2536 " 5 ...| long line th |..
2537 " 6 ...| at will wrap |..
2538 " 7 ...| other |..
2539 " 8 ...| |..
2540 " 9 ...+--------------+..
2541 " 10 .....................
2542 let tests = []
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002543
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002544 func AddItemOutsidePopup(tests, row, col)
2545 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2546 \ screenrow: a:row, screencol: a:col,
2547 \ winid: win_getid(), winrow: a:row, wincol: a:col,
2548 \ line: a:row, column: a:col,
2549 \ }})
2550 endfunc
2551 func AddItemInPopupBorder(tests, winid, row, col)
2552 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2553 \ screenrow: a:row, screencol: a:col,
2554 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2555 \ line: 0, column: 0,
2556 \ }})
2557 endfunc
2558 func AddItemInPopupText(tests, winid, row, col, textline, textcol)
2559 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2560 \ screenrow: a:row, screencol: a:col,
2561 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2562 \ line: a:textline, column: a:textcol,
2563 \ }})
2564 endfunc
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002565
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002566 " above and below popup
2567 for c in range(1, 21)
2568 call AddItemOutsidePopup(tests, 1, c)
2569 call AddItemOutsidePopup(tests, 10, c)
2570 endfor
2571 " left and right of popup
2572 for r in range(1, 10)
2573 call AddItemOutsidePopup(tests, r, 3)
2574 call AddItemOutsidePopup(tests, r, 20)
2575 endfor
2576 " top and bottom in popup
2577 for c in range(4, 19)
2578 call AddItemInPopupBorder(tests, winid, 2, c)
2579 call AddItemInPopupBorder(tests, winid, 3, c)
2580 call AddItemInPopupBorder(tests, winid, 8, c)
2581 call AddItemInPopupBorder(tests, winid, 9, c)
2582 endfor
2583 " left and right margin in popup
2584 for r in range(2, 9)
2585 call AddItemInPopupBorder(tests, winid, r, 4)
2586 call AddItemInPopupBorder(tests, winid, r, 5)
2587 call AddItemInPopupBorder(tests, winid, r, 18)
2588 call AddItemInPopupBorder(tests, winid, r, 19)
2589 endfor
2590 " text "short"
2591 call AddItemInPopupText(tests, winid, 4, 6, 1, 1)
2592 call AddItemInPopupText(tests, winid, 4, 10, 1, 5)
2593 call AddItemInPopupText(tests, winid, 4, 11, 1, 6)
2594 call AddItemInPopupText(tests, winid, 4, 17, 1, 6)
2595 " text "long line th"
2596 call AddItemInPopupText(tests, winid, 5, 6, 2, 1)
2597 call AddItemInPopupText(tests, winid, 5, 10, 2, 5)
2598 call AddItemInPopupText(tests, winid, 5, 17, 2, 12)
2599 " text "at will wrap"
2600 call AddItemInPopupText(tests, winid, 6, 6, 2, 13)
2601 call AddItemInPopupText(tests, winid, 6, 10, 2, 17)
2602 call AddItemInPopupText(tests, winid, 6, 17, 2, 24)
2603 " text "other"
2604 call AddItemInPopupText(tests, winid, 7, 6, 3, 1)
2605 call AddItemInPopupText(tests, winid, 7, 10, 3, 5)
2606 call AddItemInPopupText(tests, winid, 7, 11, 3, 6)
2607 call AddItemInPopupText(tests, winid, 7, 17, 3, 6)
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002608
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002609 for item in tests
2610 call test_setmouse(item.clickrow, item.clickcol)
2611 call feedkeys("\<LeftMouse>", 'xt')
2612 call assert_equal(item.result, g:got_mousepos)
2613 endfor
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002614
2615 call popup_close(winid)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002616 enew!
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002617 delfunc MyPopupFilter
2618endfunc
2619
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002620func Test_popupwin_with_buffer()
2621 call writefile(['some text', 'in a buffer'], 'XsomeFile')
2622 let buf = bufadd('XsomeFile')
2623 call assert_equal(0, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002624
2625 setlocal number
2626 call setbufvar(buf, "&wrapmargin", 13)
2627
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002628 let winid = popup_create(buf, {})
2629 call assert_notequal(0, winid)
2630 let pos = popup_getpos(winid)
2631 call assert_equal(2, pos.height)
2632 call assert_equal(1, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002633
2634 " window-local option is set to default, buffer-local is not
2635 call assert_equal(0, getwinvar(winid, '&number'))
2636 call assert_equal(13, getbufvar(buf, '&wrapmargin'))
2637
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002638 call popup_close(winid)
2639 call assert_equal({}, popup_getpos(winid))
2640 call assert_equal(1, bufloaded(buf))
2641 exe 'bwipe! ' .. buf
Bram Moolenaar46451042019-08-24 15:50:46 +02002642 setlocal nonumber
Bram Moolenaar7866b872019-07-01 22:21:01 +02002643
2644 edit test_popupwin.vim
2645 let winid = popup_create(bufnr(''), {})
2646 redraw
2647 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002648 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002649endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02002650
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002651func Test_popupwin_terminal_buffer()
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002652 CheckFeature terminal
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002653 CheckUnix
Bram Moolenaare06a28f2020-05-13 23:24:12 +02002654 " Starting a terminal to run a shell in is considered flaky.
2655 let g:test_is_flaky = 1
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002656
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002657 let origwin = win_getid()
Bram Moolenaar349f6092020-10-06 20:46:49 +02002658
2659 " open help window to test that :help below fails
2660 help
2661
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002662 let termbuf = term_start(&shell, #{hidden: 1})
2663 let winid = popup_create(termbuf, #{minwidth: 40, minheight: 10})
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002664 " Wait for shell to start
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002665 call WaitForAssert({-> assert_equal("run", job_status(term_getjob(termbuf)))})
Bram Moolenaare06a28f2020-05-13 23:24:12 +02002666 sleep 100m
Bram Moolenaar631ebc42020-02-03 22:15:26 +01002667 " Check this doesn't crash
2668 call assert_equal(winnr(), winnr('j'))
2669 call assert_equal(winnr(), winnr('k'))
2670 call assert_equal(winnr(), winnr('h'))
2671 call assert_equal(winnr(), winnr('l'))
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002672
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002673 " Cannot quit while job is running
2674 call assert_fails('call feedkeys("\<C-W>:quit\<CR>", "xt")', 'E948:')
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002675
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002676 " Cannot enter Terminal-Normal mode. (TODO: but it works...)
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002677 call feedkeys("xxx\<C-W>N", 'xt')
2678 call assert_fails('call feedkeys("gf", "xt")', 'E863:')
2679 call feedkeys("a\<C-U>", 'xt')
2680
Bram Moolenaar3f65c662020-05-27 23:15:16 +02002681 " Cannot escape from terminal window
2682 call assert_fails('tab drop xxx', 'E863:')
Bram Moolenaar349f6092020-10-06 20:46:49 +02002683 call assert_fails('help', 'E994:')
Bram Moolenaar3f65c662020-05-27 23:15:16 +02002684
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002685 " Cannot open a second one.
2686 let termbuf2 = term_start(&shell, #{hidden: 1})
2687 call assert_fails('call popup_create(termbuf2, #{})', 'E861:')
2688 call term_sendkeys(termbuf2, "exit\<CR>")
2689
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002690 " Exiting shell closes popup window
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002691 call feedkeys("exit\<CR>", 'xt')
2692 " Wait for shell to exit
Bram Moolenaarb2b218d2020-06-22 20:22:19 +02002693 call WaitForAssert({-> assert_equal("dead", job_status(term_getjob(termbuf)))})
Bram Moolenaar373c6512020-06-22 19:24:23 +02002694
Bram Moolenaar349f6092020-10-06 20:46:49 +02002695 helpclose
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002696 call feedkeys(":quit\<CR>", 'xt')
2697 call assert_equal(origwin, win_getid())
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002698endfunc
2699
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002700func Test_popupwin_close_prevwin()
2701 CheckFeature terminal
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002702 call Popupwin_close_prevwin()
2703endfunc
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002704
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002705def Popupwin_close_prevwin()
2706 assert_equal(1, winnr('$'))
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002707 split
2708 wincmd b
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002709 assert_equal(2, winnr())
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +02002710 var buf = term_start(&shell, #{hidden: 1})
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002711 popup_create(buf, {})
2712 TermWait(buf, 100)
2713 popup_clear(true)
2714 assert_equal(2, winnr())
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002715
2716 quit
2717 exe 'bwipe! ' .. buf
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002718enddef
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002719
Bram Moolenaar934470e2019-09-01 23:27:05 +02002720func Test_popupwin_with_buffer_and_filter()
2721 new Xwithfilter
2722 call setline(1, range(100))
2723 let bufnr = bufnr()
2724 hide
2725
2726 func BufferFilter(win, key)
2727 if a:key == 'G'
2728 " recursive use of "G" does not cause problems.
2729 call win_execute(a:win, 'normal! G')
2730 return 1
2731 endif
2732 return 0
2733 endfunc
2734
2735 let winid = popup_create(bufnr, #{maxheight: 5, filter: 'BufferFilter'})
2736 call assert_equal(1, popup_getpos(winid).firstline)
2737 redraw
2738 call feedkeys("G", 'xt')
2739 call assert_equal(99, popup_getpos(winid).firstline)
2740
2741 call popup_close(winid)
2742 exe 'bwipe! ' .. bufnr
2743endfunc
2744
Bram Moolenaare296e312019-07-03 23:20:18 +02002745func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002746 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002747 \ maxwidth: 40,
2748 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02002749 \ })
2750 for top in range(1, 20)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002751 eval winid->popup_setoptions(#{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02002752 redraw
2753 call assert_equal(19, popup_getpos(winid).width)
2754 endfor
2755 call popup_clear()
2756endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02002757
2758func Test_popupwin_buf_close()
2759 let buf = bufadd('Xtestbuf')
2760 call bufload(buf)
2761 call setbufline(buf, 1, ['just', 'some', 'lines'])
2762 let winid = popup_create(buf, {})
2763 redraw
2764 call assert_equal(3, popup_getpos(winid).height)
2765 let bufinfo = getbufinfo(buf)[0]
2766 call assert_equal(1, bufinfo.changed)
2767 call assert_equal(0, bufinfo.hidden)
2768 call assert_equal(0, bufinfo.listed)
2769 call assert_equal(1, bufinfo.loaded)
2770 call assert_equal([], bufinfo.windows)
2771 call assert_equal([winid], bufinfo.popups)
2772
2773 call popup_close(winid)
2774 call assert_equal({}, popup_getpos(winid))
2775 let bufinfo = getbufinfo(buf)[0]
2776 call assert_equal(1, bufinfo.changed)
2777 call assert_equal(1, bufinfo.hidden)
2778 call assert_equal(0, bufinfo.listed)
2779 call assert_equal(1, bufinfo.loaded)
2780 call assert_equal([], bufinfo.windows)
2781 call assert_equal([], bufinfo.popups)
2782 exe 'bwipe! ' .. buf
2783endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02002784
2785func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002786 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02002787
2788 let lines =<< trim END
2789 call setline(1, range(1, 10))
2790 hi ScrollThumb ctermbg=blue
2791 hi ScrollBar ctermbg=red
2792 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002793 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002794 \ maxwidth: 10,
2795 \ maxheight: 3,
2796 \ pos : 'topleft',
2797 \ col : a:col,
2798 \ line : a:line,
2799 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02002800 \ })
2801 endfunc
2802 call PopupMenu(['x'], 1, 1)
2803 call PopupMenu(['123456789|'], 1, 16)
2804 call PopupMenu(['123456789|' .. ' '], 7, 1)
2805 call PopupMenu([repeat('123456789|', 100)], 7, 16)
2806 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
2807 END
2808 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002809 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02002810 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
2811
2812 " close the menu popupwin.
2813 call term_sendkeys(buf, " ")
2814 call term_sendkeys(buf, " ")
2815 call term_sendkeys(buf, " ")
2816 call term_sendkeys(buf, " ")
2817 call term_sendkeys(buf, " ")
2818
2819 " clean up
2820 call StopVimInTerminal(buf)
2821 call delete('XtestPopupMenuMaxWidth')
2822endfunc
2823
Bram Moolenaara901a372019-07-13 16:38:50 +02002824func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002825 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02002826
2827 let lines =<< trim END
2828 call setline(1, range(1, 20))
2829 hi ScrollThumb ctermbg=blue
2830 hi ScrollBar ctermbg=red
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002831 eval ['one', 'two', 'three', 'four', 'five',
2832 \ 'six', 'seven', 'eight', 'nine']
2833 \ ->popup_menu(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002834 \ minwidth: 8,
2835 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02002836 \ })
2837 END
2838 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002839 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02002840
2841 call term_sendkeys(buf, "j")
2842 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
2843
2844 call term_sendkeys(buf, "jjj")
2845 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
2846
2847 " if the cursor is the bottom line, it stays at the bottom line.
2848 call term_sendkeys(buf, repeat("j", 20))
2849 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
2850
2851 call term_sendkeys(buf, "kk")
2852 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
2853
2854 call term_sendkeys(buf, "k")
2855 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
2856
2857 " if the cursor is in the top line, it stays in the top line.
2858 call term_sendkeys(buf, repeat("k", 20))
2859 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
2860
2861 " close the menu popupwin.
2862 call term_sendkeys(buf, " ")
2863
2864 " clean up
2865 call StopVimInTerminal(buf)
2866 call delete('XtestPopupMenuScroll')
2867endfunc
2868
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002869func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002870 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002871
2872 let lines =<< trim END
2873 function! MyFilter(winid, key) abort
2874 if a:key == "0"
2875 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
2876 return 1
2877 endif
2878 if a:key == "G"
2879 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
2880 return 1
2881 endif
2882 if a:key == "j"
2883 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
2884 return 1
2885 endif
2886 if a:key == "k"
2887 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
2888 return 1
2889 endif
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002890 if a:key == ':'
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002891 call popup_close(a:winid)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002892 return 0
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002893 endif
2894 return 0
2895 endfunction
2896 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
2897 \ maxheight : 3,
2898 \ filter : 'MyFilter'
2899 \ })
2900 END
2901 call writefile(lines, 'XtestPopupMenuFilter')
2902 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
2903
2904 call term_sendkeys(buf, "j")
2905 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
2906
2907 call term_sendkeys(buf, "k")
2908 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
2909
2910 call term_sendkeys(buf, "G")
2911 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
2912
2913 call term_sendkeys(buf, "0")
2914 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
2915
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002916 " check that when the popup is closed in the filter the screen is redrawn
2917 call term_sendkeys(buf, ":")
2918 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_5', {})
2919 call term_sendkeys(buf, "\<CR>")
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002920
2921 " clean up
2922 call StopVimInTerminal(buf)
2923 call delete('XtestPopupMenuFilter')
2924endfunc
2925
2926func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002927 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002928
2929 let winid = popup_create('some text', {})
2930 call assert_equal(0, popup_getoptions(winid).cursorline)
2931 call popup_close(winid)
2932
2933 let winid = popup_create('some text', #{ cursorline: 1, })
2934 call assert_equal(1, popup_getoptions(winid).cursorline)
2935 call popup_close(winid)
2936
2937 let winid = popup_create('some text', #{ cursorline: 0, })
2938 call assert_equal(0, popup_getoptions(winid).cursorline)
2939 call popup_close(winid)
2940
2941 let winid = popup_menu('some text', {})
2942 call assert_equal(1, popup_getoptions(winid).cursorline)
2943 call popup_close(winid)
2944
2945 let winid = popup_menu('some text', #{ cursorline: 1, })
2946 call assert_equal(1, popup_getoptions(winid).cursorline)
2947 call popup_close(winid)
2948
2949 let winid = popup_menu('some text', #{ cursorline: 0, })
2950 call assert_equal(0, popup_getoptions(winid).cursorline)
2951 call popup_close(winid)
2952
2953 " ---------
2954 " Pattern 1
2955 " ---------
2956 let lines =<< trim END
2957 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
2958 END
2959 call writefile(lines, 'XtestPopupCursorLine')
2960 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2961 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
2962 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2963 call StopVimInTerminal(buf)
2964
2965 " ---------
2966 " Pattern 2
2967 " ---------
2968 let lines =<< trim END
2969 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
2970 END
2971 call writefile(lines, 'XtestPopupCursorLine')
2972 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2973 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
2974 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2975 call StopVimInTerminal(buf)
2976
2977 " ---------
2978 " Pattern 3
2979 " ---------
2980 let lines =<< trim END
2981 function! MyFilter(winid, key) abort
2982 if a:key == "j"
2983 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2984 return 1
2985 endif
2986 if a:key == 'x'
2987 call popup_close(a:winid)
2988 return 1
2989 endif
2990 return 0
2991 endfunction
2992 call popup_menu(['111', '222', '333'], #{
2993 \ cursorline : 0,
2994 \ maxheight : 2,
2995 \ filter : 'MyFilter',
2996 \ })
2997 END
2998 call writefile(lines, 'XtestPopupCursorLine')
2999 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3000 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
3001 call term_sendkeys(buf, "j")
3002 call term_sendkeys(buf, "j")
3003 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
3004 call term_sendkeys(buf, "x")
3005 call StopVimInTerminal(buf)
3006
3007 " ---------
3008 " Pattern 4
3009 " ---------
3010 let lines =<< trim END
3011 function! MyFilter(winid, key) abort
3012 if a:key == "j"
3013 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
3014 return 1
3015 endif
3016 if a:key == 'x'
3017 call popup_close(a:winid)
3018 return 1
3019 endif
3020 return 0
3021 endfunction
3022 call popup_menu(['111', '222', '333'], #{
3023 \ cursorline : 1,
3024 \ maxheight : 2,
3025 \ filter : 'MyFilter',
3026 \ })
3027 END
3028 call writefile(lines, 'XtestPopupCursorLine')
3029 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3030 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
3031 call term_sendkeys(buf, "j")
3032 call term_sendkeys(buf, "j")
3033 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
3034 call term_sendkeys(buf, "x")
3035 call StopVimInTerminal(buf)
3036
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003037 " ---------
3038 " Cursor in second line when creating the popup
3039 " ---------
3040 let lines =<< trim END
3041 let winid = popup_create(['111', '222', '333'], #{
3042 \ cursorline : 1,
3043 \ })
3044 call win_execute(winid, "2")
3045 END
3046 call writefile(lines, 'XtestPopupCursorLine')
3047 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3048 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_7', {})
3049 call StopVimInTerminal(buf)
3050
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003051 call delete('XtestPopupCursorLine')
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01003052
3053 " ---------
3054 " Use current buffer for popupmenu
3055 " ---------
3056 let lines =<< trim END
3057 call setline(1, ['one', 'two', 'three'])
3058 let winid = popup_create(bufnr('%'), #{
3059 \ cursorline : 1,
3060 \ })
3061 call win_execute(winid, "2")
3062 END
3063 call writefile(lines, 'XtestPopupCursorLine')
3064 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3065 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_8', {})
3066 call StopVimInTerminal(buf)
3067
3068 call delete('XtestPopupCursorLine')
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003069endfunc
3070
Bram Moolenaarf914a332019-07-20 15:09:56 +02003071func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003072 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003073 CheckFeature quickfix
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003074
Bram Moolenaarf914a332019-07-20 15:09:56 +02003075 call writefile([
3076 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
3077 \ "another\tXtagfile\t/^this is another",
3078 \ "theword\tXtagfile\t/^theword"],
3079 \ 'Xtags')
3080 call writefile(range(1,20)
3081 \ + ['theword is here']
3082 \ + range(22, 27)
3083 \ + ['this is another place']
3084 \ + range(29, 40),
3085 \ "Xtagfile")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003086 call writefile(range(1,10)
3087 \ + ['searched word is here']
3088 \ + range(12, 20),
3089 \ "Xheader.h")
Bram Moolenaarf914a332019-07-20 15:09:56 +02003090 let lines =<< trim END
3091 set tags=Xtags
3092 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003093 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003094 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003095 \ 'three',
3096 \ 'four',
3097 \ 'five',
3098 \ 'six',
3099 \ 'seven',
3100 \ 'find theword somewhere',
3101 \ 'nine',
3102 \ 'this is another word',
3103 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02003104 set previewpopup=height:4,width:40
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003105 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02003106 END
3107 call writefile(lines, 'XtestPreviewPopup')
3108 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
3109
3110 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
3111 call term_sendkeys(buf, ":\<CR>")
3112 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
3113
3114 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
3115 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
3116
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003117 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003118 call term_sendkeys(buf, ":\<CR>")
3119 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
3120
3121 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
3122 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
3123
Bram Moolenaar799439a2020-02-11 21:44:17 +01003124 call term_sendkeys(buf, ":silent cd ..\<CR>:\<CR>")
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003125 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar799439a2020-02-11 21:44:17 +01003126 call term_sendkeys(buf, ":silent cd testdir\<CR>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003127
3128 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02003129 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003130 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
3131
3132 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
3133 call term_sendkeys(buf, ":\<CR>")
3134 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
3135
3136 call term_sendkeys(buf, ":pclose\<CR>")
3137 call term_sendkeys(buf, ":psearch searched\<CR>")
3138 call term_sendkeys(buf, ":\<CR>")
3139 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003140
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01003141 call term_sendkeys(buf, "\<C-W>p")
3142 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_9', {})
3143
3144 call term_sendkeys(buf, ":call win_execute(popup_findpreview(), 'call popup_clear()')\<CR>")
3145 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_10', {})
3146
Bram Moolenaarf914a332019-07-20 15:09:56 +02003147 call StopVimInTerminal(buf)
3148 call delete('Xtags')
3149 call delete('Xtagfile')
3150 call delete('XtestPreviewPopup')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003151 call delete('Xheader.h')
Bram Moolenaarf914a332019-07-20 15:09:56 +02003152endfunc
3153
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003154func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003155 let lines =<< trim END
3156 set completeopt+=preview,popup
3157 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02003158 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003159
3160 func CompleteFuncDict(findstart, base)
3161 if a:findstart
3162 if col('.') > 10
3163 return col('.') - 10
3164 endif
3165 return 0
3166 endif
3167
3168 return {
3169 \ 'words': [
3170 \ {
3171 \ 'word': 'aword',
3172 \ 'abbr': 'wrd',
3173 \ 'menu': 'extra text',
3174 \ 'info': 'words are cool',
3175 \ 'kind': 'W',
3176 \ 'user_data': 'test'
3177 \ },
3178 \ {
3179 \ 'word': 'anotherword',
3180 \ 'abbr': 'anotwrd',
3181 \ 'menu': 'extra text',
3182 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
3183 \ 'kind': 'W',
3184 \ 'user_data': 'notest'
3185 \ },
3186 \ {
3187 \ 'word': 'noinfo',
3188 \ 'abbr': 'noawrd',
3189 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02003190 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003191 \ 'kind': 'W',
3192 \ 'user_data': 'notest'
3193 \ },
3194 \ {
3195 \ 'word': 'thatword',
3196 \ 'abbr': 'thatwrd',
3197 \ 'menu': 'extra text',
3198 \ 'info': 'that word is cool',
3199 \ 'kind': 'W',
3200 \ 'user_data': 'notest'
3201 \ },
3202 \ ]
3203 \ }
3204 endfunc
3205 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003206 func ChangeColor()
3207 let id = popup_findinfo()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003208 eval id->popup_setoptions(#{highlight: 'InfoPopup'})
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003209 endfunc
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003210
3211 func InfoHidden()
3212 set completepopup=height:4,border:off,align:menu
3213 set completeopt-=popup completeopt+=popuphidden
3214 au CompleteChanged * call HandleChange()
3215 endfunc
3216
3217 let s:counter = 0
3218 func HandleChange()
3219 let s:counter += 1
3220 let selected = complete_info(['selected']).selected
3221 if selected <= 0
3222 " First time: do nothing, info remains hidden
3223 return
3224 endif
3225 if selected == 1
3226 " Second time: show info right away
3227 let id = popup_findinfo()
3228 if id
3229 call popup_settext(id, 'immediate info ' .. s:counter)
3230 call popup_show(id)
3231 endif
3232 else
3233 " Third time: show info after a short delay
3234 call timer_start(100, 'ShowInfo')
3235 endif
3236 endfunc
3237
3238 func ShowInfo(...)
3239 let id = popup_findinfo()
3240 if id
3241 call popup_settext(id, 'async info ' .. s:counter)
3242 call popup_show(id)
3243 endif
3244 endfunc
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003245 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003246 return lines
3247endfunc
3248
3249func Test_popupmenu_info_border()
3250 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003251 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003252
3253 let lines = Get_popupmenu_lines()
3254 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003255 call writefile(lines, 'XtestInfoPopup')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003256
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003257 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003258 call TermWait(buf, 25)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003259
3260 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3261 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
3262
3263 call term_sendkeys(buf, "\<C-N>")
3264 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
3265
3266 call term_sendkeys(buf, "\<C-N>")
3267 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
3268
3269 call term_sendkeys(buf, "\<C-N>\<C-N>")
3270 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
3271
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02003272 " info on the left with scrollbar
3273 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
3274 call term_sendkeys(buf, "\<C-N>\<C-N>")
3275 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
3276
Bram Moolenaar202c3f72019-11-21 12:12:35 +01003277 " Test that the popupmenu's scrollbar and infopopup do not overlap
3278 call term_sendkeys(buf, "\<Esc>")
3279 call term_sendkeys(buf, ":set pumheight=3\<CR>")
3280 call term_sendkeys(buf, "cc\<C-X>\<C-U>")
3281 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_6', {})
3282
Bram Moolenaarca7c0782020-01-14 20:42:48 +01003283 " Hide the info popup, cycle trough buffers, make sure it didn't get
3284 " deleted.
3285 call term_sendkeys(buf, "\<Esc>")
3286 call term_sendkeys(buf, ":set hidden\<CR>")
3287 call term_sendkeys(buf, ":bn\<CR>")
3288 call term_sendkeys(buf, ":bn\<CR>")
3289 call term_sendkeys(buf, "otest text test text\<C-X>\<C-U>")
3290 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_7', {})
3291
Bram Moolenaar447bfba2020-07-18 16:07:16 +02003292 " Test that when the option is changed the popup changes.
3293 call term_sendkeys(buf, "\<Esc>")
3294 call term_sendkeys(buf, ":set completepopup=border:off\<CR>")
3295 call term_sendkeys(buf, "a\<C-X>\<C-U>")
3296 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_8', {})
3297
Bram Moolenaar6d585f42020-07-26 22:20:54 +02003298 call term_sendkeys(buf, " \<Esc>")
3299 call term_sendkeys(buf, ":set completepopup+=width:10\<CR>")
3300 call term_sendkeys(buf, "a\<C-X>\<C-U>")
3301 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_9', {})
3302
Bram Moolenaarca7c0782020-01-14 20:42:48 +01003303 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003304 call StopVimInTerminal(buf)
3305 call delete('XtestInfoPopup')
3306endfunc
3307
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003308func Test_popupmenu_info_noborder()
3309 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003310 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003311
3312 let lines = Get_popupmenu_lines()
3313 call add(lines, 'set completepopup=height:4,border:off')
3314 call writefile(lines, 'XtestInfoPopupNb')
3315
3316 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003317 call TermWait(buf, 25)
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003318
3319 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3320 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
3321
3322 call StopVimInTerminal(buf)
3323 call delete('XtestInfoPopupNb')
3324endfunc
3325
Bram Moolenaar258cef52019-08-21 17:29:29 +02003326func Test_popupmenu_info_align_menu()
3327 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003328 CheckFeature quickfix
Bram Moolenaar258cef52019-08-21 17:29:29 +02003329
3330 let lines = Get_popupmenu_lines()
3331 call add(lines, 'set completepopup=height:4,border:off,align:menu')
3332 call writefile(lines, 'XtestInfoPopupNb')
3333
3334 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003335 call TermWait(buf, 25)
Bram Moolenaar258cef52019-08-21 17:29:29 +02003336
3337 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3338 call term_sendkeys(buf, "\<C-N>")
3339 call term_sendkeys(buf, "\<C-N>")
3340 call term_sendkeys(buf, "\<C-N>")
3341 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
3342
3343 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
3344 call term_sendkeys(buf, "\<C-N>")
3345 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
3346
3347 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003348 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02003349 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
3350 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
3351 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
3352
3353 call StopVimInTerminal(buf)
3354 call delete('XtestInfoPopupNb')
3355endfunc
3356
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003357func Test_popupmenu_info_hidden()
3358 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003359 CheckFeature quickfix
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003360
3361 let lines = Get_popupmenu_lines()
3362 call add(lines, 'call InfoHidden()')
3363 call writefile(lines, 'XtestInfoPopupHidden')
3364
3365 let buf = RunVimInTerminal('-S XtestInfoPopupHidden', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003366 call TermWait(buf, 25)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003367
3368 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3369 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_1', {})
3370
3371 call term_sendkeys(buf, "\<C-N>")
3372 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_2', {})
3373
3374 call term_sendkeys(buf, "\<C-N>")
3375 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_3', {})
3376
3377 call term_sendkeys(buf, "\<Esc>")
3378 call StopVimInTerminal(buf)
3379 call delete('XtestInfoPopupHidden')
3380endfunc
3381
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003382func Test_popupmenu_info_too_wide()
3383 CheckScreendump
3384 CheckFeature quickfix
3385
3386 let lines =<< trim END
3387 call setline(1, range(10))
3388
3389 set completeopt+=preview,popup
3390 set completepopup=align:menu
3391 set omnifunc=OmniFunc
3392 hi InfoPopup ctermbg=lightgrey
3393
3394 func OmniFunc(findstart, base)
3395 if a:findstart
3396 return 0
3397 endif
3398
3399 let menuText = 'some long text to make sure the menu takes up all of the width of the window'
3400 return #{
3401 \ words: [
3402 \ #{
3403 \ word: 'scrap',
3404 \ menu: menuText,
3405 \ info: "other words are\ncooler than this and some more text\nto make wrap",
3406 \ },
3407 \ #{
3408 \ word: 'scappier',
3409 \ menu: menuText,
3410 \ info: 'words are cool',
3411 \ },
3412 \ #{
3413 \ word: 'scrappier2',
3414 \ menu: menuText,
3415 \ info: 'words are cool',
3416 \ },
3417 \ ]
3418 \ }
3419 endfunc
3420 END
3421
3422 call writefile(lines, 'XtestInfoPopupWide')
3423 let buf = RunVimInTerminal('-S XtestInfoPopupWide', #{rows: 8})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003424 call TermWait(buf, 25)
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003425
3426 call term_sendkeys(buf, "Ascr\<C-X>\<C-O>")
3427 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_wide_1', {})
3428
3429 call term_sendkeys(buf, "\<Esc>")
3430 call StopVimInTerminal(buf)
3431 call delete('XtestInfoPopupWide')
3432endfunc
3433
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003434func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02003435 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003436 call popup_clear()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003437 let winid = 'nothing wrong'->popup_notification({})
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003438 call assert_equal(bufnr, winbufnr(winid))
3439 call popup_clear()
3440endfunc
3441
Bram Moolenaar1824f452019-10-02 23:06:46 +02003442func Test_popupwin_getoptions_tablocal()
3443 topleft split
3444 let win1 = popup_create('nothing', #{maxheight: 8})
3445 let win2 = popup_create('something', #{maxheight: 10})
3446 let win3 = popup_create('something', #{maxheight: 15})
3447 call assert_equal(8, popup_getoptions(win1).maxheight)
3448 call assert_equal(10, popup_getoptions(win2).maxheight)
3449 call assert_equal(15, popup_getoptions(win3).maxheight)
3450 call popup_clear()
3451 quit
3452endfunc
3453
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02003454func Test_popupwin_cancel()
3455 let win1 = popup_create('one', #{line: 5, filter: {... -> 0}})
3456 let win2 = popup_create('two', #{line: 10, filter: {... -> 0}})
3457 let win3 = popup_create('three', #{line: 15, filter: {... -> 0}})
3458 call assert_equal(5, popup_getpos(win1).line)
3459 call assert_equal(10, popup_getpos(win2).line)
3460 call assert_equal(15, popup_getpos(win3).line)
3461 " TODO: this also works without patch 8.1.2110
3462 call feedkeys("\<C-C>", 'xt')
3463 call assert_equal(5, popup_getpos(win1).line)
3464 call assert_equal(10, popup_getpos(win2).line)
3465 call assert_equal({}, popup_getpos(win3))
3466 call feedkeys("\<C-C>", 'xt')
3467 call assert_equal(5, popup_getpos(win1).line)
3468 call assert_equal({}, popup_getpos(win2))
3469 call assert_equal({}, popup_getpos(win3))
3470 call feedkeys("\<C-C>", 'xt')
3471 call assert_equal({}, popup_getpos(win1))
3472 call assert_equal({}, popup_getpos(win2))
3473 call assert_equal({}, popup_getpos(win3))
3474endfunc
3475
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003476func Test_popupwin_filter_redraw()
3477 " Create two popups with a filter that closes the popup when typing "0".
3478 " Both popups should close, even though the redraw also calls
3479 " popup_reset_handled()
3480
3481 func CloseFilter(winid, key)
3482 if a:key == '0'
3483 call popup_close(a:winid)
3484 redraw
3485 endif
3486 return 0 " pass the key
3487 endfunc
3488
3489 let id1 = popup_create('first one', #{
3490 \ line: 1,
3491 \ col: 1,
3492 \ filter: 'CloseFilter',
3493 \ })
3494 let id2 = popup_create('second one', #{
3495 \ line: 9,
3496 \ col: 1,
3497 \ filter: 'CloseFilter',
3498 \ })
3499 call assert_equal(1, popup_getpos(id1).line)
3500 call assert_equal(9, popup_getpos(id2).line)
3501
3502 call feedkeys('0', 'xt')
3503 call assert_equal({}, popup_getpos(id1))
3504 call assert_equal({}, popup_getpos(id2))
3505
3506 call popup_clear()
3507 delfunc CloseFilter
3508endfunc
3509
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003510func Test_popupwin_double_width()
3511 CheckScreendump
3512
3513 let lines =<< trim END
3514 call setline(1, 'x你好世界你好世你好世界你好')
3515 call setline(2, '你好世界你好世你好世界你好')
3516 call setline(3, 'x你好世界你好世你好世界你好')
3517 call popup_create('你好,世界 - 你好,世界xxxxx', #{line: 1, col: 3, maxwidth: 14})
3518 END
3519 call writefile(lines, 'XtestPopupWide')
3520
3521 let buf = RunVimInTerminal('-S XtestPopupWide', #{rows: 10})
3522 call VerifyScreenDump(buf, 'Test_popupwin_doublewidth_1', {})
3523
3524 call StopVimInTerminal(buf)
3525 call delete('XtestPopupWide')
3526endfunc
3527
3528func Test_popupwin_sign()
3529 CheckScreendump
3530
3531 let lines =<< trim END
3532 call setline(1, range(10))
3533 call sign_define('Current', {
3534 \ 'text': '>>',
3535 \ 'texthl': 'WarningMsg',
3536 \ 'linehl': 'Error',
3537 \ })
3538 call sign_define('Other', {
3539 \ 'text': '#!',
3540 \ 'texthl': 'Error',
3541 \ 'linehl': 'Search',
3542 \ })
3543 let winid = popup_create(['hello', 'bright', 'world'], {
3544 \ 'minwidth': 20,
3545 \ })
3546 call setwinvar(winid, "&signcolumn", "yes")
3547 let winbufnr = winbufnr(winid)
3548
3549 " add sign to current buffer, shows
3550 call sign_place(1, 'Selected', 'Current', bufnr('%'), {'lnum': 1})
3551 " add sign to current buffer, does not show
3552 call sign_place(2, 'PopUpSelected', 'Other', bufnr('%'), {'lnum': 2})
3553
3554 " add sign to popup buffer, shows
3555 call sign_place(3, 'PopUpSelected', 'Other', winbufnr, {'lnum': 1})
3556 " add sign to popup buffer, does not show
3557 call sign_place(4, 'Selected', 'Current', winbufnr, {'lnum': 2})
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02003558
3559 func SetOptions()
3560 call setwinvar(g:winid, '&number', 1)
3561 call setwinvar(g:winid, '&foldcolumn', 2)
3562 call popup_settext(g:winid, 'a longer line to check the width')
3563 endfunc
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003564 END
3565 call writefile(lines, 'XtestPopupSign')
3566
3567 let buf = RunVimInTerminal('-S XtestPopupSign', #{rows: 10})
3568 call VerifyScreenDump(buf, 'Test_popupwin_sign_1', {})
3569
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02003570 " set more options to check the width is adjusted
3571 call term_sendkeys(buf, ":call SetOptions()\<CR>")
3572 call VerifyScreenDump(buf, 'Test_popupwin_sign_2', {})
3573
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003574 call StopVimInTerminal(buf)
3575 call delete('XtestPopupSign')
3576endfunc
3577
Bram Moolenaar99ebf222019-12-10 23:44:48 +01003578func Test_popupwin_bufnr()
3579 let popwin = popup_create(['blah'], #{})
3580 let popbuf = winbufnr(popwin)
3581 split asdfasdf
3582 let newbuf = bufnr()
3583 call assert_true(newbuf > popbuf, 'New buffer number is higher')
3584 call assert_equal(newbuf, bufnr('$'))
3585 call popup_clear()
3586 let popwin = popup_create(['blah'], #{})
3587 " reuses previous buffer number
3588 call assert_equal(popbuf, winbufnr(popwin))
3589 call assert_equal(newbuf, bufnr('$'))
3590
3591 call popup_clear()
3592 bwipe!
3593endfunc
3594
Bram Moolenaarec084d32020-02-28 22:44:47 +01003595func Test_popupwin_filter_input_multibyte()
3596 func MyPopupFilter(winid, c)
3597 let g:bytes = range(a:c->strlen())->map({i -> char2nr(a:c[i])})
3598 return 0
3599 endfunc
3600 let winid = popup_create('', #{mapping: 0, filter: 'MyPopupFilter'})
3601
3602 " UTF-8: E3 80 80, including K_SPECIAL(0x80)
3603 call feedkeys("\u3000", 'xt')
3604 call assert_equal([0xe3, 0x80, 0x80], g:bytes)
3605
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01003606 " UTF-8: E3 80 9B, including CSI(0x9B)
3607 call feedkeys("\u301b", 'xt')
3608 call assert_equal([0xe3, 0x80, 0x9b], g:bytes)
Bram Moolenaarec084d32020-02-28 22:44:47 +01003609
Bram Moolenaar20298ce2020-06-19 21:46:52 +02003610 if has('unix')
3611 " with modifyOtherKeys <M-S-a> does not include a modifier sequence
Bram Moolenaarb326edf2020-06-20 15:03:38 +02003612 if has('gui_running')
3613 call feedkeys("\x9b\xfc\x08A", 'Lx!')
3614 else
3615 call feedkeys("\<Esc>[27;4;65~", 'Lx!')
3616 endif
Bram Moolenaar20298ce2020-06-19 21:46:52 +02003617 call assert_equal([0xc3, 0x81], g:bytes)
3618 endif
3619
Bram Moolenaarec084d32020-02-28 22:44:47 +01003620 call popup_clear()
3621 delfunc MyPopupFilter
3622 unlet g:bytes
3623endfunc
3624
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003625func Test_popupwin_filter_close_ctrl_c()
3626 CheckScreendump
3627
3628 let lines =<< trim END
3629 vsplit
3630 set laststatus=2
3631 set statusline=%!Statusline()
3632
3633 function Statusline() abort
3634 return '%<%f %h%m%r%=%-14.(%l,%c%V%) %P'
3635 endfunction
3636
3637 call popup_create('test test test test...', {'filter': {-> 0}})
3638 END
3639 call writefile(lines, 'XtestPopupCtrlC')
3640
3641 let buf = RunVimInTerminal('-S XtestPopupCtrlC', #{rows: 10})
3642
3643 call term_sendkeys(buf, "\<C-C>")
3644 call VerifyScreenDump(buf, 'Test_popupwin_ctrl_c', {})
3645
3646 call StopVimInTerminal(buf)
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003647 call delete('XtestPopupCtrlC')
3648endfunc
3649
3650func Test_popupwin_filter_close_wrong_name()
3651 CheckScreendump
3652
3653 let lines =<< trim END
3654 call popup_create('one two three...', {'filter': 'NoSuchFunc'})
3655 END
3656 call writefile(lines, 'XtestPopupWrongName')
3657
3658 let buf = RunVimInTerminal('-S XtestPopupWrongName', #{rows: 10})
3659
3660 call term_sendkeys(buf, "j")
3661 call VerifyScreenDump(buf, 'Test_popupwin_wrong_name', {})
3662
3663 call StopVimInTerminal(buf)
3664 call delete('XtestPopupWrongName')
3665endfunc
3666
3667func Test_popupwin_filter_close_three_errors()
3668 CheckScreendump
3669
3670 let lines =<< trim END
3671 set cmdheight=2
3672 call popup_create('one two three...', {'filter': 'filter'})
3673 END
3674 call writefile(lines, 'XtestPopupThreeErrors')
3675
3676 let buf = RunVimInTerminal('-S XtestPopupThreeErrors', #{rows: 10})
3677
3678 call term_sendkeys(buf, "jj")
3679 call VerifyScreenDump(buf, 'Test_popupwin_three_errors_1', {})
3680 call term_sendkeys(buf, "j")
3681 call VerifyScreenDump(buf, 'Test_popupwin_three_errors_2', {})
3682
3683 call StopVimInTerminal(buf)
3684 call delete('XtestPopupThreeErrors')
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003685endfunc
3686
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003687func Test_popupwin_atcursor_far_right()
3688 new
3689
3690 " this was getting stuck
3691 set signcolumn=yes
3692 call setline(1, repeat('=', &columns))
3693 normal! ggg$
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003694 let winid = popup_atcursor(repeat('x', 500), #{moved: 'any', border: []})
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003695
Bram Moolenaar6d585f42020-07-26 22:20:54 +02003696 " 'signcolumn' was getting reset
3697 call setwinvar(winid, '&signcolumn', 'yes')
3698 call popup_setoptions(winid, {'zindex': 1000})
3699 call assert_equal('yes', getwinvar(winid, '&signcolumn'))
3700
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003701 call popup_close(winid)
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003702 bwipe!
3703 set signcolumn&
3704endfunc
3705
Bram Moolenaar0f1563f2020-03-20 21:15:51 +01003706func Test_popupwin_splitmove()
3707 vsplit
3708 let win2 = win_getid()
3709 let popup_winid = popup_dialog('hello', {})
3710 call assert_fails('call win_splitmove(popup_winid, win2)', 'E957:')
3711 call assert_fails('call win_splitmove(win2, popup_winid)', 'E957:')
3712
3713 call popup_clear()
3714 bwipe
3715endfunc
3716
3717
Bram Moolenaar331bafd2019-07-20 17:46:05 +02003718" vim: shiftwidth=2 sts=2