blob: 140f6825b0760208d3cd08b3a0e85cf1b485bbf9 [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
588 terminal
589 $wincmd w
590 let winid = popup_create(['1111', '2222'], #{
591 \ drag: 1,
592 \ resize: 1,
593 \ border: [],
594 \ line: 3,
595 \ })
596 func Dragit()
597 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
598 endfunc
599 map <silent> <F3> :call test_setmouse(3, &columns / 2)<CR>
600 map <silent> <F4> :call test_setmouse(3, &columns / 2 - 20)<CR>
601 END
602 call writefile(lines, 'XtestPopupTerm')
603 let buf = RunVimInTerminal('-S XtestPopupTerm', #{rows: 10})
604 call VerifyScreenDump(buf, 'Test_popupwin_term_01', {})
605
606 call term_sendkeys(buf, ":call Dragit()\<CR>")
607 call VerifyScreenDump(buf, 'Test_popupwin_term_02', {})
608
609 " clean up
610 call StopVimInTerminal(buf)
611 call delete('XtestPopupTerm')
612endfunc
613
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200614func Test_popup_close_with_mouse()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200615 CheckScreendump
616
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200617 let lines =<< trim END
618 call setline(1, range(1, 20))
619 " With border, can click on X
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200620 let winid = popup_create('foobar', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200621 \ close: 'button',
622 \ border: [],
623 \ line: 1,
624 \ col: 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200625 \ })
626 func CloseMsg(id, result)
627 echomsg 'Popup closed with ' .. a:result
628 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200629 let winid = popup_create('notification', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200630 \ close: 'click',
631 \ line: 3,
632 \ col: 15,
633 \ callback: 'CloseMsg',
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200634 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200635 let winid = popup_create('no border here', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200636 \ close: 'button',
637 \ line: 5,
638 \ col: 3,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200639 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200640 let winid = popup_create('only padding', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200641 \ close: 'button',
642 \ padding: [],
643 \ line: 5,
644 \ col: 23,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200645 \ })
646 func CloseWithX()
647 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
648 endfunc
649 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
650 func CloseWithClick()
651 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
652 endfunc
653 map <silent> <F4> :call test_setmouse(3, 17)<CR>
Bram Moolenaarf6396232019-08-24 19:36:00 +0200654 func CreateWithMenuFilter()
655 let winid = popup_create('barfoo', #{
656 \ close: 'button',
657 \ filter: 'popup_filter_menu',
658 \ border: [],
659 \ line: 1,
660 \ col: 40,
661 \ })
662 endfunc
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200663 END
664 call writefile(lines, 'XtestPopupClose')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200665 let buf = RunVimInTerminal('-S XtestPopupClose', #{rows: 10})
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200666 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
667
668 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
669 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
670
671 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
672 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
673
Bram Moolenaarf6396232019-08-24 19:36:00 +0200674 call term_sendkeys(buf, ":call CreateWithMenuFilter()\<CR>")
675 call VerifyScreenDump(buf, 'Test_popupwin_close_04', {})
676
677 " We have to send the actual mouse code, feedkeys() would be caught the
678 " filter.
679 call term_sendkeys(buf, "\<Esc>[<0;47;1M")
680 call VerifyScreenDump(buf, 'Test_popupwin_close_05', {})
681
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200682 " clean up
683 call StopVimInTerminal(buf)
684 call delete('XtestPopupClose')
685endfunction
686
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200687func Test_popup_menu_wrap()
688 CheckScreendump
689
690 let lines =<< trim END
691 call setline(1, range(1, 20))
692 call popup_create([
693 \ 'one',
694 \ 'asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfas',
695 \ 'three',
696 \ 'four',
697 \ ], #{
698 \ pos: "botleft",
699 \ border: [],
700 \ padding: [0,1,0,1],
701 \ maxheight: 3,
702 \ cursorline: 1,
703 \ filter: 'popup_filter_menu',
704 \ })
705 END
706 call writefile(lines, 'XtestPopupWrap')
707 let buf = RunVimInTerminal('-S XtestPopupWrap', #{rows: 10})
708 call VerifyScreenDump(buf, 'Test_popupwin_wrap_1', {})
709
710 call term_sendkeys(buf, "jj")
711 call VerifyScreenDump(buf, 'Test_popupwin_wrap_2', {})
712
713 " clean up
714 call term_sendkeys(buf, "\<Esc>")
715 call StopVimInTerminal(buf)
716 call delete('XtestPopupWrap')
717endfunction
718
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200719func Test_popup_with_mask()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200720 CheckScreendump
721
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200722 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200723 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200724 hi PopupColor ctermbg=lightgrey
725 let winid = popup_create([
726 \ 'some text',
727 \ 'another line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200728 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200729 \ line: 1,
730 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100731 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200732 \ wrap: 0,
733 \ fixed: 1,
734 \ zindex: 90,
735 \ padding: [],
736 \ highlight: 'PopupColor',
737 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200738 call popup_create([
739 \ 'xxxxxxxxx',
740 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200741 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200742 \ line: 3,
743 \ col: 18,
744 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200745 let winidb = popup_create([
746 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200747 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200748 \ line: 7,
749 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100750 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200751 \ wrap: 0,
752 \ fixed: 1,
753 \ close: 'button',
754 \ zindex: 90,
755 \ padding: [],
756 \ border: [],
757 \ 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 +0200758 END
759 call writefile(lines, 'XtestPopupMask')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200760 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200761 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
762
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200763 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
764 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200765 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200766 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
767
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200768 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
769 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200770 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200771 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
772
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200773 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
774 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200775 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200776 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
777
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200778 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
779 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200780 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200781 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
782
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200783 " clean up
784 call StopVimInTerminal(buf)
785 call delete('XtestPopupMask')
786endfunc
787
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200788func Test_popup_select()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200789 CheckScreendump
790 CheckFeature clipboard_working
791
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200792 " create a popup with some text to be selected
793 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200794 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200795 call setline(1, range(1, 20))
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200796 let winid = popup_create(['the word', 'some more', 'several words here', 'invisible', '5', '6', '7'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200797 \ drag: 1,
798 \ border: [],
799 \ line: 3,
800 \ col: 10,
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200801 \ maxheight: 3,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200802 \ })
803 func Select1()
804 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
805 endfunc
806 map <silent> <F3> :call test_setmouse(4, 15)<CR>
807 map <silent> <F4> :call test_setmouse(6, 23)<CR>
808 END
809 call writefile(lines, 'XtestPopupSelect')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200810 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200811 call term_sendkeys(buf, ":call Select1()\<CR>")
812 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
813
814 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
815 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200816 " clean the command line, sometimes it still shows a command
817 call term_sendkeys(buf, ":\<esc>")
818
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200819 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
820
821 " clean up
822 call StopVimInTerminal(buf)
823 call delete('XtestPopupSelect')
824endfunc
825
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200826func Test_popup_in_tab()
827 " default popup is local to tab, not visible when in other tab
828 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200829 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200830 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200831 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200832 tabnew
833 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200834 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200835 quit
836 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200837
838 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200839 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200840 " buffer is gone now
841 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200842
843 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200844 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200845 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200846 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200847 tabnew
848 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200849 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200850 quit
851 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200852 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200853
854 " create popup in other tab
855 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200856 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200857 call assert_equal(0, popup_getpos(winid).visible)
858 call assert_equal(1, popup_getoptions(winid).tabpage)
859 quit
860 call assert_equal(1, popup_getpos(winid).visible)
861 call assert_equal(0, popup_getoptions(winid).tabpage)
862 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200863endfunc
864
865func Test_popup_valid_arguments()
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200866 call assert_equal(0, len(popup_list()))
867
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200868 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200869 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200870 let pos = popup_getpos(winid)
871 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200872 call assert_equal([winid], popup_list())
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200873 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200874
875 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200876 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200877 let pos = popup_getpos(winid)
878 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200879 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200880
881 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200882 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200883 let pos = popup_getpos(winid)
884 let around = (&columns - pos.width) / 2
885 call assert_inrange(around - 1, around + 1, pos.col)
886 let around = (&lines - pos.height) / 2
887 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200888 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200889endfunc
890
891func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200892 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200893 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200894 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200895 call popup_clear()
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200896 call assert_fails('call popup_create(test_null_string(), {})', 'E450:')
897 call assert_fails('call popup_create(test_null_list(), {})', 'E450:')
898 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200899
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200900 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200901 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200902 call assert_fails('call popup_create("text", #{col: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200903 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200904 call assert_fails('call popup_create("text", #{col: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200905 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200906 call assert_fails('call popup_create("text", #{col: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200907 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200908
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200909 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200910 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200911 call assert_fails('call popup_create("text", #{line: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200912 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200913 call assert_fails('call popup_create("text", #{line: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200914 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200915 call assert_fails('call popup_create("text", #{line: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200916 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200917
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200918 call assert_fails('call popup_create("text", #{pos: "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200919 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200920 call assert_fails('call popup_create("text", #{padding: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200921 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200922 call assert_fails('call popup_create("text", #{border: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200923 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200924 call assert_fails('call popup_create("text", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200925 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +0200926 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
927 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200928 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200929 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200930
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200931 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200932 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200933 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200934 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200935 call assert_fails('call popup_create([#{text: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200936 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +0100937 call assert_fails('call popup_create([#{text: "text", props: range(3)}], {})', 'E715:')
938 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200939 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
940 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +0100941 call assert_fails('call popup_create("text", #{mask: range(5)})', 'E475:')
942 call popup_clear()
943 call popup_create("text", #{mask: [range(4)]})
944 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200945 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200946 call assert_fails('call popup_create("text", #{mapping: []})', 'E745:')
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200947 call popup_clear()
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200948 call assert_fails('call popup_create("text", #{tabpage : 4})', 'E997:')
949 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200950endfunc
951
Bram Moolenaareea16992019-05-31 17:34:48 +0200952func Test_win_execute_closing_curwin()
953 split
954 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200955 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200956 call popup_clear()
Bram Moolenaar49540192019-12-11 19:34:54 +0100957
958 let winid = popup_create('some text', {})
959 call assert_fails('call win_execute(winid, printf("normal! :\<C-u>call popup_close(%d)\<CR>", winid))', 'E994')
960 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200961endfunc
962
963func Test_win_execute_not_allowed()
964 let winid = popup_create('some text', {})
965 call assert_fails('call win_execute(winid, "split")', 'E994:')
966 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
967 call assert_fails('call win_execute(winid, "close")', 'E994:')
968 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200969 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200970 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
971 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
972 call assert_fails('call win_execute(winid, "next")', 'E994:')
973 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
Bram Moolenaar3a2505c2020-03-09 16:40:41 +0100974 call assert_fails('call win_execute(winid, "pedit filename")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200975 call assert_fails('call win_execute(winid, "buf")', 'E994:')
Bram Moolenaar3029bcc2020-01-18 15:06:19 +0100976 call assert_fails('call win_execute(winid, "bnext")', 'E994:')
977 call assert_fails('call win_execute(winid, "bprev")', 'E994:')
978 call assert_fails('call win_execute(winid, "bfirst")', 'E994:')
979 call assert_fails('call win_execute(winid, "blast")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200980 call assert_fails('call win_execute(winid, "edit")', 'E994:')
981 call assert_fails('call win_execute(winid, "enew")', 'E994:')
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +0200982 call assert_fails('call win_execute(winid, "help")', 'E994:')
983 call assert_fails('call win_execute(winid, "1only")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200984 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
985 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
986 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
987 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200988 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200989endfunc
990
Bram Moolenaar402502d2019-05-30 22:07:36 +0200991func Test_popup_with_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200992 CheckScreendump
993
Bram Moolenaar402502d2019-05-30 22:07:36 +0200994 let lines =<< trim END
995 call setline(1, range(1, 100))
996 let winid = popup_create(
997 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200998 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200999 END
1000 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001001 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001002 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
1003
1004 " clean up
1005 call StopVimInTerminal(buf)
1006 call delete('XtestPopup')
1007endfunc
1008
1009func Test_popup_without_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001010 CheckScreendump
1011
Bram Moolenaar402502d2019-05-30 22:07:36 +02001012 let lines =<< trim END
1013 call setline(1, range(1, 100))
1014 let winid = popup_create(
1015 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001016 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001017 END
1018 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001019 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001020 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
1021
1022 " clean up
1023 call StopVimInTerminal(buf)
1024 call delete('XtestPopup')
1025endfunc
1026
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001027func Test_popup_with_showbreak()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001028 CheckScreendump
1029
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001030 let lines =<< trim END
1031 set showbreak=>>\
1032 call setline(1, range(1, 20))
1033 let winid = popup_dialog(
Bram Moolenaar8ae54372019-09-15 18:11:16 +02001034 \ 'a long line here that wraps',
1035 \ #{filter: 'popup_filter_yesno',
1036 \ maxwidth: 12})
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001037 END
1038 call writefile(lines, 'XtestPopupShowbreak')
1039 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
1040 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
1041
1042 " clean up
1043 call term_sendkeys(buf, "y")
1044 call StopVimInTerminal(buf)
1045 call delete('XtestPopupShowbreak')
1046endfunc
1047
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001048func Test_popup_time()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001049 CheckFeature timers
1050
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001051 topleft vnew
1052 call setline(1, 'hello')
1053
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001054 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001055 \ line: 1,
1056 \ col: 1,
1057 \ minwidth: 20,
1058 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001059 \})
1060 redraw
1061 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1062 call assert_equal('world', line)
1063
Bram Moolenaarb4f06282019-07-12 21:07:54 +02001064 call assert_equal(winid, popup_locate(1, 1))
1065 call assert_equal(winid, popup_locate(1, 20))
1066 call assert_equal(0, popup_locate(1, 21))
1067 call assert_equal(0, popup_locate(2, 1))
1068
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001069 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +02001070 redraw
Bram Moolenaar196b4662019-09-06 21:34:30 +02001071 let line = join(map(range(1, 5), '1->screenstring(v:val)'), '')
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001072 call assert_equal('hello', line)
1073
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001074 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001075 \ line: &lines,
1076 \ col: 10,
1077 \ minwidth: 20,
1078 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001079 \})
1080 redraw
1081 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1082 call assert_match('.*on the command line.*', line)
1083
1084 sleep 700m
1085 redraw
1086 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1087 call assert_notmatch('.*on the command line.*', line)
1088
1089 bwipe!
1090endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001091
1092func Test_popup_hide()
1093 topleft vnew
1094 call setline(1, 'hello')
1095
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001096 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001097 \ line: 1,
1098 \ col: 1,
1099 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001100 \})
1101 redraw
1102 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1103 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001104 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001105 " buffer is still listed and active
1106 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001107
1108 call popup_hide(winid)
1109 redraw
1110 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1111 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001112 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001113 " buffer is still listed but hidden
Bram Moolenaarca7c0782020-01-14 20:42:48 +01001114 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001115
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001116 eval winid->popup_show()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001117 redraw
1118 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1119 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001120 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001121
1122
1123 call popup_close(winid)
1124 redraw
1125 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1126 call assert_equal('hello', line)
1127
1128 " error is given for existing non-popup window
1129 call assert_fails('call popup_hide(win_getid())', 'E993:')
1130
1131 " no error non-existing window
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001132 eval 1234234->popup_hide()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001133 call popup_show(41234234)
1134
1135 bwipe!
1136endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001137
1138func Test_popup_move()
1139 topleft vnew
1140 call setline(1, 'hello')
1141
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001142 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001143 \ line: 1,
1144 \ col: 1,
1145 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001146 \})
1147 redraw
1148 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1149 call assert_equal('world ', line)
1150
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001151 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001152 redraw
1153 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1154 call assert_equal('hello ', line)
1155 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
1156 call assert_equal('~world', line)
1157
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001158 eval winid->popup_move(#{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001159 redraw
1160 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1161 call assert_equal('hworld', line)
1162
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001163 call assert_fails('call popup_move(winid, [])', 'E715:')
1164 call assert_fails('call popup_move(winid, test_null_dict())', 'E715:')
1165
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001166 call popup_close(winid)
1167
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001168 call assert_equal(0, popup_move(-1, {}))
1169
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001170 bwipe!
1171endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +02001172
Bram Moolenaar402502d2019-05-30 22:07:36 +02001173func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001174 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001175 \ line: 2,
1176 \ col: 3,
1177 \ minwidth: 10,
1178 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +02001179 \})
1180 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001181 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001182 call assert_equal(2, res.line)
1183 call assert_equal(3, res.col)
1184 call assert_equal(10, res.width)
1185 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001186 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001187
1188 call popup_close(winid)
1189endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001190
1191func Test_popup_width_longest()
1192 let tests = [
1193 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
1194 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
1195 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
1196 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
1197 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
1198 \ ]
1199
1200 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001201 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001202 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001203 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001204 call assert_equal(test[1], position.width)
1205 call popup_close(winid)
1206 endfor
1207endfunc
1208
1209func Test_popup_wraps()
1210 let tests = [
1211 \ ['nowrap', 6, 1],
1212 \ ['a line that wraps once', 12, 2],
1213 \ ['a line that wraps two times', 12, 3],
1214 \ ]
1215 for test in tests
1216 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001217 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001218 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001219 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001220 call assert_equal(test[1], position.width)
1221 call assert_equal(test[2], position.height)
1222
1223 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001224 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001225 endfor
1226endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001227
1228func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001229 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001230 \ line: 2,
1231 \ col: 3,
1232 \ minwidth: 10,
1233 \ minheight: 11,
1234 \ maxwidth: 20,
1235 \ maxheight: 21,
1236 \ zindex: 100,
1237 \ time: 5000,
1238 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001239 \})
1240 redraw
1241 let res = popup_getoptions(winid)
1242 call assert_equal(2, res.line)
1243 call assert_equal(3, res.col)
1244 call assert_equal(10, res.minwidth)
1245 call assert_equal(11, res.minheight)
1246 call assert_equal(20, res.maxwidth)
1247 call assert_equal(21, res.maxheight)
1248 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001249 call assert_equal(1, res.fixed)
Bram Moolenaarb8350ab2019-08-04 17:59:49 +02001250 call assert_equal(1, res.mapping)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001251 if has('timers')
1252 call assert_equal(5000, res.time)
1253 endif
1254 call popup_close(winid)
1255
1256 let winid = popup_create('hello', {})
1257 redraw
1258 let res = popup_getoptions(winid)
1259 call assert_equal(0, res.line)
1260 call assert_equal(0, res.col)
1261 call assert_equal(0, res.minwidth)
1262 call assert_equal(0, res.minheight)
1263 call assert_equal(0, res.maxwidth)
1264 call assert_equal(0, res.maxheight)
1265 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001266 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001267 if has('timers')
1268 call assert_equal(0, res.time)
1269 endif
1270 call popup_close(winid)
1271 call assert_equal({}, popup_getoptions(winid))
1272endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001273
1274func Test_popup_option_values()
1275 new
1276 " window-local
1277 setlocal number
1278 setlocal nowrap
1279 " buffer-local
1280 setlocal omnifunc=Something
1281 " global/buffer-local
1282 setlocal path=/there
1283 " global/window-local
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001284 setlocal statusline=2
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001285
1286 let winid = popup_create('hello', {})
1287 call assert_equal(0, getwinvar(winid, '&number'))
1288 call assert_equal(1, getwinvar(winid, '&wrap'))
1289 call assert_equal('', getwinvar(winid, '&omnifunc'))
1290 call assert_equal(&g:path, getwinvar(winid, '&path'))
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001291 call assert_equal(&g:statusline, getwinvar(winid, '&statusline'))
1292
1293 " 'scrolloff' is reset to zero
1294 call assert_equal(5, &scrolloff)
1295 call assert_equal(0, getwinvar(winid, '&scrolloff'))
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001296
1297 call popup_close(winid)
1298 bwipe
1299endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001300
1301func Test_popup_atcursor()
1302 topleft vnew
1303 call setline(1, [
1304 \ 'xxxxxxxxxxxxxxxxx',
1305 \ 'xxxxxxxxxxxxxxxxx',
1306 \ 'xxxxxxxxxxxxxxxxx',
1307 \])
1308
1309 call cursor(2, 2)
1310 redraw
1311 let winid = popup_atcursor('vim', {})
1312 redraw
1313 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1314 call assert_equal('xvimxxxxxxxxxxxxx', line)
1315 call popup_close(winid)
1316
1317 call cursor(3, 4)
1318 redraw
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001319 let winid = 'vim'->popup_atcursor({})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001320 redraw
1321 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
1322 call assert_equal('xxxvimxxxxxxxxxxx', line)
1323 call popup_close(winid)
1324
1325 call cursor(1, 1)
1326 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001327 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001328 \ line: 'cursor+2',
1329 \ col: 'cursor+1',
1330 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001331 redraw
1332 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1333 call assert_equal('xvimxxxxxxxxxxxxx', line)
1334 call popup_close(winid)
1335
1336 call cursor(3, 3)
1337 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001338 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001339 \ line: 'cursor-2',
1340 \ col: 'cursor-1',
1341 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001342 redraw
1343 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1344 call assert_equal('xvimxxxxxxxxxxxxx', line)
1345 call popup_close(winid)
1346
Bram Moolenaar402502d2019-05-30 22:07:36 +02001347 " just enough room above
1348 call cursor(3, 3)
1349 redraw
1350 let winid = popup_atcursor(['vim', 'is great'], {})
1351 redraw
1352 let pos = popup_getpos(winid)
1353 call assert_equal(1, pos.line)
1354 call popup_close(winid)
1355
1356 " not enough room above, popup goes below the cursor
1357 call cursor(3, 3)
1358 redraw
1359 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1360 redraw
1361 let pos = popup_getpos(winid)
1362 call assert_equal(4, pos.line)
1363 call popup_close(winid)
1364
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001365 " cursor in first line, popup in line 2
1366 call cursor(1, 1)
1367 redraw
1368 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1369 redraw
1370 let pos = popup_getpos(winid)
1371 call assert_equal(2, pos.line)
1372 call popup_close(winid)
1373
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001374 bwipe!
1375endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001376
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001377func Test_popup_atcursor_pos()
1378 CheckScreendump
1379
1380 let lines =<< trim END
1381 call setline(1, repeat([repeat('-', 60)], 15))
1382 set so=0
1383
1384 normal 9G3|r#
1385 let winid1 = popup_atcursor(['first', 'second'], #{
1386 \ moved: [0, 0, 0],
1387 \ })
1388 normal 9G21|r&
1389 let winid1 = popup_atcursor(['FIrsT', 'SEcoND'], #{
1390 \ pos: 'botright',
1391 \ moved: [0, 0, 0],
1392 \ })
1393 normal 3G27|r%
1394 let winid1 = popup_atcursor(['fiRSt', 'seCOnd'], #{
1395 \ pos: 'topleft',
1396 \ moved: [0, 0, 0],
1397 \ })
1398 normal 3G45|r@
1399 let winid1 = popup_atcursor(['First', 'SeconD'], #{
1400 \ pos: 'topright',
Bram Moolenaarb0992022020-01-30 14:55:42 +01001401 \ moved: range(3),
1402 \ mousemoved: range(3),
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001403 \ })
1404 END
1405 call writefile(lines, 'XtestPopupAtcursorPos')
1406 let buf = RunVimInTerminal('-S XtestPopupAtcursorPos', #{rows: 12})
1407 call VerifyScreenDump(buf, 'Test_popupwin_atcursor_pos', {})
1408
1409 " clean up
1410 call StopVimInTerminal(buf)
1411 call delete('XtestPopupAtcursorPos')
1412endfunc
1413
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001414func Test_popup_beval()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001415 CheckScreendump
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001416 CheckFeature balloon_eval_term
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001417
1418 let lines =<< trim END
1419 call setline(1, range(1, 20))
1420 call setline(5, 'here is some text to hover over')
1421 set balloonevalterm
1422 set balloonexpr=BalloonExpr()
1423 set balloondelay=100
1424 func BalloonExpr()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001425 let s:winid = [v:beval_text]->popup_beval({})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001426 return ''
1427 endfunc
1428 func Hover()
1429 call test_setmouse(5, 15)
1430 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1431 sleep 100m
1432 endfunc
1433 func MoveOntoPopup()
1434 call test_setmouse(4, 17)
1435 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1436 endfunc
1437 func MoveAway()
1438 call test_setmouse(5, 13)
1439 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1440 endfunc
1441 END
1442 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001443 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001444 call TermWait(buf, 50)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001445 call term_sendkeys(buf, 'j')
1446 call term_sendkeys(buf, ":call Hover()\<CR>")
1447 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1448
1449 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1450 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1451
1452 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1453 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1454
1455 " clean up
1456 call StopVimInTerminal(buf)
1457 call delete('XtestPopupBeval')
1458endfunc
1459
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001460func Test_popup_filter()
1461 new
1462 call setline(1, 'some text')
1463
1464 func MyPopupFilter(winid, c)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001465 if a:c == 'e' || a:c == "\<F9>"
1466 let g:eaten = a:c
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001467 return 1
1468 endif
1469 if a:c == '0'
1470 let g:ignored = '0'
1471 return 0
1472 endif
1473 if a:c == 'x'
1474 call popup_close(a:winid)
1475 return 1
1476 endif
1477 return 0
1478 endfunc
1479
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001480 let winid = 'something'->popup_create(#{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001481 redraw
1482
1483 " e is consumed by the filter
1484 call feedkeys('e', 'xt')
1485 call assert_equal('e', g:eaten)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001486 call feedkeys("\<F9>", 'xt')
1487 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001488
1489 " 0 is ignored by the filter
1490 normal $
1491 call assert_equal(9, getcurpos()[2])
1492 call feedkeys('0', 'xt')
1493 call assert_equal('0', g:ignored)
1494 call assert_equal(1, getcurpos()[2])
1495
1496 " x closes the popup
1497 call feedkeys('x', 'xt')
Bram Moolenaarec084d32020-02-28 22:44:47 +01001498 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001499 call assert_equal(-1, winbufnr(winid))
1500
1501 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001502 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001503endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001504
Bram Moolenaara42d9452019-06-15 21:46:30 +02001505func ShowDialog(key, result)
1506 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001507 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001508 \ filter: 'popup_filter_yesno',
1509 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001510 \ })
1511 redraw
1512 call feedkeys(a:key, "xt")
1513 call assert_equal(winid, s:cb_winid)
1514 call assert_equal(a:result, s:cb_res)
1515endfunc
1516
1517func Test_popup_dialog()
1518 func QuitCallback(id, res)
1519 let s:cb_winid = a:id
1520 let s:cb_res = a:res
1521 endfunc
1522
1523 let winid = ShowDialog("y", 1)
1524 let winid = ShowDialog("Y", 1)
1525 let winid = ShowDialog("n", 0)
1526 let winid = ShowDialog("N", 0)
1527 let winid = ShowDialog("x", 0)
1528 let winid = ShowDialog("X", 0)
1529 let winid = ShowDialog("\<Esc>", 0)
1530 let winid = ShowDialog("\<C-C>", -1)
1531
1532 delfunc QuitCallback
1533endfunc
1534
Bram Moolenaara730e552019-06-16 19:05:31 +02001535func ShowMenu(key, result)
1536 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001537 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001538 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001539 \ })
1540 redraw
1541 call feedkeys(a:key, "xt")
1542 call assert_equal(winid, s:cb_winid)
1543 call assert_equal(a:result, s:cb_res)
1544endfunc
1545
1546func Test_popup_menu()
1547 func QuitCallback(id, res)
1548 let s:cb_winid = a:id
1549 let s:cb_res = a:res
1550 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001551 " mapping won't be used in popup
1552 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001553
1554 let winid = ShowMenu(" ", 1)
1555 let winid = ShowMenu("j \<CR>", 2)
1556 let winid = ShowMenu("JjK \<CR>", 2)
1557 let winid = ShowMenu("jjjjjj ", 3)
1558 let winid = ShowMenu("kkk ", 1)
1559 let winid = ShowMenu("x", -1)
1560 let winid = ShowMenu("X", -1)
1561 let winid = ShowMenu("\<Esc>", -1)
1562 let winid = ShowMenu("\<C-C>", -1)
1563
1564 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001565 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001566endfunc
1567
1568func Test_popup_menu_screenshot()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001569 CheckScreendump
Bram Moolenaara730e552019-06-16 19:05:31 +02001570
1571 let lines =<< trim END
1572 call setline(1, range(1, 20))
1573 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001574 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001575 func MenuDone(id, res)
1576 echomsg "selected " .. a:res
1577 endfunc
1578 END
1579 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001580 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001581 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1582
1583 call term_sendkeys(buf, "jj")
1584 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1585
1586 call term_sendkeys(buf, " ")
1587 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1588
1589 " clean up
1590 call StopVimInTerminal(buf)
1591 call delete('XtestPopupMenu')
1592endfunc
1593
Bram Moolenaarf914a332019-07-20 15:09:56 +02001594func Test_popup_menu_narrow()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001595 CheckScreendump
Bram Moolenaarf914a332019-07-20 15:09:56 +02001596
1597 let lines =<< trim END
1598 call setline(1, range(1, 20))
1599 hi PopupSelected ctermbg=green
1600 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1601 func MenuDone(id, res)
1602 echomsg "selected " .. a:res
1603 endfunc
1604 END
1605 call writefile(lines, 'XtestPopupNarrowMenu')
1606 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1607 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1608
1609 " clean up
1610 call term_sendkeys(buf, "x")
1611 call StopVimInTerminal(buf)
1612 call delete('XtestPopupNarrowMenu')
1613endfunc
1614
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001615func Test_popup_title()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001616 CheckScreendump
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001617
1618 " Create a popup without title or border, a line of padding will be added to
1619 " put the title on.
1620 let lines =<< trim END
1621 call setline(1, range(1, 20))
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001622 let winid = popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001623 END
1624 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001625 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001626 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1627
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001628 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxwidth: 20, title: 'a very long title that is not going to fit'})\<CR>")
1629 call term_sendkeys(buf, ":\<CR>")
1630 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_1', {})
1631
1632 call term_sendkeys(buf, ":call popup_setoptions(winid, #{border: []})\<CR>")
1633 call term_sendkeys(buf, ":\<CR>")
1634 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_2', {})
1635
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001636 " clean up
1637 call StopVimInTerminal(buf)
1638 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001639
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001640 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001641 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001642 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001643 call assert_equal('Another Title', popup_getoptions(winid).title)
1644
1645 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001646endfunc
1647
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001648func Test_popup_close_callback()
1649 func PopupDone(id, result)
1650 let g:result = a:result
1651 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001652 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001653 redraw
1654 call popup_close(winid, 'done')
1655 call assert_equal('done', g:result)
1656endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001657
1658func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001659 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001660 redraw
1661 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001662 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001663 call assert_equal(5, pos.height)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001664 call popup_close(winid)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001665
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001666 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001667 redraw
1668 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001669 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001670 call assert_equal(3, pos.height)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001671 call popup_close(winid)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001672endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001673
1674func Test_popup_never_behind()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001675 CheckScreendump
1676
Bram Moolenaar988c4332019-06-02 14:12:11 +02001677 " +-----------------------------+
1678 " | | |
1679 " | | |
1680 " | | |
1681 " | line1 |
1682 " |------------line2------------|
1683 " | line3 |
1684 " | line4 |
1685 " | |
1686 " | |
1687 " +-----------------------------+
1688 let lines =<< trim END
Bram Moolenaar988c4332019-06-02 14:12:11 +02001689 split
1690 vsplit
1691 let info_window1 = getwininfo()[0]
1692 let line = info_window1['height']
1693 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001694 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001695 \ line : line,
1696 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001697 \ })
1698 END
1699 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001700 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001701 call term_sendkeys(buf, "\<C-W>w")
1702 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1703
1704 " clean up
1705 call StopVimInTerminal(buf)
1706 call delete('XtestPopupBehind')
1707endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001708
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001709func s:VerifyPosition(p, msg, line, col, width, height)
1710 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1711 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1712 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1713 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001714endfunc
1715
1716func Test_popup_position_adjust()
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001717 " Anything placed past the last cell on the right of the screen is moved to
1718 " the left.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001719 "
1720 " When wrapping is disabled, we also shift to the left to display on the
1721 " screen, unless fixed is set.
1722
1723 " Entries for cases which don't vary based on wrapping.
1724 " Format is per tests described below
1725 let both_wrap_tests = [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001726 \ ['a', 5, &columns, 5, &columns, 1, 1],
1727 \ ['b', 5, &columns + 1, 5, &columns, 1, 1],
1728 \ ['c', 5, &columns - 1, 5, &columns - 1, 1, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001729 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001730 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1]]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001731
1732 " these test groups are dicts with:
1733 " - comment: something to identify the group of tests by
1734 " - options: dict of options to merge with the row/col in tests
1735 " - tests: list of cases. Each one is a list with elements:
1736 " - text
1737 " - row
1738 " - col
1739 " - expected row
1740 " - expected col
1741 " - expected width
1742 " - expected height
1743 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001744 \ #{
1745 \ comment: 'left-aligned with wrapping',
1746 \ options: #{
1747 \ wrap: 1,
1748 \ pos: 'botleft',
1749 \ },
1750 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001751 \ ['aa', 5, &columns, 4, &columns, 1, 2],
1752 \ ['bb', 5, &columns + 1, 4, &columns, 1, 2],
1753 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1754 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1755 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1756 \
1757 \ ['aaa', 5, &columns, 3, &columns, 1, 3],
1758 \ ['bbb', 5, &columns + 1, 3, &columns, 1, 3],
1759 \ ['ccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
1760 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1761 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1762 \
1763 \ ['aaaa', 5, &columns, 2, &columns, 1, 4],
1764 \ ['bbbb', 5, &columns + 1, 2, &columns, 1, 4],
1765 \ ['cccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001766 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
1767 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001768 \ ['eeee', 5, &columns - 4, 5, &columns - 4, 4, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001769 \ ],
1770 \ },
1771 \ #{
1772 \ comment: 'left aligned without wrapping',
1773 \ options: #{
1774 \ wrap: 0,
1775 \ pos: 'botleft',
1776 \ },
1777 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001778 \ ['aa', 5, &columns, 5, &columns - 1, 2, 1],
1779 \ ['bb', 5, &columns + 1, 5, &columns - 1, 2, 1],
1780 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1781 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1782 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1783 \
1784 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
1785 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1786 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1787 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1788 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1789 \
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001790 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
1791 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
1792 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
1793 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
1794 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1795 \ ],
1796 \ },
1797 \ #{
1798 \ comment: 'left aligned with fixed position',
1799 \ options: #{
1800 \ wrap: 0,
1801 \ fixed: 1,
1802 \ pos: 'botleft',
1803 \ },
1804 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001805 \ ['aa', 5, &columns, 5, &columns, 1, 1],
1806 \ ['bb', 5, &columns + 1, 5, &columns, 1, 1],
1807 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1808 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1809 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1810 \
1811 \ ['aaa', 5, &columns, 5, &columns, 1, 1],
1812 \ ['bbb', 5, &columns + 1, 5, &columns, 1, 1],
1813 \ ['ccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1814 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1815 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1816 \
1817 \ ['aaaa', 5, &columns, 5, &columns, 1, 1],
1818 \ ['bbbb', 5, &columns + 1, 5, &columns, 1, 1],
1819 \ ['cccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001820 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1821 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1822 \ ],
1823 \ },
1824 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001825
1826 for test_group in tests
1827 for test in test_group.tests
1828 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001829 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001830 \ line: line,
1831 \ col: col,
1832 \ }
1833 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001834
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001835 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001836
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001837 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001838 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
1839 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001840 endfor
1841 endfor
1842
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001843 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001844 %bwipe!
1845endfunc
1846
Bram Moolenaar3397f742019-06-02 18:40:06 +02001847func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001848 " width of screen
1849 let X = join(map(range(&columns), {->'X'}), '')
1850
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001851 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1852 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001853
1854 redraw
1855 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1856 call assert_equal(X, line)
1857
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001858 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001859 redraw
1860
1861 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001862 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
1863 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001864
1865 redraw
1866 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1867 call assert_equal(X, line)
1868
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001869 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001870 redraw
1871
1872 " Extend so > window width
1873 let X .= 'x'
1874
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001875 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1876 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001877
1878 redraw
1879 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1880 call assert_equal(X[ : -2 ], line)
1881
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001882 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001883 redraw
1884
1885 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001886 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
1887 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001888
1889 redraw
1890 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1891 call assert_equal(X[ : -2 ], line)
1892
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001893 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001894 redraw
1895
1896 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001897 let p = popup_create(X,
1898 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
1899 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001900
1901 redraw
1902 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1903 let e_line = ' ' . X[ 1 : -2 ]
1904 call assert_equal(e_line, line)
1905
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001906 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001907 redraw
1908
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001909 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001910 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001911endfunc
1912
1913func Test_popup_moved()
1914 new
1915 call test_override('char_avail', 1)
1916 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1917
1918 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001919 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001920 redraw
1921 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001922 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001923 " trigger the check for last_cursormoved by going into insert mode
1924 call feedkeys("li\<Esc>", 'xt')
1925 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001926 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001927
1928 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001929 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001930 redraw
1931 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001932 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001933 call feedkeys("hi\<Esc>", 'xt')
1934 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001935 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001936
1937 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001938 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001939 redraw
1940 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001941 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001942 call feedkeys("li\<Esc>", 'xt')
1943 call assert_equal(1, popup_getpos(winid).visible)
1944 call feedkeys("ei\<Esc>", 'xt')
1945 call assert_equal(1, popup_getpos(winid).visible)
1946 call feedkeys("eli\<Esc>", 'xt')
1947 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001948 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001949
Bram Moolenaar17627312019-06-02 19:53:44 +02001950 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001951 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001952 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001953 redraw
1954 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001955 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001956 call feedkeys("eli\<Esc>", 'xt')
1957 call assert_equal(1, popup_getpos(winid).visible)
1958 call feedkeys("wi\<Esc>", 'xt')
1959 call assert_equal(1, popup_getpos(winid).visible)
1960 call feedkeys("Eli\<Esc>", 'xt')
1961 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001962 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001963
1964 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001965 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001966 redraw
1967 call assert_equal(1, popup_getpos(winid).visible)
1968 call feedkeys("eli\<Esc>", 'xt')
1969 call feedkeys("ei\<Esc>", 'xt')
1970 call assert_equal(1, popup_getpos(winid).visible)
1971 call feedkeys("eli\<Esc>", 'xt')
1972 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001973 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001974
1975 bwipe!
1976 call test_override('ALL', 0)
1977endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001978
1979func Test_notifications()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001980 CheckFeature timers
1981 CheckScreendump
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001982
Bram Moolenaar0fdddee2019-09-01 15:26:23 +02001983 let lines =<< trim END
1984 call setline(1, range(1, 20))
1985 hi Notification ctermbg=lightblue
1986 call popup_notification('first notification', {})
1987 END
1988 call writefile(lines, 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001989 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001990 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1991
1992 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001993 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1994 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001995 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1996
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001997 " clean up
1998 call StopVimInTerminal(buf)
1999 call delete('XtestNotifications')
2000endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002001
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002002func Test_popup_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002003 CheckScreendump
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002004
2005 let lines =<< trim END
2006 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02002007 hi ScrollThumb ctermbg=blue
2008 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002009 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002010 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002011 \ minwidth: 8,
2012 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002013 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002014 func ScrollUp()
2015 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
2016 endfunc
2017 func ScrollDown()
2018 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
2019 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002020 func ClickTop()
2021 call feedkeys("\<F4>\<LeftMouse>", "xt")
2022 endfunc
2023 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002024 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002025 call feedkeys("\<F5>\<LeftMouse>", "xt")
2026 endfunc
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002027 func Popup_filter(winid, key)
2028 if a:key == 'j'
2029 let line = popup_getoptions(a:winid).firstline
2030 let nlines = line('$', a:winid)
2031 let newline = line < nlines ? (line + 1) : nlines
2032 call popup_setoptions(a:winid, #{firstline: newline})
2033 return v:true
2034 elseif a:key == 'x'
2035 call popup_close(a:winid)
2036 return v:true
2037 endif
2038 endfunc
2039
2040 func PopupScroll()
2041 call popup_clear()
2042 let text =<< trim END
2043 1
2044 2
2045 3
2046 4
2047 long line long line long line long line long line long line
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 END
2051 call popup_create(text, #{
2052 \ minwidth: 30,
2053 \ maxwidth: 30,
2054 \ minheight: 4,
2055 \ maxheight: 4,
2056 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +01002057 \ lastline: 4,
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002058 \ wrap: v:true,
2059 \ scrollbar: v:true,
2060 \ mapping: v:false,
2061 \ filter: funcref('Popup_filter')
2062 \ })
2063 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002064 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002065 map <silent> <F4> :call test_setmouse(4, 42)<CR>
2066 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002067 END
2068 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002069 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002070 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
2071
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002072 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002073 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002074 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
2075
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002076 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002077 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002078 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
2079
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002080 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002081 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002082 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
2083
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02002084 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb', firstline: 5})\<CR>")
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002085 " this scrolls two lines (half the window height)
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002086 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
2087 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
2088
2089 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2090 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
2091
2092 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02002093 " wait a bit, otherwise it fails sometimes (double click recognized?)
2094 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002095 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2096 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
2097
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002098 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2099 sleep 100m
2100 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2101 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
2102
2103 call term_sendkeys(buf, ":call ClickBot()\<CR>")
2104 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
2105
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002106 " remove the minwidth and maxheight
2107 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxheight: 0, minwidth: 0})\<CR>")
Bram Moolenaar7e0f4622019-09-17 21:23:39 +02002108 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002109 call VerifyScreenDump(buf, 'Test_popupwin_scroll_10', {})
2110
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002111 " check size with non-wrapping lines
2112 call term_sendkeys(buf, ":call PopupScroll()\<CR>")
2113 call VerifyScreenDump(buf, 'Test_popupwin_scroll_11', {})
2114
2115 " check size with wrapping lines
2116 call term_sendkeys(buf, "j")
2117 call VerifyScreenDump(buf, 'Test_popupwin_scroll_12', {})
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002118
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002119 " clean up
Bram Moolenaar20298ce2020-06-19 21:46:52 +02002120 call term_sendkeys(buf, "x")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002121 call StopVimInTerminal(buf)
2122 call delete('XtestPopupScroll')
2123endfunc
2124
Bram Moolenaar437a7462019-07-05 20:17:22 +02002125func Test_popup_fitting_scrollbar()
2126 " this was causing a crash, divide by zero
2127 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002128 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002129 \ scrollbar: 1,
2130 \ maxwidth: 10,
2131 \ maxheight: 5,
2132 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02002133 redraw
2134 call popup_clear()
2135endfunc
2136
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002137func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002138 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002139
2140 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002141 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002142 let p = popup_create('test', opts)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002143 eval p->popup_settext('this is a text')
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002144 END
2145
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002146 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002147 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002148 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
2149
2150 " Setting to empty string clears it
2151 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
2152 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
2153
2154 " Setting a list
2155 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2156 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2157
2158 " Shrinking with a list
2159 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
2160 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
2161
2162 " Growing with a list
2163 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2164 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2165
2166 " Empty list clears
2167 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
2168 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
2169
2170 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002171 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002172 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
2173
Bram Moolenaarb0992022020-01-30 14:55:42 +01002174 " range() (doesn't work)
2175 call term_sendkeys(buf, ":call popup_settext(p, range(4, 8))\<CR>")
2176 call VerifyScreenDump(buf, 'Test_popup_settext_07', {})
2177
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002178 " clean up
2179 call StopVimInTerminal(buf)
2180 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002181endfunc
2182
2183func Test_popup_hidden()
2184 new
2185
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002186 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002187 redraw
2188 call assert_equal(0, popup_getpos(winid).visible)
2189 call popup_close(winid)
2190
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002191 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002192 redraw
2193 call assert_equal(0, popup_getpos(winid).visible)
2194 call popup_close(winid)
2195
2196 func QuitCallback(id, res)
2197 let s:cb_winid = a:id
2198 let s:cb_res = a:res
2199 endfunc
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002200 let winid = 'make a choice'->popup_dialog(#{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002201 \ filter: 'popup_filter_yesno',
2202 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002203 \ })
2204 redraw
2205 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02002206 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
2207 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002208 exe "normal anot used by filter\<Esc>"
2209 call assert_equal('not used by filter', getline(1))
2210
2211 call popup_show(winid)
2212 call feedkeys('y', "xt")
2213 call assert_equal(1, s:cb_res)
2214
2215 bwipe!
2216 delfunc QuitCallback
2217endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02002218
2219" Test options not checked elsewhere
2220func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002221 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002222 let options = popup_getoptions(winid)
2223 call assert_equal(1, options.wrap)
2224 call assert_equal(0, options.drag)
2225 call assert_equal('Beautiful', options.highlight)
2226
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002227 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002228 let options = popup_getoptions(winid)
2229 call assert_equal(0, options.wrap)
2230 call assert_equal(1, options.drag)
2231 call assert_equal('Another', options.highlight)
2232
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002233 call assert_fails('call popup_setoptions(winid, [])', 'E715:')
2234 call assert_fails('call popup_setoptions(winid, test_null_dict())', 'E715:')
2235
Bram Moolenaarae943152019-06-16 22:54:14 +02002236 call popup_close(winid)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002237 call assert_equal(0, popup_setoptions(winid, options.wrap))
Bram Moolenaarae943152019-06-16 22:54:14 +02002238endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002239
2240func Test_popupwin_garbage_collect()
2241 func MyPopupFilter(x, winid, c)
2242 " NOP
2243 endfunc
2244
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002245 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002246 call test_garbagecollect_now()
2247 redraw
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02002248 " Must not crash caused by invalid memory access
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002249 call feedkeys('j', 'xt')
2250 call assert_true(v:true)
2251
2252 call popup_close(winid)
2253 delfunc MyPopupFilter
2254endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002255
Bram Moolenaar581ba392019-09-03 22:08:33 +02002256func Test_popupwin_filter_mode()
2257 func MyPopupFilter(winid, c)
2258 let s:typed = a:c
2259 if a:c == ':' || a:c == "\r" || a:c == 'v'
2260 " can start cmdline mode, get out, and start/stop Visual mode
2261 return 0
2262 endif
2263 return 1
2264 endfunc
2265
2266 " Normal, Visual and Insert mode
2267 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'nvi'})
2268 redraw
2269 call feedkeys('x', 'xt')
2270 call assert_equal('x', s:typed)
2271
2272 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2273 call assert_equal(':', s:typed)
2274 call assert_equal('foo', g:foo)
2275
2276 let @x = 'something'
2277 call feedkeys('v$"xy', 'xt')
2278 call assert_equal('y', s:typed)
2279 call assert_equal('something', @x) " yank command is filtered out
2280 call feedkeys('v', 'xt') " end Visual mode
2281
2282 call popup_close(winid)
2283
2284 " only Normal mode
2285 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'n'})
2286 redraw
2287 call feedkeys('x', 'xt')
2288 call assert_equal('x', s:typed)
2289
2290 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2291 call assert_equal(':', s:typed)
2292 call assert_equal('foo', g:foo)
2293
2294 let @x = 'something'
2295 call feedkeys('v$"xy', 'xt')
2296 call assert_equal('v', s:typed)
2297 call assert_notequal('something', @x)
2298
2299 call popup_close(winid)
2300
2301 " default: all modes
2302 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
2303 redraw
2304 call feedkeys('x', 'xt')
2305 call assert_equal('x', s:typed)
2306
2307 let g:foo = 'bar'
2308 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2309 call assert_equal("\r", s:typed)
2310 call assert_equal('bar', g:foo)
2311
2312 let @x = 'something'
2313 call feedkeys('v$"xy', 'xt')
2314 call assert_equal('y', s:typed)
2315 call assert_equal('something', @x) " yank command is filtered out
2316 call feedkeys('v', 'xt') " end Visual mode
2317
2318 call popup_close(winid)
2319 delfunc MyPopupFilter
2320endfunc
2321
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002322func Test_popupwin_filter_mouse()
2323 func MyPopupFilter(winid, c)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002324 let g:got_mousepos = getmousepos()
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002325 return 0
2326 endfunc
2327
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002328 call setline(1, ['.'->repeat(25)]->repeat(10))
2329 let winid = popup_create(['short', 'long line that will wrap', 'other'], #{
2330 \ line: 2,
2331 \ col: 4,
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002332 \ maxwidth: 12,
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002333 \ padding: [],
2334 \ border: [],
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002335 \ filter: 'MyPopupFilter',
2336 \ })
2337 redraw
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002338 " 123456789012345678901
2339 " 1 .....................
2340 " 2 ...+--------------+..
2341 " 3 ...| |..
2342 " 4 ...| short |..
2343 " 5 ...| long line th |..
2344 " 6 ...| at will wrap |..
2345 " 7 ...| other |..
2346 " 8 ...| |..
2347 " 9 ...+--------------+..
2348 " 10 .....................
2349 let tests = []
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002350
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002351 func AddItemOutsidePopup(tests, row, col)
2352 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2353 \ screenrow: a:row, screencol: a:col,
2354 \ winid: win_getid(), winrow: a:row, wincol: a:col,
2355 \ line: a:row, column: a:col,
2356 \ }})
2357 endfunc
2358 func AddItemInPopupBorder(tests, winid, row, col)
2359 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2360 \ screenrow: a:row, screencol: a:col,
2361 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2362 \ line: 0, column: 0,
2363 \ }})
2364 endfunc
2365 func AddItemInPopupText(tests, winid, row, col, textline, textcol)
2366 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2367 \ screenrow: a:row, screencol: a:col,
2368 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2369 \ line: a:textline, column: a:textcol,
2370 \ }})
2371 endfunc
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002372
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002373 " above and below popup
2374 for c in range(1, 21)
2375 call AddItemOutsidePopup(tests, 1, c)
2376 call AddItemOutsidePopup(tests, 10, c)
2377 endfor
2378 " left and right of popup
2379 for r in range(1, 10)
2380 call AddItemOutsidePopup(tests, r, 3)
2381 call AddItemOutsidePopup(tests, r, 20)
2382 endfor
2383 " top and bottom in popup
2384 for c in range(4, 19)
2385 call AddItemInPopupBorder(tests, winid, 2, c)
2386 call AddItemInPopupBorder(tests, winid, 3, c)
2387 call AddItemInPopupBorder(tests, winid, 8, c)
2388 call AddItemInPopupBorder(tests, winid, 9, c)
2389 endfor
2390 " left and right margin in popup
2391 for r in range(2, 9)
2392 call AddItemInPopupBorder(tests, winid, r, 4)
2393 call AddItemInPopupBorder(tests, winid, r, 5)
2394 call AddItemInPopupBorder(tests, winid, r, 18)
2395 call AddItemInPopupBorder(tests, winid, r, 19)
2396 endfor
2397 " text "short"
2398 call AddItemInPopupText(tests, winid, 4, 6, 1, 1)
2399 call AddItemInPopupText(tests, winid, 4, 10, 1, 5)
2400 call AddItemInPopupText(tests, winid, 4, 11, 1, 6)
2401 call AddItemInPopupText(tests, winid, 4, 17, 1, 6)
2402 " text "long line th"
2403 call AddItemInPopupText(tests, winid, 5, 6, 2, 1)
2404 call AddItemInPopupText(tests, winid, 5, 10, 2, 5)
2405 call AddItemInPopupText(tests, winid, 5, 17, 2, 12)
2406 " text "at will wrap"
2407 call AddItemInPopupText(tests, winid, 6, 6, 2, 13)
2408 call AddItemInPopupText(tests, winid, 6, 10, 2, 17)
2409 call AddItemInPopupText(tests, winid, 6, 17, 2, 24)
2410 " text "other"
2411 call AddItemInPopupText(tests, winid, 7, 6, 3, 1)
2412 call AddItemInPopupText(tests, winid, 7, 10, 3, 5)
2413 call AddItemInPopupText(tests, winid, 7, 11, 3, 6)
2414 call AddItemInPopupText(tests, winid, 7, 17, 3, 6)
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002415
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002416 for item in tests
2417 call test_setmouse(item.clickrow, item.clickcol)
2418 call feedkeys("\<LeftMouse>", 'xt')
2419 call assert_equal(item.result, g:got_mousepos)
2420 endfor
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002421
2422 call popup_close(winid)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002423 enew!
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002424 delfunc MyPopupFilter
2425endfunc
2426
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002427func Test_popupwin_with_buffer()
2428 call writefile(['some text', 'in a buffer'], 'XsomeFile')
2429 let buf = bufadd('XsomeFile')
2430 call assert_equal(0, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002431
2432 setlocal number
2433 call setbufvar(buf, "&wrapmargin", 13)
2434
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002435 let winid = popup_create(buf, {})
2436 call assert_notequal(0, winid)
2437 let pos = popup_getpos(winid)
2438 call assert_equal(2, pos.height)
2439 call assert_equal(1, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002440
2441 " window-local option is set to default, buffer-local is not
2442 call assert_equal(0, getwinvar(winid, '&number'))
2443 call assert_equal(13, getbufvar(buf, '&wrapmargin'))
2444
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002445 call popup_close(winid)
2446 call assert_equal({}, popup_getpos(winid))
2447 call assert_equal(1, bufloaded(buf))
2448 exe 'bwipe! ' .. buf
Bram Moolenaar46451042019-08-24 15:50:46 +02002449 setlocal nonumber
Bram Moolenaar7866b872019-07-01 22:21:01 +02002450
2451 edit test_popupwin.vim
2452 let winid = popup_create(bufnr(''), {})
2453 redraw
2454 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002455 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002456endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02002457
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002458func Test_popupwin_terminal_buffer()
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002459 CheckFeature terminal
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002460 CheckUnix
Bram Moolenaare06a28f2020-05-13 23:24:12 +02002461 " Starting a terminal to run a shell in is considered flaky.
2462 let g:test_is_flaky = 1
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002463
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002464 let origwin = win_getid()
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002465 let termbuf = term_start(&shell, #{hidden: 1})
2466 let winid = popup_create(termbuf, #{minwidth: 40, minheight: 10})
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002467 " Wait for shell to start
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002468 call WaitForAssert({-> assert_equal("run", job_status(term_getjob(termbuf)))})
Bram Moolenaare06a28f2020-05-13 23:24:12 +02002469 sleep 100m
Bram Moolenaar631ebc42020-02-03 22:15:26 +01002470 " Check this doesn't crash
2471 call assert_equal(winnr(), winnr('j'))
2472 call assert_equal(winnr(), winnr('k'))
2473 call assert_equal(winnr(), winnr('h'))
2474 call assert_equal(winnr(), winnr('l'))
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002475
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002476 " Cannot quit while job is running
2477 call assert_fails('call feedkeys("\<C-W>:quit\<CR>", "xt")', 'E948:')
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002478
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002479 " Cannot enter Terminal-Normal mode. (TODO: but it works...)
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002480 call feedkeys("xxx\<C-W>N", 'xt')
2481 call assert_fails('call feedkeys("gf", "xt")', 'E863:')
2482 call feedkeys("a\<C-U>", 'xt')
2483
Bram Moolenaar3f65c662020-05-27 23:15:16 +02002484 " Cannot escape from terminal window
2485 call assert_fails('tab drop xxx', 'E863:')
2486
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002487 " Cannot open a second one.
2488 let termbuf2 = term_start(&shell, #{hidden: 1})
2489 call assert_fails('call popup_create(termbuf2, #{})', 'E861:')
2490 call term_sendkeys(termbuf2, "exit\<CR>")
2491
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002492 " Exiting shell closes popup window
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002493 call feedkeys("exit\<CR>", 'xt')
2494 " Wait for shell to exit
Bram Moolenaarb2b218d2020-06-22 20:22:19 +02002495 call WaitForAssert({-> assert_equal("dead", job_status(term_getjob(termbuf)))})
Bram Moolenaar373c6512020-06-22 19:24:23 +02002496
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002497 call feedkeys(":quit\<CR>", 'xt')
2498 call assert_equal(origwin, win_getid())
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002499endfunc
2500
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002501func Test_popupwin_close_prevwin()
2502 CheckFeature terminal
2503
2504 call assert_equal(1, winnr('$'))
2505 split
2506 wincmd b
2507 call assert_equal(2, winnr())
2508 let buf = term_start(&shell, #{hidden: 1})
2509 call popup_create(buf, {})
2510 call term_wait(buf, 100)
2511 call popup_clear(1)
2512 call assert_equal(2, winnr())
2513
2514 quit
2515 exe 'bwipe! ' .. buf
2516endfunc
2517
Bram Moolenaar934470e2019-09-01 23:27:05 +02002518func Test_popupwin_with_buffer_and_filter()
2519 new Xwithfilter
2520 call setline(1, range(100))
2521 let bufnr = bufnr()
2522 hide
2523
2524 func BufferFilter(win, key)
2525 if a:key == 'G'
2526 " recursive use of "G" does not cause problems.
2527 call win_execute(a:win, 'normal! G')
2528 return 1
2529 endif
2530 return 0
2531 endfunc
2532
2533 let winid = popup_create(bufnr, #{maxheight: 5, filter: 'BufferFilter'})
2534 call assert_equal(1, popup_getpos(winid).firstline)
2535 redraw
2536 call feedkeys("G", 'xt')
2537 call assert_equal(99, popup_getpos(winid).firstline)
2538
2539 call popup_close(winid)
2540 exe 'bwipe! ' .. bufnr
2541endfunc
2542
Bram Moolenaare296e312019-07-03 23:20:18 +02002543func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002544 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002545 \ maxwidth: 40,
2546 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02002547 \ })
2548 for top in range(1, 20)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002549 eval winid->popup_setoptions(#{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02002550 redraw
2551 call assert_equal(19, popup_getpos(winid).width)
2552 endfor
2553 call popup_clear()
2554endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02002555
2556func Test_popupwin_buf_close()
2557 let buf = bufadd('Xtestbuf')
2558 call bufload(buf)
2559 call setbufline(buf, 1, ['just', 'some', 'lines'])
2560 let winid = popup_create(buf, {})
2561 redraw
2562 call assert_equal(3, popup_getpos(winid).height)
2563 let bufinfo = getbufinfo(buf)[0]
2564 call assert_equal(1, bufinfo.changed)
2565 call assert_equal(0, bufinfo.hidden)
2566 call assert_equal(0, bufinfo.listed)
2567 call assert_equal(1, bufinfo.loaded)
2568 call assert_equal([], bufinfo.windows)
2569 call assert_equal([winid], bufinfo.popups)
2570
2571 call popup_close(winid)
2572 call assert_equal({}, popup_getpos(winid))
2573 let bufinfo = getbufinfo(buf)[0]
2574 call assert_equal(1, bufinfo.changed)
2575 call assert_equal(1, bufinfo.hidden)
2576 call assert_equal(0, bufinfo.listed)
2577 call assert_equal(1, bufinfo.loaded)
2578 call assert_equal([], bufinfo.windows)
2579 call assert_equal([], bufinfo.popups)
2580 exe 'bwipe! ' .. buf
2581endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02002582
2583func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002584 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02002585
2586 let lines =<< trim END
2587 call setline(1, range(1, 10))
2588 hi ScrollThumb ctermbg=blue
2589 hi ScrollBar ctermbg=red
2590 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002591 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002592 \ maxwidth: 10,
2593 \ maxheight: 3,
2594 \ pos : 'topleft',
2595 \ col : a:col,
2596 \ line : a:line,
2597 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02002598 \ })
2599 endfunc
2600 call PopupMenu(['x'], 1, 1)
2601 call PopupMenu(['123456789|'], 1, 16)
2602 call PopupMenu(['123456789|' .. ' '], 7, 1)
2603 call PopupMenu([repeat('123456789|', 100)], 7, 16)
2604 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
2605 END
2606 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002607 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02002608 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
2609
2610 " close the menu popupwin.
2611 call term_sendkeys(buf, " ")
2612 call term_sendkeys(buf, " ")
2613 call term_sendkeys(buf, " ")
2614 call term_sendkeys(buf, " ")
2615 call term_sendkeys(buf, " ")
2616
2617 " clean up
2618 call StopVimInTerminal(buf)
2619 call delete('XtestPopupMenuMaxWidth')
2620endfunc
2621
Bram Moolenaara901a372019-07-13 16:38:50 +02002622func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002623 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02002624
2625 let lines =<< trim END
2626 call setline(1, range(1, 20))
2627 hi ScrollThumb ctermbg=blue
2628 hi ScrollBar ctermbg=red
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002629 eval ['one', 'two', 'three', 'four', 'five',
2630 \ 'six', 'seven', 'eight', 'nine']
2631 \ ->popup_menu(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002632 \ minwidth: 8,
2633 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02002634 \ })
2635 END
2636 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002637 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02002638
2639 call term_sendkeys(buf, "j")
2640 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
2641
2642 call term_sendkeys(buf, "jjj")
2643 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
2644
2645 " if the cursor is the bottom line, it stays at the bottom line.
2646 call term_sendkeys(buf, repeat("j", 20))
2647 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
2648
2649 call term_sendkeys(buf, "kk")
2650 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
2651
2652 call term_sendkeys(buf, "k")
2653 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
2654
2655 " if the cursor is in the top line, it stays in the top line.
2656 call term_sendkeys(buf, repeat("k", 20))
2657 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
2658
2659 " close the menu popupwin.
2660 call term_sendkeys(buf, " ")
2661
2662 " clean up
2663 call StopVimInTerminal(buf)
2664 call delete('XtestPopupMenuScroll')
2665endfunc
2666
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002667func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002668 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002669
2670 let lines =<< trim END
2671 function! MyFilter(winid, key) abort
2672 if a:key == "0"
2673 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
2674 return 1
2675 endif
2676 if a:key == "G"
2677 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
2678 return 1
2679 endif
2680 if a:key == "j"
2681 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
2682 return 1
2683 endif
2684 if a:key == "k"
2685 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
2686 return 1
2687 endif
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002688 if a:key == ':'
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002689 call popup_close(a:winid)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002690 return 0
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002691 endif
2692 return 0
2693 endfunction
2694 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
2695 \ maxheight : 3,
2696 \ filter : 'MyFilter'
2697 \ })
2698 END
2699 call writefile(lines, 'XtestPopupMenuFilter')
2700 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
2701
2702 call term_sendkeys(buf, "j")
2703 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
2704
2705 call term_sendkeys(buf, "k")
2706 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
2707
2708 call term_sendkeys(buf, "G")
2709 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
2710
2711 call term_sendkeys(buf, "0")
2712 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
2713
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002714 " check that when the popup is closed in the filter the screen is redrawn
2715 call term_sendkeys(buf, ":")
2716 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_5', {})
2717 call term_sendkeys(buf, "\<CR>")
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002718
2719 " clean up
2720 call StopVimInTerminal(buf)
2721 call delete('XtestPopupMenuFilter')
2722endfunc
2723
2724func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002725 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002726
2727 let winid = popup_create('some text', {})
2728 call assert_equal(0, popup_getoptions(winid).cursorline)
2729 call popup_close(winid)
2730
2731 let winid = popup_create('some text', #{ cursorline: 1, })
2732 call assert_equal(1, popup_getoptions(winid).cursorline)
2733 call popup_close(winid)
2734
2735 let winid = popup_create('some text', #{ cursorline: 0, })
2736 call assert_equal(0, popup_getoptions(winid).cursorline)
2737 call popup_close(winid)
2738
2739 let winid = popup_menu('some text', {})
2740 call assert_equal(1, popup_getoptions(winid).cursorline)
2741 call popup_close(winid)
2742
2743 let winid = popup_menu('some text', #{ cursorline: 1, })
2744 call assert_equal(1, popup_getoptions(winid).cursorline)
2745 call popup_close(winid)
2746
2747 let winid = popup_menu('some text', #{ cursorline: 0, })
2748 call assert_equal(0, popup_getoptions(winid).cursorline)
2749 call popup_close(winid)
2750
2751 " ---------
2752 " Pattern 1
2753 " ---------
2754 let lines =<< trim END
2755 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
2756 END
2757 call writefile(lines, 'XtestPopupCursorLine')
2758 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2759 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
2760 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2761 call StopVimInTerminal(buf)
2762
2763 " ---------
2764 " Pattern 2
2765 " ---------
2766 let lines =<< trim END
2767 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
2768 END
2769 call writefile(lines, 'XtestPopupCursorLine')
2770 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2771 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
2772 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2773 call StopVimInTerminal(buf)
2774
2775 " ---------
2776 " Pattern 3
2777 " ---------
2778 let lines =<< trim END
2779 function! MyFilter(winid, key) abort
2780 if a:key == "j"
2781 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2782 return 1
2783 endif
2784 if a:key == 'x'
2785 call popup_close(a:winid)
2786 return 1
2787 endif
2788 return 0
2789 endfunction
2790 call popup_menu(['111', '222', '333'], #{
2791 \ cursorline : 0,
2792 \ maxheight : 2,
2793 \ filter : 'MyFilter',
2794 \ })
2795 END
2796 call writefile(lines, 'XtestPopupCursorLine')
2797 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2798 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
2799 call term_sendkeys(buf, "j")
2800 call term_sendkeys(buf, "j")
2801 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
2802 call term_sendkeys(buf, "x")
2803 call StopVimInTerminal(buf)
2804
2805 " ---------
2806 " Pattern 4
2807 " ---------
2808 let lines =<< trim END
2809 function! MyFilter(winid, key) abort
2810 if a:key == "j"
2811 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2812 return 1
2813 endif
2814 if a:key == 'x'
2815 call popup_close(a:winid)
2816 return 1
2817 endif
2818 return 0
2819 endfunction
2820 call popup_menu(['111', '222', '333'], #{
2821 \ cursorline : 1,
2822 \ maxheight : 2,
2823 \ filter : 'MyFilter',
2824 \ })
2825 END
2826 call writefile(lines, 'XtestPopupCursorLine')
2827 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2828 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
2829 call term_sendkeys(buf, "j")
2830 call term_sendkeys(buf, "j")
2831 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
2832 call term_sendkeys(buf, "x")
2833 call StopVimInTerminal(buf)
2834
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02002835 " ---------
2836 " Cursor in second line when creating the popup
2837 " ---------
2838 let lines =<< trim END
2839 let winid = popup_create(['111', '222', '333'], #{
2840 \ cursorline : 1,
2841 \ })
2842 call win_execute(winid, "2")
2843 END
2844 call writefile(lines, 'XtestPopupCursorLine')
2845 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2846 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_7', {})
2847 call StopVimInTerminal(buf)
2848
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002849 call delete('XtestPopupCursorLine')
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01002850
2851 " ---------
2852 " Use current buffer for popupmenu
2853 " ---------
2854 let lines =<< trim END
2855 call setline(1, ['one', 'two', 'three'])
2856 let winid = popup_create(bufnr('%'), #{
2857 \ cursorline : 1,
2858 \ })
2859 call win_execute(winid, "2")
2860 END
2861 call writefile(lines, 'XtestPopupCursorLine')
2862 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2863 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_8', {})
2864 call StopVimInTerminal(buf)
2865
2866 call delete('XtestPopupCursorLine')
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002867endfunc
2868
Bram Moolenaarf914a332019-07-20 15:09:56 +02002869func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002870 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002871 CheckFeature quickfix
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002872
Bram Moolenaarf914a332019-07-20 15:09:56 +02002873 call writefile([
2874 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
2875 \ "another\tXtagfile\t/^this is another",
2876 \ "theword\tXtagfile\t/^theword"],
2877 \ 'Xtags')
2878 call writefile(range(1,20)
2879 \ + ['theword is here']
2880 \ + range(22, 27)
2881 \ + ['this is another place']
2882 \ + range(29, 40),
2883 \ "Xtagfile")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002884 call writefile(range(1,10)
2885 \ + ['searched word is here']
2886 \ + range(12, 20),
2887 \ "Xheader.h")
Bram Moolenaarf914a332019-07-20 15:09:56 +02002888 let lines =<< trim END
2889 set tags=Xtags
2890 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002891 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002892 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002893 \ 'three',
2894 \ 'four',
2895 \ 'five',
2896 \ 'six',
2897 \ 'seven',
2898 \ 'find theword somewhere',
2899 \ 'nine',
2900 \ 'this is another word',
2901 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02002902 set previewpopup=height:4,width:40
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002903 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02002904 END
2905 call writefile(lines, 'XtestPreviewPopup')
2906 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
2907
2908 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
2909 call term_sendkeys(buf, ":\<CR>")
2910 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
2911
2912 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2913 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
2914
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002915 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002916 call term_sendkeys(buf, ":\<CR>")
2917 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
2918
2919 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2920 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
2921
Bram Moolenaar799439a2020-02-11 21:44:17 +01002922 call term_sendkeys(buf, ":silent cd ..\<CR>:\<CR>")
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002923 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar799439a2020-02-11 21:44:17 +01002924 call term_sendkeys(buf, ":silent cd testdir\<CR>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002925
2926 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02002927 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002928 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
2929
2930 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
2931 call term_sendkeys(buf, ":\<CR>")
2932 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
2933
2934 call term_sendkeys(buf, ":pclose\<CR>")
2935 call term_sendkeys(buf, ":psearch searched\<CR>")
2936 call term_sendkeys(buf, ":\<CR>")
2937 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002938
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01002939 call term_sendkeys(buf, "\<C-W>p")
2940 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_9', {})
2941
2942 call term_sendkeys(buf, ":call win_execute(popup_findpreview(), 'call popup_clear()')\<CR>")
2943 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_10', {})
2944
Bram Moolenaarf914a332019-07-20 15:09:56 +02002945 call StopVimInTerminal(buf)
2946 call delete('Xtags')
2947 call delete('Xtagfile')
2948 call delete('XtestPreviewPopup')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002949 call delete('Xheader.h')
Bram Moolenaarf914a332019-07-20 15:09:56 +02002950endfunc
2951
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002952func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002953 let lines =<< trim END
2954 set completeopt+=preview,popup
2955 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002956 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002957
2958 func CompleteFuncDict(findstart, base)
2959 if a:findstart
2960 if col('.') > 10
2961 return col('.') - 10
2962 endif
2963 return 0
2964 endif
2965
2966 return {
2967 \ 'words': [
2968 \ {
2969 \ 'word': 'aword',
2970 \ 'abbr': 'wrd',
2971 \ 'menu': 'extra text',
2972 \ 'info': 'words are cool',
2973 \ 'kind': 'W',
2974 \ 'user_data': 'test'
2975 \ },
2976 \ {
2977 \ 'word': 'anotherword',
2978 \ 'abbr': 'anotwrd',
2979 \ 'menu': 'extra text',
2980 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
2981 \ 'kind': 'W',
2982 \ 'user_data': 'notest'
2983 \ },
2984 \ {
2985 \ 'word': 'noinfo',
2986 \ 'abbr': 'noawrd',
2987 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002988 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002989 \ 'kind': 'W',
2990 \ 'user_data': 'notest'
2991 \ },
2992 \ {
2993 \ 'word': 'thatword',
2994 \ 'abbr': 'thatwrd',
2995 \ 'menu': 'extra text',
2996 \ 'info': 'that word is cool',
2997 \ 'kind': 'W',
2998 \ 'user_data': 'notest'
2999 \ },
3000 \ ]
3001 \ }
3002 endfunc
3003 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003004 func ChangeColor()
3005 let id = popup_findinfo()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003006 eval id->popup_setoptions(#{highlight: 'InfoPopup'})
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003007 endfunc
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003008
3009 func InfoHidden()
3010 set completepopup=height:4,border:off,align:menu
3011 set completeopt-=popup completeopt+=popuphidden
3012 au CompleteChanged * call HandleChange()
3013 endfunc
3014
3015 let s:counter = 0
3016 func HandleChange()
3017 let s:counter += 1
3018 let selected = complete_info(['selected']).selected
3019 if selected <= 0
3020 " First time: do nothing, info remains hidden
3021 return
3022 endif
3023 if selected == 1
3024 " Second time: show info right away
3025 let id = popup_findinfo()
3026 if id
3027 call popup_settext(id, 'immediate info ' .. s:counter)
3028 call popup_show(id)
3029 endif
3030 else
3031 " Third time: show info after a short delay
3032 call timer_start(100, 'ShowInfo')
3033 endif
3034 endfunc
3035
3036 func ShowInfo(...)
3037 let id = popup_findinfo()
3038 if id
3039 call popup_settext(id, 'async info ' .. s:counter)
3040 call popup_show(id)
3041 endif
3042 endfunc
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003043 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003044 return lines
3045endfunc
3046
3047func Test_popupmenu_info_border()
3048 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003049 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003050
3051 let lines = Get_popupmenu_lines()
3052 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003053 call writefile(lines, 'XtestInfoPopup')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003054
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003055 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003056 call TermWait(buf, 25)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003057
3058 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3059 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
3060
3061 call term_sendkeys(buf, "\<C-N>")
3062 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
3063
3064 call term_sendkeys(buf, "\<C-N>")
3065 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
3066
3067 call term_sendkeys(buf, "\<C-N>\<C-N>")
3068 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
3069
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02003070 " info on the left with scrollbar
3071 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
3072 call term_sendkeys(buf, "\<C-N>\<C-N>")
3073 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
3074
Bram Moolenaar202c3f72019-11-21 12:12:35 +01003075 " Test that the popupmenu's scrollbar and infopopup do not overlap
3076 call term_sendkeys(buf, "\<Esc>")
3077 call term_sendkeys(buf, ":set pumheight=3\<CR>")
3078 call term_sendkeys(buf, "cc\<C-X>\<C-U>")
3079 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_6', {})
3080
Bram Moolenaarca7c0782020-01-14 20:42:48 +01003081 " Hide the info popup, cycle trough buffers, make sure it didn't get
3082 " deleted.
3083 call term_sendkeys(buf, "\<Esc>")
3084 call term_sendkeys(buf, ":set hidden\<CR>")
3085 call term_sendkeys(buf, ":bn\<CR>")
3086 call term_sendkeys(buf, ":bn\<CR>")
3087 call term_sendkeys(buf, "otest text test text\<C-X>\<C-U>")
3088 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_7', {})
3089
3090 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003091 call StopVimInTerminal(buf)
3092 call delete('XtestInfoPopup')
3093endfunc
3094
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003095func Test_popupmenu_info_noborder()
3096 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003097 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003098
3099 let lines = Get_popupmenu_lines()
3100 call add(lines, 'set completepopup=height:4,border:off')
3101 call writefile(lines, 'XtestInfoPopupNb')
3102
3103 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003104 call TermWait(buf, 25)
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003105
3106 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3107 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
3108
3109 call StopVimInTerminal(buf)
3110 call delete('XtestInfoPopupNb')
3111endfunc
3112
Bram Moolenaar258cef52019-08-21 17:29:29 +02003113func Test_popupmenu_info_align_menu()
3114 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003115 CheckFeature quickfix
Bram Moolenaar258cef52019-08-21 17:29:29 +02003116
3117 let lines = Get_popupmenu_lines()
3118 call add(lines, 'set completepopup=height:4,border:off,align:menu')
3119 call writefile(lines, 'XtestInfoPopupNb')
3120
3121 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003122 call TermWait(buf, 25)
Bram Moolenaar258cef52019-08-21 17:29:29 +02003123
3124 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3125 call term_sendkeys(buf, "\<C-N>")
3126 call term_sendkeys(buf, "\<C-N>")
3127 call term_sendkeys(buf, "\<C-N>")
3128 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
3129
3130 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
3131 call term_sendkeys(buf, "\<C-N>")
3132 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
3133
3134 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003135 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02003136 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
3137 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
3138 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
3139
3140 call StopVimInTerminal(buf)
3141 call delete('XtestInfoPopupNb')
3142endfunc
3143
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003144func Test_popupmenu_info_hidden()
3145 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003146 CheckFeature quickfix
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003147
3148 let lines = Get_popupmenu_lines()
3149 call add(lines, 'call InfoHidden()')
3150 call writefile(lines, 'XtestInfoPopupHidden')
3151
3152 let buf = RunVimInTerminal('-S XtestInfoPopupHidden', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003153 call TermWait(buf, 25)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003154
3155 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3156 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_1', {})
3157
3158 call term_sendkeys(buf, "\<C-N>")
3159 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_2', {})
3160
3161 call term_sendkeys(buf, "\<C-N>")
3162 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_3', {})
3163
3164 call term_sendkeys(buf, "\<Esc>")
3165 call StopVimInTerminal(buf)
3166 call delete('XtestInfoPopupHidden')
3167endfunc
3168
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003169func Test_popupmenu_info_too_wide()
3170 CheckScreendump
3171 CheckFeature quickfix
3172
3173 let lines =<< trim END
3174 call setline(1, range(10))
3175
3176 set completeopt+=preview,popup
3177 set completepopup=align:menu
3178 set omnifunc=OmniFunc
3179 hi InfoPopup ctermbg=lightgrey
3180
3181 func OmniFunc(findstart, base)
3182 if a:findstart
3183 return 0
3184 endif
3185
3186 let menuText = 'some long text to make sure the menu takes up all of the width of the window'
3187 return #{
3188 \ words: [
3189 \ #{
3190 \ word: 'scrap',
3191 \ menu: menuText,
3192 \ info: "other words are\ncooler than this and some more text\nto make wrap",
3193 \ },
3194 \ #{
3195 \ word: 'scappier',
3196 \ menu: menuText,
3197 \ info: 'words are cool',
3198 \ },
3199 \ #{
3200 \ word: 'scrappier2',
3201 \ menu: menuText,
3202 \ info: 'words are cool',
3203 \ },
3204 \ ]
3205 \ }
3206 endfunc
3207 END
3208
3209 call writefile(lines, 'XtestInfoPopupWide')
3210 let buf = RunVimInTerminal('-S XtestInfoPopupWide', #{rows: 8})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003211 call TermWait(buf, 25)
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003212
3213 call term_sendkeys(buf, "Ascr\<C-X>\<C-O>")
3214 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_wide_1', {})
3215
3216 call term_sendkeys(buf, "\<Esc>")
3217 call StopVimInTerminal(buf)
3218 call delete('XtestInfoPopupWide')
3219endfunc
3220
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003221func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02003222 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003223 call popup_clear()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003224 let winid = 'nothing wrong'->popup_notification({})
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003225 call assert_equal(bufnr, winbufnr(winid))
3226 call popup_clear()
3227endfunc
3228
Bram Moolenaar1824f452019-10-02 23:06:46 +02003229func Test_popupwin_getoptions_tablocal()
3230 topleft split
3231 let win1 = popup_create('nothing', #{maxheight: 8})
3232 let win2 = popup_create('something', #{maxheight: 10})
3233 let win3 = popup_create('something', #{maxheight: 15})
3234 call assert_equal(8, popup_getoptions(win1).maxheight)
3235 call assert_equal(10, popup_getoptions(win2).maxheight)
3236 call assert_equal(15, popup_getoptions(win3).maxheight)
3237 call popup_clear()
3238 quit
3239endfunc
3240
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02003241func Test_popupwin_cancel()
3242 let win1 = popup_create('one', #{line: 5, filter: {... -> 0}})
3243 let win2 = popup_create('two', #{line: 10, filter: {... -> 0}})
3244 let win3 = popup_create('three', #{line: 15, filter: {... -> 0}})
3245 call assert_equal(5, popup_getpos(win1).line)
3246 call assert_equal(10, popup_getpos(win2).line)
3247 call assert_equal(15, popup_getpos(win3).line)
3248 " TODO: this also works without patch 8.1.2110
3249 call feedkeys("\<C-C>", 'xt')
3250 call assert_equal(5, popup_getpos(win1).line)
3251 call assert_equal(10, popup_getpos(win2).line)
3252 call assert_equal({}, popup_getpos(win3))
3253 call feedkeys("\<C-C>", 'xt')
3254 call assert_equal(5, popup_getpos(win1).line)
3255 call assert_equal({}, popup_getpos(win2))
3256 call assert_equal({}, popup_getpos(win3))
3257 call feedkeys("\<C-C>", 'xt')
3258 call assert_equal({}, popup_getpos(win1))
3259 call assert_equal({}, popup_getpos(win2))
3260 call assert_equal({}, popup_getpos(win3))
3261endfunc
3262
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003263func Test_popupwin_filter_redraw()
3264 " Create two popups with a filter that closes the popup when typing "0".
3265 " Both popups should close, even though the redraw also calls
3266 " popup_reset_handled()
3267
3268 func CloseFilter(winid, key)
3269 if a:key == '0'
3270 call popup_close(a:winid)
3271 redraw
3272 endif
3273 return 0 " pass the key
3274 endfunc
3275
3276 let id1 = popup_create('first one', #{
3277 \ line: 1,
3278 \ col: 1,
3279 \ filter: 'CloseFilter',
3280 \ })
3281 let id2 = popup_create('second one', #{
3282 \ line: 9,
3283 \ col: 1,
3284 \ filter: 'CloseFilter',
3285 \ })
3286 call assert_equal(1, popup_getpos(id1).line)
3287 call assert_equal(9, popup_getpos(id2).line)
3288
3289 call feedkeys('0', 'xt')
3290 call assert_equal({}, popup_getpos(id1))
3291 call assert_equal({}, popup_getpos(id2))
3292
3293 call popup_clear()
3294 delfunc CloseFilter
3295endfunc
3296
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003297func Test_popupwin_double_width()
3298 CheckScreendump
3299
3300 let lines =<< trim END
3301 call setline(1, 'x你好世界你好世你好世界你好')
3302 call setline(2, '你好世界你好世你好世界你好')
3303 call setline(3, 'x你好世界你好世你好世界你好')
3304 call popup_create('你好,世界 - 你好,世界xxxxx', #{line: 1, col: 3, maxwidth: 14})
3305 END
3306 call writefile(lines, 'XtestPopupWide')
3307
3308 let buf = RunVimInTerminal('-S XtestPopupWide', #{rows: 10})
3309 call VerifyScreenDump(buf, 'Test_popupwin_doublewidth_1', {})
3310
3311 call StopVimInTerminal(buf)
3312 call delete('XtestPopupWide')
3313endfunc
3314
3315func Test_popupwin_sign()
3316 CheckScreendump
3317
3318 let lines =<< trim END
3319 call setline(1, range(10))
3320 call sign_define('Current', {
3321 \ 'text': '>>',
3322 \ 'texthl': 'WarningMsg',
3323 \ 'linehl': 'Error',
3324 \ })
3325 call sign_define('Other', {
3326 \ 'text': '#!',
3327 \ 'texthl': 'Error',
3328 \ 'linehl': 'Search',
3329 \ })
3330 let winid = popup_create(['hello', 'bright', 'world'], {
3331 \ 'minwidth': 20,
3332 \ })
3333 call setwinvar(winid, "&signcolumn", "yes")
3334 let winbufnr = winbufnr(winid)
3335
3336 " add sign to current buffer, shows
3337 call sign_place(1, 'Selected', 'Current', bufnr('%'), {'lnum': 1})
3338 " add sign to current buffer, does not show
3339 call sign_place(2, 'PopUpSelected', 'Other', bufnr('%'), {'lnum': 2})
3340
3341 " add sign to popup buffer, shows
3342 call sign_place(3, 'PopUpSelected', 'Other', winbufnr, {'lnum': 1})
3343 " add sign to popup buffer, does not show
3344 call sign_place(4, 'Selected', 'Current', winbufnr, {'lnum': 2})
3345 END
3346 call writefile(lines, 'XtestPopupSign')
3347
3348 let buf = RunVimInTerminal('-S XtestPopupSign', #{rows: 10})
3349 call VerifyScreenDump(buf, 'Test_popupwin_sign_1', {})
3350
3351 call StopVimInTerminal(buf)
3352 call delete('XtestPopupSign')
3353endfunc
3354
Bram Moolenaar99ebf222019-12-10 23:44:48 +01003355func Test_popupwin_bufnr()
3356 let popwin = popup_create(['blah'], #{})
3357 let popbuf = winbufnr(popwin)
3358 split asdfasdf
3359 let newbuf = bufnr()
3360 call assert_true(newbuf > popbuf, 'New buffer number is higher')
3361 call assert_equal(newbuf, bufnr('$'))
3362 call popup_clear()
3363 let popwin = popup_create(['blah'], #{})
3364 " reuses previous buffer number
3365 call assert_equal(popbuf, winbufnr(popwin))
3366 call assert_equal(newbuf, bufnr('$'))
3367
3368 call popup_clear()
3369 bwipe!
3370endfunc
3371
Bram Moolenaarec084d32020-02-28 22:44:47 +01003372func Test_popupwin_filter_input_multibyte()
3373 func MyPopupFilter(winid, c)
3374 let g:bytes = range(a:c->strlen())->map({i -> char2nr(a:c[i])})
3375 return 0
3376 endfunc
3377 let winid = popup_create('', #{mapping: 0, filter: 'MyPopupFilter'})
3378
3379 " UTF-8: E3 80 80, including K_SPECIAL(0x80)
3380 call feedkeys("\u3000", 'xt')
3381 call assert_equal([0xe3, 0x80, 0x80], g:bytes)
3382
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01003383 " UTF-8: E3 80 9B, including CSI(0x9B)
3384 call feedkeys("\u301b", 'xt')
3385 call assert_equal([0xe3, 0x80, 0x9b], g:bytes)
Bram Moolenaarec084d32020-02-28 22:44:47 +01003386
Bram Moolenaar20298ce2020-06-19 21:46:52 +02003387 if has('unix')
3388 " with modifyOtherKeys <M-S-a> does not include a modifier sequence
Bram Moolenaarb326edf2020-06-20 15:03:38 +02003389 if has('gui_running')
3390 call feedkeys("\x9b\xfc\x08A", 'Lx!')
3391 else
3392 call feedkeys("\<Esc>[27;4;65~", 'Lx!')
3393 endif
Bram Moolenaar20298ce2020-06-19 21:46:52 +02003394 call assert_equal([0xc3, 0x81], g:bytes)
3395 endif
3396
Bram Moolenaarec084d32020-02-28 22:44:47 +01003397 call popup_clear()
3398 delfunc MyPopupFilter
3399 unlet g:bytes
3400endfunc
3401
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003402func Test_popupwin_atcursor_far_right()
3403 new
3404
3405 " this was getting stuck
3406 set signcolumn=yes
3407 call setline(1, repeat('=', &columns))
3408 normal! ggg$
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003409 let winid = popup_atcursor(repeat('x', 500), #{moved: 'any', border: []})
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003410
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003411 call popup_close(winid)
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003412 bwipe!
3413 set signcolumn&
3414endfunc
3415
Bram Moolenaar0f1563f2020-03-20 21:15:51 +01003416func Test_popupwin_splitmove()
3417 vsplit
3418 let win2 = win_getid()
3419 let popup_winid = popup_dialog('hello', {})
3420 call assert_fails('call win_splitmove(popup_winid, win2)', 'E957:')
3421 call assert_fails('call win_splitmove(win2, popup_winid)', 'E957:')
3422
3423 call popup_clear()
3424 bwipe
3425endfunc
3426
3427
Bram Moolenaar331bafd2019-07-20 17:46:05 +02003428" vim: shiftwidth=2 sts=2