blob: 7335c1c13ab2087c2ebbe925e9e48148deb0e46a [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001" Tests for popup windows
2
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004CheckFeature popupwin
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005
6source screendump.vim
7
8func Test_simple_popup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02009 CheckScreendump
10
Bram Moolenaare7eb9272019-06-24 00:58:07 +020011 let lines =<< trim END
12 call setline(1, range(1, 100))
13 hi PopupColor1 ctermbg=lightblue
14 hi PopupColor2 ctermbg=lightcyan
Bram Moolenaar1666ac92019-11-10 17:22:31 +010015 hi EndOfBuffer ctermbg=lightgrey
Bram Moolenaare7eb9272019-06-24 00:58:07 +020016 hi Comment ctermfg=red
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020017 call prop_type_add('comment', #{highlight: 'Comment'})
18 let winid = popup_create('hello there', #{line: 3, col: 11, minwidth: 20, highlight: 'PopupColor1'})
19 let winid2 = popup_create(['another one', 'another two', 'another three'], #{line: 3, col: 25, minwidth: 20})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020020 call setwinvar(winid2, '&wincolor', 'PopupColor2')
21 END
22 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020023 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar4d784b22019-05-25 19:51:39 +020024 call VerifyScreenDump(buf, 'Test_popupwin_01', {})
25
Bram Moolenaarec583842019-05-26 14:11:23 +020026 " Add a tabpage
27 call term_sendkeys(buf, ":tabnew\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020028 call term_sendkeys(buf, ":let popupwin = popup_create(["
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020029 \ .. "#{text: 'other tab'},"
30 \ .. "#{text: 'a comment line', props: [#{"
Bram Moolenaard5abb4c2019-07-13 22:46:10 +020031 \ .. "col: 3, length: 7, minwidth: 20, type: 'comment'"
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020032 \ .. "}]},"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020033 \ .. "], #{line: 4, col: 9, minwidth: 20})\<CR>")
Bram Moolenaarec583842019-05-26 14:11:23 +020034 call VerifyScreenDump(buf, 'Test_popupwin_02', {})
35
36 " switch back to first tabpage
37 call term_sendkeys(buf, "gt")
38 call VerifyScreenDump(buf, 'Test_popupwin_03', {})
39
40 " close that tabpage
41 call term_sendkeys(buf, ":quit!\<CR>")
42 call VerifyScreenDump(buf, 'Test_popupwin_04', {})
43
Bram Moolenaar202d9822019-06-11 21:56:30 +020044 " set 'columns' to a small value, size must be recomputed
45 call term_sendkeys(buf, ":let cols = &columns\<CR>")
46 call term_sendkeys(buf, ":set columns=12\<CR>")
47 call VerifyScreenDump(buf, 'Test_popupwin_04a', {})
48 call term_sendkeys(buf, ":let &columns = cols\<CR>")
49
Bram Moolenaar17146962019-05-30 00:12:11 +020050 " resize popup, show empty line at bottom
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020051 call term_sendkeys(buf, ":call popup_move(popupwin, #{minwidth: 15, maxwidth: 25, minheight: 3, maxheight: 5})\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020052 call term_sendkeys(buf, ":redraw\<CR>")
53 call VerifyScreenDump(buf, 'Test_popupwin_05', {})
54
Bram Moolenaar17146962019-05-30 00:12:11 +020055 " show not fitting line at bottom
56 call term_sendkeys(buf, ":call setbufline(winbufnr(popupwin), 3, 'this line will not fit here')\<CR>")
57 call term_sendkeys(buf, ":redraw\<CR>")
58 call VerifyScreenDump(buf, 'Test_popupwin_06', {})
59
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020060 " move popup over ruler
61 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020062 call term_sendkeys(buf, ":call popup_move(popupwin, #{line: 7, col: 55})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +020063 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020064 call VerifyScreenDump(buf, 'Test_popupwin_07', {})
65
66 " clear all popups after moving the cursor a bit, so that ruler is updated
67 call term_sendkeys(buf, "axxx\<Esc>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +020068 call TermWait(buf)
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020069 call term_sendkeys(buf, "0")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +020070 call TermWait(buf)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +020071 call term_sendkeys(buf, ":call popup_clear()\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020072 call VerifyScreenDump(buf, 'Test_popupwin_08', {})
73
Bram Moolenaar4d784b22019-05-25 19:51:39 +020074 " clean up
75 call StopVimInTerminal(buf)
76 call delete('XtestPopup')
77endfunc
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020078
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020079func Test_popup_with_border_and_padding()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +020080 CheckScreendump
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020081
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020082 for iter in range(0, 1)
Bram Moolenaare7eb9272019-06-24 00:58:07 +020083 let lines =<< trim END
84 call setline(1, range(1, 100))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020085 call popup_create('hello border', #{line: 2, col: 3, border: []})
86 call popup_create('hello padding', #{line: 2, col: 23, padding: []})
Bram Moolenaarc363fe12019-08-04 18:13:46 +020087 call popup_create('hello both', #{line: 2, col: 43, border: [], padding: [], highlight: 'Normal'})
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020088 call popup_create('border TL', #{line: 6, col: 3, border: [1, 0, 0, 4]})
Bram Moolenaarb0992022020-01-30 14:55:42 +010089 call popup_create('paddings', #{line: 6, col: 23, padding: range(1, 4)})
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020090 call popup_create('wrapped longer text', #{line: 8, col: 55, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
91 call popup_create('right aligned text', #{line: 11, col: 56, wrap: 0, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
Bram Moolenaarfbcdf672020-01-06 23:07:48 +010092 call popup_create('X', #{line: 2, col: 73})
93 call popup_create('X', #{line: 3, col: 74})
94 call popup_create('X', #{line: 4, col: 75})
95 call popup_create('X', #{line: 5, col: 76})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020096 END
97 call insert(lines, iter == 1 ? '' : 'set enc=latin1')
98 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020099 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 15})
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +0200100 call VerifyScreenDump(buf, 'Test_popupwin_2' .. iter, {})
101
102 call StopVimInTerminal(buf)
103 call delete('XtestPopupBorder')
104 endfor
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200105
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200106 let lines =<< trim END
107 call setline(1, range(1, 100))
108 hi BlueColor ctermbg=lightblue
109 hi TopColor ctermbg=253
110 hi RightColor ctermbg=245
111 hi BottomColor ctermbg=240
112 hi LeftColor ctermbg=248
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200113 call popup_create('hello border', #{line: 2, col: 3, border: [], borderhighlight: ['BlueColor']})
114 call popup_create(['hello border', 'and more'], #{line: 2, col: 23, border: [], borderhighlight: ['TopColor', 'RightColor', 'BottomColor', 'LeftColor']})
115 call popup_create(['hello border', 'lines only'], #{line: 2, col: 43, border: [], borderhighlight: ['BlueColor'], borderchars: ['x']})
116 call popup_create(['hello border', 'with corners'], #{line: 2, col: 60, border: [], borderhighlight: ['BlueColor'], borderchars: ['x', '#']})
117 let winid = popup_create(['hello border', 'with numbers'], #{line: 6, col: 3, border: [], borderhighlight: ['BlueColor'], borderchars: ['0', '1', '2', '3', '4', '5', '6', '7']})
118 call popup_create(['hello border', 'just blanks'], #{line: 7, col: 23, border: [], borderhighlight: ['BlueColor'], borderchars: [' ']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200119 func MultiByte()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200120 call popup_create(['hello'], #{line: 8, col: 43, border: [], borderchars: ['─', '│', '─', '│', '┌', '┐', '┘', '└']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200121 endfunc
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200122 END
123 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200124 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 12})
Bram Moolenaar790498b2019-06-01 22:15:29 +0200125 call VerifyScreenDump(buf, 'Test_popupwin_22', {})
126
Bram Moolenaarad24a712019-06-17 20:05:45 +0200127 " check that changing borderchars triggers a redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200128 call term_sendkeys(buf, ":call popup_setoptions(winid, #{borderchars: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']})\<CR>")
Bram Moolenaarad24a712019-06-17 20:05:45 +0200129 call VerifyScreenDump(buf, 'Test_popupwin_23', {})
130
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200131 " check multi-byte border only with 'ambiwidth' single
132 if &ambiwidth == 'single'
133 call term_sendkeys(buf, ":call MultiByte()\<CR>")
134 call VerifyScreenDump(buf, 'Test_popupwin_24', {})
135 endif
136
Bram Moolenaar790498b2019-06-01 22:15:29 +0200137 call StopVimInTerminal(buf)
138 call delete('XtestPopupBorder')
139
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200140 let with_border_or_padding = #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200141 \ line: 2,
142 \ core_line: 3,
143 \ col: 3,
144 \ core_col: 4,
145 \ width: 14,
146 \ core_width: 12,
147 \ height: 3,
148 \ core_height: 1,
149 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100150 \ lastline: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200151 \ scrollbar: 0,
152 \ visible: 1}
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200153 let winid = popup_create('hello border', #{line: 2, col: 3, border: []})",
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200154 call assert_equal(with_border_or_padding, winid->popup_getpos())
Bram Moolenaarae943152019-06-16 22:54:14 +0200155 let options = popup_getoptions(winid)
156 call assert_equal([], options.border)
157 call assert_false(has_key(options, "padding"))
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200158
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200159 let winid = popup_create('hello padding', #{line: 2, col: 3, padding: []})
Bram Moolenaarae943152019-06-16 22:54:14 +0200160 let with_border_or_padding.width = 15
161 let with_border_or_padding.core_width = 13
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200162 call assert_equal(with_border_or_padding, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200163 let options = popup_getoptions(winid)
164 call assert_false(has_key(options, "border"))
165 call assert_equal([], options.padding)
166
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200167 call popup_setoptions(winid, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200168 \ padding: [1, 2, 3, 4],
169 \ border: [4, 0, 7, 8],
170 \ borderhighlight: ['Top', 'Right', 'Bottom', 'Left'],
171 \ borderchars: ['1', '^', '2', '>', '3', 'v', '4', '<'],
Bram Moolenaarae943152019-06-16 22:54:14 +0200172 \ })
173 let options = popup_getoptions(winid)
174 call assert_equal([1, 0, 1, 1], options.border)
175 call assert_equal([1, 2, 3, 4], options.padding)
176 call assert_equal(['Top', 'Right', 'Bottom', 'Left'], options.borderhighlight)
177 call assert_equal(['1', '^', '2', '>', '3', 'v', '4', '<'], options.borderchars)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200178
Bram Moolenaara1396152019-10-20 18:46:05 +0200179 " Check that popup_setoptions() takes the output of popup_getoptions()
180 call popup_setoptions(winid, options)
181 call assert_equal(options, popup_getoptions(winid))
182
Bram Moolenaarb0992022020-01-30 14:55:42 +0100183 " Check that range() doesn't crash
184 call popup_setoptions(winid, #{
185 \ padding: range(1, 4),
186 \ border: range(5, 8),
187 \ borderhighlight: range(4),
188 \ borderchars: range(8),
189 \ })
190
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200191 let winid = popup_create('hello both', #{line: 3, col: 8, border: [], padding: []})
192 call assert_equal(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200193 \ line: 3,
194 \ core_line: 5,
195 \ col: 8,
196 \ core_col: 10,
197 \ width: 14,
198 \ core_width: 10,
199 \ height: 5,
200 \ scrollbar: 0,
201 \ core_height: 1,
202 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100203 \ lastline: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200204 \ visible: 1}, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200205
206 call popup_clear()
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200207endfunc
208
Bram Moolenaarb4230122019-05-30 18:40:53 +0200209func Test_popup_with_syntax_win_execute()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200210 CheckScreendump
211
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200212 let lines =<< trim END
213 call setline(1, range(1, 100))
214 hi PopupColor ctermbg=lightblue
215 let winid = popup_create([
216 \ '#include <stdio.h>',
217 \ 'int main(void)',
218 \ '{',
219 \ ' printf(123);',
220 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200221 \], #{line: 3, col: 25, highlight: 'PopupColor'})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200222 call win_execute(winid, 'set syntax=cpp')
223 END
224 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200225 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200226 call VerifyScreenDump(buf, 'Test_popupwin_10', {})
227
228 " clean up
229 call StopVimInTerminal(buf)
230 call delete('XtestPopup')
231endfunc
232
233func Test_popup_with_syntax_setbufvar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200234 CheckScreendump
235
Bram Moolenaar402502d2019-05-30 22:07:36 +0200236 let lines =<< trim END
237 call setline(1, range(1, 100))
238 hi PopupColor ctermbg=lightgrey
239 let winid = popup_create([
240 \ '#include <stdio.h>',
241 \ 'int main(void)',
242 \ '{',
Bram Moolenaare089c3f2019-07-09 20:25:25 +0200243 \ "\tprintf(567);",
Bram Moolenaar402502d2019-05-30 22:07:36 +0200244 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200245 \], #{line: 3, col: 21, highlight: 'PopupColor'})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200246 call setbufvar(winbufnr(winid), '&syntax', 'cpp')
247 END
248 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200249 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200250 call VerifyScreenDump(buf, 'Test_popupwin_11', {})
251
252 " clean up
253 call StopVimInTerminal(buf)
254 call delete('XtestPopup')
255endfunc
256
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200257func Test_popup_with_matches()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200258 CheckScreendump
259
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200260 let lines =<< trim END
261 call setline(1, ['111 222 333', '444 555 666'])
262 let winid = popup_create([
263 \ '111 222 333',
264 \ '444 555 666',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200265 \], #{line: 3, col: 10, border: []})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200266 set hlsearch
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100267 hi VeryBlue ctermfg=blue guifg=blue
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200268 /666
269 call matchadd('ErrorMsg', '111')
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100270 call matchadd('VeryBlue', '444')
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200271 call win_execute(winid, "call matchadd('ErrorMsg', '111')")
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100272 call win_execute(winid, "call matchadd('VeryBlue', '555')")
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200273 END
274 call writefile(lines, 'XtestPopupMatches')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200275 let buf = RunVimInTerminal('-S XtestPopupMatches', #{rows: 10})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200276 call VerifyScreenDump(buf, 'Test_popupwin_matches', {})
277
278 " clean up
279 call StopVimInTerminal(buf)
280 call delete('XtestPopupMatches')
281endfunc
282
Bram Moolenaar399d8982019-06-02 15:34:29 +0200283func Test_popup_all_corners()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200284 CheckScreendump
285
Bram Moolenaar399d8982019-06-02 15:34:29 +0200286 let lines =<< trim END
287 call setline(1, repeat([repeat('-', 60)], 15))
288 set so=0
289 normal 2G3|r#
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200290 let winid1 = popup_create(['first', 'second'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200291 \ line: 'cursor+1',
292 \ col: 'cursor',
293 \ pos: 'topleft',
294 \ border: [],
295 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200296 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200297 normal 24|r@
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200298 let winid1 = popup_create(['First', 'SeconD'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200299 \ line: 'cursor+1',
300 \ col: 'cursor',
301 \ pos: 'topright',
302 \ border: [],
303 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200304 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200305 normal 9G27|r%
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200306 let winid1 = popup_create(['fiRSt', 'seCOnd'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200307 \ line: 'cursor-1',
308 \ col: 'cursor',
309 \ pos: 'botleft',
310 \ border: [],
311 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200312 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200313 normal 48|r&
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200314 let winid1 = popup_create(['FIrsT', 'SEcoND'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200315 \ line: 'cursor-1',
316 \ col: 'cursor',
317 \ pos: 'botright',
318 \ border: [],
319 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200320 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200321 normal 1G51|r*
322 let winid1 = popup_create(['one', 'two'], #{
323 \ line: 'cursor-1',
324 \ col: 'cursor',
325 \ pos: 'botleft',
326 \ border: [],
327 \ padding: [],
328 \ })
Bram Moolenaar399d8982019-06-02 15:34:29 +0200329 END
330 call writefile(lines, 'XtestPopupCorners')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200331 let buf = RunVimInTerminal('-S XtestPopupCorners', #{rows: 12})
Bram Moolenaar399d8982019-06-02 15:34:29 +0200332 call VerifyScreenDump(buf, 'Test_popupwin_corners', {})
333
334 " clean up
335 call StopVimInTerminal(buf)
336 call delete('XtestPopupCorners')
337endfunc
338
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100339func Test_popup_nospace()
340 CheckScreendump
341
342 let lines =<< trim END
343 call setline(1, repeat([repeat('-', 60)], 15))
344 set so=0
345
346 " cursor in a line in top half, using "botleft" with popup that
347 " does fit
348 normal 5G2|r@
349 let winid1 = popup_create(['one', 'two'], #{
350 \ line: 'cursor-1',
351 \ col: 'cursor',
352 \ pos: 'botleft',
353 \ border: [],
354 \ })
355 " cursor in a line in top half, using "botleft" with popup that
356 " doesn't fit: gets truncated
357 normal 5G9|r#
358 let winid1 = popup_create(['one', 'two', 'tee'], #{
359 \ line: 'cursor-1',
360 \ col: 'cursor',
361 \ pos: 'botleft',
362 \ posinvert: 0,
363 \ border: [],
364 \ })
365 " cursor in a line in top half, using "botleft" with popup that
366 " doesn't fit and 'posinvert' set: flips to below.
367 normal 5G16|r%
368 let winid1 = popup_create(['one', 'two', 'tee'], #{
369 \ line: 'cursor-1',
370 \ col: 'cursor',
371 \ pos: 'botleft',
372 \ border: [],
373 \ })
374 " cursor in a line in bottom half, using "botleft" with popup that
375 " doesn't fit: does not flip.
376 normal 8G23|r*
377 let winid1 = popup_create(['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff'], #{
378 \ line: 'cursor-1',
379 \ col: 'cursor',
380 \ pos: 'botleft',
381 \ border: [],
382 \ })
383
384 " cursor in a line in bottom half, using "topleft" with popup that
385 " does fit
386 normal 8G30|r@
387 let winid1 = popup_create(['one', 'two'], #{
388 \ line: 'cursor+1',
389 \ col: 'cursor',
390 \ pos: 'topleft',
391 \ border: [],
392 \ })
393 " cursor in a line in top half, using "topleft" with popup that
394 " doesn't fit: truncated
395 normal 8G37|r#
396 let winid1 = popup_create(['one', 'two', 'tee'], #{
397 \ line: 'cursor+1',
398 \ col: 'cursor',
399 \ pos: 'topleft',
400 \ posinvert: 0,
401 \ border: [],
402 \ })
403 " cursor in a line in top half, using "topleft" with popup that
Bram Moolenaar5c6b6182019-11-10 17:51:38 +0100404 " doesn't fit and "posinvert" set: flips to above.
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100405 normal 8G44|r%
Bram Moolenaar5c6b6182019-11-10 17:51:38 +0100406 let winid1 = popup_create(['one', 'two', 'tee', 'fou', 'fiv'], #{
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100407 \ line: 'cursor+1',
408 \ col: 'cursor',
409 \ pos: 'topleft',
410 \ border: [],
411 \ })
412 " cursor in a line in top half, using "topleft" with popup that
413 " doesn't fit: does not flip.
414 normal 5G51|r*
415 let winid1 = popup_create(['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff'], #{
416 \ line: 'cursor+1',
417 \ col: 'cursor',
418 \ pos: 'topleft',
419 \ border: [],
420 \ })
421 END
422 call writefile(lines, 'XtestPopupNospace')
423 let buf = RunVimInTerminal('-S XtestPopupNospace', #{rows: 12})
424 call VerifyScreenDump(buf, 'Test_popupwin_nospace', {})
425
426 " clean up
427 call StopVimInTerminal(buf)
428 call delete('XtestPopupNospace')
429endfunc
430
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200431func Test_popup_firstline_dump()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200432 CheckScreendump
433
Bram Moolenaar8d241042019-06-12 23:40:01 +0200434 let lines =<< trim END
435 call setline(1, range(1, 20))
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200436 let winid = popup_create(['1111', '222222', '33333', '44', '5', '666666', '77777', '888', '9999999999999999'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200437 \ maxheight: 4,
438 \ firstline: 3,
Bram Moolenaar8d241042019-06-12 23:40:01 +0200439 \ })
440 END
441 call writefile(lines, 'XtestPopupFirstline')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200442 let buf = RunVimInTerminal('-S XtestPopupFirstline', #{rows: 10})
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200443 call VerifyScreenDump(buf, 'Test_popupwin_firstline_1', {})
444
445 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: -1})\<CR>")
446 call term_sendkeys(buf, ":\<CR>")
447 call VerifyScreenDump(buf, 'Test_popupwin_firstline_2', {})
Bram Moolenaar8d241042019-06-12 23:40:01 +0200448
449 " clean up
450 call StopVimInTerminal(buf)
451 call delete('XtestPopupFirstline')
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200452endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +0200453
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200454func Test_popup_firstline()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200455 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200456 \ maxheight: 2,
457 \ firstline: 3,
Bram Moolenaarae943152019-06-16 22:54:14 +0200458 \ })
459 call assert_equal(3, popup_getoptions(winid).firstline)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200460 call popup_setoptions(winid, #{firstline: 1})
Bram Moolenaarae943152019-06-16 22:54:14 +0200461 call assert_equal(1, popup_getoptions(winid).firstline)
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200462 eval winid->popup_close()
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200463
464 let winid = popup_create(['xxx']->repeat(50), #{
465 \ maxheight: 3,
466 \ firstline: 11,
467 \ })
468 redraw
469 call assert_equal(11, popup_getoptions(winid).firstline)
470 call assert_equal(11, popup_getpos(winid).firstline)
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +0200471 " check line() works with popup window
472 call assert_equal(11, line('.', winid))
473 call assert_equal(50, line('$', winid))
474 call assert_equal(0, line('$', 123456))
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200475
476 " Normal command changes what is displayed but not "firstline"
477 call win_execute(winid, "normal! \<c-y>")
478 call assert_equal(11, popup_getoptions(winid).firstline)
479 call assert_equal(10, popup_getpos(winid).firstline)
480
481 " Making some property change applies "firstline" again
482 call popup_setoptions(winid, #{line: 4})
483 call assert_equal(11, popup_getoptions(winid).firstline)
484 call assert_equal(11, popup_getpos(winid).firstline)
485
486 " Remove "firstline" property and scroll
487 call popup_setoptions(winid, #{firstline: 0})
488 call win_execute(winid, "normal! \<c-y>")
489 call assert_equal(0, popup_getoptions(winid).firstline)
490 call assert_equal(10, popup_getpos(winid).firstline)
491
492 " Making some property change has no side effect
493 call popup_setoptions(winid, #{line: 3})
494 call assert_equal(0, popup_getoptions(winid).firstline)
495 call assert_equal(10, popup_getpos(winid).firstline)
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200496 call popup_close(winid)
Bram Moolenaarae943152019-06-16 22:54:14 +0200497
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100498 " CTRL-D scrolls down half a page
499 let winid = popup_create(['xxx']->repeat(50), #{
500 \ maxheight: 8,
501 \ })
502 redraw
503 call assert_equal(1, popup_getpos(winid).firstline)
504 call win_execute(winid, "normal! \<C-D>")
505 call assert_equal(5, popup_getpos(winid).firstline)
506 call win_execute(winid, "normal! \<C-D>")
507 call assert_equal(9, popup_getpos(winid).firstline)
508 call win_execute(winid, "normal! \<C-U>")
509 call assert_equal(5, popup_getpos(winid).firstline)
510
511 call win_execute(winid, "normal! \<C-F>")
512 call assert_equal(11, popup_getpos(winid).firstline)
513 call win_execute(winid, "normal! \<C-B>")
514 call assert_equal(5, popup_getpos(winid).firstline)
515
Bram Moolenaarae943152019-06-16 22:54:14 +0200516 call popup_close(winid)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200517endfunc
518
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200519func Test_popup_firstline_cursorline()
520 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
521 \ maxheight: 2,
522 \ firstline: 3,
523 \ cursorline: 1,
524 \ })
525 call assert_equal(3, popup_getoptions(winid).firstline)
526 call assert_equal(3, getwininfo(winid)[0].topline)
527 call assert_equal(3, getcurpos(winid)[1])
528
529 call popup_close(winid)
530endfunc
531
Bram Moolenaara112f2d2019-09-01 17:38:09 +0200532func Test_popup_noscrolloff()
533 set scrolloff=5
534 let winid = popup_create(['xxx']->repeat(50), #{
535 \ maxheight: 5,
536 \ firstline: 11,
537 \ })
538 redraw
539 call assert_equal(11, popup_getoptions(winid).firstline)
540 call assert_equal(11, popup_getpos(winid).firstline)
541
542 call popup_setoptions(winid, #{firstline: 0})
543 call win_execute(winid, "normal! \<c-y>")
544 call assert_equal(0, popup_getoptions(winid).firstline)
545 call assert_equal(10, popup_getpos(winid).firstline)
546
547 call popup_close(winid)
548endfunc
549
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200550func Test_popup_drag()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200551 CheckScreendump
552
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200553 " create a popup that covers the command line
554 let lines =<< trim END
555 call setline(1, range(1, 20))
Bram Moolenaar356375f2019-08-24 14:46:29 +0200556 split
557 vsplit
558 $wincmd w
559 vsplit
560 1wincmd w
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200561 let winid = popup_create(['1111', '222222', '33333'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200562 \ drag: 1,
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200563 \ resize: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200564 \ border: [],
565 \ line: &lines - 4,
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200566 \ })
567 func Dragit()
568 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
569 endfunc
570 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
Bram Moolenaar356375f2019-08-24 14:46:29 +0200571 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2 - 20)<CR>
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200572 func Resize()
573 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
574 endfunc
Bram Moolenaar356375f2019-08-24 14:46:29 +0200575 map <silent> <F5> :call test_setmouse(6, 21)<CR>
576 map <silent> <F6> :call test_setmouse(7, 25)<CR>
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200577 END
578 call writefile(lines, 'XtestPopupDrag')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200579 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200580 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
581
582 call term_sendkeys(buf, ":call Dragit()\<CR>")
583 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
584
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200585 call term_sendkeys(buf, ":call Resize()\<CR>")
586 call VerifyScreenDump(buf, 'Test_popupwin_drag_03', {})
587
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200588 " clean up
589 call StopVimInTerminal(buf)
590 call delete('XtestPopupDrag')
591endfunc
592
Bram Moolenaar35910f22020-07-12 19:24:10 +0200593func Test_popup_drag_termwin()
594 CheckUnix
595 CheckScreendump
596 CheckFeature terminal
597
598 " create a popup that covers the terminal window
599 let lines =<< trim END
Bram Moolenaar452143c2020-07-15 17:38:21 +0200600 set foldmethod=marker
601 call setline(1, range(100))
602 for nr in range(7)
603 call setline(nr * 12 + 1, "fold {{{")
Bram Moolenaar9d489562020-07-30 20:08:50 +0200604 call setline(nr * 12 + 11, "end }}}")
Bram Moolenaar452143c2020-07-15 17:38:21 +0200605 endfor
606 %foldclose
Bram Moolenaar35910f22020-07-12 19:24:10 +0200607 set shell=/bin/sh noruler
Bram Moolenaar4b2ce122020-11-21 21:41:41 +0100608 unlet $PROMPT_COMMAND
Bram Moolenaara4dc6f92020-07-12 19:52:36 +0200609 let $PS1 = 'vim> '
Bram Moolenaar452143c2020-07-15 17:38:21 +0200610 terminal ++rows=4
Bram Moolenaar35910f22020-07-12 19:24:10 +0200611 $wincmd w
612 let winid = popup_create(['1111', '2222'], #{
613 \ drag: 1,
614 \ resize: 1,
615 \ border: [],
616 \ line: 3,
617 \ })
Bram Moolenaar452143c2020-07-15 17:38:21 +0200618 func DragitLeft()
Bram Moolenaar35910f22020-07-12 19:24:10 +0200619 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
620 endfunc
Bram Moolenaar452143c2020-07-15 17:38:21 +0200621 func DragitDown()
622 call feedkeys("\<F4>\<LeftMouse>\<F5>\<LeftDrag>\<LeftRelease>", "xt")
623 endfunc
624 func DragitDownLeft()
625 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
626 endfunc
Bram Moolenaar35910f22020-07-12 19:24:10 +0200627 map <silent> <F3> :call test_setmouse(3, &columns / 2)<CR>
628 map <silent> <F4> :call test_setmouse(3, &columns / 2 - 20)<CR>
Bram Moolenaar452143c2020-07-15 17:38:21 +0200629 map <silent> <F5> :call test_setmouse(12, &columns / 2)<CR>
630 map <silent> <F6> :call test_setmouse(12, &columns / 2 - 20)<CR>
Bram Moolenaar35910f22020-07-12 19:24:10 +0200631 END
632 call writefile(lines, 'XtestPopupTerm')
Bram Moolenaar452143c2020-07-15 17:38:21 +0200633 let buf = RunVimInTerminal('-S XtestPopupTerm', #{rows: 16})
Bram Moolenaar35910f22020-07-12 19:24:10 +0200634 call VerifyScreenDump(buf, 'Test_popupwin_term_01', {})
635
Bram Moolenaar452143c2020-07-15 17:38:21 +0200636 call term_sendkeys(buf, ":call DragitLeft()\<CR>")
Bram Moolenaar35910f22020-07-12 19:24:10 +0200637 call VerifyScreenDump(buf, 'Test_popupwin_term_02', {})
638
Bram Moolenaar452143c2020-07-15 17:38:21 +0200639 call term_sendkeys(buf, ":call DragitDown()\<CR>")
640 call VerifyScreenDump(buf, 'Test_popupwin_term_03', {})
641
642 call term_sendkeys(buf, ":call DragitDownLeft()\<CR>")
643 call VerifyScreenDump(buf, 'Test_popupwin_term_04', {})
644
Bram Moolenaar35910f22020-07-12 19:24:10 +0200645 " clean up
646 call StopVimInTerminal(buf)
647 call delete('XtestPopupTerm')
648endfunc
649
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200650func Test_popup_close_with_mouse()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200651 CheckScreendump
652
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200653 let lines =<< trim END
654 call setline(1, range(1, 20))
655 " With border, can click on X
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200656 let winid = popup_create('foobar', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200657 \ close: 'button',
658 \ border: [],
659 \ line: 1,
660 \ col: 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200661 \ })
662 func CloseMsg(id, result)
663 echomsg 'Popup closed with ' .. a:result
664 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200665 let winid = popup_create('notification', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200666 \ close: 'click',
667 \ line: 3,
668 \ col: 15,
669 \ callback: 'CloseMsg',
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200670 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200671 let winid = popup_create('no border here', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200672 \ close: 'button',
673 \ line: 5,
674 \ col: 3,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200675 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200676 let winid = popup_create('only padding', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200677 \ close: 'button',
678 \ padding: [],
679 \ line: 5,
680 \ col: 23,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200681 \ })
682 func CloseWithX()
683 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
684 endfunc
685 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
686 func CloseWithClick()
687 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
688 endfunc
689 map <silent> <F4> :call test_setmouse(3, 17)<CR>
Bram Moolenaarf6396232019-08-24 19:36:00 +0200690 func CreateWithMenuFilter()
691 let winid = popup_create('barfoo', #{
692 \ close: 'button',
693 \ filter: 'popup_filter_menu',
694 \ border: [],
695 \ line: 1,
696 \ col: 40,
697 \ })
698 endfunc
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200699 END
700 call writefile(lines, 'XtestPopupClose')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200701 let buf = RunVimInTerminal('-S XtestPopupClose', #{rows: 10})
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200702 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
703
704 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
705 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
706
707 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
708 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
709
Bram Moolenaarf6396232019-08-24 19:36:00 +0200710 call term_sendkeys(buf, ":call CreateWithMenuFilter()\<CR>")
711 call VerifyScreenDump(buf, 'Test_popupwin_close_04', {})
712
713 " We have to send the actual mouse code, feedkeys() would be caught the
714 " filter.
715 call term_sendkeys(buf, "\<Esc>[<0;47;1M")
716 call VerifyScreenDump(buf, 'Test_popupwin_close_05', {})
717
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200718 " clean up
719 call StopVimInTerminal(buf)
720 call delete('XtestPopupClose')
721endfunction
722
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200723func Test_popup_menu_wrap()
724 CheckScreendump
725
726 let lines =<< trim END
727 call setline(1, range(1, 20))
728 call popup_create([
729 \ 'one',
730 \ 'asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfas',
731 \ 'three',
732 \ 'four',
733 \ ], #{
734 \ pos: "botleft",
735 \ border: [],
736 \ padding: [0,1,0,1],
737 \ maxheight: 3,
738 \ cursorline: 1,
739 \ filter: 'popup_filter_menu',
740 \ })
741 END
742 call writefile(lines, 'XtestPopupWrap')
743 let buf = RunVimInTerminal('-S XtestPopupWrap', #{rows: 10})
744 call VerifyScreenDump(buf, 'Test_popupwin_wrap_1', {})
745
746 call term_sendkeys(buf, "jj")
747 call VerifyScreenDump(buf, 'Test_popupwin_wrap_2', {})
748
749 " clean up
750 call term_sendkeys(buf, "\<Esc>")
751 call StopVimInTerminal(buf)
752 call delete('XtestPopupWrap')
753endfunction
754
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200755func Test_popup_with_mask()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200756 CheckScreendump
757
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200758 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200759 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200760 hi PopupColor ctermbg=lightgrey
761 let winid = popup_create([
762 \ 'some text',
763 \ 'another line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200764 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200765 \ line: 1,
766 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100767 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200768 \ wrap: 0,
769 \ fixed: 1,
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +0200770 \ scrollbar: v:false,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200771 \ zindex: 90,
772 \ padding: [],
773 \ highlight: 'PopupColor',
774 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200775 call popup_create([
776 \ 'xxxxxxxxx',
777 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200778 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200779 \ line: 3,
780 \ col: 18,
781 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200782 let winidb = popup_create([
783 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200784 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200785 \ line: 7,
786 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100787 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200788 \ wrap: 0,
789 \ fixed: 1,
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +0200790 \ scrollbar: v:false,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200791 \ close: 'button',
792 \ zindex: 90,
793 \ padding: [],
794 \ border: [],
795 \ 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 +0200796 END
797 call writefile(lines, 'XtestPopupMask')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200798 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200799 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
800
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200801 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
802 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200803 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200804 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
805
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200806 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
807 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200808 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200809 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
810
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200811 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
812 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200813 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200814 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
815
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200816 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
817 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200818 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200819 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
820
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200821 " clean up
822 call StopVimInTerminal(buf)
823 call delete('XtestPopupMask')
Bram Moolenaar4012d262020-12-29 11:57:46 +0100824
825 " this was causing a crash
826 call popup_create('test', #{mask: [[0, 0, 0, 0]]})
827 call popup_clear()
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200828endfunc
829
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200830func Test_popup_select()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200831 CheckScreendump
832 CheckFeature clipboard_working
833
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200834 " create a popup with some text to be selected
835 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200836 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200837 call setline(1, range(1, 20))
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200838 let winid = popup_create(['the word', 'some more', 'several words here', 'invisible', '5', '6', '7'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200839 \ drag: 1,
840 \ border: [],
841 \ line: 3,
842 \ col: 10,
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200843 \ maxheight: 3,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200844 \ })
845 func Select1()
846 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
847 endfunc
848 map <silent> <F3> :call test_setmouse(4, 15)<CR>
849 map <silent> <F4> :call test_setmouse(6, 23)<CR>
850 END
851 call writefile(lines, 'XtestPopupSelect')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200852 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200853 call term_sendkeys(buf, ":call Select1()\<CR>")
854 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
855
856 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
857 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200858 " clean the command line, sometimes it still shows a command
859 call term_sendkeys(buf, ":\<esc>")
860
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200861 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
862
863 " clean up
864 call StopVimInTerminal(buf)
865 call delete('XtestPopupSelect')
866endfunc
867
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200868func Test_popup_in_tab()
869 " default popup is local to tab, not visible when in other tab
870 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200871 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200872 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200873 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200874 tabnew
875 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200876 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200877 quit
878 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200879
880 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200881 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200882 " buffer is gone now
883 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200884
885 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200886 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200887 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200888 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200889 tabnew
890 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200891 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200892 quit
893 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200894 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200895
896 " create popup in other tab
897 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200898 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200899 call assert_equal(0, popup_getpos(winid).visible)
900 call assert_equal(1, popup_getoptions(winid).tabpage)
901 quit
902 call assert_equal(1, popup_getpos(winid).visible)
903 call assert_equal(0, popup_getoptions(winid).tabpage)
904 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200905endfunc
906
907func Test_popup_valid_arguments()
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200908 call assert_equal(0, len(popup_list()))
909
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200910 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200911 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200912 let pos = popup_getpos(winid)
913 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200914 call assert_equal([winid], popup_list())
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200915 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200916
917 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200918 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200919 let pos = popup_getpos(winid)
920 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200921 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200922
923 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200924 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200925 let pos = popup_getpos(winid)
926 let around = (&columns - pos.width) / 2
927 call assert_inrange(around - 1, around + 1, pos.col)
928 let around = (&lines - pos.height) / 2
929 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200930 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200931endfunc
932
933func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200934 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200935 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200936 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200937 call popup_clear()
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200938 call assert_fails('call popup_create(test_null_string(), {})', 'E450:')
939 call assert_fails('call popup_create(test_null_list(), {})', 'E450:')
940 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200941
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200942 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200943 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200944 call assert_fails('call popup_create("text", #{col: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200945 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200946 call assert_fails('call popup_create("text", #{col: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200947 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200948 call assert_fails('call popup_create("text", #{col: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200949 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200950
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200951 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200952 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200953 call assert_fails('call popup_create("text", #{line: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200954 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200955 call assert_fails('call popup_create("text", #{line: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200956 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200957 call assert_fails('call popup_create("text", #{line: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200958 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200959
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200960 call assert_fails('call popup_create("text", #{pos: "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200961 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200962 call assert_fails('call popup_create("text", #{padding: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200963 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200964 call assert_fails('call popup_create("text", #{border: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200965 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200966 call assert_fails('call popup_create("text", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200967 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +0200968 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
969 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200970 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200971 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200972
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200973 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200974 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200975 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200976 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200977 call assert_fails('call popup_create([#{text: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200978 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +0100979 call assert_fails('call popup_create([#{text: "text", props: range(3)}], {})', 'E715:')
980 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200981 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
982 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +0100983 call assert_fails('call popup_create("text", #{mask: range(5)})', 'E475:')
984 call popup_clear()
985 call popup_create("text", #{mask: [range(4)]})
986 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200987 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200988 call assert_fails('call popup_create("text", #{mapping: []})', 'E745:')
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200989 call popup_clear()
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200990 call assert_fails('call popup_create("text", #{tabpage : 4})', 'E997:')
991 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200992endfunc
993
Bram Moolenaareea16992019-05-31 17:34:48 +0200994func Test_win_execute_closing_curwin()
995 split
996 let winid = popup_create('some text', {})
Bram Moolenaare2e40752020-09-04 21:18:46 +0200997 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200998 call popup_clear()
Bram Moolenaar49540192019-12-11 19:34:54 +0100999
1000 let winid = popup_create('some text', {})
Bram Moolenaare2e40752020-09-04 21:18:46 +02001001 call assert_fails('call win_execute(winid, printf("normal! :\<C-u>call popup_close(%d)\<CR>", winid))', 'E994:')
Bram Moolenaar49540192019-12-11 19:34:54 +01001002 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001003endfunc
1004
1005func Test_win_execute_not_allowed()
1006 let winid = popup_create('some text', {})
1007 call assert_fails('call win_execute(winid, "split")', 'E994:')
1008 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
1009 call assert_fails('call win_execute(winid, "close")', 'E994:')
1010 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +02001011 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001012 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
1013 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
1014 call assert_fails('call win_execute(winid, "next")', 'E994:')
1015 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01001016 call assert_fails('call win_execute(winid, "pedit filename")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001017 call assert_fails('call win_execute(winid, "buf")', 'E994:')
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01001018 call assert_fails('call win_execute(winid, "bnext")', 'E994:')
1019 call assert_fails('call win_execute(winid, "bprev")', 'E994:')
1020 call assert_fails('call win_execute(winid, "bfirst")', 'E994:')
1021 call assert_fails('call win_execute(winid, "blast")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001022 call assert_fails('call win_execute(winid, "edit")', 'E994:')
1023 call assert_fails('call win_execute(winid, "enew")', 'E994:')
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001024 call assert_fails('call win_execute(winid, "help")', 'E994:')
1025 call assert_fails('call win_execute(winid, "1only")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001026 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
1027 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
1028 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
1029 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001030 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +02001031endfunc
1032
Bram Moolenaar402502d2019-05-30 22:07:36 +02001033func Test_popup_with_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001034 CheckScreendump
1035
Bram Moolenaar402502d2019-05-30 22:07:36 +02001036 let lines =<< trim END
1037 call setline(1, range(1, 100))
1038 let winid = popup_create(
1039 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001040 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001041 END
1042 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001043 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001044 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
1045
1046 " clean up
1047 call StopVimInTerminal(buf)
1048 call delete('XtestPopup')
1049endfunc
1050
1051func Test_popup_without_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001052 CheckScreendump
1053
Bram Moolenaar402502d2019-05-30 22:07:36 +02001054 let lines =<< trim END
1055 call setline(1, range(1, 100))
1056 let winid = popup_create(
1057 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001058 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001059 END
1060 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001061 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001062 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
1063
1064 " clean up
1065 call StopVimInTerminal(buf)
1066 call delete('XtestPopup')
1067endfunc
1068
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001069func Test_popup_with_showbreak()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001070 CheckScreendump
1071
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001072 let lines =<< trim END
1073 set showbreak=>>\
1074 call setline(1, range(1, 20))
1075 let winid = popup_dialog(
Bram Moolenaar8ae54372019-09-15 18:11:16 +02001076 \ 'a long line here that wraps',
1077 \ #{filter: 'popup_filter_yesno',
1078 \ maxwidth: 12})
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001079 END
1080 call writefile(lines, 'XtestPopupShowbreak')
1081 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
1082 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
1083
1084 " clean up
1085 call term_sendkeys(buf, "y")
1086 call StopVimInTerminal(buf)
1087 call delete('XtestPopupShowbreak')
1088endfunc
1089
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001090func Test_popup_time()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001091 CheckFeature timers
1092
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001093 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,
1100 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001101 \})
1102 redraw
1103 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1104 call assert_equal('world', line)
1105
Bram Moolenaarb4f06282019-07-12 21:07:54 +02001106 call assert_equal(winid, popup_locate(1, 1))
1107 call assert_equal(winid, popup_locate(1, 20))
1108 call assert_equal(0, popup_locate(1, 21))
1109 call assert_equal(0, popup_locate(2, 1))
1110
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001111 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +02001112 redraw
Bram Moolenaar196b4662019-09-06 21:34:30 +02001113 let line = join(map(range(1, 5), '1->screenstring(v:val)'), '')
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001114 call assert_equal('hello', line)
1115
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001116 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001117 \ line: &lines,
1118 \ col: 10,
1119 \ minwidth: 20,
1120 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001121 \})
1122 redraw
1123 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1124 call assert_match('.*on the command line.*', line)
1125
1126 sleep 700m
1127 redraw
1128 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1129 call assert_notmatch('.*on the command line.*', line)
1130
1131 bwipe!
1132endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001133
1134func Test_popup_hide()
1135 topleft vnew
1136 call setline(1, 'hello')
1137
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001138 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001139 \ line: 1,
1140 \ col: 1,
1141 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001142 \})
1143 redraw
1144 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1145 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001146 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001147 " buffer is still listed and active
1148 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001149
1150 call popup_hide(winid)
1151 redraw
1152 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1153 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001154 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001155 " buffer is still listed but hidden
Bram Moolenaarca7c0782020-01-14 20:42:48 +01001156 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001157
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001158 eval winid->popup_show()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001159 redraw
1160 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1161 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001162 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001163
1164
1165 call popup_close(winid)
1166 redraw
1167 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1168 call assert_equal('hello', line)
1169
1170 " error is given for existing non-popup window
1171 call assert_fails('call popup_hide(win_getid())', 'E993:')
1172
1173 " no error non-existing window
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001174 eval 1234234->popup_hide()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001175 call popup_show(41234234)
1176
1177 bwipe!
1178endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001179
1180func Test_popup_move()
1181 topleft vnew
1182 call setline(1, 'hello')
1183
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001184 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001185 \ line: 1,
1186 \ col: 1,
1187 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001188 \})
1189 redraw
1190 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1191 call assert_equal('world ', line)
1192
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001193 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001194 redraw
1195 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1196 call assert_equal('hello ', line)
1197 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
1198 call assert_equal('~world', line)
1199
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001200 eval winid->popup_move(#{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001201 redraw
1202 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1203 call assert_equal('hworld', line)
1204
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001205 call assert_fails('call popup_move(winid, [])', 'E715:')
1206 call assert_fails('call popup_move(winid, test_null_dict())', 'E715:')
1207
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001208 call popup_close(winid)
1209
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001210 call assert_equal(0, popup_move(-1, {}))
1211
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001212 bwipe!
1213endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +02001214
Bram Moolenaar402502d2019-05-30 22:07:36 +02001215func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001216 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001217 \ line: 2,
1218 \ col: 3,
1219 \ minwidth: 10,
1220 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +02001221 \})
1222 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001223 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001224 call assert_equal(2, res.line)
1225 call assert_equal(3, res.col)
1226 call assert_equal(10, res.width)
1227 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001228 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001229
1230 call popup_close(winid)
1231endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001232
1233func Test_popup_width_longest()
1234 let tests = [
1235 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
1236 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
1237 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
1238 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
1239 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
1240 \ ]
1241
1242 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001243 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001244 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001245 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001246 call assert_equal(test[1], position.width)
1247 call popup_close(winid)
1248 endfor
1249endfunc
1250
1251func Test_popup_wraps()
1252 let tests = [
1253 \ ['nowrap', 6, 1],
1254 \ ['a line that wraps once', 12, 2],
1255 \ ['a line that wraps two times', 12, 3],
1256 \ ]
1257 for test in tests
1258 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001259 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001260 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001261 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001262 call assert_equal(test[1], position.width)
1263 call assert_equal(test[2], position.height)
1264
1265 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001266 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001267 endfor
1268endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001269
1270func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001271 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001272 \ line: 2,
1273 \ col: 3,
1274 \ minwidth: 10,
1275 \ minheight: 11,
1276 \ maxwidth: 20,
1277 \ maxheight: 21,
1278 \ zindex: 100,
1279 \ time: 5000,
1280 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001281 \})
1282 redraw
1283 let res = popup_getoptions(winid)
1284 call assert_equal(2, res.line)
1285 call assert_equal(3, res.col)
1286 call assert_equal(10, res.minwidth)
1287 call assert_equal(11, res.minheight)
1288 call assert_equal(20, res.maxwidth)
1289 call assert_equal(21, res.maxheight)
1290 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001291 call assert_equal(1, res.fixed)
Bram Moolenaarb8350ab2019-08-04 17:59:49 +02001292 call assert_equal(1, res.mapping)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001293 if has('timers')
1294 call assert_equal(5000, res.time)
1295 endif
1296 call popup_close(winid)
1297
1298 let winid = popup_create('hello', {})
1299 redraw
1300 let res = popup_getoptions(winid)
1301 call assert_equal(0, res.line)
1302 call assert_equal(0, res.col)
1303 call assert_equal(0, res.minwidth)
1304 call assert_equal(0, res.minheight)
1305 call assert_equal(0, res.maxwidth)
1306 call assert_equal(0, res.maxheight)
1307 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001308 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001309 if has('timers')
1310 call assert_equal(0, res.time)
1311 endif
1312 call popup_close(winid)
1313 call assert_equal({}, popup_getoptions(winid))
1314endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001315
1316func Test_popup_option_values()
1317 new
1318 " window-local
1319 setlocal number
1320 setlocal nowrap
1321 " buffer-local
1322 setlocal omnifunc=Something
1323 " global/buffer-local
1324 setlocal path=/there
1325 " global/window-local
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001326 setlocal statusline=2
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001327
1328 let winid = popup_create('hello', {})
1329 call assert_equal(0, getwinvar(winid, '&number'))
1330 call assert_equal(1, getwinvar(winid, '&wrap'))
1331 call assert_equal('', getwinvar(winid, '&omnifunc'))
1332 call assert_equal(&g:path, getwinvar(winid, '&path'))
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001333 call assert_equal(&g:statusline, getwinvar(winid, '&statusline'))
1334
1335 " 'scrolloff' is reset to zero
1336 call assert_equal(5, &scrolloff)
1337 call assert_equal(0, getwinvar(winid, '&scrolloff'))
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001338
1339 call popup_close(winid)
1340 bwipe
1341endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001342
1343func Test_popup_atcursor()
1344 topleft vnew
1345 call setline(1, [
1346 \ 'xxxxxxxxxxxxxxxxx',
1347 \ 'xxxxxxxxxxxxxxxxx',
1348 \ 'xxxxxxxxxxxxxxxxx',
1349 \])
1350
1351 call cursor(2, 2)
1352 redraw
1353 let winid = popup_atcursor('vim', {})
1354 redraw
1355 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1356 call assert_equal('xvimxxxxxxxxxxxxx', line)
1357 call popup_close(winid)
1358
1359 call cursor(3, 4)
1360 redraw
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001361 let winid = 'vim'->popup_atcursor({})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001362 redraw
1363 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
1364 call assert_equal('xxxvimxxxxxxxxxxx', line)
1365 call popup_close(winid)
1366
1367 call cursor(1, 1)
1368 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001369 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001370 \ line: 'cursor+2',
1371 \ col: 'cursor+1',
1372 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001373 redraw
1374 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1375 call assert_equal('xvimxxxxxxxxxxxxx', line)
1376 call popup_close(winid)
1377
1378 call cursor(3, 3)
1379 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001380 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001381 \ line: 'cursor-2',
1382 \ col: 'cursor-1',
1383 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001384 redraw
1385 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1386 call assert_equal('xvimxxxxxxxxxxxxx', line)
1387 call popup_close(winid)
1388
Bram Moolenaar402502d2019-05-30 22:07:36 +02001389 " just enough room above
1390 call cursor(3, 3)
1391 redraw
1392 let winid = popup_atcursor(['vim', 'is great'], {})
1393 redraw
1394 let pos = popup_getpos(winid)
1395 call assert_equal(1, pos.line)
1396 call popup_close(winid)
1397
1398 " not enough room above, popup goes below the cursor
1399 call cursor(3, 3)
1400 redraw
1401 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1402 redraw
1403 let pos = popup_getpos(winid)
1404 call assert_equal(4, pos.line)
1405 call popup_close(winid)
1406
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001407 " cursor in first line, popup in line 2
1408 call cursor(1, 1)
1409 redraw
1410 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1411 redraw
1412 let pos = popup_getpos(winid)
1413 call assert_equal(2, pos.line)
1414 call popup_close(winid)
1415
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001416 bwipe!
1417endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001418
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001419func Test_popup_atcursor_pos()
1420 CheckScreendump
1421
1422 let lines =<< trim END
1423 call setline(1, repeat([repeat('-', 60)], 15))
1424 set so=0
1425
1426 normal 9G3|r#
1427 let winid1 = popup_atcursor(['first', 'second'], #{
1428 \ moved: [0, 0, 0],
1429 \ })
1430 normal 9G21|r&
1431 let winid1 = popup_atcursor(['FIrsT', 'SEcoND'], #{
1432 \ pos: 'botright',
1433 \ moved: [0, 0, 0],
1434 \ })
1435 normal 3G27|r%
1436 let winid1 = popup_atcursor(['fiRSt', 'seCOnd'], #{
1437 \ pos: 'topleft',
1438 \ moved: [0, 0, 0],
1439 \ })
1440 normal 3G45|r@
1441 let winid1 = popup_atcursor(['First', 'SeconD'], #{
1442 \ pos: 'topright',
Bram Moolenaarb0992022020-01-30 14:55:42 +01001443 \ moved: range(3),
1444 \ mousemoved: range(3),
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001445 \ })
1446 END
1447 call writefile(lines, 'XtestPopupAtcursorPos')
1448 let buf = RunVimInTerminal('-S XtestPopupAtcursorPos', #{rows: 12})
1449 call VerifyScreenDump(buf, 'Test_popupwin_atcursor_pos', {})
1450
1451 " clean up
1452 call StopVimInTerminal(buf)
1453 call delete('XtestPopupAtcursorPos')
1454endfunc
1455
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001456func Test_popup_beval()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001457 CheckScreendump
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001458 CheckFeature balloon_eval_term
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001459
1460 let lines =<< trim END
1461 call setline(1, range(1, 20))
1462 call setline(5, 'here is some text to hover over')
1463 set balloonevalterm
1464 set balloonexpr=BalloonExpr()
1465 set balloondelay=100
1466 func BalloonExpr()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001467 let s:winid = [v:beval_text]->popup_beval({})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001468 return ''
1469 endfunc
1470 func Hover()
1471 call test_setmouse(5, 15)
1472 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1473 sleep 100m
1474 endfunc
1475 func MoveOntoPopup()
1476 call test_setmouse(4, 17)
1477 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1478 endfunc
1479 func MoveAway()
1480 call test_setmouse(5, 13)
1481 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1482 endfunc
1483 END
1484 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001485 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001486 call TermWait(buf, 50)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001487 call term_sendkeys(buf, 'j')
1488 call term_sendkeys(buf, ":call Hover()\<CR>")
1489 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1490
1491 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1492 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1493
1494 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1495 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1496
1497 " clean up
1498 call StopVimInTerminal(buf)
1499 call delete('XtestPopupBeval')
1500endfunc
1501
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001502func Test_popup_filter()
1503 new
1504 call setline(1, 'some text')
1505
1506 func MyPopupFilter(winid, c)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001507 if a:c == 'e' || a:c == "\<F9>"
1508 let g:eaten = a:c
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001509 return 1
1510 endif
1511 if a:c == '0'
1512 let g:ignored = '0'
1513 return 0
1514 endif
1515 if a:c == 'x'
1516 call popup_close(a:winid)
1517 return 1
1518 endif
1519 return 0
1520 endfunc
1521
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001522 let winid = 'something'->popup_create(#{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001523 redraw
1524
1525 " e is consumed by the filter
1526 call feedkeys('e', 'xt')
1527 call assert_equal('e', g:eaten)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001528 call feedkeys("\<F9>", 'xt')
1529 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001530
1531 " 0 is ignored by the filter
1532 normal $
1533 call assert_equal(9, getcurpos()[2])
1534 call feedkeys('0', 'xt')
1535 call assert_equal('0', g:ignored)
1536 call assert_equal(1, getcurpos()[2])
1537
1538 " x closes the popup
1539 call feedkeys('x', 'xt')
Bram Moolenaarec084d32020-02-28 22:44:47 +01001540 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001541 call assert_equal(-1, winbufnr(winid))
1542
1543 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001544 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001545endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001546
Bram Moolenaar4934ad02020-09-28 22:29:58 +02001547" this tests that the filter is not used for :normal command
Bram Moolenaar189832b2020-09-23 12:29:11 +02001548func Test_popup_filter_normal_cmd()
1549 CheckScreendump
1550
1551 let lines =<< trim END
Bram Moolenaar4934ad02020-09-28 22:29:58 +02001552 let text = range(1, 20)->map({_, v -> string(v)})
1553 let g:winid = popup_create(text, #{maxheight: 5, minwidth: 3, filter: 'invalidfilter'})
1554 call timer_start(0, {-> win_execute(g:winid, 'norm! 10Gzz')})
Bram Moolenaar189832b2020-09-23 12:29:11 +02001555 END
1556 call writefile(lines, 'XtestPopupNormal')
1557 let buf = RunVimInTerminal('-S XtestPopupNormal', #{rows: 10})
1558 call TermWait(buf, 100)
1559 call VerifyScreenDump(buf, 'Test_popupwin_normal_cmd', {})
1560
1561 call StopVimInTerminal(buf)
1562 call delete('XtestPopupNormal')
1563endfunc
1564
Bram Moolenaar10476522020-09-24 22:57:31 +02001565" test that cursor line highlight is updated after using win_execute()
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001566func Test_popup_filter_win_execute()
1567 CheckScreendump
1568
1569 let lines =<< trim END
Bram Moolenaar10476522020-09-24 22:57:31 +02001570 let lines = range(1, &lines * 2)->map({_, v -> string(v)})
1571 let g:id = popup_create(lines, #{
1572 \ minheight: &lines - 5,
1573 \ maxheight: &lines - 5,
1574 \ cursorline: 1,
1575 \ })
1576 redraw
1577 END
1578 call writefile(lines, 'XtestPopupWinExecute')
1579 let buf = RunVimInTerminal('-S XtestPopupWinExecute', #{rows: 14})
1580
1581 call term_sendkeys(buf, ":call win_execute(g:id, ['normal 17Gzz'])\<CR>")
1582 call term_sendkeys(buf, ":\<CR>")
1583
1584 call VerifyScreenDump(buf, 'Test_popupwin_win_execute_cursorline', {})
1585
1586 call StopVimInTerminal(buf)
1587 call delete('XtestPopupWinExecute')
1588endfunc
1589
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001590func Test_popup_set_firstline()
1591 CheckScreendump
1592
1593 let lines =<< trim END
1594 let lines = range(1, 50)->map({_, v -> string(v)})
1595 let g:id = popup_create(lines, #{
1596 \ minwidth: 20,
1597 \ maxwidth: 20,
1598 \ minheight: &lines - 5,
1599 \ maxheight: &lines - 5,
1600 \ cursorline: 1,
1601 \ })
1602 call popup_setoptions(g:id, #{firstline: 10})
1603 redraw
1604 END
1605 call writefile(lines, 'XtestPopupWinSetFirstline')
1606 let buf = RunVimInTerminal('-S XtestPopupWinSetFirstline', #{rows: 16})
1607
1608 call VerifyScreenDump(buf, 'Test_popupwin_set_firstline_1', {})
1609
1610 call term_sendkeys(buf, ":call popup_setoptions(g:id, #{firstline: 5})\<CR>")
1611 call term_sendkeys(buf, ":\<CR>")
1612 call VerifyScreenDump(buf, 'Test_popupwin_set_firstline_2', {})
1613
1614 call StopVimInTerminal(buf)
1615 call delete('XtestPopupWinSetFirstline')
1616endfunc
1617
Bram Moolenaar10476522020-09-24 22:57:31 +02001618" this tests that we don't get stuck with an error in "win_execute()"
1619func Test_popup_filter_win_execute_error()
1620 CheckScreendump
1621
1622 let lines =<< trim END
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001623 let g:winid = popup_create('some text', {'filter': 'invalidfilter'})
1624 call timer_start(0, {-> win_execute(g:winid, 'invalidCommand')})
1625 END
Bram Moolenaar10476522020-09-24 22:57:31 +02001626 call writefile(lines, 'XtestPopupWinExecuteError')
1627 let buf = RunVimInTerminal('-S XtestPopupWinExecuteError', #{rows: 10, wait_for_ruler: 0})
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001628
1629 call WaitFor({-> term_getline(buf, 9) =~ 'Not an editor command: invalidCommand'})
1630 call term_sendkeys(buf, "\<CR>")
1631 call WaitFor({-> term_getline(buf, 9) =~ 'Unknown function: invalidfilter'})
1632 call term_sendkeys(buf, "\<CR>")
1633 call WaitFor({-> term_getline(buf, 9) =~ 'Not allowed in a popup window'})
1634 call term_sendkeys(buf, "\<CR>")
1635 call term_sendkeys(buf, "\<CR>")
1636 call VerifyScreenDump(buf, 'Test_popupwin_win_execute', {})
1637
1638 call StopVimInTerminal(buf)
Bram Moolenaar10476522020-09-24 22:57:31 +02001639 call delete('XtestPopupWinExecuteError')
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001640endfunc
1641
Bram Moolenaara42d9452019-06-15 21:46:30 +02001642func ShowDialog(key, result)
1643 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001644 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001645 \ filter: 'popup_filter_yesno',
1646 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001647 \ })
1648 redraw
1649 call feedkeys(a:key, "xt")
1650 call assert_equal(winid, s:cb_winid)
1651 call assert_equal(a:result, s:cb_res)
1652endfunc
1653
1654func Test_popup_dialog()
1655 func QuitCallback(id, res)
1656 let s:cb_winid = a:id
1657 let s:cb_res = a:res
1658 endfunc
1659
1660 let winid = ShowDialog("y", 1)
1661 let winid = ShowDialog("Y", 1)
1662 let winid = ShowDialog("n", 0)
1663 let winid = ShowDialog("N", 0)
1664 let winid = ShowDialog("x", 0)
1665 let winid = ShowDialog("X", 0)
1666 let winid = ShowDialog("\<Esc>", 0)
1667 let winid = ShowDialog("\<C-C>", -1)
1668
1669 delfunc QuitCallback
1670endfunc
1671
Bram Moolenaara730e552019-06-16 19:05:31 +02001672func ShowMenu(key, result)
1673 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001674 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001675 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001676 \ })
1677 redraw
1678 call feedkeys(a:key, "xt")
1679 call assert_equal(winid, s:cb_winid)
1680 call assert_equal(a:result, s:cb_res)
1681endfunc
1682
1683func Test_popup_menu()
1684 func QuitCallback(id, res)
1685 let s:cb_winid = a:id
1686 let s:cb_res = a:res
1687 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001688 " mapping won't be used in popup
1689 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001690
1691 let winid = ShowMenu(" ", 1)
1692 let winid = ShowMenu("j \<CR>", 2)
1693 let winid = ShowMenu("JjK \<CR>", 2)
1694 let winid = ShowMenu("jjjjjj ", 3)
1695 let winid = ShowMenu("kkk ", 1)
1696 let winid = ShowMenu("x", -1)
1697 let winid = ShowMenu("X", -1)
1698 let winid = ShowMenu("\<Esc>", -1)
1699 let winid = ShowMenu("\<C-C>", -1)
1700
1701 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001702 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001703endfunc
1704
1705func Test_popup_menu_screenshot()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001706 CheckScreendump
Bram Moolenaara730e552019-06-16 19:05:31 +02001707
1708 let lines =<< trim END
1709 call setline(1, range(1, 20))
1710 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001711 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001712 func MenuDone(id, res)
1713 echomsg "selected " .. a:res
1714 endfunc
1715 END
1716 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001717 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001718 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1719
1720 call term_sendkeys(buf, "jj")
1721 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1722
1723 call term_sendkeys(buf, " ")
1724 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1725
1726 " clean up
1727 call StopVimInTerminal(buf)
1728 call delete('XtestPopupMenu')
1729endfunc
1730
Bram Moolenaarf914a332019-07-20 15:09:56 +02001731func Test_popup_menu_narrow()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001732 CheckScreendump
Bram Moolenaarf914a332019-07-20 15:09:56 +02001733
1734 let lines =<< trim END
1735 call setline(1, range(1, 20))
1736 hi PopupSelected ctermbg=green
1737 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1738 func MenuDone(id, res)
1739 echomsg "selected " .. a:res
1740 endfunc
1741 END
1742 call writefile(lines, 'XtestPopupNarrowMenu')
1743 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1744 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1745
1746 " clean up
1747 call term_sendkeys(buf, "x")
1748 call StopVimInTerminal(buf)
1749 call delete('XtestPopupNarrowMenu')
1750endfunc
1751
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001752func Test_popup_title()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001753 CheckScreendump
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001754
1755 " Create a popup without title or border, a line of padding will be added to
1756 " put the title on.
1757 let lines =<< trim END
1758 call setline(1, range(1, 20))
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001759 let winid = popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001760 END
1761 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001762 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001763 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1764
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001765 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxwidth: 20, title: 'a very long title that is not going to fit'})\<CR>")
1766 call term_sendkeys(buf, ":\<CR>")
1767 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_1', {})
1768
1769 call term_sendkeys(buf, ":call popup_setoptions(winid, #{border: []})\<CR>")
1770 call term_sendkeys(buf, ":\<CR>")
1771 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_2', {})
1772
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01001773 call term_sendkeys(buf, ":call popup_clear()\<CR>")
1774 call term_sendkeys(buf, ":call popup_create(['aaa', 'bbb'], #{title: 'Title', minwidth: 12, padding: [2, 2, 2, 2]})\<CR>")
1775 call term_sendkeys(buf, ":\<CR>")
1776 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_3', {})
1777
1778 call term_sendkeys(buf, ":call popup_clear()\<CR>")
1779 call term_sendkeys(buf, ":call popup_create(['aaa', 'bbb'], #{title: 'Title', minwidth: 12, border: [], padding: [2, 2, 2, 2]})\<CR>")
1780 call term_sendkeys(buf, ":\<CR>")
1781 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_4', {})
1782
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001783 " clean up
1784 call StopVimInTerminal(buf)
1785 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001786
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001787 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001788 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001789 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001790 call assert_equal('Another Title', popup_getoptions(winid).title)
1791
1792 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001793endfunc
1794
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001795func Test_popup_close_callback()
1796 func PopupDone(id, result)
1797 let g:result = a:result
1798 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001799 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001800 redraw
1801 call popup_close(winid, 'done')
1802 call assert_equal('done', g:result)
1803endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001804
1805func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001806 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001807 redraw
1808 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001809 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001810 call assert_equal(5, pos.height)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001811 call popup_close(winid)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001812
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001813 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001814 redraw
1815 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001816 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001817 call assert_equal(3, pos.height)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001818 call popup_close(winid)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001819endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001820
1821func Test_popup_never_behind()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001822 CheckScreendump
1823
Bram Moolenaar988c4332019-06-02 14:12:11 +02001824 " +-----------------------------+
1825 " | | |
1826 " | | |
1827 " | | |
1828 " | line1 |
1829 " |------------line2------------|
1830 " | line3 |
1831 " | line4 |
1832 " | |
1833 " | |
1834 " +-----------------------------+
1835 let lines =<< trim END
Bram Moolenaar988c4332019-06-02 14:12:11 +02001836 split
1837 vsplit
1838 let info_window1 = getwininfo()[0]
1839 let line = info_window1['height']
1840 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001841 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001842 \ line : line,
1843 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001844 \ })
1845 END
1846 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001847 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001848 call term_sendkeys(buf, "\<C-W>w")
1849 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1850
1851 " clean up
1852 call StopVimInTerminal(buf)
1853 call delete('XtestPopupBehind')
1854endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001855
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001856func s:VerifyPosition(p, msg, line, col, width, height)
1857 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1858 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1859 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1860 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001861endfunc
1862
1863func Test_popup_position_adjust()
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001864 " Anything placed past the last cell on the right of the screen is moved to
1865 " the left.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001866 "
1867 " When wrapping is disabled, we also shift to the left to display on the
1868 " screen, unless fixed is set.
1869
1870 " Entries for cases which don't vary based on wrapping.
1871 " Format is per tests described below
1872 let both_wrap_tests = [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001873 \ ['a', 5, &columns, 5, &columns, 1, 1],
1874 \ ['b', 5, &columns + 1, 5, &columns, 1, 1],
1875 \ ['c', 5, &columns - 1, 5, &columns - 1, 1, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001876 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001877 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1]]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001878
1879 " these test groups are dicts with:
1880 " - comment: something to identify the group of tests by
1881 " - options: dict of options to merge with the row/col in tests
1882 " - tests: list of cases. Each one is a list with elements:
1883 " - text
1884 " - row
1885 " - col
1886 " - expected row
1887 " - expected col
1888 " - expected width
1889 " - expected height
1890 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001891 \ #{
1892 \ comment: 'left-aligned with wrapping',
1893 \ options: #{
1894 \ wrap: 1,
1895 \ pos: 'botleft',
1896 \ },
1897 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001898 \ ['aa', 5, &columns, 4, &columns, 1, 2],
1899 \ ['bb', 5, &columns + 1, 4, &columns, 1, 2],
1900 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1901 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1902 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1903 \
1904 \ ['aaa', 5, &columns, 3, &columns, 1, 3],
1905 \ ['bbb', 5, &columns + 1, 3, &columns, 1, 3],
1906 \ ['ccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
1907 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1908 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1909 \
1910 \ ['aaaa', 5, &columns, 2, &columns, 1, 4],
1911 \ ['bbbb', 5, &columns + 1, 2, &columns, 1, 4],
1912 \ ['cccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001913 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
1914 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001915 \ ['eeee', 5, &columns - 4, 5, &columns - 4, 4, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001916 \ ],
1917 \ },
1918 \ #{
1919 \ comment: 'left aligned without wrapping',
1920 \ options: #{
1921 \ wrap: 0,
1922 \ pos: 'botleft',
1923 \ },
1924 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001925 \ ['aa', 5, &columns, 5, &columns - 1, 2, 1],
1926 \ ['bb', 5, &columns + 1, 5, &columns - 1, 2, 1],
1927 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1928 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1929 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1930 \
1931 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
1932 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1933 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1934 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1935 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1936 \
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001937 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
1938 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
1939 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
1940 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
1941 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1942 \ ],
1943 \ },
1944 \ #{
1945 \ comment: 'left aligned with fixed position',
1946 \ options: #{
1947 \ wrap: 0,
1948 \ fixed: 1,
1949 \ pos: 'botleft',
1950 \ },
1951 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001952 \ ['aa', 5, &columns, 5, &columns, 1, 1],
1953 \ ['bb', 5, &columns + 1, 5, &columns, 1, 1],
1954 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1955 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1956 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1957 \
1958 \ ['aaa', 5, &columns, 5, &columns, 1, 1],
1959 \ ['bbb', 5, &columns + 1, 5, &columns, 1, 1],
1960 \ ['ccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1961 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1962 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1963 \
1964 \ ['aaaa', 5, &columns, 5, &columns, 1, 1],
1965 \ ['bbbb', 5, &columns + 1, 5, &columns, 1, 1],
1966 \ ['cccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001967 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1968 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1969 \ ],
1970 \ },
1971 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001972
1973 for test_group in tests
1974 for test in test_group.tests
1975 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001976 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001977 \ line: line,
1978 \ col: col,
1979 \ }
1980 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001981
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001982 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001983
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001984 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001985 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
1986 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001987 endfor
1988 endfor
1989
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001990 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001991 %bwipe!
1992endfunc
1993
Bram Moolenaar3397f742019-06-02 18:40:06 +02001994func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001995 " width of screen
1996 let X = join(map(range(&columns), {->'X'}), '')
1997
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001998 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1999 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002000
2001 redraw
2002 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2003 call assert_equal(X, line)
2004
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002005 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002006 redraw
2007
2008 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002009 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
2010 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002011
2012 redraw
2013 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2014 call assert_equal(X, line)
2015
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002016 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002017 redraw
2018
2019 " Extend so > window width
2020 let X .= 'x'
2021
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002022 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
2023 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002024
2025 redraw
2026 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2027 call assert_equal(X[ : -2 ], line)
2028
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002029 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002030 redraw
2031
2032 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002033 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
2034 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002035
2036 redraw
2037 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2038 call assert_equal(X[ : -2 ], line)
2039
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002040 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002041 redraw
2042
2043 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002044 let p = popup_create(X,
2045 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
2046 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002047
2048 redraw
2049 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2050 let e_line = ' ' . X[ 1 : -2 ]
2051 call assert_equal(e_line, line)
2052
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002053 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002054 redraw
2055
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002056 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002057 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02002058endfunc
2059
2060func Test_popup_moved()
2061 new
2062 call test_override('char_avail', 1)
2063 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
2064
2065 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002066 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002067 redraw
2068 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002069 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002070 " trigger the check for last_cursormoved by going into insert mode
2071 call feedkeys("li\<Esc>", 'xt')
2072 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002073 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002074
2075 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002076 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002077 redraw
2078 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002079 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002080 call feedkeys("hi\<Esc>", 'xt')
2081 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002082 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002083
2084 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002085 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002086 redraw
2087 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002088 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002089 call feedkeys("li\<Esc>", 'xt')
2090 call assert_equal(1, popup_getpos(winid).visible)
2091 call feedkeys("ei\<Esc>", 'xt')
2092 call assert_equal(1, popup_getpos(winid).visible)
2093 call feedkeys("eli\<Esc>", 'xt')
2094 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002095 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002096
Bram Moolenaar17627312019-06-02 19:53:44 +02002097 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02002098 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02002099 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002100 redraw
2101 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002102 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002103 call feedkeys("eli\<Esc>", 'xt')
2104 call assert_equal(1, popup_getpos(winid).visible)
2105 call feedkeys("wi\<Esc>", 'xt')
2106 call assert_equal(1, popup_getpos(winid).visible)
2107 call feedkeys("Eli\<Esc>", 'xt')
2108 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002109 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002110
2111 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002112 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002113 redraw
2114 call assert_equal(1, popup_getpos(winid).visible)
2115 call feedkeys("eli\<Esc>", 'xt')
2116 call feedkeys("ei\<Esc>", 'xt')
2117 call assert_equal(1, popup_getpos(winid).visible)
2118 call feedkeys("eli\<Esc>", 'xt')
2119 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002120 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002121
2122 bwipe!
2123 call test_override('ALL', 0)
2124endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002125
2126func Test_notifications()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002127 CheckFeature timers
2128 CheckScreendump
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002129
Bram Moolenaar0fdddee2019-09-01 15:26:23 +02002130 let lines =<< trim END
2131 call setline(1, range(1, 20))
2132 hi Notification ctermbg=lightblue
2133 call popup_notification('first notification', {})
2134 END
2135 call writefile(lines, 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002136 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002137 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
2138
2139 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002140 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
2141 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002142 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
2143
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002144 " clean up
2145 call StopVimInTerminal(buf)
2146 call delete('XtestNotifications')
2147endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002148
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002149func Test_popup_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002150 CheckScreendump
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002151
2152 let lines =<< trim END
2153 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02002154 hi ScrollThumb ctermbg=blue
2155 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002156 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002157 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002158 \ minwidth: 8,
2159 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002160 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002161 func ScrollUp()
2162 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
2163 endfunc
2164 func ScrollDown()
2165 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
2166 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002167 func ClickTop()
2168 call feedkeys("\<F4>\<LeftMouse>", "xt")
2169 endfunc
2170 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002171 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002172 call feedkeys("\<F5>\<LeftMouse>", "xt")
2173 endfunc
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002174 func Popup_filter(winid, key)
2175 if a:key == 'j'
Bram Moolenaar371806e2020-10-22 13:44:54 +02002176 silent! this_throws_an_error_but_is_ignored
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002177 let line = popup_getoptions(a:winid).firstline
2178 let nlines = line('$', a:winid)
2179 let newline = line < nlines ? (line + 1) : nlines
2180 call popup_setoptions(a:winid, #{firstline: newline})
2181 return v:true
2182 elseif a:key == 'x'
2183 call popup_close(a:winid)
2184 return v:true
2185 endif
2186 endfunc
2187
Bram Moolenaar6c542f72020-09-27 21:16:45 +02002188 def CreatePopup(text: list<string>)
Bram Moolenaare0de1712020-12-02 17:36:54 +01002189 popup_create(text, {
Bram Moolenaar6c542f72020-09-27 21:16:45 +02002190 \ minwidth: 30,
2191 \ maxwidth: 30,
2192 \ minheight: 4,
2193 \ maxheight: 4,
2194 \ firstline: 1,
2195 \ lastline: 4,
2196 \ wrap: true,
2197 \ scrollbar: true,
2198 \ mapping: false,
2199 \ filter: Popup_filter,
2200 \ })
2201 enddef
2202
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002203 func PopupScroll()
2204 call popup_clear()
2205 let text =<< trim END
2206 1
2207 2
2208 3
2209 4
2210 long line long line long line long line long line long line
2211 long line long line long line long line long line long line
2212 long line long line long line long line long line long line
2213 END
Bram Moolenaar6c542f72020-09-27 21:16:45 +02002214 call CreatePopup(text)
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002215 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002216 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002217 map <silent> <F4> :call test_setmouse(4, 42)<CR>
2218 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002219 END
2220 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002221 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002222 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
2223
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002224 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002225 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002226 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
2227
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002228 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002229 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002230 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
2231
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002232 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002233 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002234 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
2235
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02002236 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb', firstline: 5})\<CR>")
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002237 " this scrolls two lines (half the window height)
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002238 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
2239 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
2240
2241 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2242 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
2243
2244 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02002245 " wait a bit, otherwise it fails sometimes (double click recognized?)
2246 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002247 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2248 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
2249
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002250 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2251 sleep 100m
2252 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2253 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
2254
2255 call term_sendkeys(buf, ":call ClickBot()\<CR>")
2256 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
2257
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002258 " remove the minwidth and maxheight
2259 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxheight: 0, minwidth: 0})\<CR>")
Bram Moolenaar7e0f4622019-09-17 21:23:39 +02002260 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002261 call VerifyScreenDump(buf, 'Test_popupwin_scroll_10', {})
2262
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002263 " check size with non-wrapping lines
2264 call term_sendkeys(buf, ":call PopupScroll()\<CR>")
2265 call VerifyScreenDump(buf, 'Test_popupwin_scroll_11', {})
2266
2267 " check size with wrapping lines
2268 call term_sendkeys(buf, "j")
2269 call VerifyScreenDump(buf, 'Test_popupwin_scroll_12', {})
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002270
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002271 " clean up
Bram Moolenaar20298ce2020-06-19 21:46:52 +02002272 call term_sendkeys(buf, "x")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002273 call StopVimInTerminal(buf)
2274 call delete('XtestPopupScroll')
2275endfunc
2276
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02002277func Test_popup_too_high_scrollbar()
2278 CheckScreendump
2279
2280 let lines =<< trim END
2281 call setline(1, range(1, 20)->map({i, v -> repeat(v, 10)}))
2282 set scrolloff=0
2283 func ShowPopup()
2284 let winid = popup_atcursor(['one', 'two', 'three', 'four', 'five',
2285 \ 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve'], #{
2286 \ minwidth: 8,
2287 \ border: [],
2288 \ })
2289 endfunc
2290 normal 3G$
2291 call ShowPopup()
2292 END
2293 call writefile(lines, 'XtestPopupToohigh')
2294 let buf = RunVimInTerminal('-S XtestPopupToohigh', #{rows: 10})
2295 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_1', {})
2296
2297 call term_sendkeys(buf, ":call popup_clear()\<CR>")
2298 call term_sendkeys(buf, "8G$")
2299 call term_sendkeys(buf, ":call ShowPopup()\<CR>")
2300 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_2', {})
2301
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02002302 call term_sendkeys(buf, ":call popup_clear()\<CR>")
2303 call term_sendkeys(buf, "gg$")
2304 call term_sendkeys(buf, ":call ShowPopup()\<CR>")
2305 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_3', {})
2306
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02002307 " clean up
2308 call StopVimInTerminal(buf)
2309 call delete('XtestPopupToohigh')
2310endfunc
2311
Bram Moolenaar437a7462019-07-05 20:17:22 +02002312func Test_popup_fitting_scrollbar()
2313 " this was causing a crash, divide by zero
2314 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002315 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002316 \ scrollbar: 1,
2317 \ maxwidth: 10,
2318 \ maxheight: 5,
2319 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02002320 redraw
2321 call popup_clear()
2322endfunc
2323
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002324func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002325 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002326
2327 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002328 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002329 let p = popup_create('test', opts)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002330 eval p->popup_settext('this is a text')
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002331 END
2332
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002333 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002334 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002335 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
2336
2337 " Setting to empty string clears it
2338 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
2339 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
2340
2341 " Setting a list
2342 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2343 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2344
2345 " Shrinking with a list
2346 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
2347 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
2348
2349 " Growing with a list
2350 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2351 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2352
2353 " Empty list clears
2354 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
2355 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
2356
2357 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002358 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002359 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
2360
Bram Moolenaarb0992022020-01-30 14:55:42 +01002361 " range() (doesn't work)
2362 call term_sendkeys(buf, ":call popup_settext(p, range(4, 8))\<CR>")
2363 call VerifyScreenDump(buf, 'Test_popup_settext_07', {})
2364
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002365 " clean up
2366 call StopVimInTerminal(buf)
2367 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002368endfunc
2369
Bram Moolenaar1f42f5a2020-09-03 18:52:24 +02002370func Test_popup_settext_getline()
2371 let id = popup_create('', #{ tabpage: 0 })
2372 call popup_settext(id, ['a','b'])
2373 call assert_equal(2, line('$', id)) " OK :)
2374 call popup_close(id)
2375
2376 let id = popup_create('', #{ tabpage: -1 })
2377 call popup_settext(id, ['a','b'])
2378 call assert_equal(2, line('$', id)) " Fails :(
2379 call popup_close(id)
2380endfunc
2381
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02002382func Test_popup_settext_null()
2383 let id = popup_create('', #{ tabpage: 0 })
2384 call popup_settext(id, test_null_list())
2385 call popup_close(id)
2386
2387 let id = popup_create('', #{ tabpage: 0 })
2388 call popup_settext(id, test_null_string())
2389 call popup_close(id)
2390endfunc
2391
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002392func Test_popup_hidden()
2393 new
2394
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002395 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002396 redraw
2397 call assert_equal(0, popup_getpos(winid).visible)
2398 call popup_close(winid)
2399
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002400 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002401 redraw
2402 call assert_equal(0, popup_getpos(winid).visible)
2403 call popup_close(winid)
2404
2405 func QuitCallback(id, res)
2406 let s:cb_winid = a:id
2407 let s:cb_res = a:res
2408 endfunc
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002409 let winid = 'make a choice'->popup_dialog(#{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002410 \ filter: 'popup_filter_yesno',
2411 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002412 \ })
2413 redraw
2414 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02002415 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
2416 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002417 exe "normal anot used by filter\<Esc>"
2418 call assert_equal('not used by filter', getline(1))
2419
2420 call popup_show(winid)
2421 call feedkeys('y', "xt")
2422 call assert_equal(1, s:cb_res)
2423
2424 bwipe!
2425 delfunc QuitCallback
2426endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02002427
2428" Test options not checked elsewhere
2429func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002430 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002431 let options = popup_getoptions(winid)
2432 call assert_equal(1, options.wrap)
2433 call assert_equal(0, options.drag)
2434 call assert_equal('Beautiful', options.highlight)
2435
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002436 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002437 let options = popup_getoptions(winid)
2438 call assert_equal(0, options.wrap)
2439 call assert_equal(1, options.drag)
2440 call assert_equal('Another', options.highlight)
2441
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002442 call assert_fails('call popup_setoptions(winid, [])', 'E715:')
2443 call assert_fails('call popup_setoptions(winid, test_null_dict())', 'E715:')
2444
Bram Moolenaarae943152019-06-16 22:54:14 +02002445 call popup_close(winid)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002446 call assert_equal(0, popup_setoptions(winid, options.wrap))
Bram Moolenaarae943152019-06-16 22:54:14 +02002447endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002448
2449func Test_popupwin_garbage_collect()
2450 func MyPopupFilter(x, winid, c)
2451 " NOP
2452 endfunc
2453
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002454 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002455 call test_garbagecollect_now()
2456 redraw
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02002457 " Must not crash caused by invalid memory access
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002458 call feedkeys('j', 'xt')
2459 call assert_true(v:true)
2460
2461 call popup_close(winid)
2462 delfunc MyPopupFilter
2463endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002464
Bram Moolenaar581ba392019-09-03 22:08:33 +02002465func Test_popupwin_filter_mode()
2466 func MyPopupFilter(winid, c)
2467 let s:typed = a:c
2468 if a:c == ':' || a:c == "\r" || a:c == 'v'
2469 " can start cmdline mode, get out, and start/stop Visual mode
2470 return 0
2471 endif
2472 return 1
2473 endfunc
2474
2475 " Normal, Visual and Insert mode
2476 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'nvi'})
2477 redraw
2478 call feedkeys('x', 'xt')
2479 call assert_equal('x', s:typed)
2480
2481 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2482 call assert_equal(':', s:typed)
2483 call assert_equal('foo', g:foo)
2484
2485 let @x = 'something'
2486 call feedkeys('v$"xy', 'xt')
2487 call assert_equal('y', s:typed)
2488 call assert_equal('something', @x) " yank command is filtered out
2489 call feedkeys('v', 'xt') " end Visual mode
2490
2491 call popup_close(winid)
2492
2493 " only Normal mode
2494 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'n'})
2495 redraw
2496 call feedkeys('x', 'xt')
2497 call assert_equal('x', s:typed)
2498
2499 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2500 call assert_equal(':', s:typed)
2501 call assert_equal('foo', g:foo)
2502
2503 let @x = 'something'
2504 call feedkeys('v$"xy', 'xt')
2505 call assert_equal('v', s:typed)
2506 call assert_notequal('something', @x)
2507
2508 call popup_close(winid)
2509
2510 " default: all modes
2511 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
2512 redraw
2513 call feedkeys('x', 'xt')
2514 call assert_equal('x', s:typed)
2515
2516 let g:foo = 'bar'
2517 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2518 call assert_equal("\r", s:typed)
2519 call assert_equal('bar', g:foo)
2520
2521 let @x = 'something'
2522 call feedkeys('v$"xy', 'xt')
2523 call assert_equal('y', s:typed)
2524 call assert_equal('something', @x) " yank command is filtered out
2525 call feedkeys('v', 'xt') " end Visual mode
2526
2527 call popup_close(winid)
2528 delfunc MyPopupFilter
2529endfunc
2530
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002531func Test_popupwin_filter_mouse()
2532 func MyPopupFilter(winid, c)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002533 let g:got_mousepos = getmousepos()
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002534 return 0
2535 endfunc
2536
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002537 call setline(1, ['.'->repeat(25)]->repeat(10))
2538 let winid = popup_create(['short', 'long line that will wrap', 'other'], #{
2539 \ line: 2,
2540 \ col: 4,
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002541 \ maxwidth: 12,
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002542 \ padding: [],
2543 \ border: [],
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002544 \ filter: 'MyPopupFilter',
2545 \ })
2546 redraw
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002547 " 123456789012345678901
2548 " 1 .....................
2549 " 2 ...+--------------+..
2550 " 3 ...| |..
2551 " 4 ...| short |..
2552 " 5 ...| long line th |..
2553 " 6 ...| at will wrap |..
2554 " 7 ...| other |..
2555 " 8 ...| |..
2556 " 9 ...+--------------+..
2557 " 10 .....................
2558 let tests = []
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002559
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002560 func AddItemOutsidePopup(tests, row, col)
2561 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2562 \ screenrow: a:row, screencol: a:col,
2563 \ winid: win_getid(), winrow: a:row, wincol: a:col,
2564 \ line: a:row, column: a:col,
2565 \ }})
2566 endfunc
2567 func AddItemInPopupBorder(tests, winid, row, col)
2568 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2569 \ screenrow: a:row, screencol: a:col,
2570 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2571 \ line: 0, column: 0,
2572 \ }})
2573 endfunc
2574 func AddItemInPopupText(tests, winid, row, col, textline, textcol)
2575 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2576 \ screenrow: a:row, screencol: a:col,
2577 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2578 \ line: a:textline, column: a:textcol,
2579 \ }})
2580 endfunc
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002581
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002582 " above and below popup
2583 for c in range(1, 21)
2584 call AddItemOutsidePopup(tests, 1, c)
2585 call AddItemOutsidePopup(tests, 10, c)
2586 endfor
2587 " left and right of popup
2588 for r in range(1, 10)
2589 call AddItemOutsidePopup(tests, r, 3)
2590 call AddItemOutsidePopup(tests, r, 20)
2591 endfor
2592 " top and bottom in popup
2593 for c in range(4, 19)
2594 call AddItemInPopupBorder(tests, winid, 2, c)
2595 call AddItemInPopupBorder(tests, winid, 3, c)
2596 call AddItemInPopupBorder(tests, winid, 8, c)
2597 call AddItemInPopupBorder(tests, winid, 9, c)
2598 endfor
2599 " left and right margin in popup
2600 for r in range(2, 9)
2601 call AddItemInPopupBorder(tests, winid, r, 4)
2602 call AddItemInPopupBorder(tests, winid, r, 5)
2603 call AddItemInPopupBorder(tests, winid, r, 18)
2604 call AddItemInPopupBorder(tests, winid, r, 19)
2605 endfor
2606 " text "short"
2607 call AddItemInPopupText(tests, winid, 4, 6, 1, 1)
2608 call AddItemInPopupText(tests, winid, 4, 10, 1, 5)
2609 call AddItemInPopupText(tests, winid, 4, 11, 1, 6)
2610 call AddItemInPopupText(tests, winid, 4, 17, 1, 6)
2611 " text "long line th"
2612 call AddItemInPopupText(tests, winid, 5, 6, 2, 1)
2613 call AddItemInPopupText(tests, winid, 5, 10, 2, 5)
2614 call AddItemInPopupText(tests, winid, 5, 17, 2, 12)
2615 " text "at will wrap"
2616 call AddItemInPopupText(tests, winid, 6, 6, 2, 13)
2617 call AddItemInPopupText(tests, winid, 6, 10, 2, 17)
2618 call AddItemInPopupText(tests, winid, 6, 17, 2, 24)
2619 " text "other"
2620 call AddItemInPopupText(tests, winid, 7, 6, 3, 1)
2621 call AddItemInPopupText(tests, winid, 7, 10, 3, 5)
2622 call AddItemInPopupText(tests, winid, 7, 11, 3, 6)
2623 call AddItemInPopupText(tests, winid, 7, 17, 3, 6)
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002624
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002625 for item in tests
2626 call test_setmouse(item.clickrow, item.clickcol)
2627 call feedkeys("\<LeftMouse>", 'xt')
2628 call assert_equal(item.result, g:got_mousepos)
2629 endfor
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002630
2631 call popup_close(winid)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002632 enew!
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002633 delfunc MyPopupFilter
2634endfunc
2635
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002636func Test_popupwin_with_buffer()
2637 call writefile(['some text', 'in a buffer'], 'XsomeFile')
2638 let buf = bufadd('XsomeFile')
2639 call assert_equal(0, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002640
2641 setlocal number
2642 call setbufvar(buf, "&wrapmargin", 13)
2643
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002644 let winid = popup_create(buf, {})
2645 call assert_notequal(0, winid)
2646 let pos = popup_getpos(winid)
2647 call assert_equal(2, pos.height)
2648 call assert_equal(1, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002649
2650 " window-local option is set to default, buffer-local is not
2651 call assert_equal(0, getwinvar(winid, '&number'))
2652 call assert_equal(13, getbufvar(buf, '&wrapmargin'))
2653
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002654 call popup_close(winid)
2655 call assert_equal({}, popup_getpos(winid))
2656 call assert_equal(1, bufloaded(buf))
2657 exe 'bwipe! ' .. buf
Bram Moolenaar46451042019-08-24 15:50:46 +02002658 setlocal nonumber
Bram Moolenaar7866b872019-07-01 22:21:01 +02002659
2660 edit test_popupwin.vim
2661 let winid = popup_create(bufnr(''), {})
2662 redraw
2663 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002664 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002665endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02002666
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002667func Test_popupwin_terminal_buffer()
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002668 CheckFeature terminal
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002669 CheckUnix
Bram Moolenaare06a28f2020-05-13 23:24:12 +02002670 " Starting a terminal to run a shell in is considered flaky.
2671 let g:test_is_flaky = 1
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002672
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002673 let origwin = win_getid()
Bram Moolenaar349f6092020-10-06 20:46:49 +02002674
2675 " open help window to test that :help below fails
2676 help
2677
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002678 let termbuf = term_start(&shell, #{hidden: 1})
Bram Moolenaarf5452692020-11-28 21:56:06 +01002679 let winid = popup_create(termbuf, #{minwidth: 40, minheight: 10, border: []})
Bram Moolenaar0353f562020-12-17 22:27:38 +01002680 " Wait for shell to start
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002681 call WaitForAssert({-> assert_equal("run", job_status(term_getjob(termbuf)))})
Bram Moolenaar0353f562020-12-17 22:27:38 +01002682 " Wait for a prompt (see border char first, then space after prompt)
2683 call WaitForAssert({ -> assert_equal(' ', screenstring(screenrow(), screencol() - 1))})
Bram Moolenaarf5452692020-11-28 21:56:06 +01002684
2685 " When typing a character, the cursor is after it.
2686 call feedkeys("x", 'xt')
Bram Moolenaar0353f562020-12-17 22:27:38 +01002687 call term_wait(termbuf)
Bram Moolenaarf5452692020-11-28 21:56:06 +01002688 redraw
2689 call WaitForAssert({ -> assert_equal('x', screenstring(screenrow(), screencol() - 1))})
2690 call feedkeys("\<BS>", 'xt')
2691
Bram Moolenaar631ebc42020-02-03 22:15:26 +01002692 " Check this doesn't crash
2693 call assert_equal(winnr(), winnr('j'))
2694 call assert_equal(winnr(), winnr('k'))
2695 call assert_equal(winnr(), winnr('h'))
2696 call assert_equal(winnr(), winnr('l'))
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002697
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002698 " Cannot quit while job is running
2699 call assert_fails('call feedkeys("\<C-W>:quit\<CR>", "xt")', 'E948:')
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002700
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002701 " Cannot enter Terminal-Normal mode. (TODO: but it works...)
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002702 call feedkeys("xxx\<C-W>N", 'xt')
2703 call assert_fails('call feedkeys("gf", "xt")', 'E863:')
2704 call feedkeys("a\<C-U>", 'xt')
2705
Bram Moolenaar3f65c662020-05-27 23:15:16 +02002706 " Cannot escape from terminal window
2707 call assert_fails('tab drop xxx', 'E863:')
Bram Moolenaar349f6092020-10-06 20:46:49 +02002708 call assert_fails('help', 'E994:')
Bram Moolenaar3f65c662020-05-27 23:15:16 +02002709
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002710 " Cannot open a second one.
2711 let termbuf2 = term_start(&shell, #{hidden: 1})
2712 call assert_fails('call popup_create(termbuf2, #{})', 'E861:')
2713 call term_sendkeys(termbuf2, "exit\<CR>")
2714
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002715 " Exiting shell closes popup window
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002716 call feedkeys("exit\<CR>", 'xt')
2717 " Wait for shell to exit
Bram Moolenaarb2b218d2020-06-22 20:22:19 +02002718 call WaitForAssert({-> assert_equal("dead", job_status(term_getjob(termbuf)))})
Bram Moolenaar373c6512020-06-22 19:24:23 +02002719
Bram Moolenaar349f6092020-10-06 20:46:49 +02002720 helpclose
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002721 call feedkeys(":quit\<CR>", 'xt')
2722 call assert_equal(origwin, win_getid())
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002723endfunc
2724
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002725func Test_popupwin_close_prevwin()
2726 CheckFeature terminal
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002727 call Popupwin_close_prevwin()
2728endfunc
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002729
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002730def Popupwin_close_prevwin()
2731 assert_equal(1, winnr('$'))
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002732 split
2733 wincmd b
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002734 assert_equal(2, winnr())
Bram Moolenaare0de1712020-12-02 17:36:54 +01002735 var buf = term_start(&shell, {hidden: 1})
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002736 popup_create(buf, {})
2737 TermWait(buf, 100)
2738 popup_clear(true)
2739 assert_equal(2, winnr())
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002740
2741 quit
2742 exe 'bwipe! ' .. buf
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002743enddef
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002744
Bram Moolenaar934470e2019-09-01 23:27:05 +02002745func Test_popupwin_with_buffer_and_filter()
2746 new Xwithfilter
2747 call setline(1, range(100))
2748 let bufnr = bufnr()
2749 hide
2750
2751 func BufferFilter(win, key)
2752 if a:key == 'G'
2753 " recursive use of "G" does not cause problems.
2754 call win_execute(a:win, 'normal! G')
2755 return 1
2756 endif
2757 return 0
2758 endfunc
2759
2760 let winid = popup_create(bufnr, #{maxheight: 5, filter: 'BufferFilter'})
2761 call assert_equal(1, popup_getpos(winid).firstline)
2762 redraw
2763 call feedkeys("G", 'xt')
2764 call assert_equal(99, popup_getpos(winid).firstline)
2765
2766 call popup_close(winid)
2767 exe 'bwipe! ' .. bufnr
2768endfunc
2769
Bram Moolenaare296e312019-07-03 23:20:18 +02002770func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002771 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002772 \ maxwidth: 40,
2773 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02002774 \ })
2775 for top in range(1, 20)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002776 eval winid->popup_setoptions(#{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02002777 redraw
2778 call assert_equal(19, popup_getpos(winid).width)
2779 endfor
2780 call popup_clear()
2781endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02002782
2783func Test_popupwin_buf_close()
2784 let buf = bufadd('Xtestbuf')
2785 call bufload(buf)
2786 call setbufline(buf, 1, ['just', 'some', 'lines'])
2787 let winid = popup_create(buf, {})
2788 redraw
2789 call assert_equal(3, popup_getpos(winid).height)
2790 let bufinfo = getbufinfo(buf)[0]
2791 call assert_equal(1, bufinfo.changed)
2792 call assert_equal(0, bufinfo.hidden)
2793 call assert_equal(0, bufinfo.listed)
2794 call assert_equal(1, bufinfo.loaded)
2795 call assert_equal([], bufinfo.windows)
2796 call assert_equal([winid], bufinfo.popups)
2797
2798 call popup_close(winid)
2799 call assert_equal({}, popup_getpos(winid))
2800 let bufinfo = getbufinfo(buf)[0]
2801 call assert_equal(1, bufinfo.changed)
2802 call assert_equal(1, bufinfo.hidden)
2803 call assert_equal(0, bufinfo.listed)
2804 call assert_equal(1, bufinfo.loaded)
2805 call assert_equal([], bufinfo.windows)
2806 call assert_equal([], bufinfo.popups)
2807 exe 'bwipe! ' .. buf
2808endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02002809
2810func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002811 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02002812
2813 let lines =<< trim END
2814 call setline(1, range(1, 10))
2815 hi ScrollThumb ctermbg=blue
2816 hi ScrollBar ctermbg=red
2817 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002818 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002819 \ maxwidth: 10,
2820 \ maxheight: 3,
2821 \ pos : 'topleft',
2822 \ col : a:col,
2823 \ line : a:line,
2824 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02002825 \ })
2826 endfunc
2827 call PopupMenu(['x'], 1, 1)
2828 call PopupMenu(['123456789|'], 1, 16)
2829 call PopupMenu(['123456789|' .. ' '], 7, 1)
2830 call PopupMenu([repeat('123456789|', 100)], 7, 16)
2831 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
2832 END
2833 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002834 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02002835 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
2836
2837 " close the menu popupwin.
2838 call term_sendkeys(buf, " ")
2839 call term_sendkeys(buf, " ")
2840 call term_sendkeys(buf, " ")
2841 call term_sendkeys(buf, " ")
2842 call term_sendkeys(buf, " ")
2843
2844 " clean up
2845 call StopVimInTerminal(buf)
2846 call delete('XtestPopupMenuMaxWidth')
2847endfunc
2848
Bram Moolenaara901a372019-07-13 16:38:50 +02002849func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002850 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02002851
2852 let lines =<< trim END
2853 call setline(1, range(1, 20))
2854 hi ScrollThumb ctermbg=blue
2855 hi ScrollBar ctermbg=red
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002856 eval ['one', 'two', 'three', 'four', 'five',
2857 \ 'six', 'seven', 'eight', 'nine']
2858 \ ->popup_menu(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002859 \ minwidth: 8,
2860 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02002861 \ })
2862 END
2863 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002864 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02002865
2866 call term_sendkeys(buf, "j")
2867 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
2868
2869 call term_sendkeys(buf, "jjj")
2870 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
2871
2872 " if the cursor is the bottom line, it stays at the bottom line.
2873 call term_sendkeys(buf, repeat("j", 20))
2874 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
2875
2876 call term_sendkeys(buf, "kk")
2877 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
2878
2879 call term_sendkeys(buf, "k")
2880 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
2881
2882 " if the cursor is in the top line, it stays in the top line.
2883 call term_sendkeys(buf, repeat("k", 20))
2884 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
2885
2886 " close the menu popupwin.
2887 call term_sendkeys(buf, " ")
2888
2889 " clean up
2890 call StopVimInTerminal(buf)
2891 call delete('XtestPopupMenuScroll')
2892endfunc
2893
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002894func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002895 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002896
2897 let lines =<< trim END
2898 function! MyFilter(winid, key) abort
2899 if a:key == "0"
2900 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
2901 return 1
2902 endif
2903 if a:key == "G"
2904 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
2905 return 1
2906 endif
2907 if a:key == "j"
2908 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
2909 return 1
2910 endif
2911 if a:key == "k"
2912 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
2913 return 1
2914 endif
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002915 if a:key == ':'
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002916 call popup_close(a:winid)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002917 return 0
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002918 endif
2919 return 0
2920 endfunction
2921 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
2922 \ maxheight : 3,
2923 \ filter : 'MyFilter'
2924 \ })
2925 END
2926 call writefile(lines, 'XtestPopupMenuFilter')
2927 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
2928
2929 call term_sendkeys(buf, "j")
2930 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
2931
2932 call term_sendkeys(buf, "k")
2933 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
2934
2935 call term_sendkeys(buf, "G")
2936 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
2937
2938 call term_sendkeys(buf, "0")
2939 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
2940
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002941 " check that when the popup is closed in the filter the screen is redrawn
2942 call term_sendkeys(buf, ":")
2943 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_5', {})
2944 call term_sendkeys(buf, "\<CR>")
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002945
2946 " clean up
2947 call StopVimInTerminal(buf)
2948 call delete('XtestPopupMenuFilter')
2949endfunc
2950
2951func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002952 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002953
2954 let winid = popup_create('some text', {})
2955 call assert_equal(0, popup_getoptions(winid).cursorline)
2956 call popup_close(winid)
2957
2958 let winid = popup_create('some text', #{ cursorline: 1, })
2959 call assert_equal(1, popup_getoptions(winid).cursorline)
2960 call popup_close(winid)
2961
2962 let winid = popup_create('some text', #{ cursorline: 0, })
2963 call assert_equal(0, popup_getoptions(winid).cursorline)
2964 call popup_close(winid)
2965
2966 let winid = popup_menu('some text', {})
2967 call assert_equal(1, popup_getoptions(winid).cursorline)
2968 call popup_close(winid)
2969
2970 let winid = popup_menu('some text', #{ cursorline: 1, })
2971 call assert_equal(1, popup_getoptions(winid).cursorline)
2972 call popup_close(winid)
2973
2974 let winid = popup_menu('some text', #{ cursorline: 0, })
2975 call assert_equal(0, popup_getoptions(winid).cursorline)
2976 call popup_close(winid)
2977
2978 " ---------
2979 " Pattern 1
2980 " ---------
2981 let lines =<< trim END
2982 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
2983 END
2984 call writefile(lines, 'XtestPopupCursorLine')
2985 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2986 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
2987 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2988 call StopVimInTerminal(buf)
2989
2990 " ---------
2991 " Pattern 2
2992 " ---------
2993 let lines =<< trim END
2994 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
2995 END
2996 call writefile(lines, 'XtestPopupCursorLine')
2997 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2998 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
2999 call term_sendkeys(buf, ":call popup_clear()\<cr>")
3000 call StopVimInTerminal(buf)
3001
3002 " ---------
3003 " Pattern 3
3004 " ---------
3005 let lines =<< trim END
3006 function! MyFilter(winid, key) abort
3007 if a:key == "j"
3008 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
3009 return 1
3010 endif
3011 if a:key == 'x'
3012 call popup_close(a:winid)
3013 return 1
3014 endif
3015 return 0
3016 endfunction
3017 call popup_menu(['111', '222', '333'], #{
3018 \ cursorline : 0,
3019 \ maxheight : 2,
3020 \ filter : 'MyFilter',
3021 \ })
3022 END
3023 call writefile(lines, 'XtestPopupCursorLine')
3024 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3025 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
3026 call term_sendkeys(buf, "j")
3027 call term_sendkeys(buf, "j")
3028 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
3029 call term_sendkeys(buf, "x")
3030 call StopVimInTerminal(buf)
3031
3032 " ---------
3033 " Pattern 4
3034 " ---------
3035 let lines =<< trim END
3036 function! MyFilter(winid, key) abort
3037 if a:key == "j"
3038 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
3039 return 1
3040 endif
3041 if a:key == 'x'
3042 call popup_close(a:winid)
3043 return 1
3044 endif
3045 return 0
3046 endfunction
3047 call popup_menu(['111', '222', '333'], #{
3048 \ cursorline : 1,
3049 \ maxheight : 2,
3050 \ filter : 'MyFilter',
3051 \ })
3052 END
3053 call writefile(lines, 'XtestPopupCursorLine')
3054 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3055 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
3056 call term_sendkeys(buf, "j")
3057 call term_sendkeys(buf, "j")
3058 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
3059 call term_sendkeys(buf, "x")
3060 call StopVimInTerminal(buf)
3061
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003062 " ---------
3063 " Cursor in second line when creating the popup
3064 " ---------
3065 let lines =<< trim END
3066 let winid = popup_create(['111', '222', '333'], #{
3067 \ cursorline : 1,
3068 \ })
3069 call win_execute(winid, "2")
3070 END
3071 call writefile(lines, 'XtestPopupCursorLine')
3072 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3073 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_7', {})
3074 call StopVimInTerminal(buf)
3075
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003076 call delete('XtestPopupCursorLine')
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01003077
3078 " ---------
3079 " Use current buffer for popupmenu
3080 " ---------
3081 let lines =<< trim END
3082 call setline(1, ['one', 'two', 'three'])
3083 let winid = popup_create(bufnr('%'), #{
3084 \ cursorline : 1,
3085 \ })
3086 call win_execute(winid, "2")
3087 END
3088 call writefile(lines, 'XtestPopupCursorLine')
3089 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3090 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_8', {})
3091 call StopVimInTerminal(buf)
3092
3093 call delete('XtestPopupCursorLine')
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003094endfunc
3095
Bram Moolenaarf914a332019-07-20 15:09:56 +02003096func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003097 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003098 CheckFeature quickfix
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003099
Bram Moolenaarf914a332019-07-20 15:09:56 +02003100 call writefile([
3101 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
3102 \ "another\tXtagfile\t/^this is another",
3103 \ "theword\tXtagfile\t/^theword"],
3104 \ 'Xtags')
3105 call writefile(range(1,20)
3106 \ + ['theword is here']
3107 \ + range(22, 27)
3108 \ + ['this is another place']
3109 \ + range(29, 40),
3110 \ "Xtagfile")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003111 call writefile(range(1,10)
3112 \ + ['searched word is here']
3113 \ + range(12, 20),
3114 \ "Xheader.h")
Bram Moolenaarf914a332019-07-20 15:09:56 +02003115 let lines =<< trim END
3116 set tags=Xtags
3117 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003118 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003119 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003120 \ 'three',
3121 \ 'four',
3122 \ 'five',
3123 \ 'six',
3124 \ 'seven',
3125 \ 'find theword somewhere',
3126 \ 'nine',
3127 \ 'this is another word',
3128 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02003129 set previewpopup=height:4,width:40
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003130 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02003131 END
3132 call writefile(lines, 'XtestPreviewPopup')
3133 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
3134
3135 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
3136 call term_sendkeys(buf, ":\<CR>")
3137 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
3138
3139 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
3140 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
3141
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003142 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003143 call term_sendkeys(buf, ":\<CR>")
3144 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
3145
3146 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
3147 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
3148
Bram Moolenaar799439a2020-02-11 21:44:17 +01003149 call term_sendkeys(buf, ":silent cd ..\<CR>:\<CR>")
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003150 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar799439a2020-02-11 21:44:17 +01003151 call term_sendkeys(buf, ":silent cd testdir\<CR>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003152
3153 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02003154 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003155 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
3156
3157 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
3158 call term_sendkeys(buf, ":\<CR>")
3159 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
3160
3161 call term_sendkeys(buf, ":pclose\<CR>")
3162 call term_sendkeys(buf, ":psearch searched\<CR>")
3163 call term_sendkeys(buf, ":\<CR>")
3164 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003165
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01003166 call term_sendkeys(buf, "\<C-W>p")
3167 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_9', {})
3168
3169 call term_sendkeys(buf, ":call win_execute(popup_findpreview(), 'call popup_clear()')\<CR>")
3170 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_10', {})
3171
Bram Moolenaarf914a332019-07-20 15:09:56 +02003172 call StopVimInTerminal(buf)
3173 call delete('Xtags')
3174 call delete('Xtagfile')
3175 call delete('XtestPreviewPopup')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003176 call delete('Xheader.h')
Bram Moolenaarf914a332019-07-20 15:09:56 +02003177endfunc
3178
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003179func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003180 let lines =<< trim END
3181 set completeopt+=preview,popup
3182 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02003183 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003184
3185 func CompleteFuncDict(findstart, base)
3186 if a:findstart
3187 if col('.') > 10
3188 return col('.') - 10
3189 endif
3190 return 0
3191 endif
3192
3193 return {
3194 \ 'words': [
3195 \ {
3196 \ 'word': 'aword',
3197 \ 'abbr': 'wrd',
3198 \ 'menu': 'extra text',
3199 \ 'info': 'words are cool',
3200 \ 'kind': 'W',
3201 \ 'user_data': 'test'
3202 \ },
3203 \ {
3204 \ 'word': 'anotherword',
3205 \ 'abbr': 'anotwrd',
3206 \ 'menu': 'extra text',
3207 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
3208 \ 'kind': 'W',
3209 \ 'user_data': 'notest'
3210 \ },
3211 \ {
3212 \ 'word': 'noinfo',
3213 \ 'abbr': 'noawrd',
3214 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02003215 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003216 \ 'kind': 'W',
3217 \ 'user_data': 'notest'
3218 \ },
3219 \ {
3220 \ 'word': 'thatword',
3221 \ 'abbr': 'thatwrd',
3222 \ 'menu': 'extra text',
3223 \ 'info': 'that word is cool',
3224 \ 'kind': 'W',
3225 \ 'user_data': 'notest'
3226 \ },
3227 \ ]
3228 \ }
3229 endfunc
3230 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003231 func ChangeColor()
3232 let id = popup_findinfo()
Bram Moolenaard356fc62020-12-09 18:13:44 +01003233 if buflisted(winbufnr(id))
3234 call setline(1, 'buffer is listed')
3235 endif
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003236 eval id->popup_setoptions(#{highlight: 'InfoPopup'})
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003237 endfunc
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003238
3239 func InfoHidden()
3240 set completepopup=height:4,border:off,align:menu
3241 set completeopt-=popup completeopt+=popuphidden
3242 au CompleteChanged * call HandleChange()
3243 endfunc
3244
3245 let s:counter = 0
3246 func HandleChange()
3247 let s:counter += 1
3248 let selected = complete_info(['selected']).selected
3249 if selected <= 0
3250 " First time: do nothing, info remains hidden
3251 return
3252 endif
3253 if selected == 1
3254 " Second time: show info right away
3255 let id = popup_findinfo()
3256 if id
3257 call popup_settext(id, 'immediate info ' .. s:counter)
3258 call popup_show(id)
3259 endif
3260 else
3261 " Third time: show info after a short delay
3262 call timer_start(100, 'ShowInfo')
3263 endif
3264 endfunc
3265
3266 func ShowInfo(...)
3267 let id = popup_findinfo()
3268 if id
3269 call popup_settext(id, 'async info ' .. s:counter)
3270 call popup_show(id)
3271 endif
3272 endfunc
Bram Moolenaar2dfae042020-11-15 14:09:37 +01003273
3274 " Check that no autocommands are triggered for the info popup
3275 au WinEnter * if win_gettype() == 'popup' | call setline(2, 'WinEnter') | endif
3276 au WinLeave * if win_gettype() == 'popup' | call setline(2, 'WinLeave') | endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003277 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003278 return lines
3279endfunc
3280
3281func Test_popupmenu_info_border()
3282 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003283 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003284
3285 let lines = Get_popupmenu_lines()
3286 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003287 call writefile(lines, 'XtestInfoPopup')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003288
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003289 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003290 call TermWait(buf, 25)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003291
3292 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3293 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
3294
3295 call term_sendkeys(buf, "\<C-N>")
3296 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
3297
3298 call term_sendkeys(buf, "\<C-N>")
3299 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
3300
3301 call term_sendkeys(buf, "\<C-N>\<C-N>")
3302 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
3303
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02003304 " info on the left with scrollbar
3305 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
3306 call term_sendkeys(buf, "\<C-N>\<C-N>")
3307 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
3308
Bram Moolenaar202c3f72019-11-21 12:12:35 +01003309 " Test that the popupmenu's scrollbar and infopopup do not overlap
3310 call term_sendkeys(buf, "\<Esc>")
3311 call term_sendkeys(buf, ":set pumheight=3\<CR>")
3312 call term_sendkeys(buf, "cc\<C-X>\<C-U>")
3313 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_6', {})
3314
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003315 " Hide the info popup, cycle through buffers, make sure it didn't get
Bram Moolenaarca7c0782020-01-14 20:42:48 +01003316 " deleted.
3317 call term_sendkeys(buf, "\<Esc>")
3318 call term_sendkeys(buf, ":set hidden\<CR>")
3319 call term_sendkeys(buf, ":bn\<CR>")
3320 call term_sendkeys(buf, ":bn\<CR>")
3321 call term_sendkeys(buf, "otest text test text\<C-X>\<C-U>")
3322 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_7', {})
3323
Bram Moolenaar447bfba2020-07-18 16:07:16 +02003324 " Test that when the option is changed the popup changes.
3325 call term_sendkeys(buf, "\<Esc>")
3326 call term_sendkeys(buf, ":set completepopup=border:off\<CR>")
3327 call term_sendkeys(buf, "a\<C-X>\<C-U>")
3328 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_8', {})
3329
Bram Moolenaar6d585f42020-07-26 22:20:54 +02003330 call term_sendkeys(buf, " \<Esc>")
3331 call term_sendkeys(buf, ":set completepopup+=width:10\<CR>")
3332 call term_sendkeys(buf, "a\<C-X>\<C-U>")
3333 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_9', {})
3334
Bram Moolenaarca7c0782020-01-14 20:42:48 +01003335 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003336 call StopVimInTerminal(buf)
3337 call delete('XtestInfoPopup')
3338endfunc
3339
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003340func Test_popupmenu_info_noborder()
3341 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003342 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003343
3344 let lines = Get_popupmenu_lines()
3345 call add(lines, 'set completepopup=height:4,border:off')
3346 call writefile(lines, 'XtestInfoPopupNb')
3347
3348 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003349 call TermWait(buf, 25)
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003350
3351 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3352 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
3353
3354 call StopVimInTerminal(buf)
3355 call delete('XtestInfoPopupNb')
3356endfunc
3357
Bram Moolenaar258cef52019-08-21 17:29:29 +02003358func Test_popupmenu_info_align_menu()
3359 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003360 CheckFeature quickfix
Bram Moolenaar258cef52019-08-21 17:29:29 +02003361
3362 let lines = Get_popupmenu_lines()
3363 call add(lines, 'set completepopup=height:4,border:off,align:menu')
3364 call writefile(lines, 'XtestInfoPopupNb')
3365
3366 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003367 call TermWait(buf, 25)
Bram Moolenaar258cef52019-08-21 17:29:29 +02003368
3369 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3370 call term_sendkeys(buf, "\<C-N>")
3371 call term_sendkeys(buf, "\<C-N>")
3372 call term_sendkeys(buf, "\<C-N>")
3373 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
3374
3375 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
3376 call term_sendkeys(buf, "\<C-N>")
3377 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
3378
3379 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003380 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02003381 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
3382 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
3383 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
3384
3385 call StopVimInTerminal(buf)
3386 call delete('XtestInfoPopupNb')
3387endfunc
3388
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003389func Test_popupmenu_info_hidden()
3390 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003391 CheckFeature quickfix
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003392
3393 let lines = Get_popupmenu_lines()
3394 call add(lines, 'call InfoHidden()')
3395 call writefile(lines, 'XtestInfoPopupHidden')
3396
3397 let buf = RunVimInTerminal('-S XtestInfoPopupHidden', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003398 call TermWait(buf, 25)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003399
3400 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3401 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_1', {})
3402
3403 call term_sendkeys(buf, "\<C-N>")
3404 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_2', {})
3405
3406 call term_sendkeys(buf, "\<C-N>")
3407 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_3', {})
3408
3409 call term_sendkeys(buf, "\<Esc>")
3410 call StopVimInTerminal(buf)
3411 call delete('XtestInfoPopupHidden')
3412endfunc
3413
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003414func Test_popupmenu_info_too_wide()
3415 CheckScreendump
3416 CheckFeature quickfix
3417
3418 let lines =<< trim END
3419 call setline(1, range(10))
3420
3421 set completeopt+=preview,popup
3422 set completepopup=align:menu
3423 set omnifunc=OmniFunc
3424 hi InfoPopup ctermbg=lightgrey
3425
3426 func OmniFunc(findstart, base)
3427 if a:findstart
3428 return 0
3429 endif
3430
3431 let menuText = 'some long text to make sure the menu takes up all of the width of the window'
3432 return #{
3433 \ words: [
3434 \ #{
3435 \ word: 'scrap',
3436 \ menu: menuText,
3437 \ info: "other words are\ncooler than this and some more text\nto make wrap",
3438 \ },
3439 \ #{
3440 \ word: 'scappier',
3441 \ menu: menuText,
3442 \ info: 'words are cool',
3443 \ },
3444 \ #{
3445 \ word: 'scrappier2',
3446 \ menu: menuText,
3447 \ info: 'words are cool',
3448 \ },
3449 \ ]
3450 \ }
3451 endfunc
3452 END
3453
3454 call writefile(lines, 'XtestInfoPopupWide')
3455 let buf = RunVimInTerminal('-S XtestInfoPopupWide', #{rows: 8})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003456 call TermWait(buf, 25)
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003457
3458 call term_sendkeys(buf, "Ascr\<C-X>\<C-O>")
3459 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_wide_1', {})
3460
3461 call term_sendkeys(buf, "\<Esc>")
3462 call StopVimInTerminal(buf)
3463 call delete('XtestInfoPopupWide')
3464endfunc
3465
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003466func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02003467 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003468 call popup_clear()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003469 let winid = 'nothing wrong'->popup_notification({})
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003470 call assert_equal(bufnr, winbufnr(winid))
3471 call popup_clear()
3472endfunc
3473
Bram Moolenaar1824f452019-10-02 23:06:46 +02003474func Test_popupwin_getoptions_tablocal()
3475 topleft split
3476 let win1 = popup_create('nothing', #{maxheight: 8})
3477 let win2 = popup_create('something', #{maxheight: 10})
3478 let win3 = popup_create('something', #{maxheight: 15})
3479 call assert_equal(8, popup_getoptions(win1).maxheight)
3480 call assert_equal(10, popup_getoptions(win2).maxheight)
3481 call assert_equal(15, popup_getoptions(win3).maxheight)
3482 call popup_clear()
3483 quit
3484endfunc
3485
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02003486func Test_popupwin_cancel()
3487 let win1 = popup_create('one', #{line: 5, filter: {... -> 0}})
3488 let win2 = popup_create('two', #{line: 10, filter: {... -> 0}})
3489 let win3 = popup_create('three', #{line: 15, filter: {... -> 0}})
3490 call assert_equal(5, popup_getpos(win1).line)
3491 call assert_equal(10, popup_getpos(win2).line)
3492 call assert_equal(15, popup_getpos(win3).line)
3493 " TODO: this also works without patch 8.1.2110
3494 call feedkeys("\<C-C>", 'xt')
3495 call assert_equal(5, popup_getpos(win1).line)
3496 call assert_equal(10, popup_getpos(win2).line)
3497 call assert_equal({}, popup_getpos(win3))
3498 call feedkeys("\<C-C>", 'xt')
3499 call assert_equal(5, popup_getpos(win1).line)
3500 call assert_equal({}, popup_getpos(win2))
3501 call assert_equal({}, popup_getpos(win3))
3502 call feedkeys("\<C-C>", 'xt')
3503 call assert_equal({}, popup_getpos(win1))
3504 call assert_equal({}, popup_getpos(win2))
3505 call assert_equal({}, popup_getpos(win3))
3506endfunc
3507
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003508func Test_popupwin_filter_redraw()
3509 " Create two popups with a filter that closes the popup when typing "0".
3510 " Both popups should close, even though the redraw also calls
3511 " popup_reset_handled()
3512
3513 func CloseFilter(winid, key)
3514 if a:key == '0'
3515 call popup_close(a:winid)
3516 redraw
3517 endif
3518 return 0 " pass the key
3519 endfunc
3520
3521 let id1 = popup_create('first one', #{
3522 \ line: 1,
3523 \ col: 1,
3524 \ filter: 'CloseFilter',
3525 \ })
3526 let id2 = popup_create('second one', #{
3527 \ line: 9,
3528 \ col: 1,
3529 \ filter: 'CloseFilter',
3530 \ })
3531 call assert_equal(1, popup_getpos(id1).line)
3532 call assert_equal(9, popup_getpos(id2).line)
3533
3534 call feedkeys('0', 'xt')
3535 call assert_equal({}, popup_getpos(id1))
3536 call assert_equal({}, popup_getpos(id2))
3537
3538 call popup_clear()
3539 delfunc CloseFilter
3540endfunc
3541
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003542func Test_popupwin_double_width()
3543 CheckScreendump
3544
3545 let lines =<< trim END
3546 call setline(1, 'x你好世界你好世你好世界你好')
3547 call setline(2, '你好世界你好世你好世界你好')
3548 call setline(3, 'x你好世界你好世你好世界你好')
3549 call popup_create('你好,世界 - 你好,世界xxxxx', #{line: 1, col: 3, maxwidth: 14})
3550 END
3551 call writefile(lines, 'XtestPopupWide')
3552
3553 let buf = RunVimInTerminal('-S XtestPopupWide', #{rows: 10})
3554 call VerifyScreenDump(buf, 'Test_popupwin_doublewidth_1', {})
3555
3556 call StopVimInTerminal(buf)
3557 call delete('XtestPopupWide')
3558endfunc
3559
3560func Test_popupwin_sign()
3561 CheckScreendump
3562
3563 let lines =<< trim END
3564 call setline(1, range(10))
3565 call sign_define('Current', {
3566 \ 'text': '>>',
3567 \ 'texthl': 'WarningMsg',
3568 \ 'linehl': 'Error',
3569 \ })
3570 call sign_define('Other', {
3571 \ 'text': '#!',
3572 \ 'texthl': 'Error',
3573 \ 'linehl': 'Search',
3574 \ })
3575 let winid = popup_create(['hello', 'bright', 'world'], {
3576 \ 'minwidth': 20,
3577 \ })
3578 call setwinvar(winid, "&signcolumn", "yes")
3579 let winbufnr = winbufnr(winid)
3580
3581 " add sign to current buffer, shows
3582 call sign_place(1, 'Selected', 'Current', bufnr('%'), {'lnum': 1})
3583 " add sign to current buffer, does not show
3584 call sign_place(2, 'PopUpSelected', 'Other', bufnr('%'), {'lnum': 2})
3585
3586 " add sign to popup buffer, shows
3587 call sign_place(3, 'PopUpSelected', 'Other', winbufnr, {'lnum': 1})
3588 " add sign to popup buffer, does not show
3589 call sign_place(4, 'Selected', 'Current', winbufnr, {'lnum': 2})
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02003590
3591 func SetOptions()
3592 call setwinvar(g:winid, '&number', 1)
3593 call setwinvar(g:winid, '&foldcolumn', 2)
3594 call popup_settext(g:winid, 'a longer line to check the width')
3595 endfunc
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003596 END
3597 call writefile(lines, 'XtestPopupSign')
3598
3599 let buf = RunVimInTerminal('-S XtestPopupSign', #{rows: 10})
3600 call VerifyScreenDump(buf, 'Test_popupwin_sign_1', {})
3601
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02003602 " set more options to check the width is adjusted
3603 call term_sendkeys(buf, ":call SetOptions()\<CR>")
3604 call VerifyScreenDump(buf, 'Test_popupwin_sign_2', {})
3605
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003606 call StopVimInTerminal(buf)
3607 call delete('XtestPopupSign')
3608endfunc
3609
Bram Moolenaar99ebf222019-12-10 23:44:48 +01003610func Test_popupwin_bufnr()
3611 let popwin = popup_create(['blah'], #{})
3612 let popbuf = winbufnr(popwin)
3613 split asdfasdf
3614 let newbuf = bufnr()
3615 call assert_true(newbuf > popbuf, 'New buffer number is higher')
3616 call assert_equal(newbuf, bufnr('$'))
3617 call popup_clear()
3618 let popwin = popup_create(['blah'], #{})
3619 " reuses previous buffer number
3620 call assert_equal(popbuf, winbufnr(popwin))
3621 call assert_equal(newbuf, bufnr('$'))
3622
3623 call popup_clear()
3624 bwipe!
3625endfunc
3626
Bram Moolenaarec084d32020-02-28 22:44:47 +01003627func Test_popupwin_filter_input_multibyte()
3628 func MyPopupFilter(winid, c)
3629 let g:bytes = range(a:c->strlen())->map({i -> char2nr(a:c[i])})
3630 return 0
3631 endfunc
3632 let winid = popup_create('', #{mapping: 0, filter: 'MyPopupFilter'})
3633
3634 " UTF-8: E3 80 80, including K_SPECIAL(0x80)
3635 call feedkeys("\u3000", 'xt')
3636 call assert_equal([0xe3, 0x80, 0x80], g:bytes)
3637
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01003638 " UTF-8: E3 80 9B, including CSI(0x9B)
3639 call feedkeys("\u301b", 'xt')
3640 call assert_equal([0xe3, 0x80, 0x9b], g:bytes)
Bram Moolenaarec084d32020-02-28 22:44:47 +01003641
Bram Moolenaar20298ce2020-06-19 21:46:52 +02003642 if has('unix')
3643 " with modifyOtherKeys <M-S-a> does not include a modifier sequence
Bram Moolenaarb326edf2020-06-20 15:03:38 +02003644 if has('gui_running')
3645 call feedkeys("\x9b\xfc\x08A", 'Lx!')
3646 else
3647 call feedkeys("\<Esc>[27;4;65~", 'Lx!')
3648 endif
Bram Moolenaar20298ce2020-06-19 21:46:52 +02003649 call assert_equal([0xc3, 0x81], g:bytes)
3650 endif
3651
Bram Moolenaarec084d32020-02-28 22:44:47 +01003652 call popup_clear()
3653 delfunc MyPopupFilter
3654 unlet g:bytes
3655endfunc
3656
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003657func Test_popupwin_filter_close_ctrl_c()
3658 CheckScreendump
3659
3660 let lines =<< trim END
3661 vsplit
3662 set laststatus=2
3663 set statusline=%!Statusline()
3664
3665 function Statusline() abort
3666 return '%<%f %h%m%r%=%-14.(%l,%c%V%) %P'
3667 endfunction
3668
3669 call popup_create('test test test test...', {'filter': {-> 0}})
3670 END
3671 call writefile(lines, 'XtestPopupCtrlC')
3672
3673 let buf = RunVimInTerminal('-S XtestPopupCtrlC', #{rows: 10})
3674
3675 call term_sendkeys(buf, "\<C-C>")
3676 call VerifyScreenDump(buf, 'Test_popupwin_ctrl_c', {})
3677
3678 call StopVimInTerminal(buf)
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003679 call delete('XtestPopupCtrlC')
3680endfunc
3681
3682func Test_popupwin_filter_close_wrong_name()
3683 CheckScreendump
3684
3685 let lines =<< trim END
3686 call popup_create('one two three...', {'filter': 'NoSuchFunc'})
3687 END
3688 call writefile(lines, 'XtestPopupWrongName')
3689
3690 let buf = RunVimInTerminal('-S XtestPopupWrongName', #{rows: 10})
3691
3692 call term_sendkeys(buf, "j")
3693 call VerifyScreenDump(buf, 'Test_popupwin_wrong_name', {})
3694
3695 call StopVimInTerminal(buf)
3696 call delete('XtestPopupWrongName')
3697endfunc
3698
3699func Test_popupwin_filter_close_three_errors()
3700 CheckScreendump
3701
3702 let lines =<< trim END
3703 set cmdheight=2
3704 call popup_create('one two three...', {'filter': 'filter'})
3705 END
3706 call writefile(lines, 'XtestPopupThreeErrors')
3707
3708 let buf = RunVimInTerminal('-S XtestPopupThreeErrors', #{rows: 10})
3709
3710 call term_sendkeys(buf, "jj")
3711 call VerifyScreenDump(buf, 'Test_popupwin_three_errors_1', {})
3712 call term_sendkeys(buf, "j")
3713 call VerifyScreenDump(buf, 'Test_popupwin_three_errors_2', {})
3714
3715 call StopVimInTerminal(buf)
3716 call delete('XtestPopupThreeErrors')
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003717endfunc
3718
Bram Moolenaar927495b2020-11-06 17:58:35 +01003719func Test_popupwin_latin1_encoding()
3720 CheckScreendump
3721 CheckUnix
3722
3723 " When 'encoding' is a single-byte encoding a terminal window will mess up
3724 " the display. Check that showing a popup on top of that doesn't crash.
3725 let lines =<< trim END
3726 set encoding=latin1
3727 terminal cat Xmultibyte
3728 call popup_create(['one', 'two', 'three', 'four'], #{line: 1, col: 10})
Bram Moolenaar4c5bdb92020-12-17 17:45:59 +01003729 redraw
Bram Moolenaarb125b532020-12-17 21:56:09 +01003730 " wait for "cat" to finish
3731 while execute('ls!') !~ 'finished'
3732 sleep 10m
3733 endwhile
Bram Moolenaar4c5bdb92020-12-17 17:45:59 +01003734 echo "Done"
Bram Moolenaar927495b2020-11-06 17:58:35 +01003735 END
3736 call writefile(lines, 'XtestPopupLatin')
3737 call writefile([repeat("\u3042 ", 120)], 'Xmultibyte')
3738
3739 let buf = RunVimInTerminal('-S XtestPopupLatin', #{rows: 10})
Bram Moolenaar4c5bdb92020-12-17 17:45:59 +01003740 call WaitForAssert({-> assert_match('Done', term_getline(buf, 10))})
Bram Moolenaar927495b2020-11-06 17:58:35 +01003741
3742 call term_sendkeys(buf, ":q\<CR>")
3743 call StopVimInTerminal(buf)
3744 call delete('XtestPopupLatin')
3745 call delete('Xmultibyte')
3746endfunc
3747
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003748func Test_popupwin_atcursor_far_right()
3749 new
3750
3751 " this was getting stuck
3752 set signcolumn=yes
3753 call setline(1, repeat('=', &columns))
3754 normal! ggg$
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003755 let winid = popup_atcursor(repeat('x', 500), #{moved: 'any', border: []})
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003756
Bram Moolenaar6d585f42020-07-26 22:20:54 +02003757 " 'signcolumn' was getting reset
3758 call setwinvar(winid, '&signcolumn', 'yes')
3759 call popup_setoptions(winid, {'zindex': 1000})
3760 call assert_equal('yes', getwinvar(winid, '&signcolumn'))
3761
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003762 call popup_close(winid)
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003763 bwipe!
3764 set signcolumn&
3765endfunc
3766
Bram Moolenaar0f1563f2020-03-20 21:15:51 +01003767func Test_popupwin_splitmove()
3768 vsplit
3769 let win2 = win_getid()
3770 let popup_winid = popup_dialog('hello', {})
3771 call assert_fails('call win_splitmove(popup_winid, win2)', 'E957:')
3772 call assert_fails('call win_splitmove(win2, popup_winid)', 'E957:')
3773
3774 call popup_clear()
3775 bwipe
3776endfunc
3777
Bram Moolenaarcbcd9cb2020-11-07 16:58:59 +01003778func Test_popupwin_exiting_terminal()
3779 CheckFeature terminal
3780
3781 " Tests that when creating a popup right after closing a terminal window does
3782 " not make the popup the current window.
3783 let winid = win_getid()
3784 try
3785 augroup Test_popupwin_exiting_terminal
3786 autocmd!
3787 autocmd WinEnter * :call popup_create('test', {})
3788 augroup END
3789 let bnr = term_start(&shell, #{term_finish: 'close'})
3790 call term_sendkeys(bnr, "exit\r\n")
3791 call WaitForAssert({-> assert_equal(winid, win_getid())})
3792 finally
3793 call popup_clear(1)
3794 augroup Test_popupwin_exiting_terminal
3795 autocmd!
3796 augroup END
3797 endtry
3798endfunc
Bram Moolenaar0f1563f2020-03-20 21:15:51 +01003799
Bram Moolenaar014f6982021-01-04 13:18:30 +01003800func Test_popup_filter_menu()
3801 let colors = ['red', 'green', 'blue']
3802 call popup_menu(colors, #{callback: {_, result -> assert_equal('green', colors[result - 1])}})
3803 call feedkeys("\<c-n>\<c-n>\<c-p>\<cr>", 'xt')
3804endfunc
3805
Bram Moolenaar331bafd2019-07-20 17:46:05 +02003806" vim: shiftwidth=2 sts=2