blob: 94ea423c1b519b5848e257ef457569ade1426179 [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>")
68 call term_wait(buf)
69 call term_sendkeys(buf, "0")
70 call term_wait(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]})
89 call popup_create('paddings', #{line: 6, col: 23, padding: [1, 3, 2, 4]})
90 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 Moolenaar4c6d9042019-07-16 22:04:02 +0200183 let winid = popup_create('hello both', #{line: 3, col: 8, border: [], padding: []})
184 call assert_equal(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200185 \ line: 3,
186 \ core_line: 5,
187 \ col: 8,
188 \ core_col: 10,
189 \ width: 14,
190 \ core_width: 10,
191 \ height: 5,
192 \ scrollbar: 0,
193 \ core_height: 1,
194 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100195 \ lastline: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200196 \ visible: 1}, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200197
198 call popup_clear()
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200199endfunc
200
Bram Moolenaarb4230122019-05-30 18:40:53 +0200201func Test_popup_with_syntax_win_execute()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200202 CheckScreendump
203
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200204 let lines =<< trim END
205 call setline(1, range(1, 100))
206 hi PopupColor ctermbg=lightblue
207 let winid = popup_create([
208 \ '#include <stdio.h>',
209 \ 'int main(void)',
210 \ '{',
211 \ ' printf(123);',
212 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200213 \], #{line: 3, col: 25, highlight: 'PopupColor'})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200214 call win_execute(winid, 'set syntax=cpp')
215 END
216 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200217 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200218 call VerifyScreenDump(buf, 'Test_popupwin_10', {})
219
220 " clean up
221 call StopVimInTerminal(buf)
222 call delete('XtestPopup')
223endfunc
224
225func Test_popup_with_syntax_setbufvar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200226 CheckScreendump
227
Bram Moolenaar402502d2019-05-30 22:07:36 +0200228 let lines =<< trim END
229 call setline(1, range(1, 100))
230 hi PopupColor ctermbg=lightgrey
231 let winid = popup_create([
232 \ '#include <stdio.h>',
233 \ 'int main(void)',
234 \ '{',
Bram Moolenaare089c3f2019-07-09 20:25:25 +0200235 \ "\tprintf(567);",
Bram Moolenaar402502d2019-05-30 22:07:36 +0200236 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200237 \], #{line: 3, col: 21, highlight: 'PopupColor'})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200238 call setbufvar(winbufnr(winid), '&syntax', 'cpp')
239 END
240 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200241 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200242 call VerifyScreenDump(buf, 'Test_popupwin_11', {})
243
244 " clean up
245 call StopVimInTerminal(buf)
246 call delete('XtestPopup')
247endfunc
248
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200249func Test_popup_with_matches()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200250 CheckScreendump
251
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200252 let lines =<< trim END
253 call setline(1, ['111 222 333', '444 555 666'])
254 let winid = popup_create([
255 \ '111 222 333',
256 \ '444 555 666',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200257 \], #{line: 3, col: 10, border: []})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200258 set hlsearch
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100259 hi VeryBlue ctermfg=blue guifg=blue
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200260 /666
261 call matchadd('ErrorMsg', '111')
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100262 call matchadd('VeryBlue', '444')
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200263 call win_execute(winid, "call matchadd('ErrorMsg', '111')")
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100264 call win_execute(winid, "call matchadd('VeryBlue', '555')")
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200265 END
266 call writefile(lines, 'XtestPopupMatches')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200267 let buf = RunVimInTerminal('-S XtestPopupMatches', #{rows: 10})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200268 call VerifyScreenDump(buf, 'Test_popupwin_matches', {})
269
270 " clean up
271 call StopVimInTerminal(buf)
272 call delete('XtestPopupMatches')
273endfunc
274
Bram Moolenaar399d8982019-06-02 15:34:29 +0200275func Test_popup_all_corners()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200276 CheckScreendump
277
Bram Moolenaar399d8982019-06-02 15:34:29 +0200278 let lines =<< trim END
279 call setline(1, repeat([repeat('-', 60)], 15))
280 set so=0
281 normal 2G3|r#
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200282 let winid1 = popup_create(['first', 'second'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200283 \ line: 'cursor+1',
284 \ col: 'cursor',
285 \ pos: 'topleft',
286 \ border: [],
287 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200288 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200289 normal 24|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: 'topright',
294 \ border: [],
295 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200296 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200297 normal 9G27|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: 'botleft',
302 \ border: [],
303 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200304 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200305 normal 48|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: 'botright',
310 \ border: [],
311 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200312 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200313 normal 1G51|r*
314 let winid1 = popup_create(['one', 'two'], #{
315 \ line: 'cursor-1',
316 \ col: 'cursor',
317 \ pos: 'botleft',
318 \ border: [],
319 \ padding: [],
320 \ })
Bram Moolenaar399d8982019-06-02 15:34:29 +0200321 END
322 call writefile(lines, 'XtestPopupCorners')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200323 let buf = RunVimInTerminal('-S XtestPopupCorners', #{rows: 12})
Bram Moolenaar399d8982019-06-02 15:34:29 +0200324 call VerifyScreenDump(buf, 'Test_popupwin_corners', {})
325
326 " clean up
327 call StopVimInTerminal(buf)
328 call delete('XtestPopupCorners')
329endfunc
330
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100331func Test_popup_nospace()
332 CheckScreendump
333
334 let lines =<< trim END
335 call setline(1, repeat([repeat('-', 60)], 15))
336 set so=0
337
338 " cursor in a line in top half, using "botleft" with popup that
339 " does fit
340 normal 5G2|r@
341 let winid1 = popup_create(['one', 'two'], #{
342 \ line: 'cursor-1',
343 \ col: 'cursor',
344 \ pos: 'botleft',
345 \ border: [],
346 \ })
347 " cursor in a line in top half, using "botleft" with popup that
348 " doesn't fit: gets truncated
349 normal 5G9|r#
350 let winid1 = popup_create(['one', 'two', 'tee'], #{
351 \ line: 'cursor-1',
352 \ col: 'cursor',
353 \ pos: 'botleft',
354 \ posinvert: 0,
355 \ border: [],
356 \ })
357 " cursor in a line in top half, using "botleft" with popup that
358 " doesn't fit and 'posinvert' set: flips to below.
359 normal 5G16|r%
360 let winid1 = popup_create(['one', 'two', 'tee'], #{
361 \ line: 'cursor-1',
362 \ col: 'cursor',
363 \ pos: 'botleft',
364 \ border: [],
365 \ })
366 " cursor in a line in bottom half, using "botleft" with popup that
367 " doesn't fit: does not flip.
368 normal 8G23|r*
369 let winid1 = popup_create(['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff'], #{
370 \ line: 'cursor-1',
371 \ col: 'cursor',
372 \ pos: 'botleft',
373 \ border: [],
374 \ })
375
376 " cursor in a line in bottom half, using "topleft" with popup that
377 " does fit
378 normal 8G30|r@
379 let winid1 = popup_create(['one', 'two'], #{
380 \ line: 'cursor+1',
381 \ col: 'cursor',
382 \ pos: 'topleft',
383 \ border: [],
384 \ })
385 " cursor in a line in top half, using "topleft" with popup that
386 " doesn't fit: truncated
387 normal 8G37|r#
388 let winid1 = popup_create(['one', 'two', 'tee'], #{
389 \ line: 'cursor+1',
390 \ col: 'cursor',
391 \ pos: 'topleft',
392 \ posinvert: 0,
393 \ border: [],
394 \ })
395 " cursor in a line in top half, using "topleft" with popup that
Bram Moolenaar5c6b6182019-11-10 17:51:38 +0100396 " doesn't fit and "posinvert" set: flips to above.
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100397 normal 8G44|r%
Bram Moolenaar5c6b6182019-11-10 17:51:38 +0100398 let winid1 = popup_create(['one', 'two', 'tee', 'fou', 'fiv'], #{
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100399 \ line: 'cursor+1',
400 \ col: 'cursor',
401 \ pos: 'topleft',
402 \ border: [],
403 \ })
404 " cursor in a line in top half, using "topleft" with popup that
405 " doesn't fit: does not flip.
406 normal 5G51|r*
407 let winid1 = popup_create(['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff'], #{
408 \ line: 'cursor+1',
409 \ col: 'cursor',
410 \ pos: 'topleft',
411 \ border: [],
412 \ })
413 END
414 call writefile(lines, 'XtestPopupNospace')
415 let buf = RunVimInTerminal('-S XtestPopupNospace', #{rows: 12})
416 call VerifyScreenDump(buf, 'Test_popupwin_nospace', {})
417
418 " clean up
419 call StopVimInTerminal(buf)
420 call delete('XtestPopupNospace')
421endfunc
422
Bram Moolenaar8d241042019-06-12 23:40:01 +0200423func Test_popup_firstline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200424 CheckScreendump
425
Bram Moolenaar8d241042019-06-12 23:40:01 +0200426 let lines =<< trim END
427 call setline(1, range(1, 20))
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200428 let winid = popup_create(['1111', '222222', '33333', '44', '5', '666666', '77777', '888', '9999999999999999'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200429 \ maxheight: 4,
430 \ firstline: 3,
Bram Moolenaar8d241042019-06-12 23:40:01 +0200431 \ })
432 END
433 call writefile(lines, 'XtestPopupFirstline')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200434 let buf = RunVimInTerminal('-S XtestPopupFirstline', #{rows: 10})
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200435 call VerifyScreenDump(buf, 'Test_popupwin_firstline_1', {})
436
437 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: -1})\<CR>")
438 call term_sendkeys(buf, ":\<CR>")
439 call VerifyScreenDump(buf, 'Test_popupwin_firstline_2', {})
Bram Moolenaar8d241042019-06-12 23:40:01 +0200440
441 " clean up
442 call StopVimInTerminal(buf)
443 call delete('XtestPopupFirstline')
Bram Moolenaarae943152019-06-16 22:54:14 +0200444
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200445 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200446 \ maxheight: 2,
447 \ firstline: 3,
Bram Moolenaarae943152019-06-16 22:54:14 +0200448 \ })
449 call assert_equal(3, popup_getoptions(winid).firstline)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200450 call popup_setoptions(winid, #{firstline: 1})
Bram Moolenaarae943152019-06-16 22:54:14 +0200451 call assert_equal(1, popup_getoptions(winid).firstline)
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200452 eval winid->popup_close()
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200453
454 let winid = popup_create(['xxx']->repeat(50), #{
455 \ maxheight: 3,
456 \ firstline: 11,
457 \ })
458 redraw
459 call assert_equal(11, popup_getoptions(winid).firstline)
460 call assert_equal(11, popup_getpos(winid).firstline)
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +0200461 " check line() works with popup window
462 call assert_equal(11, line('.', winid))
463 call assert_equal(50, line('$', winid))
464 call assert_equal(0, line('$', 123456))
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200465
466 " Normal command changes what is displayed but not "firstline"
467 call win_execute(winid, "normal! \<c-y>")
468 call assert_equal(11, popup_getoptions(winid).firstline)
469 call assert_equal(10, popup_getpos(winid).firstline)
470
471 " Making some property change applies "firstline" again
472 call popup_setoptions(winid, #{line: 4})
473 call assert_equal(11, popup_getoptions(winid).firstline)
474 call assert_equal(11, popup_getpos(winid).firstline)
475
476 " Remove "firstline" property and scroll
477 call popup_setoptions(winid, #{firstline: 0})
478 call win_execute(winid, "normal! \<c-y>")
479 call assert_equal(0, popup_getoptions(winid).firstline)
480 call assert_equal(10, popup_getpos(winid).firstline)
481
482 " Making some property change has no side effect
483 call popup_setoptions(winid, #{line: 3})
484 call assert_equal(0, popup_getoptions(winid).firstline)
485 call assert_equal(10, popup_getpos(winid).firstline)
Bram Moolenaarae943152019-06-16 22:54:14 +0200486
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100487 " CTRL-D scrolls down half a page
488 let winid = popup_create(['xxx']->repeat(50), #{
489 \ maxheight: 8,
490 \ })
491 redraw
492 call assert_equal(1, popup_getpos(winid).firstline)
493 call win_execute(winid, "normal! \<C-D>")
494 call assert_equal(5, popup_getpos(winid).firstline)
495 call win_execute(winid, "normal! \<C-D>")
496 call assert_equal(9, popup_getpos(winid).firstline)
497 call win_execute(winid, "normal! \<C-U>")
498 call assert_equal(5, popup_getpos(winid).firstline)
499
500 call win_execute(winid, "normal! \<C-F>")
501 call assert_equal(11, popup_getpos(winid).firstline)
502 call win_execute(winid, "normal! \<C-B>")
503 call assert_equal(5, popup_getpos(winid).firstline)
504
Bram Moolenaarae943152019-06-16 22:54:14 +0200505 call popup_close(winid)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200506endfunc
507
Bram Moolenaara112f2d2019-09-01 17:38:09 +0200508func Test_popup_noscrolloff()
509 set scrolloff=5
510 let winid = popup_create(['xxx']->repeat(50), #{
511 \ maxheight: 5,
512 \ firstline: 11,
513 \ })
514 redraw
515 call assert_equal(11, popup_getoptions(winid).firstline)
516 call assert_equal(11, popup_getpos(winid).firstline)
517
518 call popup_setoptions(winid, #{firstline: 0})
519 call win_execute(winid, "normal! \<c-y>")
520 call assert_equal(0, popup_getoptions(winid).firstline)
521 call assert_equal(10, popup_getpos(winid).firstline)
522
523 call popup_close(winid)
524endfunc
525
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200526func Test_popup_drag()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200527 CheckScreendump
528
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200529 " create a popup that covers the command line
530 let lines =<< trim END
531 call setline(1, range(1, 20))
Bram Moolenaar356375f2019-08-24 14:46:29 +0200532 split
533 vsplit
534 $wincmd w
535 vsplit
536 1wincmd w
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200537 let winid = popup_create(['1111', '222222', '33333'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200538 \ drag: 1,
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200539 \ resize: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200540 \ border: [],
541 \ line: &lines - 4,
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200542 \ })
543 func Dragit()
544 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
545 endfunc
546 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
Bram Moolenaar356375f2019-08-24 14:46:29 +0200547 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2 - 20)<CR>
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200548 func Resize()
549 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
550 endfunc
Bram Moolenaar356375f2019-08-24 14:46:29 +0200551 map <silent> <F5> :call test_setmouse(6, 21)<CR>
552 map <silent> <F6> :call test_setmouse(7, 25)<CR>
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200553 END
554 call writefile(lines, 'XtestPopupDrag')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200555 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200556 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
557
558 call term_sendkeys(buf, ":call Dragit()\<CR>")
559 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
560
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200561 call term_sendkeys(buf, ":call Resize()\<CR>")
562 call VerifyScreenDump(buf, 'Test_popupwin_drag_03', {})
563
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200564 " clean up
565 call StopVimInTerminal(buf)
566 call delete('XtestPopupDrag')
567endfunc
568
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200569func Test_popup_close_with_mouse()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200570 CheckScreendump
571
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200572 let lines =<< trim END
573 call setline(1, range(1, 20))
574 " With border, can click on X
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200575 let winid = popup_create('foobar', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200576 \ close: 'button',
577 \ border: [],
578 \ line: 1,
579 \ col: 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200580 \ })
581 func CloseMsg(id, result)
582 echomsg 'Popup closed with ' .. a:result
583 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200584 let winid = popup_create('notification', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200585 \ close: 'click',
586 \ line: 3,
587 \ col: 15,
588 \ callback: 'CloseMsg',
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200589 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200590 let winid = popup_create('no border here', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200591 \ close: 'button',
592 \ line: 5,
593 \ col: 3,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200594 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200595 let winid = popup_create('only padding', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200596 \ close: 'button',
597 \ padding: [],
598 \ line: 5,
599 \ col: 23,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200600 \ })
601 func CloseWithX()
602 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
603 endfunc
604 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
605 func CloseWithClick()
606 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
607 endfunc
608 map <silent> <F4> :call test_setmouse(3, 17)<CR>
Bram Moolenaarf6396232019-08-24 19:36:00 +0200609 func CreateWithMenuFilter()
610 let winid = popup_create('barfoo', #{
611 \ close: 'button',
612 \ filter: 'popup_filter_menu',
613 \ border: [],
614 \ line: 1,
615 \ col: 40,
616 \ })
617 endfunc
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200618 END
619 call writefile(lines, 'XtestPopupClose')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200620 let buf = RunVimInTerminal('-S XtestPopupClose', #{rows: 10})
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200621 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
622
623 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
624 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
625
626 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
627 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
628
Bram Moolenaarf6396232019-08-24 19:36:00 +0200629 call term_sendkeys(buf, ":call CreateWithMenuFilter()\<CR>")
630 call VerifyScreenDump(buf, 'Test_popupwin_close_04', {})
631
632 " We have to send the actual mouse code, feedkeys() would be caught the
633 " filter.
634 call term_sendkeys(buf, "\<Esc>[<0;47;1M")
635 call VerifyScreenDump(buf, 'Test_popupwin_close_05', {})
636
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200637 " clean up
638 call StopVimInTerminal(buf)
639 call delete('XtestPopupClose')
640endfunction
641
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200642func Test_popup_menu_wrap()
643 CheckScreendump
644
645 let lines =<< trim END
646 call setline(1, range(1, 20))
647 call popup_create([
648 \ 'one',
649 \ 'asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfas',
650 \ 'three',
651 \ 'four',
652 \ ], #{
653 \ pos: "botleft",
654 \ border: [],
655 \ padding: [0,1,0,1],
656 \ maxheight: 3,
657 \ cursorline: 1,
658 \ filter: 'popup_filter_menu',
659 \ })
660 END
661 call writefile(lines, 'XtestPopupWrap')
662 let buf = RunVimInTerminal('-S XtestPopupWrap', #{rows: 10})
663 call VerifyScreenDump(buf, 'Test_popupwin_wrap_1', {})
664
665 call term_sendkeys(buf, "jj")
666 call VerifyScreenDump(buf, 'Test_popupwin_wrap_2', {})
667
668 " clean up
669 call term_sendkeys(buf, "\<Esc>")
670 call StopVimInTerminal(buf)
671 call delete('XtestPopupWrap')
672endfunction
673
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200674func Test_popup_with_mask()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200675 CheckScreendump
676
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200677 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200678 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200679 hi PopupColor ctermbg=lightgrey
680 let winid = popup_create([
681 \ 'some text',
682 \ 'another line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200683 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200684 \ line: 1,
685 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100686 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200687 \ wrap: 0,
688 \ fixed: 1,
689 \ zindex: 90,
690 \ padding: [],
691 \ highlight: 'PopupColor',
692 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200693 call popup_create([
694 \ 'xxxxxxxxx',
695 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200696 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200697 \ line: 3,
698 \ col: 18,
699 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200700 let winidb = popup_create([
701 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200702 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200703 \ line: 7,
704 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100705 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200706 \ wrap: 0,
707 \ fixed: 1,
708 \ close: 'button',
709 \ zindex: 90,
710 \ padding: [],
711 \ border: [],
712 \ 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 +0200713 END
714 call writefile(lines, 'XtestPopupMask')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200715 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200716 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
717
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200718 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
719 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200720 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200721 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
722
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200723 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
724 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200725 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200726 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
727
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200728 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
729 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200730 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200731 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
732
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200733 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
734 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200735 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200736 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
737
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200738 " clean up
739 call StopVimInTerminal(buf)
740 call delete('XtestPopupMask')
741endfunc
742
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200743func Test_popup_select()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200744 CheckScreendump
745 CheckFeature clipboard_working
746
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200747 " create a popup with some text to be selected
748 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200749 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200750 call setline(1, range(1, 20))
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200751 let winid = popup_create(['the word', 'some more', 'several words here', 'invisible', '5', '6', '7'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200752 \ drag: 1,
753 \ border: [],
754 \ line: 3,
755 \ col: 10,
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200756 \ maxheight: 3,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200757 \ })
758 func Select1()
759 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
760 endfunc
761 map <silent> <F3> :call test_setmouse(4, 15)<CR>
762 map <silent> <F4> :call test_setmouse(6, 23)<CR>
763 END
764 call writefile(lines, 'XtestPopupSelect')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200765 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200766 call term_sendkeys(buf, ":call Select1()\<CR>")
767 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
768
769 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
770 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200771 " clean the command line, sometimes it still shows a command
772 call term_sendkeys(buf, ":\<esc>")
773
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200774 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
775
776 " clean up
777 call StopVimInTerminal(buf)
778 call delete('XtestPopupSelect')
779endfunc
780
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200781func Test_popup_in_tab()
782 " default popup is local to tab, not visible when in other tab
783 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200784 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200785 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200786 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200787 tabnew
788 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200789 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200790 quit
791 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200792
793 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200794 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200795 " buffer is gone now
796 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200797
798 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200799 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200800 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200801 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200802 tabnew
803 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200804 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200805 quit
806 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200807 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200808
809 " create popup in other tab
810 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200811 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200812 call assert_equal(0, popup_getpos(winid).visible)
813 call assert_equal(1, popup_getoptions(winid).tabpage)
814 quit
815 call assert_equal(1, popup_getpos(winid).visible)
816 call assert_equal(0, popup_getoptions(winid).tabpage)
817 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200818endfunc
819
820func Test_popup_valid_arguments()
821 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200822 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200823 let pos = popup_getpos(winid)
824 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200825 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200826
827 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200828 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200829 let pos = popup_getpos(winid)
830 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200831 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200832
833 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200834 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200835 let pos = popup_getpos(winid)
836 let around = (&columns - pos.width) / 2
837 call assert_inrange(around - 1, around + 1, pos.col)
838 let around = (&lines - pos.height) / 2
839 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200840 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200841endfunc
842
843func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200844 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200845 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200846 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200847 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200848
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200849 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200850 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200851 call assert_fails('call popup_create("text", #{col: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200852 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200853 call assert_fails('call popup_create("text", #{col: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200854 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200855 call assert_fails('call popup_create("text", #{col: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200856 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200857
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200858 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200859 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200860 call assert_fails('call popup_create("text", #{line: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200861 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200862 call assert_fails('call popup_create("text", #{line: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200863 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200864 call assert_fails('call popup_create("text", #{line: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200865 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200866
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200867 call assert_fails('call popup_create("text", #{pos: "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200868 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200869 call assert_fails('call popup_create("text", #{padding: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200870 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200871 call assert_fails('call popup_create("text", #{border: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200872 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200873 call assert_fails('call popup_create("text", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200874 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +0200875 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
876 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200877 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200878 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200879
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200880 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200881 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200882 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200883 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200884 call assert_fails('call popup_create([#{text: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200885 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200886 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
887 call popup_clear()
888 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200889 call assert_fails('call popup_create("text", #{mapping: []})', 'E745:')
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200890 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200891endfunc
892
Bram Moolenaareea16992019-05-31 17:34:48 +0200893func Test_win_execute_closing_curwin()
894 split
895 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200896 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200897 call popup_clear()
Bram Moolenaar49540192019-12-11 19:34:54 +0100898
899 let winid = popup_create('some text', {})
900 call assert_fails('call win_execute(winid, printf("normal! :\<C-u>call popup_close(%d)\<CR>", winid))', 'E994')
901 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200902endfunc
903
904func Test_win_execute_not_allowed()
905 let winid = popup_create('some text', {})
906 call assert_fails('call win_execute(winid, "split")', 'E994:')
907 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
908 call assert_fails('call win_execute(winid, "close")', 'E994:')
909 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200910 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200911 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
912 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
913 call assert_fails('call win_execute(winid, "next")', 'E994:')
914 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
915 call assert_fails('call win_execute(winid, "buf")', 'E994:')
916 call assert_fails('call win_execute(winid, "edit")', 'E994:')
917 call assert_fails('call win_execute(winid, "enew")', 'E994:')
918 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
919 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
920 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
921 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200922 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200923endfunc
924
Bram Moolenaar402502d2019-05-30 22:07:36 +0200925func Test_popup_with_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200926 CheckScreendump
927
Bram Moolenaar402502d2019-05-30 22:07:36 +0200928 let lines =<< trim END
929 call setline(1, range(1, 100))
930 let winid = popup_create(
931 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200932 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200933 END
934 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200935 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200936 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
937
938 " clean up
939 call StopVimInTerminal(buf)
940 call delete('XtestPopup')
941endfunc
942
943func Test_popup_without_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200944 CheckScreendump
945
Bram Moolenaar402502d2019-05-30 22:07:36 +0200946 let lines =<< trim END
947 call setline(1, range(1, 100))
948 let winid = popup_create(
949 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200950 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200951 END
952 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200953 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200954 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
955
956 " clean up
957 call StopVimInTerminal(buf)
958 call delete('XtestPopup')
959endfunc
960
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200961func Test_popup_with_showbreak()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200962 CheckScreendump
963
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200964 let lines =<< trim END
965 set showbreak=>>\
966 call setline(1, range(1, 20))
967 let winid = popup_dialog(
Bram Moolenaar8ae54372019-09-15 18:11:16 +0200968 \ 'a long line here that wraps',
969 \ #{filter: 'popup_filter_yesno',
970 \ maxwidth: 12})
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200971 END
972 call writefile(lines, 'XtestPopupShowbreak')
973 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
974 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
975
976 " clean up
977 call term_sendkeys(buf, "y")
978 call StopVimInTerminal(buf)
979 call delete('XtestPopupShowbreak')
980endfunc
981
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200982func Test_popup_time()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200983 CheckFeature timers
984
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200985 topleft vnew
986 call setline(1, 'hello')
987
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200988 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200989 \ line: 1,
990 \ col: 1,
991 \ minwidth: 20,
992 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200993 \})
994 redraw
995 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
996 call assert_equal('world', line)
997
Bram Moolenaarb4f06282019-07-12 21:07:54 +0200998 call assert_equal(winid, popup_locate(1, 1))
999 call assert_equal(winid, popup_locate(1, 20))
1000 call assert_equal(0, popup_locate(1, 21))
1001 call assert_equal(0, popup_locate(2, 1))
1002
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001003 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +02001004 redraw
Bram Moolenaar196b4662019-09-06 21:34:30 +02001005 let line = join(map(range(1, 5), '1->screenstring(v:val)'), '')
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001006 call assert_equal('hello', line)
1007
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001008 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001009 \ line: &lines,
1010 \ col: 10,
1011 \ minwidth: 20,
1012 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001013 \})
1014 redraw
1015 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1016 call assert_match('.*on the command line.*', line)
1017
1018 sleep 700m
1019 redraw
1020 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1021 call assert_notmatch('.*on the command line.*', line)
1022
1023 bwipe!
1024endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001025
1026func Test_popup_hide()
1027 topleft vnew
1028 call setline(1, 'hello')
1029
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001030 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001031 \ line: 1,
1032 \ col: 1,
1033 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001034 \})
1035 redraw
1036 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1037 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001038 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001039 " buffer is still listed and active
1040 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001041
1042 call popup_hide(winid)
1043 redraw
1044 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1045 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001046 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001047 " buffer is still listed but hidden
1048 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001049
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001050 eval winid->popup_show()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001051 redraw
1052 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1053 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001054 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001055
1056
1057 call popup_close(winid)
1058 redraw
1059 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1060 call assert_equal('hello', line)
1061
1062 " error is given for existing non-popup window
1063 call assert_fails('call popup_hide(win_getid())', 'E993:')
1064
1065 " no error non-existing window
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001066 eval 1234234->popup_hide()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001067 call popup_show(41234234)
1068
1069 bwipe!
1070endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001071
1072func Test_popup_move()
1073 topleft vnew
1074 call setline(1, 'hello')
1075
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001076 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001077 \ line: 1,
1078 \ col: 1,
1079 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001080 \})
1081 redraw
1082 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1083 call assert_equal('world ', line)
1084
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001085 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001086 redraw
1087 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1088 call assert_equal('hello ', line)
1089 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
1090 call assert_equal('~world', line)
1091
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001092 eval winid->popup_move(#{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001093 redraw
1094 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1095 call assert_equal('hworld', line)
1096
1097 call popup_close(winid)
1098
1099 bwipe!
1100endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +02001101
Bram Moolenaar402502d2019-05-30 22:07:36 +02001102func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001103 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001104 \ line: 2,
1105 \ col: 3,
1106 \ minwidth: 10,
1107 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +02001108 \})
1109 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001110 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001111 call assert_equal(2, res.line)
1112 call assert_equal(3, res.col)
1113 call assert_equal(10, res.width)
1114 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001115 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001116
1117 call popup_close(winid)
1118endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001119
1120func Test_popup_width_longest()
1121 let tests = [
1122 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
1123 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
1124 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
1125 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
1126 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
1127 \ ]
1128
1129 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001130 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001131 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001132 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001133 call assert_equal(test[1], position.width)
1134 call popup_close(winid)
1135 endfor
1136endfunc
1137
1138func Test_popup_wraps()
1139 let tests = [
1140 \ ['nowrap', 6, 1],
1141 \ ['a line that wraps once', 12, 2],
1142 \ ['a line that wraps two times', 12, 3],
1143 \ ]
1144 for test in tests
1145 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001146 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001147 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001148 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001149 call assert_equal(test[1], position.width)
1150 call assert_equal(test[2], position.height)
1151
1152 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001153 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001154 endfor
1155endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001156
1157func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001158 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001159 \ line: 2,
1160 \ col: 3,
1161 \ minwidth: 10,
1162 \ minheight: 11,
1163 \ maxwidth: 20,
1164 \ maxheight: 21,
1165 \ zindex: 100,
1166 \ time: 5000,
1167 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001168 \})
1169 redraw
1170 let res = popup_getoptions(winid)
1171 call assert_equal(2, res.line)
1172 call assert_equal(3, res.col)
1173 call assert_equal(10, res.minwidth)
1174 call assert_equal(11, res.minheight)
1175 call assert_equal(20, res.maxwidth)
1176 call assert_equal(21, res.maxheight)
1177 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001178 call assert_equal(1, res.fixed)
Bram Moolenaarb8350ab2019-08-04 17:59:49 +02001179 call assert_equal(1, res.mapping)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001180 if has('timers')
1181 call assert_equal(5000, res.time)
1182 endif
1183 call popup_close(winid)
1184
1185 let winid = popup_create('hello', {})
1186 redraw
1187 let res = popup_getoptions(winid)
1188 call assert_equal(0, res.line)
1189 call assert_equal(0, res.col)
1190 call assert_equal(0, res.minwidth)
1191 call assert_equal(0, res.minheight)
1192 call assert_equal(0, res.maxwidth)
1193 call assert_equal(0, res.maxheight)
1194 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001195 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001196 if has('timers')
1197 call assert_equal(0, res.time)
1198 endif
1199 call popup_close(winid)
1200 call assert_equal({}, popup_getoptions(winid))
1201endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001202
1203func Test_popup_option_values()
1204 new
1205 " window-local
1206 setlocal number
1207 setlocal nowrap
1208 " buffer-local
1209 setlocal omnifunc=Something
1210 " global/buffer-local
1211 setlocal path=/there
1212 " global/window-local
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001213 setlocal statusline=2
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001214
1215 let winid = popup_create('hello', {})
1216 call assert_equal(0, getwinvar(winid, '&number'))
1217 call assert_equal(1, getwinvar(winid, '&wrap'))
1218 call assert_equal('', getwinvar(winid, '&omnifunc'))
1219 call assert_equal(&g:path, getwinvar(winid, '&path'))
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001220 call assert_equal(&g:statusline, getwinvar(winid, '&statusline'))
1221
1222 " 'scrolloff' is reset to zero
1223 call assert_equal(5, &scrolloff)
1224 call assert_equal(0, getwinvar(winid, '&scrolloff'))
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001225
1226 call popup_close(winid)
1227 bwipe
1228endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001229
1230func Test_popup_atcursor()
1231 topleft vnew
1232 call setline(1, [
1233 \ 'xxxxxxxxxxxxxxxxx',
1234 \ 'xxxxxxxxxxxxxxxxx',
1235 \ 'xxxxxxxxxxxxxxxxx',
1236 \])
1237
1238 call cursor(2, 2)
1239 redraw
1240 let winid = popup_atcursor('vim', {})
1241 redraw
1242 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1243 call assert_equal('xvimxxxxxxxxxxxxx', line)
1244 call popup_close(winid)
1245
1246 call cursor(3, 4)
1247 redraw
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001248 let winid = 'vim'->popup_atcursor({})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001249 redraw
1250 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
1251 call assert_equal('xxxvimxxxxxxxxxxx', line)
1252 call popup_close(winid)
1253
1254 call cursor(1, 1)
1255 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001256 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001257 \ line: 'cursor+2',
1258 \ col: 'cursor+1',
1259 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001260 redraw
1261 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1262 call assert_equal('xvimxxxxxxxxxxxxx', line)
1263 call popup_close(winid)
1264
1265 call cursor(3, 3)
1266 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001267 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001268 \ line: 'cursor-2',
1269 \ col: 'cursor-1',
1270 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001271 redraw
1272 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1273 call assert_equal('xvimxxxxxxxxxxxxx', line)
1274 call popup_close(winid)
1275
Bram Moolenaar402502d2019-05-30 22:07:36 +02001276 " just enough room above
1277 call cursor(3, 3)
1278 redraw
1279 let winid = popup_atcursor(['vim', 'is great'], {})
1280 redraw
1281 let pos = popup_getpos(winid)
1282 call assert_equal(1, pos.line)
1283 call popup_close(winid)
1284
1285 " not enough room above, popup goes below the cursor
1286 call cursor(3, 3)
1287 redraw
1288 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1289 redraw
1290 let pos = popup_getpos(winid)
1291 call assert_equal(4, pos.line)
1292 call popup_close(winid)
1293
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001294 " cursor in first line, popup in line 2
1295 call cursor(1, 1)
1296 redraw
1297 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1298 redraw
1299 let pos = popup_getpos(winid)
1300 call assert_equal(2, pos.line)
1301 call popup_close(winid)
1302
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001303 bwipe!
1304endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001305
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001306func Test_popup_atcursor_pos()
1307 CheckScreendump
1308
1309 let lines =<< trim END
1310 call setline(1, repeat([repeat('-', 60)], 15))
1311 set so=0
1312
1313 normal 9G3|r#
1314 let winid1 = popup_atcursor(['first', 'second'], #{
1315 \ moved: [0, 0, 0],
1316 \ })
1317 normal 9G21|r&
1318 let winid1 = popup_atcursor(['FIrsT', 'SEcoND'], #{
1319 \ pos: 'botright',
1320 \ moved: [0, 0, 0],
1321 \ })
1322 normal 3G27|r%
1323 let winid1 = popup_atcursor(['fiRSt', 'seCOnd'], #{
1324 \ pos: 'topleft',
1325 \ moved: [0, 0, 0],
1326 \ })
1327 normal 3G45|r@
1328 let winid1 = popup_atcursor(['First', 'SeconD'], #{
1329 \ pos: 'topright',
1330 \ moved: [0, 0, 0],
1331 \ })
1332 END
1333 call writefile(lines, 'XtestPopupAtcursorPos')
1334 let buf = RunVimInTerminal('-S XtestPopupAtcursorPos', #{rows: 12})
1335 call VerifyScreenDump(buf, 'Test_popupwin_atcursor_pos', {})
1336
1337 " clean up
1338 call StopVimInTerminal(buf)
1339 call delete('XtestPopupAtcursorPos')
1340endfunc
1341
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001342func Test_popup_beval()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001343 CheckScreendump
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001344 CheckFeature balloon_eval_term
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001345
1346 let lines =<< trim END
1347 call setline(1, range(1, 20))
1348 call setline(5, 'here is some text to hover over')
1349 set balloonevalterm
1350 set balloonexpr=BalloonExpr()
1351 set balloondelay=100
1352 func BalloonExpr()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001353 let s:winid = [v:beval_text]->popup_beval({})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001354 return ''
1355 endfunc
1356 func Hover()
1357 call test_setmouse(5, 15)
1358 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1359 sleep 100m
1360 endfunc
1361 func MoveOntoPopup()
1362 call test_setmouse(4, 17)
1363 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1364 endfunc
1365 func MoveAway()
1366 call test_setmouse(5, 13)
1367 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1368 endfunc
1369 END
1370 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001371 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001372 call term_wait(buf, 100)
1373 call term_sendkeys(buf, 'j')
1374 call term_sendkeys(buf, ":call Hover()\<CR>")
1375 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1376
1377 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1378 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1379
1380 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1381 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1382
1383 " clean up
1384 call StopVimInTerminal(buf)
1385 call delete('XtestPopupBeval')
1386endfunc
1387
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001388func Test_popup_filter()
1389 new
1390 call setline(1, 'some text')
1391
1392 func MyPopupFilter(winid, c)
1393 if a:c == 'e'
1394 let g:eaten = 'e'
1395 return 1
1396 endif
1397 if a:c == '0'
1398 let g:ignored = '0'
1399 return 0
1400 endif
1401 if a:c == 'x'
1402 call popup_close(a:winid)
1403 return 1
1404 endif
1405 return 0
1406 endfunc
1407
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001408 let winid = 'something'->popup_create(#{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001409 redraw
1410
1411 " e is consumed by the filter
1412 call feedkeys('e', 'xt')
1413 call assert_equal('e', g:eaten)
1414
1415 " 0 is ignored by the filter
1416 normal $
1417 call assert_equal(9, getcurpos()[2])
1418 call feedkeys('0', 'xt')
1419 call assert_equal('0', g:ignored)
1420 call assert_equal(1, getcurpos()[2])
1421
1422 " x closes the popup
1423 call feedkeys('x', 'xt')
1424 call assert_equal('e', g:eaten)
1425 call assert_equal(-1, winbufnr(winid))
1426
1427 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001428 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001429endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001430
Bram Moolenaara42d9452019-06-15 21:46:30 +02001431func ShowDialog(key, result)
1432 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001433 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001434 \ filter: 'popup_filter_yesno',
1435 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001436 \ })
1437 redraw
1438 call feedkeys(a:key, "xt")
1439 call assert_equal(winid, s:cb_winid)
1440 call assert_equal(a:result, s:cb_res)
1441endfunc
1442
1443func Test_popup_dialog()
1444 func QuitCallback(id, res)
1445 let s:cb_winid = a:id
1446 let s:cb_res = a:res
1447 endfunc
1448
1449 let winid = ShowDialog("y", 1)
1450 let winid = ShowDialog("Y", 1)
1451 let winid = ShowDialog("n", 0)
1452 let winid = ShowDialog("N", 0)
1453 let winid = ShowDialog("x", 0)
1454 let winid = ShowDialog("X", 0)
1455 let winid = ShowDialog("\<Esc>", 0)
1456 let winid = ShowDialog("\<C-C>", -1)
1457
1458 delfunc QuitCallback
1459endfunc
1460
Bram Moolenaara730e552019-06-16 19:05:31 +02001461func ShowMenu(key, result)
1462 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001463 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001464 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001465 \ })
1466 redraw
1467 call feedkeys(a:key, "xt")
1468 call assert_equal(winid, s:cb_winid)
1469 call assert_equal(a:result, s:cb_res)
1470endfunc
1471
1472func Test_popup_menu()
1473 func QuitCallback(id, res)
1474 let s:cb_winid = a:id
1475 let s:cb_res = a:res
1476 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001477 " mapping won't be used in popup
1478 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001479
1480 let winid = ShowMenu(" ", 1)
1481 let winid = ShowMenu("j \<CR>", 2)
1482 let winid = ShowMenu("JjK \<CR>", 2)
1483 let winid = ShowMenu("jjjjjj ", 3)
1484 let winid = ShowMenu("kkk ", 1)
1485 let winid = ShowMenu("x", -1)
1486 let winid = ShowMenu("X", -1)
1487 let winid = ShowMenu("\<Esc>", -1)
1488 let winid = ShowMenu("\<C-C>", -1)
1489
1490 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001491 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001492endfunc
1493
1494func Test_popup_menu_screenshot()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001495 CheckScreendump
Bram Moolenaara730e552019-06-16 19:05:31 +02001496
1497 let lines =<< trim END
1498 call setline(1, range(1, 20))
1499 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001500 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001501 func MenuDone(id, res)
1502 echomsg "selected " .. a:res
1503 endfunc
1504 END
1505 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001506 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001507 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1508
1509 call term_sendkeys(buf, "jj")
1510 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1511
1512 call term_sendkeys(buf, " ")
1513 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1514
1515 " clean up
1516 call StopVimInTerminal(buf)
1517 call delete('XtestPopupMenu')
1518endfunc
1519
Bram Moolenaarf914a332019-07-20 15:09:56 +02001520func Test_popup_menu_narrow()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001521 CheckScreendump
Bram Moolenaarf914a332019-07-20 15:09:56 +02001522
1523 let lines =<< trim END
1524 call setline(1, range(1, 20))
1525 hi PopupSelected ctermbg=green
1526 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1527 func MenuDone(id, res)
1528 echomsg "selected " .. a:res
1529 endfunc
1530 END
1531 call writefile(lines, 'XtestPopupNarrowMenu')
1532 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1533 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1534
1535 " clean up
1536 call term_sendkeys(buf, "x")
1537 call StopVimInTerminal(buf)
1538 call delete('XtestPopupNarrowMenu')
1539endfunc
1540
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001541func Test_popup_title()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001542 CheckScreendump
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001543
1544 " Create a popup without title or border, a line of padding will be added to
1545 " put the title on.
1546 let lines =<< trim END
1547 call setline(1, range(1, 20))
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001548 let winid = popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001549 END
1550 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001551 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001552 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1553
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001554 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxwidth: 20, title: 'a very long title that is not going to fit'})\<CR>")
1555 call term_sendkeys(buf, ":\<CR>")
1556 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_1', {})
1557
1558 call term_sendkeys(buf, ":call popup_setoptions(winid, #{border: []})\<CR>")
1559 call term_sendkeys(buf, ":\<CR>")
1560 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_2', {})
1561
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001562 " clean up
1563 call StopVimInTerminal(buf)
1564 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001565
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001566 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001567 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001568 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001569 call assert_equal('Another Title', popup_getoptions(winid).title)
1570
1571 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001572endfunc
1573
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001574func Test_popup_close_callback()
1575 func PopupDone(id, result)
1576 let g:result = a:result
1577 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001578 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001579 redraw
1580 call popup_close(winid, 'done')
1581 call assert_equal('done', g:result)
1582endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001583
1584func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001585 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001586 redraw
1587 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001588 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001589 call assert_equal(5, pos.height)
1590
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001591 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001592 redraw
1593 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001594 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001595 call assert_equal(3, pos.height)
1596endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001597
1598func Test_popup_never_behind()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001599 CheckScreendump
1600
Bram Moolenaar988c4332019-06-02 14:12:11 +02001601 " +-----------------------------+
1602 " | | |
1603 " | | |
1604 " | | |
1605 " | line1 |
1606 " |------------line2------------|
1607 " | line3 |
1608 " | line4 |
1609 " | |
1610 " | |
1611 " +-----------------------------+
1612 let lines =<< trim END
Bram Moolenaar988c4332019-06-02 14:12:11 +02001613 split
1614 vsplit
1615 let info_window1 = getwininfo()[0]
1616 let line = info_window1['height']
1617 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001618 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001619 \ line : line,
1620 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001621 \ })
1622 END
1623 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001624 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001625 call term_sendkeys(buf, "\<C-W>w")
1626 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1627
1628 " clean up
1629 call StopVimInTerminal(buf)
1630 call delete('XtestPopupBehind')
1631endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001632
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001633func s:VerifyPosition(p, msg, line, col, width, height)
1634 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1635 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1636 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1637 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001638endfunc
1639
1640func Test_popup_position_adjust()
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001641 " Anything placed past the last cell on the right of the screen is moved to
1642 " the left.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001643 "
1644 " When wrapping is disabled, we also shift to the left to display on the
1645 " screen, unless fixed is set.
1646
1647 " Entries for cases which don't vary based on wrapping.
1648 " Format is per tests described below
1649 let both_wrap_tests = [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001650 \ ['a', 5, &columns, 5, &columns, 1, 1],
1651 \ ['b', 5, &columns + 1, 5, &columns, 1, 1],
1652 \ ['c', 5, &columns - 1, 5, &columns - 1, 1, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001653 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001654 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1]]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001655
1656 " these test groups are dicts with:
1657 " - comment: something to identify the group of tests by
1658 " - options: dict of options to merge with the row/col in tests
1659 " - tests: list of cases. Each one is a list with elements:
1660 " - text
1661 " - row
1662 " - col
1663 " - expected row
1664 " - expected col
1665 " - expected width
1666 " - expected height
1667 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001668 \ #{
1669 \ comment: 'left-aligned with wrapping',
1670 \ options: #{
1671 \ wrap: 1,
1672 \ pos: 'botleft',
1673 \ },
1674 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001675 \ ['aa', 5, &columns, 4, &columns, 1, 2],
1676 \ ['bb', 5, &columns + 1, 4, &columns, 1, 2],
1677 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1678 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1679 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1680 \
1681 \ ['aaa', 5, &columns, 3, &columns, 1, 3],
1682 \ ['bbb', 5, &columns + 1, 3, &columns, 1, 3],
1683 \ ['ccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
1684 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1685 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1686 \
1687 \ ['aaaa', 5, &columns, 2, &columns, 1, 4],
1688 \ ['bbbb', 5, &columns + 1, 2, &columns, 1, 4],
1689 \ ['cccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001690 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
1691 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001692 \ ['eeee', 5, &columns - 4, 5, &columns - 4, 4, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001693 \ ],
1694 \ },
1695 \ #{
1696 \ comment: 'left aligned without wrapping',
1697 \ options: #{
1698 \ wrap: 0,
1699 \ pos: 'botleft',
1700 \ },
1701 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001702 \ ['aa', 5, &columns, 5, &columns - 1, 2, 1],
1703 \ ['bb', 5, &columns + 1, 5, &columns - 1, 2, 1],
1704 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1705 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1706 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1707 \
1708 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
1709 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1710 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1711 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1712 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1713 \
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001714 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
1715 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
1716 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
1717 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
1718 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1719 \ ],
1720 \ },
1721 \ #{
1722 \ comment: 'left aligned with fixed position',
1723 \ options: #{
1724 \ wrap: 0,
1725 \ fixed: 1,
1726 \ pos: 'botleft',
1727 \ },
1728 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001729 \ ['aa', 5, &columns, 5, &columns, 1, 1],
1730 \ ['bb', 5, &columns + 1, 5, &columns, 1, 1],
1731 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1732 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1733 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1734 \
1735 \ ['aaa', 5, &columns, 5, &columns, 1, 1],
1736 \ ['bbb', 5, &columns + 1, 5, &columns, 1, 1],
1737 \ ['ccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1738 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1739 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1740 \
1741 \ ['aaaa', 5, &columns, 5, &columns, 1, 1],
1742 \ ['bbbb', 5, &columns + 1, 5, &columns, 1, 1],
1743 \ ['cccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001744 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1745 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1746 \ ],
1747 \ },
1748 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001749
1750 for test_group in tests
1751 for test in test_group.tests
1752 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001753 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001754 \ line: line,
1755 \ col: col,
1756 \ }
1757 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001758
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001759 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001760
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001761 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001762 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
1763 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001764 endfor
1765 endfor
1766
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001767 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001768 %bwipe!
1769endfunc
1770
Bram Moolenaar3397f742019-06-02 18:40:06 +02001771func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001772 " width of screen
1773 let X = join(map(range(&columns), {->'X'}), '')
1774
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001775 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1776 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001777
1778 redraw
1779 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1780 call assert_equal(X, line)
1781
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001782 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001783 redraw
1784
1785 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001786 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
1787 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001788
1789 redraw
1790 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1791 call assert_equal(X, line)
1792
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001793 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001794 redraw
1795
1796 " Extend so > window width
1797 let X .= 'x'
1798
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001799 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1800 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001801
1802 redraw
1803 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1804 call assert_equal(X[ : -2 ], line)
1805
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001806 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001807 redraw
1808
1809 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001810 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
1811 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001812
1813 redraw
1814 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1815 call assert_equal(X[ : -2 ], line)
1816
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001817 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001818 redraw
1819
1820 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001821 let p = popup_create(X,
1822 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
1823 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001824
1825 redraw
1826 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1827 let e_line = ' ' . X[ 1 : -2 ]
1828 call assert_equal(e_line, line)
1829
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001830 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001831 redraw
1832
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001833 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001834 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001835endfunc
1836
1837func Test_popup_moved()
1838 new
1839 call test_override('char_avail', 1)
1840 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1841
1842 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001843 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001844 redraw
1845 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001846 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001847 " trigger the check for last_cursormoved by going into insert mode
1848 call feedkeys("li\<Esc>", 'xt')
1849 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001850 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001851
1852 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001853 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001854 redraw
1855 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001856 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001857 call feedkeys("hi\<Esc>", 'xt')
1858 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001859 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001860
1861 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001862 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001863 redraw
1864 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001865 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001866 call feedkeys("li\<Esc>", 'xt')
1867 call assert_equal(1, popup_getpos(winid).visible)
1868 call feedkeys("ei\<Esc>", 'xt')
1869 call assert_equal(1, popup_getpos(winid).visible)
1870 call feedkeys("eli\<Esc>", 'xt')
1871 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001872 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001873
Bram Moolenaar17627312019-06-02 19:53:44 +02001874 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001875 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001876 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001877 redraw
1878 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001879 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001880 call feedkeys("eli\<Esc>", 'xt')
1881 call assert_equal(1, popup_getpos(winid).visible)
1882 call feedkeys("wi\<Esc>", 'xt')
1883 call assert_equal(1, popup_getpos(winid).visible)
1884 call feedkeys("Eli\<Esc>", 'xt')
1885 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001886 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001887
1888 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001889 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001890 redraw
1891 call assert_equal(1, popup_getpos(winid).visible)
1892 call feedkeys("eli\<Esc>", 'xt')
1893 call feedkeys("ei\<Esc>", 'xt')
1894 call assert_equal(1, popup_getpos(winid).visible)
1895 call feedkeys("eli\<Esc>", 'xt')
1896 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001897 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001898
1899 bwipe!
1900 call test_override('ALL', 0)
1901endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001902
1903func Test_notifications()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001904 CheckFeature timers
1905 CheckScreendump
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001906
Bram Moolenaar0fdddee2019-09-01 15:26:23 +02001907 let lines =<< trim END
1908 call setline(1, range(1, 20))
1909 hi Notification ctermbg=lightblue
1910 call popup_notification('first notification', {})
1911 END
1912 call writefile(lines, 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001913 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001914 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1915
1916 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001917 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1918 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001919 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1920
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001921 " clean up
1922 call StopVimInTerminal(buf)
1923 call delete('XtestNotifications')
1924endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001925
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001926func Test_popup_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001927 CheckScreendump
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001928
1929 let lines =<< trim END
1930 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02001931 hi ScrollThumb ctermbg=blue
1932 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001933 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001934 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001935 \ minwidth: 8,
1936 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001937 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001938 func ScrollUp()
1939 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
1940 endfunc
1941 func ScrollDown()
1942 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
1943 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001944 func ClickTop()
1945 call feedkeys("\<F4>\<LeftMouse>", "xt")
1946 endfunc
1947 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001948 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001949 call feedkeys("\<F5>\<LeftMouse>", "xt")
1950 endfunc
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001951 func Popup_filter(winid, key)
1952 if a:key == 'j'
1953 let line = popup_getoptions(a:winid).firstline
1954 let nlines = line('$', a:winid)
1955 let newline = line < nlines ? (line + 1) : nlines
1956 call popup_setoptions(a:winid, #{firstline: newline})
1957 return v:true
1958 elseif a:key == 'x'
1959 call popup_close(a:winid)
1960 return v:true
1961 endif
1962 endfunc
1963
1964 func PopupScroll()
1965 call popup_clear()
1966 let text =<< trim END
1967 1
1968 2
1969 3
1970 4
1971 long line long line long line long line long line long line
1972 long line long line long line long line long line long line
1973 long line long line long line long line long line long line
1974 END
1975 call popup_create(text, #{
1976 \ minwidth: 30,
1977 \ maxwidth: 30,
1978 \ minheight: 4,
1979 \ maxheight: 4,
1980 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +01001981 \ lastline: 4,
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001982 \ wrap: v:true,
1983 \ scrollbar: v:true,
1984 \ mapping: v:false,
1985 \ filter: funcref('Popup_filter')
1986 \ })
1987 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001988 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001989 map <silent> <F4> :call test_setmouse(4, 42)<CR>
1990 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001991 END
1992 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001993 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001994 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
1995
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001996 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001997 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001998 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
1999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002000 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002001 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002002 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
2003
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002004 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002005 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002006 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
2007
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02002008 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb', firstline: 5})\<CR>")
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002009 " this scrolls two lines (half the window height)
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002010 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
2011 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
2012
2013 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2014 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
2015
2016 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02002017 " wait a bit, otherwise it fails sometimes (double click recognized?)
2018 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002019 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2020 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
2021
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002022 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2023 sleep 100m
2024 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2025 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
2026
2027 call term_sendkeys(buf, ":call ClickBot()\<CR>")
2028 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
2029
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002030 " remove the minwidth and maxheight
2031 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxheight: 0, minwidth: 0})\<CR>")
Bram Moolenaar7e0f4622019-09-17 21:23:39 +02002032 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002033 call VerifyScreenDump(buf, 'Test_popupwin_scroll_10', {})
2034
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002035 " check size with non-wrapping lines
2036 call term_sendkeys(buf, ":call PopupScroll()\<CR>")
2037 call VerifyScreenDump(buf, 'Test_popupwin_scroll_11', {})
2038
2039 " check size with wrapping lines
2040 call term_sendkeys(buf, "j")
2041 call VerifyScreenDump(buf, 'Test_popupwin_scroll_12', {})
2042 call term_sendkeys(buf, "x")
2043
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002044 " clean up
2045 call StopVimInTerminal(buf)
2046 call delete('XtestPopupScroll')
2047endfunc
2048
Bram Moolenaar437a7462019-07-05 20:17:22 +02002049func Test_popup_fitting_scrollbar()
2050 " this was causing a crash, divide by zero
2051 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002052 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002053 \ scrollbar: 1,
2054 \ maxwidth: 10,
2055 \ maxheight: 5,
2056 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02002057 redraw
2058 call popup_clear()
2059endfunc
2060
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002061func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002062 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002063
2064 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002065 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002066 let p = popup_create('test', opts)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002067 eval p->popup_settext('this is a text')
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002068 END
2069
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002070 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002071 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002072 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
2073
2074 " Setting to empty string clears it
2075 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
2076 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
2077
2078 " Setting a list
2079 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2080 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2081
2082 " Shrinking with a list
2083 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
2084 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
2085
2086 " Growing with a list
2087 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2088 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2089
2090 " Empty list clears
2091 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
2092 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
2093
2094 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002095 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002096 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
2097
2098 " clean up
2099 call StopVimInTerminal(buf)
2100 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002101endfunc
2102
2103func Test_popup_hidden()
2104 new
2105
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002106 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002107 redraw
2108 call assert_equal(0, popup_getpos(winid).visible)
2109 call popup_close(winid)
2110
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002111 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002112 redraw
2113 call assert_equal(0, popup_getpos(winid).visible)
2114 call popup_close(winid)
2115
2116 func QuitCallback(id, res)
2117 let s:cb_winid = a:id
2118 let s:cb_res = a:res
2119 endfunc
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002120 let winid = 'make a choice'->popup_dialog(#{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002121 \ filter: 'popup_filter_yesno',
2122 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002123 \ })
2124 redraw
2125 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02002126 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
2127 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002128 exe "normal anot used by filter\<Esc>"
2129 call assert_equal('not used by filter', getline(1))
2130
2131 call popup_show(winid)
2132 call feedkeys('y', "xt")
2133 call assert_equal(1, s:cb_res)
2134
2135 bwipe!
2136 delfunc QuitCallback
2137endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02002138
2139" Test options not checked elsewhere
2140func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002141 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002142 let options = popup_getoptions(winid)
2143 call assert_equal(1, options.wrap)
2144 call assert_equal(0, options.drag)
2145 call assert_equal('Beautiful', options.highlight)
2146
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002147 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002148 let options = popup_getoptions(winid)
2149 call assert_equal(0, options.wrap)
2150 call assert_equal(1, options.drag)
2151 call assert_equal('Another', options.highlight)
2152
2153 call popup_close(winid)
2154endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002155
2156func Test_popupwin_garbage_collect()
2157 func MyPopupFilter(x, winid, c)
2158 " NOP
2159 endfunc
2160
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002161 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002162 call test_garbagecollect_now()
2163 redraw
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02002164 " Must not crash caused by invalid memory access
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002165 call feedkeys('j', 'xt')
2166 call assert_true(v:true)
2167
2168 call popup_close(winid)
2169 delfunc MyPopupFilter
2170endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002171
Bram Moolenaar581ba392019-09-03 22:08:33 +02002172func Test_popupwin_filter_mode()
2173 func MyPopupFilter(winid, c)
2174 let s:typed = a:c
2175 if a:c == ':' || a:c == "\r" || a:c == 'v'
2176 " can start cmdline mode, get out, and start/stop Visual mode
2177 return 0
2178 endif
2179 return 1
2180 endfunc
2181
2182 " Normal, Visual and Insert mode
2183 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'nvi'})
2184 redraw
2185 call feedkeys('x', 'xt')
2186 call assert_equal('x', s:typed)
2187
2188 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2189 call assert_equal(':', s:typed)
2190 call assert_equal('foo', g:foo)
2191
2192 let @x = 'something'
2193 call feedkeys('v$"xy', 'xt')
2194 call assert_equal('y', s:typed)
2195 call assert_equal('something', @x) " yank command is filtered out
2196 call feedkeys('v', 'xt') " end Visual mode
2197
2198 call popup_close(winid)
2199
2200 " only Normal mode
2201 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'n'})
2202 redraw
2203 call feedkeys('x', 'xt')
2204 call assert_equal('x', s:typed)
2205
2206 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2207 call assert_equal(':', s:typed)
2208 call assert_equal('foo', g:foo)
2209
2210 let @x = 'something'
2211 call feedkeys('v$"xy', 'xt')
2212 call assert_equal('v', s:typed)
2213 call assert_notequal('something', @x)
2214
2215 call popup_close(winid)
2216
2217 " default: all modes
2218 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
2219 redraw
2220 call feedkeys('x', 'xt')
2221 call assert_equal('x', s:typed)
2222
2223 let g:foo = 'bar'
2224 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2225 call assert_equal("\r", s:typed)
2226 call assert_equal('bar', g:foo)
2227
2228 let @x = 'something'
2229 call feedkeys('v$"xy', 'xt')
2230 call assert_equal('y', s:typed)
2231 call assert_equal('something', @x) " yank command is filtered out
2232 call feedkeys('v', 'xt') " end Visual mode
2233
2234 call popup_close(winid)
2235 delfunc MyPopupFilter
2236endfunc
2237
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002238func Test_popupwin_filter_mouse()
2239 func MyPopupFilter(winid, c)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002240 let g:got_mousepos = getmousepos()
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002241 return 0
2242 endfunc
2243
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002244 call setline(1, ['.'->repeat(25)]->repeat(10))
2245 let winid = popup_create(['short', 'long line that will wrap', 'other'], #{
2246 \ line: 2,
2247 \ col: 4,
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002248 \ maxwidth: 12,
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002249 \ padding: [],
2250 \ border: [],
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002251 \ filter: 'MyPopupFilter',
2252 \ })
2253 redraw
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002254 " 123456789012345678901
2255 " 1 .....................
2256 " 2 ...+--------------+..
2257 " 3 ...| |..
2258 " 4 ...| short |..
2259 " 5 ...| long line th |..
2260 " 6 ...| at will wrap |..
2261 " 7 ...| other |..
2262 " 8 ...| |..
2263 " 9 ...+--------------+..
2264 " 10 .....................
2265 let tests = []
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002266
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002267 func AddItemOutsidePopup(tests, row, col)
2268 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2269 \ screenrow: a:row, screencol: a:col,
2270 \ winid: win_getid(), winrow: a:row, wincol: a:col,
2271 \ line: a:row, column: a:col,
2272 \ }})
2273 endfunc
2274 func AddItemInPopupBorder(tests, winid, row, col)
2275 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2276 \ screenrow: a:row, screencol: a:col,
2277 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2278 \ line: 0, column: 0,
2279 \ }})
2280 endfunc
2281 func AddItemInPopupText(tests, winid, row, col, textline, textcol)
2282 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2283 \ screenrow: a:row, screencol: a:col,
2284 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2285 \ line: a:textline, column: a:textcol,
2286 \ }})
2287 endfunc
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002288
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002289 " above and below popup
2290 for c in range(1, 21)
2291 call AddItemOutsidePopup(tests, 1, c)
2292 call AddItemOutsidePopup(tests, 10, c)
2293 endfor
2294 " left and right of popup
2295 for r in range(1, 10)
2296 call AddItemOutsidePopup(tests, r, 3)
2297 call AddItemOutsidePopup(tests, r, 20)
2298 endfor
2299 " top and bottom in popup
2300 for c in range(4, 19)
2301 call AddItemInPopupBorder(tests, winid, 2, c)
2302 call AddItemInPopupBorder(tests, winid, 3, c)
2303 call AddItemInPopupBorder(tests, winid, 8, c)
2304 call AddItemInPopupBorder(tests, winid, 9, c)
2305 endfor
2306 " left and right margin in popup
2307 for r in range(2, 9)
2308 call AddItemInPopupBorder(tests, winid, r, 4)
2309 call AddItemInPopupBorder(tests, winid, r, 5)
2310 call AddItemInPopupBorder(tests, winid, r, 18)
2311 call AddItemInPopupBorder(tests, winid, r, 19)
2312 endfor
2313 " text "short"
2314 call AddItemInPopupText(tests, winid, 4, 6, 1, 1)
2315 call AddItemInPopupText(tests, winid, 4, 10, 1, 5)
2316 call AddItemInPopupText(tests, winid, 4, 11, 1, 6)
2317 call AddItemInPopupText(tests, winid, 4, 17, 1, 6)
2318 " text "long line th"
2319 call AddItemInPopupText(tests, winid, 5, 6, 2, 1)
2320 call AddItemInPopupText(tests, winid, 5, 10, 2, 5)
2321 call AddItemInPopupText(tests, winid, 5, 17, 2, 12)
2322 " text "at will wrap"
2323 call AddItemInPopupText(tests, winid, 6, 6, 2, 13)
2324 call AddItemInPopupText(tests, winid, 6, 10, 2, 17)
2325 call AddItemInPopupText(tests, winid, 6, 17, 2, 24)
2326 " text "other"
2327 call AddItemInPopupText(tests, winid, 7, 6, 3, 1)
2328 call AddItemInPopupText(tests, winid, 7, 10, 3, 5)
2329 call AddItemInPopupText(tests, winid, 7, 11, 3, 6)
2330 call AddItemInPopupText(tests, winid, 7, 17, 3, 6)
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002331
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002332 for item in tests
2333 call test_setmouse(item.clickrow, item.clickcol)
2334 call feedkeys("\<LeftMouse>", 'xt')
2335 call assert_equal(item.result, g:got_mousepos)
2336 endfor
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002337
2338 call popup_close(winid)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002339 enew!
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002340 delfunc MyPopupFilter
2341endfunc
2342
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002343func Test_popupwin_with_buffer()
2344 call writefile(['some text', 'in a buffer'], 'XsomeFile')
2345 let buf = bufadd('XsomeFile')
2346 call assert_equal(0, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002347
2348 setlocal number
2349 call setbufvar(buf, "&wrapmargin", 13)
2350
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002351 let winid = popup_create(buf, {})
2352 call assert_notequal(0, winid)
2353 let pos = popup_getpos(winid)
2354 call assert_equal(2, pos.height)
2355 call assert_equal(1, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002356
2357 " window-local option is set to default, buffer-local is not
2358 call assert_equal(0, getwinvar(winid, '&number'))
2359 call assert_equal(13, getbufvar(buf, '&wrapmargin'))
2360
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002361 call popup_close(winid)
2362 call assert_equal({}, popup_getpos(winid))
2363 call assert_equal(1, bufloaded(buf))
2364 exe 'bwipe! ' .. buf
Bram Moolenaar46451042019-08-24 15:50:46 +02002365 setlocal nonumber
Bram Moolenaar7866b872019-07-01 22:21:01 +02002366
2367 edit test_popupwin.vim
2368 let winid = popup_create(bufnr(''), {})
2369 redraw
2370 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002371 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002372endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02002373
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002374func Test_popupwin_terminal_buffer()
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002375 CheckFeature terminal
2376
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002377 let ptybuf = term_start(&shell, #{hidden: 1})
2378 call assert_fails('let winnr = popup_create(ptybuf, #{})', 'E278:')
2379 exe 'bwipe! ' .. ptybuf
2380endfunc
2381
Bram Moolenaar934470e2019-09-01 23:27:05 +02002382func Test_popupwin_with_buffer_and_filter()
2383 new Xwithfilter
2384 call setline(1, range(100))
2385 let bufnr = bufnr()
2386 hide
2387
2388 func BufferFilter(win, key)
2389 if a:key == 'G'
2390 " recursive use of "G" does not cause problems.
2391 call win_execute(a:win, 'normal! G')
2392 return 1
2393 endif
2394 return 0
2395 endfunc
2396
2397 let winid = popup_create(bufnr, #{maxheight: 5, filter: 'BufferFilter'})
2398 call assert_equal(1, popup_getpos(winid).firstline)
2399 redraw
2400 call feedkeys("G", 'xt')
2401 call assert_equal(99, popup_getpos(winid).firstline)
2402
2403 call popup_close(winid)
2404 exe 'bwipe! ' .. bufnr
2405endfunc
2406
Bram Moolenaare296e312019-07-03 23:20:18 +02002407func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002408 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002409 \ maxwidth: 40,
2410 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02002411 \ })
2412 for top in range(1, 20)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002413 eval winid->popup_setoptions(#{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02002414 redraw
2415 call assert_equal(19, popup_getpos(winid).width)
2416 endfor
2417 call popup_clear()
2418endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02002419
2420func Test_popupwin_buf_close()
2421 let buf = bufadd('Xtestbuf')
2422 call bufload(buf)
2423 call setbufline(buf, 1, ['just', 'some', 'lines'])
2424 let winid = popup_create(buf, {})
2425 redraw
2426 call assert_equal(3, popup_getpos(winid).height)
2427 let bufinfo = getbufinfo(buf)[0]
2428 call assert_equal(1, bufinfo.changed)
2429 call assert_equal(0, bufinfo.hidden)
2430 call assert_equal(0, bufinfo.listed)
2431 call assert_equal(1, bufinfo.loaded)
2432 call assert_equal([], bufinfo.windows)
2433 call assert_equal([winid], bufinfo.popups)
2434
2435 call popup_close(winid)
2436 call assert_equal({}, popup_getpos(winid))
2437 let bufinfo = getbufinfo(buf)[0]
2438 call assert_equal(1, bufinfo.changed)
2439 call assert_equal(1, bufinfo.hidden)
2440 call assert_equal(0, bufinfo.listed)
2441 call assert_equal(1, bufinfo.loaded)
2442 call assert_equal([], bufinfo.windows)
2443 call assert_equal([], bufinfo.popups)
2444 exe 'bwipe! ' .. buf
2445endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02002446
2447func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002448 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02002449
2450 let lines =<< trim END
2451 call setline(1, range(1, 10))
2452 hi ScrollThumb ctermbg=blue
2453 hi ScrollBar ctermbg=red
2454 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002455 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002456 \ maxwidth: 10,
2457 \ maxheight: 3,
2458 \ pos : 'topleft',
2459 \ col : a:col,
2460 \ line : a:line,
2461 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02002462 \ })
2463 endfunc
2464 call PopupMenu(['x'], 1, 1)
2465 call PopupMenu(['123456789|'], 1, 16)
2466 call PopupMenu(['123456789|' .. ' '], 7, 1)
2467 call PopupMenu([repeat('123456789|', 100)], 7, 16)
2468 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
2469 END
2470 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002471 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02002472 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
2473
2474 " close the menu popupwin.
2475 call term_sendkeys(buf, " ")
2476 call term_sendkeys(buf, " ")
2477 call term_sendkeys(buf, " ")
2478 call term_sendkeys(buf, " ")
2479 call term_sendkeys(buf, " ")
2480
2481 " clean up
2482 call StopVimInTerminal(buf)
2483 call delete('XtestPopupMenuMaxWidth')
2484endfunc
2485
Bram Moolenaara901a372019-07-13 16:38:50 +02002486func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002487 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02002488
2489 let lines =<< trim END
2490 call setline(1, range(1, 20))
2491 hi ScrollThumb ctermbg=blue
2492 hi ScrollBar ctermbg=red
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002493 eval ['one', 'two', 'three', 'four', 'five',
2494 \ 'six', 'seven', 'eight', 'nine']
2495 \ ->popup_menu(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002496 \ minwidth: 8,
2497 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02002498 \ })
2499 END
2500 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002501 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02002502
2503 call term_sendkeys(buf, "j")
2504 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
2505
2506 call term_sendkeys(buf, "jjj")
2507 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
2508
2509 " if the cursor is the bottom line, it stays at the bottom line.
2510 call term_sendkeys(buf, repeat("j", 20))
2511 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
2512
2513 call term_sendkeys(buf, "kk")
2514 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
2515
2516 call term_sendkeys(buf, "k")
2517 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
2518
2519 " if the cursor is in the top line, it stays in the top line.
2520 call term_sendkeys(buf, repeat("k", 20))
2521 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
2522
2523 " close the menu popupwin.
2524 call term_sendkeys(buf, " ")
2525
2526 " clean up
2527 call StopVimInTerminal(buf)
2528 call delete('XtestPopupMenuScroll')
2529endfunc
2530
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002531func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002532 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002533
2534 let lines =<< trim END
2535 function! MyFilter(winid, key) abort
2536 if a:key == "0"
2537 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
2538 return 1
2539 endif
2540 if a:key == "G"
2541 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
2542 return 1
2543 endif
2544 if a:key == "j"
2545 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
2546 return 1
2547 endif
2548 if a:key == "k"
2549 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
2550 return 1
2551 endif
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002552 if a:key == ':'
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002553 call popup_close(a:winid)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002554 return 0
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002555 endif
2556 return 0
2557 endfunction
2558 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
2559 \ maxheight : 3,
2560 \ filter : 'MyFilter'
2561 \ })
2562 END
2563 call writefile(lines, 'XtestPopupMenuFilter')
2564 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
2565
2566 call term_sendkeys(buf, "j")
2567 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
2568
2569 call term_sendkeys(buf, "k")
2570 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
2571
2572 call term_sendkeys(buf, "G")
2573 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
2574
2575 call term_sendkeys(buf, "0")
2576 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
2577
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002578 " check that when the popup is closed in the filter the screen is redrawn
2579 call term_sendkeys(buf, ":")
2580 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_5', {})
2581 call term_sendkeys(buf, "\<CR>")
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002582
2583 " clean up
2584 call StopVimInTerminal(buf)
2585 call delete('XtestPopupMenuFilter')
2586endfunc
2587
2588func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002589 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002590
2591 let winid = popup_create('some text', {})
2592 call assert_equal(0, popup_getoptions(winid).cursorline)
2593 call popup_close(winid)
2594
2595 let winid = popup_create('some text', #{ cursorline: 1, })
2596 call assert_equal(1, popup_getoptions(winid).cursorline)
2597 call popup_close(winid)
2598
2599 let winid = popup_create('some text', #{ cursorline: 0, })
2600 call assert_equal(0, popup_getoptions(winid).cursorline)
2601 call popup_close(winid)
2602
2603 let winid = popup_menu('some text', {})
2604 call assert_equal(1, popup_getoptions(winid).cursorline)
2605 call popup_close(winid)
2606
2607 let winid = popup_menu('some text', #{ cursorline: 1, })
2608 call assert_equal(1, popup_getoptions(winid).cursorline)
2609 call popup_close(winid)
2610
2611 let winid = popup_menu('some text', #{ cursorline: 0, })
2612 call assert_equal(0, popup_getoptions(winid).cursorline)
2613 call popup_close(winid)
2614
2615 " ---------
2616 " Pattern 1
2617 " ---------
2618 let lines =<< trim END
2619 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
2620 END
2621 call writefile(lines, 'XtestPopupCursorLine')
2622 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2623 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
2624 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2625 call StopVimInTerminal(buf)
2626
2627 " ---------
2628 " Pattern 2
2629 " ---------
2630 let lines =<< trim END
2631 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
2632 END
2633 call writefile(lines, 'XtestPopupCursorLine')
2634 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2635 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
2636 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2637 call StopVimInTerminal(buf)
2638
2639 " ---------
2640 " Pattern 3
2641 " ---------
2642 let lines =<< trim END
2643 function! MyFilter(winid, key) abort
2644 if a:key == "j"
2645 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2646 return 1
2647 endif
2648 if a:key == 'x'
2649 call popup_close(a:winid)
2650 return 1
2651 endif
2652 return 0
2653 endfunction
2654 call popup_menu(['111', '222', '333'], #{
2655 \ cursorline : 0,
2656 \ maxheight : 2,
2657 \ filter : 'MyFilter',
2658 \ })
2659 END
2660 call writefile(lines, 'XtestPopupCursorLine')
2661 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2662 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
2663 call term_sendkeys(buf, "j")
2664 call term_sendkeys(buf, "j")
2665 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
2666 call term_sendkeys(buf, "x")
2667 call StopVimInTerminal(buf)
2668
2669 " ---------
2670 " Pattern 4
2671 " ---------
2672 let lines =<< trim END
2673 function! MyFilter(winid, key) abort
2674 if a:key == "j"
2675 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2676 return 1
2677 endif
2678 if a:key == 'x'
2679 call popup_close(a:winid)
2680 return 1
2681 endif
2682 return 0
2683 endfunction
2684 call popup_menu(['111', '222', '333'], #{
2685 \ cursorline : 1,
2686 \ maxheight : 2,
2687 \ filter : 'MyFilter',
2688 \ })
2689 END
2690 call writefile(lines, 'XtestPopupCursorLine')
2691 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2692 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
2693 call term_sendkeys(buf, "j")
2694 call term_sendkeys(buf, "j")
2695 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
2696 call term_sendkeys(buf, "x")
2697 call StopVimInTerminal(buf)
2698
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02002699 " ---------
2700 " Cursor in second line when creating the popup
2701 " ---------
2702 let lines =<< trim END
2703 let winid = popup_create(['111', '222', '333'], #{
2704 \ cursorline : 1,
2705 \ })
2706 call win_execute(winid, "2")
2707 END
2708 call writefile(lines, 'XtestPopupCursorLine')
2709 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2710 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_7', {})
2711 call StopVimInTerminal(buf)
2712
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002713 call delete('XtestPopupCursorLine')
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01002714
2715 " ---------
2716 " Use current buffer for popupmenu
2717 " ---------
2718 let lines =<< trim END
2719 call setline(1, ['one', 'two', 'three'])
2720 let winid = popup_create(bufnr('%'), #{
2721 \ cursorline : 1,
2722 \ })
2723 call win_execute(winid, "2")
2724 END
2725 call writefile(lines, 'XtestPopupCursorLine')
2726 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2727 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_8', {})
2728 call StopVimInTerminal(buf)
2729
2730 call delete('XtestPopupCursorLine')
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002731endfunc
2732
Bram Moolenaarf914a332019-07-20 15:09:56 +02002733func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002734 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002735 CheckFeature quickfix
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002736
Bram Moolenaarf914a332019-07-20 15:09:56 +02002737 call writefile([
2738 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
2739 \ "another\tXtagfile\t/^this is another",
2740 \ "theword\tXtagfile\t/^theword"],
2741 \ 'Xtags')
2742 call writefile(range(1,20)
2743 \ + ['theword is here']
2744 \ + range(22, 27)
2745 \ + ['this is another place']
2746 \ + range(29, 40),
2747 \ "Xtagfile")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002748 call writefile(range(1,10)
2749 \ + ['searched word is here']
2750 \ + range(12, 20),
2751 \ "Xheader.h")
Bram Moolenaarf914a332019-07-20 15:09:56 +02002752 let lines =<< trim END
2753 set tags=Xtags
2754 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002755 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002756 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002757 \ 'three',
2758 \ 'four',
2759 \ 'five',
2760 \ 'six',
2761 \ 'seven',
2762 \ 'find theword somewhere',
2763 \ 'nine',
2764 \ 'this is another word',
2765 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02002766 set previewpopup=height:4,width:40
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002767 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02002768 END
2769 call writefile(lines, 'XtestPreviewPopup')
2770 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
2771
2772 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
2773 call term_sendkeys(buf, ":\<CR>")
2774 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
2775
2776 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2777 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
2778
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002779 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002780 call term_sendkeys(buf, ":\<CR>")
2781 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
2782
2783 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2784 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
2785
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002786 call term_sendkeys(buf, ":cd ..\<CR>:\<CR>")
2787 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002788 call term_sendkeys(buf, ":cd testdir\<CR>")
2789
2790 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02002791 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002792 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
2793
2794 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
2795 call term_sendkeys(buf, ":\<CR>")
2796 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
2797
2798 call term_sendkeys(buf, ":pclose\<CR>")
2799 call term_sendkeys(buf, ":psearch searched\<CR>")
2800 call term_sendkeys(buf, ":\<CR>")
2801 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002802
Bram Moolenaarf914a332019-07-20 15:09:56 +02002803 call StopVimInTerminal(buf)
2804 call delete('Xtags')
2805 call delete('Xtagfile')
2806 call delete('XtestPreviewPopup')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002807 call delete('Xheader.h')
Bram Moolenaarf914a332019-07-20 15:09:56 +02002808endfunc
2809
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002810func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002811 let lines =<< trim END
2812 set completeopt+=preview,popup
2813 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002814 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002815
2816 func CompleteFuncDict(findstart, base)
2817 if a:findstart
2818 if col('.') > 10
2819 return col('.') - 10
2820 endif
2821 return 0
2822 endif
2823
2824 return {
2825 \ 'words': [
2826 \ {
2827 \ 'word': 'aword',
2828 \ 'abbr': 'wrd',
2829 \ 'menu': 'extra text',
2830 \ 'info': 'words are cool',
2831 \ 'kind': 'W',
2832 \ 'user_data': 'test'
2833 \ },
2834 \ {
2835 \ 'word': 'anotherword',
2836 \ 'abbr': 'anotwrd',
2837 \ 'menu': 'extra text',
2838 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
2839 \ 'kind': 'W',
2840 \ 'user_data': 'notest'
2841 \ },
2842 \ {
2843 \ 'word': 'noinfo',
2844 \ 'abbr': 'noawrd',
2845 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002846 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002847 \ 'kind': 'W',
2848 \ 'user_data': 'notest'
2849 \ },
2850 \ {
2851 \ 'word': 'thatword',
2852 \ 'abbr': 'thatwrd',
2853 \ 'menu': 'extra text',
2854 \ 'info': 'that word is cool',
2855 \ 'kind': 'W',
2856 \ 'user_data': 'notest'
2857 \ },
2858 \ ]
2859 \ }
2860 endfunc
2861 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002862 func ChangeColor()
2863 let id = popup_findinfo()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002864 eval id->popup_setoptions(#{highlight: 'InfoPopup'})
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002865 endfunc
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002866
2867 func InfoHidden()
2868 set completepopup=height:4,border:off,align:menu
2869 set completeopt-=popup completeopt+=popuphidden
2870 au CompleteChanged * call HandleChange()
2871 endfunc
2872
2873 let s:counter = 0
2874 func HandleChange()
2875 let s:counter += 1
2876 let selected = complete_info(['selected']).selected
2877 if selected <= 0
2878 " First time: do nothing, info remains hidden
2879 return
2880 endif
2881 if selected == 1
2882 " Second time: show info right away
2883 let id = popup_findinfo()
2884 if id
2885 call popup_settext(id, 'immediate info ' .. s:counter)
2886 call popup_show(id)
2887 endif
2888 else
2889 " Third time: show info after a short delay
2890 call timer_start(100, 'ShowInfo')
2891 endif
2892 endfunc
2893
2894 func ShowInfo(...)
2895 let id = popup_findinfo()
2896 if id
2897 call popup_settext(id, 'async info ' .. s:counter)
2898 call popup_show(id)
2899 endif
2900 endfunc
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002901 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002902 return lines
2903endfunc
2904
2905func Test_popupmenu_info_border()
2906 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002907 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002908
2909 let lines = Get_popupmenu_lines()
2910 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002911 call writefile(lines, 'XtestInfoPopup')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002912
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002913 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
2914 call term_wait(buf, 50)
2915
2916 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2917 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
2918
2919 call term_sendkeys(buf, "\<C-N>")
2920 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
2921
2922 call term_sendkeys(buf, "\<C-N>")
2923 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
2924
2925 call term_sendkeys(buf, "\<C-N>\<C-N>")
2926 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
2927
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02002928 " info on the left with scrollbar
2929 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
2930 call term_sendkeys(buf, "\<C-N>\<C-N>")
2931 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
2932
Bram Moolenaar202c3f72019-11-21 12:12:35 +01002933 " Test that the popupmenu's scrollbar and infopopup do not overlap
2934 call term_sendkeys(buf, "\<Esc>")
2935 call term_sendkeys(buf, ":set pumheight=3\<CR>")
2936 call term_sendkeys(buf, "cc\<C-X>\<C-U>")
2937 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_6', {})
2938
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002939 call StopVimInTerminal(buf)
2940 call delete('XtestInfoPopup')
2941endfunc
2942
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002943func Test_popupmenu_info_noborder()
2944 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002945 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002946
2947 let lines = Get_popupmenu_lines()
2948 call add(lines, 'set completepopup=height:4,border:off')
2949 call writefile(lines, 'XtestInfoPopupNb')
2950
2951 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
2952 call term_wait(buf, 50)
2953
2954 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2955 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
2956
2957 call StopVimInTerminal(buf)
2958 call delete('XtestInfoPopupNb')
2959endfunc
2960
Bram Moolenaar258cef52019-08-21 17:29:29 +02002961func Test_popupmenu_info_align_menu()
2962 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002963 CheckFeature quickfix
Bram Moolenaar258cef52019-08-21 17:29:29 +02002964
2965 let lines = Get_popupmenu_lines()
2966 call add(lines, 'set completepopup=height:4,border:off,align:menu')
2967 call writefile(lines, 'XtestInfoPopupNb')
2968
2969 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
2970 call term_wait(buf, 50)
2971
2972 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2973 call term_sendkeys(buf, "\<C-N>")
2974 call term_sendkeys(buf, "\<C-N>")
2975 call term_sendkeys(buf, "\<C-N>")
2976 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
2977
2978 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
2979 call term_sendkeys(buf, "\<C-N>")
2980 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
2981
2982 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002983 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02002984 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
2985 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
2986 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
2987
2988 call StopVimInTerminal(buf)
2989 call delete('XtestInfoPopupNb')
2990endfunc
2991
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002992func Test_popupmenu_info_hidden()
2993 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002994 CheckFeature quickfix
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002995
2996 let lines = Get_popupmenu_lines()
2997 call add(lines, 'call InfoHidden()')
2998 call writefile(lines, 'XtestInfoPopupHidden')
2999
3000 let buf = RunVimInTerminal('-S XtestInfoPopupHidden', #{rows: 14})
3001 call term_wait(buf, 50)
3002
3003 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3004 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_1', {})
3005
3006 call term_sendkeys(buf, "\<C-N>")
3007 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_2', {})
3008
3009 call term_sendkeys(buf, "\<C-N>")
3010 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_3', {})
3011
3012 call term_sendkeys(buf, "\<Esc>")
3013 call StopVimInTerminal(buf)
3014 call delete('XtestInfoPopupHidden')
3015endfunc
3016
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003017func Test_popupmenu_info_too_wide()
3018 CheckScreendump
3019 CheckFeature quickfix
3020
3021 let lines =<< trim END
3022 call setline(1, range(10))
3023
3024 set completeopt+=preview,popup
3025 set completepopup=align:menu
3026 set omnifunc=OmniFunc
3027 hi InfoPopup ctermbg=lightgrey
3028
3029 func OmniFunc(findstart, base)
3030 if a:findstart
3031 return 0
3032 endif
3033
3034 let menuText = 'some long text to make sure the menu takes up all of the width of the window'
3035 return #{
3036 \ words: [
3037 \ #{
3038 \ word: 'scrap',
3039 \ menu: menuText,
3040 \ info: "other words are\ncooler than this and some more text\nto make wrap",
3041 \ },
3042 \ #{
3043 \ word: 'scappier',
3044 \ menu: menuText,
3045 \ info: 'words are cool',
3046 \ },
3047 \ #{
3048 \ word: 'scrappier2',
3049 \ menu: menuText,
3050 \ info: 'words are cool',
3051 \ },
3052 \ ]
3053 \ }
3054 endfunc
3055 END
3056
3057 call writefile(lines, 'XtestInfoPopupWide')
3058 let buf = RunVimInTerminal('-S XtestInfoPopupWide', #{rows: 8})
3059 call term_wait(buf, 50)
3060
3061 call term_sendkeys(buf, "Ascr\<C-X>\<C-O>")
3062 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_wide_1', {})
3063
3064 call term_sendkeys(buf, "\<Esc>")
3065 call StopVimInTerminal(buf)
3066 call delete('XtestInfoPopupWide')
3067endfunc
3068
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003069func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02003070 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003071 call popup_clear()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003072 let winid = 'nothing wrong'->popup_notification({})
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003073 call assert_equal(bufnr, winbufnr(winid))
3074 call popup_clear()
3075endfunc
3076
Bram Moolenaar1824f452019-10-02 23:06:46 +02003077func Test_popupwin_getoptions_tablocal()
3078 topleft split
3079 let win1 = popup_create('nothing', #{maxheight: 8})
3080 let win2 = popup_create('something', #{maxheight: 10})
3081 let win3 = popup_create('something', #{maxheight: 15})
3082 call assert_equal(8, popup_getoptions(win1).maxheight)
3083 call assert_equal(10, popup_getoptions(win2).maxheight)
3084 call assert_equal(15, popup_getoptions(win3).maxheight)
3085 call popup_clear()
3086 quit
3087endfunc
3088
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02003089func Test_popupwin_cancel()
3090 let win1 = popup_create('one', #{line: 5, filter: {... -> 0}})
3091 let win2 = popup_create('two', #{line: 10, filter: {... -> 0}})
3092 let win3 = popup_create('three', #{line: 15, filter: {... -> 0}})
3093 call assert_equal(5, popup_getpos(win1).line)
3094 call assert_equal(10, popup_getpos(win2).line)
3095 call assert_equal(15, popup_getpos(win3).line)
3096 " TODO: this also works without patch 8.1.2110
3097 call feedkeys("\<C-C>", 'xt')
3098 call assert_equal(5, popup_getpos(win1).line)
3099 call assert_equal(10, popup_getpos(win2).line)
3100 call assert_equal({}, popup_getpos(win3))
3101 call feedkeys("\<C-C>", 'xt')
3102 call assert_equal(5, popup_getpos(win1).line)
3103 call assert_equal({}, popup_getpos(win2))
3104 call assert_equal({}, popup_getpos(win3))
3105 call feedkeys("\<C-C>", 'xt')
3106 call assert_equal({}, popup_getpos(win1))
3107 call assert_equal({}, popup_getpos(win2))
3108 call assert_equal({}, popup_getpos(win3))
3109endfunc
3110
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003111func Test_popupwin_filter_redraw()
3112 " Create two popups with a filter that closes the popup when typing "0".
3113 " Both popups should close, even though the redraw also calls
3114 " popup_reset_handled()
3115
3116 func CloseFilter(winid, key)
3117 if a:key == '0'
3118 call popup_close(a:winid)
3119 redraw
3120 endif
3121 return 0 " pass the key
3122 endfunc
3123
3124 let id1 = popup_create('first one', #{
3125 \ line: 1,
3126 \ col: 1,
3127 \ filter: 'CloseFilter',
3128 \ })
3129 let id2 = popup_create('second one', #{
3130 \ line: 9,
3131 \ col: 1,
3132 \ filter: 'CloseFilter',
3133 \ })
3134 call assert_equal(1, popup_getpos(id1).line)
3135 call assert_equal(9, popup_getpos(id2).line)
3136
3137 call feedkeys('0', 'xt')
3138 call assert_equal({}, popup_getpos(id1))
3139 call assert_equal({}, popup_getpos(id2))
3140
3141 call popup_clear()
3142 delfunc CloseFilter
3143endfunc
3144
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003145func Test_popupwin_double_width()
3146 CheckScreendump
3147
3148 let lines =<< trim END
3149 call setline(1, 'x你好世界你好世你好世界你好')
3150 call setline(2, '你好世界你好世你好世界你好')
3151 call setline(3, 'x你好世界你好世你好世界你好')
3152 call popup_create('你好,世界 - 你好,世界xxxxx', #{line: 1, col: 3, maxwidth: 14})
3153 END
3154 call writefile(lines, 'XtestPopupWide')
3155
3156 let buf = RunVimInTerminal('-S XtestPopupWide', #{rows: 10})
3157 call VerifyScreenDump(buf, 'Test_popupwin_doublewidth_1', {})
3158
3159 call StopVimInTerminal(buf)
3160 call delete('XtestPopupWide')
3161endfunc
3162
3163func Test_popupwin_sign()
3164 CheckScreendump
3165
3166 let lines =<< trim END
3167 call setline(1, range(10))
3168 call sign_define('Current', {
3169 \ 'text': '>>',
3170 \ 'texthl': 'WarningMsg',
3171 \ 'linehl': 'Error',
3172 \ })
3173 call sign_define('Other', {
3174 \ 'text': '#!',
3175 \ 'texthl': 'Error',
3176 \ 'linehl': 'Search',
3177 \ })
3178 let winid = popup_create(['hello', 'bright', 'world'], {
3179 \ 'minwidth': 20,
3180 \ })
3181 call setwinvar(winid, "&signcolumn", "yes")
3182 let winbufnr = winbufnr(winid)
3183
3184 " add sign to current buffer, shows
3185 call sign_place(1, 'Selected', 'Current', bufnr('%'), {'lnum': 1})
3186 " add sign to current buffer, does not show
3187 call sign_place(2, 'PopUpSelected', 'Other', bufnr('%'), {'lnum': 2})
3188
3189 " add sign to popup buffer, shows
3190 call sign_place(3, 'PopUpSelected', 'Other', winbufnr, {'lnum': 1})
3191 " add sign to popup buffer, does not show
3192 call sign_place(4, 'Selected', 'Current', winbufnr, {'lnum': 2})
3193 END
3194 call writefile(lines, 'XtestPopupSign')
3195
3196 let buf = RunVimInTerminal('-S XtestPopupSign', #{rows: 10})
3197 call VerifyScreenDump(buf, 'Test_popupwin_sign_1', {})
3198
3199 call StopVimInTerminal(buf)
3200 call delete('XtestPopupSign')
3201endfunc
3202
Bram Moolenaar99ebf222019-12-10 23:44:48 +01003203func Test_popupwin_bufnr()
3204 let popwin = popup_create(['blah'], #{})
3205 let popbuf = winbufnr(popwin)
3206 split asdfasdf
3207 let newbuf = bufnr()
3208 call assert_true(newbuf > popbuf, 'New buffer number is higher')
3209 call assert_equal(newbuf, bufnr('$'))
3210 call popup_clear()
3211 let popwin = popup_create(['blah'], #{})
3212 " reuses previous buffer number
3213 call assert_equal(popbuf, winbufnr(popwin))
3214 call assert_equal(newbuf, bufnr('$'))
3215
3216 call popup_clear()
3217 bwipe!
3218endfunc
3219
Bram Moolenaar331bafd2019-07-20 17:46:05 +02003220" vim: shiftwidth=2 sts=2