blob: d5ee7160a13363b3dd225047cc203cb336c343d0 [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001" Tests for popup windows
2
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004CheckFeature popupwin
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005
6source screendump.vim
7
8func Test_simple_popup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02009 CheckScreendump
10
Bram Moolenaare7eb9272019-06-24 00:58:07 +020011 let lines =<< trim END
12 call setline(1, range(1, 100))
13 hi PopupColor1 ctermbg=lightblue
14 hi PopupColor2 ctermbg=lightcyan
Bram Moolenaar1666ac92019-11-10 17:22:31 +010015 hi EndOfBuffer ctermbg=lightgrey
Bram Moolenaare7eb9272019-06-24 00:58:07 +020016 hi Comment ctermfg=red
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020017 call prop_type_add('comment', #{highlight: 'Comment'})
18 let winid = popup_create('hello there', #{line: 3, col: 11, minwidth: 20, highlight: 'PopupColor1'})
19 let winid2 = popup_create(['another one', 'another two', 'another three'], #{line: 3, col: 25, minwidth: 20})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020020 call setwinvar(winid2, '&wincolor', 'PopupColor2')
21 END
22 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020023 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar4d784b22019-05-25 19:51:39 +020024 call VerifyScreenDump(buf, 'Test_popupwin_01', {})
25
Bram Moolenaarec583842019-05-26 14:11:23 +020026 " Add a tabpage
27 call term_sendkeys(buf, ":tabnew\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020028 call term_sendkeys(buf, ":let popupwin = popup_create(["
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020029 \ .. "#{text: 'other tab'},"
30 \ .. "#{text: 'a comment line', props: [#{"
Bram Moolenaard5abb4c2019-07-13 22:46:10 +020031 \ .. "col: 3, length: 7, minwidth: 20, type: 'comment'"
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020032 \ .. "}]},"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020033 \ .. "], #{line: 4, col: 9, minwidth: 20})\<CR>")
Bram Moolenaarec583842019-05-26 14:11:23 +020034 call VerifyScreenDump(buf, 'Test_popupwin_02', {})
35
36 " switch back to first tabpage
37 call term_sendkeys(buf, "gt")
38 call VerifyScreenDump(buf, 'Test_popupwin_03', {})
39
40 " close that tabpage
41 call term_sendkeys(buf, ":quit!\<CR>")
42 call VerifyScreenDump(buf, 'Test_popupwin_04', {})
43
Bram Moolenaar202d9822019-06-11 21:56:30 +020044 " set 'columns' to a small value, size must be recomputed
45 call term_sendkeys(buf, ":let cols = &columns\<CR>")
46 call term_sendkeys(buf, ":set columns=12\<CR>")
47 call VerifyScreenDump(buf, 'Test_popupwin_04a', {})
48 call term_sendkeys(buf, ":let &columns = cols\<CR>")
49
Bram Moolenaar17146962019-05-30 00:12:11 +020050 " resize popup, show empty line at bottom
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020051 call term_sendkeys(buf, ":call popup_move(popupwin, #{minwidth: 15, maxwidth: 25, minheight: 3, maxheight: 5})\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020052 call term_sendkeys(buf, ":redraw\<CR>")
53 call VerifyScreenDump(buf, 'Test_popupwin_05', {})
54
Bram Moolenaar17146962019-05-30 00:12:11 +020055 " show not fitting line at bottom
56 call term_sendkeys(buf, ":call setbufline(winbufnr(popupwin), 3, 'this line will not fit here')\<CR>")
57 call term_sendkeys(buf, ":redraw\<CR>")
58 call VerifyScreenDump(buf, 'Test_popupwin_06', {})
59
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020060 " move popup over ruler
61 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020062 call term_sendkeys(buf, ":call popup_move(popupwin, #{line: 7, col: 55})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +020063 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020064 call VerifyScreenDump(buf, 'Test_popupwin_07', {})
65
66 " clear all popups after moving the cursor a bit, so that ruler is updated
67 call term_sendkeys(buf, "axxx\<Esc>")
68 call term_wait(buf)
69 call term_sendkeys(buf, "0")
70 call term_wait(buf)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +020071 call term_sendkeys(buf, ":call popup_clear()\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020072 call VerifyScreenDump(buf, 'Test_popupwin_08', {})
73
Bram Moolenaar4d784b22019-05-25 19:51:39 +020074 " clean up
75 call StopVimInTerminal(buf)
76 call delete('XtestPopup')
77endfunc
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020078
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020079func Test_popup_with_border_and_padding()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +020080 CheckScreendump
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020081
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020082 for iter in range(0, 1)
Bram Moolenaare7eb9272019-06-24 00:58:07 +020083 let lines =<< trim END
84 call setline(1, range(1, 100))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020085 call popup_create('hello border', #{line: 2, col: 3, border: []})
86 call popup_create('hello padding', #{line: 2, col: 23, padding: []})
Bram Moolenaarc363fe12019-08-04 18:13:46 +020087 call popup_create('hello both', #{line: 2, col: 43, border: [], padding: [], highlight: 'Normal'})
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020088 call popup_create('border TL', #{line: 6, col: 3, border: [1, 0, 0, 4]})
89 call popup_create('paddings', #{line: 6, col: 23, padding: [1, 3, 2, 4]})
90 call popup_create('wrapped longer text', #{line: 8, col: 55, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
91 call popup_create('right aligned text', #{line: 11, col: 56, wrap: 0, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020092 END
93 call insert(lines, iter == 1 ? '' : 'set enc=latin1')
94 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020095 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 15})
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020096 call VerifyScreenDump(buf, 'Test_popupwin_2' .. iter, {})
97
98 call StopVimInTerminal(buf)
99 call delete('XtestPopupBorder')
100 endfor
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200101
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200102 let lines =<< trim END
103 call setline(1, range(1, 100))
104 hi BlueColor ctermbg=lightblue
105 hi TopColor ctermbg=253
106 hi RightColor ctermbg=245
107 hi BottomColor ctermbg=240
108 hi LeftColor ctermbg=248
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200109 call popup_create('hello border', #{line: 2, col: 3, border: [], borderhighlight: ['BlueColor']})
110 call popup_create(['hello border', 'and more'], #{line: 2, col: 23, border: [], borderhighlight: ['TopColor', 'RightColor', 'BottomColor', 'LeftColor']})
111 call popup_create(['hello border', 'lines only'], #{line: 2, col: 43, border: [], borderhighlight: ['BlueColor'], borderchars: ['x']})
112 call popup_create(['hello border', 'with corners'], #{line: 2, col: 60, border: [], borderhighlight: ['BlueColor'], borderchars: ['x', '#']})
113 let winid = popup_create(['hello border', 'with numbers'], #{line: 6, col: 3, border: [], borderhighlight: ['BlueColor'], borderchars: ['0', '1', '2', '3', '4', '5', '6', '7']})
114 call popup_create(['hello border', 'just blanks'], #{line: 7, col: 23, border: [], borderhighlight: ['BlueColor'], borderchars: [' ']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200115 func MultiByte()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200116 call popup_create(['hello'], #{line: 8, col: 43, border: [], borderchars: ['─', '│', '─', '│', '┌', '┐', '┘', '└']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200117 endfunc
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200118 END
119 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200120 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 12})
Bram Moolenaar790498b2019-06-01 22:15:29 +0200121 call VerifyScreenDump(buf, 'Test_popupwin_22', {})
122
Bram Moolenaarad24a712019-06-17 20:05:45 +0200123 " check that changing borderchars triggers a redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200124 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 +0200125 call VerifyScreenDump(buf, 'Test_popupwin_23', {})
126
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200127 " check multi-byte border only with 'ambiwidth' single
128 if &ambiwidth == 'single'
129 call term_sendkeys(buf, ":call MultiByte()\<CR>")
130 call VerifyScreenDump(buf, 'Test_popupwin_24', {})
131 endif
132
Bram Moolenaar790498b2019-06-01 22:15:29 +0200133 call StopVimInTerminal(buf)
134 call delete('XtestPopupBorder')
135
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200136 let with_border_or_padding = #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200137 \ line: 2,
138 \ core_line: 3,
139 \ col: 3,
140 \ core_col: 4,
141 \ width: 14,
142 \ core_width: 12,
143 \ height: 3,
144 \ core_height: 1,
145 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100146 \ lastline: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200147 \ scrollbar: 0,
148 \ visible: 1}
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200149 let winid = popup_create('hello border', #{line: 2, col: 3, border: []})",
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200150 call assert_equal(with_border_or_padding, winid->popup_getpos())
Bram Moolenaarae943152019-06-16 22:54:14 +0200151 let options = popup_getoptions(winid)
152 call assert_equal([], options.border)
153 call assert_false(has_key(options, "padding"))
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200154
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200155 let winid = popup_create('hello padding', #{line: 2, col: 3, padding: []})
Bram Moolenaarae943152019-06-16 22:54:14 +0200156 let with_border_or_padding.width = 15
157 let with_border_or_padding.core_width = 13
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200158 call assert_equal(with_border_or_padding, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200159 let options = popup_getoptions(winid)
160 call assert_false(has_key(options, "border"))
161 call assert_equal([], options.padding)
162
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200163 call popup_setoptions(winid, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200164 \ padding: [1, 2, 3, 4],
165 \ border: [4, 0, 7, 8],
166 \ borderhighlight: ['Top', 'Right', 'Bottom', 'Left'],
167 \ borderchars: ['1', '^', '2', '>', '3', 'v', '4', '<'],
Bram Moolenaarae943152019-06-16 22:54:14 +0200168 \ })
169 let options = popup_getoptions(winid)
170 call assert_equal([1, 0, 1, 1], options.border)
171 call assert_equal([1, 2, 3, 4], options.padding)
172 call assert_equal(['Top', 'Right', 'Bottom', 'Left'], options.borderhighlight)
173 call assert_equal(['1', '^', '2', '>', '3', 'v', '4', '<'], options.borderchars)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200174
Bram Moolenaara1396152019-10-20 18:46:05 +0200175 " Check that popup_setoptions() takes the output of popup_getoptions()
176 call popup_setoptions(winid, options)
177 call assert_equal(options, popup_getoptions(winid))
178
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200179 let winid = popup_create('hello both', #{line: 3, col: 8, border: [], padding: []})
180 call assert_equal(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200181 \ line: 3,
182 \ core_line: 5,
183 \ col: 8,
184 \ core_col: 10,
185 \ width: 14,
186 \ core_width: 10,
187 \ height: 5,
188 \ scrollbar: 0,
189 \ core_height: 1,
190 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100191 \ lastline: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200192 \ visible: 1}, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200193
194 call popup_clear()
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200195endfunc
196
Bram Moolenaarb4230122019-05-30 18:40:53 +0200197func Test_popup_with_syntax_win_execute()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200198 CheckScreendump
199
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200200 let lines =<< trim END
201 call setline(1, range(1, 100))
202 hi PopupColor ctermbg=lightblue
203 let winid = popup_create([
204 \ '#include <stdio.h>',
205 \ 'int main(void)',
206 \ '{',
207 \ ' printf(123);',
208 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200209 \], #{line: 3, col: 25, highlight: 'PopupColor'})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200210 call win_execute(winid, 'set syntax=cpp')
211 END
212 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200213 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200214 call VerifyScreenDump(buf, 'Test_popupwin_10', {})
215
216 " clean up
217 call StopVimInTerminal(buf)
218 call delete('XtestPopup')
219endfunc
220
221func Test_popup_with_syntax_setbufvar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200222 CheckScreendump
223
Bram Moolenaar402502d2019-05-30 22:07:36 +0200224 let lines =<< trim END
225 call setline(1, range(1, 100))
226 hi PopupColor ctermbg=lightgrey
227 let winid = popup_create([
228 \ '#include <stdio.h>',
229 \ 'int main(void)',
230 \ '{',
Bram Moolenaare089c3f2019-07-09 20:25:25 +0200231 \ "\tprintf(567);",
Bram Moolenaar402502d2019-05-30 22:07:36 +0200232 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200233 \], #{line: 3, col: 21, highlight: 'PopupColor'})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200234 call setbufvar(winbufnr(winid), '&syntax', 'cpp')
235 END
236 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200237 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200238 call VerifyScreenDump(buf, 'Test_popupwin_11', {})
239
240 " clean up
241 call StopVimInTerminal(buf)
242 call delete('XtestPopup')
243endfunc
244
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200245func Test_popup_with_matches()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200246 CheckScreendump
247
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200248 let lines =<< trim END
249 call setline(1, ['111 222 333', '444 555 666'])
250 let winid = popup_create([
251 \ '111 222 333',
252 \ '444 555 666',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200253 \], #{line: 3, col: 10, border: []})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200254 set hlsearch
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100255 hi VeryBlue ctermfg=blue guifg=blue
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200256 /666
257 call matchadd('ErrorMsg', '111')
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100258 call matchadd('VeryBlue', '444')
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200259 call win_execute(winid, "call matchadd('ErrorMsg', '111')")
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100260 call win_execute(winid, "call matchadd('VeryBlue', '555')")
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200261 END
262 call writefile(lines, 'XtestPopupMatches')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200263 let buf = RunVimInTerminal('-S XtestPopupMatches', #{rows: 10})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200264 call VerifyScreenDump(buf, 'Test_popupwin_matches', {})
265
266 " clean up
267 call StopVimInTerminal(buf)
268 call delete('XtestPopupMatches')
269endfunc
270
Bram Moolenaar399d8982019-06-02 15:34:29 +0200271func Test_popup_all_corners()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200272 CheckScreendump
273
Bram Moolenaar399d8982019-06-02 15:34:29 +0200274 let lines =<< trim END
275 call setline(1, repeat([repeat('-', 60)], 15))
276 set so=0
277 normal 2G3|r#
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200278 let winid1 = popup_create(['first', 'second'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200279 \ line: 'cursor+1',
280 \ col: 'cursor',
281 \ pos: 'topleft',
282 \ border: [],
283 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200284 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200285 normal 24|r@
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200286 let winid1 = popup_create(['First', 'SeconD'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200287 \ line: 'cursor+1',
288 \ col: 'cursor',
289 \ pos: 'topright',
290 \ border: [],
291 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200292 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200293 normal 9G27|r%
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200294 let winid1 = popup_create(['fiRSt', 'seCOnd'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200295 \ line: 'cursor-1',
296 \ col: 'cursor',
297 \ pos: 'botleft',
298 \ border: [],
299 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200300 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200301 normal 48|r&
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200302 let winid1 = popup_create(['FIrsT', 'SEcoND'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200303 \ line: 'cursor-1',
304 \ col: 'cursor',
305 \ pos: 'botright',
306 \ border: [],
307 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200308 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200309 normal 1G51|r*
310 let winid1 = popup_create(['one', 'two'], #{
311 \ line: 'cursor-1',
312 \ col: 'cursor',
313 \ pos: 'botleft',
314 \ border: [],
315 \ padding: [],
316 \ })
Bram Moolenaar399d8982019-06-02 15:34:29 +0200317 END
318 call writefile(lines, 'XtestPopupCorners')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200319 let buf = RunVimInTerminal('-S XtestPopupCorners', #{rows: 12})
Bram Moolenaar399d8982019-06-02 15:34:29 +0200320 call VerifyScreenDump(buf, 'Test_popupwin_corners', {})
321
322 " clean up
323 call StopVimInTerminal(buf)
324 call delete('XtestPopupCorners')
325endfunc
326
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100327func Test_popup_nospace()
328 CheckScreendump
329
330 let lines =<< trim END
331 call setline(1, repeat([repeat('-', 60)], 15))
332 set so=0
333
334 " cursor in a line in top half, using "botleft" with popup that
335 " does fit
336 normal 5G2|r@
337 let winid1 = popup_create(['one', 'two'], #{
338 \ line: 'cursor-1',
339 \ col: 'cursor',
340 \ pos: 'botleft',
341 \ border: [],
342 \ })
343 " cursor in a line in top half, using "botleft" with popup that
344 " doesn't fit: gets truncated
345 normal 5G9|r#
346 let winid1 = popup_create(['one', 'two', 'tee'], #{
347 \ line: 'cursor-1',
348 \ col: 'cursor',
349 \ pos: 'botleft',
350 \ posinvert: 0,
351 \ border: [],
352 \ })
353 " cursor in a line in top half, using "botleft" with popup that
354 " doesn't fit and 'posinvert' set: flips to below.
355 normal 5G16|r%
356 let winid1 = popup_create(['one', 'two', 'tee'], #{
357 \ line: 'cursor-1',
358 \ col: 'cursor',
359 \ pos: 'botleft',
360 \ border: [],
361 \ })
362 " cursor in a line in bottom half, using "botleft" with popup that
363 " doesn't fit: does not flip.
364 normal 8G23|r*
365 let winid1 = popup_create(['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff'], #{
366 \ line: 'cursor-1',
367 \ col: 'cursor',
368 \ pos: 'botleft',
369 \ border: [],
370 \ })
371
372 " cursor in a line in bottom half, using "topleft" with popup that
373 " does fit
374 normal 8G30|r@
375 let winid1 = popup_create(['one', 'two'], #{
376 \ line: 'cursor+1',
377 \ col: 'cursor',
378 \ pos: 'topleft',
379 \ border: [],
380 \ })
381 " cursor in a line in top half, using "topleft" with popup that
382 " doesn't fit: truncated
383 normal 8G37|r#
384 let winid1 = popup_create(['one', 'two', 'tee'], #{
385 \ line: 'cursor+1',
386 \ col: 'cursor',
387 \ pos: 'topleft',
388 \ posinvert: 0,
389 \ border: [],
390 \ })
391 " cursor in a line in top half, using "topleft" with popup that
Bram Moolenaar5c6b6182019-11-10 17:51:38 +0100392 " doesn't fit and "posinvert" set: flips to above.
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100393 normal 8G44|r%
Bram Moolenaar5c6b6182019-11-10 17:51:38 +0100394 let winid1 = popup_create(['one', 'two', 'tee', 'fou', 'fiv'], #{
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100395 \ line: 'cursor+1',
396 \ col: 'cursor',
397 \ pos: 'topleft',
398 \ border: [],
399 \ })
400 " cursor in a line in top half, using "topleft" with popup that
401 " doesn't fit: does not flip.
402 normal 5G51|r*
403 let winid1 = popup_create(['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff'], #{
404 \ line: 'cursor+1',
405 \ col: 'cursor',
406 \ pos: 'topleft',
407 \ border: [],
408 \ })
409 END
410 call writefile(lines, 'XtestPopupNospace')
411 let buf = RunVimInTerminal('-S XtestPopupNospace', #{rows: 12})
412 call VerifyScreenDump(buf, 'Test_popupwin_nospace', {})
413
414 " clean up
415 call StopVimInTerminal(buf)
416 call delete('XtestPopupNospace')
417endfunc
418
Bram Moolenaar8d241042019-06-12 23:40:01 +0200419func Test_popup_firstline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200420 CheckScreendump
421
Bram Moolenaar8d241042019-06-12 23:40:01 +0200422 let lines =<< trim END
423 call setline(1, range(1, 20))
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200424 let winid = popup_create(['1111', '222222', '33333', '44', '5', '666666', '77777', '888', '9999999999999999'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200425 \ maxheight: 4,
426 \ firstline: 3,
Bram Moolenaar8d241042019-06-12 23:40:01 +0200427 \ })
428 END
429 call writefile(lines, 'XtestPopupFirstline')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200430 let buf = RunVimInTerminal('-S XtestPopupFirstline', #{rows: 10})
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200431 call VerifyScreenDump(buf, 'Test_popupwin_firstline_1', {})
432
433 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: -1})\<CR>")
434 call term_sendkeys(buf, ":\<CR>")
435 call VerifyScreenDump(buf, 'Test_popupwin_firstline_2', {})
Bram Moolenaar8d241042019-06-12 23:40:01 +0200436
437 " clean up
438 call StopVimInTerminal(buf)
439 call delete('XtestPopupFirstline')
Bram Moolenaarae943152019-06-16 22:54:14 +0200440
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200441 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200442 \ maxheight: 2,
443 \ firstline: 3,
Bram Moolenaarae943152019-06-16 22:54:14 +0200444 \ })
445 call assert_equal(3, popup_getoptions(winid).firstline)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200446 call popup_setoptions(winid, #{firstline: 1})
Bram Moolenaarae943152019-06-16 22:54:14 +0200447 call assert_equal(1, popup_getoptions(winid).firstline)
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200448 eval winid->popup_close()
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200449
450 let winid = popup_create(['xxx']->repeat(50), #{
451 \ maxheight: 3,
452 \ firstline: 11,
453 \ })
454 redraw
455 call assert_equal(11, popup_getoptions(winid).firstline)
456 call assert_equal(11, popup_getpos(winid).firstline)
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +0200457 " check line() works with popup window
458 call assert_equal(11, line('.', winid))
459 call assert_equal(50, line('$', winid))
460 call assert_equal(0, line('$', 123456))
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200461
462 " Normal command changes what is displayed but not "firstline"
463 call win_execute(winid, "normal! \<c-y>")
464 call assert_equal(11, popup_getoptions(winid).firstline)
465 call assert_equal(10, popup_getpos(winid).firstline)
466
467 " Making some property change applies "firstline" again
468 call popup_setoptions(winid, #{line: 4})
469 call assert_equal(11, popup_getoptions(winid).firstline)
470 call assert_equal(11, popup_getpos(winid).firstline)
471
472 " Remove "firstline" property and scroll
473 call popup_setoptions(winid, #{firstline: 0})
474 call win_execute(winid, "normal! \<c-y>")
475 call assert_equal(0, popup_getoptions(winid).firstline)
476 call assert_equal(10, popup_getpos(winid).firstline)
477
478 " Making some property change has no side effect
479 call popup_setoptions(winid, #{line: 3})
480 call assert_equal(0, popup_getoptions(winid).firstline)
481 call assert_equal(10, popup_getpos(winid).firstline)
Bram Moolenaarae943152019-06-16 22:54:14 +0200482
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100483 " CTRL-D scrolls down half a page
484 let winid = popup_create(['xxx']->repeat(50), #{
485 \ maxheight: 8,
486 \ })
487 redraw
488 call assert_equal(1, popup_getpos(winid).firstline)
489 call win_execute(winid, "normal! \<C-D>")
490 call assert_equal(5, popup_getpos(winid).firstline)
491 call win_execute(winid, "normal! \<C-D>")
492 call assert_equal(9, popup_getpos(winid).firstline)
493 call win_execute(winid, "normal! \<C-U>")
494 call assert_equal(5, popup_getpos(winid).firstline)
495
496 call win_execute(winid, "normal! \<C-F>")
497 call assert_equal(11, popup_getpos(winid).firstline)
498 call win_execute(winid, "normal! \<C-B>")
499 call assert_equal(5, popup_getpos(winid).firstline)
500
Bram Moolenaarae943152019-06-16 22:54:14 +0200501 call popup_close(winid)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200502endfunc
503
Bram Moolenaara112f2d2019-09-01 17:38:09 +0200504func Test_popup_noscrolloff()
505 set scrolloff=5
506 let winid = popup_create(['xxx']->repeat(50), #{
507 \ maxheight: 5,
508 \ firstline: 11,
509 \ })
510 redraw
511 call assert_equal(11, popup_getoptions(winid).firstline)
512 call assert_equal(11, popup_getpos(winid).firstline)
513
514 call popup_setoptions(winid, #{firstline: 0})
515 call win_execute(winid, "normal! \<c-y>")
516 call assert_equal(0, popup_getoptions(winid).firstline)
517 call assert_equal(10, popup_getpos(winid).firstline)
518
519 call popup_close(winid)
520endfunc
521
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200522func Test_popup_drag()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200523 CheckScreendump
524
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200525 " create a popup that covers the command line
526 let lines =<< trim END
527 call setline(1, range(1, 20))
Bram Moolenaar356375f2019-08-24 14:46:29 +0200528 split
529 vsplit
530 $wincmd w
531 vsplit
532 1wincmd w
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200533 let winid = popup_create(['1111', '222222', '33333'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200534 \ drag: 1,
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200535 \ resize: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200536 \ border: [],
537 \ line: &lines - 4,
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200538 \ })
539 func Dragit()
540 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
541 endfunc
542 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
Bram Moolenaar356375f2019-08-24 14:46:29 +0200543 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2 - 20)<CR>
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200544 func Resize()
545 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
546 endfunc
Bram Moolenaar356375f2019-08-24 14:46:29 +0200547 map <silent> <F5> :call test_setmouse(6, 21)<CR>
548 map <silent> <F6> :call test_setmouse(7, 25)<CR>
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200549 END
550 call writefile(lines, 'XtestPopupDrag')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200551 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200552 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
553
554 call term_sendkeys(buf, ":call Dragit()\<CR>")
555 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
556
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200557 call term_sendkeys(buf, ":call Resize()\<CR>")
558 call VerifyScreenDump(buf, 'Test_popupwin_drag_03', {})
559
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200560 " clean up
561 call StopVimInTerminal(buf)
562 call delete('XtestPopupDrag')
563endfunc
564
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200565func Test_popup_close_with_mouse()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200566 CheckScreendump
567
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200568 let lines =<< trim END
569 call setline(1, range(1, 20))
570 " With border, can click on X
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200571 let winid = popup_create('foobar', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200572 \ close: 'button',
573 \ border: [],
574 \ line: 1,
575 \ col: 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200576 \ })
577 func CloseMsg(id, result)
578 echomsg 'Popup closed with ' .. a:result
579 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200580 let winid = popup_create('notification', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200581 \ close: 'click',
582 \ line: 3,
583 \ col: 15,
584 \ callback: 'CloseMsg',
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200585 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200586 let winid = popup_create('no border here', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200587 \ close: 'button',
588 \ line: 5,
589 \ col: 3,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200590 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200591 let winid = popup_create('only padding', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200592 \ close: 'button',
593 \ padding: [],
594 \ line: 5,
595 \ col: 23,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200596 \ })
597 func CloseWithX()
598 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
599 endfunc
600 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
601 func CloseWithClick()
602 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
603 endfunc
604 map <silent> <F4> :call test_setmouse(3, 17)<CR>
Bram Moolenaarf6396232019-08-24 19:36:00 +0200605 func CreateWithMenuFilter()
606 let winid = popup_create('barfoo', #{
607 \ close: 'button',
608 \ filter: 'popup_filter_menu',
609 \ border: [],
610 \ line: 1,
611 \ col: 40,
612 \ })
613 endfunc
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200614 END
615 call writefile(lines, 'XtestPopupClose')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200616 let buf = RunVimInTerminal('-S XtestPopupClose', #{rows: 10})
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200617 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
618
619 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
620 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
621
622 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
623 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
624
Bram Moolenaarf6396232019-08-24 19:36:00 +0200625 call term_sendkeys(buf, ":call CreateWithMenuFilter()\<CR>")
626 call VerifyScreenDump(buf, 'Test_popupwin_close_04', {})
627
628 " We have to send the actual mouse code, feedkeys() would be caught the
629 " filter.
630 call term_sendkeys(buf, "\<Esc>[<0;47;1M")
631 call VerifyScreenDump(buf, 'Test_popupwin_close_05', {})
632
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200633 " clean up
634 call StopVimInTerminal(buf)
635 call delete('XtestPopupClose')
636endfunction
637
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200638func Test_popup_menu_wrap()
639 CheckScreendump
640
641 let lines =<< trim END
642 call setline(1, range(1, 20))
643 call popup_create([
644 \ 'one',
645 \ 'asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfas',
646 \ 'three',
647 \ 'four',
648 \ ], #{
649 \ pos: "botleft",
650 \ border: [],
651 \ padding: [0,1,0,1],
652 \ maxheight: 3,
653 \ cursorline: 1,
654 \ filter: 'popup_filter_menu',
655 \ })
656 END
657 call writefile(lines, 'XtestPopupWrap')
658 let buf = RunVimInTerminal('-S XtestPopupWrap', #{rows: 10})
659 call VerifyScreenDump(buf, 'Test_popupwin_wrap_1', {})
660
661 call term_sendkeys(buf, "jj")
662 call VerifyScreenDump(buf, 'Test_popupwin_wrap_2', {})
663
664 " clean up
665 call term_sendkeys(buf, "\<Esc>")
666 call StopVimInTerminal(buf)
667 call delete('XtestPopupWrap')
668endfunction
669
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200670func Test_popup_with_mask()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200671 CheckScreendump
672
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200673 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200674 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200675 hi PopupColor ctermbg=lightgrey
676 let winid = popup_create([
677 \ 'some text',
678 \ 'another line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200679 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200680 \ line: 1,
681 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100682 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200683 \ wrap: 0,
684 \ fixed: 1,
685 \ zindex: 90,
686 \ padding: [],
687 \ highlight: 'PopupColor',
688 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200689 call popup_create([
690 \ 'xxxxxxxxx',
691 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200692 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200693 \ line: 3,
694 \ col: 18,
695 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200696 let winidb = popup_create([
697 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200698 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200699 \ line: 7,
700 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100701 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200702 \ wrap: 0,
703 \ fixed: 1,
704 \ close: 'button',
705 \ zindex: 90,
706 \ padding: [],
707 \ border: [],
708 \ 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 +0200709 END
710 call writefile(lines, 'XtestPopupMask')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200711 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200712 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
713
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200714 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
715 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200716 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200717 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
718
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200719 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
720 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200721 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200722 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
723
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200724 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
725 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200726 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200727 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
728
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200729 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
730 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200731 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200732 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
733
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200734 " clean up
735 call StopVimInTerminal(buf)
736 call delete('XtestPopupMask')
737endfunc
738
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200739func Test_popup_select()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200740 CheckScreendump
741 CheckFeature clipboard_working
742
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200743 " create a popup with some text to be selected
744 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200745 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200746 call setline(1, range(1, 20))
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200747 let winid = popup_create(['the word', 'some more', 'several words here', 'invisible', '5', '6', '7'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200748 \ drag: 1,
749 \ border: [],
750 \ line: 3,
751 \ col: 10,
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200752 \ maxheight: 3,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200753 \ })
754 func Select1()
755 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
756 endfunc
757 map <silent> <F3> :call test_setmouse(4, 15)<CR>
758 map <silent> <F4> :call test_setmouse(6, 23)<CR>
759 END
760 call writefile(lines, 'XtestPopupSelect')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200761 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200762 call term_sendkeys(buf, ":call Select1()\<CR>")
763 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
764
765 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
766 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200767 " clean the command line, sometimes it still shows a command
768 call term_sendkeys(buf, ":\<esc>")
769
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200770 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
771
772 " clean up
773 call StopVimInTerminal(buf)
774 call delete('XtestPopupSelect')
775endfunc
776
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200777func Test_popup_in_tab()
778 " default popup is local to tab, not visible when in other tab
779 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200780 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200781 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200782 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200783 tabnew
784 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200785 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200786 quit
787 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200788
789 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200790 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200791 " buffer is gone now
792 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200793
794 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200795 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200796 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200797 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200798 tabnew
799 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200800 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200801 quit
802 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200803 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200804
805 " create popup in other tab
806 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200807 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200808 call assert_equal(0, popup_getpos(winid).visible)
809 call assert_equal(1, popup_getoptions(winid).tabpage)
810 quit
811 call assert_equal(1, popup_getpos(winid).visible)
812 call assert_equal(0, popup_getoptions(winid).tabpage)
813 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200814endfunc
815
816func Test_popup_valid_arguments()
817 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200818 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200819 let pos = popup_getpos(winid)
820 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200821 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200822
823 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200824 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200825 let pos = popup_getpos(winid)
826 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200827 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200828
829 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200830 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200831 let pos = popup_getpos(winid)
832 let around = (&columns - pos.width) / 2
833 call assert_inrange(around - 1, around + 1, pos.col)
834 let around = (&lines - pos.height) / 2
835 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200836 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200837endfunc
838
839func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200840 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200841 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200842 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200843 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200844
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200845 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200846 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200847 call assert_fails('call popup_create("text", #{col: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200848 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200849 call assert_fails('call popup_create("text", #{col: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200850 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200851 call assert_fails('call popup_create("text", #{col: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200852 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200853
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200854 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200855 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200856 call assert_fails('call popup_create("text", #{line: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200857 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200858 call assert_fails('call popup_create("text", #{line: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200859 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200860 call assert_fails('call popup_create("text", #{line: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200861 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200862
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200863 call assert_fails('call popup_create("text", #{pos: "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200864 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200865 call assert_fails('call popup_create("text", #{padding: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200866 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200867 call assert_fails('call popup_create("text", #{border: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200868 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200869 call assert_fails('call popup_create("text", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200870 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +0200871 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
872 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200873 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200874 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200875
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200876 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200877 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200878 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200879 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200880 call assert_fails('call popup_create([#{text: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200881 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200882 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
883 call popup_clear()
884 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200885 call assert_fails('call popup_create("text", #{mapping: []})', 'E745:')
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200886 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200887endfunc
888
Bram Moolenaareea16992019-05-31 17:34:48 +0200889func Test_win_execute_closing_curwin()
890 split
891 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200892 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200893 call popup_clear()
Bram Moolenaar49540192019-12-11 19:34:54 +0100894
895 let winid = popup_create('some text', {})
896 call assert_fails('call win_execute(winid, printf("normal! :\<C-u>call popup_close(%d)\<CR>", winid))', 'E994')
897 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200898endfunc
899
900func Test_win_execute_not_allowed()
901 let winid = popup_create('some text', {})
902 call assert_fails('call win_execute(winid, "split")', 'E994:')
903 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
904 call assert_fails('call win_execute(winid, "close")', 'E994:')
905 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200906 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200907 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
908 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
909 call assert_fails('call win_execute(winid, "next")', 'E994:')
910 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
911 call assert_fails('call win_execute(winid, "buf")', 'E994:')
912 call assert_fails('call win_execute(winid, "edit")', 'E994:')
913 call assert_fails('call win_execute(winid, "enew")', 'E994:')
914 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
915 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
916 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
917 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200918 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200919endfunc
920
Bram Moolenaar402502d2019-05-30 22:07:36 +0200921func Test_popup_with_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200922 CheckScreendump
923
Bram Moolenaar402502d2019-05-30 22:07:36 +0200924 let lines =<< trim END
925 call setline(1, range(1, 100))
926 let winid = popup_create(
927 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200928 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200929 END
930 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200931 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200932 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
933
934 " clean up
935 call StopVimInTerminal(buf)
936 call delete('XtestPopup')
937endfunc
938
939func Test_popup_without_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200940 CheckScreendump
941
Bram Moolenaar402502d2019-05-30 22:07:36 +0200942 let lines =<< trim END
943 call setline(1, range(1, 100))
944 let winid = popup_create(
945 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200946 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200947 END
948 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200949 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200950 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
951
952 " clean up
953 call StopVimInTerminal(buf)
954 call delete('XtestPopup')
955endfunc
956
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200957func Test_popup_with_showbreak()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200958 CheckScreendump
959
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200960 let lines =<< trim END
961 set showbreak=>>\
962 call setline(1, range(1, 20))
963 let winid = popup_dialog(
Bram Moolenaar8ae54372019-09-15 18:11:16 +0200964 \ 'a long line here that wraps',
965 \ #{filter: 'popup_filter_yesno',
966 \ maxwidth: 12})
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200967 END
968 call writefile(lines, 'XtestPopupShowbreak')
969 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
970 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
971
972 " clean up
973 call term_sendkeys(buf, "y")
974 call StopVimInTerminal(buf)
975 call delete('XtestPopupShowbreak')
976endfunc
977
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200978func Test_popup_time()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200979 CheckFeature timers
980
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200981 topleft vnew
982 call setline(1, 'hello')
983
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200984 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200985 \ line: 1,
986 \ col: 1,
987 \ minwidth: 20,
988 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200989 \})
990 redraw
991 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
992 call assert_equal('world', line)
993
Bram Moolenaarb4f06282019-07-12 21:07:54 +0200994 call assert_equal(winid, popup_locate(1, 1))
995 call assert_equal(winid, popup_locate(1, 20))
996 call assert_equal(0, popup_locate(1, 21))
997 call assert_equal(0, popup_locate(2, 1))
998
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200999 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +02001000 redraw
Bram Moolenaar196b4662019-09-06 21:34:30 +02001001 let line = join(map(range(1, 5), '1->screenstring(v:val)'), '')
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001002 call assert_equal('hello', line)
1003
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001004 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001005 \ line: &lines,
1006 \ col: 10,
1007 \ minwidth: 20,
1008 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001009 \})
1010 redraw
1011 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1012 call assert_match('.*on the command line.*', line)
1013
1014 sleep 700m
1015 redraw
1016 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1017 call assert_notmatch('.*on the command line.*', line)
1018
1019 bwipe!
1020endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001021
1022func Test_popup_hide()
1023 topleft vnew
1024 call setline(1, 'hello')
1025
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001026 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001027 \ line: 1,
1028 \ col: 1,
1029 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001030 \})
1031 redraw
1032 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1033 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001034 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001035 " buffer is still listed and active
1036 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001037
1038 call popup_hide(winid)
1039 redraw
1040 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1041 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001042 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001043 " buffer is still listed but hidden
1044 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001045
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001046 eval winid->popup_show()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001047 redraw
1048 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1049 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001050 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001051
1052
1053 call popup_close(winid)
1054 redraw
1055 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1056 call assert_equal('hello', line)
1057
1058 " error is given for existing non-popup window
1059 call assert_fails('call popup_hide(win_getid())', 'E993:')
1060
1061 " no error non-existing window
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001062 eval 1234234->popup_hide()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001063 call popup_show(41234234)
1064
1065 bwipe!
1066endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001067
1068func Test_popup_move()
1069 topleft vnew
1070 call setline(1, 'hello')
1071
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001072 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001073 \ line: 1,
1074 \ col: 1,
1075 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001076 \})
1077 redraw
1078 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1079 call assert_equal('world ', line)
1080
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001081 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001082 redraw
1083 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1084 call assert_equal('hello ', line)
1085 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
1086 call assert_equal('~world', line)
1087
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001088 eval winid->popup_move(#{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001089 redraw
1090 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1091 call assert_equal('hworld', line)
1092
1093 call popup_close(winid)
1094
1095 bwipe!
1096endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +02001097
Bram Moolenaar402502d2019-05-30 22:07:36 +02001098func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001099 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001100 \ line: 2,
1101 \ col: 3,
1102 \ minwidth: 10,
1103 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +02001104 \})
1105 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001106 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001107 call assert_equal(2, res.line)
1108 call assert_equal(3, res.col)
1109 call assert_equal(10, res.width)
1110 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001111 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001112
1113 call popup_close(winid)
1114endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001115
1116func Test_popup_width_longest()
1117 let tests = [
1118 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
1119 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
1120 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
1121 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
1122 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
1123 \ ]
1124
1125 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001126 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001127 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001128 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001129 call assert_equal(test[1], position.width)
1130 call popup_close(winid)
1131 endfor
1132endfunc
1133
1134func Test_popup_wraps()
1135 let tests = [
1136 \ ['nowrap', 6, 1],
1137 \ ['a line that wraps once', 12, 2],
1138 \ ['a line that wraps two times', 12, 3],
1139 \ ]
1140 for test in tests
1141 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001142 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001143 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001144 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001145 call assert_equal(test[1], position.width)
1146 call assert_equal(test[2], position.height)
1147
1148 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001149 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001150 endfor
1151endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001152
1153func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001154 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001155 \ line: 2,
1156 \ col: 3,
1157 \ minwidth: 10,
1158 \ minheight: 11,
1159 \ maxwidth: 20,
1160 \ maxheight: 21,
1161 \ zindex: 100,
1162 \ time: 5000,
1163 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001164 \})
1165 redraw
1166 let res = popup_getoptions(winid)
1167 call assert_equal(2, res.line)
1168 call assert_equal(3, res.col)
1169 call assert_equal(10, res.minwidth)
1170 call assert_equal(11, res.minheight)
1171 call assert_equal(20, res.maxwidth)
1172 call assert_equal(21, res.maxheight)
1173 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001174 call assert_equal(1, res.fixed)
Bram Moolenaarb8350ab2019-08-04 17:59:49 +02001175 call assert_equal(1, res.mapping)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001176 if has('timers')
1177 call assert_equal(5000, res.time)
1178 endif
1179 call popup_close(winid)
1180
1181 let winid = popup_create('hello', {})
1182 redraw
1183 let res = popup_getoptions(winid)
1184 call assert_equal(0, res.line)
1185 call assert_equal(0, res.col)
1186 call assert_equal(0, res.minwidth)
1187 call assert_equal(0, res.minheight)
1188 call assert_equal(0, res.maxwidth)
1189 call assert_equal(0, res.maxheight)
1190 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001191 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001192 if has('timers')
1193 call assert_equal(0, res.time)
1194 endif
1195 call popup_close(winid)
1196 call assert_equal({}, popup_getoptions(winid))
1197endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001198
1199func Test_popup_option_values()
1200 new
1201 " window-local
1202 setlocal number
1203 setlocal nowrap
1204 " buffer-local
1205 setlocal omnifunc=Something
1206 " global/buffer-local
1207 setlocal path=/there
1208 " global/window-local
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001209 setlocal statusline=2
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001210
1211 let winid = popup_create('hello', {})
1212 call assert_equal(0, getwinvar(winid, '&number'))
1213 call assert_equal(1, getwinvar(winid, '&wrap'))
1214 call assert_equal('', getwinvar(winid, '&omnifunc'))
1215 call assert_equal(&g:path, getwinvar(winid, '&path'))
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001216 call assert_equal(&g:statusline, getwinvar(winid, '&statusline'))
1217
1218 " 'scrolloff' is reset to zero
1219 call assert_equal(5, &scrolloff)
1220 call assert_equal(0, getwinvar(winid, '&scrolloff'))
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001221
1222 call popup_close(winid)
1223 bwipe
1224endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001225
1226func Test_popup_atcursor()
1227 topleft vnew
1228 call setline(1, [
1229 \ 'xxxxxxxxxxxxxxxxx',
1230 \ 'xxxxxxxxxxxxxxxxx',
1231 \ 'xxxxxxxxxxxxxxxxx',
1232 \])
1233
1234 call cursor(2, 2)
1235 redraw
1236 let winid = popup_atcursor('vim', {})
1237 redraw
1238 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1239 call assert_equal('xvimxxxxxxxxxxxxx', line)
1240 call popup_close(winid)
1241
1242 call cursor(3, 4)
1243 redraw
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001244 let winid = 'vim'->popup_atcursor({})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001245 redraw
1246 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
1247 call assert_equal('xxxvimxxxxxxxxxxx', line)
1248 call popup_close(winid)
1249
1250 call cursor(1, 1)
1251 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001252 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001253 \ line: 'cursor+2',
1254 \ col: 'cursor+1',
1255 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001256 redraw
1257 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1258 call assert_equal('xvimxxxxxxxxxxxxx', line)
1259 call popup_close(winid)
1260
1261 call cursor(3, 3)
1262 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001263 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001264 \ line: 'cursor-2',
1265 \ col: 'cursor-1',
1266 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001267 redraw
1268 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1269 call assert_equal('xvimxxxxxxxxxxxxx', line)
1270 call popup_close(winid)
1271
Bram Moolenaar402502d2019-05-30 22:07:36 +02001272 " just enough room above
1273 call cursor(3, 3)
1274 redraw
1275 let winid = popup_atcursor(['vim', 'is great'], {})
1276 redraw
1277 let pos = popup_getpos(winid)
1278 call assert_equal(1, pos.line)
1279 call popup_close(winid)
1280
1281 " not enough room above, popup goes below the cursor
1282 call cursor(3, 3)
1283 redraw
1284 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1285 redraw
1286 let pos = popup_getpos(winid)
1287 call assert_equal(4, pos.line)
1288 call popup_close(winid)
1289
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001290 " cursor in first line, popup in line 2
1291 call cursor(1, 1)
1292 redraw
1293 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1294 redraw
1295 let pos = popup_getpos(winid)
1296 call assert_equal(2, pos.line)
1297 call popup_close(winid)
1298
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001299 bwipe!
1300endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001301
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001302func Test_popup_atcursor_pos()
1303 CheckScreendump
1304
1305 let lines =<< trim END
1306 call setline(1, repeat([repeat('-', 60)], 15))
1307 set so=0
1308
1309 normal 9G3|r#
1310 let winid1 = popup_atcursor(['first', 'second'], #{
1311 \ moved: [0, 0, 0],
1312 \ })
1313 normal 9G21|r&
1314 let winid1 = popup_atcursor(['FIrsT', 'SEcoND'], #{
1315 \ pos: 'botright',
1316 \ moved: [0, 0, 0],
1317 \ })
1318 normal 3G27|r%
1319 let winid1 = popup_atcursor(['fiRSt', 'seCOnd'], #{
1320 \ pos: 'topleft',
1321 \ moved: [0, 0, 0],
1322 \ })
1323 normal 3G45|r@
1324 let winid1 = popup_atcursor(['First', 'SeconD'], #{
1325 \ pos: 'topright',
1326 \ moved: [0, 0, 0],
1327 \ })
1328 END
1329 call writefile(lines, 'XtestPopupAtcursorPos')
1330 let buf = RunVimInTerminal('-S XtestPopupAtcursorPos', #{rows: 12})
1331 call VerifyScreenDump(buf, 'Test_popupwin_atcursor_pos', {})
1332
1333 " clean up
1334 call StopVimInTerminal(buf)
1335 call delete('XtestPopupAtcursorPos')
1336endfunc
1337
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001338func Test_popup_beval()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001339 CheckScreendump
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001340 CheckFeature balloon_eval_term
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001341
1342 let lines =<< trim END
1343 call setline(1, range(1, 20))
1344 call setline(5, 'here is some text to hover over')
1345 set balloonevalterm
1346 set balloonexpr=BalloonExpr()
1347 set balloondelay=100
1348 func BalloonExpr()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001349 let s:winid = [v:beval_text]->popup_beval({})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001350 return ''
1351 endfunc
1352 func Hover()
1353 call test_setmouse(5, 15)
1354 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1355 sleep 100m
1356 endfunc
1357 func MoveOntoPopup()
1358 call test_setmouse(4, 17)
1359 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1360 endfunc
1361 func MoveAway()
1362 call test_setmouse(5, 13)
1363 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1364 endfunc
1365 END
1366 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001367 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001368 call term_wait(buf, 100)
1369 call term_sendkeys(buf, 'j')
1370 call term_sendkeys(buf, ":call Hover()\<CR>")
1371 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1372
1373 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1374 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1375
1376 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1377 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1378
1379 " clean up
1380 call StopVimInTerminal(buf)
1381 call delete('XtestPopupBeval')
1382endfunc
1383
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001384func Test_popup_filter()
1385 new
1386 call setline(1, 'some text')
1387
1388 func MyPopupFilter(winid, c)
1389 if a:c == 'e'
1390 let g:eaten = 'e'
1391 return 1
1392 endif
1393 if a:c == '0'
1394 let g:ignored = '0'
1395 return 0
1396 endif
1397 if a:c == 'x'
1398 call popup_close(a:winid)
1399 return 1
1400 endif
1401 return 0
1402 endfunc
1403
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001404 let winid = 'something'->popup_create(#{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001405 redraw
1406
1407 " e is consumed by the filter
1408 call feedkeys('e', 'xt')
1409 call assert_equal('e', g:eaten)
1410
1411 " 0 is ignored by the filter
1412 normal $
1413 call assert_equal(9, getcurpos()[2])
1414 call feedkeys('0', 'xt')
1415 call assert_equal('0', g:ignored)
1416 call assert_equal(1, getcurpos()[2])
1417
1418 " x closes the popup
1419 call feedkeys('x', 'xt')
1420 call assert_equal('e', g:eaten)
1421 call assert_equal(-1, winbufnr(winid))
1422
1423 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001424 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001425endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001426
Bram Moolenaara42d9452019-06-15 21:46:30 +02001427func ShowDialog(key, result)
1428 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001429 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001430 \ filter: 'popup_filter_yesno',
1431 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001432 \ })
1433 redraw
1434 call feedkeys(a:key, "xt")
1435 call assert_equal(winid, s:cb_winid)
1436 call assert_equal(a:result, s:cb_res)
1437endfunc
1438
1439func Test_popup_dialog()
1440 func QuitCallback(id, res)
1441 let s:cb_winid = a:id
1442 let s:cb_res = a:res
1443 endfunc
1444
1445 let winid = ShowDialog("y", 1)
1446 let winid = ShowDialog("Y", 1)
1447 let winid = ShowDialog("n", 0)
1448 let winid = ShowDialog("N", 0)
1449 let winid = ShowDialog("x", 0)
1450 let winid = ShowDialog("X", 0)
1451 let winid = ShowDialog("\<Esc>", 0)
1452 let winid = ShowDialog("\<C-C>", -1)
1453
1454 delfunc QuitCallback
1455endfunc
1456
Bram Moolenaara730e552019-06-16 19:05:31 +02001457func ShowMenu(key, result)
1458 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001459 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001460 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001461 \ })
1462 redraw
1463 call feedkeys(a:key, "xt")
1464 call assert_equal(winid, s:cb_winid)
1465 call assert_equal(a:result, s:cb_res)
1466endfunc
1467
1468func Test_popup_menu()
1469 func QuitCallback(id, res)
1470 let s:cb_winid = a:id
1471 let s:cb_res = a:res
1472 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001473 " mapping won't be used in popup
1474 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001475
1476 let winid = ShowMenu(" ", 1)
1477 let winid = ShowMenu("j \<CR>", 2)
1478 let winid = ShowMenu("JjK \<CR>", 2)
1479 let winid = ShowMenu("jjjjjj ", 3)
1480 let winid = ShowMenu("kkk ", 1)
1481 let winid = ShowMenu("x", -1)
1482 let winid = ShowMenu("X", -1)
1483 let winid = ShowMenu("\<Esc>", -1)
1484 let winid = ShowMenu("\<C-C>", -1)
1485
1486 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001487 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001488endfunc
1489
1490func Test_popup_menu_screenshot()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001491 CheckScreendump
Bram Moolenaara730e552019-06-16 19:05:31 +02001492
1493 let lines =<< trim END
1494 call setline(1, range(1, 20))
1495 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001496 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001497 func MenuDone(id, res)
1498 echomsg "selected " .. a:res
1499 endfunc
1500 END
1501 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001502 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001503 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1504
1505 call term_sendkeys(buf, "jj")
1506 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1507
1508 call term_sendkeys(buf, " ")
1509 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1510
1511 " clean up
1512 call StopVimInTerminal(buf)
1513 call delete('XtestPopupMenu')
1514endfunc
1515
Bram Moolenaarf914a332019-07-20 15:09:56 +02001516func Test_popup_menu_narrow()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001517 CheckScreendump
Bram Moolenaarf914a332019-07-20 15:09:56 +02001518
1519 let lines =<< trim END
1520 call setline(1, range(1, 20))
1521 hi PopupSelected ctermbg=green
1522 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1523 func MenuDone(id, res)
1524 echomsg "selected " .. a:res
1525 endfunc
1526 END
1527 call writefile(lines, 'XtestPopupNarrowMenu')
1528 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1529 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1530
1531 " clean up
1532 call term_sendkeys(buf, "x")
1533 call StopVimInTerminal(buf)
1534 call delete('XtestPopupNarrowMenu')
1535endfunc
1536
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001537func Test_popup_title()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001538 CheckScreendump
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001539
1540 " Create a popup without title or border, a line of padding will be added to
1541 " put the title on.
1542 let lines =<< trim END
1543 call setline(1, range(1, 20))
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001544 let winid = popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001545 END
1546 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001547 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001548 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1549
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001550 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxwidth: 20, title: 'a very long title that is not going to fit'})\<CR>")
1551 call term_sendkeys(buf, ":\<CR>")
1552 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_1', {})
1553
1554 call term_sendkeys(buf, ":call popup_setoptions(winid, #{border: []})\<CR>")
1555 call term_sendkeys(buf, ":\<CR>")
1556 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_2', {})
1557
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001558 " clean up
1559 call StopVimInTerminal(buf)
1560 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001561
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001562 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001563 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001564 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001565 call assert_equal('Another Title', popup_getoptions(winid).title)
1566
1567 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001568endfunc
1569
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001570func Test_popup_close_callback()
1571 func PopupDone(id, result)
1572 let g:result = a:result
1573 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001574 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001575 redraw
1576 call popup_close(winid, 'done')
1577 call assert_equal('done', g:result)
1578endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001579
1580func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001581 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001582 redraw
1583 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001584 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001585 call assert_equal(5, pos.height)
1586
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001587 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001588 redraw
1589 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001590 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001591 call assert_equal(3, pos.height)
1592endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001593
1594func Test_popup_never_behind()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001595 CheckScreendump
1596
Bram Moolenaar988c4332019-06-02 14:12:11 +02001597 " +-----------------------------+
1598 " | | |
1599 " | | |
1600 " | | |
1601 " | line1 |
1602 " |------------line2------------|
1603 " | line3 |
1604 " | line4 |
1605 " | |
1606 " | |
1607 " +-----------------------------+
1608 let lines =<< trim END
Bram Moolenaar988c4332019-06-02 14:12:11 +02001609 split
1610 vsplit
1611 let info_window1 = getwininfo()[0]
1612 let line = info_window1['height']
1613 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001614 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001615 \ line : line,
1616 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001617 \ })
1618 END
1619 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001620 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001621 call term_sendkeys(buf, "\<C-W>w")
1622 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1623
1624 " clean up
1625 call StopVimInTerminal(buf)
1626 call delete('XtestPopupBehind')
1627endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001628
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001629func s:VerifyPosition(p, msg, line, col, width, height)
1630 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1631 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1632 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1633 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001634endfunc
1635
1636func Test_popup_position_adjust()
1637 " Anything placed past 2 cells from of the right of the screen is moved to the
1638 " left.
1639 "
1640 " When wrapping is disabled, we also shift to the left to display on the
1641 " screen, unless fixed is set.
1642
1643 " Entries for cases which don't vary based on wrapping.
1644 " Format is per tests described below
1645 let both_wrap_tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001646 \ ['a', 5, &columns, 5, &columns - 2, 1, 1],
1647 \ ['b', 5, &columns + 1, 5, &columns - 2, 1, 1],
1648 \ ['c', 5, &columns - 1, 5, &columns - 2, 1, 1],
1649 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
1650 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1],
1651 \
1652 \ ['aa', 5, &columns, 5, &columns - 2, 2, 1],
1653 \ ['bb', 5, &columns + 1, 5, &columns - 2, 2, 1],
1654 \ ['cc', 5, &columns - 1, 5, &columns - 2, 2, 1],
1655 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1656 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1657 \
1658 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
1659 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1660 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1661 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1662 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1663 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001664
1665 " these test groups are dicts with:
1666 " - comment: something to identify the group of tests by
1667 " - options: dict of options to merge with the row/col in tests
1668 " - tests: list of cases. Each one is a list with elements:
1669 " - text
1670 " - row
1671 " - col
1672 " - expected row
1673 " - expected col
1674 " - expected width
1675 " - expected height
1676 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001677 \ #{
1678 \ comment: 'left-aligned with wrapping',
1679 \ options: #{
1680 \ wrap: 1,
1681 \ pos: 'botleft',
1682 \ },
1683 \ tests: both_wrap_tests + [
1684 \ ['aaaa', 5, &columns, 4, &columns - 2, 3, 2],
1685 \ ['bbbb', 5, &columns + 1, 4, &columns - 2, 3, 2],
1686 \ ['cccc', 5, &columns - 1, 4, &columns - 2, 3, 2],
1687 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
1688 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1689 \ ],
1690 \ },
1691 \ #{
1692 \ comment: 'left aligned without wrapping',
1693 \ options: #{
1694 \ wrap: 0,
1695 \ pos: 'botleft',
1696 \ },
1697 \ tests: both_wrap_tests + [
1698 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
1699 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
1700 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
1701 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
1702 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1703 \ ],
1704 \ },
1705 \ #{
1706 \ comment: 'left aligned with fixed position',
1707 \ options: #{
1708 \ wrap: 0,
1709 \ fixed: 1,
1710 \ pos: 'botleft',
1711 \ },
1712 \ tests: both_wrap_tests + [
1713 \ ['aaaa', 5, &columns, 5, &columns - 2, 3, 1],
1714 \ ['bbbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1715 \ ['cccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1716 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1717 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1718 \ ],
1719 \ },
1720 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001721
1722 for test_group in tests
1723 for test in test_group.tests
1724 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001725 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001726 \ line: line,
1727 \ col: col,
1728 \ }
1729 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001730
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001731 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001732
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001733 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001734 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
1735 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001736 endfor
1737 endfor
1738
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001739 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001740 %bwipe!
1741endfunc
1742
Bram Moolenaar3397f742019-06-02 18:40:06 +02001743func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001744 " width of screen
1745 let X = join(map(range(&columns), {->'X'}), '')
1746
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001747 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1748 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001749
1750 redraw
1751 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1752 call assert_equal(X, line)
1753
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001754 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001755 redraw
1756
1757 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001758 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
1759 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001760
1761 redraw
1762 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1763 call assert_equal(X, line)
1764
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001765 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001766 redraw
1767
1768 " Extend so > window width
1769 let X .= 'x'
1770
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001771 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1772 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001773
1774 redraw
1775 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1776 call assert_equal(X[ : -2 ], line)
1777
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001778 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001779 redraw
1780
1781 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001782 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
1783 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001784
1785 redraw
1786 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1787 call assert_equal(X[ : -2 ], line)
1788
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001789 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001790 redraw
1791
1792 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001793 let p = popup_create(X,
1794 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
1795 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001796
1797 redraw
1798 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1799 let e_line = ' ' . X[ 1 : -2 ]
1800 call assert_equal(e_line, line)
1801
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001802 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001803 redraw
1804
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001805 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001806 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001807endfunc
1808
1809func Test_popup_moved()
1810 new
1811 call test_override('char_avail', 1)
1812 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1813
1814 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001815 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001816 redraw
1817 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001818 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001819 " trigger the check for last_cursormoved by going into insert mode
1820 call feedkeys("li\<Esc>", 'xt')
1821 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001822 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001823
1824 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001825 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001826 redraw
1827 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001828 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001829 call feedkeys("hi\<Esc>", 'xt')
1830 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001831 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001832
1833 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001834 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001835 redraw
1836 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001837 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001838 call feedkeys("li\<Esc>", 'xt')
1839 call assert_equal(1, popup_getpos(winid).visible)
1840 call feedkeys("ei\<Esc>", 'xt')
1841 call assert_equal(1, popup_getpos(winid).visible)
1842 call feedkeys("eli\<Esc>", 'xt')
1843 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001844 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001845
Bram Moolenaar17627312019-06-02 19:53:44 +02001846 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001847 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001848 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001849 redraw
1850 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001851 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001852 call feedkeys("eli\<Esc>", 'xt')
1853 call assert_equal(1, popup_getpos(winid).visible)
1854 call feedkeys("wi\<Esc>", 'xt')
1855 call assert_equal(1, popup_getpos(winid).visible)
1856 call feedkeys("Eli\<Esc>", 'xt')
1857 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001858 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001859
1860 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001861 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001862 redraw
1863 call assert_equal(1, popup_getpos(winid).visible)
1864 call feedkeys("eli\<Esc>", 'xt')
1865 call feedkeys("ei\<Esc>", 'xt')
1866 call assert_equal(1, popup_getpos(winid).visible)
1867 call feedkeys("eli\<Esc>", 'xt')
1868 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001869 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001870
1871 bwipe!
1872 call test_override('ALL', 0)
1873endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001874
1875func Test_notifications()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001876 CheckFeature timers
1877 CheckScreendump
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001878
Bram Moolenaar0fdddee2019-09-01 15:26:23 +02001879 let lines =<< trim END
1880 call setline(1, range(1, 20))
1881 hi Notification ctermbg=lightblue
1882 call popup_notification('first notification', {})
1883 END
1884 call writefile(lines, 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001885 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001886 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1887
1888 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001889 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1890 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001891 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1892
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001893 " clean up
1894 call StopVimInTerminal(buf)
1895 call delete('XtestNotifications')
1896endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001897
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001898func Test_popup_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001899 CheckScreendump
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001900
1901 let lines =<< trim END
1902 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02001903 hi ScrollThumb ctermbg=blue
1904 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001905 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001906 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001907 \ minwidth: 8,
1908 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001909 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001910 func ScrollUp()
1911 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
1912 endfunc
1913 func ScrollDown()
1914 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
1915 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001916 func ClickTop()
1917 call feedkeys("\<F4>\<LeftMouse>", "xt")
1918 endfunc
1919 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001920 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001921 call feedkeys("\<F5>\<LeftMouse>", "xt")
1922 endfunc
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001923 func Popup_filter(winid, key)
1924 if a:key == 'j'
1925 let line = popup_getoptions(a:winid).firstline
1926 let nlines = line('$', a:winid)
1927 let newline = line < nlines ? (line + 1) : nlines
1928 call popup_setoptions(a:winid, #{firstline: newline})
1929 return v:true
1930 elseif a:key == 'x'
1931 call popup_close(a:winid)
1932 return v:true
1933 endif
1934 endfunc
1935
1936 func PopupScroll()
1937 call popup_clear()
1938 let text =<< trim END
1939 1
1940 2
1941 3
1942 4
1943 long line long line long line long line long line long line
1944 long line long line long line long line long line long line
1945 long line long line long line long line long line long line
1946 END
1947 call popup_create(text, #{
1948 \ minwidth: 30,
1949 \ maxwidth: 30,
1950 \ minheight: 4,
1951 \ maxheight: 4,
1952 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +01001953 \ lastline: 4,
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001954 \ wrap: v:true,
1955 \ scrollbar: v:true,
1956 \ mapping: v:false,
1957 \ filter: funcref('Popup_filter')
1958 \ })
1959 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001960 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001961 map <silent> <F4> :call test_setmouse(4, 42)<CR>
1962 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001963 END
1964 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001965 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001966 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
1967
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001968 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001969 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001970 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
1971
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001972 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001973 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001974 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
1975
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001976 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001977 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001978 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
1979
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02001980 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb', firstline: 5})\<CR>")
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001981 " this scrolls two lines (half the window height)
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001982 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
1983 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
1984
1985 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1986 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
1987
1988 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02001989 " wait a bit, otherwise it fails sometimes (double click recognized?)
1990 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001991 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1992 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
1993
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001994 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1995 sleep 100m
1996 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1997 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
1998
1999 call term_sendkeys(buf, ":call ClickBot()\<CR>")
2000 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
2001
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002002 " remove the minwidth and maxheight
2003 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxheight: 0, minwidth: 0})\<CR>")
Bram Moolenaar7e0f4622019-09-17 21:23:39 +02002004 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002005 call VerifyScreenDump(buf, 'Test_popupwin_scroll_10', {})
2006
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002007 " check size with non-wrapping lines
2008 call term_sendkeys(buf, ":call PopupScroll()\<CR>")
2009 call VerifyScreenDump(buf, 'Test_popupwin_scroll_11', {})
2010
2011 " check size with wrapping lines
2012 call term_sendkeys(buf, "j")
2013 call VerifyScreenDump(buf, 'Test_popupwin_scroll_12', {})
2014 call term_sendkeys(buf, "x")
2015
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002016 " clean up
2017 call StopVimInTerminal(buf)
2018 call delete('XtestPopupScroll')
2019endfunc
2020
Bram Moolenaar437a7462019-07-05 20:17:22 +02002021func Test_popup_fitting_scrollbar()
2022 " this was causing a crash, divide by zero
2023 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002024 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002025 \ scrollbar: 1,
2026 \ maxwidth: 10,
2027 \ maxheight: 5,
2028 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02002029 redraw
2030 call popup_clear()
2031endfunc
2032
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002033func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002034 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002035
2036 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002037 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002038 let p = popup_create('test', opts)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002039 eval p->popup_settext('this is a text')
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002040 END
2041
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002042 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002043 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002044 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
2045
2046 " Setting to empty string clears it
2047 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
2048 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
2049
2050 " Setting a list
2051 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2052 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2053
2054 " Shrinking with a list
2055 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
2056 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
2057
2058 " Growing with a list
2059 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2060 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2061
2062 " Empty list clears
2063 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
2064 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
2065
2066 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002067 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002068 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
2069
2070 " clean up
2071 call StopVimInTerminal(buf)
2072 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002073endfunc
2074
2075func Test_popup_hidden()
2076 new
2077
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002078 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002079 redraw
2080 call assert_equal(0, popup_getpos(winid).visible)
2081 call popup_close(winid)
2082
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002083 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002084 redraw
2085 call assert_equal(0, popup_getpos(winid).visible)
2086 call popup_close(winid)
2087
2088 func QuitCallback(id, res)
2089 let s:cb_winid = a:id
2090 let s:cb_res = a:res
2091 endfunc
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002092 let winid = 'make a choice'->popup_dialog(#{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002093 \ filter: 'popup_filter_yesno',
2094 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002095 \ })
2096 redraw
2097 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02002098 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
2099 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002100 exe "normal anot used by filter\<Esc>"
2101 call assert_equal('not used by filter', getline(1))
2102
2103 call popup_show(winid)
2104 call feedkeys('y', "xt")
2105 call assert_equal(1, s:cb_res)
2106
2107 bwipe!
2108 delfunc QuitCallback
2109endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02002110
2111" Test options not checked elsewhere
2112func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002113 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002114 let options = popup_getoptions(winid)
2115 call assert_equal(1, options.wrap)
2116 call assert_equal(0, options.drag)
2117 call assert_equal('Beautiful', options.highlight)
2118
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002119 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002120 let options = popup_getoptions(winid)
2121 call assert_equal(0, options.wrap)
2122 call assert_equal(1, options.drag)
2123 call assert_equal('Another', options.highlight)
2124
2125 call popup_close(winid)
2126endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002127
2128func Test_popupwin_garbage_collect()
2129 func MyPopupFilter(x, winid, c)
2130 " NOP
2131 endfunc
2132
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002133 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002134 call test_garbagecollect_now()
2135 redraw
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02002136 " Must not crash caused by invalid memory access
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002137 call feedkeys('j', 'xt')
2138 call assert_true(v:true)
2139
2140 call popup_close(winid)
2141 delfunc MyPopupFilter
2142endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002143
Bram Moolenaar581ba392019-09-03 22:08:33 +02002144func Test_popupwin_filter_mode()
2145 func MyPopupFilter(winid, c)
2146 let s:typed = a:c
2147 if a:c == ':' || a:c == "\r" || a:c == 'v'
2148 " can start cmdline mode, get out, and start/stop Visual mode
2149 return 0
2150 endif
2151 return 1
2152 endfunc
2153
2154 " Normal, Visual and Insert mode
2155 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'nvi'})
2156 redraw
2157 call feedkeys('x', 'xt')
2158 call assert_equal('x', s:typed)
2159
2160 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2161 call assert_equal(':', s:typed)
2162 call assert_equal('foo', g:foo)
2163
2164 let @x = 'something'
2165 call feedkeys('v$"xy', 'xt')
2166 call assert_equal('y', s:typed)
2167 call assert_equal('something', @x) " yank command is filtered out
2168 call feedkeys('v', 'xt') " end Visual mode
2169
2170 call popup_close(winid)
2171
2172 " only Normal mode
2173 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'n'})
2174 redraw
2175 call feedkeys('x', 'xt')
2176 call assert_equal('x', s:typed)
2177
2178 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2179 call assert_equal(':', s:typed)
2180 call assert_equal('foo', g:foo)
2181
2182 let @x = 'something'
2183 call feedkeys('v$"xy', 'xt')
2184 call assert_equal('v', s:typed)
2185 call assert_notequal('something', @x)
2186
2187 call popup_close(winid)
2188
2189 " default: all modes
2190 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
2191 redraw
2192 call feedkeys('x', 'xt')
2193 call assert_equal('x', s:typed)
2194
2195 let g:foo = 'bar'
2196 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2197 call assert_equal("\r", s:typed)
2198 call assert_equal('bar', g:foo)
2199
2200 let @x = 'something'
2201 call feedkeys('v$"xy', 'xt')
2202 call assert_equal('y', s:typed)
2203 call assert_equal('something', @x) " yank command is filtered out
2204 call feedkeys('v', 'xt') " end Visual mode
2205
2206 call popup_close(winid)
2207 delfunc MyPopupFilter
2208endfunc
2209
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002210func Test_popupwin_filter_mouse()
2211 func MyPopupFilter(winid, c)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002212 let g:got_mousepos = getmousepos()
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002213 return 0
2214 endfunc
2215
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002216 call setline(1, ['.'->repeat(25)]->repeat(10))
2217 let winid = popup_create(['short', 'long line that will wrap', 'other'], #{
2218 \ line: 2,
2219 \ col: 4,
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002220 \ maxwidth: 12,
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002221 \ padding: [],
2222 \ border: [],
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002223 \ filter: 'MyPopupFilter',
2224 \ })
2225 redraw
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002226 " 123456789012345678901
2227 " 1 .....................
2228 " 2 ...+--------------+..
2229 " 3 ...| |..
2230 " 4 ...| short |..
2231 " 5 ...| long line th |..
2232 " 6 ...| at will wrap |..
2233 " 7 ...| other |..
2234 " 8 ...| |..
2235 " 9 ...+--------------+..
2236 " 10 .....................
2237 let tests = []
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002238
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002239 func AddItemOutsidePopup(tests, row, col)
2240 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2241 \ screenrow: a:row, screencol: a:col,
2242 \ winid: win_getid(), winrow: a:row, wincol: a:col,
2243 \ line: a:row, column: a:col,
2244 \ }})
2245 endfunc
2246 func AddItemInPopupBorder(tests, winid, row, col)
2247 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2248 \ screenrow: a:row, screencol: a:col,
2249 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2250 \ line: 0, column: 0,
2251 \ }})
2252 endfunc
2253 func AddItemInPopupText(tests, winid, row, col, textline, textcol)
2254 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2255 \ screenrow: a:row, screencol: a:col,
2256 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2257 \ line: a:textline, column: a:textcol,
2258 \ }})
2259 endfunc
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002260
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002261 " above and below popup
2262 for c in range(1, 21)
2263 call AddItemOutsidePopup(tests, 1, c)
2264 call AddItemOutsidePopup(tests, 10, c)
2265 endfor
2266 " left and right of popup
2267 for r in range(1, 10)
2268 call AddItemOutsidePopup(tests, r, 3)
2269 call AddItemOutsidePopup(tests, r, 20)
2270 endfor
2271 " top and bottom in popup
2272 for c in range(4, 19)
2273 call AddItemInPopupBorder(tests, winid, 2, c)
2274 call AddItemInPopupBorder(tests, winid, 3, c)
2275 call AddItemInPopupBorder(tests, winid, 8, c)
2276 call AddItemInPopupBorder(tests, winid, 9, c)
2277 endfor
2278 " left and right margin in popup
2279 for r in range(2, 9)
2280 call AddItemInPopupBorder(tests, winid, r, 4)
2281 call AddItemInPopupBorder(tests, winid, r, 5)
2282 call AddItemInPopupBorder(tests, winid, r, 18)
2283 call AddItemInPopupBorder(tests, winid, r, 19)
2284 endfor
2285 " text "short"
2286 call AddItemInPopupText(tests, winid, 4, 6, 1, 1)
2287 call AddItemInPopupText(tests, winid, 4, 10, 1, 5)
2288 call AddItemInPopupText(tests, winid, 4, 11, 1, 6)
2289 call AddItemInPopupText(tests, winid, 4, 17, 1, 6)
2290 " text "long line th"
2291 call AddItemInPopupText(tests, winid, 5, 6, 2, 1)
2292 call AddItemInPopupText(tests, winid, 5, 10, 2, 5)
2293 call AddItemInPopupText(tests, winid, 5, 17, 2, 12)
2294 " text "at will wrap"
2295 call AddItemInPopupText(tests, winid, 6, 6, 2, 13)
2296 call AddItemInPopupText(tests, winid, 6, 10, 2, 17)
2297 call AddItemInPopupText(tests, winid, 6, 17, 2, 24)
2298 " text "other"
2299 call AddItemInPopupText(tests, winid, 7, 6, 3, 1)
2300 call AddItemInPopupText(tests, winid, 7, 10, 3, 5)
2301 call AddItemInPopupText(tests, winid, 7, 11, 3, 6)
2302 call AddItemInPopupText(tests, winid, 7, 17, 3, 6)
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002303
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002304 for item in tests
2305 call test_setmouse(item.clickrow, item.clickcol)
2306 call feedkeys("\<LeftMouse>", 'xt')
2307 call assert_equal(item.result, g:got_mousepos)
2308 endfor
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002309
2310 call popup_close(winid)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002311 enew!
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002312 delfunc MyPopupFilter
2313endfunc
2314
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002315func Test_popupwin_with_buffer()
2316 call writefile(['some text', 'in a buffer'], 'XsomeFile')
2317 let buf = bufadd('XsomeFile')
2318 call assert_equal(0, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002319
2320 setlocal number
2321 call setbufvar(buf, "&wrapmargin", 13)
2322
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002323 let winid = popup_create(buf, {})
2324 call assert_notequal(0, winid)
2325 let pos = popup_getpos(winid)
2326 call assert_equal(2, pos.height)
2327 call assert_equal(1, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002328
2329 " window-local option is set to default, buffer-local is not
2330 call assert_equal(0, getwinvar(winid, '&number'))
2331 call assert_equal(13, getbufvar(buf, '&wrapmargin'))
2332
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002333 call popup_close(winid)
2334 call assert_equal({}, popup_getpos(winid))
2335 call assert_equal(1, bufloaded(buf))
2336 exe 'bwipe! ' .. buf
Bram Moolenaar46451042019-08-24 15:50:46 +02002337 setlocal nonumber
Bram Moolenaar7866b872019-07-01 22:21:01 +02002338
2339 edit test_popupwin.vim
2340 let winid = popup_create(bufnr(''), {})
2341 redraw
2342 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002343 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002344endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02002345
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002346func Test_popupwin_terminal_buffer()
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002347 CheckFeature terminal
2348
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002349 let ptybuf = term_start(&shell, #{hidden: 1})
2350 call assert_fails('let winnr = popup_create(ptybuf, #{})', 'E278:')
2351 exe 'bwipe! ' .. ptybuf
2352endfunc
2353
Bram Moolenaar934470e2019-09-01 23:27:05 +02002354func Test_popupwin_with_buffer_and_filter()
2355 new Xwithfilter
2356 call setline(1, range(100))
2357 let bufnr = bufnr()
2358 hide
2359
2360 func BufferFilter(win, key)
2361 if a:key == 'G'
2362 " recursive use of "G" does not cause problems.
2363 call win_execute(a:win, 'normal! G')
2364 return 1
2365 endif
2366 return 0
2367 endfunc
2368
2369 let winid = popup_create(bufnr, #{maxheight: 5, filter: 'BufferFilter'})
2370 call assert_equal(1, popup_getpos(winid).firstline)
2371 redraw
2372 call feedkeys("G", 'xt')
2373 call assert_equal(99, popup_getpos(winid).firstline)
2374
2375 call popup_close(winid)
2376 exe 'bwipe! ' .. bufnr
2377endfunc
2378
Bram Moolenaare296e312019-07-03 23:20:18 +02002379func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002380 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002381 \ maxwidth: 40,
2382 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02002383 \ })
2384 for top in range(1, 20)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002385 eval winid->popup_setoptions(#{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02002386 redraw
2387 call assert_equal(19, popup_getpos(winid).width)
2388 endfor
2389 call popup_clear()
2390endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02002391
2392func Test_popupwin_buf_close()
2393 let buf = bufadd('Xtestbuf')
2394 call bufload(buf)
2395 call setbufline(buf, 1, ['just', 'some', 'lines'])
2396 let winid = popup_create(buf, {})
2397 redraw
2398 call assert_equal(3, popup_getpos(winid).height)
2399 let bufinfo = getbufinfo(buf)[0]
2400 call assert_equal(1, bufinfo.changed)
2401 call assert_equal(0, bufinfo.hidden)
2402 call assert_equal(0, bufinfo.listed)
2403 call assert_equal(1, bufinfo.loaded)
2404 call assert_equal([], bufinfo.windows)
2405 call assert_equal([winid], bufinfo.popups)
2406
2407 call popup_close(winid)
2408 call assert_equal({}, popup_getpos(winid))
2409 let bufinfo = getbufinfo(buf)[0]
2410 call assert_equal(1, bufinfo.changed)
2411 call assert_equal(1, bufinfo.hidden)
2412 call assert_equal(0, bufinfo.listed)
2413 call assert_equal(1, bufinfo.loaded)
2414 call assert_equal([], bufinfo.windows)
2415 call assert_equal([], bufinfo.popups)
2416 exe 'bwipe! ' .. buf
2417endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02002418
2419func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002420 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02002421
2422 let lines =<< trim END
2423 call setline(1, range(1, 10))
2424 hi ScrollThumb ctermbg=blue
2425 hi ScrollBar ctermbg=red
2426 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002427 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002428 \ maxwidth: 10,
2429 \ maxheight: 3,
2430 \ pos : 'topleft',
2431 \ col : a:col,
2432 \ line : a:line,
2433 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02002434 \ })
2435 endfunc
2436 call PopupMenu(['x'], 1, 1)
2437 call PopupMenu(['123456789|'], 1, 16)
2438 call PopupMenu(['123456789|' .. ' '], 7, 1)
2439 call PopupMenu([repeat('123456789|', 100)], 7, 16)
2440 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
2441 END
2442 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002443 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02002444 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
2445
2446 " close the menu popupwin.
2447 call term_sendkeys(buf, " ")
2448 call term_sendkeys(buf, " ")
2449 call term_sendkeys(buf, " ")
2450 call term_sendkeys(buf, " ")
2451 call term_sendkeys(buf, " ")
2452
2453 " clean up
2454 call StopVimInTerminal(buf)
2455 call delete('XtestPopupMenuMaxWidth')
2456endfunc
2457
Bram Moolenaara901a372019-07-13 16:38:50 +02002458func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002459 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02002460
2461 let lines =<< trim END
2462 call setline(1, range(1, 20))
2463 hi ScrollThumb ctermbg=blue
2464 hi ScrollBar ctermbg=red
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002465 eval ['one', 'two', 'three', 'four', 'five',
2466 \ 'six', 'seven', 'eight', 'nine']
2467 \ ->popup_menu(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002468 \ minwidth: 8,
2469 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02002470 \ })
2471 END
2472 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002473 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02002474
2475 call term_sendkeys(buf, "j")
2476 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
2477
2478 call term_sendkeys(buf, "jjj")
2479 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
2480
2481 " if the cursor is the bottom line, it stays at the bottom line.
2482 call term_sendkeys(buf, repeat("j", 20))
2483 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
2484
2485 call term_sendkeys(buf, "kk")
2486 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
2487
2488 call term_sendkeys(buf, "k")
2489 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
2490
2491 " if the cursor is in the top line, it stays in the top line.
2492 call term_sendkeys(buf, repeat("k", 20))
2493 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
2494
2495 " close the menu popupwin.
2496 call term_sendkeys(buf, " ")
2497
2498 " clean up
2499 call StopVimInTerminal(buf)
2500 call delete('XtestPopupMenuScroll')
2501endfunc
2502
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002503func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002504 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002505
2506 let lines =<< trim END
2507 function! MyFilter(winid, key) abort
2508 if a:key == "0"
2509 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
2510 return 1
2511 endif
2512 if a:key == "G"
2513 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
2514 return 1
2515 endif
2516 if a:key == "j"
2517 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
2518 return 1
2519 endif
2520 if a:key == "k"
2521 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
2522 return 1
2523 endif
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002524 if a:key == ':'
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002525 call popup_close(a:winid)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002526 return 0
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002527 endif
2528 return 0
2529 endfunction
2530 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
2531 \ maxheight : 3,
2532 \ filter : 'MyFilter'
2533 \ })
2534 END
2535 call writefile(lines, 'XtestPopupMenuFilter')
2536 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
2537
2538 call term_sendkeys(buf, "j")
2539 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
2540
2541 call term_sendkeys(buf, "k")
2542 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
2543
2544 call term_sendkeys(buf, "G")
2545 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
2546
2547 call term_sendkeys(buf, "0")
2548 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
2549
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002550 " check that when the popup is closed in the filter the screen is redrawn
2551 call term_sendkeys(buf, ":")
2552 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_5', {})
2553 call term_sendkeys(buf, "\<CR>")
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002554
2555 " clean up
2556 call StopVimInTerminal(buf)
2557 call delete('XtestPopupMenuFilter')
2558endfunc
2559
2560func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002561 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002562
2563 let winid = popup_create('some text', {})
2564 call assert_equal(0, popup_getoptions(winid).cursorline)
2565 call popup_close(winid)
2566
2567 let winid = popup_create('some text', #{ cursorline: 1, })
2568 call assert_equal(1, popup_getoptions(winid).cursorline)
2569 call popup_close(winid)
2570
2571 let winid = popup_create('some text', #{ cursorline: 0, })
2572 call assert_equal(0, popup_getoptions(winid).cursorline)
2573 call popup_close(winid)
2574
2575 let winid = popup_menu('some text', {})
2576 call assert_equal(1, popup_getoptions(winid).cursorline)
2577 call popup_close(winid)
2578
2579 let winid = popup_menu('some text', #{ cursorline: 1, })
2580 call assert_equal(1, popup_getoptions(winid).cursorline)
2581 call popup_close(winid)
2582
2583 let winid = popup_menu('some text', #{ cursorline: 0, })
2584 call assert_equal(0, popup_getoptions(winid).cursorline)
2585 call popup_close(winid)
2586
2587 " ---------
2588 " Pattern 1
2589 " ---------
2590 let lines =<< trim END
2591 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
2592 END
2593 call writefile(lines, 'XtestPopupCursorLine')
2594 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2595 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
2596 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2597 call StopVimInTerminal(buf)
2598
2599 " ---------
2600 " Pattern 2
2601 " ---------
2602 let lines =<< trim END
2603 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
2604 END
2605 call writefile(lines, 'XtestPopupCursorLine')
2606 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2607 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
2608 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2609 call StopVimInTerminal(buf)
2610
2611 " ---------
2612 " Pattern 3
2613 " ---------
2614 let lines =<< trim END
2615 function! MyFilter(winid, key) abort
2616 if a:key == "j"
2617 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2618 return 1
2619 endif
2620 if a:key == 'x'
2621 call popup_close(a:winid)
2622 return 1
2623 endif
2624 return 0
2625 endfunction
2626 call popup_menu(['111', '222', '333'], #{
2627 \ cursorline : 0,
2628 \ maxheight : 2,
2629 \ filter : 'MyFilter',
2630 \ })
2631 END
2632 call writefile(lines, 'XtestPopupCursorLine')
2633 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2634 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
2635 call term_sendkeys(buf, "j")
2636 call term_sendkeys(buf, "j")
2637 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
2638 call term_sendkeys(buf, "x")
2639 call StopVimInTerminal(buf)
2640
2641 " ---------
2642 " Pattern 4
2643 " ---------
2644 let lines =<< trim END
2645 function! MyFilter(winid, key) abort
2646 if a:key == "j"
2647 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2648 return 1
2649 endif
2650 if a:key == 'x'
2651 call popup_close(a:winid)
2652 return 1
2653 endif
2654 return 0
2655 endfunction
2656 call popup_menu(['111', '222', '333'], #{
2657 \ cursorline : 1,
2658 \ maxheight : 2,
2659 \ filter : 'MyFilter',
2660 \ })
2661 END
2662 call writefile(lines, 'XtestPopupCursorLine')
2663 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2664 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
2665 call term_sendkeys(buf, "j")
2666 call term_sendkeys(buf, "j")
2667 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
2668 call term_sendkeys(buf, "x")
2669 call StopVimInTerminal(buf)
2670
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02002671 " ---------
2672 " Cursor in second line when creating the popup
2673 " ---------
2674 let lines =<< trim END
2675 let winid = popup_create(['111', '222', '333'], #{
2676 \ cursorline : 1,
2677 \ })
2678 call win_execute(winid, "2")
2679 END
2680 call writefile(lines, 'XtestPopupCursorLine')
2681 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2682 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_7', {})
2683 call StopVimInTerminal(buf)
2684
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002685 call delete('XtestPopupCursorLine')
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01002686
2687 " ---------
2688 " Use current buffer for popupmenu
2689 " ---------
2690 let lines =<< trim END
2691 call setline(1, ['one', 'two', 'three'])
2692 let winid = popup_create(bufnr('%'), #{
2693 \ cursorline : 1,
2694 \ })
2695 call win_execute(winid, "2")
2696 END
2697 call writefile(lines, 'XtestPopupCursorLine')
2698 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2699 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_8', {})
2700 call StopVimInTerminal(buf)
2701
2702 call delete('XtestPopupCursorLine')
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002703endfunc
2704
Bram Moolenaarf914a332019-07-20 15:09:56 +02002705func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002706 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002707 CheckFeature quickfix
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002708
Bram Moolenaarf914a332019-07-20 15:09:56 +02002709 call writefile([
2710 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
2711 \ "another\tXtagfile\t/^this is another",
2712 \ "theword\tXtagfile\t/^theword"],
2713 \ 'Xtags')
2714 call writefile(range(1,20)
2715 \ + ['theword is here']
2716 \ + range(22, 27)
2717 \ + ['this is another place']
2718 \ + range(29, 40),
2719 \ "Xtagfile")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002720 call writefile(range(1,10)
2721 \ + ['searched word is here']
2722 \ + range(12, 20),
2723 \ "Xheader.h")
Bram Moolenaarf914a332019-07-20 15:09:56 +02002724 let lines =<< trim END
2725 set tags=Xtags
2726 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002727 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002728 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002729 \ 'three',
2730 \ 'four',
2731 \ 'five',
2732 \ 'six',
2733 \ 'seven',
2734 \ 'find theword somewhere',
2735 \ 'nine',
2736 \ 'this is another word',
2737 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02002738 set previewpopup=height:4,width:40
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002739 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02002740 END
2741 call writefile(lines, 'XtestPreviewPopup')
2742 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
2743
2744 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
2745 call term_sendkeys(buf, ":\<CR>")
2746 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
2747
2748 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2749 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
2750
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002751 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002752 call term_sendkeys(buf, ":\<CR>")
2753 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
2754
2755 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2756 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
2757
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002758 call term_sendkeys(buf, ":cd ..\<CR>:\<CR>")
2759 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002760 call term_sendkeys(buf, ":cd testdir\<CR>")
2761
2762 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02002763 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002764 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
2765
2766 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
2767 call term_sendkeys(buf, ":\<CR>")
2768 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
2769
2770 call term_sendkeys(buf, ":pclose\<CR>")
2771 call term_sendkeys(buf, ":psearch searched\<CR>")
2772 call term_sendkeys(buf, ":\<CR>")
2773 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002774
Bram Moolenaarf914a332019-07-20 15:09:56 +02002775 call StopVimInTerminal(buf)
2776 call delete('Xtags')
2777 call delete('Xtagfile')
2778 call delete('XtestPreviewPopup')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002779 call delete('Xheader.h')
Bram Moolenaarf914a332019-07-20 15:09:56 +02002780endfunc
2781
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002782func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002783 let lines =<< trim END
2784 set completeopt+=preview,popup
2785 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002786 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002787
2788 func CompleteFuncDict(findstart, base)
2789 if a:findstart
2790 if col('.') > 10
2791 return col('.') - 10
2792 endif
2793 return 0
2794 endif
2795
2796 return {
2797 \ 'words': [
2798 \ {
2799 \ 'word': 'aword',
2800 \ 'abbr': 'wrd',
2801 \ 'menu': 'extra text',
2802 \ 'info': 'words are cool',
2803 \ 'kind': 'W',
2804 \ 'user_data': 'test'
2805 \ },
2806 \ {
2807 \ 'word': 'anotherword',
2808 \ 'abbr': 'anotwrd',
2809 \ 'menu': 'extra text',
2810 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
2811 \ 'kind': 'W',
2812 \ 'user_data': 'notest'
2813 \ },
2814 \ {
2815 \ 'word': 'noinfo',
2816 \ 'abbr': 'noawrd',
2817 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002818 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002819 \ 'kind': 'W',
2820 \ 'user_data': 'notest'
2821 \ },
2822 \ {
2823 \ 'word': 'thatword',
2824 \ 'abbr': 'thatwrd',
2825 \ 'menu': 'extra text',
2826 \ 'info': 'that word is cool',
2827 \ 'kind': 'W',
2828 \ 'user_data': 'notest'
2829 \ },
2830 \ ]
2831 \ }
2832 endfunc
2833 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002834 func ChangeColor()
2835 let id = popup_findinfo()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002836 eval id->popup_setoptions(#{highlight: 'InfoPopup'})
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002837 endfunc
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002838
2839 func InfoHidden()
2840 set completepopup=height:4,border:off,align:menu
2841 set completeopt-=popup completeopt+=popuphidden
2842 au CompleteChanged * call HandleChange()
2843 endfunc
2844
2845 let s:counter = 0
2846 func HandleChange()
2847 let s:counter += 1
2848 let selected = complete_info(['selected']).selected
2849 if selected <= 0
2850 " First time: do nothing, info remains hidden
2851 return
2852 endif
2853 if selected == 1
2854 " Second time: show info right away
2855 let id = popup_findinfo()
2856 if id
2857 call popup_settext(id, 'immediate info ' .. s:counter)
2858 call popup_show(id)
2859 endif
2860 else
2861 " Third time: show info after a short delay
2862 call timer_start(100, 'ShowInfo')
2863 endif
2864 endfunc
2865
2866 func ShowInfo(...)
2867 let id = popup_findinfo()
2868 if id
2869 call popup_settext(id, 'async info ' .. s:counter)
2870 call popup_show(id)
2871 endif
2872 endfunc
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002873 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002874 return lines
2875endfunc
2876
2877func Test_popupmenu_info_border()
2878 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002879 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002880
2881 let lines = Get_popupmenu_lines()
2882 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002883 call writefile(lines, 'XtestInfoPopup')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002884
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002885 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
2886 call term_wait(buf, 50)
2887
2888 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2889 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
2890
2891 call term_sendkeys(buf, "\<C-N>")
2892 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
2893
2894 call term_sendkeys(buf, "\<C-N>")
2895 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
2896
2897 call term_sendkeys(buf, "\<C-N>\<C-N>")
2898 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
2899
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02002900 " info on the left with scrollbar
2901 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
2902 call term_sendkeys(buf, "\<C-N>\<C-N>")
2903 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
2904
Bram Moolenaar202c3f72019-11-21 12:12:35 +01002905 " Test that the popupmenu's scrollbar and infopopup do not overlap
2906 call term_sendkeys(buf, "\<Esc>")
2907 call term_sendkeys(buf, ":set pumheight=3\<CR>")
2908 call term_sendkeys(buf, "cc\<C-X>\<C-U>")
2909 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_6', {})
2910
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002911 call StopVimInTerminal(buf)
2912 call delete('XtestInfoPopup')
2913endfunc
2914
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002915func Test_popupmenu_info_noborder()
2916 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002917 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002918
2919 let lines = Get_popupmenu_lines()
2920 call add(lines, 'set completepopup=height:4,border:off')
2921 call writefile(lines, 'XtestInfoPopupNb')
2922
2923 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
2924 call term_wait(buf, 50)
2925
2926 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2927 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
2928
2929 call StopVimInTerminal(buf)
2930 call delete('XtestInfoPopupNb')
2931endfunc
2932
Bram Moolenaar258cef52019-08-21 17:29:29 +02002933func Test_popupmenu_info_align_menu()
2934 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002935 CheckFeature quickfix
Bram Moolenaar258cef52019-08-21 17:29:29 +02002936
2937 let lines = Get_popupmenu_lines()
2938 call add(lines, 'set completepopup=height:4,border:off,align:menu')
2939 call writefile(lines, 'XtestInfoPopupNb')
2940
2941 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
2942 call term_wait(buf, 50)
2943
2944 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2945 call term_sendkeys(buf, "\<C-N>")
2946 call term_sendkeys(buf, "\<C-N>")
2947 call term_sendkeys(buf, "\<C-N>")
2948 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
2949
2950 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
2951 call term_sendkeys(buf, "\<C-N>")
2952 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
2953
2954 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002955 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02002956 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
2957 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
2958 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
2959
2960 call StopVimInTerminal(buf)
2961 call delete('XtestInfoPopupNb')
2962endfunc
2963
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002964func Test_popupmenu_info_hidden()
2965 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002966 CheckFeature quickfix
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002967
2968 let lines = Get_popupmenu_lines()
2969 call add(lines, 'call InfoHidden()')
2970 call writefile(lines, 'XtestInfoPopupHidden')
2971
2972 let buf = RunVimInTerminal('-S XtestInfoPopupHidden', #{rows: 14})
2973 call term_wait(buf, 50)
2974
2975 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2976 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_1', {})
2977
2978 call term_sendkeys(buf, "\<C-N>")
2979 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_2', {})
2980
2981 call term_sendkeys(buf, "\<C-N>")
2982 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_3', {})
2983
2984 call term_sendkeys(buf, "\<Esc>")
2985 call StopVimInTerminal(buf)
2986 call delete('XtestInfoPopupHidden')
2987endfunc
2988
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01002989func Test_popupmenu_info_too_wide()
2990 CheckScreendump
2991 CheckFeature quickfix
2992
2993 let lines =<< trim END
2994 call setline(1, range(10))
2995
2996 set completeopt+=preview,popup
2997 set completepopup=align:menu
2998 set omnifunc=OmniFunc
2999 hi InfoPopup ctermbg=lightgrey
3000
3001 func OmniFunc(findstart, base)
3002 if a:findstart
3003 return 0
3004 endif
3005
3006 let menuText = 'some long text to make sure the menu takes up all of the width of the window'
3007 return #{
3008 \ words: [
3009 \ #{
3010 \ word: 'scrap',
3011 \ menu: menuText,
3012 \ info: "other words are\ncooler than this and some more text\nto make wrap",
3013 \ },
3014 \ #{
3015 \ word: 'scappier',
3016 \ menu: menuText,
3017 \ info: 'words are cool',
3018 \ },
3019 \ #{
3020 \ word: 'scrappier2',
3021 \ menu: menuText,
3022 \ info: 'words are cool',
3023 \ },
3024 \ ]
3025 \ }
3026 endfunc
3027 END
3028
3029 call writefile(lines, 'XtestInfoPopupWide')
3030 let buf = RunVimInTerminal('-S XtestInfoPopupWide', #{rows: 8})
3031 call term_wait(buf, 50)
3032
3033 call term_sendkeys(buf, "Ascr\<C-X>\<C-O>")
3034 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_wide_1', {})
3035
3036 call term_sendkeys(buf, "\<Esc>")
3037 call StopVimInTerminal(buf)
3038 call delete('XtestInfoPopupWide')
3039endfunc
3040
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003041func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02003042 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003043 call popup_clear()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003044 let winid = 'nothing wrong'->popup_notification({})
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003045 call assert_equal(bufnr, winbufnr(winid))
3046 call popup_clear()
3047endfunc
3048
Bram Moolenaar1824f452019-10-02 23:06:46 +02003049func Test_popupwin_getoptions_tablocal()
3050 topleft split
3051 let win1 = popup_create('nothing', #{maxheight: 8})
3052 let win2 = popup_create('something', #{maxheight: 10})
3053 let win3 = popup_create('something', #{maxheight: 15})
3054 call assert_equal(8, popup_getoptions(win1).maxheight)
3055 call assert_equal(10, popup_getoptions(win2).maxheight)
3056 call assert_equal(15, popup_getoptions(win3).maxheight)
3057 call popup_clear()
3058 quit
3059endfunc
3060
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02003061func Test_popupwin_cancel()
3062 let win1 = popup_create('one', #{line: 5, filter: {... -> 0}})
3063 let win2 = popup_create('two', #{line: 10, filter: {... -> 0}})
3064 let win3 = popup_create('three', #{line: 15, filter: {... -> 0}})
3065 call assert_equal(5, popup_getpos(win1).line)
3066 call assert_equal(10, popup_getpos(win2).line)
3067 call assert_equal(15, popup_getpos(win3).line)
3068 " TODO: this also works without patch 8.1.2110
3069 call feedkeys("\<C-C>", 'xt')
3070 call assert_equal(5, popup_getpos(win1).line)
3071 call assert_equal(10, popup_getpos(win2).line)
3072 call assert_equal({}, popup_getpos(win3))
3073 call feedkeys("\<C-C>", 'xt')
3074 call assert_equal(5, popup_getpos(win1).line)
3075 call assert_equal({}, popup_getpos(win2))
3076 call assert_equal({}, popup_getpos(win3))
3077 call feedkeys("\<C-C>", 'xt')
3078 call assert_equal({}, popup_getpos(win1))
3079 call assert_equal({}, popup_getpos(win2))
3080 call assert_equal({}, popup_getpos(win3))
3081endfunc
3082
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003083func Test_popupwin_filter_redraw()
3084 " Create two popups with a filter that closes the popup when typing "0".
3085 " Both popups should close, even though the redraw also calls
3086 " popup_reset_handled()
3087
3088 func CloseFilter(winid, key)
3089 if a:key == '0'
3090 call popup_close(a:winid)
3091 redraw
3092 endif
3093 return 0 " pass the key
3094 endfunc
3095
3096 let id1 = popup_create('first one', #{
3097 \ line: 1,
3098 \ col: 1,
3099 \ filter: 'CloseFilter',
3100 \ })
3101 let id2 = popup_create('second one', #{
3102 \ line: 9,
3103 \ col: 1,
3104 \ filter: 'CloseFilter',
3105 \ })
3106 call assert_equal(1, popup_getpos(id1).line)
3107 call assert_equal(9, popup_getpos(id2).line)
3108
3109 call feedkeys('0', 'xt')
3110 call assert_equal({}, popup_getpos(id1))
3111 call assert_equal({}, popup_getpos(id2))
3112
3113 call popup_clear()
3114 delfunc CloseFilter
3115endfunc
3116
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003117func Test_popupwin_double_width()
3118 CheckScreendump
3119
3120 let lines =<< trim END
3121 call setline(1, 'x你好世界你好世你好世界你好')
3122 call setline(2, '你好世界你好世你好世界你好')
3123 call setline(3, 'x你好世界你好世你好世界你好')
3124 call popup_create('你好,世界 - 你好,世界xxxxx', #{line: 1, col: 3, maxwidth: 14})
3125 END
3126 call writefile(lines, 'XtestPopupWide')
3127
3128 let buf = RunVimInTerminal('-S XtestPopupWide', #{rows: 10})
3129 call VerifyScreenDump(buf, 'Test_popupwin_doublewidth_1', {})
3130
3131 call StopVimInTerminal(buf)
3132 call delete('XtestPopupWide')
3133endfunc
3134
3135func Test_popupwin_sign()
3136 CheckScreendump
3137
3138 let lines =<< trim END
3139 call setline(1, range(10))
3140 call sign_define('Current', {
3141 \ 'text': '>>',
3142 \ 'texthl': 'WarningMsg',
3143 \ 'linehl': 'Error',
3144 \ })
3145 call sign_define('Other', {
3146 \ 'text': '#!',
3147 \ 'texthl': 'Error',
3148 \ 'linehl': 'Search',
3149 \ })
3150 let winid = popup_create(['hello', 'bright', 'world'], {
3151 \ 'minwidth': 20,
3152 \ })
3153 call setwinvar(winid, "&signcolumn", "yes")
3154 let winbufnr = winbufnr(winid)
3155
3156 " add sign to current buffer, shows
3157 call sign_place(1, 'Selected', 'Current', bufnr('%'), {'lnum': 1})
3158 " add sign to current buffer, does not show
3159 call sign_place(2, 'PopUpSelected', 'Other', bufnr('%'), {'lnum': 2})
3160
3161 " add sign to popup buffer, shows
3162 call sign_place(3, 'PopUpSelected', 'Other', winbufnr, {'lnum': 1})
3163 " add sign to popup buffer, does not show
3164 call sign_place(4, 'Selected', 'Current', winbufnr, {'lnum': 2})
3165 END
3166 call writefile(lines, 'XtestPopupSign')
3167
3168 let buf = RunVimInTerminal('-S XtestPopupSign', #{rows: 10})
3169 call VerifyScreenDump(buf, 'Test_popupwin_sign_1', {})
3170
3171 call StopVimInTerminal(buf)
3172 call delete('XtestPopupSign')
3173endfunc
3174
Bram Moolenaar99ebf222019-12-10 23:44:48 +01003175func Test_popupwin_bufnr()
3176 let popwin = popup_create(['blah'], #{})
3177 let popbuf = winbufnr(popwin)
3178 split asdfasdf
3179 let newbuf = bufnr()
3180 call assert_true(newbuf > popbuf, 'New buffer number is higher')
3181 call assert_equal(newbuf, bufnr('$'))
3182 call popup_clear()
3183 let popwin = popup_create(['blah'], #{})
3184 " reuses previous buffer number
3185 call assert_equal(popbuf, winbufnr(popwin))
3186 call assert_equal(newbuf, bufnr('$'))
3187
3188 call popup_clear()
3189 bwipe!
3190endfunc
3191
Bram Moolenaar331bafd2019-07-20 17:46:05 +02003192" vim: shiftwidth=2 sts=2