blob: b8641fe7d2b37ef5769a8f1cddc4cd0baa7d0727 [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')
824endfunc
825
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200826func Test_popup_select()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200827 CheckScreendump
828 CheckFeature clipboard_working
829
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200830 " create a popup with some text to be selected
831 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200832 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200833 call setline(1, range(1, 20))
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200834 let winid = popup_create(['the word', 'some more', 'several words here', 'invisible', '5', '6', '7'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200835 \ drag: 1,
836 \ border: [],
837 \ line: 3,
838 \ col: 10,
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200839 \ maxheight: 3,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200840 \ })
841 func Select1()
842 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
843 endfunc
844 map <silent> <F3> :call test_setmouse(4, 15)<CR>
845 map <silent> <F4> :call test_setmouse(6, 23)<CR>
846 END
847 call writefile(lines, 'XtestPopupSelect')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200848 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200849 call term_sendkeys(buf, ":call Select1()\<CR>")
850 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
851
852 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
853 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200854 " clean the command line, sometimes it still shows a command
855 call term_sendkeys(buf, ":\<esc>")
856
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200857 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
858
859 " clean up
860 call StopVimInTerminal(buf)
861 call delete('XtestPopupSelect')
862endfunc
863
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200864func Test_popup_in_tab()
865 " default popup is local to tab, not visible when in other tab
866 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200867 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200868 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200869 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200870 tabnew
871 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200872 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200873 quit
874 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200875
876 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200877 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200878 " buffer is gone now
879 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200880
881 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200882 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200883 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200884 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200885 tabnew
886 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200887 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200888 quit
889 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200890 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200891
892 " create popup in other tab
893 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200894 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200895 call assert_equal(0, popup_getpos(winid).visible)
896 call assert_equal(1, popup_getoptions(winid).tabpage)
897 quit
898 call assert_equal(1, popup_getpos(winid).visible)
899 call assert_equal(0, popup_getoptions(winid).tabpage)
900 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200901endfunc
902
903func Test_popup_valid_arguments()
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200904 call assert_equal(0, len(popup_list()))
905
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200906 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200907 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200908 let pos = popup_getpos(winid)
909 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200910 call assert_equal([winid], popup_list())
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200911 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200912
913 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200914 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200915 let pos = popup_getpos(winid)
916 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200917 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200918
919 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200920 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200921 let pos = popup_getpos(winid)
922 let around = (&columns - pos.width) / 2
923 call assert_inrange(around - 1, around + 1, pos.col)
924 let around = (&lines - pos.height) / 2
925 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200926 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200927endfunc
928
929func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200930 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200931 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200932 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200933 call popup_clear()
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200934 call assert_fails('call popup_create(test_null_string(), {})', 'E450:')
935 call assert_fails('call popup_create(test_null_list(), {})', 'E450:')
936 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200937
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200938 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200939 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200940 call assert_fails('call popup_create("text", #{col: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200941 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200942 call assert_fails('call popup_create("text", #{col: "cursor+x"})', 'E15:')
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: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200945 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200946
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200947 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200948 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200949 call assert_fails('call popup_create("text", #{line: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200950 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200951 call assert_fails('call popup_create("text", #{line: "cursor+x"})', 'E15:')
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: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200954 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200955
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200956 call assert_fails('call popup_create("text", #{pos: "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200957 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200958 call assert_fails('call popup_create("text", #{padding: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200959 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200960 call assert_fails('call popup_create("text", #{border: "none"})', 'E714:')
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", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200963 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +0200964 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
965 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200966 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200967 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200968
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200969 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200970 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200971 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200972 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200973 call assert_fails('call popup_create([#{text: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200974 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +0100975 call assert_fails('call popup_create([#{text: "text", props: range(3)}], {})', 'E715:')
976 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200977 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
978 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +0100979 call assert_fails('call popup_create("text", #{mask: range(5)})', 'E475:')
980 call popup_clear()
981 call popup_create("text", #{mask: [range(4)]})
982 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200983 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200984 call assert_fails('call popup_create("text", #{mapping: []})', 'E745:')
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200985 call popup_clear()
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200986 call assert_fails('call popup_create("text", #{tabpage : 4})', 'E997:')
987 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200988endfunc
989
Bram Moolenaareea16992019-05-31 17:34:48 +0200990func Test_win_execute_closing_curwin()
991 split
992 let winid = popup_create('some text', {})
Bram Moolenaare2e40752020-09-04 21:18:46 +0200993 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200994 call popup_clear()
Bram Moolenaar49540192019-12-11 19:34:54 +0100995
996 let winid = popup_create('some text', {})
Bram Moolenaare2e40752020-09-04 21:18:46 +0200997 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 +0100998 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200999endfunc
1000
1001func Test_win_execute_not_allowed()
1002 let winid = popup_create('some text', {})
1003 call assert_fails('call win_execute(winid, "split")', 'E994:')
1004 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
1005 call assert_fails('call win_execute(winid, "close")', 'E994:')
1006 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +02001007 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001008 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
1009 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
1010 call assert_fails('call win_execute(winid, "next")', 'E994:')
1011 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01001012 call assert_fails('call win_execute(winid, "pedit filename")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001013 call assert_fails('call win_execute(winid, "buf")', 'E994:')
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01001014 call assert_fails('call win_execute(winid, "bnext")', 'E994:')
1015 call assert_fails('call win_execute(winid, "bprev")', 'E994:')
1016 call assert_fails('call win_execute(winid, "bfirst")', 'E994:')
1017 call assert_fails('call win_execute(winid, "blast")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001018 call assert_fails('call win_execute(winid, "edit")', 'E994:')
1019 call assert_fails('call win_execute(winid, "enew")', 'E994:')
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001020 call assert_fails('call win_execute(winid, "help")', 'E994:')
1021 call assert_fails('call win_execute(winid, "1only")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001022 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
1023 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
1024 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
1025 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001026 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +02001027endfunc
1028
Bram Moolenaar402502d2019-05-30 22:07:36 +02001029func Test_popup_with_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001030 CheckScreendump
1031
Bram Moolenaar402502d2019-05-30 22:07:36 +02001032 let lines =<< trim END
1033 call setline(1, range(1, 100))
1034 let winid = popup_create(
1035 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001036 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001037 END
1038 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001039 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001040 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
1041
1042 " clean up
1043 call StopVimInTerminal(buf)
1044 call delete('XtestPopup')
1045endfunc
1046
1047func Test_popup_without_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001048 CheckScreendump
1049
Bram Moolenaar402502d2019-05-30 22:07:36 +02001050 let lines =<< trim END
1051 call setline(1, range(1, 100))
1052 let winid = popup_create(
1053 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001054 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001055 END
1056 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001057 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001058 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
1059
1060 " clean up
1061 call StopVimInTerminal(buf)
1062 call delete('XtestPopup')
1063endfunc
1064
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001065func Test_popup_with_showbreak()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001066 CheckScreendump
1067
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001068 let lines =<< trim END
1069 set showbreak=>>\
1070 call setline(1, range(1, 20))
1071 let winid = popup_dialog(
Bram Moolenaar8ae54372019-09-15 18:11:16 +02001072 \ 'a long line here that wraps',
1073 \ #{filter: 'popup_filter_yesno',
1074 \ maxwidth: 12})
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001075 END
1076 call writefile(lines, 'XtestPopupShowbreak')
1077 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
1078 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
1079
1080 " clean up
1081 call term_sendkeys(buf, "y")
1082 call StopVimInTerminal(buf)
1083 call delete('XtestPopupShowbreak')
1084endfunc
1085
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001086func Test_popup_time()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001087 CheckFeature timers
1088
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001089 topleft vnew
1090 call setline(1, 'hello')
1091
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001092 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001093 \ line: 1,
1094 \ col: 1,
1095 \ minwidth: 20,
1096 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001097 \})
1098 redraw
1099 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1100 call assert_equal('world', line)
1101
Bram Moolenaarb4f06282019-07-12 21:07:54 +02001102 call assert_equal(winid, popup_locate(1, 1))
1103 call assert_equal(winid, popup_locate(1, 20))
1104 call assert_equal(0, popup_locate(1, 21))
1105 call assert_equal(0, popup_locate(2, 1))
1106
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001107 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +02001108 redraw
Bram Moolenaar196b4662019-09-06 21:34:30 +02001109 let line = join(map(range(1, 5), '1->screenstring(v:val)'), '')
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001110 call assert_equal('hello', line)
1111
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001112 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001113 \ line: &lines,
1114 \ col: 10,
1115 \ minwidth: 20,
1116 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001117 \})
1118 redraw
1119 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1120 call assert_match('.*on the command line.*', line)
1121
1122 sleep 700m
1123 redraw
1124 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1125 call assert_notmatch('.*on the command line.*', line)
1126
1127 bwipe!
1128endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001129
1130func Test_popup_hide()
1131 topleft vnew
1132 call setline(1, 'hello')
1133
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001134 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001135 \ line: 1,
1136 \ col: 1,
1137 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001138 \})
1139 redraw
1140 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1141 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001142 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001143 " buffer is still listed and active
1144 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001145
1146 call popup_hide(winid)
1147 redraw
1148 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1149 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001150 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001151 " buffer is still listed but hidden
Bram Moolenaarca7c0782020-01-14 20:42:48 +01001152 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001153
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001154 eval winid->popup_show()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001155 redraw
1156 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1157 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001158 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001159
1160
1161 call popup_close(winid)
1162 redraw
1163 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1164 call assert_equal('hello', line)
1165
1166 " error is given for existing non-popup window
1167 call assert_fails('call popup_hide(win_getid())', 'E993:')
1168
1169 " no error non-existing window
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001170 eval 1234234->popup_hide()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001171 call popup_show(41234234)
1172
1173 bwipe!
1174endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001175
1176func Test_popup_move()
1177 topleft vnew
1178 call setline(1, 'hello')
1179
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001180 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001181 \ line: 1,
1182 \ col: 1,
1183 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001184 \})
1185 redraw
1186 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1187 call assert_equal('world ', line)
1188
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001189 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001190 redraw
1191 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1192 call assert_equal('hello ', line)
1193 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
1194 call assert_equal('~world', line)
1195
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001196 eval winid->popup_move(#{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001197 redraw
1198 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1199 call assert_equal('hworld', line)
1200
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001201 call assert_fails('call popup_move(winid, [])', 'E715:')
1202 call assert_fails('call popup_move(winid, test_null_dict())', 'E715:')
1203
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001204 call popup_close(winid)
1205
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001206 call assert_equal(0, popup_move(-1, {}))
1207
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001208 bwipe!
1209endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +02001210
Bram Moolenaar402502d2019-05-30 22:07:36 +02001211func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001212 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001213 \ line: 2,
1214 \ col: 3,
1215 \ minwidth: 10,
1216 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +02001217 \})
1218 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001219 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001220 call assert_equal(2, res.line)
1221 call assert_equal(3, res.col)
1222 call assert_equal(10, res.width)
1223 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001224 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001225
1226 call popup_close(winid)
1227endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001228
1229func Test_popup_width_longest()
1230 let tests = [
1231 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
1232 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
1233 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
1234 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
1235 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
1236 \ ]
1237
1238 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001239 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001240 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001241 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001242 call assert_equal(test[1], position.width)
1243 call popup_close(winid)
1244 endfor
1245endfunc
1246
1247func Test_popup_wraps()
1248 let tests = [
1249 \ ['nowrap', 6, 1],
1250 \ ['a line that wraps once', 12, 2],
1251 \ ['a line that wraps two times', 12, 3],
1252 \ ]
1253 for test in tests
1254 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001255 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001256 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001257 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001258 call assert_equal(test[1], position.width)
1259 call assert_equal(test[2], position.height)
1260
1261 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001262 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001263 endfor
1264endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001265
1266func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001267 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001268 \ line: 2,
1269 \ col: 3,
1270 \ minwidth: 10,
1271 \ minheight: 11,
1272 \ maxwidth: 20,
1273 \ maxheight: 21,
1274 \ zindex: 100,
1275 \ time: 5000,
1276 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001277 \})
1278 redraw
1279 let res = popup_getoptions(winid)
1280 call assert_equal(2, res.line)
1281 call assert_equal(3, res.col)
1282 call assert_equal(10, res.minwidth)
1283 call assert_equal(11, res.minheight)
1284 call assert_equal(20, res.maxwidth)
1285 call assert_equal(21, res.maxheight)
1286 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001287 call assert_equal(1, res.fixed)
Bram Moolenaarb8350ab2019-08-04 17:59:49 +02001288 call assert_equal(1, res.mapping)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001289 if has('timers')
1290 call assert_equal(5000, res.time)
1291 endif
1292 call popup_close(winid)
1293
1294 let winid = popup_create('hello', {})
1295 redraw
1296 let res = popup_getoptions(winid)
1297 call assert_equal(0, res.line)
1298 call assert_equal(0, res.col)
1299 call assert_equal(0, res.minwidth)
1300 call assert_equal(0, res.minheight)
1301 call assert_equal(0, res.maxwidth)
1302 call assert_equal(0, res.maxheight)
1303 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001304 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001305 if has('timers')
1306 call assert_equal(0, res.time)
1307 endif
1308 call popup_close(winid)
1309 call assert_equal({}, popup_getoptions(winid))
1310endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001311
1312func Test_popup_option_values()
1313 new
1314 " window-local
1315 setlocal number
1316 setlocal nowrap
1317 " buffer-local
1318 setlocal omnifunc=Something
1319 " global/buffer-local
1320 setlocal path=/there
1321 " global/window-local
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001322 setlocal statusline=2
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001323
1324 let winid = popup_create('hello', {})
1325 call assert_equal(0, getwinvar(winid, '&number'))
1326 call assert_equal(1, getwinvar(winid, '&wrap'))
1327 call assert_equal('', getwinvar(winid, '&omnifunc'))
1328 call assert_equal(&g:path, getwinvar(winid, '&path'))
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001329 call assert_equal(&g:statusline, getwinvar(winid, '&statusline'))
1330
1331 " 'scrolloff' is reset to zero
1332 call assert_equal(5, &scrolloff)
1333 call assert_equal(0, getwinvar(winid, '&scrolloff'))
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001334
1335 call popup_close(winid)
1336 bwipe
1337endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001338
1339func Test_popup_atcursor()
1340 topleft vnew
1341 call setline(1, [
1342 \ 'xxxxxxxxxxxxxxxxx',
1343 \ 'xxxxxxxxxxxxxxxxx',
1344 \ 'xxxxxxxxxxxxxxxxx',
1345 \])
1346
1347 call cursor(2, 2)
1348 redraw
1349 let winid = popup_atcursor('vim', {})
1350 redraw
1351 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1352 call assert_equal('xvimxxxxxxxxxxxxx', line)
1353 call popup_close(winid)
1354
1355 call cursor(3, 4)
1356 redraw
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001357 let winid = 'vim'->popup_atcursor({})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001358 redraw
1359 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
1360 call assert_equal('xxxvimxxxxxxxxxxx', line)
1361 call popup_close(winid)
1362
1363 call cursor(1, 1)
1364 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001365 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001366 \ line: 'cursor+2',
1367 \ col: 'cursor+1',
1368 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001369 redraw
1370 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1371 call assert_equal('xvimxxxxxxxxxxxxx', line)
1372 call popup_close(winid)
1373
1374 call cursor(3, 3)
1375 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001376 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001377 \ line: 'cursor-2',
1378 \ col: 'cursor-1',
1379 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001380 redraw
1381 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1382 call assert_equal('xvimxxxxxxxxxxxxx', line)
1383 call popup_close(winid)
1384
Bram Moolenaar402502d2019-05-30 22:07:36 +02001385 " just enough room above
1386 call cursor(3, 3)
1387 redraw
1388 let winid = popup_atcursor(['vim', 'is great'], {})
1389 redraw
1390 let pos = popup_getpos(winid)
1391 call assert_equal(1, pos.line)
1392 call popup_close(winid)
1393
1394 " not enough room above, popup goes below the cursor
1395 call cursor(3, 3)
1396 redraw
1397 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1398 redraw
1399 let pos = popup_getpos(winid)
1400 call assert_equal(4, pos.line)
1401 call popup_close(winid)
1402
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001403 " cursor in first line, popup in line 2
1404 call cursor(1, 1)
1405 redraw
1406 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1407 redraw
1408 let pos = popup_getpos(winid)
1409 call assert_equal(2, pos.line)
1410 call popup_close(winid)
1411
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001412 bwipe!
1413endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001414
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001415func Test_popup_atcursor_pos()
1416 CheckScreendump
1417
1418 let lines =<< trim END
1419 call setline(1, repeat([repeat('-', 60)], 15))
1420 set so=0
1421
1422 normal 9G3|r#
1423 let winid1 = popup_atcursor(['first', 'second'], #{
1424 \ moved: [0, 0, 0],
1425 \ })
1426 normal 9G21|r&
1427 let winid1 = popup_atcursor(['FIrsT', 'SEcoND'], #{
1428 \ pos: 'botright',
1429 \ moved: [0, 0, 0],
1430 \ })
1431 normal 3G27|r%
1432 let winid1 = popup_atcursor(['fiRSt', 'seCOnd'], #{
1433 \ pos: 'topleft',
1434 \ moved: [0, 0, 0],
1435 \ })
1436 normal 3G45|r@
1437 let winid1 = popup_atcursor(['First', 'SeconD'], #{
1438 \ pos: 'topright',
Bram Moolenaarb0992022020-01-30 14:55:42 +01001439 \ moved: range(3),
1440 \ mousemoved: range(3),
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001441 \ })
1442 END
1443 call writefile(lines, 'XtestPopupAtcursorPos')
1444 let buf = RunVimInTerminal('-S XtestPopupAtcursorPos', #{rows: 12})
1445 call VerifyScreenDump(buf, 'Test_popupwin_atcursor_pos', {})
1446
1447 " clean up
1448 call StopVimInTerminal(buf)
1449 call delete('XtestPopupAtcursorPos')
1450endfunc
1451
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001452func Test_popup_beval()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001453 CheckScreendump
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001454 CheckFeature balloon_eval_term
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001455
1456 let lines =<< trim END
1457 call setline(1, range(1, 20))
1458 call setline(5, 'here is some text to hover over')
1459 set balloonevalterm
1460 set balloonexpr=BalloonExpr()
1461 set balloondelay=100
1462 func BalloonExpr()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001463 let s:winid = [v:beval_text]->popup_beval({})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001464 return ''
1465 endfunc
1466 func Hover()
1467 call test_setmouse(5, 15)
1468 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1469 sleep 100m
1470 endfunc
1471 func MoveOntoPopup()
1472 call test_setmouse(4, 17)
1473 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1474 endfunc
1475 func MoveAway()
1476 call test_setmouse(5, 13)
1477 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1478 endfunc
1479 END
1480 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001481 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001482 call TermWait(buf, 50)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001483 call term_sendkeys(buf, 'j')
1484 call term_sendkeys(buf, ":call Hover()\<CR>")
1485 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1486
1487 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1488 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1489
1490 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1491 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1492
1493 " clean up
1494 call StopVimInTerminal(buf)
1495 call delete('XtestPopupBeval')
1496endfunc
1497
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001498func Test_popup_filter()
1499 new
1500 call setline(1, 'some text')
1501
1502 func MyPopupFilter(winid, c)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001503 if a:c == 'e' || a:c == "\<F9>"
1504 let g:eaten = a:c
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001505 return 1
1506 endif
1507 if a:c == '0'
1508 let g:ignored = '0'
1509 return 0
1510 endif
1511 if a:c == 'x'
1512 call popup_close(a:winid)
1513 return 1
1514 endif
1515 return 0
1516 endfunc
1517
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001518 let winid = 'something'->popup_create(#{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001519 redraw
1520
1521 " e is consumed by the filter
1522 call feedkeys('e', 'xt')
1523 call assert_equal('e', g:eaten)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001524 call feedkeys("\<F9>", 'xt')
1525 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001526
1527 " 0 is ignored by the filter
1528 normal $
1529 call assert_equal(9, getcurpos()[2])
1530 call feedkeys('0', 'xt')
1531 call assert_equal('0', g:ignored)
1532 call assert_equal(1, getcurpos()[2])
1533
1534 " x closes the popup
1535 call feedkeys('x', 'xt')
Bram Moolenaarec084d32020-02-28 22:44:47 +01001536 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001537 call assert_equal(-1, winbufnr(winid))
1538
1539 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001540 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001541endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001542
Bram Moolenaar4934ad02020-09-28 22:29:58 +02001543" this tests that the filter is not used for :normal command
Bram Moolenaar189832b2020-09-23 12:29:11 +02001544func Test_popup_filter_normal_cmd()
1545 CheckScreendump
1546
1547 let lines =<< trim END
Bram Moolenaar4934ad02020-09-28 22:29:58 +02001548 let text = range(1, 20)->map({_, v -> string(v)})
1549 let g:winid = popup_create(text, #{maxheight: 5, minwidth: 3, filter: 'invalidfilter'})
1550 call timer_start(0, {-> win_execute(g:winid, 'norm! 10Gzz')})
Bram Moolenaar189832b2020-09-23 12:29:11 +02001551 END
1552 call writefile(lines, 'XtestPopupNormal')
1553 let buf = RunVimInTerminal('-S XtestPopupNormal', #{rows: 10})
1554 call TermWait(buf, 100)
1555 call VerifyScreenDump(buf, 'Test_popupwin_normal_cmd', {})
1556
1557 call StopVimInTerminal(buf)
1558 call delete('XtestPopupNormal')
1559endfunc
1560
Bram Moolenaar10476522020-09-24 22:57:31 +02001561" test that cursor line highlight is updated after using win_execute()
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001562func Test_popup_filter_win_execute()
1563 CheckScreendump
1564
1565 let lines =<< trim END
Bram Moolenaar10476522020-09-24 22:57:31 +02001566 let lines = range(1, &lines * 2)->map({_, v -> string(v)})
1567 let g:id = popup_create(lines, #{
1568 \ minheight: &lines - 5,
1569 \ maxheight: &lines - 5,
1570 \ cursorline: 1,
1571 \ })
1572 redraw
1573 END
1574 call writefile(lines, 'XtestPopupWinExecute')
1575 let buf = RunVimInTerminal('-S XtestPopupWinExecute', #{rows: 14})
1576
1577 call term_sendkeys(buf, ":call win_execute(g:id, ['normal 17Gzz'])\<CR>")
1578 call term_sendkeys(buf, ":\<CR>")
1579
1580 call VerifyScreenDump(buf, 'Test_popupwin_win_execute_cursorline', {})
1581
1582 call StopVimInTerminal(buf)
1583 call delete('XtestPopupWinExecute')
1584endfunc
1585
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001586func Test_popup_set_firstline()
1587 CheckScreendump
1588
1589 let lines =<< trim END
1590 let lines = range(1, 50)->map({_, v -> string(v)})
1591 let g:id = popup_create(lines, #{
1592 \ minwidth: 20,
1593 \ maxwidth: 20,
1594 \ minheight: &lines - 5,
1595 \ maxheight: &lines - 5,
1596 \ cursorline: 1,
1597 \ })
1598 call popup_setoptions(g:id, #{firstline: 10})
1599 redraw
1600 END
1601 call writefile(lines, 'XtestPopupWinSetFirstline')
1602 let buf = RunVimInTerminal('-S XtestPopupWinSetFirstline', #{rows: 16})
1603
1604 call VerifyScreenDump(buf, 'Test_popupwin_set_firstline_1', {})
1605
1606 call term_sendkeys(buf, ":call popup_setoptions(g:id, #{firstline: 5})\<CR>")
1607 call term_sendkeys(buf, ":\<CR>")
1608 call VerifyScreenDump(buf, 'Test_popupwin_set_firstline_2', {})
1609
1610 call StopVimInTerminal(buf)
1611 call delete('XtestPopupWinSetFirstline')
1612endfunc
1613
Bram Moolenaar10476522020-09-24 22:57:31 +02001614" this tests that we don't get stuck with an error in "win_execute()"
1615func Test_popup_filter_win_execute_error()
1616 CheckScreendump
1617
1618 let lines =<< trim END
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001619 let g:winid = popup_create('some text', {'filter': 'invalidfilter'})
1620 call timer_start(0, {-> win_execute(g:winid, 'invalidCommand')})
1621 END
Bram Moolenaar10476522020-09-24 22:57:31 +02001622 call writefile(lines, 'XtestPopupWinExecuteError')
1623 let buf = RunVimInTerminal('-S XtestPopupWinExecuteError', #{rows: 10, wait_for_ruler: 0})
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001624
1625 call WaitFor({-> term_getline(buf, 9) =~ 'Not an editor command: invalidCommand'})
1626 call term_sendkeys(buf, "\<CR>")
1627 call WaitFor({-> term_getline(buf, 9) =~ 'Unknown function: invalidfilter'})
1628 call term_sendkeys(buf, "\<CR>")
1629 call WaitFor({-> term_getline(buf, 9) =~ 'Not allowed in a popup window'})
1630 call term_sendkeys(buf, "\<CR>")
1631 call term_sendkeys(buf, "\<CR>")
1632 call VerifyScreenDump(buf, 'Test_popupwin_win_execute', {})
1633
1634 call StopVimInTerminal(buf)
Bram Moolenaar10476522020-09-24 22:57:31 +02001635 call delete('XtestPopupWinExecuteError')
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001636endfunc
1637
Bram Moolenaara42d9452019-06-15 21:46:30 +02001638func ShowDialog(key, result)
1639 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001640 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001641 \ filter: 'popup_filter_yesno',
1642 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001643 \ })
1644 redraw
1645 call feedkeys(a:key, "xt")
1646 call assert_equal(winid, s:cb_winid)
1647 call assert_equal(a:result, s:cb_res)
1648endfunc
1649
1650func Test_popup_dialog()
1651 func QuitCallback(id, res)
1652 let s:cb_winid = a:id
1653 let s:cb_res = a:res
1654 endfunc
1655
1656 let winid = ShowDialog("y", 1)
1657 let winid = ShowDialog("Y", 1)
1658 let winid = ShowDialog("n", 0)
1659 let winid = ShowDialog("N", 0)
1660 let winid = ShowDialog("x", 0)
1661 let winid = ShowDialog("X", 0)
1662 let winid = ShowDialog("\<Esc>", 0)
1663 let winid = ShowDialog("\<C-C>", -1)
1664
1665 delfunc QuitCallback
1666endfunc
1667
Bram Moolenaara730e552019-06-16 19:05:31 +02001668func ShowMenu(key, result)
1669 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001670 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001671 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001672 \ })
1673 redraw
1674 call feedkeys(a:key, "xt")
1675 call assert_equal(winid, s:cb_winid)
1676 call assert_equal(a:result, s:cb_res)
1677endfunc
1678
1679func Test_popup_menu()
1680 func QuitCallback(id, res)
1681 let s:cb_winid = a:id
1682 let s:cb_res = a:res
1683 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001684 " mapping won't be used in popup
1685 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001686
1687 let winid = ShowMenu(" ", 1)
1688 let winid = ShowMenu("j \<CR>", 2)
1689 let winid = ShowMenu("JjK \<CR>", 2)
1690 let winid = ShowMenu("jjjjjj ", 3)
1691 let winid = ShowMenu("kkk ", 1)
1692 let winid = ShowMenu("x", -1)
1693 let winid = ShowMenu("X", -1)
1694 let winid = ShowMenu("\<Esc>", -1)
1695 let winid = ShowMenu("\<C-C>", -1)
1696
1697 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001698 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001699endfunc
1700
1701func Test_popup_menu_screenshot()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001702 CheckScreendump
Bram Moolenaara730e552019-06-16 19:05:31 +02001703
1704 let lines =<< trim END
1705 call setline(1, range(1, 20))
1706 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001707 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001708 func MenuDone(id, res)
1709 echomsg "selected " .. a:res
1710 endfunc
1711 END
1712 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001713 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001714 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1715
1716 call term_sendkeys(buf, "jj")
1717 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1718
1719 call term_sendkeys(buf, " ")
1720 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1721
1722 " clean up
1723 call StopVimInTerminal(buf)
1724 call delete('XtestPopupMenu')
1725endfunc
1726
Bram Moolenaarf914a332019-07-20 15:09:56 +02001727func Test_popup_menu_narrow()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001728 CheckScreendump
Bram Moolenaarf914a332019-07-20 15:09:56 +02001729
1730 let lines =<< trim END
1731 call setline(1, range(1, 20))
1732 hi PopupSelected ctermbg=green
1733 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1734 func MenuDone(id, res)
1735 echomsg "selected " .. a:res
1736 endfunc
1737 END
1738 call writefile(lines, 'XtestPopupNarrowMenu')
1739 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1740 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1741
1742 " clean up
1743 call term_sendkeys(buf, "x")
1744 call StopVimInTerminal(buf)
1745 call delete('XtestPopupNarrowMenu')
1746endfunc
1747
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001748func Test_popup_title()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001749 CheckScreendump
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001750
1751 " Create a popup without title or border, a line of padding will be added to
1752 " put the title on.
1753 let lines =<< trim END
1754 call setline(1, range(1, 20))
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001755 let winid = popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001756 END
1757 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001758 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001759 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1760
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001761 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxwidth: 20, title: 'a very long title that is not going to fit'})\<CR>")
1762 call term_sendkeys(buf, ":\<CR>")
1763 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_1', {})
1764
1765 call term_sendkeys(buf, ":call popup_setoptions(winid, #{border: []})\<CR>")
1766 call term_sendkeys(buf, ":\<CR>")
1767 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_2', {})
1768
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01001769 call term_sendkeys(buf, ":call popup_clear()\<CR>")
1770 call term_sendkeys(buf, ":call popup_create(['aaa', 'bbb'], #{title: 'Title', minwidth: 12, padding: [2, 2, 2, 2]})\<CR>")
1771 call term_sendkeys(buf, ":\<CR>")
1772 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_3', {})
1773
1774 call term_sendkeys(buf, ":call popup_clear()\<CR>")
1775 call term_sendkeys(buf, ":call popup_create(['aaa', 'bbb'], #{title: 'Title', minwidth: 12, border: [], padding: [2, 2, 2, 2]})\<CR>")
1776 call term_sendkeys(buf, ":\<CR>")
1777 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_4', {})
1778
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001779 " clean up
1780 call StopVimInTerminal(buf)
1781 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001782
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001783 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001784 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001785 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001786 call assert_equal('Another Title', popup_getoptions(winid).title)
1787
1788 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001789endfunc
1790
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001791func Test_popup_close_callback()
1792 func PopupDone(id, result)
1793 let g:result = a:result
1794 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001795 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001796 redraw
1797 call popup_close(winid, 'done')
1798 call assert_equal('done', g:result)
1799endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001800
1801func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001802 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001803 redraw
1804 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001805 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001806 call assert_equal(5, pos.height)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001807 call popup_close(winid)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001808
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001809 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001810 redraw
1811 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001812 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001813 call assert_equal(3, pos.height)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001814 call popup_close(winid)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001815endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001816
1817func Test_popup_never_behind()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001818 CheckScreendump
1819
Bram Moolenaar988c4332019-06-02 14:12:11 +02001820 " +-----------------------------+
1821 " | | |
1822 " | | |
1823 " | | |
1824 " | line1 |
1825 " |------------line2------------|
1826 " | line3 |
1827 " | line4 |
1828 " | |
1829 " | |
1830 " +-----------------------------+
1831 let lines =<< trim END
Bram Moolenaar988c4332019-06-02 14:12:11 +02001832 split
1833 vsplit
1834 let info_window1 = getwininfo()[0]
1835 let line = info_window1['height']
1836 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001837 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001838 \ line : line,
1839 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001840 \ })
1841 END
1842 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001843 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001844 call term_sendkeys(buf, "\<C-W>w")
1845 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1846
1847 " clean up
1848 call StopVimInTerminal(buf)
1849 call delete('XtestPopupBehind')
1850endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001851
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001852func s:VerifyPosition(p, msg, line, col, width, height)
1853 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1854 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1855 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1856 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001857endfunc
1858
1859func Test_popup_position_adjust()
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001860 " Anything placed past the last cell on the right of the screen is moved to
1861 " the left.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001862 "
1863 " When wrapping is disabled, we also shift to the left to display on the
1864 " screen, unless fixed is set.
1865
1866 " Entries for cases which don't vary based on wrapping.
1867 " Format is per tests described below
1868 let both_wrap_tests = [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001869 \ ['a', 5, &columns, 5, &columns, 1, 1],
1870 \ ['b', 5, &columns + 1, 5, &columns, 1, 1],
1871 \ ['c', 5, &columns - 1, 5, &columns - 1, 1, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001872 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001873 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1]]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001874
1875 " these test groups are dicts with:
1876 " - comment: something to identify the group of tests by
1877 " - options: dict of options to merge with the row/col in tests
1878 " - tests: list of cases. Each one is a list with elements:
1879 " - text
1880 " - row
1881 " - col
1882 " - expected row
1883 " - expected col
1884 " - expected width
1885 " - expected height
1886 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001887 \ #{
1888 \ comment: 'left-aligned with wrapping',
1889 \ options: #{
1890 \ wrap: 1,
1891 \ pos: 'botleft',
1892 \ },
1893 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001894 \ ['aa', 5, &columns, 4, &columns, 1, 2],
1895 \ ['bb', 5, &columns + 1, 4, &columns, 1, 2],
1896 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1897 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1898 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1899 \
1900 \ ['aaa', 5, &columns, 3, &columns, 1, 3],
1901 \ ['bbb', 5, &columns + 1, 3, &columns, 1, 3],
1902 \ ['ccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
1903 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1904 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1905 \
1906 \ ['aaaa', 5, &columns, 2, &columns, 1, 4],
1907 \ ['bbbb', 5, &columns + 1, 2, &columns, 1, 4],
1908 \ ['cccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001909 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
1910 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001911 \ ['eeee', 5, &columns - 4, 5, &columns - 4, 4, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001912 \ ],
1913 \ },
1914 \ #{
1915 \ comment: 'left aligned without wrapping',
1916 \ options: #{
1917 \ wrap: 0,
1918 \ pos: 'botleft',
1919 \ },
1920 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001921 \ ['aa', 5, &columns, 5, &columns - 1, 2, 1],
1922 \ ['bb', 5, &columns + 1, 5, &columns - 1, 2, 1],
1923 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1924 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1925 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1926 \
1927 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
1928 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1929 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1930 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1931 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1932 \
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001933 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
1934 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
1935 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
1936 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
1937 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1938 \ ],
1939 \ },
1940 \ #{
1941 \ comment: 'left aligned with fixed position',
1942 \ options: #{
1943 \ wrap: 0,
1944 \ fixed: 1,
1945 \ pos: 'botleft',
1946 \ },
1947 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001948 \ ['aa', 5, &columns, 5, &columns, 1, 1],
1949 \ ['bb', 5, &columns + 1, 5, &columns, 1, 1],
1950 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1951 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1952 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1953 \
1954 \ ['aaa', 5, &columns, 5, &columns, 1, 1],
1955 \ ['bbb', 5, &columns + 1, 5, &columns, 1, 1],
1956 \ ['ccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1957 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1958 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1959 \
1960 \ ['aaaa', 5, &columns, 5, &columns, 1, 1],
1961 \ ['bbbb', 5, &columns + 1, 5, &columns, 1, 1],
1962 \ ['cccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001963 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1964 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1965 \ ],
1966 \ },
1967 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001968
1969 for test_group in tests
1970 for test in test_group.tests
1971 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001972 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001973 \ line: line,
1974 \ col: col,
1975 \ }
1976 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001977
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001978 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001979
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001980 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001981 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
1982 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001983 endfor
1984 endfor
1985
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001986 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001987 %bwipe!
1988endfunc
1989
Bram Moolenaar3397f742019-06-02 18:40:06 +02001990func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001991 " width of screen
1992 let X = join(map(range(&columns), {->'X'}), '')
1993
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001994 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1995 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001996
1997 redraw
1998 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1999 call assert_equal(X, line)
2000
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002001 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002002 redraw
2003
2004 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002005 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
2006 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002007
2008 redraw
2009 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2010 call assert_equal(X, line)
2011
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002012 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002013 redraw
2014
2015 " Extend so > window width
2016 let X .= 'x'
2017
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002018 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
2019 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002020
2021 redraw
2022 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2023 call assert_equal(X[ : -2 ], line)
2024
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002025 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002026 redraw
2027
2028 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002029 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
2030 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002031
2032 redraw
2033 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2034 call assert_equal(X[ : -2 ], line)
2035
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002036 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002037 redraw
2038
2039 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002040 let p = popup_create(X,
2041 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
2042 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002043
2044 redraw
2045 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2046 let e_line = ' ' . X[ 1 : -2 ]
2047 call assert_equal(e_line, line)
2048
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002049 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002050 redraw
2051
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002052 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002053 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02002054endfunc
2055
2056func Test_popup_moved()
2057 new
2058 call test_override('char_avail', 1)
2059 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
2060
2061 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002062 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002063 redraw
2064 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002065 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002066 " trigger the check for last_cursormoved by going into insert mode
2067 call feedkeys("li\<Esc>", 'xt')
2068 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002069 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002070
2071 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002072 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002073 redraw
2074 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002075 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002076 call feedkeys("hi\<Esc>", 'xt')
2077 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002078 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002079
2080 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002081 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002082 redraw
2083 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002084 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002085 call feedkeys("li\<Esc>", 'xt')
2086 call assert_equal(1, popup_getpos(winid).visible)
2087 call feedkeys("ei\<Esc>", 'xt')
2088 call assert_equal(1, popup_getpos(winid).visible)
2089 call feedkeys("eli\<Esc>", 'xt')
2090 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002091 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002092
Bram Moolenaar17627312019-06-02 19:53:44 +02002093 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02002094 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02002095 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002096 redraw
2097 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002098 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002099 call feedkeys("eli\<Esc>", 'xt')
2100 call assert_equal(1, popup_getpos(winid).visible)
2101 call feedkeys("wi\<Esc>", 'xt')
2102 call assert_equal(1, popup_getpos(winid).visible)
2103 call feedkeys("Eli\<Esc>", 'xt')
2104 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002105 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002106
2107 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002108 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002109 redraw
2110 call assert_equal(1, popup_getpos(winid).visible)
2111 call feedkeys("eli\<Esc>", 'xt')
2112 call feedkeys("ei\<Esc>", 'xt')
2113 call assert_equal(1, popup_getpos(winid).visible)
2114 call feedkeys("eli\<Esc>", 'xt')
2115 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002116 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002117
2118 bwipe!
2119 call test_override('ALL', 0)
2120endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002121
2122func Test_notifications()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002123 CheckFeature timers
2124 CheckScreendump
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002125
Bram Moolenaar0fdddee2019-09-01 15:26:23 +02002126 let lines =<< trim END
2127 call setline(1, range(1, 20))
2128 hi Notification ctermbg=lightblue
2129 call popup_notification('first notification', {})
2130 END
2131 call writefile(lines, 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002132 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002133 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
2134
2135 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002136 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
2137 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002138 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
2139
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002140 " clean up
2141 call StopVimInTerminal(buf)
2142 call delete('XtestNotifications')
2143endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002144
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002145func Test_popup_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002146 CheckScreendump
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002147
2148 let lines =<< trim END
2149 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02002150 hi ScrollThumb ctermbg=blue
2151 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002152 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002153 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002154 \ minwidth: 8,
2155 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002156 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002157 func ScrollUp()
2158 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
2159 endfunc
2160 func ScrollDown()
2161 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
2162 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002163 func ClickTop()
2164 call feedkeys("\<F4>\<LeftMouse>", "xt")
2165 endfunc
2166 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002167 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002168 call feedkeys("\<F5>\<LeftMouse>", "xt")
2169 endfunc
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002170 func Popup_filter(winid, key)
2171 if a:key == 'j'
Bram Moolenaar371806e2020-10-22 13:44:54 +02002172 silent! this_throws_an_error_but_is_ignored
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002173 let line = popup_getoptions(a:winid).firstline
2174 let nlines = line('$', a:winid)
2175 let newline = line < nlines ? (line + 1) : nlines
2176 call popup_setoptions(a:winid, #{firstline: newline})
2177 return v:true
2178 elseif a:key == 'x'
2179 call popup_close(a:winid)
2180 return v:true
2181 endif
2182 endfunc
2183
Bram Moolenaar6c542f72020-09-27 21:16:45 +02002184 def CreatePopup(text: list<string>)
Bram Moolenaare0de1712020-12-02 17:36:54 +01002185 popup_create(text, {
Bram Moolenaar6c542f72020-09-27 21:16:45 +02002186 \ minwidth: 30,
2187 \ maxwidth: 30,
2188 \ minheight: 4,
2189 \ maxheight: 4,
2190 \ firstline: 1,
2191 \ lastline: 4,
2192 \ wrap: true,
2193 \ scrollbar: true,
2194 \ mapping: false,
2195 \ filter: Popup_filter,
2196 \ })
2197 enddef
2198
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002199 func PopupScroll()
2200 call popup_clear()
2201 let text =<< trim END
2202 1
2203 2
2204 3
2205 4
2206 long line long line long line long line long line long line
2207 long line long line long line long line long line long line
2208 long line long line long line long line long line long line
2209 END
Bram Moolenaar6c542f72020-09-27 21:16:45 +02002210 call CreatePopup(text)
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002211 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002212 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002213 map <silent> <F4> :call test_setmouse(4, 42)<CR>
2214 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002215 END
2216 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002217 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002218 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
2219
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002220 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002221 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002222 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
2223
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002224 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<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_3', {})
2227
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002228 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<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_4', {})
2231
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02002232 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb', firstline: 5})\<CR>")
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002233 " this scrolls two lines (half the window height)
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002234 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
2235 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
2236
2237 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2238 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
2239
2240 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02002241 " wait a bit, otherwise it fails sometimes (double click recognized?)
2242 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002243 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2244 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
2245
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002246 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2247 sleep 100m
2248 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2249 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
2250
2251 call term_sendkeys(buf, ":call ClickBot()\<CR>")
2252 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
2253
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002254 " remove the minwidth and maxheight
2255 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxheight: 0, minwidth: 0})\<CR>")
Bram Moolenaar7e0f4622019-09-17 21:23:39 +02002256 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002257 call VerifyScreenDump(buf, 'Test_popupwin_scroll_10', {})
2258
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002259 " check size with non-wrapping lines
2260 call term_sendkeys(buf, ":call PopupScroll()\<CR>")
2261 call VerifyScreenDump(buf, 'Test_popupwin_scroll_11', {})
2262
2263 " check size with wrapping lines
2264 call term_sendkeys(buf, "j")
2265 call VerifyScreenDump(buf, 'Test_popupwin_scroll_12', {})
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002266
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002267 " clean up
Bram Moolenaar20298ce2020-06-19 21:46:52 +02002268 call term_sendkeys(buf, "x")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002269 call StopVimInTerminal(buf)
2270 call delete('XtestPopupScroll')
2271endfunc
2272
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02002273func Test_popup_too_high_scrollbar()
2274 CheckScreendump
2275
2276 let lines =<< trim END
2277 call setline(1, range(1, 20)->map({i, v -> repeat(v, 10)}))
2278 set scrolloff=0
2279 func ShowPopup()
2280 let winid = popup_atcursor(['one', 'two', 'three', 'four', 'five',
2281 \ 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve'], #{
2282 \ minwidth: 8,
2283 \ border: [],
2284 \ })
2285 endfunc
2286 normal 3G$
2287 call ShowPopup()
2288 END
2289 call writefile(lines, 'XtestPopupToohigh')
2290 let buf = RunVimInTerminal('-S XtestPopupToohigh', #{rows: 10})
2291 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_1', {})
2292
2293 call term_sendkeys(buf, ":call popup_clear()\<CR>")
2294 call term_sendkeys(buf, "8G$")
2295 call term_sendkeys(buf, ":call ShowPopup()\<CR>")
2296 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_2', {})
2297
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02002298 call term_sendkeys(buf, ":call popup_clear()\<CR>")
2299 call term_sendkeys(buf, "gg$")
2300 call term_sendkeys(buf, ":call ShowPopup()\<CR>")
2301 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_3', {})
2302
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02002303 " clean up
2304 call StopVimInTerminal(buf)
2305 call delete('XtestPopupToohigh')
2306endfunc
2307
Bram Moolenaar437a7462019-07-05 20:17:22 +02002308func Test_popup_fitting_scrollbar()
2309 " this was causing a crash, divide by zero
2310 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002311 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002312 \ scrollbar: 1,
2313 \ maxwidth: 10,
2314 \ maxheight: 5,
2315 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02002316 redraw
2317 call popup_clear()
2318endfunc
2319
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002320func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002321 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002322
2323 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002324 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002325 let p = popup_create('test', opts)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002326 eval p->popup_settext('this is a text')
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002327 END
2328
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002329 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002330 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002331 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
2332
2333 " Setting to empty string clears it
2334 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
2335 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
2336
2337 " Setting a list
2338 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2339 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2340
2341 " Shrinking with a list
2342 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
2343 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
2344
2345 " Growing with a list
2346 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2347 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2348
2349 " Empty list clears
2350 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
2351 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
2352
2353 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002354 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002355 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
2356
Bram Moolenaarb0992022020-01-30 14:55:42 +01002357 " range() (doesn't work)
2358 call term_sendkeys(buf, ":call popup_settext(p, range(4, 8))\<CR>")
2359 call VerifyScreenDump(buf, 'Test_popup_settext_07', {})
2360
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002361 " clean up
2362 call StopVimInTerminal(buf)
2363 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002364endfunc
2365
Bram Moolenaar1f42f5a2020-09-03 18:52:24 +02002366func Test_popup_settext_getline()
2367 let id = popup_create('', #{ tabpage: 0 })
2368 call popup_settext(id, ['a','b'])
2369 call assert_equal(2, line('$', id)) " OK :)
2370 call popup_close(id)
2371
2372 let id = popup_create('', #{ tabpage: -1 })
2373 call popup_settext(id, ['a','b'])
2374 call assert_equal(2, line('$', id)) " Fails :(
2375 call popup_close(id)
2376endfunc
2377
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02002378func Test_popup_settext_null()
2379 let id = popup_create('', #{ tabpage: 0 })
2380 call popup_settext(id, test_null_list())
2381 call popup_close(id)
2382
2383 let id = popup_create('', #{ tabpage: 0 })
2384 call popup_settext(id, test_null_string())
2385 call popup_close(id)
2386endfunc
2387
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002388func Test_popup_hidden()
2389 new
2390
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002391 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002392 redraw
2393 call assert_equal(0, popup_getpos(winid).visible)
2394 call popup_close(winid)
2395
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002396 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002397 redraw
2398 call assert_equal(0, popup_getpos(winid).visible)
2399 call popup_close(winid)
2400
2401 func QuitCallback(id, res)
2402 let s:cb_winid = a:id
2403 let s:cb_res = a:res
2404 endfunc
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002405 let winid = 'make a choice'->popup_dialog(#{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002406 \ filter: 'popup_filter_yesno',
2407 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002408 \ })
2409 redraw
2410 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02002411 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
2412 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002413 exe "normal anot used by filter\<Esc>"
2414 call assert_equal('not used by filter', getline(1))
2415
2416 call popup_show(winid)
2417 call feedkeys('y', "xt")
2418 call assert_equal(1, s:cb_res)
2419
2420 bwipe!
2421 delfunc QuitCallback
2422endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02002423
2424" Test options not checked elsewhere
2425func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002426 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002427 let options = popup_getoptions(winid)
2428 call assert_equal(1, options.wrap)
2429 call assert_equal(0, options.drag)
2430 call assert_equal('Beautiful', options.highlight)
2431
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002432 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002433 let options = popup_getoptions(winid)
2434 call assert_equal(0, options.wrap)
2435 call assert_equal(1, options.drag)
2436 call assert_equal('Another', options.highlight)
2437
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002438 call assert_fails('call popup_setoptions(winid, [])', 'E715:')
2439 call assert_fails('call popup_setoptions(winid, test_null_dict())', 'E715:')
2440
Bram Moolenaarae943152019-06-16 22:54:14 +02002441 call popup_close(winid)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002442 call assert_equal(0, popup_setoptions(winid, options.wrap))
Bram Moolenaarae943152019-06-16 22:54:14 +02002443endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002444
2445func Test_popupwin_garbage_collect()
2446 func MyPopupFilter(x, winid, c)
2447 " NOP
2448 endfunc
2449
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002450 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002451 call test_garbagecollect_now()
2452 redraw
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02002453 " Must not crash caused by invalid memory access
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002454 call feedkeys('j', 'xt')
2455 call assert_true(v:true)
2456
2457 call popup_close(winid)
2458 delfunc MyPopupFilter
2459endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002460
Bram Moolenaar581ba392019-09-03 22:08:33 +02002461func Test_popupwin_filter_mode()
2462 func MyPopupFilter(winid, c)
2463 let s:typed = a:c
2464 if a:c == ':' || a:c == "\r" || a:c == 'v'
2465 " can start cmdline mode, get out, and start/stop Visual mode
2466 return 0
2467 endif
2468 return 1
2469 endfunc
2470
2471 " Normal, Visual and Insert mode
2472 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'nvi'})
2473 redraw
2474 call feedkeys('x', 'xt')
2475 call assert_equal('x', s:typed)
2476
2477 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2478 call assert_equal(':', s:typed)
2479 call assert_equal('foo', g:foo)
2480
2481 let @x = 'something'
2482 call feedkeys('v$"xy', 'xt')
2483 call assert_equal('y', s:typed)
2484 call assert_equal('something', @x) " yank command is filtered out
2485 call feedkeys('v', 'xt') " end Visual mode
2486
2487 call popup_close(winid)
2488
2489 " only Normal mode
2490 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'n'})
2491 redraw
2492 call feedkeys('x', 'xt')
2493 call assert_equal('x', s:typed)
2494
2495 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2496 call assert_equal(':', s:typed)
2497 call assert_equal('foo', g:foo)
2498
2499 let @x = 'something'
2500 call feedkeys('v$"xy', 'xt')
2501 call assert_equal('v', s:typed)
2502 call assert_notequal('something', @x)
2503
2504 call popup_close(winid)
2505
2506 " default: all modes
2507 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
2508 redraw
2509 call feedkeys('x', 'xt')
2510 call assert_equal('x', s:typed)
2511
2512 let g:foo = 'bar'
2513 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2514 call assert_equal("\r", s:typed)
2515 call assert_equal('bar', g:foo)
2516
2517 let @x = 'something'
2518 call feedkeys('v$"xy', 'xt')
2519 call assert_equal('y', s:typed)
2520 call assert_equal('something', @x) " yank command is filtered out
2521 call feedkeys('v', 'xt') " end Visual mode
2522
2523 call popup_close(winid)
2524 delfunc MyPopupFilter
2525endfunc
2526
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002527func Test_popupwin_filter_mouse()
2528 func MyPopupFilter(winid, c)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002529 let g:got_mousepos = getmousepos()
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002530 return 0
2531 endfunc
2532
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002533 call setline(1, ['.'->repeat(25)]->repeat(10))
2534 let winid = popup_create(['short', 'long line that will wrap', 'other'], #{
2535 \ line: 2,
2536 \ col: 4,
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002537 \ maxwidth: 12,
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002538 \ padding: [],
2539 \ border: [],
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002540 \ filter: 'MyPopupFilter',
2541 \ })
2542 redraw
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002543 " 123456789012345678901
2544 " 1 .....................
2545 " 2 ...+--------------+..
2546 " 3 ...| |..
2547 " 4 ...| short |..
2548 " 5 ...| long line th |..
2549 " 6 ...| at will wrap |..
2550 " 7 ...| other |..
2551 " 8 ...| |..
2552 " 9 ...+--------------+..
2553 " 10 .....................
2554 let tests = []
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002555
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002556 func AddItemOutsidePopup(tests, row, col)
2557 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2558 \ screenrow: a:row, screencol: a:col,
2559 \ winid: win_getid(), winrow: a:row, wincol: a:col,
2560 \ line: a:row, column: a:col,
2561 \ }})
2562 endfunc
2563 func AddItemInPopupBorder(tests, winid, row, col)
2564 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2565 \ screenrow: a:row, screencol: a:col,
2566 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2567 \ line: 0, column: 0,
2568 \ }})
2569 endfunc
2570 func AddItemInPopupText(tests, winid, row, col, textline, textcol)
2571 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2572 \ screenrow: a:row, screencol: a:col,
2573 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2574 \ line: a:textline, column: a:textcol,
2575 \ }})
2576 endfunc
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002577
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002578 " above and below popup
2579 for c in range(1, 21)
2580 call AddItemOutsidePopup(tests, 1, c)
2581 call AddItemOutsidePopup(tests, 10, c)
2582 endfor
2583 " left and right of popup
2584 for r in range(1, 10)
2585 call AddItemOutsidePopup(tests, r, 3)
2586 call AddItemOutsidePopup(tests, r, 20)
2587 endfor
2588 " top and bottom in popup
2589 for c in range(4, 19)
2590 call AddItemInPopupBorder(tests, winid, 2, c)
2591 call AddItemInPopupBorder(tests, winid, 3, c)
2592 call AddItemInPopupBorder(tests, winid, 8, c)
2593 call AddItemInPopupBorder(tests, winid, 9, c)
2594 endfor
2595 " left and right margin in popup
2596 for r in range(2, 9)
2597 call AddItemInPopupBorder(tests, winid, r, 4)
2598 call AddItemInPopupBorder(tests, winid, r, 5)
2599 call AddItemInPopupBorder(tests, winid, r, 18)
2600 call AddItemInPopupBorder(tests, winid, r, 19)
2601 endfor
2602 " text "short"
2603 call AddItemInPopupText(tests, winid, 4, 6, 1, 1)
2604 call AddItemInPopupText(tests, winid, 4, 10, 1, 5)
2605 call AddItemInPopupText(tests, winid, 4, 11, 1, 6)
2606 call AddItemInPopupText(tests, winid, 4, 17, 1, 6)
2607 " text "long line th"
2608 call AddItemInPopupText(tests, winid, 5, 6, 2, 1)
2609 call AddItemInPopupText(tests, winid, 5, 10, 2, 5)
2610 call AddItemInPopupText(tests, winid, 5, 17, 2, 12)
2611 " text "at will wrap"
2612 call AddItemInPopupText(tests, winid, 6, 6, 2, 13)
2613 call AddItemInPopupText(tests, winid, 6, 10, 2, 17)
2614 call AddItemInPopupText(tests, winid, 6, 17, 2, 24)
2615 " text "other"
2616 call AddItemInPopupText(tests, winid, 7, 6, 3, 1)
2617 call AddItemInPopupText(tests, winid, 7, 10, 3, 5)
2618 call AddItemInPopupText(tests, winid, 7, 11, 3, 6)
2619 call AddItemInPopupText(tests, winid, 7, 17, 3, 6)
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002620
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002621 for item in tests
2622 call test_setmouse(item.clickrow, item.clickcol)
2623 call feedkeys("\<LeftMouse>", 'xt')
2624 call assert_equal(item.result, g:got_mousepos)
2625 endfor
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002626
2627 call popup_close(winid)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002628 enew!
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002629 delfunc MyPopupFilter
2630endfunc
2631
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002632func Test_popupwin_with_buffer()
2633 call writefile(['some text', 'in a buffer'], 'XsomeFile')
2634 let buf = bufadd('XsomeFile')
2635 call assert_equal(0, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002636
2637 setlocal number
2638 call setbufvar(buf, "&wrapmargin", 13)
2639
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002640 let winid = popup_create(buf, {})
2641 call assert_notequal(0, winid)
2642 let pos = popup_getpos(winid)
2643 call assert_equal(2, pos.height)
2644 call assert_equal(1, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002645
2646 " window-local option is set to default, buffer-local is not
2647 call assert_equal(0, getwinvar(winid, '&number'))
2648 call assert_equal(13, getbufvar(buf, '&wrapmargin'))
2649
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002650 call popup_close(winid)
2651 call assert_equal({}, popup_getpos(winid))
2652 call assert_equal(1, bufloaded(buf))
2653 exe 'bwipe! ' .. buf
Bram Moolenaar46451042019-08-24 15:50:46 +02002654 setlocal nonumber
Bram Moolenaar7866b872019-07-01 22:21:01 +02002655
2656 edit test_popupwin.vim
2657 let winid = popup_create(bufnr(''), {})
2658 redraw
2659 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002660 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002661endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02002662
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002663func Test_popupwin_terminal_buffer()
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002664 CheckFeature terminal
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002665 CheckUnix
Bram Moolenaare06a28f2020-05-13 23:24:12 +02002666 " Starting a terminal to run a shell in is considered flaky.
2667 let g:test_is_flaky = 1
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002668
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002669 let origwin = win_getid()
Bram Moolenaar349f6092020-10-06 20:46:49 +02002670
2671 " open help window to test that :help below fails
2672 help
2673
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002674 let termbuf = term_start(&shell, #{hidden: 1})
Bram Moolenaarf5452692020-11-28 21:56:06 +01002675 let winid = popup_create(termbuf, #{minwidth: 40, minheight: 10, border: []})
Bram Moolenaar0353f562020-12-17 22:27:38 +01002676 " Wait for shell to start
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002677 call WaitForAssert({-> assert_equal("run", job_status(term_getjob(termbuf)))})
Bram Moolenaar0353f562020-12-17 22:27:38 +01002678 " Wait for a prompt (see border char first, then space after prompt)
2679 call WaitForAssert({ -> assert_equal(' ', screenstring(screenrow(), screencol() - 1))})
Bram Moolenaarf5452692020-11-28 21:56:06 +01002680
2681 " When typing a character, the cursor is after it.
2682 call feedkeys("x", 'xt')
Bram Moolenaar0353f562020-12-17 22:27:38 +01002683 call term_wait(termbuf)
Bram Moolenaarf5452692020-11-28 21:56:06 +01002684 redraw
2685 call WaitForAssert({ -> assert_equal('x', screenstring(screenrow(), screencol() - 1))})
2686 call feedkeys("\<BS>", 'xt')
2687
Bram Moolenaar631ebc42020-02-03 22:15:26 +01002688 " Check this doesn't crash
2689 call assert_equal(winnr(), winnr('j'))
2690 call assert_equal(winnr(), winnr('k'))
2691 call assert_equal(winnr(), winnr('h'))
2692 call assert_equal(winnr(), winnr('l'))
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002693
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002694 " Cannot quit while job is running
2695 call assert_fails('call feedkeys("\<C-W>:quit\<CR>", "xt")', 'E948:')
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002696
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002697 " Cannot enter Terminal-Normal mode. (TODO: but it works...)
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002698 call feedkeys("xxx\<C-W>N", 'xt')
2699 call assert_fails('call feedkeys("gf", "xt")', 'E863:')
2700 call feedkeys("a\<C-U>", 'xt')
2701
Bram Moolenaar3f65c662020-05-27 23:15:16 +02002702 " Cannot escape from terminal window
2703 call assert_fails('tab drop xxx', 'E863:')
Bram Moolenaar349f6092020-10-06 20:46:49 +02002704 call assert_fails('help', 'E994:')
Bram Moolenaar3f65c662020-05-27 23:15:16 +02002705
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002706 " Cannot open a second one.
2707 let termbuf2 = term_start(&shell, #{hidden: 1})
2708 call assert_fails('call popup_create(termbuf2, #{})', 'E861:')
2709 call term_sendkeys(termbuf2, "exit\<CR>")
2710
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002711 " Exiting shell closes popup window
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002712 call feedkeys("exit\<CR>", 'xt')
2713 " Wait for shell to exit
Bram Moolenaarb2b218d2020-06-22 20:22:19 +02002714 call WaitForAssert({-> assert_equal("dead", job_status(term_getjob(termbuf)))})
Bram Moolenaar373c6512020-06-22 19:24:23 +02002715
Bram Moolenaar349f6092020-10-06 20:46:49 +02002716 helpclose
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002717 call feedkeys(":quit\<CR>", 'xt')
2718 call assert_equal(origwin, win_getid())
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002719endfunc
2720
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002721func Test_popupwin_close_prevwin()
2722 CheckFeature terminal
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002723 call Popupwin_close_prevwin()
2724endfunc
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002725
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002726def Popupwin_close_prevwin()
2727 assert_equal(1, winnr('$'))
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002728 split
2729 wincmd b
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002730 assert_equal(2, winnr())
Bram Moolenaare0de1712020-12-02 17:36:54 +01002731 var buf = term_start(&shell, {hidden: 1})
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002732 popup_create(buf, {})
2733 TermWait(buf, 100)
2734 popup_clear(true)
2735 assert_equal(2, winnr())
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002736
2737 quit
2738 exe 'bwipe! ' .. buf
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002739enddef
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002740
Bram Moolenaar934470e2019-09-01 23:27:05 +02002741func Test_popupwin_with_buffer_and_filter()
2742 new Xwithfilter
2743 call setline(1, range(100))
2744 let bufnr = bufnr()
2745 hide
2746
2747 func BufferFilter(win, key)
2748 if a:key == 'G'
2749 " recursive use of "G" does not cause problems.
2750 call win_execute(a:win, 'normal! G')
2751 return 1
2752 endif
2753 return 0
2754 endfunc
2755
2756 let winid = popup_create(bufnr, #{maxheight: 5, filter: 'BufferFilter'})
2757 call assert_equal(1, popup_getpos(winid).firstline)
2758 redraw
2759 call feedkeys("G", 'xt')
2760 call assert_equal(99, popup_getpos(winid).firstline)
2761
2762 call popup_close(winid)
2763 exe 'bwipe! ' .. bufnr
2764endfunc
2765
Bram Moolenaare296e312019-07-03 23:20:18 +02002766func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002767 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002768 \ maxwidth: 40,
2769 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02002770 \ })
2771 for top in range(1, 20)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002772 eval winid->popup_setoptions(#{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02002773 redraw
2774 call assert_equal(19, popup_getpos(winid).width)
2775 endfor
2776 call popup_clear()
2777endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02002778
2779func Test_popupwin_buf_close()
2780 let buf = bufadd('Xtestbuf')
2781 call bufload(buf)
2782 call setbufline(buf, 1, ['just', 'some', 'lines'])
2783 let winid = popup_create(buf, {})
2784 redraw
2785 call assert_equal(3, popup_getpos(winid).height)
2786 let bufinfo = getbufinfo(buf)[0]
2787 call assert_equal(1, bufinfo.changed)
2788 call assert_equal(0, bufinfo.hidden)
2789 call assert_equal(0, bufinfo.listed)
2790 call assert_equal(1, bufinfo.loaded)
2791 call assert_equal([], bufinfo.windows)
2792 call assert_equal([winid], bufinfo.popups)
2793
2794 call popup_close(winid)
2795 call assert_equal({}, popup_getpos(winid))
2796 let bufinfo = getbufinfo(buf)[0]
2797 call assert_equal(1, bufinfo.changed)
2798 call assert_equal(1, bufinfo.hidden)
2799 call assert_equal(0, bufinfo.listed)
2800 call assert_equal(1, bufinfo.loaded)
2801 call assert_equal([], bufinfo.windows)
2802 call assert_equal([], bufinfo.popups)
2803 exe 'bwipe! ' .. buf
2804endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02002805
2806func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002807 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02002808
2809 let lines =<< trim END
2810 call setline(1, range(1, 10))
2811 hi ScrollThumb ctermbg=blue
2812 hi ScrollBar ctermbg=red
2813 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002814 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002815 \ maxwidth: 10,
2816 \ maxheight: 3,
2817 \ pos : 'topleft',
2818 \ col : a:col,
2819 \ line : a:line,
2820 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02002821 \ })
2822 endfunc
2823 call PopupMenu(['x'], 1, 1)
2824 call PopupMenu(['123456789|'], 1, 16)
2825 call PopupMenu(['123456789|' .. ' '], 7, 1)
2826 call PopupMenu([repeat('123456789|', 100)], 7, 16)
2827 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
2828 END
2829 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002830 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02002831 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
2832
2833 " close the menu popupwin.
2834 call term_sendkeys(buf, " ")
2835 call term_sendkeys(buf, " ")
2836 call term_sendkeys(buf, " ")
2837 call term_sendkeys(buf, " ")
2838 call term_sendkeys(buf, " ")
2839
2840 " clean up
2841 call StopVimInTerminal(buf)
2842 call delete('XtestPopupMenuMaxWidth')
2843endfunc
2844
Bram Moolenaara901a372019-07-13 16:38:50 +02002845func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002846 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02002847
2848 let lines =<< trim END
2849 call setline(1, range(1, 20))
2850 hi ScrollThumb ctermbg=blue
2851 hi ScrollBar ctermbg=red
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002852 eval ['one', 'two', 'three', 'four', 'five',
2853 \ 'six', 'seven', 'eight', 'nine']
2854 \ ->popup_menu(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002855 \ minwidth: 8,
2856 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02002857 \ })
2858 END
2859 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002860 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02002861
2862 call term_sendkeys(buf, "j")
2863 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
2864
2865 call term_sendkeys(buf, "jjj")
2866 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
2867
2868 " if the cursor is the bottom line, it stays at the bottom line.
2869 call term_sendkeys(buf, repeat("j", 20))
2870 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
2871
2872 call term_sendkeys(buf, "kk")
2873 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
2874
2875 call term_sendkeys(buf, "k")
2876 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
2877
2878 " if the cursor is in the top line, it stays in the top line.
2879 call term_sendkeys(buf, repeat("k", 20))
2880 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
2881
2882 " close the menu popupwin.
2883 call term_sendkeys(buf, " ")
2884
2885 " clean up
2886 call StopVimInTerminal(buf)
2887 call delete('XtestPopupMenuScroll')
2888endfunc
2889
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002890func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002891 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002892
2893 let lines =<< trim END
2894 function! MyFilter(winid, key) abort
2895 if a:key == "0"
2896 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
2897 return 1
2898 endif
2899 if a:key == "G"
2900 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
2901 return 1
2902 endif
2903 if a:key == "j"
2904 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
2905 return 1
2906 endif
2907 if a:key == "k"
2908 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
2909 return 1
2910 endif
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002911 if a:key == ':'
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002912 call popup_close(a:winid)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002913 return 0
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002914 endif
2915 return 0
2916 endfunction
2917 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
2918 \ maxheight : 3,
2919 \ filter : 'MyFilter'
2920 \ })
2921 END
2922 call writefile(lines, 'XtestPopupMenuFilter')
2923 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
2924
2925 call term_sendkeys(buf, "j")
2926 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
2927
2928 call term_sendkeys(buf, "k")
2929 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
2930
2931 call term_sendkeys(buf, "G")
2932 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
2933
2934 call term_sendkeys(buf, "0")
2935 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
2936
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002937 " check that when the popup is closed in the filter the screen is redrawn
2938 call term_sendkeys(buf, ":")
2939 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_5', {})
2940 call term_sendkeys(buf, "\<CR>")
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002941
2942 " clean up
2943 call StopVimInTerminal(buf)
2944 call delete('XtestPopupMenuFilter')
2945endfunc
2946
2947func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002948 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002949
2950 let winid = popup_create('some text', {})
2951 call assert_equal(0, popup_getoptions(winid).cursorline)
2952 call popup_close(winid)
2953
2954 let winid = popup_create('some text', #{ cursorline: 1, })
2955 call assert_equal(1, popup_getoptions(winid).cursorline)
2956 call popup_close(winid)
2957
2958 let winid = popup_create('some text', #{ cursorline: 0, })
2959 call assert_equal(0, popup_getoptions(winid).cursorline)
2960 call popup_close(winid)
2961
2962 let winid = popup_menu('some text', {})
2963 call assert_equal(1, popup_getoptions(winid).cursorline)
2964 call popup_close(winid)
2965
2966 let winid = popup_menu('some text', #{ cursorline: 1, })
2967 call assert_equal(1, popup_getoptions(winid).cursorline)
2968 call popup_close(winid)
2969
2970 let winid = popup_menu('some text', #{ cursorline: 0, })
2971 call assert_equal(0, popup_getoptions(winid).cursorline)
2972 call popup_close(winid)
2973
2974 " ---------
2975 " Pattern 1
2976 " ---------
2977 let lines =<< trim END
2978 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
2979 END
2980 call writefile(lines, 'XtestPopupCursorLine')
2981 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2982 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
2983 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2984 call StopVimInTerminal(buf)
2985
2986 " ---------
2987 " Pattern 2
2988 " ---------
2989 let lines =<< trim END
2990 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
2991 END
2992 call writefile(lines, 'XtestPopupCursorLine')
2993 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2994 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
2995 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2996 call StopVimInTerminal(buf)
2997
2998 " ---------
2999 " Pattern 3
3000 " ---------
3001 let lines =<< trim END
3002 function! MyFilter(winid, key) abort
3003 if a:key == "j"
3004 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
3005 return 1
3006 endif
3007 if a:key == 'x'
3008 call popup_close(a:winid)
3009 return 1
3010 endif
3011 return 0
3012 endfunction
3013 call popup_menu(['111', '222', '333'], #{
3014 \ cursorline : 0,
3015 \ maxheight : 2,
3016 \ filter : 'MyFilter',
3017 \ })
3018 END
3019 call writefile(lines, 'XtestPopupCursorLine')
3020 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3021 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
3022 call term_sendkeys(buf, "j")
3023 call term_sendkeys(buf, "j")
3024 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
3025 call term_sendkeys(buf, "x")
3026 call StopVimInTerminal(buf)
3027
3028 " ---------
3029 " Pattern 4
3030 " ---------
3031 let lines =<< trim END
3032 function! MyFilter(winid, key) abort
3033 if a:key == "j"
3034 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
3035 return 1
3036 endif
3037 if a:key == 'x'
3038 call popup_close(a:winid)
3039 return 1
3040 endif
3041 return 0
3042 endfunction
3043 call popup_menu(['111', '222', '333'], #{
3044 \ cursorline : 1,
3045 \ maxheight : 2,
3046 \ filter : 'MyFilter',
3047 \ })
3048 END
3049 call writefile(lines, 'XtestPopupCursorLine')
3050 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3051 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
3052 call term_sendkeys(buf, "j")
3053 call term_sendkeys(buf, "j")
3054 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
3055 call term_sendkeys(buf, "x")
3056 call StopVimInTerminal(buf)
3057
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003058 " ---------
3059 " Cursor in second line when creating the popup
3060 " ---------
3061 let lines =<< trim END
3062 let winid = popup_create(['111', '222', '333'], #{
3063 \ cursorline : 1,
3064 \ })
3065 call win_execute(winid, "2")
3066 END
3067 call writefile(lines, 'XtestPopupCursorLine')
3068 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3069 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_7', {})
3070 call StopVimInTerminal(buf)
3071
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003072 call delete('XtestPopupCursorLine')
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01003073
3074 " ---------
3075 " Use current buffer for popupmenu
3076 " ---------
3077 let lines =<< trim END
3078 call setline(1, ['one', 'two', 'three'])
3079 let winid = popup_create(bufnr('%'), #{
3080 \ cursorline : 1,
3081 \ })
3082 call win_execute(winid, "2")
3083 END
3084 call writefile(lines, 'XtestPopupCursorLine')
3085 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3086 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_8', {})
3087 call StopVimInTerminal(buf)
3088
3089 call delete('XtestPopupCursorLine')
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003090endfunc
3091
Bram Moolenaarf914a332019-07-20 15:09:56 +02003092func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003093 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003094 CheckFeature quickfix
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003095
Bram Moolenaarf914a332019-07-20 15:09:56 +02003096 call writefile([
3097 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
3098 \ "another\tXtagfile\t/^this is another",
3099 \ "theword\tXtagfile\t/^theword"],
3100 \ 'Xtags')
3101 call writefile(range(1,20)
3102 \ + ['theword is here']
3103 \ + range(22, 27)
3104 \ + ['this is another place']
3105 \ + range(29, 40),
3106 \ "Xtagfile")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003107 call writefile(range(1,10)
3108 \ + ['searched word is here']
3109 \ + range(12, 20),
3110 \ "Xheader.h")
Bram Moolenaarf914a332019-07-20 15:09:56 +02003111 let lines =<< trim END
3112 set tags=Xtags
3113 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003114 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003115 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003116 \ 'three',
3117 \ 'four',
3118 \ 'five',
3119 \ 'six',
3120 \ 'seven',
3121 \ 'find theword somewhere',
3122 \ 'nine',
3123 \ 'this is another word',
3124 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02003125 set previewpopup=height:4,width:40
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003126 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02003127 END
3128 call writefile(lines, 'XtestPreviewPopup')
3129 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
3130
3131 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
3132 call term_sendkeys(buf, ":\<CR>")
3133 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
3134
3135 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
3136 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
3137
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003138 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003139 call term_sendkeys(buf, ":\<CR>")
3140 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
3141
3142 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
3143 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
3144
Bram Moolenaar799439a2020-02-11 21:44:17 +01003145 call term_sendkeys(buf, ":silent cd ..\<CR>:\<CR>")
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003146 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar799439a2020-02-11 21:44:17 +01003147 call term_sendkeys(buf, ":silent cd testdir\<CR>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003148
3149 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02003150 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003151 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
3152
3153 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
3154 call term_sendkeys(buf, ":\<CR>")
3155 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
3156
3157 call term_sendkeys(buf, ":pclose\<CR>")
3158 call term_sendkeys(buf, ":psearch searched\<CR>")
3159 call term_sendkeys(buf, ":\<CR>")
3160 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003161
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01003162 call term_sendkeys(buf, "\<C-W>p")
3163 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_9', {})
3164
3165 call term_sendkeys(buf, ":call win_execute(popup_findpreview(), 'call popup_clear()')\<CR>")
3166 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_10', {})
3167
Bram Moolenaarf914a332019-07-20 15:09:56 +02003168 call StopVimInTerminal(buf)
3169 call delete('Xtags')
3170 call delete('Xtagfile')
3171 call delete('XtestPreviewPopup')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003172 call delete('Xheader.h')
Bram Moolenaarf914a332019-07-20 15:09:56 +02003173endfunc
3174
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003175func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003176 let lines =<< trim END
3177 set completeopt+=preview,popup
3178 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02003179 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003180
3181 func CompleteFuncDict(findstart, base)
3182 if a:findstart
3183 if col('.') > 10
3184 return col('.') - 10
3185 endif
3186 return 0
3187 endif
3188
3189 return {
3190 \ 'words': [
3191 \ {
3192 \ 'word': 'aword',
3193 \ 'abbr': 'wrd',
3194 \ 'menu': 'extra text',
3195 \ 'info': 'words are cool',
3196 \ 'kind': 'W',
3197 \ 'user_data': 'test'
3198 \ },
3199 \ {
3200 \ 'word': 'anotherword',
3201 \ 'abbr': 'anotwrd',
3202 \ 'menu': 'extra text',
3203 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
3204 \ 'kind': 'W',
3205 \ 'user_data': 'notest'
3206 \ },
3207 \ {
3208 \ 'word': 'noinfo',
3209 \ 'abbr': 'noawrd',
3210 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02003211 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003212 \ 'kind': 'W',
3213 \ 'user_data': 'notest'
3214 \ },
3215 \ {
3216 \ 'word': 'thatword',
3217 \ 'abbr': 'thatwrd',
3218 \ 'menu': 'extra text',
3219 \ 'info': 'that word is cool',
3220 \ 'kind': 'W',
3221 \ 'user_data': 'notest'
3222 \ },
3223 \ ]
3224 \ }
3225 endfunc
3226 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003227 func ChangeColor()
3228 let id = popup_findinfo()
Bram Moolenaard356fc62020-12-09 18:13:44 +01003229 if buflisted(winbufnr(id))
3230 call setline(1, 'buffer is listed')
3231 endif
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003232 eval id->popup_setoptions(#{highlight: 'InfoPopup'})
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003233 endfunc
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003234
3235 func InfoHidden()
3236 set completepopup=height:4,border:off,align:menu
3237 set completeopt-=popup completeopt+=popuphidden
3238 au CompleteChanged * call HandleChange()
3239 endfunc
3240
3241 let s:counter = 0
3242 func HandleChange()
3243 let s:counter += 1
3244 let selected = complete_info(['selected']).selected
3245 if selected <= 0
3246 " First time: do nothing, info remains hidden
3247 return
3248 endif
3249 if selected == 1
3250 " Second time: show info right away
3251 let id = popup_findinfo()
3252 if id
3253 call popup_settext(id, 'immediate info ' .. s:counter)
3254 call popup_show(id)
3255 endif
3256 else
3257 " Third time: show info after a short delay
3258 call timer_start(100, 'ShowInfo')
3259 endif
3260 endfunc
3261
3262 func ShowInfo(...)
3263 let id = popup_findinfo()
3264 if id
3265 call popup_settext(id, 'async info ' .. s:counter)
3266 call popup_show(id)
3267 endif
3268 endfunc
Bram Moolenaar2dfae042020-11-15 14:09:37 +01003269
3270 " Check that no autocommands are triggered for the info popup
3271 au WinEnter * if win_gettype() == 'popup' | call setline(2, 'WinEnter') | endif
3272 au WinLeave * if win_gettype() == 'popup' | call setline(2, 'WinLeave') | endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003273 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003274 return lines
3275endfunc
3276
3277func Test_popupmenu_info_border()
3278 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003279 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003280
3281 let lines = Get_popupmenu_lines()
3282 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003283 call writefile(lines, 'XtestInfoPopup')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003284
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003285 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003286 call TermWait(buf, 25)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003287
3288 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3289 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
3290
3291 call term_sendkeys(buf, "\<C-N>")
3292 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
3293
3294 call term_sendkeys(buf, "\<C-N>")
3295 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
3296
3297 call term_sendkeys(buf, "\<C-N>\<C-N>")
3298 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
3299
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02003300 " info on the left with scrollbar
3301 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
3302 call term_sendkeys(buf, "\<C-N>\<C-N>")
3303 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
3304
Bram Moolenaar202c3f72019-11-21 12:12:35 +01003305 " Test that the popupmenu's scrollbar and infopopup do not overlap
3306 call term_sendkeys(buf, "\<Esc>")
3307 call term_sendkeys(buf, ":set pumheight=3\<CR>")
3308 call term_sendkeys(buf, "cc\<C-X>\<C-U>")
3309 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_6', {})
3310
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003311 " Hide the info popup, cycle through buffers, make sure it didn't get
Bram Moolenaarca7c0782020-01-14 20:42:48 +01003312 " deleted.
3313 call term_sendkeys(buf, "\<Esc>")
3314 call term_sendkeys(buf, ":set hidden\<CR>")
3315 call term_sendkeys(buf, ":bn\<CR>")
3316 call term_sendkeys(buf, ":bn\<CR>")
3317 call term_sendkeys(buf, "otest text test text\<C-X>\<C-U>")
3318 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_7', {})
3319
Bram Moolenaar447bfba2020-07-18 16:07:16 +02003320 " Test that when the option is changed the popup changes.
3321 call term_sendkeys(buf, "\<Esc>")
3322 call term_sendkeys(buf, ":set completepopup=border:off\<CR>")
3323 call term_sendkeys(buf, "a\<C-X>\<C-U>")
3324 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_8', {})
3325
Bram Moolenaar6d585f42020-07-26 22:20:54 +02003326 call term_sendkeys(buf, " \<Esc>")
3327 call term_sendkeys(buf, ":set completepopup+=width:10\<CR>")
3328 call term_sendkeys(buf, "a\<C-X>\<C-U>")
3329 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_9', {})
3330
Bram Moolenaarca7c0782020-01-14 20:42:48 +01003331 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003332 call StopVimInTerminal(buf)
3333 call delete('XtestInfoPopup')
3334endfunc
3335
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003336func Test_popupmenu_info_noborder()
3337 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003338 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003339
3340 let lines = Get_popupmenu_lines()
3341 call add(lines, 'set completepopup=height:4,border:off')
3342 call writefile(lines, 'XtestInfoPopupNb')
3343
3344 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003345 call TermWait(buf, 25)
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003346
3347 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3348 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
3349
3350 call StopVimInTerminal(buf)
3351 call delete('XtestInfoPopupNb')
3352endfunc
3353
Bram Moolenaar258cef52019-08-21 17:29:29 +02003354func Test_popupmenu_info_align_menu()
3355 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003356 CheckFeature quickfix
Bram Moolenaar258cef52019-08-21 17:29:29 +02003357
3358 let lines = Get_popupmenu_lines()
3359 call add(lines, 'set completepopup=height:4,border:off,align:menu')
3360 call writefile(lines, 'XtestInfoPopupNb')
3361
3362 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003363 call TermWait(buf, 25)
Bram Moolenaar258cef52019-08-21 17:29:29 +02003364
3365 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3366 call term_sendkeys(buf, "\<C-N>")
3367 call term_sendkeys(buf, "\<C-N>")
3368 call term_sendkeys(buf, "\<C-N>")
3369 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
3370
3371 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
3372 call term_sendkeys(buf, "\<C-N>")
3373 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
3374
3375 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003376 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02003377 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
3378 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
3379 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
3380
3381 call StopVimInTerminal(buf)
3382 call delete('XtestInfoPopupNb')
3383endfunc
3384
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003385func Test_popupmenu_info_hidden()
3386 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003387 CheckFeature quickfix
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003388
3389 let lines = Get_popupmenu_lines()
3390 call add(lines, 'call InfoHidden()')
3391 call writefile(lines, 'XtestInfoPopupHidden')
3392
3393 let buf = RunVimInTerminal('-S XtestInfoPopupHidden', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003394 call TermWait(buf, 25)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003395
3396 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3397 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_1', {})
3398
3399 call term_sendkeys(buf, "\<C-N>")
3400 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_2', {})
3401
3402 call term_sendkeys(buf, "\<C-N>")
3403 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_3', {})
3404
3405 call term_sendkeys(buf, "\<Esc>")
3406 call StopVimInTerminal(buf)
3407 call delete('XtestInfoPopupHidden')
3408endfunc
3409
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003410func Test_popupmenu_info_too_wide()
3411 CheckScreendump
3412 CheckFeature quickfix
3413
3414 let lines =<< trim END
3415 call setline(1, range(10))
3416
3417 set completeopt+=preview,popup
3418 set completepopup=align:menu
3419 set omnifunc=OmniFunc
3420 hi InfoPopup ctermbg=lightgrey
3421
3422 func OmniFunc(findstart, base)
3423 if a:findstart
3424 return 0
3425 endif
3426
3427 let menuText = 'some long text to make sure the menu takes up all of the width of the window'
3428 return #{
3429 \ words: [
3430 \ #{
3431 \ word: 'scrap',
3432 \ menu: menuText,
3433 \ info: "other words are\ncooler than this and some more text\nto make wrap",
3434 \ },
3435 \ #{
3436 \ word: 'scappier',
3437 \ menu: menuText,
3438 \ info: 'words are cool',
3439 \ },
3440 \ #{
3441 \ word: 'scrappier2',
3442 \ menu: menuText,
3443 \ info: 'words are cool',
3444 \ },
3445 \ ]
3446 \ }
3447 endfunc
3448 END
3449
3450 call writefile(lines, 'XtestInfoPopupWide')
3451 let buf = RunVimInTerminal('-S XtestInfoPopupWide', #{rows: 8})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003452 call TermWait(buf, 25)
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003453
3454 call term_sendkeys(buf, "Ascr\<C-X>\<C-O>")
3455 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_wide_1', {})
3456
3457 call term_sendkeys(buf, "\<Esc>")
3458 call StopVimInTerminal(buf)
3459 call delete('XtestInfoPopupWide')
3460endfunc
3461
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003462func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02003463 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003464 call popup_clear()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003465 let winid = 'nothing wrong'->popup_notification({})
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003466 call assert_equal(bufnr, winbufnr(winid))
3467 call popup_clear()
3468endfunc
3469
Bram Moolenaar1824f452019-10-02 23:06:46 +02003470func Test_popupwin_getoptions_tablocal()
3471 topleft split
3472 let win1 = popup_create('nothing', #{maxheight: 8})
3473 let win2 = popup_create('something', #{maxheight: 10})
3474 let win3 = popup_create('something', #{maxheight: 15})
3475 call assert_equal(8, popup_getoptions(win1).maxheight)
3476 call assert_equal(10, popup_getoptions(win2).maxheight)
3477 call assert_equal(15, popup_getoptions(win3).maxheight)
3478 call popup_clear()
3479 quit
3480endfunc
3481
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02003482func Test_popupwin_cancel()
3483 let win1 = popup_create('one', #{line: 5, filter: {... -> 0}})
3484 let win2 = popup_create('two', #{line: 10, filter: {... -> 0}})
3485 let win3 = popup_create('three', #{line: 15, filter: {... -> 0}})
3486 call assert_equal(5, popup_getpos(win1).line)
3487 call assert_equal(10, popup_getpos(win2).line)
3488 call assert_equal(15, popup_getpos(win3).line)
3489 " TODO: this also works without patch 8.1.2110
3490 call feedkeys("\<C-C>", 'xt')
3491 call assert_equal(5, popup_getpos(win1).line)
3492 call assert_equal(10, popup_getpos(win2).line)
3493 call assert_equal({}, popup_getpos(win3))
3494 call feedkeys("\<C-C>", 'xt')
3495 call assert_equal(5, popup_getpos(win1).line)
3496 call assert_equal({}, popup_getpos(win2))
3497 call assert_equal({}, popup_getpos(win3))
3498 call feedkeys("\<C-C>", 'xt')
3499 call assert_equal({}, popup_getpos(win1))
3500 call assert_equal({}, popup_getpos(win2))
3501 call assert_equal({}, popup_getpos(win3))
3502endfunc
3503
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003504func Test_popupwin_filter_redraw()
3505 " Create two popups with a filter that closes the popup when typing "0".
3506 " Both popups should close, even though the redraw also calls
3507 " popup_reset_handled()
3508
3509 func CloseFilter(winid, key)
3510 if a:key == '0'
3511 call popup_close(a:winid)
3512 redraw
3513 endif
3514 return 0 " pass the key
3515 endfunc
3516
3517 let id1 = popup_create('first one', #{
3518 \ line: 1,
3519 \ col: 1,
3520 \ filter: 'CloseFilter',
3521 \ })
3522 let id2 = popup_create('second one', #{
3523 \ line: 9,
3524 \ col: 1,
3525 \ filter: 'CloseFilter',
3526 \ })
3527 call assert_equal(1, popup_getpos(id1).line)
3528 call assert_equal(9, popup_getpos(id2).line)
3529
3530 call feedkeys('0', 'xt')
3531 call assert_equal({}, popup_getpos(id1))
3532 call assert_equal({}, popup_getpos(id2))
3533
3534 call popup_clear()
3535 delfunc CloseFilter
3536endfunc
3537
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003538func Test_popupwin_double_width()
3539 CheckScreendump
3540
3541 let lines =<< trim END
3542 call setline(1, 'x你好世界你好世你好世界你好')
3543 call setline(2, '你好世界你好世你好世界你好')
3544 call setline(3, 'x你好世界你好世你好世界你好')
3545 call popup_create('你好,世界 - 你好,世界xxxxx', #{line: 1, col: 3, maxwidth: 14})
3546 END
3547 call writefile(lines, 'XtestPopupWide')
3548
3549 let buf = RunVimInTerminal('-S XtestPopupWide', #{rows: 10})
3550 call VerifyScreenDump(buf, 'Test_popupwin_doublewidth_1', {})
3551
3552 call StopVimInTerminal(buf)
3553 call delete('XtestPopupWide')
3554endfunc
3555
3556func Test_popupwin_sign()
3557 CheckScreendump
3558
3559 let lines =<< trim END
3560 call setline(1, range(10))
3561 call sign_define('Current', {
3562 \ 'text': '>>',
3563 \ 'texthl': 'WarningMsg',
3564 \ 'linehl': 'Error',
3565 \ })
3566 call sign_define('Other', {
3567 \ 'text': '#!',
3568 \ 'texthl': 'Error',
3569 \ 'linehl': 'Search',
3570 \ })
3571 let winid = popup_create(['hello', 'bright', 'world'], {
3572 \ 'minwidth': 20,
3573 \ })
3574 call setwinvar(winid, "&signcolumn", "yes")
3575 let winbufnr = winbufnr(winid)
3576
3577 " add sign to current buffer, shows
3578 call sign_place(1, 'Selected', 'Current', bufnr('%'), {'lnum': 1})
3579 " add sign to current buffer, does not show
3580 call sign_place(2, 'PopUpSelected', 'Other', bufnr('%'), {'lnum': 2})
3581
3582 " add sign to popup buffer, shows
3583 call sign_place(3, 'PopUpSelected', 'Other', winbufnr, {'lnum': 1})
3584 " add sign to popup buffer, does not show
3585 call sign_place(4, 'Selected', 'Current', winbufnr, {'lnum': 2})
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02003586
3587 func SetOptions()
3588 call setwinvar(g:winid, '&number', 1)
3589 call setwinvar(g:winid, '&foldcolumn', 2)
3590 call popup_settext(g:winid, 'a longer line to check the width')
3591 endfunc
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003592 END
3593 call writefile(lines, 'XtestPopupSign')
3594
3595 let buf = RunVimInTerminal('-S XtestPopupSign', #{rows: 10})
3596 call VerifyScreenDump(buf, 'Test_popupwin_sign_1', {})
3597
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02003598 " set more options to check the width is adjusted
3599 call term_sendkeys(buf, ":call SetOptions()\<CR>")
3600 call VerifyScreenDump(buf, 'Test_popupwin_sign_2', {})
3601
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003602 call StopVimInTerminal(buf)
3603 call delete('XtestPopupSign')
3604endfunc
3605
Bram Moolenaar99ebf222019-12-10 23:44:48 +01003606func Test_popupwin_bufnr()
3607 let popwin = popup_create(['blah'], #{})
3608 let popbuf = winbufnr(popwin)
3609 split asdfasdf
3610 let newbuf = bufnr()
3611 call assert_true(newbuf > popbuf, 'New buffer number is higher')
3612 call assert_equal(newbuf, bufnr('$'))
3613 call popup_clear()
3614 let popwin = popup_create(['blah'], #{})
3615 " reuses previous buffer number
3616 call assert_equal(popbuf, winbufnr(popwin))
3617 call assert_equal(newbuf, bufnr('$'))
3618
3619 call popup_clear()
3620 bwipe!
3621endfunc
3622
Bram Moolenaarec084d32020-02-28 22:44:47 +01003623func Test_popupwin_filter_input_multibyte()
3624 func MyPopupFilter(winid, c)
3625 let g:bytes = range(a:c->strlen())->map({i -> char2nr(a:c[i])})
3626 return 0
3627 endfunc
3628 let winid = popup_create('', #{mapping: 0, filter: 'MyPopupFilter'})
3629
3630 " UTF-8: E3 80 80, including K_SPECIAL(0x80)
3631 call feedkeys("\u3000", 'xt')
3632 call assert_equal([0xe3, 0x80, 0x80], g:bytes)
3633
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01003634 " UTF-8: E3 80 9B, including CSI(0x9B)
3635 call feedkeys("\u301b", 'xt')
3636 call assert_equal([0xe3, 0x80, 0x9b], g:bytes)
Bram Moolenaarec084d32020-02-28 22:44:47 +01003637
Bram Moolenaar20298ce2020-06-19 21:46:52 +02003638 if has('unix')
3639 " with modifyOtherKeys <M-S-a> does not include a modifier sequence
Bram Moolenaarb326edf2020-06-20 15:03:38 +02003640 if has('gui_running')
3641 call feedkeys("\x9b\xfc\x08A", 'Lx!')
3642 else
3643 call feedkeys("\<Esc>[27;4;65~", 'Lx!')
3644 endif
Bram Moolenaar20298ce2020-06-19 21:46:52 +02003645 call assert_equal([0xc3, 0x81], g:bytes)
3646 endif
3647
Bram Moolenaarec084d32020-02-28 22:44:47 +01003648 call popup_clear()
3649 delfunc MyPopupFilter
3650 unlet g:bytes
3651endfunc
3652
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003653func Test_popupwin_filter_close_ctrl_c()
3654 CheckScreendump
3655
3656 let lines =<< trim END
3657 vsplit
3658 set laststatus=2
3659 set statusline=%!Statusline()
3660
3661 function Statusline() abort
3662 return '%<%f %h%m%r%=%-14.(%l,%c%V%) %P'
3663 endfunction
3664
3665 call popup_create('test test test test...', {'filter': {-> 0}})
3666 END
3667 call writefile(lines, 'XtestPopupCtrlC')
3668
3669 let buf = RunVimInTerminal('-S XtestPopupCtrlC', #{rows: 10})
3670
3671 call term_sendkeys(buf, "\<C-C>")
3672 call VerifyScreenDump(buf, 'Test_popupwin_ctrl_c', {})
3673
3674 call StopVimInTerminal(buf)
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003675 call delete('XtestPopupCtrlC')
3676endfunc
3677
3678func Test_popupwin_filter_close_wrong_name()
3679 CheckScreendump
3680
3681 let lines =<< trim END
3682 call popup_create('one two three...', {'filter': 'NoSuchFunc'})
3683 END
3684 call writefile(lines, 'XtestPopupWrongName')
3685
3686 let buf = RunVimInTerminal('-S XtestPopupWrongName', #{rows: 10})
3687
3688 call term_sendkeys(buf, "j")
3689 call VerifyScreenDump(buf, 'Test_popupwin_wrong_name', {})
3690
3691 call StopVimInTerminal(buf)
3692 call delete('XtestPopupWrongName')
3693endfunc
3694
3695func Test_popupwin_filter_close_three_errors()
3696 CheckScreendump
3697
3698 let lines =<< trim END
3699 set cmdheight=2
3700 call popup_create('one two three...', {'filter': 'filter'})
3701 END
3702 call writefile(lines, 'XtestPopupThreeErrors')
3703
3704 let buf = RunVimInTerminal('-S XtestPopupThreeErrors', #{rows: 10})
3705
3706 call term_sendkeys(buf, "jj")
3707 call VerifyScreenDump(buf, 'Test_popupwin_three_errors_1', {})
3708 call term_sendkeys(buf, "j")
3709 call VerifyScreenDump(buf, 'Test_popupwin_three_errors_2', {})
3710
3711 call StopVimInTerminal(buf)
3712 call delete('XtestPopupThreeErrors')
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003713endfunc
3714
Bram Moolenaar927495b2020-11-06 17:58:35 +01003715func Test_popupwin_latin1_encoding()
3716 CheckScreendump
3717 CheckUnix
3718
3719 " When 'encoding' is a single-byte encoding a terminal window will mess up
3720 " the display. Check that showing a popup on top of that doesn't crash.
3721 let lines =<< trim END
3722 set encoding=latin1
3723 terminal cat Xmultibyte
3724 call popup_create(['one', 'two', 'three', 'four'], #{line: 1, col: 10})
Bram Moolenaar4c5bdb92020-12-17 17:45:59 +01003725 redraw
Bram Moolenaarb125b532020-12-17 21:56:09 +01003726 " wait for "cat" to finish
3727 while execute('ls!') !~ 'finished'
3728 sleep 10m
3729 endwhile
Bram Moolenaar4c5bdb92020-12-17 17:45:59 +01003730 echo "Done"
Bram Moolenaar927495b2020-11-06 17:58:35 +01003731 END
3732 call writefile(lines, 'XtestPopupLatin')
3733 call writefile([repeat("\u3042 ", 120)], 'Xmultibyte')
3734
3735 let buf = RunVimInTerminal('-S XtestPopupLatin', #{rows: 10})
Bram Moolenaar4c5bdb92020-12-17 17:45:59 +01003736 call WaitForAssert({-> assert_match('Done', term_getline(buf, 10))})
Bram Moolenaar927495b2020-11-06 17:58:35 +01003737
3738 call term_sendkeys(buf, ":q\<CR>")
3739 call StopVimInTerminal(buf)
3740 call delete('XtestPopupLatin')
3741 call delete('Xmultibyte')
3742endfunc
3743
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003744func Test_popupwin_atcursor_far_right()
3745 new
3746
3747 " this was getting stuck
3748 set signcolumn=yes
3749 call setline(1, repeat('=', &columns))
3750 normal! ggg$
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003751 let winid = popup_atcursor(repeat('x', 500), #{moved: 'any', border: []})
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003752
Bram Moolenaar6d585f42020-07-26 22:20:54 +02003753 " 'signcolumn' was getting reset
3754 call setwinvar(winid, '&signcolumn', 'yes')
3755 call popup_setoptions(winid, {'zindex': 1000})
3756 call assert_equal('yes', getwinvar(winid, '&signcolumn'))
3757
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003758 call popup_close(winid)
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003759 bwipe!
3760 set signcolumn&
3761endfunc
3762
Bram Moolenaar0f1563f2020-03-20 21:15:51 +01003763func Test_popupwin_splitmove()
3764 vsplit
3765 let win2 = win_getid()
3766 let popup_winid = popup_dialog('hello', {})
3767 call assert_fails('call win_splitmove(popup_winid, win2)', 'E957:')
3768 call assert_fails('call win_splitmove(win2, popup_winid)', 'E957:')
3769
3770 call popup_clear()
3771 bwipe
3772endfunc
3773
Bram Moolenaarcbcd9cb2020-11-07 16:58:59 +01003774func Test_popupwin_exiting_terminal()
3775 CheckFeature terminal
3776
3777 " Tests that when creating a popup right after closing a terminal window does
3778 " not make the popup the current window.
3779 let winid = win_getid()
3780 try
3781 augroup Test_popupwin_exiting_terminal
3782 autocmd!
3783 autocmd WinEnter * :call popup_create('test', {})
3784 augroup END
3785 let bnr = term_start(&shell, #{term_finish: 'close'})
3786 call term_sendkeys(bnr, "exit\r\n")
3787 call WaitForAssert({-> assert_equal(winid, win_getid())})
3788 finally
3789 call popup_clear(1)
3790 augroup Test_popupwin_exiting_terminal
3791 autocmd!
3792 augroup END
3793 endtry
3794endfunc
Bram Moolenaar0f1563f2020-03-20 21:15:51 +01003795
Bram Moolenaar331bafd2019-07-20 17:46:05 +02003796" vim: shiftwidth=2 sts=2