blob: 2c4da0b794c67ee0437ac89ac6756d5b6ea1f853 [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
4CheckFeature textprop
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
15 hi Comment ctermfg=red
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020016 call prop_type_add('comment', #{highlight: 'Comment'})
17 let winid = popup_create('hello there', #{line: 3, col: 11, minwidth: 20, highlight: 'PopupColor1'})
18 let winid2 = popup_create(['another one', 'another two', 'another three'], #{line: 3, col: 25, minwidth: 20})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020019 call setwinvar(winid2, '&wincolor', 'PopupColor2')
20 END
21 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020022 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar4d784b22019-05-25 19:51:39 +020023 call VerifyScreenDump(buf, 'Test_popupwin_01', {})
24
Bram Moolenaarec583842019-05-26 14:11:23 +020025 " Add a tabpage
26 call term_sendkeys(buf, ":tabnew\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020027 call term_sendkeys(buf, ":let popupwin = popup_create(["
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020028 \ .. "#{text: 'other tab'},"
29 \ .. "#{text: 'a comment line', props: [#{"
Bram Moolenaard5abb4c2019-07-13 22:46:10 +020030 \ .. "col: 3, length: 7, minwidth: 20, type: 'comment'"
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020031 \ .. "}]},"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020032 \ .. "], #{line: 4, col: 9, minwidth: 20})\<CR>")
Bram Moolenaarec583842019-05-26 14:11:23 +020033 call VerifyScreenDump(buf, 'Test_popupwin_02', {})
34
35 " switch back to first tabpage
36 call term_sendkeys(buf, "gt")
37 call VerifyScreenDump(buf, 'Test_popupwin_03', {})
38
39 " close that tabpage
40 call term_sendkeys(buf, ":quit!\<CR>")
41 call VerifyScreenDump(buf, 'Test_popupwin_04', {})
42
Bram Moolenaar202d9822019-06-11 21:56:30 +020043 " set 'columns' to a small value, size must be recomputed
44 call term_sendkeys(buf, ":let cols = &columns\<CR>")
45 call term_sendkeys(buf, ":set columns=12\<CR>")
46 call VerifyScreenDump(buf, 'Test_popupwin_04a', {})
47 call term_sendkeys(buf, ":let &columns = cols\<CR>")
48
Bram Moolenaar17146962019-05-30 00:12:11 +020049 " resize popup, show empty line at bottom
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020050 call term_sendkeys(buf, ":call popup_move(popupwin, #{minwidth: 15, maxwidth: 25, minheight: 3, maxheight: 5})\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020051 call term_sendkeys(buf, ":redraw\<CR>")
52 call VerifyScreenDump(buf, 'Test_popupwin_05', {})
53
Bram Moolenaar17146962019-05-30 00:12:11 +020054 " show not fitting line at bottom
55 call term_sendkeys(buf, ":call setbufline(winbufnr(popupwin), 3, 'this line will not fit here')\<CR>")
56 call term_sendkeys(buf, ":redraw\<CR>")
57 call VerifyScreenDump(buf, 'Test_popupwin_06', {})
58
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020059 " move popup over ruler
60 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020061 call term_sendkeys(buf, ":call popup_move(popupwin, #{line: 7, col: 55})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +020062 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020063 call VerifyScreenDump(buf, 'Test_popupwin_07', {})
64
65 " clear all popups after moving the cursor a bit, so that ruler is updated
66 call term_sendkeys(buf, "axxx\<Esc>")
67 call term_wait(buf)
68 call term_sendkeys(buf, "0")
69 call term_wait(buf)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +020070 call term_sendkeys(buf, ":call popup_clear()\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020071 call VerifyScreenDump(buf, 'Test_popupwin_08', {})
72
Bram Moolenaar4d784b22019-05-25 19:51:39 +020073 " clean up
74 call StopVimInTerminal(buf)
75 call delete('XtestPopup')
76endfunc
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020077
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020078func Test_popup_with_border_and_padding()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +020079 CheckScreendump
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020080
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020081 for iter in range(0, 1)
Bram Moolenaare7eb9272019-06-24 00:58:07 +020082 let lines =<< trim END
83 call setline(1, range(1, 100))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020084 call popup_create('hello border', #{line: 2, col: 3, border: []})
85 call popup_create('hello padding', #{line: 2, col: 23, padding: []})
Bram Moolenaarc363fe12019-08-04 18:13:46 +020086 call popup_create('hello both', #{line: 2, col: 43, border: [], padding: [], highlight: 'Normal'})
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020087 call popup_create('border TL', #{line: 6, col: 3, border: [1, 0, 0, 4]})
88 call popup_create('paddings', #{line: 6, col: 23, padding: [1, 3, 2, 4]})
89 call popup_create('wrapped longer text', #{line: 8, col: 55, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
90 call popup_create('right aligned text', #{line: 11, col: 56, wrap: 0, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020091 END
92 call insert(lines, iter == 1 ? '' : 'set enc=latin1')
93 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020094 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 15})
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020095 call VerifyScreenDump(buf, 'Test_popupwin_2' .. iter, {})
96
97 call StopVimInTerminal(buf)
98 call delete('XtestPopupBorder')
99 endfor
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200100
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200101 let lines =<< trim END
102 call setline(1, range(1, 100))
103 hi BlueColor ctermbg=lightblue
104 hi TopColor ctermbg=253
105 hi RightColor ctermbg=245
106 hi BottomColor ctermbg=240
107 hi LeftColor ctermbg=248
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200108 call popup_create('hello border', #{line: 2, col: 3, border: [], borderhighlight: ['BlueColor']})
109 call popup_create(['hello border', 'and more'], #{line: 2, col: 23, border: [], borderhighlight: ['TopColor', 'RightColor', 'BottomColor', 'LeftColor']})
110 call popup_create(['hello border', 'lines only'], #{line: 2, col: 43, border: [], borderhighlight: ['BlueColor'], borderchars: ['x']})
111 call popup_create(['hello border', 'with corners'], #{line: 2, col: 60, border: [], borderhighlight: ['BlueColor'], borderchars: ['x', '#']})
112 let winid = popup_create(['hello border', 'with numbers'], #{line: 6, col: 3, border: [], borderhighlight: ['BlueColor'], borderchars: ['0', '1', '2', '3', '4', '5', '6', '7']})
113 call popup_create(['hello border', 'just blanks'], #{line: 7, col: 23, border: [], borderhighlight: ['BlueColor'], borderchars: [' ']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200114 func MultiByte()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200115 call popup_create(['hello'], #{line: 8, col: 43, border: [], borderchars: ['─', '│', '─', '│', '┌', '┐', '┘', '└']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200116 endfunc
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200117 END
118 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200119 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 12})
Bram Moolenaar790498b2019-06-01 22:15:29 +0200120 call VerifyScreenDump(buf, 'Test_popupwin_22', {})
121
Bram Moolenaarad24a712019-06-17 20:05:45 +0200122 " check that changing borderchars triggers a redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200123 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 +0200124 call VerifyScreenDump(buf, 'Test_popupwin_23', {})
125
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200126 " check multi-byte border only with 'ambiwidth' single
127 if &ambiwidth == 'single'
128 call term_sendkeys(buf, ":call MultiByte()\<CR>")
129 call VerifyScreenDump(buf, 'Test_popupwin_24', {})
130 endif
131
Bram Moolenaar790498b2019-06-01 22:15:29 +0200132 call StopVimInTerminal(buf)
133 call delete('XtestPopupBorder')
134
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200135 let with_border_or_padding = #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200136 \ line: 2,
137 \ core_line: 3,
138 \ col: 3,
139 \ core_col: 4,
140 \ width: 14,
141 \ core_width: 12,
142 \ height: 3,
143 \ core_height: 1,
144 \ firstline: 1,
145 \ scrollbar: 0,
146 \ visible: 1}
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200147 let winid = popup_create('hello border', #{line: 2, col: 3, border: []})",
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200148 call assert_equal(with_border_or_padding, winid->popup_getpos())
Bram Moolenaarae943152019-06-16 22:54:14 +0200149 let options = popup_getoptions(winid)
150 call assert_equal([], options.border)
151 call assert_false(has_key(options, "padding"))
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200152
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200153 let winid = popup_create('hello padding', #{line: 2, col: 3, padding: []})
Bram Moolenaarae943152019-06-16 22:54:14 +0200154 let with_border_or_padding.width = 15
155 let with_border_or_padding.core_width = 13
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200156 call assert_equal(with_border_or_padding, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200157 let options = popup_getoptions(winid)
158 call assert_false(has_key(options, "border"))
159 call assert_equal([], options.padding)
160
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200161 call popup_setoptions(winid, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200162 \ padding: [1, 2, 3, 4],
163 \ border: [4, 0, 7, 8],
164 \ borderhighlight: ['Top', 'Right', 'Bottom', 'Left'],
165 \ borderchars: ['1', '^', '2', '>', '3', 'v', '4', '<'],
Bram Moolenaarae943152019-06-16 22:54:14 +0200166 \ })
167 let options = popup_getoptions(winid)
168 call assert_equal([1, 0, 1, 1], options.border)
169 call assert_equal([1, 2, 3, 4], options.padding)
170 call assert_equal(['Top', 'Right', 'Bottom', 'Left'], options.borderhighlight)
171 call assert_equal(['1', '^', '2', '>', '3', 'v', '4', '<'], options.borderchars)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200172
Bram Moolenaara1396152019-10-20 18:46:05 +0200173 " Check that popup_setoptions() takes the output of popup_getoptions()
174 call popup_setoptions(winid, options)
175 call assert_equal(options, popup_getoptions(winid))
176
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200177 let winid = popup_create('hello both', #{line: 3, col: 8, border: [], padding: []})
178 call assert_equal(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200179 \ line: 3,
180 \ core_line: 5,
181 \ col: 8,
182 \ core_col: 10,
183 \ width: 14,
184 \ core_width: 10,
185 \ height: 5,
186 \ scrollbar: 0,
187 \ core_height: 1,
188 \ firstline: 1,
189 \ visible: 1}, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200190
191 call popup_clear()
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200192endfunc
193
Bram Moolenaarb4230122019-05-30 18:40:53 +0200194func Test_popup_with_syntax_win_execute()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200195 CheckScreendump
196
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200197 let lines =<< trim END
198 call setline(1, range(1, 100))
199 hi PopupColor ctermbg=lightblue
200 let winid = popup_create([
201 \ '#include <stdio.h>',
202 \ 'int main(void)',
203 \ '{',
204 \ ' printf(123);',
205 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200206 \], #{line: 3, col: 25, highlight: 'PopupColor'})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200207 call win_execute(winid, 'set syntax=cpp')
208 END
209 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200210 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200211 call VerifyScreenDump(buf, 'Test_popupwin_10', {})
212
213 " clean up
214 call StopVimInTerminal(buf)
215 call delete('XtestPopup')
216endfunc
217
218func Test_popup_with_syntax_setbufvar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200219 CheckScreendump
220
Bram Moolenaar402502d2019-05-30 22:07:36 +0200221 let lines =<< trim END
222 call setline(1, range(1, 100))
223 hi PopupColor ctermbg=lightgrey
224 let winid = popup_create([
225 \ '#include <stdio.h>',
226 \ 'int main(void)',
227 \ '{',
Bram Moolenaare089c3f2019-07-09 20:25:25 +0200228 \ "\tprintf(567);",
Bram Moolenaar402502d2019-05-30 22:07:36 +0200229 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200230 \], #{line: 3, col: 21, highlight: 'PopupColor'})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200231 call setbufvar(winbufnr(winid), '&syntax', 'cpp')
232 END
233 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200234 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200235 call VerifyScreenDump(buf, 'Test_popupwin_11', {})
236
237 " clean up
238 call StopVimInTerminal(buf)
239 call delete('XtestPopup')
240endfunc
241
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200242func Test_popup_with_matches()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200243 CheckScreendump
244
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200245 let lines =<< trim END
246 call setline(1, ['111 222 333', '444 555 666'])
247 let winid = popup_create([
248 \ '111 222 333',
249 \ '444 555 666',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200250 \], #{line: 3, col: 10, border: []})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200251 set hlsearch
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100252 hi VeryBlue ctermfg=blue guifg=blue
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200253 /666
254 call matchadd('ErrorMsg', '111')
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100255 call matchadd('VeryBlue', '444')
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200256 call win_execute(winid, "call matchadd('ErrorMsg', '111')")
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100257 call win_execute(winid, "call matchadd('VeryBlue', '555')")
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200258 END
259 call writefile(lines, 'XtestPopupMatches')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200260 let buf = RunVimInTerminal('-S XtestPopupMatches', #{rows: 10})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200261 call VerifyScreenDump(buf, 'Test_popupwin_matches', {})
262
263 " clean up
264 call StopVimInTerminal(buf)
265 call delete('XtestPopupMatches')
266endfunc
267
Bram Moolenaar399d8982019-06-02 15:34:29 +0200268func Test_popup_all_corners()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200269 CheckScreendump
270
Bram Moolenaar399d8982019-06-02 15:34:29 +0200271 let lines =<< trim END
272 call setline(1, repeat([repeat('-', 60)], 15))
273 set so=0
274 normal 2G3|r#
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200275 let winid1 = popup_create(['first', 'second'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200276 \ line: 'cursor+1',
277 \ col: 'cursor',
278 \ pos: 'topleft',
279 \ border: [],
280 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200281 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200282 normal 24|r@
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200283 let winid1 = popup_create(['First', 'SeconD'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200284 \ line: 'cursor+1',
285 \ col: 'cursor',
286 \ pos: 'topright',
287 \ border: [],
288 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200289 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200290 normal 9G27|r%
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200291 let winid1 = popup_create(['fiRSt', 'seCOnd'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200292 \ line: 'cursor-1',
293 \ col: 'cursor',
294 \ pos: 'botleft',
295 \ border: [],
296 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200297 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200298 normal 48|r&
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200299 let winid1 = popup_create(['FIrsT', 'SEcoND'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200300 \ line: 'cursor-1',
301 \ col: 'cursor',
302 \ pos: 'botright',
303 \ border: [],
304 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200305 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200306 normal 1G51|r*
307 let winid1 = popup_create(['one', 'two'], #{
308 \ line: 'cursor-1',
309 \ col: 'cursor',
310 \ pos: 'botleft',
311 \ border: [],
312 \ padding: [],
313 \ })
Bram Moolenaar399d8982019-06-02 15:34:29 +0200314 END
315 call writefile(lines, 'XtestPopupCorners')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200316 let buf = RunVimInTerminal('-S XtestPopupCorners', #{rows: 12})
Bram Moolenaar399d8982019-06-02 15:34:29 +0200317 call VerifyScreenDump(buf, 'Test_popupwin_corners', {})
318
319 " clean up
320 call StopVimInTerminal(buf)
321 call delete('XtestPopupCorners')
322endfunc
323
Bram Moolenaar8d241042019-06-12 23:40:01 +0200324func Test_popup_firstline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200325 CheckScreendump
326
Bram Moolenaar8d241042019-06-12 23:40:01 +0200327 let lines =<< trim END
328 call setline(1, range(1, 20))
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200329 let winid = popup_create(['1111', '222222', '33333', '44', '5', '666666', '77777', '888', '9999999999999999'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200330 \ maxheight: 4,
331 \ firstline: 3,
Bram Moolenaar8d241042019-06-12 23:40:01 +0200332 \ })
333 END
334 call writefile(lines, 'XtestPopupFirstline')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200335 let buf = RunVimInTerminal('-S XtestPopupFirstline', #{rows: 10})
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200336 call VerifyScreenDump(buf, 'Test_popupwin_firstline_1', {})
337
338 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: -1})\<CR>")
339 call term_sendkeys(buf, ":\<CR>")
340 call VerifyScreenDump(buf, 'Test_popupwin_firstline_2', {})
Bram Moolenaar8d241042019-06-12 23:40:01 +0200341
342 " clean up
343 call StopVimInTerminal(buf)
344 call delete('XtestPopupFirstline')
Bram Moolenaarae943152019-06-16 22:54:14 +0200345
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200346 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200347 \ maxheight: 2,
348 \ firstline: 3,
Bram Moolenaarae943152019-06-16 22:54:14 +0200349 \ })
350 call assert_equal(3, popup_getoptions(winid).firstline)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200351 call popup_setoptions(winid, #{firstline: 1})
Bram Moolenaarae943152019-06-16 22:54:14 +0200352 call assert_equal(1, popup_getoptions(winid).firstline)
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200353 eval winid->popup_close()
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200354
355 let winid = popup_create(['xxx']->repeat(50), #{
356 \ maxheight: 3,
357 \ firstline: 11,
358 \ })
359 redraw
360 call assert_equal(11, popup_getoptions(winid).firstline)
361 call assert_equal(11, popup_getpos(winid).firstline)
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +0200362 " check line() works with popup window
363 call assert_equal(11, line('.', winid))
364 call assert_equal(50, line('$', winid))
365 call assert_equal(0, line('$', 123456))
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200366
367 " Normal command changes what is displayed but not "firstline"
368 call win_execute(winid, "normal! \<c-y>")
369 call assert_equal(11, popup_getoptions(winid).firstline)
370 call assert_equal(10, popup_getpos(winid).firstline)
371
372 " Making some property change applies "firstline" again
373 call popup_setoptions(winid, #{line: 4})
374 call assert_equal(11, popup_getoptions(winid).firstline)
375 call assert_equal(11, popup_getpos(winid).firstline)
376
377 " Remove "firstline" property and scroll
378 call popup_setoptions(winid, #{firstline: 0})
379 call win_execute(winid, "normal! \<c-y>")
380 call assert_equal(0, popup_getoptions(winid).firstline)
381 call assert_equal(10, popup_getpos(winid).firstline)
382
383 " Making some property change has no side effect
384 call popup_setoptions(winid, #{line: 3})
385 call assert_equal(0, popup_getoptions(winid).firstline)
386 call assert_equal(10, popup_getpos(winid).firstline)
Bram Moolenaarae943152019-06-16 22:54:14 +0200387
388 call popup_close(winid)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200389endfunc
390
Bram Moolenaara112f2d2019-09-01 17:38:09 +0200391func Test_popup_noscrolloff()
392 set scrolloff=5
393 let winid = popup_create(['xxx']->repeat(50), #{
394 \ maxheight: 5,
395 \ firstline: 11,
396 \ })
397 redraw
398 call assert_equal(11, popup_getoptions(winid).firstline)
399 call assert_equal(11, popup_getpos(winid).firstline)
400
401 call popup_setoptions(winid, #{firstline: 0})
402 call win_execute(winid, "normal! \<c-y>")
403 call assert_equal(0, popup_getoptions(winid).firstline)
404 call assert_equal(10, popup_getpos(winid).firstline)
405
406 call popup_close(winid)
407endfunc
408
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200409func Test_popup_drag()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200410 CheckScreendump
411
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200412 " create a popup that covers the command line
413 let lines =<< trim END
414 call setline(1, range(1, 20))
Bram Moolenaar356375f2019-08-24 14:46:29 +0200415 split
416 vsplit
417 $wincmd w
418 vsplit
419 1wincmd w
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200420 let winid = popup_create(['1111', '222222', '33333'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200421 \ drag: 1,
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200422 \ resize: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200423 \ border: [],
424 \ line: &lines - 4,
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200425 \ })
426 func Dragit()
427 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
428 endfunc
429 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
Bram Moolenaar356375f2019-08-24 14:46:29 +0200430 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2 - 20)<CR>
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200431 func Resize()
432 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
433 endfunc
Bram Moolenaar356375f2019-08-24 14:46:29 +0200434 map <silent> <F5> :call test_setmouse(6, 21)<CR>
435 map <silent> <F6> :call test_setmouse(7, 25)<CR>
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200436 END
437 call writefile(lines, 'XtestPopupDrag')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200438 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200439 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
440
441 call term_sendkeys(buf, ":call Dragit()\<CR>")
442 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
443
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200444 call term_sendkeys(buf, ":call Resize()\<CR>")
445 call VerifyScreenDump(buf, 'Test_popupwin_drag_03', {})
446
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200447 " clean up
448 call StopVimInTerminal(buf)
449 call delete('XtestPopupDrag')
450endfunc
451
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200452func Test_popup_close_with_mouse()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200453 CheckScreendump
454
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200455 let lines =<< trim END
456 call setline(1, range(1, 20))
457 " With border, can click on X
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200458 let winid = popup_create('foobar', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200459 \ close: 'button',
460 \ border: [],
461 \ line: 1,
462 \ col: 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200463 \ })
464 func CloseMsg(id, result)
465 echomsg 'Popup closed with ' .. a:result
466 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200467 let winid = popup_create('notification', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200468 \ close: 'click',
469 \ line: 3,
470 \ col: 15,
471 \ callback: 'CloseMsg',
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200472 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200473 let winid = popup_create('no border here', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200474 \ close: 'button',
475 \ line: 5,
476 \ col: 3,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200477 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200478 let winid = popup_create('only padding', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200479 \ close: 'button',
480 \ padding: [],
481 \ line: 5,
482 \ col: 23,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200483 \ })
484 func CloseWithX()
485 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
486 endfunc
487 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
488 func CloseWithClick()
489 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
490 endfunc
491 map <silent> <F4> :call test_setmouse(3, 17)<CR>
Bram Moolenaarf6396232019-08-24 19:36:00 +0200492 func CreateWithMenuFilter()
493 let winid = popup_create('barfoo', #{
494 \ close: 'button',
495 \ filter: 'popup_filter_menu',
496 \ border: [],
497 \ line: 1,
498 \ col: 40,
499 \ })
500 endfunc
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200501 END
502 call writefile(lines, 'XtestPopupClose')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200503 let buf = RunVimInTerminal('-S XtestPopupClose', #{rows: 10})
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200504 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
505
506 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
507 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
508
509 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
510 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
511
Bram Moolenaarf6396232019-08-24 19:36:00 +0200512 call term_sendkeys(buf, ":call CreateWithMenuFilter()\<CR>")
513 call VerifyScreenDump(buf, 'Test_popupwin_close_04', {})
514
515 " We have to send the actual mouse code, feedkeys() would be caught the
516 " filter.
517 call term_sendkeys(buf, "\<Esc>[<0;47;1M")
518 call VerifyScreenDump(buf, 'Test_popupwin_close_05', {})
519
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200520 " clean up
521 call StopVimInTerminal(buf)
522 call delete('XtestPopupClose')
523endfunction
524
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200525func Test_popup_menu_wrap()
526 CheckScreendump
527
528 let lines =<< trim END
529 call setline(1, range(1, 20))
530 call popup_create([
531 \ 'one',
532 \ 'asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfas',
533 \ 'three',
534 \ 'four',
535 \ ], #{
536 \ pos: "botleft",
537 \ border: [],
538 \ padding: [0,1,0,1],
539 \ maxheight: 3,
540 \ cursorline: 1,
541 \ filter: 'popup_filter_menu',
542 \ })
543 END
544 call writefile(lines, 'XtestPopupWrap')
545 let buf = RunVimInTerminal('-S XtestPopupWrap', #{rows: 10})
546 call VerifyScreenDump(buf, 'Test_popupwin_wrap_1', {})
547
548 call term_sendkeys(buf, "jj")
549 call VerifyScreenDump(buf, 'Test_popupwin_wrap_2', {})
550
551 " clean up
552 call term_sendkeys(buf, "\<Esc>")
553 call StopVimInTerminal(buf)
554 call delete('XtestPopupWrap')
555endfunction
556
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200557func Test_popup_with_mask()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200558 CheckScreendump
559
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200560 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200561 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200562 hi PopupColor ctermbg=lightgrey
563 let winid = popup_create([
564 \ 'some text',
565 \ 'another line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200566 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200567 \ line: 1,
568 \ col: 10,
569 \ wrap: 0,
570 \ fixed: 1,
571 \ zindex: 90,
572 \ padding: [],
573 \ highlight: 'PopupColor',
574 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200575 call popup_create([
576 \ 'xxxxxxxxx',
577 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200578 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200579 \ line: 3,
580 \ col: 18,
581 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200582 let winidb = popup_create([
583 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200584 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200585 \ line: 7,
586 \ col: 10,
587 \ wrap: 0,
588 \ fixed: 1,
589 \ close: 'button',
590 \ zindex: 90,
591 \ padding: [],
592 \ border: [],
593 \ 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 +0200594 END
595 call writefile(lines, 'XtestPopupMask')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200596 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200597 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
598
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200599 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
600 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200601 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200602 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
603
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200604 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
605 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200606 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200607 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
608
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200609 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
610 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200611 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200612 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
613
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200614 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
615 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200616 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200617 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
618
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200619 " clean up
620 call StopVimInTerminal(buf)
621 call delete('XtestPopupMask')
622endfunc
623
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200624func Test_popup_select()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200625 CheckScreendump
626 CheckFeature clipboard_working
627
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200628 " create a popup with some text to be selected
629 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200630 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200631 call setline(1, range(1, 20))
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200632 let winid = popup_create(['the word', 'some more', 'several words here', 'invisible', '5', '6', '7'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200633 \ drag: 1,
634 \ border: [],
635 \ line: 3,
636 \ col: 10,
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200637 \ maxheight: 3,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200638 \ })
639 func Select1()
640 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
641 endfunc
642 map <silent> <F3> :call test_setmouse(4, 15)<CR>
643 map <silent> <F4> :call test_setmouse(6, 23)<CR>
644 END
645 call writefile(lines, 'XtestPopupSelect')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200646 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200647 call term_sendkeys(buf, ":call Select1()\<CR>")
648 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
649
650 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
651 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200652 " clean the command line, sometimes it still shows a command
653 call term_sendkeys(buf, ":\<esc>")
654
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200655 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
656
657 " clean up
658 call StopVimInTerminal(buf)
659 call delete('XtestPopupSelect')
660endfunc
661
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200662func Test_popup_in_tab()
663 " default popup is local to tab, not visible when in other tab
664 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200665 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200666 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200667 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200668 tabnew
669 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200670 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200671 quit
672 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200673
674 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200675 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200676 " buffer is gone now
677 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200678
679 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200680 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200681 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200682 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200683 tabnew
684 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200685 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200686 quit
687 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200688 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200689
690 " create popup in other tab
691 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200692 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200693 call assert_equal(0, popup_getpos(winid).visible)
694 call assert_equal(1, popup_getoptions(winid).tabpage)
695 quit
696 call assert_equal(1, popup_getpos(winid).visible)
697 call assert_equal(0, popup_getoptions(winid).tabpage)
698 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200699endfunc
700
701func Test_popup_valid_arguments()
702 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200703 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200704 let pos = popup_getpos(winid)
705 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200706 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200707
708 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200709 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200710 let pos = popup_getpos(winid)
711 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200712 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200713
714 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200715 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200716 let pos = popup_getpos(winid)
717 let around = (&columns - pos.width) / 2
718 call assert_inrange(around - 1, around + 1, pos.col)
719 let around = (&lines - pos.height) / 2
720 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200721 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200722endfunc
723
724func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200725 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200726 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200727 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200728 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200729
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200730 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200731 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200732 call assert_fails('call popup_create("text", #{col: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200733 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200734 call assert_fails('call popup_create("text", #{col: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200735 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200736 call assert_fails('call popup_create("text", #{col: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200737 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200738
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200739 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200740 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200741 call assert_fails('call popup_create("text", #{line: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200742 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200743 call assert_fails('call popup_create("text", #{line: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200744 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200745 call assert_fails('call popup_create("text", #{line: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200746 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200747
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200748 call assert_fails('call popup_create("text", #{pos: "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200749 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200750 call assert_fails('call popup_create("text", #{padding: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200751 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200752 call assert_fails('call popup_create("text", #{border: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200753 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200754 call assert_fails('call popup_create("text", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200755 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +0200756 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
757 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200758 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200759 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200760
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200761 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200762 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200763 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200764 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200765 call assert_fails('call popup_create([#{text: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200766 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200767 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
768 call popup_clear()
769 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200770 call assert_fails('call popup_create("text", #{mapping: []})', 'E745:')
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200771 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200772endfunc
773
Bram Moolenaareea16992019-05-31 17:34:48 +0200774func Test_win_execute_closing_curwin()
775 split
776 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200777 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200778 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200779endfunc
780
781func Test_win_execute_not_allowed()
782 let winid = popup_create('some text', {})
783 call assert_fails('call win_execute(winid, "split")', 'E994:')
784 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
785 call assert_fails('call win_execute(winid, "close")', 'E994:')
786 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200787 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200788 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
789 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
790 call assert_fails('call win_execute(winid, "next")', 'E994:')
791 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
792 call assert_fails('call win_execute(winid, "buf")', 'E994:')
793 call assert_fails('call win_execute(winid, "edit")', 'E994:')
794 call assert_fails('call win_execute(winid, "enew")', 'E994:')
795 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
796 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
797 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
798 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200799 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200800endfunc
801
Bram Moolenaar402502d2019-05-30 22:07:36 +0200802func Test_popup_with_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200803 CheckScreendump
804
Bram Moolenaar402502d2019-05-30 22:07:36 +0200805 let lines =<< trim END
806 call setline(1, range(1, 100))
807 let winid = popup_create(
808 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200809 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200810 END
811 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200812 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200813 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
814
815 " clean up
816 call StopVimInTerminal(buf)
817 call delete('XtestPopup')
818endfunc
819
820func Test_popup_without_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200821 CheckScreendump
822
Bram Moolenaar402502d2019-05-30 22:07:36 +0200823 let lines =<< trim END
824 call setline(1, range(1, 100))
825 let winid = popup_create(
826 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200827 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200828 END
829 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200830 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200831 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
832
833 " clean up
834 call StopVimInTerminal(buf)
835 call delete('XtestPopup')
836endfunc
837
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200838func Test_popup_with_showbreak()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200839 CheckScreendump
840
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200841 let lines =<< trim END
842 set showbreak=>>\
843 call setline(1, range(1, 20))
844 let winid = popup_dialog(
Bram Moolenaar8ae54372019-09-15 18:11:16 +0200845 \ 'a long line here that wraps',
846 \ #{filter: 'popup_filter_yesno',
847 \ maxwidth: 12})
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200848 END
849 call writefile(lines, 'XtestPopupShowbreak')
850 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
851 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
852
853 " clean up
854 call term_sendkeys(buf, "y")
855 call StopVimInTerminal(buf)
856 call delete('XtestPopupShowbreak')
857endfunc
858
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200859func Test_popup_time()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200860 CheckFeature timers
861
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200862 topleft vnew
863 call setline(1, 'hello')
864
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200865 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200866 \ line: 1,
867 \ col: 1,
868 \ minwidth: 20,
869 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200870 \})
871 redraw
872 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
873 call assert_equal('world', line)
874
Bram Moolenaarb4f06282019-07-12 21:07:54 +0200875 call assert_equal(winid, popup_locate(1, 1))
876 call assert_equal(winid, popup_locate(1, 20))
877 call assert_equal(0, popup_locate(1, 21))
878 call assert_equal(0, popup_locate(2, 1))
879
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200880 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200881 redraw
Bram Moolenaar196b4662019-09-06 21:34:30 +0200882 let line = join(map(range(1, 5), '1->screenstring(v:val)'), '')
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200883 call assert_equal('hello', line)
884
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200885 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200886 \ line: &lines,
887 \ col: 10,
888 \ minwidth: 20,
889 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200890 \})
891 redraw
892 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
893 call assert_match('.*on the command line.*', line)
894
895 sleep 700m
896 redraw
897 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
898 call assert_notmatch('.*on the command line.*', line)
899
900 bwipe!
901endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200902
903func Test_popup_hide()
904 topleft vnew
905 call setline(1, 'hello')
906
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200907 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200908 \ line: 1,
909 \ col: 1,
910 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200911 \})
912 redraw
913 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
914 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200915 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200916 " buffer is still listed and active
917 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200918
919 call popup_hide(winid)
920 redraw
921 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
922 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200923 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200924 " buffer is still listed but hidden
925 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200926
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200927 eval winid->popup_show()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200928 redraw
929 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
930 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200931 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200932
933
934 call popup_close(winid)
935 redraw
936 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
937 call assert_equal('hello', line)
938
939 " error is given for existing non-popup window
940 call assert_fails('call popup_hide(win_getid())', 'E993:')
941
942 " no error non-existing window
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200943 eval 1234234->popup_hide()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200944 call popup_show(41234234)
945
946 bwipe!
947endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200948
949func Test_popup_move()
950 topleft vnew
951 call setline(1, 'hello')
952
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200953 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200954 \ line: 1,
955 \ col: 1,
956 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200957 \})
958 redraw
959 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
960 call assert_equal('world ', line)
961
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200962 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200963 redraw
964 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
965 call assert_equal('hello ', line)
966 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
967 call assert_equal('~world', line)
968
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200969 eval winid->popup_move(#{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200970 redraw
971 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
972 call assert_equal('hworld', line)
973
974 call popup_close(winid)
975
976 bwipe!
977endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200978
Bram Moolenaar402502d2019-05-30 22:07:36 +0200979func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200980 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200981 \ line: 2,
982 \ col: 3,
983 \ minwidth: 10,
984 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +0200985 \})
986 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200987 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200988 call assert_equal(2, res.line)
989 call assert_equal(3, res.col)
990 call assert_equal(10, res.width)
991 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200992 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200993
994 call popup_close(winid)
995endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200996
997func Test_popup_width_longest()
998 let tests = [
999 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
1000 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
1001 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
1002 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
1003 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
1004 \ ]
1005
1006 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001007 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001008 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001009 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001010 call assert_equal(test[1], position.width)
1011 call popup_close(winid)
1012 endfor
1013endfunc
1014
1015func Test_popup_wraps()
1016 let tests = [
1017 \ ['nowrap', 6, 1],
1018 \ ['a line that wraps once', 12, 2],
1019 \ ['a line that wraps two times', 12, 3],
1020 \ ]
1021 for test in tests
1022 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001023 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001024 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001025 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001026 call assert_equal(test[1], position.width)
1027 call assert_equal(test[2], position.height)
1028
1029 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001030 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001031 endfor
1032endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001033
1034func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001035 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001036 \ line: 2,
1037 \ col: 3,
1038 \ minwidth: 10,
1039 \ minheight: 11,
1040 \ maxwidth: 20,
1041 \ maxheight: 21,
1042 \ zindex: 100,
1043 \ time: 5000,
1044 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001045 \})
1046 redraw
1047 let res = popup_getoptions(winid)
1048 call assert_equal(2, res.line)
1049 call assert_equal(3, res.col)
1050 call assert_equal(10, res.minwidth)
1051 call assert_equal(11, res.minheight)
1052 call assert_equal(20, res.maxwidth)
1053 call assert_equal(21, res.maxheight)
1054 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001055 call assert_equal(1, res.fixed)
Bram Moolenaarb8350ab2019-08-04 17:59:49 +02001056 call assert_equal(1, res.mapping)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001057 if has('timers')
1058 call assert_equal(5000, res.time)
1059 endif
1060 call popup_close(winid)
1061
1062 let winid = popup_create('hello', {})
1063 redraw
1064 let res = popup_getoptions(winid)
1065 call assert_equal(0, res.line)
1066 call assert_equal(0, res.col)
1067 call assert_equal(0, res.minwidth)
1068 call assert_equal(0, res.minheight)
1069 call assert_equal(0, res.maxwidth)
1070 call assert_equal(0, res.maxheight)
1071 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001072 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001073 if has('timers')
1074 call assert_equal(0, res.time)
1075 endif
1076 call popup_close(winid)
1077 call assert_equal({}, popup_getoptions(winid))
1078endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001079
1080func Test_popup_option_values()
1081 new
1082 " window-local
1083 setlocal number
1084 setlocal nowrap
1085 " buffer-local
1086 setlocal omnifunc=Something
1087 " global/buffer-local
1088 setlocal path=/there
1089 " global/window-local
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001090 setlocal statusline=2
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001091
1092 let winid = popup_create('hello', {})
1093 call assert_equal(0, getwinvar(winid, '&number'))
1094 call assert_equal(1, getwinvar(winid, '&wrap'))
1095 call assert_equal('', getwinvar(winid, '&omnifunc'))
1096 call assert_equal(&g:path, getwinvar(winid, '&path'))
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001097 call assert_equal(&g:statusline, getwinvar(winid, '&statusline'))
1098
1099 " 'scrolloff' is reset to zero
1100 call assert_equal(5, &scrolloff)
1101 call assert_equal(0, getwinvar(winid, '&scrolloff'))
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001102
1103 call popup_close(winid)
1104 bwipe
1105endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001106
1107func Test_popup_atcursor()
1108 topleft vnew
1109 call setline(1, [
1110 \ 'xxxxxxxxxxxxxxxxx',
1111 \ 'xxxxxxxxxxxxxxxxx',
1112 \ 'xxxxxxxxxxxxxxxxx',
1113 \])
1114
1115 call cursor(2, 2)
1116 redraw
1117 let winid = popup_atcursor('vim', {})
1118 redraw
1119 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1120 call assert_equal('xvimxxxxxxxxxxxxx', line)
1121 call popup_close(winid)
1122
1123 call cursor(3, 4)
1124 redraw
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001125 let winid = 'vim'->popup_atcursor({})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001126 redraw
1127 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
1128 call assert_equal('xxxvimxxxxxxxxxxx', line)
1129 call popup_close(winid)
1130
1131 call cursor(1, 1)
1132 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001133 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001134 \ line: 'cursor+2',
1135 \ col: 'cursor+1',
1136 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001137 redraw
1138 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1139 call assert_equal('xvimxxxxxxxxxxxxx', line)
1140 call popup_close(winid)
1141
1142 call cursor(3, 3)
1143 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001144 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001145 \ line: 'cursor-2',
1146 \ col: 'cursor-1',
1147 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001148 redraw
1149 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1150 call assert_equal('xvimxxxxxxxxxxxxx', line)
1151 call popup_close(winid)
1152
Bram Moolenaar402502d2019-05-30 22:07:36 +02001153 " just enough room above
1154 call cursor(3, 3)
1155 redraw
1156 let winid = popup_atcursor(['vim', 'is great'], {})
1157 redraw
1158 let pos = popup_getpos(winid)
1159 call assert_equal(1, pos.line)
1160 call popup_close(winid)
1161
1162 " not enough room above, popup goes below the cursor
1163 call cursor(3, 3)
1164 redraw
1165 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1166 redraw
1167 let pos = popup_getpos(winid)
1168 call assert_equal(4, pos.line)
1169 call popup_close(winid)
1170
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001171 " cursor in first line, popup in line 2
1172 call cursor(1, 1)
1173 redraw
1174 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1175 redraw
1176 let pos = popup_getpos(winid)
1177 call assert_equal(2, pos.line)
1178 call popup_close(winid)
1179
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001180 bwipe!
1181endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001182
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001183func Test_popup_beval()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001184 CheckScreendump
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001185 CheckFeature balloon_eval_term
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001186
1187 let lines =<< trim END
1188 call setline(1, range(1, 20))
1189 call setline(5, 'here is some text to hover over')
1190 set balloonevalterm
1191 set balloonexpr=BalloonExpr()
1192 set balloondelay=100
1193 func BalloonExpr()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001194 let s:winid = [v:beval_text]->popup_beval({})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001195 return ''
1196 endfunc
1197 func Hover()
1198 call test_setmouse(5, 15)
1199 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1200 sleep 100m
1201 endfunc
1202 func MoveOntoPopup()
1203 call test_setmouse(4, 17)
1204 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1205 endfunc
1206 func MoveAway()
1207 call test_setmouse(5, 13)
1208 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1209 endfunc
1210 END
1211 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001212 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001213 call term_wait(buf, 100)
1214 call term_sendkeys(buf, 'j')
1215 call term_sendkeys(buf, ":call Hover()\<CR>")
1216 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1217
1218 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1219 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1220
1221 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1222 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1223
1224 " clean up
1225 call StopVimInTerminal(buf)
1226 call delete('XtestPopupBeval')
1227endfunc
1228
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001229func Test_popup_filter()
1230 new
1231 call setline(1, 'some text')
1232
1233 func MyPopupFilter(winid, c)
1234 if a:c == 'e'
1235 let g:eaten = 'e'
1236 return 1
1237 endif
1238 if a:c == '0'
1239 let g:ignored = '0'
1240 return 0
1241 endif
1242 if a:c == 'x'
1243 call popup_close(a:winid)
1244 return 1
1245 endif
1246 return 0
1247 endfunc
1248
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001249 let winid = 'something'->popup_create(#{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001250 redraw
1251
1252 " e is consumed by the filter
1253 call feedkeys('e', 'xt')
1254 call assert_equal('e', g:eaten)
1255
1256 " 0 is ignored by the filter
1257 normal $
1258 call assert_equal(9, getcurpos()[2])
1259 call feedkeys('0', 'xt')
1260 call assert_equal('0', g:ignored)
1261 call assert_equal(1, getcurpos()[2])
1262
1263 " x closes the popup
1264 call feedkeys('x', 'xt')
1265 call assert_equal('e', g:eaten)
1266 call assert_equal(-1, winbufnr(winid))
1267
1268 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001269 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001270endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001271
Bram Moolenaara42d9452019-06-15 21:46:30 +02001272func ShowDialog(key, result)
1273 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001274 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001275 \ filter: 'popup_filter_yesno',
1276 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001277 \ })
1278 redraw
1279 call feedkeys(a:key, "xt")
1280 call assert_equal(winid, s:cb_winid)
1281 call assert_equal(a:result, s:cb_res)
1282endfunc
1283
1284func Test_popup_dialog()
1285 func QuitCallback(id, res)
1286 let s:cb_winid = a:id
1287 let s:cb_res = a:res
1288 endfunc
1289
1290 let winid = ShowDialog("y", 1)
1291 let winid = ShowDialog("Y", 1)
1292 let winid = ShowDialog("n", 0)
1293 let winid = ShowDialog("N", 0)
1294 let winid = ShowDialog("x", 0)
1295 let winid = ShowDialog("X", 0)
1296 let winid = ShowDialog("\<Esc>", 0)
1297 let winid = ShowDialog("\<C-C>", -1)
1298
1299 delfunc QuitCallback
1300endfunc
1301
Bram Moolenaara730e552019-06-16 19:05:31 +02001302func ShowMenu(key, result)
1303 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001304 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001305 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001306 \ })
1307 redraw
1308 call feedkeys(a:key, "xt")
1309 call assert_equal(winid, s:cb_winid)
1310 call assert_equal(a:result, s:cb_res)
1311endfunc
1312
1313func Test_popup_menu()
1314 func QuitCallback(id, res)
1315 let s:cb_winid = a:id
1316 let s:cb_res = a:res
1317 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001318 " mapping won't be used in popup
1319 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001320
1321 let winid = ShowMenu(" ", 1)
1322 let winid = ShowMenu("j \<CR>", 2)
1323 let winid = ShowMenu("JjK \<CR>", 2)
1324 let winid = ShowMenu("jjjjjj ", 3)
1325 let winid = ShowMenu("kkk ", 1)
1326 let winid = ShowMenu("x", -1)
1327 let winid = ShowMenu("X", -1)
1328 let winid = ShowMenu("\<Esc>", -1)
1329 let winid = ShowMenu("\<C-C>", -1)
1330
1331 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001332 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001333endfunc
1334
1335func Test_popup_menu_screenshot()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001336 CheckScreendump
Bram Moolenaara730e552019-06-16 19:05:31 +02001337
1338 let lines =<< trim END
1339 call setline(1, range(1, 20))
1340 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001341 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001342 func MenuDone(id, res)
1343 echomsg "selected " .. a:res
1344 endfunc
1345 END
1346 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001347 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001348 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1349
1350 call term_sendkeys(buf, "jj")
1351 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1352
1353 call term_sendkeys(buf, " ")
1354 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1355
1356 " clean up
1357 call StopVimInTerminal(buf)
1358 call delete('XtestPopupMenu')
1359endfunc
1360
Bram Moolenaarf914a332019-07-20 15:09:56 +02001361func Test_popup_menu_narrow()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001362 CheckScreendump
Bram Moolenaarf914a332019-07-20 15:09:56 +02001363
1364 let lines =<< trim END
1365 call setline(1, range(1, 20))
1366 hi PopupSelected ctermbg=green
1367 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1368 func MenuDone(id, res)
1369 echomsg "selected " .. a:res
1370 endfunc
1371 END
1372 call writefile(lines, 'XtestPopupNarrowMenu')
1373 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1374 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1375
1376 " clean up
1377 call term_sendkeys(buf, "x")
1378 call StopVimInTerminal(buf)
1379 call delete('XtestPopupNarrowMenu')
1380endfunc
1381
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001382func Test_popup_title()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001383 CheckScreendump
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001384
1385 " Create a popup without title or border, a line of padding will be added to
1386 " put the title on.
1387 let lines =<< trim END
1388 call setline(1, range(1, 20))
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001389 let winid = popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001390 END
1391 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001392 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001393 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1394
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001395 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxwidth: 20, title: 'a very long title that is not going to fit'})\<CR>")
1396 call term_sendkeys(buf, ":\<CR>")
1397 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_1', {})
1398
1399 call term_sendkeys(buf, ":call popup_setoptions(winid, #{border: []})\<CR>")
1400 call term_sendkeys(buf, ":\<CR>")
1401 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_2', {})
1402
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001403 " clean up
1404 call StopVimInTerminal(buf)
1405 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001406
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001407 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001408 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001409 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001410 call assert_equal('Another Title', popup_getoptions(winid).title)
1411
1412 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001413endfunc
1414
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001415func Test_popup_close_callback()
1416 func PopupDone(id, result)
1417 let g:result = a:result
1418 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001419 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001420 redraw
1421 call popup_close(winid, 'done')
1422 call assert_equal('done', g:result)
1423endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001424
1425func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001426 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001427 redraw
1428 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001429 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001430 call assert_equal(5, pos.height)
1431
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001432 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001433 redraw
1434 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001435 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001436 call assert_equal(3, pos.height)
1437endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001438
1439func Test_popup_never_behind()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001440 CheckScreendump
1441
Bram Moolenaar988c4332019-06-02 14:12:11 +02001442 " +-----------------------------+
1443 " | | |
1444 " | | |
1445 " | | |
1446 " | line1 |
1447 " |------------line2------------|
1448 " | line3 |
1449 " | line4 |
1450 " | |
1451 " | |
1452 " +-----------------------------+
1453 let lines =<< trim END
Bram Moolenaar988c4332019-06-02 14:12:11 +02001454 split
1455 vsplit
1456 let info_window1 = getwininfo()[0]
1457 let line = info_window1['height']
1458 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001459 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001460 \ line : line,
1461 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001462 \ })
1463 END
1464 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001465 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001466 call term_sendkeys(buf, "\<C-W>w")
1467 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1468
1469 " clean up
1470 call StopVimInTerminal(buf)
1471 call delete('XtestPopupBehind')
1472endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001473
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001474func s:VerifyPosition(p, msg, line, col, width, height)
1475 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1476 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1477 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1478 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001479endfunc
1480
1481func Test_popup_position_adjust()
1482 " Anything placed past 2 cells from of the right of the screen is moved to the
1483 " left.
1484 "
1485 " When wrapping is disabled, we also shift to the left to display on the
1486 " screen, unless fixed is set.
1487
1488 " Entries for cases which don't vary based on wrapping.
1489 " Format is per tests described below
1490 let both_wrap_tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001491 \ ['a', 5, &columns, 5, &columns - 2, 1, 1],
1492 \ ['b', 5, &columns + 1, 5, &columns - 2, 1, 1],
1493 \ ['c', 5, &columns - 1, 5, &columns - 2, 1, 1],
1494 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
1495 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1],
1496 \
1497 \ ['aa', 5, &columns, 5, &columns - 2, 2, 1],
1498 \ ['bb', 5, &columns + 1, 5, &columns - 2, 2, 1],
1499 \ ['cc', 5, &columns - 1, 5, &columns - 2, 2, 1],
1500 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1501 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1502 \
1503 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
1504 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1505 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1506 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1507 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1508 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001509
1510 " these test groups are dicts with:
1511 " - comment: something to identify the group of tests by
1512 " - options: dict of options to merge with the row/col in tests
1513 " - tests: list of cases. Each one is a list with elements:
1514 " - text
1515 " - row
1516 " - col
1517 " - expected row
1518 " - expected col
1519 " - expected width
1520 " - expected height
1521 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001522 \ #{
1523 \ comment: 'left-aligned with wrapping',
1524 \ options: #{
1525 \ wrap: 1,
1526 \ pos: 'botleft',
1527 \ },
1528 \ tests: both_wrap_tests + [
1529 \ ['aaaa', 5, &columns, 4, &columns - 2, 3, 2],
1530 \ ['bbbb', 5, &columns + 1, 4, &columns - 2, 3, 2],
1531 \ ['cccc', 5, &columns - 1, 4, &columns - 2, 3, 2],
1532 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
1533 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1534 \ ],
1535 \ },
1536 \ #{
1537 \ comment: 'left aligned without wrapping',
1538 \ options: #{
1539 \ wrap: 0,
1540 \ pos: 'botleft',
1541 \ },
1542 \ tests: both_wrap_tests + [
1543 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
1544 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
1545 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
1546 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
1547 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1548 \ ],
1549 \ },
1550 \ #{
1551 \ comment: 'left aligned with fixed position',
1552 \ options: #{
1553 \ wrap: 0,
1554 \ fixed: 1,
1555 \ pos: 'botleft',
1556 \ },
1557 \ tests: both_wrap_tests + [
1558 \ ['aaaa', 5, &columns, 5, &columns - 2, 3, 1],
1559 \ ['bbbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1560 \ ['cccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1561 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1562 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1563 \ ],
1564 \ },
1565 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001566
1567 for test_group in tests
1568 for test in test_group.tests
1569 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001570 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001571 \ line: line,
1572 \ col: col,
1573 \ }
1574 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001575
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001576 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001577
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001578 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001579 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
1580 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001581 endfor
1582 endfor
1583
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001584 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001585 %bwipe!
1586endfunc
1587
Bram Moolenaar3397f742019-06-02 18:40:06 +02001588func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001589 " width of screen
1590 let X = join(map(range(&columns), {->'X'}), '')
1591
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001592 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1593 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001594
1595 redraw
1596 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1597 call assert_equal(X, line)
1598
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001599 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001600 redraw
1601
1602 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001603 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
1604 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001605
1606 redraw
1607 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1608 call assert_equal(X, line)
1609
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001610 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001611 redraw
1612
1613 " Extend so > window width
1614 let X .= 'x'
1615
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001616 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1617 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001618
1619 redraw
1620 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1621 call assert_equal(X[ : -2 ], line)
1622
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001623 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001624 redraw
1625
1626 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001627 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
1628 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001629
1630 redraw
1631 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1632 call assert_equal(X[ : -2 ], line)
1633
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001634 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001635 redraw
1636
1637 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001638 let p = popup_create(X,
1639 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
1640 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001641
1642 redraw
1643 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1644 let e_line = ' ' . X[ 1 : -2 ]
1645 call assert_equal(e_line, line)
1646
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001647 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001648 redraw
1649
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001650 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001651 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001652endfunc
1653
1654func Test_popup_moved()
1655 new
1656 call test_override('char_avail', 1)
1657 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1658
1659 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001660 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001661 redraw
1662 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001663 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001664 " trigger the check for last_cursormoved by going into insert mode
1665 call feedkeys("li\<Esc>", 'xt')
1666 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001667 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001668
1669 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001670 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001671 redraw
1672 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001673 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001674 call feedkeys("hi\<Esc>", 'xt')
1675 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001676 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001677
1678 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001679 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001680 redraw
1681 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001682 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001683 call feedkeys("li\<Esc>", 'xt')
1684 call assert_equal(1, popup_getpos(winid).visible)
1685 call feedkeys("ei\<Esc>", 'xt')
1686 call assert_equal(1, popup_getpos(winid).visible)
1687 call feedkeys("eli\<Esc>", 'xt')
1688 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001689 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001690
Bram Moolenaar17627312019-06-02 19:53:44 +02001691 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001692 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001693 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001694 redraw
1695 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001696 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001697 call feedkeys("eli\<Esc>", 'xt')
1698 call assert_equal(1, popup_getpos(winid).visible)
1699 call feedkeys("wi\<Esc>", 'xt')
1700 call assert_equal(1, popup_getpos(winid).visible)
1701 call feedkeys("Eli\<Esc>", 'xt')
1702 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001703 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001704
1705 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001706 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001707 redraw
1708 call assert_equal(1, popup_getpos(winid).visible)
1709 call feedkeys("eli\<Esc>", 'xt')
1710 call feedkeys("ei\<Esc>", 'xt')
1711 call assert_equal(1, popup_getpos(winid).visible)
1712 call feedkeys("eli\<Esc>", 'xt')
1713 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001714 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001715
1716 bwipe!
1717 call test_override('ALL', 0)
1718endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001719
1720func Test_notifications()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001721 CheckFeature timers
1722 CheckScreendump
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001723
Bram Moolenaar0fdddee2019-09-01 15:26:23 +02001724 let lines =<< trim END
1725 call setline(1, range(1, 20))
1726 hi Notification ctermbg=lightblue
1727 call popup_notification('first notification', {})
1728 END
1729 call writefile(lines, 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001730 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001731 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1732
1733 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001734 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1735 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001736 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1737
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001738 " clean up
1739 call StopVimInTerminal(buf)
1740 call delete('XtestNotifications')
1741endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001742
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001743func Test_popup_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001744 CheckScreendump
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001745
1746 let lines =<< trim END
1747 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02001748 hi ScrollThumb ctermbg=blue
1749 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001750 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001751 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001752 \ minwidth: 8,
1753 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001754 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001755 func ScrollUp()
1756 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
1757 endfunc
1758 func ScrollDown()
1759 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
1760 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001761 func ClickTop()
1762 call feedkeys("\<F4>\<LeftMouse>", "xt")
1763 endfunc
1764 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001765 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001766 call feedkeys("\<F5>\<LeftMouse>", "xt")
1767 endfunc
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001768 func Popup_filter(winid, key)
1769 if a:key == 'j'
1770 let line = popup_getoptions(a:winid).firstline
1771 let nlines = line('$', a:winid)
1772 let newline = line < nlines ? (line + 1) : nlines
1773 call popup_setoptions(a:winid, #{firstline: newline})
1774 return v:true
1775 elseif a:key == 'x'
1776 call popup_close(a:winid)
1777 return v:true
1778 endif
1779 endfunc
1780
1781 func PopupScroll()
1782 call popup_clear()
1783 let text =<< trim END
1784 1
1785 2
1786 3
1787 4
1788 long line long line long line long line long line long line
1789 long line long line long line long line long line long line
1790 long line long line long line long line long line long line
1791 END
1792 call popup_create(text, #{
1793 \ minwidth: 30,
1794 \ maxwidth: 30,
1795 \ minheight: 4,
1796 \ maxheight: 4,
1797 \ firstline: 1,
1798 \ wrap: v:true,
1799 \ scrollbar: v:true,
1800 \ mapping: v:false,
1801 \ filter: funcref('Popup_filter')
1802 \ })
1803 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001804 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001805 map <silent> <F4> :call test_setmouse(4, 42)<CR>
1806 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001807 END
1808 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001809 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001810 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
1811
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001812 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001813 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001814 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
1815
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001816 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001817 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001818 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
1819
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001820 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001821 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001822 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
1823
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02001824 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb', firstline: 5})\<CR>")
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001825 " this scrolls two lines (half the window height)
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001826 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
1827 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
1828
1829 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1830 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
1831
1832 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02001833 " wait a bit, otherwise it fails sometimes (double click recognized?)
1834 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001835 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1836 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
1837
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001838 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1839 sleep 100m
1840 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1841 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
1842
1843 call term_sendkeys(buf, ":call ClickBot()\<CR>")
1844 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
1845
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001846 " remove the minwidth and maxheight
1847 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxheight: 0, minwidth: 0})\<CR>")
Bram Moolenaar7e0f4622019-09-17 21:23:39 +02001848 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001849 call VerifyScreenDump(buf, 'Test_popupwin_scroll_10', {})
1850
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001851 " check size with non-wrapping lines
1852 call term_sendkeys(buf, ":call PopupScroll()\<CR>")
1853 call VerifyScreenDump(buf, 'Test_popupwin_scroll_11', {})
1854
1855 " check size with wrapping lines
1856 call term_sendkeys(buf, "j")
1857 call VerifyScreenDump(buf, 'Test_popupwin_scroll_12', {})
1858 call term_sendkeys(buf, "x")
1859
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001860 " clean up
1861 call StopVimInTerminal(buf)
1862 call delete('XtestPopupScroll')
1863endfunc
1864
Bram Moolenaar437a7462019-07-05 20:17:22 +02001865func Test_popup_fitting_scrollbar()
1866 " this was causing a crash, divide by zero
1867 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001868 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001869 \ scrollbar: 1,
1870 \ maxwidth: 10,
1871 \ maxheight: 5,
1872 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02001873 redraw
1874 call popup_clear()
1875endfunc
1876
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001877func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001878 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001879
1880 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001881 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001882 let p = popup_create('test', opts)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001883 eval p->popup_settext('this is a text')
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001884 END
1885
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001886 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001887 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001888 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
1889
1890 " Setting to empty string clears it
1891 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
1892 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
1893
1894 " Setting a list
1895 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1896 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1897
1898 " Shrinking with a list
1899 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
1900 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
1901
1902 " Growing with a list
1903 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1904 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1905
1906 " Empty list clears
1907 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
1908 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
1909
1910 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001911 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001912 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
1913
1914 " clean up
1915 call StopVimInTerminal(buf)
1916 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001917endfunc
1918
1919func Test_popup_hidden()
1920 new
1921
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001922 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001923 redraw
1924 call assert_equal(0, popup_getpos(winid).visible)
1925 call popup_close(winid)
1926
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001927 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001928 redraw
1929 call assert_equal(0, popup_getpos(winid).visible)
1930 call popup_close(winid)
1931
1932 func QuitCallback(id, res)
1933 let s:cb_winid = a:id
1934 let s:cb_res = a:res
1935 endfunc
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001936 let winid = 'make a choice'->popup_dialog(#{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001937 \ filter: 'popup_filter_yesno',
1938 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001939 \ })
1940 redraw
1941 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001942 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
1943 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001944 exe "normal anot used by filter\<Esc>"
1945 call assert_equal('not used by filter', getline(1))
1946
1947 call popup_show(winid)
1948 call feedkeys('y', "xt")
1949 call assert_equal(1, s:cb_res)
1950
1951 bwipe!
1952 delfunc QuitCallback
1953endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02001954
1955" Test options not checked elsewhere
1956func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001957 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001958 let options = popup_getoptions(winid)
1959 call assert_equal(1, options.wrap)
1960 call assert_equal(0, options.drag)
1961 call assert_equal('Beautiful', options.highlight)
1962
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001963 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001964 let options = popup_getoptions(winid)
1965 call assert_equal(0, options.wrap)
1966 call assert_equal(1, options.drag)
1967 call assert_equal('Another', options.highlight)
1968
1969 call popup_close(winid)
1970endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001971
1972func Test_popupwin_garbage_collect()
1973 func MyPopupFilter(x, winid, c)
1974 " NOP
1975 endfunc
1976
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001977 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001978 call test_garbagecollect_now()
1979 redraw
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001980 " Must not crash caused by invalid memory access
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001981 call feedkeys('j', 'xt')
1982 call assert_true(v:true)
1983
1984 call popup_close(winid)
1985 delfunc MyPopupFilter
1986endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001987
Bram Moolenaar581ba392019-09-03 22:08:33 +02001988func Test_popupwin_filter_mode()
1989 func MyPopupFilter(winid, c)
1990 let s:typed = a:c
1991 if a:c == ':' || a:c == "\r" || a:c == 'v'
1992 " can start cmdline mode, get out, and start/stop Visual mode
1993 return 0
1994 endif
1995 return 1
1996 endfunc
1997
1998 " Normal, Visual and Insert mode
1999 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'nvi'})
2000 redraw
2001 call feedkeys('x', 'xt')
2002 call assert_equal('x', s:typed)
2003
2004 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2005 call assert_equal(':', s:typed)
2006 call assert_equal('foo', g:foo)
2007
2008 let @x = 'something'
2009 call feedkeys('v$"xy', 'xt')
2010 call assert_equal('y', s:typed)
2011 call assert_equal('something', @x) " yank command is filtered out
2012 call feedkeys('v', 'xt') " end Visual mode
2013
2014 call popup_close(winid)
2015
2016 " only Normal mode
2017 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'n'})
2018 redraw
2019 call feedkeys('x', 'xt')
2020 call assert_equal('x', s:typed)
2021
2022 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2023 call assert_equal(':', s:typed)
2024 call assert_equal('foo', g:foo)
2025
2026 let @x = 'something'
2027 call feedkeys('v$"xy', 'xt')
2028 call assert_equal('v', s:typed)
2029 call assert_notequal('something', @x)
2030
2031 call popup_close(winid)
2032
2033 " default: all modes
2034 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
2035 redraw
2036 call feedkeys('x', 'xt')
2037 call assert_equal('x', s:typed)
2038
2039 let g:foo = 'bar'
2040 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2041 call assert_equal("\r", s:typed)
2042 call assert_equal('bar', g:foo)
2043
2044 let @x = 'something'
2045 call feedkeys('v$"xy', 'xt')
2046 call assert_equal('y', s:typed)
2047 call assert_equal('something', @x) " yank command is filtered out
2048 call feedkeys('v', 'xt') " end Visual mode
2049
2050 call popup_close(winid)
2051 delfunc MyPopupFilter
2052endfunc
2053
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002054func Test_popupwin_with_buffer()
2055 call writefile(['some text', 'in a buffer'], 'XsomeFile')
2056 let buf = bufadd('XsomeFile')
2057 call assert_equal(0, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002058
2059 setlocal number
2060 call setbufvar(buf, "&wrapmargin", 13)
2061
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002062 let winid = popup_create(buf, {})
2063 call assert_notequal(0, winid)
2064 let pos = popup_getpos(winid)
2065 call assert_equal(2, pos.height)
2066 call assert_equal(1, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002067
2068 " window-local option is set to default, buffer-local is not
2069 call assert_equal(0, getwinvar(winid, '&number'))
2070 call assert_equal(13, getbufvar(buf, '&wrapmargin'))
2071
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002072 call popup_close(winid)
2073 call assert_equal({}, popup_getpos(winid))
2074 call assert_equal(1, bufloaded(buf))
2075 exe 'bwipe! ' .. buf
Bram Moolenaar46451042019-08-24 15:50:46 +02002076 setlocal nonumber
Bram Moolenaar7866b872019-07-01 22:21:01 +02002077
2078 edit test_popupwin.vim
2079 let winid = popup_create(bufnr(''), {})
2080 redraw
2081 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002082 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002083endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02002084
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002085func Test_popupwin_terminal_buffer()
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002086 CheckFeature terminal
2087
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002088 let ptybuf = term_start(&shell, #{hidden: 1})
2089 call assert_fails('let winnr = popup_create(ptybuf, #{})', 'E278:')
2090 exe 'bwipe! ' .. ptybuf
2091endfunc
2092
Bram Moolenaar934470e2019-09-01 23:27:05 +02002093func Test_popupwin_with_buffer_and_filter()
2094 new Xwithfilter
2095 call setline(1, range(100))
2096 let bufnr = bufnr()
2097 hide
2098
2099 func BufferFilter(win, key)
2100 if a:key == 'G'
2101 " recursive use of "G" does not cause problems.
2102 call win_execute(a:win, 'normal! G')
2103 return 1
2104 endif
2105 return 0
2106 endfunc
2107
2108 let winid = popup_create(bufnr, #{maxheight: 5, filter: 'BufferFilter'})
2109 call assert_equal(1, popup_getpos(winid).firstline)
2110 redraw
2111 call feedkeys("G", 'xt')
2112 call assert_equal(99, popup_getpos(winid).firstline)
2113
2114 call popup_close(winid)
2115 exe 'bwipe! ' .. bufnr
2116endfunc
2117
Bram Moolenaare296e312019-07-03 23:20:18 +02002118func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002119 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002120 \ maxwidth: 40,
2121 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02002122 \ })
2123 for top in range(1, 20)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002124 eval winid->popup_setoptions(#{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02002125 redraw
2126 call assert_equal(19, popup_getpos(winid).width)
2127 endfor
2128 call popup_clear()
2129endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02002130
2131func Test_popupwin_buf_close()
2132 let buf = bufadd('Xtestbuf')
2133 call bufload(buf)
2134 call setbufline(buf, 1, ['just', 'some', 'lines'])
2135 let winid = popup_create(buf, {})
2136 redraw
2137 call assert_equal(3, popup_getpos(winid).height)
2138 let bufinfo = getbufinfo(buf)[0]
2139 call assert_equal(1, bufinfo.changed)
2140 call assert_equal(0, bufinfo.hidden)
2141 call assert_equal(0, bufinfo.listed)
2142 call assert_equal(1, bufinfo.loaded)
2143 call assert_equal([], bufinfo.windows)
2144 call assert_equal([winid], bufinfo.popups)
2145
2146 call popup_close(winid)
2147 call assert_equal({}, popup_getpos(winid))
2148 let bufinfo = getbufinfo(buf)[0]
2149 call assert_equal(1, bufinfo.changed)
2150 call assert_equal(1, bufinfo.hidden)
2151 call assert_equal(0, bufinfo.listed)
2152 call assert_equal(1, bufinfo.loaded)
2153 call assert_equal([], bufinfo.windows)
2154 call assert_equal([], bufinfo.popups)
2155 exe 'bwipe! ' .. buf
2156endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02002157
2158func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002159 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02002160
2161 let lines =<< trim END
2162 call setline(1, range(1, 10))
2163 hi ScrollThumb ctermbg=blue
2164 hi ScrollBar ctermbg=red
2165 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002166 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002167 \ maxwidth: 10,
2168 \ maxheight: 3,
2169 \ pos : 'topleft',
2170 \ col : a:col,
2171 \ line : a:line,
2172 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02002173 \ })
2174 endfunc
2175 call PopupMenu(['x'], 1, 1)
2176 call PopupMenu(['123456789|'], 1, 16)
2177 call PopupMenu(['123456789|' .. ' '], 7, 1)
2178 call PopupMenu([repeat('123456789|', 100)], 7, 16)
2179 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
2180 END
2181 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002182 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02002183 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
2184
2185 " close the menu popupwin.
2186 call term_sendkeys(buf, " ")
2187 call term_sendkeys(buf, " ")
2188 call term_sendkeys(buf, " ")
2189 call term_sendkeys(buf, " ")
2190 call term_sendkeys(buf, " ")
2191
2192 " clean up
2193 call StopVimInTerminal(buf)
2194 call delete('XtestPopupMenuMaxWidth')
2195endfunc
2196
Bram Moolenaara901a372019-07-13 16:38:50 +02002197func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002198 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02002199
2200 let lines =<< trim END
2201 call setline(1, range(1, 20))
2202 hi ScrollThumb ctermbg=blue
2203 hi ScrollBar ctermbg=red
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002204 eval ['one', 'two', 'three', 'four', 'five',
2205 \ 'six', 'seven', 'eight', 'nine']
2206 \ ->popup_menu(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002207 \ minwidth: 8,
2208 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02002209 \ })
2210 END
2211 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002212 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02002213
2214 call term_sendkeys(buf, "j")
2215 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
2216
2217 call term_sendkeys(buf, "jjj")
2218 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
2219
2220 " if the cursor is the bottom line, it stays at the bottom line.
2221 call term_sendkeys(buf, repeat("j", 20))
2222 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
2223
2224 call term_sendkeys(buf, "kk")
2225 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
2226
2227 call term_sendkeys(buf, "k")
2228 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
2229
2230 " if the cursor is in the top line, it stays in the top line.
2231 call term_sendkeys(buf, repeat("k", 20))
2232 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
2233
2234 " close the menu popupwin.
2235 call term_sendkeys(buf, " ")
2236
2237 " clean up
2238 call StopVimInTerminal(buf)
2239 call delete('XtestPopupMenuScroll')
2240endfunc
2241
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002242func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002243 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002244
2245 let lines =<< trim END
2246 function! MyFilter(winid, key) abort
2247 if a:key == "0"
2248 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
2249 return 1
2250 endif
2251 if a:key == "G"
2252 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
2253 return 1
2254 endif
2255 if a:key == "j"
2256 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
2257 return 1
2258 endif
2259 if a:key == "k"
2260 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
2261 return 1
2262 endif
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002263 if a:key == ':'
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002264 call popup_close(a:winid)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002265 return 0
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002266 endif
2267 return 0
2268 endfunction
2269 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
2270 \ maxheight : 3,
2271 \ filter : 'MyFilter'
2272 \ })
2273 END
2274 call writefile(lines, 'XtestPopupMenuFilter')
2275 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
2276
2277 call term_sendkeys(buf, "j")
2278 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
2279
2280 call term_sendkeys(buf, "k")
2281 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
2282
2283 call term_sendkeys(buf, "G")
2284 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
2285
2286 call term_sendkeys(buf, "0")
2287 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
2288
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002289 " check that when the popup is closed in the filter the screen is redrawn
2290 call term_sendkeys(buf, ":")
2291 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_5', {})
2292 call term_sendkeys(buf, "\<CR>")
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002293
2294 " clean up
2295 call StopVimInTerminal(buf)
2296 call delete('XtestPopupMenuFilter')
2297endfunc
2298
2299func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002300 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002301
2302 let winid = popup_create('some text', {})
2303 call assert_equal(0, popup_getoptions(winid).cursorline)
2304 call popup_close(winid)
2305
2306 let winid = popup_create('some text', #{ cursorline: 1, })
2307 call assert_equal(1, popup_getoptions(winid).cursorline)
2308 call popup_close(winid)
2309
2310 let winid = popup_create('some text', #{ cursorline: 0, })
2311 call assert_equal(0, popup_getoptions(winid).cursorline)
2312 call popup_close(winid)
2313
2314 let winid = popup_menu('some text', {})
2315 call assert_equal(1, popup_getoptions(winid).cursorline)
2316 call popup_close(winid)
2317
2318 let winid = popup_menu('some text', #{ cursorline: 1, })
2319 call assert_equal(1, popup_getoptions(winid).cursorline)
2320 call popup_close(winid)
2321
2322 let winid = popup_menu('some text', #{ cursorline: 0, })
2323 call assert_equal(0, popup_getoptions(winid).cursorline)
2324 call popup_close(winid)
2325
2326 " ---------
2327 " Pattern 1
2328 " ---------
2329 let lines =<< trim END
2330 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
2331 END
2332 call writefile(lines, 'XtestPopupCursorLine')
2333 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2334 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
2335 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2336 call StopVimInTerminal(buf)
2337
2338 " ---------
2339 " Pattern 2
2340 " ---------
2341 let lines =<< trim END
2342 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
2343 END
2344 call writefile(lines, 'XtestPopupCursorLine')
2345 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2346 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
2347 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2348 call StopVimInTerminal(buf)
2349
2350 " ---------
2351 " Pattern 3
2352 " ---------
2353 let lines =<< trim END
2354 function! MyFilter(winid, key) abort
2355 if a:key == "j"
2356 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2357 return 1
2358 endif
2359 if a:key == 'x'
2360 call popup_close(a:winid)
2361 return 1
2362 endif
2363 return 0
2364 endfunction
2365 call popup_menu(['111', '222', '333'], #{
2366 \ cursorline : 0,
2367 \ maxheight : 2,
2368 \ filter : 'MyFilter',
2369 \ })
2370 END
2371 call writefile(lines, 'XtestPopupCursorLine')
2372 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2373 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
2374 call term_sendkeys(buf, "j")
2375 call term_sendkeys(buf, "j")
2376 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
2377 call term_sendkeys(buf, "x")
2378 call StopVimInTerminal(buf)
2379
2380 " ---------
2381 " Pattern 4
2382 " ---------
2383 let lines =<< trim END
2384 function! MyFilter(winid, key) abort
2385 if a:key == "j"
2386 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2387 return 1
2388 endif
2389 if a:key == 'x'
2390 call popup_close(a:winid)
2391 return 1
2392 endif
2393 return 0
2394 endfunction
2395 call popup_menu(['111', '222', '333'], #{
2396 \ cursorline : 1,
2397 \ maxheight : 2,
2398 \ filter : 'MyFilter',
2399 \ })
2400 END
2401 call writefile(lines, 'XtestPopupCursorLine')
2402 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2403 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
2404 call term_sendkeys(buf, "j")
2405 call term_sendkeys(buf, "j")
2406 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
2407 call term_sendkeys(buf, "x")
2408 call StopVimInTerminal(buf)
2409
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02002410 " ---------
2411 " Cursor in second line when creating the popup
2412 " ---------
2413 let lines =<< trim END
2414 let winid = popup_create(['111', '222', '333'], #{
2415 \ cursorline : 1,
2416 \ })
2417 call win_execute(winid, "2")
2418 END
2419 call writefile(lines, 'XtestPopupCursorLine')
2420 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2421 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_7', {})
2422 call StopVimInTerminal(buf)
2423
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002424 call delete('XtestPopupCursorLine')
2425endfunc
2426
Bram Moolenaarf914a332019-07-20 15:09:56 +02002427func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002428 CheckScreendump
2429
Bram Moolenaarf914a332019-07-20 15:09:56 +02002430 call writefile([
2431 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
2432 \ "another\tXtagfile\t/^this is another",
2433 \ "theword\tXtagfile\t/^theword"],
2434 \ 'Xtags')
2435 call writefile(range(1,20)
2436 \ + ['theword is here']
2437 \ + range(22, 27)
2438 \ + ['this is another place']
2439 \ + range(29, 40),
2440 \ "Xtagfile")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002441 call writefile(range(1,10)
2442 \ + ['searched word is here']
2443 \ + range(12, 20),
2444 \ "Xheader.h")
Bram Moolenaarf914a332019-07-20 15:09:56 +02002445 let lines =<< trim END
2446 set tags=Xtags
2447 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002448 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002449 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002450 \ 'three',
2451 \ 'four',
2452 \ 'five',
2453 \ 'six',
2454 \ 'seven',
2455 \ 'find theword somewhere',
2456 \ 'nine',
2457 \ 'this is another word',
2458 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02002459 set previewpopup=height:4,width:40
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002460 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02002461 END
2462 call writefile(lines, 'XtestPreviewPopup')
2463 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
2464
2465 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
2466 call term_sendkeys(buf, ":\<CR>")
2467 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
2468
2469 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2470 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
2471
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002472 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002473 call term_sendkeys(buf, ":\<CR>")
2474 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
2475
2476 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2477 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
2478
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002479 call term_sendkeys(buf, ":cd ..\<CR>:\<CR>")
2480 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002481 call term_sendkeys(buf, ":cd testdir\<CR>")
2482
2483 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02002484 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002485 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
2486
2487 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
2488 call term_sendkeys(buf, ":\<CR>")
2489 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
2490
2491 call term_sendkeys(buf, ":pclose\<CR>")
2492 call term_sendkeys(buf, ":psearch searched\<CR>")
2493 call term_sendkeys(buf, ":\<CR>")
2494 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002495
Bram Moolenaarf914a332019-07-20 15:09:56 +02002496 call StopVimInTerminal(buf)
2497 call delete('Xtags')
2498 call delete('Xtagfile')
2499 call delete('XtestPreviewPopup')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002500 call delete('Xheader.h')
Bram Moolenaarf914a332019-07-20 15:09:56 +02002501endfunc
2502
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002503func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002504 let lines =<< trim END
2505 set completeopt+=preview,popup
2506 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002507 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002508
2509 func CompleteFuncDict(findstart, base)
2510 if a:findstart
2511 if col('.') > 10
2512 return col('.') - 10
2513 endif
2514 return 0
2515 endif
2516
2517 return {
2518 \ 'words': [
2519 \ {
2520 \ 'word': 'aword',
2521 \ 'abbr': 'wrd',
2522 \ 'menu': 'extra text',
2523 \ 'info': 'words are cool',
2524 \ 'kind': 'W',
2525 \ 'user_data': 'test'
2526 \ },
2527 \ {
2528 \ 'word': 'anotherword',
2529 \ 'abbr': 'anotwrd',
2530 \ 'menu': 'extra text',
2531 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
2532 \ 'kind': 'W',
2533 \ 'user_data': 'notest'
2534 \ },
2535 \ {
2536 \ 'word': 'noinfo',
2537 \ 'abbr': 'noawrd',
2538 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002539 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002540 \ 'kind': 'W',
2541 \ 'user_data': 'notest'
2542 \ },
2543 \ {
2544 \ 'word': 'thatword',
2545 \ 'abbr': 'thatwrd',
2546 \ 'menu': 'extra text',
2547 \ 'info': 'that word is cool',
2548 \ 'kind': 'W',
2549 \ 'user_data': 'notest'
2550 \ },
2551 \ ]
2552 \ }
2553 endfunc
2554 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002555 func ChangeColor()
2556 let id = popup_findinfo()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002557 eval id->popup_setoptions(#{highlight: 'InfoPopup'})
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002558 endfunc
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002559
2560 func InfoHidden()
2561 set completepopup=height:4,border:off,align:menu
2562 set completeopt-=popup completeopt+=popuphidden
2563 au CompleteChanged * call HandleChange()
2564 endfunc
2565
2566 let s:counter = 0
2567 func HandleChange()
2568 let s:counter += 1
2569 let selected = complete_info(['selected']).selected
2570 if selected <= 0
2571 " First time: do nothing, info remains hidden
2572 return
2573 endif
2574 if selected == 1
2575 " Second time: show info right away
2576 let id = popup_findinfo()
2577 if id
2578 call popup_settext(id, 'immediate info ' .. s:counter)
2579 call popup_show(id)
2580 endif
2581 else
2582 " Third time: show info after a short delay
2583 call timer_start(100, 'ShowInfo')
2584 endif
2585 endfunc
2586
2587 func ShowInfo(...)
2588 let id = popup_findinfo()
2589 if id
2590 call popup_settext(id, 'async info ' .. s:counter)
2591 call popup_show(id)
2592 endif
2593 endfunc
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002594 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002595 return lines
2596endfunc
2597
2598func Test_popupmenu_info_border()
2599 CheckScreendump
2600
2601 let lines = Get_popupmenu_lines()
2602 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002603 call writefile(lines, 'XtestInfoPopup')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002604
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002605 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
2606 call term_wait(buf, 50)
2607
2608 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2609 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
2610
2611 call term_sendkeys(buf, "\<C-N>")
2612 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
2613
2614 call term_sendkeys(buf, "\<C-N>")
2615 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
2616
2617 call term_sendkeys(buf, "\<C-N>\<C-N>")
2618 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
2619
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02002620 " info on the left with scrollbar
2621 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
2622 call term_sendkeys(buf, "\<C-N>\<C-N>")
2623 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
2624
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002625 call StopVimInTerminal(buf)
2626 call delete('XtestInfoPopup')
2627endfunc
2628
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002629func Test_popupmenu_info_noborder()
2630 CheckScreendump
2631
2632 let lines = Get_popupmenu_lines()
2633 call add(lines, 'set completepopup=height:4,border:off')
2634 call writefile(lines, 'XtestInfoPopupNb')
2635
2636 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
2637 call term_wait(buf, 50)
2638
2639 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2640 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
2641
2642 call StopVimInTerminal(buf)
2643 call delete('XtestInfoPopupNb')
2644endfunc
2645
Bram Moolenaar258cef52019-08-21 17:29:29 +02002646func Test_popupmenu_info_align_menu()
2647 CheckScreendump
2648
2649 let lines = Get_popupmenu_lines()
2650 call add(lines, 'set completepopup=height:4,border:off,align:menu')
2651 call writefile(lines, 'XtestInfoPopupNb')
2652
2653 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
2654 call term_wait(buf, 50)
2655
2656 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2657 call term_sendkeys(buf, "\<C-N>")
2658 call term_sendkeys(buf, "\<C-N>")
2659 call term_sendkeys(buf, "\<C-N>")
2660 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
2661
2662 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
2663 call term_sendkeys(buf, "\<C-N>")
2664 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
2665
2666 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002667 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02002668 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
2669 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
2670 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
2671
2672 call StopVimInTerminal(buf)
2673 call delete('XtestInfoPopupNb')
2674endfunc
2675
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002676func Test_popupmenu_info_hidden()
2677 CheckScreendump
2678
2679 let lines = Get_popupmenu_lines()
2680 call add(lines, 'call InfoHidden()')
2681 call writefile(lines, 'XtestInfoPopupHidden')
2682
2683 let buf = RunVimInTerminal('-S XtestInfoPopupHidden', #{rows: 14})
2684 call term_wait(buf, 50)
2685
2686 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2687 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_1', {})
2688
2689 call term_sendkeys(buf, "\<C-N>")
2690 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_2', {})
2691
2692 call term_sendkeys(buf, "\<C-N>")
2693 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_3', {})
2694
2695 call term_sendkeys(buf, "\<Esc>")
2696 call StopVimInTerminal(buf)
2697 call delete('XtestInfoPopupHidden')
2698endfunc
2699
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002700func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02002701 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002702 call popup_clear()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002703 let winid = 'nothing wrong'->popup_notification({})
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002704 call assert_equal(bufnr, winbufnr(winid))
2705 call popup_clear()
2706endfunc
2707
Bram Moolenaar1824f452019-10-02 23:06:46 +02002708func Test_popupwin_getoptions_tablocal()
2709 topleft split
2710 let win1 = popup_create('nothing', #{maxheight: 8})
2711 let win2 = popup_create('something', #{maxheight: 10})
2712 let win3 = popup_create('something', #{maxheight: 15})
2713 call assert_equal(8, popup_getoptions(win1).maxheight)
2714 call assert_equal(10, popup_getoptions(win2).maxheight)
2715 call assert_equal(15, popup_getoptions(win3).maxheight)
2716 call popup_clear()
2717 quit
2718endfunc
2719
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02002720func Test_popupwin_cancel()
2721 let win1 = popup_create('one', #{line: 5, filter: {... -> 0}})
2722 let win2 = popup_create('two', #{line: 10, filter: {... -> 0}})
2723 let win3 = popup_create('three', #{line: 15, filter: {... -> 0}})
2724 call assert_equal(5, popup_getpos(win1).line)
2725 call assert_equal(10, popup_getpos(win2).line)
2726 call assert_equal(15, popup_getpos(win3).line)
2727 " TODO: this also works without patch 8.1.2110
2728 call feedkeys("\<C-C>", 'xt')
2729 call assert_equal(5, popup_getpos(win1).line)
2730 call assert_equal(10, popup_getpos(win2).line)
2731 call assert_equal({}, popup_getpos(win3))
2732 call feedkeys("\<C-C>", 'xt')
2733 call assert_equal(5, popup_getpos(win1).line)
2734 call assert_equal({}, popup_getpos(win2))
2735 call assert_equal({}, popup_getpos(win3))
2736 call feedkeys("\<C-C>", 'xt')
2737 call assert_equal({}, popup_getpos(win1))
2738 call assert_equal({}, popup_getpos(win2))
2739 call assert_equal({}, popup_getpos(win3))
2740endfunc
2741
Bram Moolenaar331bafd2019-07-20 17:46:05 +02002742" vim: shiftwidth=2 sts=2