blob: 45f55f55b20b29908aec8f4796c8172df09de045 [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001" Tests for popup windows
2
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004CheckFeature popupwin
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005
6source screendump.vim
7
8func Test_simple_popup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02009 CheckScreendump
10
Bram Moolenaare7eb9272019-06-24 00:58:07 +020011 let lines =<< trim END
12 call setline(1, range(1, 100))
13 hi PopupColor1 ctermbg=lightblue
14 hi PopupColor2 ctermbg=lightcyan
Bram Moolenaar1666ac92019-11-10 17:22:31 +010015 hi EndOfBuffer ctermbg=lightgrey
Bram Moolenaare7eb9272019-06-24 00:58:07 +020016 hi Comment ctermfg=red
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020017 call prop_type_add('comment', #{highlight: 'Comment'})
18 let winid = popup_create('hello there', #{line: 3, col: 11, minwidth: 20, highlight: 'PopupColor1'})
19 let winid2 = popup_create(['another one', 'another two', 'another three'], #{line: 3, col: 25, minwidth: 20})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020020 call setwinvar(winid2, '&wincolor', 'PopupColor2')
21 END
22 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020023 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar4d784b22019-05-25 19:51:39 +020024 call VerifyScreenDump(buf, 'Test_popupwin_01', {})
25
Bram Moolenaarec583842019-05-26 14:11:23 +020026 " Add a tabpage
27 call term_sendkeys(buf, ":tabnew\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020028 call term_sendkeys(buf, ":let popupwin = popup_create(["
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020029 \ .. "#{text: 'other tab'},"
30 \ .. "#{text: 'a comment line', props: [#{"
Bram Moolenaard5abb4c2019-07-13 22:46:10 +020031 \ .. "col: 3, length: 7, minwidth: 20, type: 'comment'"
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020032 \ .. "}]},"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020033 \ .. "], #{line: 4, col: 9, minwidth: 20})\<CR>")
Bram Moolenaarec583842019-05-26 14:11:23 +020034 call VerifyScreenDump(buf, 'Test_popupwin_02', {})
35
36 " switch back to first tabpage
37 call term_sendkeys(buf, "gt")
38 call VerifyScreenDump(buf, 'Test_popupwin_03', {})
39
40 " close that tabpage
41 call term_sendkeys(buf, ":quit!\<CR>")
42 call VerifyScreenDump(buf, 'Test_popupwin_04', {})
43
Bram Moolenaar202d9822019-06-11 21:56:30 +020044 " set 'columns' to a small value, size must be recomputed
45 call term_sendkeys(buf, ":let cols = &columns\<CR>")
46 call term_sendkeys(buf, ":set columns=12\<CR>")
47 call VerifyScreenDump(buf, 'Test_popupwin_04a', {})
48 call term_sendkeys(buf, ":let &columns = cols\<CR>")
49
Bram Moolenaar17146962019-05-30 00:12:11 +020050 " resize popup, show empty line at bottom
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020051 call term_sendkeys(buf, ":call popup_move(popupwin, #{minwidth: 15, maxwidth: 25, minheight: 3, maxheight: 5})\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020052 call term_sendkeys(buf, ":redraw\<CR>")
53 call VerifyScreenDump(buf, 'Test_popupwin_05', {})
54
Bram Moolenaar17146962019-05-30 00:12:11 +020055 " show not fitting line at bottom
56 call term_sendkeys(buf, ":call setbufline(winbufnr(popupwin), 3, 'this line will not fit here')\<CR>")
57 call term_sendkeys(buf, ":redraw\<CR>")
58 call VerifyScreenDump(buf, 'Test_popupwin_06', {})
59
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020060 " move popup over ruler
61 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020062 call term_sendkeys(buf, ":call popup_move(popupwin, #{line: 7, col: 55})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +020063 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020064 call VerifyScreenDump(buf, 'Test_popupwin_07', {})
65
66 " clear all popups after moving the cursor a bit, so that ruler is updated
67 call term_sendkeys(buf, "axxx\<Esc>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +020068 call TermWait(buf)
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020069 call term_sendkeys(buf, "0")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +020070 call TermWait(buf)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +020071 call term_sendkeys(buf, ":call popup_clear()\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020072 call VerifyScreenDump(buf, 'Test_popupwin_08', {})
73
Bram Moolenaar4d784b22019-05-25 19:51:39 +020074 " clean up
75 call StopVimInTerminal(buf)
76 call delete('XtestPopup')
77endfunc
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020078
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020079func Test_popup_with_border_and_padding()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +020080 CheckScreendump
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020081
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020082 for iter in range(0, 1)
Bram Moolenaare7eb9272019-06-24 00:58:07 +020083 let lines =<< trim END
84 call setline(1, range(1, 100))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020085 call popup_create('hello border', #{line: 2, col: 3, border: []})
86 call popup_create('hello padding', #{line: 2, col: 23, padding: []})
Bram Moolenaarc363fe12019-08-04 18:13:46 +020087 call popup_create('hello both', #{line: 2, col: 43, border: [], padding: [], highlight: 'Normal'})
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020088 call popup_create('border TL', #{line: 6, col: 3, border: [1, 0, 0, 4]})
Bram Moolenaarb0992022020-01-30 14:55:42 +010089 call popup_create('paddings', #{line: 6, col: 23, padding: range(1, 4)})
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020090 call popup_create('wrapped longer text', #{line: 8, col: 55, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
91 call popup_create('right aligned text', #{line: 11, col: 56, wrap: 0, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
Bram Moolenaarfbcdf672020-01-06 23:07:48 +010092 call popup_create('X', #{line: 2, col: 73})
93 call popup_create('X', #{line: 3, col: 74})
94 call popup_create('X', #{line: 4, col: 75})
95 call popup_create('X', #{line: 5, col: 76})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020096 END
97 call insert(lines, iter == 1 ? '' : 'set enc=latin1')
98 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020099 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 15})
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +0200100 call VerifyScreenDump(buf, 'Test_popupwin_2' .. iter, {})
101
102 call StopVimInTerminal(buf)
103 call delete('XtestPopupBorder')
104 endfor
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200105
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200106 let lines =<< trim END
107 call setline(1, range(1, 100))
108 hi BlueColor ctermbg=lightblue
109 hi TopColor ctermbg=253
110 hi RightColor ctermbg=245
111 hi BottomColor ctermbg=240
112 hi LeftColor ctermbg=248
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200113 call popup_create('hello border', #{line: 2, col: 3, border: [], borderhighlight: ['BlueColor']})
114 call popup_create(['hello border', 'and more'], #{line: 2, col: 23, border: [], borderhighlight: ['TopColor', 'RightColor', 'BottomColor', 'LeftColor']})
115 call popup_create(['hello border', 'lines only'], #{line: 2, col: 43, border: [], borderhighlight: ['BlueColor'], borderchars: ['x']})
116 call popup_create(['hello border', 'with corners'], #{line: 2, col: 60, border: [], borderhighlight: ['BlueColor'], borderchars: ['x', '#']})
117 let winid = popup_create(['hello border', 'with numbers'], #{line: 6, col: 3, border: [], borderhighlight: ['BlueColor'], borderchars: ['0', '1', '2', '3', '4', '5', '6', '7']})
118 call popup_create(['hello border', 'just blanks'], #{line: 7, col: 23, border: [], borderhighlight: ['BlueColor'], borderchars: [' ']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200119 func MultiByte()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200120 call popup_create(['hello'], #{line: 8, col: 43, border: [], borderchars: ['─', '│', '─', '│', '┌', '┐', '┘', '└']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200121 endfunc
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200122 END
123 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200124 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 12})
Bram Moolenaar790498b2019-06-01 22:15:29 +0200125 call VerifyScreenDump(buf, 'Test_popupwin_22', {})
126
Bram Moolenaarad24a712019-06-17 20:05:45 +0200127 " check that changing borderchars triggers a redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200128 call term_sendkeys(buf, ":call popup_setoptions(winid, #{borderchars: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']})\<CR>")
Bram Moolenaarad24a712019-06-17 20:05:45 +0200129 call VerifyScreenDump(buf, 'Test_popupwin_23', {})
130
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200131 " check multi-byte border only with 'ambiwidth' single
132 if &ambiwidth == 'single'
133 call term_sendkeys(buf, ":call MultiByte()\<CR>")
134 call VerifyScreenDump(buf, 'Test_popupwin_24', {})
135 endif
136
Bram Moolenaar790498b2019-06-01 22:15:29 +0200137 call StopVimInTerminal(buf)
138 call delete('XtestPopupBorder')
139
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200140 let with_border_or_padding = #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200141 \ line: 2,
142 \ core_line: 3,
143 \ col: 3,
144 \ core_col: 4,
145 \ width: 14,
146 \ core_width: 12,
147 \ height: 3,
148 \ core_height: 1,
149 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100150 \ lastline: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200151 \ scrollbar: 0,
152 \ visible: 1}
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200153 let winid = popup_create('hello border', #{line: 2, col: 3, border: []})",
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200154 call assert_equal(with_border_or_padding, winid->popup_getpos())
Bram Moolenaarae943152019-06-16 22:54:14 +0200155 let options = popup_getoptions(winid)
156 call assert_equal([], options.border)
157 call assert_false(has_key(options, "padding"))
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200158
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200159 let winid = popup_create('hello padding', #{line: 2, col: 3, padding: []})
Bram Moolenaarae943152019-06-16 22:54:14 +0200160 let with_border_or_padding.width = 15
161 let with_border_or_padding.core_width = 13
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200162 call assert_equal(with_border_or_padding, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200163 let options = popup_getoptions(winid)
164 call assert_false(has_key(options, "border"))
165 call assert_equal([], options.padding)
166
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200167 call popup_setoptions(winid, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200168 \ padding: [1, 2, 3, 4],
169 \ border: [4, 0, 7, 8],
170 \ borderhighlight: ['Top', 'Right', 'Bottom', 'Left'],
171 \ borderchars: ['1', '^', '2', '>', '3', 'v', '4', '<'],
Bram Moolenaarae943152019-06-16 22:54:14 +0200172 \ })
173 let options = popup_getoptions(winid)
174 call assert_equal([1, 0, 1, 1], options.border)
175 call assert_equal([1, 2, 3, 4], options.padding)
176 call assert_equal(['Top', 'Right', 'Bottom', 'Left'], options.borderhighlight)
177 call assert_equal(['1', '^', '2', '>', '3', 'v', '4', '<'], options.borderchars)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200178
Bram Moolenaara1396152019-10-20 18:46:05 +0200179 " Check that popup_setoptions() takes the output of popup_getoptions()
180 call popup_setoptions(winid, options)
181 call assert_equal(options, popup_getoptions(winid))
182
Bram Moolenaarb0992022020-01-30 14:55:42 +0100183 " Check that range() doesn't crash
184 call popup_setoptions(winid, #{
185 \ padding: range(1, 4),
186 \ border: range(5, 8),
187 \ borderhighlight: range(4),
188 \ borderchars: range(8),
189 \ })
190
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200191 let winid = popup_create('hello both', #{line: 3, col: 8, border: [], padding: []})
192 call assert_equal(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200193 \ line: 3,
194 \ core_line: 5,
195 \ col: 8,
196 \ core_col: 10,
197 \ width: 14,
198 \ core_width: 10,
199 \ height: 5,
200 \ scrollbar: 0,
201 \ core_height: 1,
202 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100203 \ lastline: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200204 \ visible: 1}, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200205
206 call popup_clear()
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200207endfunc
208
Bram Moolenaarb4230122019-05-30 18:40:53 +0200209func Test_popup_with_syntax_win_execute()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200210 CheckScreendump
211
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200212 let lines =<< trim END
213 call setline(1, range(1, 100))
214 hi PopupColor ctermbg=lightblue
215 let winid = popup_create([
216 \ '#include <stdio.h>',
217 \ 'int main(void)',
218 \ '{',
219 \ ' printf(123);',
220 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200221 \], #{line: 3, col: 25, highlight: 'PopupColor'})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200222 call win_execute(winid, 'set syntax=cpp')
223 END
224 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200225 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200226 call VerifyScreenDump(buf, 'Test_popupwin_10', {})
227
228 " clean up
229 call StopVimInTerminal(buf)
230 call delete('XtestPopup')
231endfunc
232
233func Test_popup_with_syntax_setbufvar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200234 CheckScreendump
235
Bram Moolenaar402502d2019-05-30 22:07:36 +0200236 let lines =<< trim END
237 call setline(1, range(1, 100))
238 hi PopupColor ctermbg=lightgrey
239 let winid = popup_create([
240 \ '#include <stdio.h>',
241 \ 'int main(void)',
242 \ '{',
Bram Moolenaare089c3f2019-07-09 20:25:25 +0200243 \ "\tprintf(567);",
Bram Moolenaar402502d2019-05-30 22:07:36 +0200244 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200245 \], #{line: 3, col: 21, highlight: 'PopupColor'})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200246 call setbufvar(winbufnr(winid), '&syntax', 'cpp')
247 END
248 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200249 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200250 call VerifyScreenDump(buf, 'Test_popupwin_11', {})
251
252 " clean up
253 call StopVimInTerminal(buf)
254 call delete('XtestPopup')
255endfunc
256
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200257func Test_popup_with_matches()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200258 CheckScreendump
259
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200260 let lines =<< trim END
261 call setline(1, ['111 222 333', '444 555 666'])
262 let winid = popup_create([
263 \ '111 222 333',
264 \ '444 555 666',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200265 \], #{line: 3, col: 10, border: []})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200266 set hlsearch
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100267 hi VeryBlue ctermfg=blue guifg=blue
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200268 /666
269 call matchadd('ErrorMsg', '111')
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100270 call matchadd('VeryBlue', '444')
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200271 call win_execute(winid, "call matchadd('ErrorMsg', '111')")
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100272 call win_execute(winid, "call matchadd('VeryBlue', '555')")
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200273 END
274 call writefile(lines, 'XtestPopupMatches')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200275 let buf = RunVimInTerminal('-S XtestPopupMatches', #{rows: 10})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200276 call VerifyScreenDump(buf, 'Test_popupwin_matches', {})
277
278 " clean up
279 call StopVimInTerminal(buf)
280 call delete('XtestPopupMatches')
281endfunc
282
Bram Moolenaar399d8982019-06-02 15:34:29 +0200283func Test_popup_all_corners()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200284 CheckScreendump
285
Bram Moolenaar399d8982019-06-02 15:34:29 +0200286 let lines =<< trim END
287 call setline(1, repeat([repeat('-', 60)], 15))
288 set so=0
289 normal 2G3|r#
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200290 let winid1 = popup_create(['first', 'second'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200291 \ line: 'cursor+1',
292 \ col: 'cursor',
293 \ pos: 'topleft',
294 \ border: [],
295 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200296 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200297 normal 24|r@
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200298 let winid1 = popup_create(['First', 'SeconD'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200299 \ line: 'cursor+1',
300 \ col: 'cursor',
301 \ pos: 'topright',
302 \ border: [],
303 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200304 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200305 normal 9G27|r%
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200306 let winid1 = popup_create(['fiRSt', 'seCOnd'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200307 \ line: 'cursor-1',
308 \ col: 'cursor',
309 \ pos: 'botleft',
310 \ border: [],
311 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200312 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200313 normal 48|r&
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200314 let winid1 = popup_create(['FIrsT', 'SEcoND'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200315 \ line: 'cursor-1',
316 \ col: 'cursor',
317 \ pos: 'botright',
318 \ border: [],
319 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200320 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200321 normal 1G51|r*
322 let winid1 = popup_create(['one', 'two'], #{
323 \ line: 'cursor-1',
324 \ col: 'cursor',
325 \ pos: 'botleft',
326 \ border: [],
327 \ padding: [],
328 \ })
Bram Moolenaar399d8982019-06-02 15:34:29 +0200329 END
330 call writefile(lines, 'XtestPopupCorners')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200331 let buf = RunVimInTerminal('-S XtestPopupCorners', #{rows: 12})
Bram Moolenaar399d8982019-06-02 15:34:29 +0200332 call VerifyScreenDump(buf, 'Test_popupwin_corners', {})
333
334 " clean up
335 call StopVimInTerminal(buf)
336 call delete('XtestPopupCorners')
337endfunc
338
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100339func Test_popup_nospace()
340 CheckScreendump
341
342 let lines =<< trim END
343 call setline(1, repeat([repeat('-', 60)], 15))
344 set so=0
345
346 " cursor in a line in top half, using "botleft" with popup that
347 " does fit
348 normal 5G2|r@
349 let winid1 = popup_create(['one', 'two'], #{
350 \ line: 'cursor-1',
351 \ col: 'cursor',
352 \ pos: 'botleft',
353 \ border: [],
354 \ })
355 " cursor in a line in top half, using "botleft" with popup that
356 " doesn't fit: gets truncated
357 normal 5G9|r#
358 let winid1 = popup_create(['one', 'two', 'tee'], #{
359 \ line: 'cursor-1',
360 \ col: 'cursor',
361 \ pos: 'botleft',
362 \ posinvert: 0,
363 \ border: [],
364 \ })
365 " cursor in a line in top half, using "botleft" with popup that
366 " doesn't fit and 'posinvert' set: flips to below.
367 normal 5G16|r%
368 let winid1 = popup_create(['one', 'two', 'tee'], #{
369 \ line: 'cursor-1',
370 \ col: 'cursor',
371 \ pos: 'botleft',
372 \ border: [],
373 \ })
374 " cursor in a line in bottom half, using "botleft" with popup that
375 " doesn't fit: does not flip.
376 normal 8G23|r*
377 let winid1 = popup_create(['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff'], #{
378 \ line: 'cursor-1',
379 \ col: 'cursor',
380 \ pos: 'botleft',
381 \ border: [],
382 \ })
383
384 " cursor in a line in bottom half, using "topleft" with popup that
385 " does fit
386 normal 8G30|r@
387 let winid1 = popup_create(['one', 'two'], #{
388 \ line: 'cursor+1',
389 \ col: 'cursor',
390 \ pos: 'topleft',
391 \ border: [],
392 \ })
393 " cursor in a line in top half, using "topleft" with popup that
394 " doesn't fit: truncated
395 normal 8G37|r#
396 let winid1 = popup_create(['one', 'two', 'tee'], #{
397 \ line: 'cursor+1',
398 \ col: 'cursor',
399 \ pos: 'topleft',
400 \ posinvert: 0,
401 \ border: [],
402 \ })
403 " cursor in a line in top half, using "topleft" with popup that
Bram Moolenaar5c6b6182019-11-10 17:51:38 +0100404 " doesn't fit and "posinvert" set: flips to above.
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100405 normal 8G44|r%
Bram Moolenaar5c6b6182019-11-10 17:51:38 +0100406 let winid1 = popup_create(['one', 'two', 'tee', 'fou', 'fiv'], #{
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100407 \ line: 'cursor+1',
408 \ col: 'cursor',
409 \ pos: 'topleft',
410 \ border: [],
411 \ })
412 " cursor in a line in top half, using "topleft" with popup that
413 " doesn't fit: does not flip.
414 normal 5G51|r*
415 let winid1 = popup_create(['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff'], #{
416 \ line: 'cursor+1',
417 \ col: 'cursor',
418 \ pos: 'topleft',
419 \ border: [],
420 \ })
421 END
422 call writefile(lines, 'XtestPopupNospace')
423 let buf = RunVimInTerminal('-S XtestPopupNospace', #{rows: 12})
424 call VerifyScreenDump(buf, 'Test_popupwin_nospace', {})
425
426 " clean up
427 call StopVimInTerminal(buf)
428 call delete('XtestPopupNospace')
429endfunc
430
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200431func Test_popup_firstline_dump()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200432 CheckScreendump
433
Bram Moolenaar8d241042019-06-12 23:40:01 +0200434 let lines =<< trim END
435 call setline(1, range(1, 20))
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200436 let winid = popup_create(['1111', '222222', '33333', '44', '5', '666666', '77777', '888', '9999999999999999'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200437 \ maxheight: 4,
438 \ firstline: 3,
Bram Moolenaar8d241042019-06-12 23:40:01 +0200439 \ })
440 END
441 call writefile(lines, 'XtestPopupFirstline')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200442 let buf = RunVimInTerminal('-S XtestPopupFirstline', #{rows: 10})
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200443 call VerifyScreenDump(buf, 'Test_popupwin_firstline_1', {})
444
445 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: -1})\<CR>")
446 call term_sendkeys(buf, ":\<CR>")
447 call VerifyScreenDump(buf, 'Test_popupwin_firstline_2', {})
Bram Moolenaar8d241042019-06-12 23:40:01 +0200448
449 " clean up
450 call StopVimInTerminal(buf)
451 call delete('XtestPopupFirstline')
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200452endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +0200453
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200454func Test_popup_firstline()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200455 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200456 \ maxheight: 2,
457 \ firstline: 3,
Bram Moolenaarae943152019-06-16 22:54:14 +0200458 \ })
459 call assert_equal(3, popup_getoptions(winid).firstline)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200460 call popup_setoptions(winid, #{firstline: 1})
Bram Moolenaarae943152019-06-16 22:54:14 +0200461 call assert_equal(1, popup_getoptions(winid).firstline)
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200462 eval winid->popup_close()
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200463
464 let winid = popup_create(['xxx']->repeat(50), #{
465 \ maxheight: 3,
466 \ firstline: 11,
467 \ })
468 redraw
469 call assert_equal(11, popup_getoptions(winid).firstline)
470 call assert_equal(11, popup_getpos(winid).firstline)
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +0200471 " check line() works with popup window
472 call assert_equal(11, line('.', winid))
473 call assert_equal(50, line('$', winid))
474 call assert_equal(0, line('$', 123456))
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200475
476 " Normal command changes what is displayed but not "firstline"
477 call win_execute(winid, "normal! \<c-y>")
478 call assert_equal(11, popup_getoptions(winid).firstline)
479 call assert_equal(10, popup_getpos(winid).firstline)
480
481 " Making some property change applies "firstline" again
482 call popup_setoptions(winid, #{line: 4})
483 call assert_equal(11, popup_getoptions(winid).firstline)
484 call assert_equal(11, popup_getpos(winid).firstline)
485
486 " Remove "firstline" property and scroll
487 call popup_setoptions(winid, #{firstline: 0})
488 call win_execute(winid, "normal! \<c-y>")
489 call assert_equal(0, popup_getoptions(winid).firstline)
490 call assert_equal(10, popup_getpos(winid).firstline)
491
492 " Making some property change has no side effect
493 call popup_setoptions(winid, #{line: 3})
494 call assert_equal(0, popup_getoptions(winid).firstline)
495 call assert_equal(10, popup_getpos(winid).firstline)
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200496 call popup_close(winid)
Bram Moolenaarae943152019-06-16 22:54:14 +0200497
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100498 " CTRL-D scrolls down half a page
499 let winid = popup_create(['xxx']->repeat(50), #{
500 \ maxheight: 8,
501 \ })
502 redraw
503 call assert_equal(1, popup_getpos(winid).firstline)
504 call win_execute(winid, "normal! \<C-D>")
505 call assert_equal(5, popup_getpos(winid).firstline)
506 call win_execute(winid, "normal! \<C-D>")
507 call assert_equal(9, popup_getpos(winid).firstline)
508 call win_execute(winid, "normal! \<C-U>")
509 call assert_equal(5, popup_getpos(winid).firstline)
510
511 call win_execute(winid, "normal! \<C-F>")
512 call assert_equal(11, popup_getpos(winid).firstline)
513 call win_execute(winid, "normal! \<C-B>")
514 call assert_equal(5, popup_getpos(winid).firstline)
515
Bram Moolenaarae943152019-06-16 22:54:14 +0200516 call popup_close(winid)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200517endfunc
518
Bram Moolenaara112f2d2019-09-01 17:38:09 +0200519func Test_popup_noscrolloff()
520 set scrolloff=5
521 let winid = popup_create(['xxx']->repeat(50), #{
522 \ maxheight: 5,
523 \ firstline: 11,
524 \ })
525 redraw
526 call assert_equal(11, popup_getoptions(winid).firstline)
527 call assert_equal(11, popup_getpos(winid).firstline)
528
529 call popup_setoptions(winid, #{firstline: 0})
530 call win_execute(winid, "normal! \<c-y>")
531 call assert_equal(0, popup_getoptions(winid).firstline)
532 call assert_equal(10, popup_getpos(winid).firstline)
533
534 call popup_close(winid)
535endfunc
536
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200537func Test_popup_drag()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200538 CheckScreendump
539
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200540 " create a popup that covers the command line
541 let lines =<< trim END
542 call setline(1, range(1, 20))
Bram Moolenaar356375f2019-08-24 14:46:29 +0200543 split
544 vsplit
545 $wincmd w
546 vsplit
547 1wincmd w
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200548 let winid = popup_create(['1111', '222222', '33333'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200549 \ drag: 1,
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200550 \ resize: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200551 \ border: [],
552 \ line: &lines - 4,
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200553 \ })
554 func Dragit()
555 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
556 endfunc
557 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
Bram Moolenaar356375f2019-08-24 14:46:29 +0200558 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2 - 20)<CR>
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200559 func Resize()
560 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
561 endfunc
Bram Moolenaar356375f2019-08-24 14:46:29 +0200562 map <silent> <F5> :call test_setmouse(6, 21)<CR>
563 map <silent> <F6> :call test_setmouse(7, 25)<CR>
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200564 END
565 call writefile(lines, 'XtestPopupDrag')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200566 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200567 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
568
569 call term_sendkeys(buf, ":call Dragit()\<CR>")
570 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
571
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200572 call term_sendkeys(buf, ":call Resize()\<CR>")
573 call VerifyScreenDump(buf, 'Test_popupwin_drag_03', {})
574
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200575 " clean up
576 call StopVimInTerminal(buf)
577 call delete('XtestPopupDrag')
578endfunc
579
Bram Moolenaar35910f22020-07-12 19:24:10 +0200580func Test_popup_drag_termwin()
581 CheckUnix
582 CheckScreendump
583 CheckFeature terminal
584
585 " create a popup that covers the terminal window
586 let lines =<< trim END
587 set shell=/bin/sh noruler
Bram Moolenaara4dc6f92020-07-12 19:52:36 +0200588 let $PS1 = 'vim> '
Bram Moolenaar35910f22020-07-12 19:24:10 +0200589 terminal
590 $wincmd w
591 let winid = popup_create(['1111', '2222'], #{
592 \ drag: 1,
593 \ resize: 1,
594 \ border: [],
595 \ line: 3,
596 \ })
597 func Dragit()
598 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
599 endfunc
600 map <silent> <F3> :call test_setmouse(3, &columns / 2)<CR>
601 map <silent> <F4> :call test_setmouse(3, &columns / 2 - 20)<CR>
602 END
603 call writefile(lines, 'XtestPopupTerm')
604 let buf = RunVimInTerminal('-S XtestPopupTerm', #{rows: 10})
605 call VerifyScreenDump(buf, 'Test_popupwin_term_01', {})
606
607 call term_sendkeys(buf, ":call Dragit()\<CR>")
608 call VerifyScreenDump(buf, 'Test_popupwin_term_02', {})
609
610 " clean up
611 call StopVimInTerminal(buf)
612 call delete('XtestPopupTerm')
613endfunc
614
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200615func Test_popup_close_with_mouse()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200616 CheckScreendump
617
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200618 let lines =<< trim END
619 call setline(1, range(1, 20))
620 " With border, can click on X
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200621 let winid = popup_create('foobar', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200622 \ close: 'button',
623 \ border: [],
624 \ line: 1,
625 \ col: 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200626 \ })
627 func CloseMsg(id, result)
628 echomsg 'Popup closed with ' .. a:result
629 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200630 let winid = popup_create('notification', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200631 \ close: 'click',
632 \ line: 3,
633 \ col: 15,
634 \ callback: 'CloseMsg',
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200635 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200636 let winid = popup_create('no border here', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200637 \ close: 'button',
638 \ line: 5,
639 \ col: 3,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200640 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200641 let winid = popup_create('only padding', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200642 \ close: 'button',
643 \ padding: [],
644 \ line: 5,
645 \ col: 23,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200646 \ })
647 func CloseWithX()
648 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
649 endfunc
650 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
651 func CloseWithClick()
652 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
653 endfunc
654 map <silent> <F4> :call test_setmouse(3, 17)<CR>
Bram Moolenaarf6396232019-08-24 19:36:00 +0200655 func CreateWithMenuFilter()
656 let winid = popup_create('barfoo', #{
657 \ close: 'button',
658 \ filter: 'popup_filter_menu',
659 \ border: [],
660 \ line: 1,
661 \ col: 40,
662 \ })
663 endfunc
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200664 END
665 call writefile(lines, 'XtestPopupClose')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200666 let buf = RunVimInTerminal('-S XtestPopupClose', #{rows: 10})
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200667 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
668
669 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
670 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
671
672 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
673 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
674
Bram Moolenaarf6396232019-08-24 19:36:00 +0200675 call term_sendkeys(buf, ":call CreateWithMenuFilter()\<CR>")
676 call VerifyScreenDump(buf, 'Test_popupwin_close_04', {})
677
678 " We have to send the actual mouse code, feedkeys() would be caught the
679 " filter.
680 call term_sendkeys(buf, "\<Esc>[<0;47;1M")
681 call VerifyScreenDump(buf, 'Test_popupwin_close_05', {})
682
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200683 " clean up
684 call StopVimInTerminal(buf)
685 call delete('XtestPopupClose')
686endfunction
687
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200688func Test_popup_menu_wrap()
689 CheckScreendump
690
691 let lines =<< trim END
692 call setline(1, range(1, 20))
693 call popup_create([
694 \ 'one',
695 \ 'asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfas',
696 \ 'three',
697 \ 'four',
698 \ ], #{
699 \ pos: "botleft",
700 \ border: [],
701 \ padding: [0,1,0,1],
702 \ maxheight: 3,
703 \ cursorline: 1,
704 \ filter: 'popup_filter_menu',
705 \ })
706 END
707 call writefile(lines, 'XtestPopupWrap')
708 let buf = RunVimInTerminal('-S XtestPopupWrap', #{rows: 10})
709 call VerifyScreenDump(buf, 'Test_popupwin_wrap_1', {})
710
711 call term_sendkeys(buf, "jj")
712 call VerifyScreenDump(buf, 'Test_popupwin_wrap_2', {})
713
714 " clean up
715 call term_sendkeys(buf, "\<Esc>")
716 call StopVimInTerminal(buf)
717 call delete('XtestPopupWrap')
718endfunction
719
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200720func Test_popup_with_mask()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200721 CheckScreendump
722
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200723 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200724 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200725 hi PopupColor ctermbg=lightgrey
726 let winid = popup_create([
727 \ 'some text',
728 \ 'another line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200729 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200730 \ line: 1,
731 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100732 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200733 \ wrap: 0,
734 \ fixed: 1,
735 \ zindex: 90,
736 \ padding: [],
737 \ highlight: 'PopupColor',
738 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200739 call popup_create([
740 \ 'xxxxxxxxx',
741 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200742 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200743 \ line: 3,
744 \ col: 18,
745 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200746 let winidb = popup_create([
747 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200748 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200749 \ line: 7,
750 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100751 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200752 \ wrap: 0,
753 \ fixed: 1,
754 \ close: 'button',
755 \ zindex: 90,
756 \ padding: [],
757 \ border: [],
758 \ 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 +0200759 END
760 call writefile(lines, 'XtestPopupMask')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200761 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200762 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
763
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200764 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
765 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200766 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200767 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
768
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200769 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
770 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200771 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200772 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
773
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200774 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
775 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200776 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200777 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
778
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200779 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
780 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200781 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200782 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
783
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200784 " clean up
785 call StopVimInTerminal(buf)
786 call delete('XtestPopupMask')
787endfunc
788
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200789func Test_popup_select()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200790 CheckScreendump
791 CheckFeature clipboard_working
792
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200793 " create a popup with some text to be selected
794 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200795 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200796 call setline(1, range(1, 20))
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200797 let winid = popup_create(['the word', 'some more', 'several words here', 'invisible', '5', '6', '7'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200798 \ drag: 1,
799 \ border: [],
800 \ line: 3,
801 \ col: 10,
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200802 \ maxheight: 3,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200803 \ })
804 func Select1()
805 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
806 endfunc
807 map <silent> <F3> :call test_setmouse(4, 15)<CR>
808 map <silent> <F4> :call test_setmouse(6, 23)<CR>
809 END
810 call writefile(lines, 'XtestPopupSelect')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200811 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200812 call term_sendkeys(buf, ":call Select1()\<CR>")
813 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
814
815 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
816 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200817 " clean the command line, sometimes it still shows a command
818 call term_sendkeys(buf, ":\<esc>")
819
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200820 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
821
822 " clean up
823 call StopVimInTerminal(buf)
824 call delete('XtestPopupSelect')
825endfunc
826
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200827func Test_popup_in_tab()
828 " default popup is local to tab, not visible when in other tab
829 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200830 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200831 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200832 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200833 tabnew
834 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200835 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200836 quit
837 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200838
839 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200840 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200841 " buffer is gone now
842 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200843
844 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200845 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200846 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200847 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200848 tabnew
849 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200850 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200851 quit
852 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200853 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200854
855 " create popup in other tab
856 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200857 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200858 call assert_equal(0, popup_getpos(winid).visible)
859 call assert_equal(1, popup_getoptions(winid).tabpage)
860 quit
861 call assert_equal(1, popup_getpos(winid).visible)
862 call assert_equal(0, popup_getoptions(winid).tabpage)
863 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200864endfunc
865
866func Test_popup_valid_arguments()
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200867 call assert_equal(0, len(popup_list()))
868
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200869 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200870 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200871 let pos = popup_getpos(winid)
872 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200873 call assert_equal([winid], popup_list())
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200874 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200875
876 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200877 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200878 let pos = popup_getpos(winid)
879 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200880 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200881
882 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200883 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200884 let pos = popup_getpos(winid)
885 let around = (&columns - pos.width) / 2
886 call assert_inrange(around - 1, around + 1, pos.col)
887 let around = (&lines - pos.height) / 2
888 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200889 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200890endfunc
891
892func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200893 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200894 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200895 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200896 call popup_clear()
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200897 call assert_fails('call popup_create(test_null_string(), {})', 'E450:')
898 call assert_fails('call popup_create(test_null_list(), {})', 'E450:')
899 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200900
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200901 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200902 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200903 call assert_fails('call popup_create("text", #{col: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200904 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200905 call assert_fails('call popup_create("text", #{col: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200906 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200907 call assert_fails('call popup_create("text", #{col: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200908 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200909
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200910 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200911 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200912 call assert_fails('call popup_create("text", #{line: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200913 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200914 call assert_fails('call popup_create("text", #{line: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200915 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200916 call assert_fails('call popup_create("text", #{line: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200917 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200918
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200919 call assert_fails('call popup_create("text", #{pos: "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200920 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200921 call assert_fails('call popup_create("text", #{padding: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200922 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200923 call assert_fails('call popup_create("text", #{border: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200924 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200925 call assert_fails('call popup_create("text", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200926 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +0200927 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
928 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200929 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200930 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200931
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200932 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200933 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200934 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200935 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200936 call assert_fails('call popup_create([#{text: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200937 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +0100938 call assert_fails('call popup_create([#{text: "text", props: range(3)}], {})', 'E715:')
939 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200940 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
941 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +0100942 call assert_fails('call popup_create("text", #{mask: range(5)})', 'E475:')
943 call popup_clear()
944 call popup_create("text", #{mask: [range(4)]})
945 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200946 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200947 call assert_fails('call popup_create("text", #{mapping: []})', 'E745:')
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200948 call popup_clear()
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200949 call assert_fails('call popup_create("text", #{tabpage : 4})', 'E997:')
950 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200951endfunc
952
Bram Moolenaareea16992019-05-31 17:34:48 +0200953func Test_win_execute_closing_curwin()
954 split
955 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200956 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200957 call popup_clear()
Bram Moolenaar49540192019-12-11 19:34:54 +0100958
959 let winid = popup_create('some text', {})
960 call assert_fails('call win_execute(winid, printf("normal! :\<C-u>call popup_close(%d)\<CR>", winid))', 'E994')
961 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200962endfunc
963
964func Test_win_execute_not_allowed()
965 let winid = popup_create('some text', {})
966 call assert_fails('call win_execute(winid, "split")', 'E994:')
967 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
968 call assert_fails('call win_execute(winid, "close")', 'E994:')
969 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200970 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200971 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
972 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
973 call assert_fails('call win_execute(winid, "next")', 'E994:')
974 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
Bram Moolenaar3a2505c2020-03-09 16:40:41 +0100975 call assert_fails('call win_execute(winid, "pedit filename")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200976 call assert_fails('call win_execute(winid, "buf")', 'E994:')
Bram Moolenaar3029bcc2020-01-18 15:06:19 +0100977 call assert_fails('call win_execute(winid, "bnext")', 'E994:')
978 call assert_fails('call win_execute(winid, "bprev")', 'E994:')
979 call assert_fails('call win_execute(winid, "bfirst")', 'E994:')
980 call assert_fails('call win_execute(winid, "blast")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200981 call assert_fails('call win_execute(winid, "edit")', 'E994:')
982 call assert_fails('call win_execute(winid, "enew")', 'E994:')
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +0200983 call assert_fails('call win_execute(winid, "help")', 'E994:')
984 call assert_fails('call win_execute(winid, "1only")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200985 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
986 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
987 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
988 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200989 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200990endfunc
991
Bram Moolenaar402502d2019-05-30 22:07:36 +0200992func Test_popup_with_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200993 CheckScreendump
994
Bram Moolenaar402502d2019-05-30 22:07:36 +0200995 let lines =<< trim END
996 call setline(1, range(1, 100))
997 let winid = popup_create(
998 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200999 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001000 END
1001 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001002 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001003 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
1004
1005 " clean up
1006 call StopVimInTerminal(buf)
1007 call delete('XtestPopup')
1008endfunc
1009
1010func Test_popup_without_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001011 CheckScreendump
1012
Bram Moolenaar402502d2019-05-30 22:07:36 +02001013 let lines =<< trim END
1014 call setline(1, range(1, 100))
1015 let winid = popup_create(
1016 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001017 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001018 END
1019 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001020 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001021 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
1022
1023 " clean up
1024 call StopVimInTerminal(buf)
1025 call delete('XtestPopup')
1026endfunc
1027
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001028func Test_popup_with_showbreak()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001029 CheckScreendump
1030
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001031 let lines =<< trim END
1032 set showbreak=>>\
1033 call setline(1, range(1, 20))
1034 let winid = popup_dialog(
Bram Moolenaar8ae54372019-09-15 18:11:16 +02001035 \ 'a long line here that wraps',
1036 \ #{filter: 'popup_filter_yesno',
1037 \ maxwidth: 12})
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001038 END
1039 call writefile(lines, 'XtestPopupShowbreak')
1040 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
1041 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
1042
1043 " clean up
1044 call term_sendkeys(buf, "y")
1045 call StopVimInTerminal(buf)
1046 call delete('XtestPopupShowbreak')
1047endfunc
1048
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001049func Test_popup_time()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001050 CheckFeature timers
1051
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001052 topleft vnew
1053 call setline(1, 'hello')
1054
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001055 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001056 \ line: 1,
1057 \ col: 1,
1058 \ minwidth: 20,
1059 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001060 \})
1061 redraw
1062 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1063 call assert_equal('world', line)
1064
Bram Moolenaarb4f06282019-07-12 21:07:54 +02001065 call assert_equal(winid, popup_locate(1, 1))
1066 call assert_equal(winid, popup_locate(1, 20))
1067 call assert_equal(0, popup_locate(1, 21))
1068 call assert_equal(0, popup_locate(2, 1))
1069
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001070 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +02001071 redraw
Bram Moolenaar196b4662019-09-06 21:34:30 +02001072 let line = join(map(range(1, 5), '1->screenstring(v:val)'), '')
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001073 call assert_equal('hello', line)
1074
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001075 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001076 \ line: &lines,
1077 \ col: 10,
1078 \ minwidth: 20,
1079 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001080 \})
1081 redraw
1082 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1083 call assert_match('.*on the command line.*', line)
1084
1085 sleep 700m
1086 redraw
1087 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1088 call assert_notmatch('.*on the command line.*', line)
1089
1090 bwipe!
1091endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001092
1093func Test_popup_hide()
1094 topleft vnew
1095 call setline(1, 'hello')
1096
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001097 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001098 \ line: 1,
1099 \ col: 1,
1100 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001101 \})
1102 redraw
1103 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1104 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001105 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001106 " buffer is still listed and active
1107 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001108
1109 call popup_hide(winid)
1110 redraw
1111 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1112 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001113 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001114 " buffer is still listed but hidden
Bram Moolenaarca7c0782020-01-14 20:42:48 +01001115 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001116
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001117 eval winid->popup_show()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001118 redraw
1119 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1120 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001121 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001122
1123
1124 call popup_close(winid)
1125 redraw
1126 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1127 call assert_equal('hello', line)
1128
1129 " error is given for existing non-popup window
1130 call assert_fails('call popup_hide(win_getid())', 'E993:')
1131
1132 " no error non-existing window
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001133 eval 1234234->popup_hide()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001134 call popup_show(41234234)
1135
1136 bwipe!
1137endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001138
1139func Test_popup_move()
1140 topleft vnew
1141 call setline(1, 'hello')
1142
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001143 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001144 \ line: 1,
1145 \ col: 1,
1146 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001147 \})
1148 redraw
1149 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1150 call assert_equal('world ', line)
1151
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001152 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001153 redraw
1154 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1155 call assert_equal('hello ', line)
1156 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
1157 call assert_equal('~world', line)
1158
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001159 eval winid->popup_move(#{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001160 redraw
1161 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1162 call assert_equal('hworld', line)
1163
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001164 call assert_fails('call popup_move(winid, [])', 'E715:')
1165 call assert_fails('call popup_move(winid, test_null_dict())', 'E715:')
1166
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001167 call popup_close(winid)
1168
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001169 call assert_equal(0, popup_move(-1, {}))
1170
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001171 bwipe!
1172endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +02001173
Bram Moolenaar402502d2019-05-30 22:07:36 +02001174func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001175 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001176 \ line: 2,
1177 \ col: 3,
1178 \ minwidth: 10,
1179 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +02001180 \})
1181 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001182 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001183 call assert_equal(2, res.line)
1184 call assert_equal(3, res.col)
1185 call assert_equal(10, res.width)
1186 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001187 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001188
1189 call popup_close(winid)
1190endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001191
1192func Test_popup_width_longest()
1193 let tests = [
1194 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
1195 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
1196 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
1197 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
1198 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
1199 \ ]
1200
1201 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001202 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001203 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001204 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001205 call assert_equal(test[1], position.width)
1206 call popup_close(winid)
1207 endfor
1208endfunc
1209
1210func Test_popup_wraps()
1211 let tests = [
1212 \ ['nowrap', 6, 1],
1213 \ ['a line that wraps once', 12, 2],
1214 \ ['a line that wraps two times', 12, 3],
1215 \ ]
1216 for test in tests
1217 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001218 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001219 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001220 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001221 call assert_equal(test[1], position.width)
1222 call assert_equal(test[2], position.height)
1223
1224 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001225 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001226 endfor
1227endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001228
1229func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001230 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001231 \ line: 2,
1232 \ col: 3,
1233 \ minwidth: 10,
1234 \ minheight: 11,
1235 \ maxwidth: 20,
1236 \ maxheight: 21,
1237 \ zindex: 100,
1238 \ time: 5000,
1239 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001240 \})
1241 redraw
1242 let res = popup_getoptions(winid)
1243 call assert_equal(2, res.line)
1244 call assert_equal(3, res.col)
1245 call assert_equal(10, res.minwidth)
1246 call assert_equal(11, res.minheight)
1247 call assert_equal(20, res.maxwidth)
1248 call assert_equal(21, res.maxheight)
1249 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001250 call assert_equal(1, res.fixed)
Bram Moolenaarb8350ab2019-08-04 17:59:49 +02001251 call assert_equal(1, res.mapping)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001252 if has('timers')
1253 call assert_equal(5000, res.time)
1254 endif
1255 call popup_close(winid)
1256
1257 let winid = popup_create('hello', {})
1258 redraw
1259 let res = popup_getoptions(winid)
1260 call assert_equal(0, res.line)
1261 call assert_equal(0, res.col)
1262 call assert_equal(0, res.minwidth)
1263 call assert_equal(0, res.minheight)
1264 call assert_equal(0, res.maxwidth)
1265 call assert_equal(0, res.maxheight)
1266 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001267 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001268 if has('timers')
1269 call assert_equal(0, res.time)
1270 endif
1271 call popup_close(winid)
1272 call assert_equal({}, popup_getoptions(winid))
1273endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001274
1275func Test_popup_option_values()
1276 new
1277 " window-local
1278 setlocal number
1279 setlocal nowrap
1280 " buffer-local
1281 setlocal omnifunc=Something
1282 " global/buffer-local
1283 setlocal path=/there
1284 " global/window-local
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001285 setlocal statusline=2
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001286
1287 let winid = popup_create('hello', {})
1288 call assert_equal(0, getwinvar(winid, '&number'))
1289 call assert_equal(1, getwinvar(winid, '&wrap'))
1290 call assert_equal('', getwinvar(winid, '&omnifunc'))
1291 call assert_equal(&g:path, getwinvar(winid, '&path'))
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001292 call assert_equal(&g:statusline, getwinvar(winid, '&statusline'))
1293
1294 " 'scrolloff' is reset to zero
1295 call assert_equal(5, &scrolloff)
1296 call assert_equal(0, getwinvar(winid, '&scrolloff'))
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001297
1298 call popup_close(winid)
1299 bwipe
1300endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001301
1302func Test_popup_atcursor()
1303 topleft vnew
1304 call setline(1, [
1305 \ 'xxxxxxxxxxxxxxxxx',
1306 \ 'xxxxxxxxxxxxxxxxx',
1307 \ 'xxxxxxxxxxxxxxxxx',
1308 \])
1309
1310 call cursor(2, 2)
1311 redraw
1312 let winid = popup_atcursor('vim', {})
1313 redraw
1314 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1315 call assert_equal('xvimxxxxxxxxxxxxx', line)
1316 call popup_close(winid)
1317
1318 call cursor(3, 4)
1319 redraw
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001320 let winid = 'vim'->popup_atcursor({})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001321 redraw
1322 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
1323 call assert_equal('xxxvimxxxxxxxxxxx', line)
1324 call popup_close(winid)
1325
1326 call cursor(1, 1)
1327 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001328 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001329 \ line: 'cursor+2',
1330 \ col: 'cursor+1',
1331 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001332 redraw
1333 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1334 call assert_equal('xvimxxxxxxxxxxxxx', line)
1335 call popup_close(winid)
1336
1337 call cursor(3, 3)
1338 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001339 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001340 \ line: 'cursor-2',
1341 \ col: 'cursor-1',
1342 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001343 redraw
1344 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1345 call assert_equal('xvimxxxxxxxxxxxxx', line)
1346 call popup_close(winid)
1347
Bram Moolenaar402502d2019-05-30 22:07:36 +02001348 " just enough room above
1349 call cursor(3, 3)
1350 redraw
1351 let winid = popup_atcursor(['vim', 'is great'], {})
1352 redraw
1353 let pos = popup_getpos(winid)
1354 call assert_equal(1, pos.line)
1355 call popup_close(winid)
1356
1357 " not enough room above, popup goes below the cursor
1358 call cursor(3, 3)
1359 redraw
1360 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1361 redraw
1362 let pos = popup_getpos(winid)
1363 call assert_equal(4, pos.line)
1364 call popup_close(winid)
1365
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001366 " cursor in first line, popup in line 2
1367 call cursor(1, 1)
1368 redraw
1369 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1370 redraw
1371 let pos = popup_getpos(winid)
1372 call assert_equal(2, pos.line)
1373 call popup_close(winid)
1374
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001375 bwipe!
1376endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001377
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001378func Test_popup_atcursor_pos()
1379 CheckScreendump
1380
1381 let lines =<< trim END
1382 call setline(1, repeat([repeat('-', 60)], 15))
1383 set so=0
1384
1385 normal 9G3|r#
1386 let winid1 = popup_atcursor(['first', 'second'], #{
1387 \ moved: [0, 0, 0],
1388 \ })
1389 normal 9G21|r&
1390 let winid1 = popup_atcursor(['FIrsT', 'SEcoND'], #{
1391 \ pos: 'botright',
1392 \ moved: [0, 0, 0],
1393 \ })
1394 normal 3G27|r%
1395 let winid1 = popup_atcursor(['fiRSt', 'seCOnd'], #{
1396 \ pos: 'topleft',
1397 \ moved: [0, 0, 0],
1398 \ })
1399 normal 3G45|r@
1400 let winid1 = popup_atcursor(['First', 'SeconD'], #{
1401 \ pos: 'topright',
Bram Moolenaarb0992022020-01-30 14:55:42 +01001402 \ moved: range(3),
1403 \ mousemoved: range(3),
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001404 \ })
1405 END
1406 call writefile(lines, 'XtestPopupAtcursorPos')
1407 let buf = RunVimInTerminal('-S XtestPopupAtcursorPos', #{rows: 12})
1408 call VerifyScreenDump(buf, 'Test_popupwin_atcursor_pos', {})
1409
1410 " clean up
1411 call StopVimInTerminal(buf)
1412 call delete('XtestPopupAtcursorPos')
1413endfunc
1414
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001415func Test_popup_beval()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001416 CheckScreendump
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001417 CheckFeature balloon_eval_term
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001418
1419 let lines =<< trim END
1420 call setline(1, range(1, 20))
1421 call setline(5, 'here is some text to hover over')
1422 set balloonevalterm
1423 set balloonexpr=BalloonExpr()
1424 set balloondelay=100
1425 func BalloonExpr()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001426 let s:winid = [v:beval_text]->popup_beval({})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001427 return ''
1428 endfunc
1429 func Hover()
1430 call test_setmouse(5, 15)
1431 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1432 sleep 100m
1433 endfunc
1434 func MoveOntoPopup()
1435 call test_setmouse(4, 17)
1436 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1437 endfunc
1438 func MoveAway()
1439 call test_setmouse(5, 13)
1440 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1441 endfunc
1442 END
1443 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001444 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001445 call TermWait(buf, 50)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001446 call term_sendkeys(buf, 'j')
1447 call term_sendkeys(buf, ":call Hover()\<CR>")
1448 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1449
1450 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1451 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1452
1453 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1454 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1455
1456 " clean up
1457 call StopVimInTerminal(buf)
1458 call delete('XtestPopupBeval')
1459endfunc
1460
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001461func Test_popup_filter()
1462 new
1463 call setline(1, 'some text')
1464
1465 func MyPopupFilter(winid, c)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001466 if a:c == 'e' || a:c == "\<F9>"
1467 let g:eaten = a:c
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001468 return 1
1469 endif
1470 if a:c == '0'
1471 let g:ignored = '0'
1472 return 0
1473 endif
1474 if a:c == 'x'
1475 call popup_close(a:winid)
1476 return 1
1477 endif
1478 return 0
1479 endfunc
1480
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001481 let winid = 'something'->popup_create(#{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001482 redraw
1483
1484 " e is consumed by the filter
1485 call feedkeys('e', 'xt')
1486 call assert_equal('e', g:eaten)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001487 call feedkeys("\<F9>", 'xt')
1488 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001489
1490 " 0 is ignored by the filter
1491 normal $
1492 call assert_equal(9, getcurpos()[2])
1493 call feedkeys('0', 'xt')
1494 call assert_equal('0', g:ignored)
1495 call assert_equal(1, getcurpos()[2])
1496
1497 " x closes the popup
1498 call feedkeys('x', 'xt')
Bram Moolenaarec084d32020-02-28 22:44:47 +01001499 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001500 call assert_equal(-1, winbufnr(winid))
1501
1502 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001503 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001504endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001505
Bram Moolenaara42d9452019-06-15 21:46:30 +02001506func ShowDialog(key, result)
1507 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001508 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001509 \ filter: 'popup_filter_yesno',
1510 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001511 \ })
1512 redraw
1513 call feedkeys(a:key, "xt")
1514 call assert_equal(winid, s:cb_winid)
1515 call assert_equal(a:result, s:cb_res)
1516endfunc
1517
1518func Test_popup_dialog()
1519 func QuitCallback(id, res)
1520 let s:cb_winid = a:id
1521 let s:cb_res = a:res
1522 endfunc
1523
1524 let winid = ShowDialog("y", 1)
1525 let winid = ShowDialog("Y", 1)
1526 let winid = ShowDialog("n", 0)
1527 let winid = ShowDialog("N", 0)
1528 let winid = ShowDialog("x", 0)
1529 let winid = ShowDialog("X", 0)
1530 let winid = ShowDialog("\<Esc>", 0)
1531 let winid = ShowDialog("\<C-C>", -1)
1532
1533 delfunc QuitCallback
1534endfunc
1535
Bram Moolenaara730e552019-06-16 19:05:31 +02001536func ShowMenu(key, result)
1537 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001538 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001539 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001540 \ })
1541 redraw
1542 call feedkeys(a:key, "xt")
1543 call assert_equal(winid, s:cb_winid)
1544 call assert_equal(a:result, s:cb_res)
1545endfunc
1546
1547func Test_popup_menu()
1548 func QuitCallback(id, res)
1549 let s:cb_winid = a:id
1550 let s:cb_res = a:res
1551 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001552 " mapping won't be used in popup
1553 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001554
1555 let winid = ShowMenu(" ", 1)
1556 let winid = ShowMenu("j \<CR>", 2)
1557 let winid = ShowMenu("JjK \<CR>", 2)
1558 let winid = ShowMenu("jjjjjj ", 3)
1559 let winid = ShowMenu("kkk ", 1)
1560 let winid = ShowMenu("x", -1)
1561 let winid = ShowMenu("X", -1)
1562 let winid = ShowMenu("\<Esc>", -1)
1563 let winid = ShowMenu("\<C-C>", -1)
1564
1565 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001566 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001567endfunc
1568
1569func Test_popup_menu_screenshot()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001570 CheckScreendump
Bram Moolenaara730e552019-06-16 19:05:31 +02001571
1572 let lines =<< trim END
1573 call setline(1, range(1, 20))
1574 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001575 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001576 func MenuDone(id, res)
1577 echomsg "selected " .. a:res
1578 endfunc
1579 END
1580 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001581 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001582 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1583
1584 call term_sendkeys(buf, "jj")
1585 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1586
1587 call term_sendkeys(buf, " ")
1588 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1589
1590 " clean up
1591 call StopVimInTerminal(buf)
1592 call delete('XtestPopupMenu')
1593endfunc
1594
Bram Moolenaarf914a332019-07-20 15:09:56 +02001595func Test_popup_menu_narrow()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001596 CheckScreendump
Bram Moolenaarf914a332019-07-20 15:09:56 +02001597
1598 let lines =<< trim END
1599 call setline(1, range(1, 20))
1600 hi PopupSelected ctermbg=green
1601 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1602 func MenuDone(id, res)
1603 echomsg "selected " .. a:res
1604 endfunc
1605 END
1606 call writefile(lines, 'XtestPopupNarrowMenu')
1607 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1608 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1609
1610 " clean up
1611 call term_sendkeys(buf, "x")
1612 call StopVimInTerminal(buf)
1613 call delete('XtestPopupNarrowMenu')
1614endfunc
1615
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001616func Test_popup_title()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001617 CheckScreendump
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001618
1619 " Create a popup without title or border, a line of padding will be added to
1620 " put the title on.
1621 let lines =<< trim END
1622 call setline(1, range(1, 20))
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001623 let winid = popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001624 END
1625 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001626 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001627 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1628
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001629 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxwidth: 20, title: 'a very long title that is not going to fit'})\<CR>")
1630 call term_sendkeys(buf, ":\<CR>")
1631 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_1', {})
1632
1633 call term_sendkeys(buf, ":call popup_setoptions(winid, #{border: []})\<CR>")
1634 call term_sendkeys(buf, ":\<CR>")
1635 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_2', {})
1636
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001637 " clean up
1638 call StopVimInTerminal(buf)
1639 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001640
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001641 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001642 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001643 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001644 call assert_equal('Another Title', popup_getoptions(winid).title)
1645
1646 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001647endfunc
1648
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001649func Test_popup_close_callback()
1650 func PopupDone(id, result)
1651 let g:result = a:result
1652 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001653 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001654 redraw
1655 call popup_close(winid, 'done')
1656 call assert_equal('done', g:result)
1657endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001658
1659func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001660 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001661 redraw
1662 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001663 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001664 call assert_equal(5, pos.height)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001665 call popup_close(winid)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001666
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001667 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001668 redraw
1669 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001670 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001671 call assert_equal(3, pos.height)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001672 call popup_close(winid)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001673endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001674
1675func Test_popup_never_behind()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001676 CheckScreendump
1677
Bram Moolenaar988c4332019-06-02 14:12:11 +02001678 " +-----------------------------+
1679 " | | |
1680 " | | |
1681 " | | |
1682 " | line1 |
1683 " |------------line2------------|
1684 " | line3 |
1685 " | line4 |
1686 " | |
1687 " | |
1688 " +-----------------------------+
1689 let lines =<< trim END
Bram Moolenaar988c4332019-06-02 14:12:11 +02001690 split
1691 vsplit
1692 let info_window1 = getwininfo()[0]
1693 let line = info_window1['height']
1694 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001695 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001696 \ line : line,
1697 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001698 \ })
1699 END
1700 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001701 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001702 call term_sendkeys(buf, "\<C-W>w")
1703 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1704
1705 " clean up
1706 call StopVimInTerminal(buf)
1707 call delete('XtestPopupBehind')
1708endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001709
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001710func s:VerifyPosition(p, msg, line, col, width, height)
1711 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1712 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1713 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1714 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001715endfunc
1716
1717func Test_popup_position_adjust()
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001718 " Anything placed past the last cell on the right of the screen is moved to
1719 " the left.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001720 "
1721 " When wrapping is disabled, we also shift to the left to display on the
1722 " screen, unless fixed is set.
1723
1724 " Entries for cases which don't vary based on wrapping.
1725 " Format is per tests described below
1726 let both_wrap_tests = [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001727 \ ['a', 5, &columns, 5, &columns, 1, 1],
1728 \ ['b', 5, &columns + 1, 5, &columns, 1, 1],
1729 \ ['c', 5, &columns - 1, 5, &columns - 1, 1, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001730 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001731 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1]]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001732
1733 " these test groups are dicts with:
1734 " - comment: something to identify the group of tests by
1735 " - options: dict of options to merge with the row/col in tests
1736 " - tests: list of cases. Each one is a list with elements:
1737 " - text
1738 " - row
1739 " - col
1740 " - expected row
1741 " - expected col
1742 " - expected width
1743 " - expected height
1744 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001745 \ #{
1746 \ comment: 'left-aligned with wrapping',
1747 \ options: #{
1748 \ wrap: 1,
1749 \ pos: 'botleft',
1750 \ },
1751 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001752 \ ['aa', 5, &columns, 4, &columns, 1, 2],
1753 \ ['bb', 5, &columns + 1, 4, &columns, 1, 2],
1754 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1755 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1756 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1757 \
1758 \ ['aaa', 5, &columns, 3, &columns, 1, 3],
1759 \ ['bbb', 5, &columns + 1, 3, &columns, 1, 3],
1760 \ ['ccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
1761 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1762 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1763 \
1764 \ ['aaaa', 5, &columns, 2, &columns, 1, 4],
1765 \ ['bbbb', 5, &columns + 1, 2, &columns, 1, 4],
1766 \ ['cccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001767 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
1768 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001769 \ ['eeee', 5, &columns - 4, 5, &columns - 4, 4, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001770 \ ],
1771 \ },
1772 \ #{
1773 \ comment: 'left aligned without wrapping',
1774 \ options: #{
1775 \ wrap: 0,
1776 \ pos: 'botleft',
1777 \ },
1778 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001779 \ ['aa', 5, &columns, 5, &columns - 1, 2, 1],
1780 \ ['bb', 5, &columns + 1, 5, &columns - 1, 2, 1],
1781 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1782 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1783 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1784 \
1785 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
1786 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1787 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1788 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1789 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1790 \
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001791 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
1792 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
1793 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
1794 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
1795 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1796 \ ],
1797 \ },
1798 \ #{
1799 \ comment: 'left aligned with fixed position',
1800 \ options: #{
1801 \ wrap: 0,
1802 \ fixed: 1,
1803 \ pos: 'botleft',
1804 \ },
1805 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001806 \ ['aa', 5, &columns, 5, &columns, 1, 1],
1807 \ ['bb', 5, &columns + 1, 5, &columns, 1, 1],
1808 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1809 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1810 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1811 \
1812 \ ['aaa', 5, &columns, 5, &columns, 1, 1],
1813 \ ['bbb', 5, &columns + 1, 5, &columns, 1, 1],
1814 \ ['ccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1815 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1816 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1817 \
1818 \ ['aaaa', 5, &columns, 5, &columns, 1, 1],
1819 \ ['bbbb', 5, &columns + 1, 5, &columns, 1, 1],
1820 \ ['cccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001821 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1822 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1823 \ ],
1824 \ },
1825 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001826
1827 for test_group in tests
1828 for test in test_group.tests
1829 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001830 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001831 \ line: line,
1832 \ col: col,
1833 \ }
1834 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001835
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001836 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001837
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001838 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001839 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
1840 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001841 endfor
1842 endfor
1843
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001844 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001845 %bwipe!
1846endfunc
1847
Bram Moolenaar3397f742019-06-02 18:40:06 +02001848func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001849 " width of screen
1850 let X = join(map(range(&columns), {->'X'}), '')
1851
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001852 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1853 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001854
1855 redraw
1856 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1857 call assert_equal(X, line)
1858
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001859 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001860 redraw
1861
1862 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001863 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
1864 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001865
1866 redraw
1867 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1868 call assert_equal(X, line)
1869
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001870 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001871 redraw
1872
1873 " Extend so > window width
1874 let X .= 'x'
1875
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001876 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1877 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001878
1879 redraw
1880 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1881 call assert_equal(X[ : -2 ], line)
1882
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001883 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001884 redraw
1885
1886 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001887 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
1888 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001889
1890 redraw
1891 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1892 call assert_equal(X[ : -2 ], line)
1893
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001894 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001895 redraw
1896
1897 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001898 let p = popup_create(X,
1899 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
1900 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001901
1902 redraw
1903 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1904 let e_line = ' ' . X[ 1 : -2 ]
1905 call assert_equal(e_line, line)
1906
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001907 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001908 redraw
1909
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001910 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001911 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001912endfunc
1913
1914func Test_popup_moved()
1915 new
1916 call test_override('char_avail', 1)
1917 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1918
1919 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001920 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001921 redraw
1922 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001923 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001924 " trigger the check for last_cursormoved by going into insert mode
1925 call feedkeys("li\<Esc>", 'xt')
1926 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001927 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001928
1929 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001930 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001931 redraw
1932 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001933 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001934 call feedkeys("hi\<Esc>", 'xt')
1935 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001936 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001937
1938 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001939 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001940 redraw
1941 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001942 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001943 call feedkeys("li\<Esc>", 'xt')
1944 call assert_equal(1, popup_getpos(winid).visible)
1945 call feedkeys("ei\<Esc>", 'xt')
1946 call assert_equal(1, popup_getpos(winid).visible)
1947 call feedkeys("eli\<Esc>", 'xt')
1948 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001949 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001950
Bram Moolenaar17627312019-06-02 19:53:44 +02001951 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001952 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001953 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001954 redraw
1955 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001956 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001957 call feedkeys("eli\<Esc>", 'xt')
1958 call assert_equal(1, popup_getpos(winid).visible)
1959 call feedkeys("wi\<Esc>", 'xt')
1960 call assert_equal(1, popup_getpos(winid).visible)
1961 call feedkeys("Eli\<Esc>", 'xt')
1962 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001963 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001964
1965 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001966 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001967 redraw
1968 call assert_equal(1, popup_getpos(winid).visible)
1969 call feedkeys("eli\<Esc>", 'xt')
1970 call feedkeys("ei\<Esc>", 'xt')
1971 call assert_equal(1, popup_getpos(winid).visible)
1972 call feedkeys("eli\<Esc>", 'xt')
1973 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001974 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001975
1976 bwipe!
1977 call test_override('ALL', 0)
1978endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001979
1980func Test_notifications()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001981 CheckFeature timers
1982 CheckScreendump
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001983
Bram Moolenaar0fdddee2019-09-01 15:26:23 +02001984 let lines =<< trim END
1985 call setline(1, range(1, 20))
1986 hi Notification ctermbg=lightblue
1987 call popup_notification('first notification', {})
1988 END
1989 call writefile(lines, 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001990 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001991 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1992
1993 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001994 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1995 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001996 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1997
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001998 " clean up
1999 call StopVimInTerminal(buf)
2000 call delete('XtestNotifications')
2001endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002002
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002003func Test_popup_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002004 CheckScreendump
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002005
2006 let lines =<< trim END
2007 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02002008 hi ScrollThumb ctermbg=blue
2009 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002010 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002011 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002012 \ minwidth: 8,
2013 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002014 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002015 func ScrollUp()
2016 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
2017 endfunc
2018 func ScrollDown()
2019 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
2020 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002021 func ClickTop()
2022 call feedkeys("\<F4>\<LeftMouse>", "xt")
2023 endfunc
2024 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002025 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002026 call feedkeys("\<F5>\<LeftMouse>", "xt")
2027 endfunc
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002028 func Popup_filter(winid, key)
2029 if a:key == 'j'
2030 let line = popup_getoptions(a:winid).firstline
2031 let nlines = line('$', a:winid)
2032 let newline = line < nlines ? (line + 1) : nlines
2033 call popup_setoptions(a:winid, #{firstline: newline})
2034 return v:true
2035 elseif a:key == 'x'
2036 call popup_close(a:winid)
2037 return v:true
2038 endif
2039 endfunc
2040
2041 func PopupScroll()
2042 call popup_clear()
2043 let text =<< trim END
2044 1
2045 2
2046 3
2047 4
2048 long line long line long line long line long line long line
2049 long line long line long line long line long line long line
2050 long line long line long line long line long line long line
2051 END
2052 call popup_create(text, #{
2053 \ minwidth: 30,
2054 \ maxwidth: 30,
2055 \ minheight: 4,
2056 \ maxheight: 4,
2057 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +01002058 \ lastline: 4,
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002059 \ wrap: v:true,
2060 \ scrollbar: v:true,
2061 \ mapping: v:false,
2062 \ filter: funcref('Popup_filter')
2063 \ })
2064 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002065 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002066 map <silent> <F4> :call test_setmouse(4, 42)<CR>
2067 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002068 END
2069 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002070 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002071 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
2072
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002073 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002074 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002075 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
2076
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002077 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002078 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002079 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
2080
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002081 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002082 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002083 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
2084
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02002085 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb', firstline: 5})\<CR>")
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002086 " this scrolls two lines (half the window height)
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002087 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
2088 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
2089
2090 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2091 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
2092
2093 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02002094 " wait a bit, otherwise it fails sometimes (double click recognized?)
2095 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002096 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2097 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
2098
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002099 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2100 sleep 100m
2101 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2102 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
2103
2104 call term_sendkeys(buf, ":call ClickBot()\<CR>")
2105 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
2106
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002107 " remove the minwidth and maxheight
2108 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxheight: 0, minwidth: 0})\<CR>")
Bram Moolenaar7e0f4622019-09-17 21:23:39 +02002109 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002110 call VerifyScreenDump(buf, 'Test_popupwin_scroll_10', {})
2111
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002112 " check size with non-wrapping lines
2113 call term_sendkeys(buf, ":call PopupScroll()\<CR>")
2114 call VerifyScreenDump(buf, 'Test_popupwin_scroll_11', {})
2115
2116 " check size with wrapping lines
2117 call term_sendkeys(buf, "j")
2118 call VerifyScreenDump(buf, 'Test_popupwin_scroll_12', {})
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002119
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002120 " clean up
Bram Moolenaar20298ce2020-06-19 21:46:52 +02002121 call term_sendkeys(buf, "x")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002122 call StopVimInTerminal(buf)
2123 call delete('XtestPopupScroll')
2124endfunc
2125
Bram Moolenaar437a7462019-07-05 20:17:22 +02002126func Test_popup_fitting_scrollbar()
2127 " this was causing a crash, divide by zero
2128 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002129 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002130 \ scrollbar: 1,
2131 \ maxwidth: 10,
2132 \ maxheight: 5,
2133 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02002134 redraw
2135 call popup_clear()
2136endfunc
2137
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002138func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002139 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002140
2141 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002142 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002143 let p = popup_create('test', opts)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002144 eval p->popup_settext('this is a text')
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002145 END
2146
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002147 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002148 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002149 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
2150
2151 " Setting to empty string clears it
2152 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
2153 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
2154
2155 " Setting a list
2156 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2157 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2158
2159 " Shrinking with a list
2160 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
2161 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
2162
2163 " Growing with a list
2164 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2165 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2166
2167 " Empty list clears
2168 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
2169 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
2170
2171 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002172 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002173 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
2174
Bram Moolenaarb0992022020-01-30 14:55:42 +01002175 " range() (doesn't work)
2176 call term_sendkeys(buf, ":call popup_settext(p, range(4, 8))\<CR>")
2177 call VerifyScreenDump(buf, 'Test_popup_settext_07', {})
2178
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002179 " clean up
2180 call StopVimInTerminal(buf)
2181 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002182endfunc
2183
2184func Test_popup_hidden()
2185 new
2186
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002187 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002188 redraw
2189 call assert_equal(0, popup_getpos(winid).visible)
2190 call popup_close(winid)
2191
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002192 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002193 redraw
2194 call assert_equal(0, popup_getpos(winid).visible)
2195 call popup_close(winid)
2196
2197 func QuitCallback(id, res)
2198 let s:cb_winid = a:id
2199 let s:cb_res = a:res
2200 endfunc
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002201 let winid = 'make a choice'->popup_dialog(#{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002202 \ filter: 'popup_filter_yesno',
2203 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002204 \ })
2205 redraw
2206 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02002207 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
2208 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002209 exe "normal anot used by filter\<Esc>"
2210 call assert_equal('not used by filter', getline(1))
2211
2212 call popup_show(winid)
2213 call feedkeys('y', "xt")
2214 call assert_equal(1, s:cb_res)
2215
2216 bwipe!
2217 delfunc QuitCallback
2218endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02002219
2220" Test options not checked elsewhere
2221func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002222 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002223 let options = popup_getoptions(winid)
2224 call assert_equal(1, options.wrap)
2225 call assert_equal(0, options.drag)
2226 call assert_equal('Beautiful', options.highlight)
2227
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002228 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002229 let options = popup_getoptions(winid)
2230 call assert_equal(0, options.wrap)
2231 call assert_equal(1, options.drag)
2232 call assert_equal('Another', options.highlight)
2233
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002234 call assert_fails('call popup_setoptions(winid, [])', 'E715:')
2235 call assert_fails('call popup_setoptions(winid, test_null_dict())', 'E715:')
2236
Bram Moolenaarae943152019-06-16 22:54:14 +02002237 call popup_close(winid)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002238 call assert_equal(0, popup_setoptions(winid, options.wrap))
Bram Moolenaarae943152019-06-16 22:54:14 +02002239endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002240
2241func Test_popupwin_garbage_collect()
2242 func MyPopupFilter(x, winid, c)
2243 " NOP
2244 endfunc
2245
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002246 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002247 call test_garbagecollect_now()
2248 redraw
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02002249 " Must not crash caused by invalid memory access
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002250 call feedkeys('j', 'xt')
2251 call assert_true(v:true)
2252
2253 call popup_close(winid)
2254 delfunc MyPopupFilter
2255endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002256
Bram Moolenaar581ba392019-09-03 22:08:33 +02002257func Test_popupwin_filter_mode()
2258 func MyPopupFilter(winid, c)
2259 let s:typed = a:c
2260 if a:c == ':' || a:c == "\r" || a:c == 'v'
2261 " can start cmdline mode, get out, and start/stop Visual mode
2262 return 0
2263 endif
2264 return 1
2265 endfunc
2266
2267 " Normal, Visual and Insert mode
2268 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'nvi'})
2269 redraw
2270 call feedkeys('x', 'xt')
2271 call assert_equal('x', s:typed)
2272
2273 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2274 call assert_equal(':', s:typed)
2275 call assert_equal('foo', g:foo)
2276
2277 let @x = 'something'
2278 call feedkeys('v$"xy', 'xt')
2279 call assert_equal('y', s:typed)
2280 call assert_equal('something', @x) " yank command is filtered out
2281 call feedkeys('v', 'xt') " end Visual mode
2282
2283 call popup_close(winid)
2284
2285 " only Normal mode
2286 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'n'})
2287 redraw
2288 call feedkeys('x', 'xt')
2289 call assert_equal('x', s:typed)
2290
2291 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2292 call assert_equal(':', s:typed)
2293 call assert_equal('foo', g:foo)
2294
2295 let @x = 'something'
2296 call feedkeys('v$"xy', 'xt')
2297 call assert_equal('v', s:typed)
2298 call assert_notequal('something', @x)
2299
2300 call popup_close(winid)
2301
2302 " default: all modes
2303 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
2304 redraw
2305 call feedkeys('x', 'xt')
2306 call assert_equal('x', s:typed)
2307
2308 let g:foo = 'bar'
2309 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2310 call assert_equal("\r", s:typed)
2311 call assert_equal('bar', g:foo)
2312
2313 let @x = 'something'
2314 call feedkeys('v$"xy', 'xt')
2315 call assert_equal('y', s:typed)
2316 call assert_equal('something', @x) " yank command is filtered out
2317 call feedkeys('v', 'xt') " end Visual mode
2318
2319 call popup_close(winid)
2320 delfunc MyPopupFilter
2321endfunc
2322
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002323func Test_popupwin_filter_mouse()
2324 func MyPopupFilter(winid, c)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002325 let g:got_mousepos = getmousepos()
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002326 return 0
2327 endfunc
2328
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002329 call setline(1, ['.'->repeat(25)]->repeat(10))
2330 let winid = popup_create(['short', 'long line that will wrap', 'other'], #{
2331 \ line: 2,
2332 \ col: 4,
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002333 \ maxwidth: 12,
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002334 \ padding: [],
2335 \ border: [],
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002336 \ filter: 'MyPopupFilter',
2337 \ })
2338 redraw
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002339 " 123456789012345678901
2340 " 1 .....................
2341 " 2 ...+--------------+..
2342 " 3 ...| |..
2343 " 4 ...| short |..
2344 " 5 ...| long line th |..
2345 " 6 ...| at will wrap |..
2346 " 7 ...| other |..
2347 " 8 ...| |..
2348 " 9 ...+--------------+..
2349 " 10 .....................
2350 let tests = []
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002351
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002352 func AddItemOutsidePopup(tests, row, col)
2353 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2354 \ screenrow: a:row, screencol: a:col,
2355 \ winid: win_getid(), winrow: a:row, wincol: a:col,
2356 \ line: a:row, column: a:col,
2357 \ }})
2358 endfunc
2359 func AddItemInPopupBorder(tests, winid, row, col)
2360 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2361 \ screenrow: a:row, screencol: a:col,
2362 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2363 \ line: 0, column: 0,
2364 \ }})
2365 endfunc
2366 func AddItemInPopupText(tests, winid, row, col, textline, textcol)
2367 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2368 \ screenrow: a:row, screencol: a:col,
2369 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2370 \ line: a:textline, column: a:textcol,
2371 \ }})
2372 endfunc
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002373
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002374 " above and below popup
2375 for c in range(1, 21)
2376 call AddItemOutsidePopup(tests, 1, c)
2377 call AddItemOutsidePopup(tests, 10, c)
2378 endfor
2379 " left and right of popup
2380 for r in range(1, 10)
2381 call AddItemOutsidePopup(tests, r, 3)
2382 call AddItemOutsidePopup(tests, r, 20)
2383 endfor
2384 " top and bottom in popup
2385 for c in range(4, 19)
2386 call AddItemInPopupBorder(tests, winid, 2, c)
2387 call AddItemInPopupBorder(tests, winid, 3, c)
2388 call AddItemInPopupBorder(tests, winid, 8, c)
2389 call AddItemInPopupBorder(tests, winid, 9, c)
2390 endfor
2391 " left and right margin in popup
2392 for r in range(2, 9)
2393 call AddItemInPopupBorder(tests, winid, r, 4)
2394 call AddItemInPopupBorder(tests, winid, r, 5)
2395 call AddItemInPopupBorder(tests, winid, r, 18)
2396 call AddItemInPopupBorder(tests, winid, r, 19)
2397 endfor
2398 " text "short"
2399 call AddItemInPopupText(tests, winid, 4, 6, 1, 1)
2400 call AddItemInPopupText(tests, winid, 4, 10, 1, 5)
2401 call AddItemInPopupText(tests, winid, 4, 11, 1, 6)
2402 call AddItemInPopupText(tests, winid, 4, 17, 1, 6)
2403 " text "long line th"
2404 call AddItemInPopupText(tests, winid, 5, 6, 2, 1)
2405 call AddItemInPopupText(tests, winid, 5, 10, 2, 5)
2406 call AddItemInPopupText(tests, winid, 5, 17, 2, 12)
2407 " text "at will wrap"
2408 call AddItemInPopupText(tests, winid, 6, 6, 2, 13)
2409 call AddItemInPopupText(tests, winid, 6, 10, 2, 17)
2410 call AddItemInPopupText(tests, winid, 6, 17, 2, 24)
2411 " text "other"
2412 call AddItemInPopupText(tests, winid, 7, 6, 3, 1)
2413 call AddItemInPopupText(tests, winid, 7, 10, 3, 5)
2414 call AddItemInPopupText(tests, winid, 7, 11, 3, 6)
2415 call AddItemInPopupText(tests, winid, 7, 17, 3, 6)
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002416
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002417 for item in tests
2418 call test_setmouse(item.clickrow, item.clickcol)
2419 call feedkeys("\<LeftMouse>", 'xt')
2420 call assert_equal(item.result, g:got_mousepos)
2421 endfor
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002422
2423 call popup_close(winid)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002424 enew!
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002425 delfunc MyPopupFilter
2426endfunc
2427
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002428func Test_popupwin_with_buffer()
2429 call writefile(['some text', 'in a buffer'], 'XsomeFile')
2430 let buf = bufadd('XsomeFile')
2431 call assert_equal(0, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002432
2433 setlocal number
2434 call setbufvar(buf, "&wrapmargin", 13)
2435
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002436 let winid = popup_create(buf, {})
2437 call assert_notequal(0, winid)
2438 let pos = popup_getpos(winid)
2439 call assert_equal(2, pos.height)
2440 call assert_equal(1, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002441
2442 " window-local option is set to default, buffer-local is not
2443 call assert_equal(0, getwinvar(winid, '&number'))
2444 call assert_equal(13, getbufvar(buf, '&wrapmargin'))
2445
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002446 call popup_close(winid)
2447 call assert_equal({}, popup_getpos(winid))
2448 call assert_equal(1, bufloaded(buf))
2449 exe 'bwipe! ' .. buf
Bram Moolenaar46451042019-08-24 15:50:46 +02002450 setlocal nonumber
Bram Moolenaar7866b872019-07-01 22:21:01 +02002451
2452 edit test_popupwin.vim
2453 let winid = popup_create(bufnr(''), {})
2454 redraw
2455 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002456 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002457endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02002458
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002459func Test_popupwin_terminal_buffer()
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002460 CheckFeature terminal
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002461 CheckUnix
Bram Moolenaare06a28f2020-05-13 23:24:12 +02002462 " Starting a terminal to run a shell in is considered flaky.
2463 let g:test_is_flaky = 1
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002464
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002465 let origwin = win_getid()
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002466 let termbuf = term_start(&shell, #{hidden: 1})
2467 let winid = popup_create(termbuf, #{minwidth: 40, minheight: 10})
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002468 " Wait for shell to start
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002469 call WaitForAssert({-> assert_equal("run", job_status(term_getjob(termbuf)))})
Bram Moolenaare06a28f2020-05-13 23:24:12 +02002470 sleep 100m
Bram Moolenaar631ebc42020-02-03 22:15:26 +01002471 " Check this doesn't crash
2472 call assert_equal(winnr(), winnr('j'))
2473 call assert_equal(winnr(), winnr('k'))
2474 call assert_equal(winnr(), winnr('h'))
2475 call assert_equal(winnr(), winnr('l'))
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002476
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002477 " Cannot quit while job is running
2478 call assert_fails('call feedkeys("\<C-W>:quit\<CR>", "xt")', 'E948:')
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002479
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002480 " Cannot enter Terminal-Normal mode. (TODO: but it works...)
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002481 call feedkeys("xxx\<C-W>N", 'xt')
2482 call assert_fails('call feedkeys("gf", "xt")', 'E863:')
2483 call feedkeys("a\<C-U>", 'xt')
2484
Bram Moolenaar3f65c662020-05-27 23:15:16 +02002485 " Cannot escape from terminal window
2486 call assert_fails('tab drop xxx', 'E863:')
2487
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002488 " Cannot open a second one.
2489 let termbuf2 = term_start(&shell, #{hidden: 1})
2490 call assert_fails('call popup_create(termbuf2, #{})', 'E861:')
2491 call term_sendkeys(termbuf2, "exit\<CR>")
2492
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002493 " Exiting shell closes popup window
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002494 call feedkeys("exit\<CR>", 'xt')
2495 " Wait for shell to exit
Bram Moolenaarb2b218d2020-06-22 20:22:19 +02002496 call WaitForAssert({-> assert_equal("dead", job_status(term_getjob(termbuf)))})
Bram Moolenaar373c6512020-06-22 19:24:23 +02002497
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002498 call feedkeys(":quit\<CR>", 'xt')
2499 call assert_equal(origwin, win_getid())
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002500endfunc
2501
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002502func Test_popupwin_close_prevwin()
2503 CheckFeature terminal
2504
2505 call assert_equal(1, winnr('$'))
2506 split
2507 wincmd b
2508 call assert_equal(2, winnr())
2509 let buf = term_start(&shell, #{hidden: 1})
2510 call popup_create(buf, {})
2511 call term_wait(buf, 100)
2512 call popup_clear(1)
2513 call assert_equal(2, winnr())
2514
2515 quit
2516 exe 'bwipe! ' .. buf
2517endfunc
2518
Bram Moolenaar934470e2019-09-01 23:27:05 +02002519func Test_popupwin_with_buffer_and_filter()
2520 new Xwithfilter
2521 call setline(1, range(100))
2522 let bufnr = bufnr()
2523 hide
2524
2525 func BufferFilter(win, key)
2526 if a:key == 'G'
2527 " recursive use of "G" does not cause problems.
2528 call win_execute(a:win, 'normal! G')
2529 return 1
2530 endif
2531 return 0
2532 endfunc
2533
2534 let winid = popup_create(bufnr, #{maxheight: 5, filter: 'BufferFilter'})
2535 call assert_equal(1, popup_getpos(winid).firstline)
2536 redraw
2537 call feedkeys("G", 'xt')
2538 call assert_equal(99, popup_getpos(winid).firstline)
2539
2540 call popup_close(winid)
2541 exe 'bwipe! ' .. bufnr
2542endfunc
2543
Bram Moolenaare296e312019-07-03 23:20:18 +02002544func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002545 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002546 \ maxwidth: 40,
2547 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02002548 \ })
2549 for top in range(1, 20)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002550 eval winid->popup_setoptions(#{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02002551 redraw
2552 call assert_equal(19, popup_getpos(winid).width)
2553 endfor
2554 call popup_clear()
2555endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02002556
2557func Test_popupwin_buf_close()
2558 let buf = bufadd('Xtestbuf')
2559 call bufload(buf)
2560 call setbufline(buf, 1, ['just', 'some', 'lines'])
2561 let winid = popup_create(buf, {})
2562 redraw
2563 call assert_equal(3, popup_getpos(winid).height)
2564 let bufinfo = getbufinfo(buf)[0]
2565 call assert_equal(1, bufinfo.changed)
2566 call assert_equal(0, bufinfo.hidden)
2567 call assert_equal(0, bufinfo.listed)
2568 call assert_equal(1, bufinfo.loaded)
2569 call assert_equal([], bufinfo.windows)
2570 call assert_equal([winid], bufinfo.popups)
2571
2572 call popup_close(winid)
2573 call assert_equal({}, popup_getpos(winid))
2574 let bufinfo = getbufinfo(buf)[0]
2575 call assert_equal(1, bufinfo.changed)
2576 call assert_equal(1, bufinfo.hidden)
2577 call assert_equal(0, bufinfo.listed)
2578 call assert_equal(1, bufinfo.loaded)
2579 call assert_equal([], bufinfo.windows)
2580 call assert_equal([], bufinfo.popups)
2581 exe 'bwipe! ' .. buf
2582endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02002583
2584func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002585 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02002586
2587 let lines =<< trim END
2588 call setline(1, range(1, 10))
2589 hi ScrollThumb ctermbg=blue
2590 hi ScrollBar ctermbg=red
2591 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002592 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002593 \ maxwidth: 10,
2594 \ maxheight: 3,
2595 \ pos : 'topleft',
2596 \ col : a:col,
2597 \ line : a:line,
2598 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02002599 \ })
2600 endfunc
2601 call PopupMenu(['x'], 1, 1)
2602 call PopupMenu(['123456789|'], 1, 16)
2603 call PopupMenu(['123456789|' .. ' '], 7, 1)
2604 call PopupMenu([repeat('123456789|', 100)], 7, 16)
2605 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
2606 END
2607 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002608 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02002609 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
2610
2611 " close the menu popupwin.
2612 call term_sendkeys(buf, " ")
2613 call term_sendkeys(buf, " ")
2614 call term_sendkeys(buf, " ")
2615 call term_sendkeys(buf, " ")
2616 call term_sendkeys(buf, " ")
2617
2618 " clean up
2619 call StopVimInTerminal(buf)
2620 call delete('XtestPopupMenuMaxWidth')
2621endfunc
2622
Bram Moolenaara901a372019-07-13 16:38:50 +02002623func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002624 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02002625
2626 let lines =<< trim END
2627 call setline(1, range(1, 20))
2628 hi ScrollThumb ctermbg=blue
2629 hi ScrollBar ctermbg=red
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002630 eval ['one', 'two', 'three', 'four', 'five',
2631 \ 'six', 'seven', 'eight', 'nine']
2632 \ ->popup_menu(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002633 \ minwidth: 8,
2634 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02002635 \ })
2636 END
2637 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002638 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02002639
2640 call term_sendkeys(buf, "j")
2641 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
2642
2643 call term_sendkeys(buf, "jjj")
2644 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
2645
2646 " if the cursor is the bottom line, it stays at the bottom line.
2647 call term_sendkeys(buf, repeat("j", 20))
2648 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
2649
2650 call term_sendkeys(buf, "kk")
2651 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
2652
2653 call term_sendkeys(buf, "k")
2654 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
2655
2656 " if the cursor is in the top line, it stays in the top line.
2657 call term_sendkeys(buf, repeat("k", 20))
2658 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
2659
2660 " close the menu popupwin.
2661 call term_sendkeys(buf, " ")
2662
2663 " clean up
2664 call StopVimInTerminal(buf)
2665 call delete('XtestPopupMenuScroll')
2666endfunc
2667
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002668func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002669 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002670
2671 let lines =<< trim END
2672 function! MyFilter(winid, key) abort
2673 if a:key == "0"
2674 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
2675 return 1
2676 endif
2677 if a:key == "G"
2678 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
2679 return 1
2680 endif
2681 if a:key == "j"
2682 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
2683 return 1
2684 endif
2685 if a:key == "k"
2686 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
2687 return 1
2688 endif
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002689 if a:key == ':'
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002690 call popup_close(a:winid)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002691 return 0
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002692 endif
2693 return 0
2694 endfunction
2695 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
2696 \ maxheight : 3,
2697 \ filter : 'MyFilter'
2698 \ })
2699 END
2700 call writefile(lines, 'XtestPopupMenuFilter')
2701 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
2702
2703 call term_sendkeys(buf, "j")
2704 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
2705
2706 call term_sendkeys(buf, "k")
2707 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
2708
2709 call term_sendkeys(buf, "G")
2710 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
2711
2712 call term_sendkeys(buf, "0")
2713 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
2714
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002715 " check that when the popup is closed in the filter the screen is redrawn
2716 call term_sendkeys(buf, ":")
2717 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_5', {})
2718 call term_sendkeys(buf, "\<CR>")
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002719
2720 " clean up
2721 call StopVimInTerminal(buf)
2722 call delete('XtestPopupMenuFilter')
2723endfunc
2724
2725func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002726 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002727
2728 let winid = popup_create('some text', {})
2729 call assert_equal(0, popup_getoptions(winid).cursorline)
2730 call popup_close(winid)
2731
2732 let winid = popup_create('some text', #{ cursorline: 1, })
2733 call assert_equal(1, popup_getoptions(winid).cursorline)
2734 call popup_close(winid)
2735
2736 let winid = popup_create('some text', #{ cursorline: 0, })
2737 call assert_equal(0, popup_getoptions(winid).cursorline)
2738 call popup_close(winid)
2739
2740 let winid = popup_menu('some text', {})
2741 call assert_equal(1, popup_getoptions(winid).cursorline)
2742 call popup_close(winid)
2743
2744 let winid = popup_menu('some text', #{ cursorline: 1, })
2745 call assert_equal(1, popup_getoptions(winid).cursorline)
2746 call popup_close(winid)
2747
2748 let winid = popup_menu('some text', #{ cursorline: 0, })
2749 call assert_equal(0, popup_getoptions(winid).cursorline)
2750 call popup_close(winid)
2751
2752 " ---------
2753 " Pattern 1
2754 " ---------
2755 let lines =<< trim END
2756 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
2757 END
2758 call writefile(lines, 'XtestPopupCursorLine')
2759 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2760 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
2761 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2762 call StopVimInTerminal(buf)
2763
2764 " ---------
2765 " Pattern 2
2766 " ---------
2767 let lines =<< trim END
2768 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
2769 END
2770 call writefile(lines, 'XtestPopupCursorLine')
2771 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2772 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
2773 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2774 call StopVimInTerminal(buf)
2775
2776 " ---------
2777 " Pattern 3
2778 " ---------
2779 let lines =<< trim END
2780 function! MyFilter(winid, key) abort
2781 if a:key == "j"
2782 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2783 return 1
2784 endif
2785 if a:key == 'x'
2786 call popup_close(a:winid)
2787 return 1
2788 endif
2789 return 0
2790 endfunction
2791 call popup_menu(['111', '222', '333'], #{
2792 \ cursorline : 0,
2793 \ maxheight : 2,
2794 \ filter : 'MyFilter',
2795 \ })
2796 END
2797 call writefile(lines, 'XtestPopupCursorLine')
2798 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2799 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
2800 call term_sendkeys(buf, "j")
2801 call term_sendkeys(buf, "j")
2802 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
2803 call term_sendkeys(buf, "x")
2804 call StopVimInTerminal(buf)
2805
2806 " ---------
2807 " Pattern 4
2808 " ---------
2809 let lines =<< trim END
2810 function! MyFilter(winid, key) abort
2811 if a:key == "j"
2812 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2813 return 1
2814 endif
2815 if a:key == 'x'
2816 call popup_close(a:winid)
2817 return 1
2818 endif
2819 return 0
2820 endfunction
2821 call popup_menu(['111', '222', '333'], #{
2822 \ cursorline : 1,
2823 \ maxheight : 2,
2824 \ filter : 'MyFilter',
2825 \ })
2826 END
2827 call writefile(lines, 'XtestPopupCursorLine')
2828 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2829 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
2830 call term_sendkeys(buf, "j")
2831 call term_sendkeys(buf, "j")
2832 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
2833 call term_sendkeys(buf, "x")
2834 call StopVimInTerminal(buf)
2835
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02002836 " ---------
2837 " Cursor in second line when creating the popup
2838 " ---------
2839 let lines =<< trim END
2840 let winid = popup_create(['111', '222', '333'], #{
2841 \ cursorline : 1,
2842 \ })
2843 call win_execute(winid, "2")
2844 END
2845 call writefile(lines, 'XtestPopupCursorLine')
2846 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2847 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_7', {})
2848 call StopVimInTerminal(buf)
2849
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002850 call delete('XtestPopupCursorLine')
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01002851
2852 " ---------
2853 " Use current buffer for popupmenu
2854 " ---------
2855 let lines =<< trim END
2856 call setline(1, ['one', 'two', 'three'])
2857 let winid = popup_create(bufnr('%'), #{
2858 \ cursorline : 1,
2859 \ })
2860 call win_execute(winid, "2")
2861 END
2862 call writefile(lines, 'XtestPopupCursorLine')
2863 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2864 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_8', {})
2865 call StopVimInTerminal(buf)
2866
2867 call delete('XtestPopupCursorLine')
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002868endfunc
2869
Bram Moolenaarf914a332019-07-20 15:09:56 +02002870func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002871 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002872 CheckFeature quickfix
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002873
Bram Moolenaarf914a332019-07-20 15:09:56 +02002874 call writefile([
2875 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
2876 \ "another\tXtagfile\t/^this is another",
2877 \ "theword\tXtagfile\t/^theword"],
2878 \ 'Xtags')
2879 call writefile(range(1,20)
2880 \ + ['theword is here']
2881 \ + range(22, 27)
2882 \ + ['this is another place']
2883 \ + range(29, 40),
2884 \ "Xtagfile")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002885 call writefile(range(1,10)
2886 \ + ['searched word is here']
2887 \ + range(12, 20),
2888 \ "Xheader.h")
Bram Moolenaarf914a332019-07-20 15:09:56 +02002889 let lines =<< trim END
2890 set tags=Xtags
2891 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002892 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002893 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002894 \ 'three',
2895 \ 'four',
2896 \ 'five',
2897 \ 'six',
2898 \ 'seven',
2899 \ 'find theword somewhere',
2900 \ 'nine',
2901 \ 'this is another word',
2902 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02002903 set previewpopup=height:4,width:40
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002904 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02002905 END
2906 call writefile(lines, 'XtestPreviewPopup')
2907 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
2908
2909 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
2910 call term_sendkeys(buf, ":\<CR>")
2911 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
2912
2913 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2914 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
2915
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002916 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002917 call term_sendkeys(buf, ":\<CR>")
2918 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
2919
2920 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2921 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
2922
Bram Moolenaar799439a2020-02-11 21:44:17 +01002923 call term_sendkeys(buf, ":silent cd ..\<CR>:\<CR>")
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002924 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar799439a2020-02-11 21:44:17 +01002925 call term_sendkeys(buf, ":silent cd testdir\<CR>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002926
2927 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02002928 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002929 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
2930
2931 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
2932 call term_sendkeys(buf, ":\<CR>")
2933 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
2934
2935 call term_sendkeys(buf, ":pclose\<CR>")
2936 call term_sendkeys(buf, ":psearch searched\<CR>")
2937 call term_sendkeys(buf, ":\<CR>")
2938 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002939
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01002940 call term_sendkeys(buf, "\<C-W>p")
2941 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_9', {})
2942
2943 call term_sendkeys(buf, ":call win_execute(popup_findpreview(), 'call popup_clear()')\<CR>")
2944 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_10', {})
2945
Bram Moolenaarf914a332019-07-20 15:09:56 +02002946 call StopVimInTerminal(buf)
2947 call delete('Xtags')
2948 call delete('Xtagfile')
2949 call delete('XtestPreviewPopup')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002950 call delete('Xheader.h')
Bram Moolenaarf914a332019-07-20 15:09:56 +02002951endfunc
2952
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002953func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002954 let lines =<< trim END
2955 set completeopt+=preview,popup
2956 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002957 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002958
2959 func CompleteFuncDict(findstart, base)
2960 if a:findstart
2961 if col('.') > 10
2962 return col('.') - 10
2963 endif
2964 return 0
2965 endif
2966
2967 return {
2968 \ 'words': [
2969 \ {
2970 \ 'word': 'aword',
2971 \ 'abbr': 'wrd',
2972 \ 'menu': 'extra text',
2973 \ 'info': 'words are cool',
2974 \ 'kind': 'W',
2975 \ 'user_data': 'test'
2976 \ },
2977 \ {
2978 \ 'word': 'anotherword',
2979 \ 'abbr': 'anotwrd',
2980 \ 'menu': 'extra text',
2981 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
2982 \ 'kind': 'W',
2983 \ 'user_data': 'notest'
2984 \ },
2985 \ {
2986 \ 'word': 'noinfo',
2987 \ 'abbr': 'noawrd',
2988 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002989 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002990 \ 'kind': 'W',
2991 \ 'user_data': 'notest'
2992 \ },
2993 \ {
2994 \ 'word': 'thatword',
2995 \ 'abbr': 'thatwrd',
2996 \ 'menu': 'extra text',
2997 \ 'info': 'that word is cool',
2998 \ 'kind': 'W',
2999 \ 'user_data': 'notest'
3000 \ },
3001 \ ]
3002 \ }
3003 endfunc
3004 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003005 func ChangeColor()
3006 let id = popup_findinfo()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003007 eval id->popup_setoptions(#{highlight: 'InfoPopup'})
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003008 endfunc
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003009
3010 func InfoHidden()
3011 set completepopup=height:4,border:off,align:menu
3012 set completeopt-=popup completeopt+=popuphidden
3013 au CompleteChanged * call HandleChange()
3014 endfunc
3015
3016 let s:counter = 0
3017 func HandleChange()
3018 let s:counter += 1
3019 let selected = complete_info(['selected']).selected
3020 if selected <= 0
3021 " First time: do nothing, info remains hidden
3022 return
3023 endif
3024 if selected == 1
3025 " Second time: show info right away
3026 let id = popup_findinfo()
3027 if id
3028 call popup_settext(id, 'immediate info ' .. s:counter)
3029 call popup_show(id)
3030 endif
3031 else
3032 " Third time: show info after a short delay
3033 call timer_start(100, 'ShowInfo')
3034 endif
3035 endfunc
3036
3037 func ShowInfo(...)
3038 let id = popup_findinfo()
3039 if id
3040 call popup_settext(id, 'async info ' .. s:counter)
3041 call popup_show(id)
3042 endif
3043 endfunc
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003044 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003045 return lines
3046endfunc
3047
3048func Test_popupmenu_info_border()
3049 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003050 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003051
3052 let lines = Get_popupmenu_lines()
3053 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003054 call writefile(lines, 'XtestInfoPopup')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003055
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003056 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003057 call TermWait(buf, 25)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003058
3059 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3060 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
3061
3062 call term_sendkeys(buf, "\<C-N>")
3063 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
3064
3065 call term_sendkeys(buf, "\<C-N>")
3066 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
3067
3068 call term_sendkeys(buf, "\<C-N>\<C-N>")
3069 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
3070
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02003071 " info on the left with scrollbar
3072 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
3073 call term_sendkeys(buf, "\<C-N>\<C-N>")
3074 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
3075
Bram Moolenaar202c3f72019-11-21 12:12:35 +01003076 " Test that the popupmenu's scrollbar and infopopup do not overlap
3077 call term_sendkeys(buf, "\<Esc>")
3078 call term_sendkeys(buf, ":set pumheight=3\<CR>")
3079 call term_sendkeys(buf, "cc\<C-X>\<C-U>")
3080 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_6', {})
3081
Bram Moolenaarca7c0782020-01-14 20:42:48 +01003082 " Hide the info popup, cycle trough buffers, make sure it didn't get
3083 " deleted.
3084 call term_sendkeys(buf, "\<Esc>")
3085 call term_sendkeys(buf, ":set hidden\<CR>")
3086 call term_sendkeys(buf, ":bn\<CR>")
3087 call term_sendkeys(buf, ":bn\<CR>")
3088 call term_sendkeys(buf, "otest text test text\<C-X>\<C-U>")
3089 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_7', {})
3090
3091 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003092 call StopVimInTerminal(buf)
3093 call delete('XtestInfoPopup')
3094endfunc
3095
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003096func Test_popupmenu_info_noborder()
3097 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003098 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003099
3100 let lines = Get_popupmenu_lines()
3101 call add(lines, 'set completepopup=height:4,border:off')
3102 call writefile(lines, 'XtestInfoPopupNb')
3103
3104 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003105 call TermWait(buf, 25)
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003106
3107 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3108 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
3109
3110 call StopVimInTerminal(buf)
3111 call delete('XtestInfoPopupNb')
3112endfunc
3113
Bram Moolenaar258cef52019-08-21 17:29:29 +02003114func Test_popupmenu_info_align_menu()
3115 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003116 CheckFeature quickfix
Bram Moolenaar258cef52019-08-21 17:29:29 +02003117
3118 let lines = Get_popupmenu_lines()
3119 call add(lines, 'set completepopup=height:4,border:off,align:menu')
3120 call writefile(lines, 'XtestInfoPopupNb')
3121
3122 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003123 call TermWait(buf, 25)
Bram Moolenaar258cef52019-08-21 17:29:29 +02003124
3125 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3126 call term_sendkeys(buf, "\<C-N>")
3127 call term_sendkeys(buf, "\<C-N>")
3128 call term_sendkeys(buf, "\<C-N>")
3129 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
3130
3131 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
3132 call term_sendkeys(buf, "\<C-N>")
3133 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
3134
3135 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003136 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02003137 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
3138 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
3139 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
3140
3141 call StopVimInTerminal(buf)
3142 call delete('XtestInfoPopupNb')
3143endfunc
3144
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003145func Test_popupmenu_info_hidden()
3146 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003147 CheckFeature quickfix
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003148
3149 let lines = Get_popupmenu_lines()
3150 call add(lines, 'call InfoHidden()')
3151 call writefile(lines, 'XtestInfoPopupHidden')
3152
3153 let buf = RunVimInTerminal('-S XtestInfoPopupHidden', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003154 call TermWait(buf, 25)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003155
3156 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3157 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_1', {})
3158
3159 call term_sendkeys(buf, "\<C-N>")
3160 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_2', {})
3161
3162 call term_sendkeys(buf, "\<C-N>")
3163 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_3', {})
3164
3165 call term_sendkeys(buf, "\<Esc>")
3166 call StopVimInTerminal(buf)
3167 call delete('XtestInfoPopupHidden')
3168endfunc
3169
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003170func Test_popupmenu_info_too_wide()
3171 CheckScreendump
3172 CheckFeature quickfix
3173
3174 let lines =<< trim END
3175 call setline(1, range(10))
3176
3177 set completeopt+=preview,popup
3178 set completepopup=align:menu
3179 set omnifunc=OmniFunc
3180 hi InfoPopup ctermbg=lightgrey
3181
3182 func OmniFunc(findstart, base)
3183 if a:findstart
3184 return 0
3185 endif
3186
3187 let menuText = 'some long text to make sure the menu takes up all of the width of the window'
3188 return #{
3189 \ words: [
3190 \ #{
3191 \ word: 'scrap',
3192 \ menu: menuText,
3193 \ info: "other words are\ncooler than this and some more text\nto make wrap",
3194 \ },
3195 \ #{
3196 \ word: 'scappier',
3197 \ menu: menuText,
3198 \ info: 'words are cool',
3199 \ },
3200 \ #{
3201 \ word: 'scrappier2',
3202 \ menu: menuText,
3203 \ info: 'words are cool',
3204 \ },
3205 \ ]
3206 \ }
3207 endfunc
3208 END
3209
3210 call writefile(lines, 'XtestInfoPopupWide')
3211 let buf = RunVimInTerminal('-S XtestInfoPopupWide', #{rows: 8})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003212 call TermWait(buf, 25)
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003213
3214 call term_sendkeys(buf, "Ascr\<C-X>\<C-O>")
3215 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_wide_1', {})
3216
3217 call term_sendkeys(buf, "\<Esc>")
3218 call StopVimInTerminal(buf)
3219 call delete('XtestInfoPopupWide')
3220endfunc
3221
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003222func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02003223 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003224 call popup_clear()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003225 let winid = 'nothing wrong'->popup_notification({})
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003226 call assert_equal(bufnr, winbufnr(winid))
3227 call popup_clear()
3228endfunc
3229
Bram Moolenaar1824f452019-10-02 23:06:46 +02003230func Test_popupwin_getoptions_tablocal()
3231 topleft split
3232 let win1 = popup_create('nothing', #{maxheight: 8})
3233 let win2 = popup_create('something', #{maxheight: 10})
3234 let win3 = popup_create('something', #{maxheight: 15})
3235 call assert_equal(8, popup_getoptions(win1).maxheight)
3236 call assert_equal(10, popup_getoptions(win2).maxheight)
3237 call assert_equal(15, popup_getoptions(win3).maxheight)
3238 call popup_clear()
3239 quit
3240endfunc
3241
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02003242func Test_popupwin_cancel()
3243 let win1 = popup_create('one', #{line: 5, filter: {... -> 0}})
3244 let win2 = popup_create('two', #{line: 10, filter: {... -> 0}})
3245 let win3 = popup_create('three', #{line: 15, filter: {... -> 0}})
3246 call assert_equal(5, popup_getpos(win1).line)
3247 call assert_equal(10, popup_getpos(win2).line)
3248 call assert_equal(15, popup_getpos(win3).line)
3249 " TODO: this also works without patch 8.1.2110
3250 call feedkeys("\<C-C>", 'xt')
3251 call assert_equal(5, popup_getpos(win1).line)
3252 call assert_equal(10, popup_getpos(win2).line)
3253 call assert_equal({}, popup_getpos(win3))
3254 call feedkeys("\<C-C>", 'xt')
3255 call assert_equal(5, popup_getpos(win1).line)
3256 call assert_equal({}, popup_getpos(win2))
3257 call assert_equal({}, popup_getpos(win3))
3258 call feedkeys("\<C-C>", 'xt')
3259 call assert_equal({}, popup_getpos(win1))
3260 call assert_equal({}, popup_getpos(win2))
3261 call assert_equal({}, popup_getpos(win3))
3262endfunc
3263
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003264func Test_popupwin_filter_redraw()
3265 " Create two popups with a filter that closes the popup when typing "0".
3266 " Both popups should close, even though the redraw also calls
3267 " popup_reset_handled()
3268
3269 func CloseFilter(winid, key)
3270 if a:key == '0'
3271 call popup_close(a:winid)
3272 redraw
3273 endif
3274 return 0 " pass the key
3275 endfunc
3276
3277 let id1 = popup_create('first one', #{
3278 \ line: 1,
3279 \ col: 1,
3280 \ filter: 'CloseFilter',
3281 \ })
3282 let id2 = popup_create('second one', #{
3283 \ line: 9,
3284 \ col: 1,
3285 \ filter: 'CloseFilter',
3286 \ })
3287 call assert_equal(1, popup_getpos(id1).line)
3288 call assert_equal(9, popup_getpos(id2).line)
3289
3290 call feedkeys('0', 'xt')
3291 call assert_equal({}, popup_getpos(id1))
3292 call assert_equal({}, popup_getpos(id2))
3293
3294 call popup_clear()
3295 delfunc CloseFilter
3296endfunc
3297
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003298func Test_popupwin_double_width()
3299 CheckScreendump
3300
3301 let lines =<< trim END
3302 call setline(1, 'x你好世界你好世你好世界你好')
3303 call setline(2, '你好世界你好世你好世界你好')
3304 call setline(3, 'x你好世界你好世你好世界你好')
3305 call popup_create('你好,世界 - 你好,世界xxxxx', #{line: 1, col: 3, maxwidth: 14})
3306 END
3307 call writefile(lines, 'XtestPopupWide')
3308
3309 let buf = RunVimInTerminal('-S XtestPopupWide', #{rows: 10})
3310 call VerifyScreenDump(buf, 'Test_popupwin_doublewidth_1', {})
3311
3312 call StopVimInTerminal(buf)
3313 call delete('XtestPopupWide')
3314endfunc
3315
3316func Test_popupwin_sign()
3317 CheckScreendump
3318
3319 let lines =<< trim END
3320 call setline(1, range(10))
3321 call sign_define('Current', {
3322 \ 'text': '>>',
3323 \ 'texthl': 'WarningMsg',
3324 \ 'linehl': 'Error',
3325 \ })
3326 call sign_define('Other', {
3327 \ 'text': '#!',
3328 \ 'texthl': 'Error',
3329 \ 'linehl': 'Search',
3330 \ })
3331 let winid = popup_create(['hello', 'bright', 'world'], {
3332 \ 'minwidth': 20,
3333 \ })
3334 call setwinvar(winid, "&signcolumn", "yes")
3335 let winbufnr = winbufnr(winid)
3336
3337 " add sign to current buffer, shows
3338 call sign_place(1, 'Selected', 'Current', bufnr('%'), {'lnum': 1})
3339 " add sign to current buffer, does not show
3340 call sign_place(2, 'PopUpSelected', 'Other', bufnr('%'), {'lnum': 2})
3341
3342 " add sign to popup buffer, shows
3343 call sign_place(3, 'PopUpSelected', 'Other', winbufnr, {'lnum': 1})
3344 " add sign to popup buffer, does not show
3345 call sign_place(4, 'Selected', 'Current', winbufnr, {'lnum': 2})
3346 END
3347 call writefile(lines, 'XtestPopupSign')
3348
3349 let buf = RunVimInTerminal('-S XtestPopupSign', #{rows: 10})
3350 call VerifyScreenDump(buf, 'Test_popupwin_sign_1', {})
3351
3352 call StopVimInTerminal(buf)
3353 call delete('XtestPopupSign')
3354endfunc
3355
Bram Moolenaar99ebf222019-12-10 23:44:48 +01003356func Test_popupwin_bufnr()
3357 let popwin = popup_create(['blah'], #{})
3358 let popbuf = winbufnr(popwin)
3359 split asdfasdf
3360 let newbuf = bufnr()
3361 call assert_true(newbuf > popbuf, 'New buffer number is higher')
3362 call assert_equal(newbuf, bufnr('$'))
3363 call popup_clear()
3364 let popwin = popup_create(['blah'], #{})
3365 " reuses previous buffer number
3366 call assert_equal(popbuf, winbufnr(popwin))
3367 call assert_equal(newbuf, bufnr('$'))
3368
3369 call popup_clear()
3370 bwipe!
3371endfunc
3372
Bram Moolenaarec084d32020-02-28 22:44:47 +01003373func Test_popupwin_filter_input_multibyte()
3374 func MyPopupFilter(winid, c)
3375 let g:bytes = range(a:c->strlen())->map({i -> char2nr(a:c[i])})
3376 return 0
3377 endfunc
3378 let winid = popup_create('', #{mapping: 0, filter: 'MyPopupFilter'})
3379
3380 " UTF-8: E3 80 80, including K_SPECIAL(0x80)
3381 call feedkeys("\u3000", 'xt')
3382 call assert_equal([0xe3, 0x80, 0x80], g:bytes)
3383
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01003384 " UTF-8: E3 80 9B, including CSI(0x9B)
3385 call feedkeys("\u301b", 'xt')
3386 call assert_equal([0xe3, 0x80, 0x9b], g:bytes)
Bram Moolenaarec084d32020-02-28 22:44:47 +01003387
Bram Moolenaar20298ce2020-06-19 21:46:52 +02003388 if has('unix')
3389 " with modifyOtherKeys <M-S-a> does not include a modifier sequence
Bram Moolenaarb326edf2020-06-20 15:03:38 +02003390 if has('gui_running')
3391 call feedkeys("\x9b\xfc\x08A", 'Lx!')
3392 else
3393 call feedkeys("\<Esc>[27;4;65~", 'Lx!')
3394 endif
Bram Moolenaar20298ce2020-06-19 21:46:52 +02003395 call assert_equal([0xc3, 0x81], g:bytes)
3396 endif
3397
Bram Moolenaarec084d32020-02-28 22:44:47 +01003398 call popup_clear()
3399 delfunc MyPopupFilter
3400 unlet g:bytes
3401endfunc
3402
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003403func Test_popupwin_atcursor_far_right()
3404 new
3405
3406 " this was getting stuck
3407 set signcolumn=yes
3408 call setline(1, repeat('=', &columns))
3409 normal! ggg$
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003410 let winid = popup_atcursor(repeat('x', 500), #{moved: 'any', border: []})
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003411
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003412 call popup_close(winid)
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003413 bwipe!
3414 set signcolumn&
3415endfunc
3416
Bram Moolenaar0f1563f2020-03-20 21:15:51 +01003417func Test_popupwin_splitmove()
3418 vsplit
3419 let win2 = win_getid()
3420 let popup_winid = popup_dialog('hello', {})
3421 call assert_fails('call win_splitmove(popup_winid, win2)', 'E957:')
3422 call assert_fails('call win_splitmove(win2, popup_winid)', 'E957:')
3423
3424 call popup_clear()
3425 bwipe
3426endfunc
3427
3428
Bram Moolenaar331bafd2019-07-20 17:46:05 +02003429" vim: shiftwidth=2 sts=2