blob: 45883b1e83338fb6c249cd94006d9e3a411a5d34 [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001" Tests for popup windows
2
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004CheckFeature popupwin
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005
6source screendump.vim
7
8func Test_simple_popup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02009 CheckScreendump
10
Bram Moolenaare7eb9272019-06-24 00:58:07 +020011 let lines =<< trim END
12 call setline(1, range(1, 100))
13 hi PopupColor1 ctermbg=lightblue
14 hi PopupColor2 ctermbg=lightcyan
Bram Moolenaar1666ac92019-11-10 17:22:31 +010015 hi EndOfBuffer ctermbg=lightgrey
Bram Moolenaare7eb9272019-06-24 00:58:07 +020016 hi Comment ctermfg=red
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020017 call prop_type_add('comment', #{highlight: 'Comment'})
18 let winid = popup_create('hello there', #{line: 3, col: 11, minwidth: 20, highlight: 'PopupColor1'})
19 let winid2 = popup_create(['another one', 'another two', 'another three'], #{line: 3, col: 25, minwidth: 20})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020020 call setwinvar(winid2, '&wincolor', 'PopupColor2')
21 END
22 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020023 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar4d784b22019-05-25 19:51:39 +020024 call VerifyScreenDump(buf, 'Test_popupwin_01', {})
25
Bram Moolenaarec583842019-05-26 14:11:23 +020026 " Add a tabpage
27 call term_sendkeys(buf, ":tabnew\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020028 call term_sendkeys(buf, ":let popupwin = popup_create(["
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020029 \ .. "#{text: 'other tab'},"
30 \ .. "#{text: 'a comment line', props: [#{"
Bram Moolenaard5abb4c2019-07-13 22:46:10 +020031 \ .. "col: 3, length: 7, minwidth: 20, type: 'comment'"
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020032 \ .. "}]},"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020033 \ .. "], #{line: 4, col: 9, minwidth: 20})\<CR>")
Bram Moolenaarec583842019-05-26 14:11:23 +020034 call VerifyScreenDump(buf, 'Test_popupwin_02', {})
35
36 " switch back to first tabpage
37 call term_sendkeys(buf, "gt")
38 call VerifyScreenDump(buf, 'Test_popupwin_03', {})
39
40 " close that tabpage
41 call term_sendkeys(buf, ":quit!\<CR>")
42 call VerifyScreenDump(buf, 'Test_popupwin_04', {})
43
Bram Moolenaar202d9822019-06-11 21:56:30 +020044 " set 'columns' to a small value, size must be recomputed
45 call term_sendkeys(buf, ":let cols = &columns\<CR>")
46 call term_sendkeys(buf, ":set columns=12\<CR>")
47 call VerifyScreenDump(buf, 'Test_popupwin_04a', {})
48 call term_sendkeys(buf, ":let &columns = cols\<CR>")
49
Bram Moolenaar17146962019-05-30 00:12:11 +020050 " resize popup, show empty line at bottom
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020051 call term_sendkeys(buf, ":call popup_move(popupwin, #{minwidth: 15, maxwidth: 25, minheight: 3, maxheight: 5})\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020052 call term_sendkeys(buf, ":redraw\<CR>")
53 call VerifyScreenDump(buf, 'Test_popupwin_05', {})
54
Bram Moolenaar17146962019-05-30 00:12:11 +020055 " show not fitting line at bottom
56 call term_sendkeys(buf, ":call setbufline(winbufnr(popupwin), 3, 'this line will not fit here')\<CR>")
57 call term_sendkeys(buf, ":redraw\<CR>")
58 call VerifyScreenDump(buf, 'Test_popupwin_06', {})
59
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020060 " move popup over ruler
61 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020062 call term_sendkeys(buf, ":call popup_move(popupwin, #{line: 7, col: 55})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +020063 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020064 call VerifyScreenDump(buf, 'Test_popupwin_07', {})
65
66 " clear all popups after moving the cursor a bit, so that ruler is updated
67 call term_sendkeys(buf, "axxx\<Esc>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +020068 call TermWait(buf)
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020069 call term_sendkeys(buf, "0")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +020070 call TermWait(buf)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +020071 call term_sendkeys(buf, ":call popup_clear()\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020072 call VerifyScreenDump(buf, 'Test_popupwin_08', {})
73
Bram Moolenaar4d784b22019-05-25 19:51:39 +020074 " clean up
75 call StopVimInTerminal(buf)
76 call delete('XtestPopup')
77endfunc
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020078
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020079func Test_popup_with_border_and_padding()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +020080 CheckScreendump
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020081
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020082 for iter in range(0, 1)
Bram Moolenaare7eb9272019-06-24 00:58:07 +020083 let lines =<< trim END
84 call setline(1, range(1, 100))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020085 call popup_create('hello border', #{line: 2, col: 3, border: []})
86 call popup_create('hello padding', #{line: 2, col: 23, padding: []})
Bram Moolenaarc363fe12019-08-04 18:13:46 +020087 call popup_create('hello both', #{line: 2, col: 43, border: [], padding: [], highlight: 'Normal'})
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020088 call popup_create('border TL', #{line: 6, col: 3, border: [1, 0, 0, 4]})
Bram Moolenaarb0992022020-01-30 14:55:42 +010089 call popup_create('paddings', #{line: 6, col: 23, padding: range(1, 4)})
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020090 call popup_create('wrapped longer text', #{line: 8, col: 55, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
91 call popup_create('right aligned text', #{line: 11, col: 56, wrap: 0, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
Bram Moolenaarfbcdf672020-01-06 23:07:48 +010092 call popup_create('X', #{line: 2, col: 73})
93 call popup_create('X', #{line: 3, col: 74})
94 call popup_create('X', #{line: 4, col: 75})
95 call popup_create('X', #{line: 5, col: 76})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020096 END
97 call insert(lines, iter == 1 ? '' : 'set enc=latin1')
98 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020099 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 15})
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +0200100 call VerifyScreenDump(buf, 'Test_popupwin_2' .. iter, {})
101
102 call StopVimInTerminal(buf)
103 call delete('XtestPopupBorder')
104 endfor
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200105
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200106 let lines =<< trim END
107 call setline(1, range(1, 100))
108 hi BlueColor ctermbg=lightblue
109 hi TopColor ctermbg=253
110 hi RightColor ctermbg=245
111 hi BottomColor ctermbg=240
112 hi LeftColor ctermbg=248
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200113 call popup_create('hello border', #{line: 2, col: 3, border: [], borderhighlight: ['BlueColor']})
114 call popup_create(['hello border', 'and more'], #{line: 2, col: 23, border: [], borderhighlight: ['TopColor', 'RightColor', 'BottomColor', 'LeftColor']})
115 call popup_create(['hello border', 'lines only'], #{line: 2, col: 43, border: [], borderhighlight: ['BlueColor'], borderchars: ['x']})
116 call popup_create(['hello border', 'with corners'], #{line: 2, col: 60, border: [], borderhighlight: ['BlueColor'], borderchars: ['x', '#']})
117 let winid = popup_create(['hello border', 'with numbers'], #{line: 6, col: 3, border: [], borderhighlight: ['BlueColor'], borderchars: ['0', '1', '2', '3', '4', '5', '6', '7']})
118 call popup_create(['hello border', 'just blanks'], #{line: 7, col: 23, border: [], borderhighlight: ['BlueColor'], borderchars: [' ']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200119 func MultiByte()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200120 call popup_create(['hello'], #{line: 8, col: 43, border: [], borderchars: ['─', '│', '─', '│', '┌', '┐', '┘', '└']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200121 endfunc
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200122 END
123 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200124 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 12})
Bram Moolenaar790498b2019-06-01 22:15:29 +0200125 call VerifyScreenDump(buf, 'Test_popupwin_22', {})
126
Bram Moolenaarad24a712019-06-17 20:05:45 +0200127 " check that changing borderchars triggers a redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200128 call term_sendkeys(buf, ":call popup_setoptions(winid, #{borderchars: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']})\<CR>")
Bram Moolenaarad24a712019-06-17 20:05:45 +0200129 call VerifyScreenDump(buf, 'Test_popupwin_23', {})
130
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200131 " check multi-byte border only with 'ambiwidth' single
132 if &ambiwidth == 'single'
133 call term_sendkeys(buf, ":call MultiByte()\<CR>")
134 call VerifyScreenDump(buf, 'Test_popupwin_24', {})
135 endif
136
Bram Moolenaar790498b2019-06-01 22:15:29 +0200137 call StopVimInTerminal(buf)
138 call delete('XtestPopupBorder')
139
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200140 let with_border_or_padding = #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200141 \ line: 2,
142 \ core_line: 3,
143 \ col: 3,
144 \ core_col: 4,
145 \ width: 14,
146 \ core_width: 12,
147 \ height: 3,
148 \ core_height: 1,
149 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100150 \ lastline: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200151 \ scrollbar: 0,
152 \ visible: 1}
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200153 let winid = popup_create('hello border', #{line: 2, col: 3, border: []})",
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200154 call assert_equal(with_border_or_padding, winid->popup_getpos())
Bram Moolenaarae943152019-06-16 22:54:14 +0200155 let options = popup_getoptions(winid)
156 call assert_equal([], options.border)
157 call assert_false(has_key(options, "padding"))
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200158
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200159 let winid = popup_create('hello padding', #{line: 2, col: 3, padding: []})
Bram Moolenaarae943152019-06-16 22:54:14 +0200160 let with_border_or_padding.width = 15
161 let with_border_or_padding.core_width = 13
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200162 call assert_equal(with_border_or_padding, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200163 let options = popup_getoptions(winid)
164 call assert_false(has_key(options, "border"))
165 call assert_equal([], options.padding)
166
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200167 call popup_setoptions(winid, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200168 \ padding: [1, 2, 3, 4],
169 \ border: [4, 0, 7, 8],
170 \ borderhighlight: ['Top', 'Right', 'Bottom', 'Left'],
171 \ borderchars: ['1', '^', '2', '>', '3', 'v', '4', '<'],
Bram Moolenaarae943152019-06-16 22:54:14 +0200172 \ })
173 let options = popup_getoptions(winid)
174 call assert_equal([1, 0, 1, 1], options.border)
175 call assert_equal([1, 2, 3, 4], options.padding)
176 call assert_equal(['Top', 'Right', 'Bottom', 'Left'], options.borderhighlight)
177 call assert_equal(['1', '^', '2', '>', '3', 'v', '4', '<'], options.borderchars)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200178
Bram Moolenaara1396152019-10-20 18:46:05 +0200179 " Check that popup_setoptions() takes the output of popup_getoptions()
180 call popup_setoptions(winid, options)
181 call assert_equal(options, popup_getoptions(winid))
182
Bram Moolenaarb0992022020-01-30 14:55:42 +0100183 " Check that range() doesn't crash
184 call popup_setoptions(winid, #{
185 \ padding: range(1, 4),
186 \ border: range(5, 8),
187 \ borderhighlight: range(4),
188 \ borderchars: range(8),
189 \ })
190
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200191 let winid = popup_create('hello both', #{line: 3, col: 8, border: [], padding: []})
192 call assert_equal(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200193 \ line: 3,
194 \ core_line: 5,
195 \ col: 8,
196 \ core_col: 10,
197 \ width: 14,
198 \ core_width: 10,
199 \ height: 5,
200 \ scrollbar: 0,
201 \ core_height: 1,
202 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100203 \ lastline: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200204 \ visible: 1}, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200205
206 call popup_clear()
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200207endfunc
208
Bram Moolenaarb4230122019-05-30 18:40:53 +0200209func Test_popup_with_syntax_win_execute()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200210 CheckScreendump
211
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200212 let lines =<< trim END
213 call setline(1, range(1, 100))
214 hi PopupColor ctermbg=lightblue
215 let winid = popup_create([
216 \ '#include <stdio.h>',
217 \ 'int main(void)',
218 \ '{',
219 \ ' printf(123);',
220 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200221 \], #{line: 3, col: 25, highlight: 'PopupColor'})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200222 call win_execute(winid, 'set syntax=cpp')
223 END
224 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200225 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200226 call VerifyScreenDump(buf, 'Test_popupwin_10', {})
227
228 " clean up
229 call StopVimInTerminal(buf)
230 call delete('XtestPopup')
231endfunc
232
233func Test_popup_with_syntax_setbufvar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200234 CheckScreendump
235
Bram Moolenaar402502d2019-05-30 22:07:36 +0200236 let lines =<< trim END
237 call setline(1, range(1, 100))
238 hi PopupColor ctermbg=lightgrey
239 let winid = popup_create([
240 \ '#include <stdio.h>',
241 \ 'int main(void)',
242 \ '{',
Bram Moolenaare089c3f2019-07-09 20:25:25 +0200243 \ "\tprintf(567);",
Bram Moolenaar402502d2019-05-30 22:07:36 +0200244 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200245 \], #{line: 3, col: 21, highlight: 'PopupColor'})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200246 call setbufvar(winbufnr(winid), '&syntax', 'cpp')
247 END
248 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200249 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200250 call VerifyScreenDump(buf, 'Test_popupwin_11', {})
251
252 " clean up
253 call StopVimInTerminal(buf)
254 call delete('XtestPopup')
255endfunc
256
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200257func Test_popup_with_matches()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200258 CheckScreendump
259
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200260 let lines =<< trim END
261 call setline(1, ['111 222 333', '444 555 666'])
262 let winid = popup_create([
263 \ '111 222 333',
264 \ '444 555 666',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200265 \], #{line: 3, col: 10, border: []})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200266 set hlsearch
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100267 hi VeryBlue ctermfg=blue guifg=blue
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200268 /666
269 call matchadd('ErrorMsg', '111')
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100270 call matchadd('VeryBlue', '444')
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200271 call win_execute(winid, "call matchadd('ErrorMsg', '111')")
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100272 call win_execute(winid, "call matchadd('VeryBlue', '555')")
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200273 END
274 call writefile(lines, 'XtestPopupMatches')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200275 let buf = RunVimInTerminal('-S XtestPopupMatches', #{rows: 10})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200276 call VerifyScreenDump(buf, 'Test_popupwin_matches', {})
277
278 " clean up
279 call StopVimInTerminal(buf)
280 call delete('XtestPopupMatches')
281endfunc
282
Bram Moolenaar399d8982019-06-02 15:34:29 +0200283func Test_popup_all_corners()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200284 CheckScreendump
285
Bram Moolenaar399d8982019-06-02 15:34:29 +0200286 let lines =<< trim END
287 call setline(1, repeat([repeat('-', 60)], 15))
288 set so=0
289 normal 2G3|r#
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200290 let winid1 = popup_create(['first', 'second'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200291 \ line: 'cursor+1',
292 \ col: 'cursor',
293 \ pos: 'topleft',
294 \ border: [],
295 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200296 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200297 normal 24|r@
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200298 let winid1 = popup_create(['First', 'SeconD'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200299 \ line: 'cursor+1',
300 \ col: 'cursor',
301 \ pos: 'topright',
302 \ border: [],
303 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200304 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200305 normal 9G27|r%
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200306 let winid1 = popup_create(['fiRSt', 'seCOnd'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200307 \ line: 'cursor-1',
308 \ col: 'cursor',
309 \ pos: 'botleft',
310 \ border: [],
311 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200312 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200313 normal 48|r&
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200314 let winid1 = popup_create(['FIrsT', 'SEcoND'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200315 \ line: 'cursor-1',
316 \ col: 'cursor',
317 \ pos: 'botright',
318 \ border: [],
319 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200320 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200321 normal 1G51|r*
322 let winid1 = popup_create(['one', 'two'], #{
323 \ line: 'cursor-1',
324 \ col: 'cursor',
325 \ pos: 'botleft',
326 \ border: [],
327 \ padding: [],
328 \ })
Bram Moolenaar399d8982019-06-02 15:34:29 +0200329 END
330 call writefile(lines, 'XtestPopupCorners')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200331 let buf = RunVimInTerminal('-S XtestPopupCorners', #{rows: 12})
Bram Moolenaar399d8982019-06-02 15:34:29 +0200332 call VerifyScreenDump(buf, 'Test_popupwin_corners', {})
333
334 " clean up
335 call StopVimInTerminal(buf)
336 call delete('XtestPopupCorners')
337endfunc
338
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100339func Test_popup_nospace()
340 CheckScreendump
341
342 let lines =<< trim END
343 call setline(1, repeat([repeat('-', 60)], 15))
344 set so=0
345
346 " cursor in a line in top half, using "botleft" with popup that
347 " does fit
348 normal 5G2|r@
349 let winid1 = popup_create(['one', 'two'], #{
350 \ line: 'cursor-1',
351 \ col: 'cursor',
352 \ pos: 'botleft',
353 \ border: [],
354 \ })
355 " cursor in a line in top half, using "botleft" with popup that
356 " doesn't fit: gets truncated
357 normal 5G9|r#
358 let winid1 = popup_create(['one', 'two', 'tee'], #{
359 \ line: 'cursor-1',
360 \ col: 'cursor',
361 \ pos: 'botleft',
362 \ posinvert: 0,
363 \ border: [],
364 \ })
365 " cursor in a line in top half, using "botleft" with popup that
366 " doesn't fit and 'posinvert' set: flips to below.
367 normal 5G16|r%
368 let winid1 = popup_create(['one', 'two', 'tee'], #{
369 \ line: 'cursor-1',
370 \ col: 'cursor',
371 \ pos: 'botleft',
372 \ border: [],
373 \ })
374 " cursor in a line in bottom half, using "botleft" with popup that
375 " doesn't fit: does not flip.
376 normal 8G23|r*
377 let winid1 = popup_create(['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff'], #{
378 \ line: 'cursor-1',
379 \ col: 'cursor',
380 \ pos: 'botleft',
381 \ border: [],
382 \ })
383
384 " cursor in a line in bottom half, using "topleft" with popup that
385 " does fit
386 normal 8G30|r@
387 let winid1 = popup_create(['one', 'two'], #{
388 \ line: 'cursor+1',
389 \ col: 'cursor',
390 \ pos: 'topleft',
391 \ border: [],
392 \ })
393 " cursor in a line in top half, using "topleft" with popup that
394 " doesn't fit: truncated
395 normal 8G37|r#
396 let winid1 = popup_create(['one', 'two', 'tee'], #{
397 \ line: 'cursor+1',
398 \ col: 'cursor',
399 \ pos: 'topleft',
400 \ posinvert: 0,
401 \ border: [],
402 \ })
403 " cursor in a line in top half, using "topleft" with popup that
Bram Moolenaar5c6b6182019-11-10 17:51:38 +0100404 " doesn't fit and "posinvert" set: flips to above.
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100405 normal 8G44|r%
Bram Moolenaar5c6b6182019-11-10 17:51:38 +0100406 let winid1 = popup_create(['one', 'two', 'tee', 'fou', 'fiv'], #{
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100407 \ line: 'cursor+1',
408 \ col: 'cursor',
409 \ pos: 'topleft',
410 \ border: [],
411 \ })
412 " cursor in a line in top half, using "topleft" with popup that
413 " doesn't fit: does not flip.
414 normal 5G51|r*
415 let winid1 = popup_create(['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff'], #{
416 \ line: 'cursor+1',
417 \ col: 'cursor',
418 \ pos: 'topleft',
419 \ border: [],
420 \ })
421 END
422 call writefile(lines, 'XtestPopupNospace')
423 let buf = RunVimInTerminal('-S XtestPopupNospace', #{rows: 12})
424 call VerifyScreenDump(buf, 'Test_popupwin_nospace', {})
425
426 " clean up
427 call StopVimInTerminal(buf)
428 call delete('XtestPopupNospace')
429endfunc
430
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200431func Test_popup_firstline_dump()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200432 CheckScreendump
433
Bram Moolenaar8d241042019-06-12 23:40:01 +0200434 let lines =<< trim END
435 call setline(1, range(1, 20))
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200436 let winid = popup_create(['1111', '222222', '33333', '44', '5', '666666', '77777', '888', '9999999999999999'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200437 \ maxheight: 4,
438 \ firstline: 3,
Bram Moolenaar8d241042019-06-12 23:40:01 +0200439 \ })
440 END
441 call writefile(lines, 'XtestPopupFirstline')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200442 let buf = RunVimInTerminal('-S XtestPopupFirstline', #{rows: 10})
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200443 call VerifyScreenDump(buf, 'Test_popupwin_firstline_1', {})
444
445 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: -1})\<CR>")
446 call term_sendkeys(buf, ":\<CR>")
447 call VerifyScreenDump(buf, 'Test_popupwin_firstline_2', {})
Bram Moolenaar8d241042019-06-12 23:40:01 +0200448
449 " clean up
450 call StopVimInTerminal(buf)
451 call delete('XtestPopupFirstline')
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200452endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +0200453
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200454func Test_popup_firstline()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200455 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200456 \ maxheight: 2,
457 \ firstline: 3,
Bram Moolenaarae943152019-06-16 22:54:14 +0200458 \ })
459 call assert_equal(3, popup_getoptions(winid).firstline)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200460 call popup_setoptions(winid, #{firstline: 1})
Bram Moolenaarae943152019-06-16 22:54:14 +0200461 call assert_equal(1, popup_getoptions(winid).firstline)
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200462 eval winid->popup_close()
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200463
464 let winid = popup_create(['xxx']->repeat(50), #{
465 \ maxheight: 3,
466 \ firstline: 11,
467 \ })
468 redraw
469 call assert_equal(11, popup_getoptions(winid).firstline)
470 call assert_equal(11, popup_getpos(winid).firstline)
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +0200471 " check line() works with popup window
472 call assert_equal(11, line('.', winid))
473 call assert_equal(50, line('$', winid))
474 call assert_equal(0, line('$', 123456))
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200475
476 " Normal command changes what is displayed but not "firstline"
477 call win_execute(winid, "normal! \<c-y>")
478 call assert_equal(11, popup_getoptions(winid).firstline)
479 call assert_equal(10, popup_getpos(winid).firstline)
480
481 " Making some property change applies "firstline" again
482 call popup_setoptions(winid, #{line: 4})
483 call assert_equal(11, popup_getoptions(winid).firstline)
484 call assert_equal(11, popup_getpos(winid).firstline)
485
486 " Remove "firstline" property and scroll
487 call popup_setoptions(winid, #{firstline: 0})
488 call win_execute(winid, "normal! \<c-y>")
489 call assert_equal(0, popup_getoptions(winid).firstline)
490 call assert_equal(10, popup_getpos(winid).firstline)
491
492 " Making some property change has no side effect
493 call popup_setoptions(winid, #{line: 3})
494 call assert_equal(0, popup_getoptions(winid).firstline)
495 call assert_equal(10, popup_getpos(winid).firstline)
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200496 call popup_close(winid)
Bram Moolenaarae943152019-06-16 22:54:14 +0200497
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100498 " CTRL-D scrolls down half a page
499 let winid = popup_create(['xxx']->repeat(50), #{
500 \ maxheight: 8,
501 \ })
502 redraw
503 call assert_equal(1, popup_getpos(winid).firstline)
504 call win_execute(winid, "normal! \<C-D>")
505 call assert_equal(5, popup_getpos(winid).firstline)
506 call win_execute(winid, "normal! \<C-D>")
507 call assert_equal(9, popup_getpos(winid).firstline)
508 call win_execute(winid, "normal! \<C-U>")
509 call assert_equal(5, popup_getpos(winid).firstline)
510
511 call win_execute(winid, "normal! \<C-F>")
512 call assert_equal(11, popup_getpos(winid).firstline)
513 call win_execute(winid, "normal! \<C-B>")
514 call assert_equal(5, popup_getpos(winid).firstline)
515
Bram Moolenaarae943152019-06-16 22:54:14 +0200516 call popup_close(winid)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200517endfunc
518
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200519func Test_popup_firstline_cursorline()
520 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
521 \ maxheight: 2,
522 \ firstline: 3,
523 \ cursorline: 1,
524 \ })
525 call assert_equal(3, popup_getoptions(winid).firstline)
526 call assert_equal(3, getwininfo(winid)[0].topline)
527 call assert_equal(3, getcurpos(winid)[1])
528
529 call popup_close(winid)
530endfunc
531
Bram Moolenaara112f2d2019-09-01 17:38:09 +0200532func Test_popup_noscrolloff()
533 set scrolloff=5
534 let winid = popup_create(['xxx']->repeat(50), #{
535 \ maxheight: 5,
536 \ firstline: 11,
537 \ })
538 redraw
539 call assert_equal(11, popup_getoptions(winid).firstline)
540 call assert_equal(11, popup_getpos(winid).firstline)
541
542 call popup_setoptions(winid, #{firstline: 0})
543 call win_execute(winid, "normal! \<c-y>")
544 call assert_equal(0, popup_getoptions(winid).firstline)
545 call assert_equal(10, popup_getpos(winid).firstline)
546
547 call popup_close(winid)
548endfunc
549
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200550func Test_popup_drag()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200551 CheckScreendump
552
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200553 " create a popup that covers the command line
554 let lines =<< trim END
555 call setline(1, range(1, 20))
Bram Moolenaar356375f2019-08-24 14:46:29 +0200556 split
557 vsplit
558 $wincmd w
559 vsplit
560 1wincmd w
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200561 let winid = popup_create(['1111', '222222', '33333'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200562 \ drag: 1,
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200563 \ resize: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200564 \ border: [],
565 \ line: &lines - 4,
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200566 \ })
567 func Dragit()
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000568 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
569 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2 - 20)<CR>
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200570 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
571 endfunc
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200572 func Resize()
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000573 map <silent> <F5> :call test_setmouse(6, 21)<CR>
574 map <silent> <F6> :call test_setmouse(7, 25)<CR>
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200575 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
576 endfunc
Bram Moolenaarbfc57862021-11-26 15:57:40 +0000577 func ClickAndDrag()
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000578 map <silent> <F3> :call test_setmouse(5, 2)<CR>
579 map <silent> <F4> :call test_setmouse(3, 14)<CR>
580 map <silent> <F5> :call test_setmouse(3, 18)<CR>
581 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
582 call feedkeys("\<F4>\<LeftMouse>\<F5>\<LeftDrag>\<LeftRelease>", "xt")
Bram Moolenaarbfc57862021-11-26 15:57:40 +0000583 endfunc
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000584 func DragAllStart()
585 call popup_clear()
586 call popup_create('hello', #{line: 3, col: 5, dragall: 1})
587 endfunc
588 func DragAllDrag()
589 map <silent> <F3> :call test_setmouse(3, 5)<CR>
590 map <silent> <F4> :call test_setmouse(5, 36)<CR>
591 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
592 endfunc
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200593 END
594 call writefile(lines, 'XtestPopupDrag')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200595 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200596 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
597
598 call term_sendkeys(buf, ":call Dragit()\<CR>")
599 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
600
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200601 call term_sendkeys(buf, ":call Resize()\<CR>")
602 call VerifyScreenDump(buf, 'Test_popupwin_drag_03', {})
603
Bram Moolenaarbfc57862021-11-26 15:57:40 +0000604 " dragging works after click on a status line
605 call term_sendkeys(buf, ":call ClickAndDrag()\<CR>")
606 call VerifyScreenDump(buf, 'Test_popupwin_drag_04', {})
607
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000608 " dragging without border
609 call term_sendkeys(buf, ":call DragAllStart()\<CR>")
610 call VerifyScreenDump(buf, 'Test_popupwin_drag_05', {})
611 call term_sendkeys(buf, ":call DragAllDrag()\<CR>")
612 call VerifyScreenDump(buf, 'Test_popupwin_drag_06', {})
613
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200614 " clean up
615 call StopVimInTerminal(buf)
616 call delete('XtestPopupDrag')
617endfunc
618
Bram Moolenaar35910f22020-07-12 19:24:10 +0200619func Test_popup_drag_termwin()
620 CheckUnix
621 CheckScreendump
622 CheckFeature terminal
623
624 " create a popup that covers the terminal window
625 let lines =<< trim END
Bram Moolenaar452143c2020-07-15 17:38:21 +0200626 set foldmethod=marker
627 call setline(1, range(100))
628 for nr in range(7)
629 call setline(nr * 12 + 1, "fold {{{")
Bram Moolenaar9d489562020-07-30 20:08:50 +0200630 call setline(nr * 12 + 11, "end }}}")
Bram Moolenaar452143c2020-07-15 17:38:21 +0200631 endfor
632 %foldclose
Bram Moolenaar35910f22020-07-12 19:24:10 +0200633 set shell=/bin/sh noruler
Bram Moolenaar4b2ce122020-11-21 21:41:41 +0100634 unlet $PROMPT_COMMAND
Bram Moolenaara4dc6f92020-07-12 19:52:36 +0200635 let $PS1 = 'vim> '
Bram Moolenaar452143c2020-07-15 17:38:21 +0200636 terminal ++rows=4
Bram Moolenaar35910f22020-07-12 19:24:10 +0200637 $wincmd w
638 let winid = popup_create(['1111', '2222'], #{
639 \ drag: 1,
640 \ resize: 1,
641 \ border: [],
642 \ line: 3,
643 \ })
Bram Moolenaar452143c2020-07-15 17:38:21 +0200644 func DragitLeft()
Bram Moolenaar35910f22020-07-12 19:24:10 +0200645 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
646 endfunc
Bram Moolenaar452143c2020-07-15 17:38:21 +0200647 func DragitDown()
648 call feedkeys("\<F4>\<LeftMouse>\<F5>\<LeftDrag>\<LeftRelease>", "xt")
649 endfunc
650 func DragitDownLeft()
651 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
652 endfunc
Bram Moolenaar35910f22020-07-12 19:24:10 +0200653 map <silent> <F3> :call test_setmouse(3, &columns / 2)<CR>
654 map <silent> <F4> :call test_setmouse(3, &columns / 2 - 20)<CR>
Bram Moolenaar452143c2020-07-15 17:38:21 +0200655 map <silent> <F5> :call test_setmouse(12, &columns / 2)<CR>
656 map <silent> <F6> :call test_setmouse(12, &columns / 2 - 20)<CR>
Bram Moolenaar35910f22020-07-12 19:24:10 +0200657 END
658 call writefile(lines, 'XtestPopupTerm')
Bram Moolenaar452143c2020-07-15 17:38:21 +0200659 let buf = RunVimInTerminal('-S XtestPopupTerm', #{rows: 16})
Bram Moolenaar35910f22020-07-12 19:24:10 +0200660 call VerifyScreenDump(buf, 'Test_popupwin_term_01', {})
661
Bram Moolenaar452143c2020-07-15 17:38:21 +0200662 call term_sendkeys(buf, ":call DragitLeft()\<CR>")
Bram Moolenaar35910f22020-07-12 19:24:10 +0200663 call VerifyScreenDump(buf, 'Test_popupwin_term_02', {})
664
Bram Moolenaar452143c2020-07-15 17:38:21 +0200665 call term_sendkeys(buf, ":call DragitDown()\<CR>")
666 call VerifyScreenDump(buf, 'Test_popupwin_term_03', {})
667
668 call term_sendkeys(buf, ":call DragitDownLeft()\<CR>")
669 call VerifyScreenDump(buf, 'Test_popupwin_term_04', {})
670
Bram Moolenaar35910f22020-07-12 19:24:10 +0200671 " clean up
672 call StopVimInTerminal(buf)
673 call delete('XtestPopupTerm')
674endfunc
675
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200676func Test_popup_close_with_mouse()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200677 CheckScreendump
678
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200679 let lines =<< trim END
680 call setline(1, range(1, 20))
681 " With border, can click on X
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200682 let winid = popup_create('foobar', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200683 \ close: 'button',
684 \ border: [],
685 \ line: 1,
686 \ col: 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200687 \ })
688 func CloseMsg(id, result)
689 echomsg 'Popup closed with ' .. a:result
690 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200691 let winid = popup_create('notification', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200692 \ close: 'click',
693 \ line: 3,
694 \ col: 15,
695 \ callback: 'CloseMsg',
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200696 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200697 let winid = popup_create('no border here', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200698 \ close: 'button',
699 \ line: 5,
700 \ col: 3,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200701 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200702 let winid = popup_create('only padding', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200703 \ close: 'button',
704 \ padding: [],
705 \ line: 5,
706 \ col: 23,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200707 \ })
708 func CloseWithX()
709 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
710 endfunc
711 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
712 func CloseWithClick()
713 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
714 endfunc
715 map <silent> <F4> :call test_setmouse(3, 17)<CR>
Bram Moolenaarf6396232019-08-24 19:36:00 +0200716 func CreateWithMenuFilter()
717 let winid = popup_create('barfoo', #{
718 \ close: 'button',
719 \ filter: 'popup_filter_menu',
720 \ border: [],
721 \ line: 1,
722 \ col: 40,
723 \ })
724 endfunc
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200725 END
726 call writefile(lines, 'XtestPopupClose')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200727 let buf = RunVimInTerminal('-S XtestPopupClose', #{rows: 10})
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200728 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
729
730 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
731 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
732
733 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
734 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
735
Bram Moolenaarf6396232019-08-24 19:36:00 +0200736 call term_sendkeys(buf, ":call CreateWithMenuFilter()\<CR>")
737 call VerifyScreenDump(buf, 'Test_popupwin_close_04', {})
738
739 " We have to send the actual mouse code, feedkeys() would be caught the
740 " filter.
741 call term_sendkeys(buf, "\<Esc>[<0;47;1M")
742 call VerifyScreenDump(buf, 'Test_popupwin_close_05', {})
743
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200744 " clean up
745 call StopVimInTerminal(buf)
746 call delete('XtestPopupClose')
747endfunction
748
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200749func Test_popup_menu_wrap()
750 CheckScreendump
751
752 let lines =<< trim END
753 call setline(1, range(1, 20))
754 call popup_create([
755 \ 'one',
756 \ 'asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfas',
757 \ 'three',
758 \ 'four',
759 \ ], #{
760 \ pos: "botleft",
761 \ border: [],
762 \ padding: [0,1,0,1],
763 \ maxheight: 3,
764 \ cursorline: 1,
765 \ filter: 'popup_filter_menu',
766 \ })
767 END
768 call writefile(lines, 'XtestPopupWrap')
769 let buf = RunVimInTerminal('-S XtestPopupWrap', #{rows: 10})
770 call VerifyScreenDump(buf, 'Test_popupwin_wrap_1', {})
771
772 call term_sendkeys(buf, "jj")
773 call VerifyScreenDump(buf, 'Test_popupwin_wrap_2', {})
774
775 " clean up
776 call term_sendkeys(buf, "\<Esc>")
777 call StopVimInTerminal(buf)
778 call delete('XtestPopupWrap')
779endfunction
780
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200781func Test_popup_with_mask()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200782 CheckScreendump
783
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200784 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200785 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200786 hi PopupColor ctermbg=lightgrey
787 let winid = popup_create([
788 \ 'some text',
789 \ 'another line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200790 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200791 \ line: 1,
792 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100793 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200794 \ wrap: 0,
795 \ fixed: 1,
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +0200796 \ scrollbar: v:false,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200797 \ zindex: 90,
798 \ padding: [],
799 \ highlight: 'PopupColor',
800 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200801 call popup_create([
802 \ 'xxxxxxxxx',
803 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200804 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200805 \ line: 3,
806 \ col: 18,
807 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200808 let winidb = popup_create([
809 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200810 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200811 \ line: 7,
812 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100813 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200814 \ wrap: 0,
815 \ fixed: 1,
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +0200816 \ scrollbar: v:false,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200817 \ close: 'button',
818 \ zindex: 90,
819 \ padding: [],
820 \ border: [],
821 \ 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 +0200822 END
823 call writefile(lines, 'XtestPopupMask')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200824 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200825 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
826
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200827 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
828 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200829 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200830 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
831
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200832 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
833 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200834 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200835 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
836
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200837 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
838 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200839 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200840 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
841
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200842 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
843 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200844 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200845 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
846
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200847 " clean up
848 call StopVimInTerminal(buf)
849 call delete('XtestPopupMask')
Bram Moolenaar4012d262020-12-29 11:57:46 +0100850
851 " this was causing a crash
852 call popup_create('test', #{mask: [[0, 0, 0, 0]]})
853 call popup_clear()
Bram Moolenaar10ccfb22021-02-13 21:31:18 +0100854
855 " this was causing an internal error
856 enew
857 set nowrap
858 call repeat('x', &columns)->setline(1)
859 call prop_type_add('textprop', {})
860 call prop_add(1, 1, #{length: &columns, type: 'textprop'})
861 vsplit
862 let opts = popup_create('', #{textprop: 'textprop'})
863 \ ->popup_getoptions()
864 \ ->extend(#{mask: [[1, 1, 1, 1]]})
865 call popup_create('', opts)
866 redraw
867
868 close!
869 bwipe!
870 call prop_type_delete('textprop')
871 call popup_clear()
872 set wrap&
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200873endfunc
874
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200875func Test_popup_select()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200876 CheckScreendump
877 CheckFeature clipboard_working
878
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200879 " create a popup with some text to be selected
880 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200881 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200882 call setline(1, range(1, 20))
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200883 let winid = popup_create(['the word', 'some more', 'several words here', 'invisible', '5', '6', '7'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200884 \ drag: 1,
885 \ border: [],
886 \ line: 3,
887 \ col: 10,
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200888 \ maxheight: 3,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200889 \ })
890 func Select1()
891 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
892 endfunc
893 map <silent> <F3> :call test_setmouse(4, 15)<CR>
894 map <silent> <F4> :call test_setmouse(6, 23)<CR>
895 END
896 call writefile(lines, 'XtestPopupSelect')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200897 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200898 call term_sendkeys(buf, ":call Select1()\<CR>")
899 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
900
901 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
902 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200903 " clean the command line, sometimes it still shows a command
904 call term_sendkeys(buf, ":\<esc>")
905
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200906 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
907
908 " clean up
909 call StopVimInTerminal(buf)
910 call delete('XtestPopupSelect')
911endfunc
912
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200913func Test_popup_in_tab()
914 " default popup is local to tab, not visible when in other tab
915 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200916 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200917 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200918 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200919 tabnew
920 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200921 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200922 quit
923 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200924
925 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200926 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200927 " buffer is gone now
928 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200929
930 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200931 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200932 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200933 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200934 tabnew
935 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200936 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200937 quit
938 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200939 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200940
941 " create popup in other tab
942 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200943 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200944 call assert_equal(0, popup_getpos(winid).visible)
945 call assert_equal(1, popup_getoptions(winid).tabpage)
946 quit
947 call assert_equal(1, popup_getpos(winid).visible)
948 call assert_equal(0, popup_getoptions(winid).tabpage)
949 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200950endfunc
951
952func Test_popup_valid_arguments()
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200953 call assert_equal(0, len(popup_list()))
954
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200955 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200956 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200957 let pos = popup_getpos(winid)
958 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200959 call assert_equal([winid], popup_list())
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200960 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200961
962 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200963 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200964 let pos = popup_getpos(winid)
965 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200966 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200967
968 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200969 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200970 let pos = popup_getpos(winid)
971 let around = (&columns - pos.width) / 2
972 call assert_inrange(around - 1, around + 1, pos.col)
973 let around = (&lines - pos.height) / 2
974 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200975 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200976endfunc
977
978func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200979 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200980 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200981 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200982 call popup_clear()
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200983 call assert_fails('call popup_create(test_null_string(), {})', 'E450:')
984 call assert_fails('call popup_create(test_null_list(), {})', 'E450:')
985 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200986
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200987 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200988 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200989 call assert_fails('call popup_create("text", #{col: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200990 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200991 call assert_fails('call popup_create("text", #{col: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200992 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200993 call assert_fails('call popup_create("text", #{col: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200994 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200995
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200996 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200997 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200998 call assert_fails('call popup_create("text", #{line: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200999 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001000 call assert_fails('call popup_create("text", #{line: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001001 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001002 call assert_fails('call popup_create("text", #{line: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001003 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001004
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001005 call assert_fails('call popup_create("text", #{pos: "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001006 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001007 call assert_fails('call popup_create("text", #{padding: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001008 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001009 call assert_fails('call popup_create("text", #{border: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001010 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001011 call assert_fails('call popup_create("text", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001012 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +02001013 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
1014 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001015 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001016 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001017
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001018 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001019 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001020 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001021 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001022 call assert_fails('call popup_create([#{text: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001023 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +01001024 call assert_fails('call popup_create([#{text: "text", props: range(3)}], {})', 'E715:')
1025 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +02001026 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
1027 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +01001028 call assert_fails('call popup_create("text", #{mask: range(5)})', 'E475:')
1029 call popup_clear()
1030 call popup_create("text", #{mask: [range(4)]})
1031 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +02001032 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001033 call assert_fails('call popup_create("text", #{mapping: []})', 'E745:')
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +02001034 call popup_clear()
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001035 call assert_fails('call popup_create("text", #{tabpage : 4})', 'E997:')
1036 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001037endfunc
1038
Bram Moolenaareea16992019-05-31 17:34:48 +02001039func Test_win_execute_closing_curwin()
1040 split
1041 let winid = popup_create('some text', {})
Bram Moolenaare2e40752020-09-04 21:18:46 +02001042 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001043 call popup_clear()
Bram Moolenaar49540192019-12-11 19:34:54 +01001044
1045 let winid = popup_create('some text', {})
Bram Moolenaare2e40752020-09-04 21:18:46 +02001046 call assert_fails('call win_execute(winid, printf("normal! :\<C-u>call popup_close(%d)\<CR>", winid))', 'E994:')
Bram Moolenaar49540192019-12-11 19:34:54 +01001047 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001048endfunc
1049
1050func Test_win_execute_not_allowed()
1051 let winid = popup_create('some text', {})
1052 call assert_fails('call win_execute(winid, "split")', 'E994:')
1053 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
1054 call assert_fails('call win_execute(winid, "close")', 'E994:')
1055 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +02001056 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001057 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
1058 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
1059 call assert_fails('call win_execute(winid, "next")', 'E994:')
1060 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01001061 call assert_fails('call win_execute(winid, "pedit filename")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001062 call assert_fails('call win_execute(winid, "buf")', 'E994:')
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01001063 call assert_fails('call win_execute(winid, "bnext")', 'E994:')
1064 call assert_fails('call win_execute(winid, "bprev")', 'E994:')
1065 call assert_fails('call win_execute(winid, "bfirst")', 'E994:')
1066 call assert_fails('call win_execute(winid, "blast")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001067 call assert_fails('call win_execute(winid, "edit")', 'E994:')
1068 call assert_fails('call win_execute(winid, "enew")', 'E994:')
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001069 call assert_fails('call win_execute(winid, "help")', 'E994:')
1070 call assert_fails('call win_execute(winid, "1only")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001071 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
1072 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
1073 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
1074 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001075 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +02001076endfunc
1077
Bram Moolenaar402502d2019-05-30 22:07:36 +02001078func Test_popup_with_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001079 CheckScreendump
1080
Bram Moolenaar402502d2019-05-30 22:07:36 +02001081 let lines =<< trim END
1082 call setline(1, range(1, 100))
1083 let winid = popup_create(
1084 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001085 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001086 END
1087 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001088 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001089 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
1090
1091 " clean up
1092 call StopVimInTerminal(buf)
1093 call delete('XtestPopup')
1094endfunc
1095
1096func Test_popup_without_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001097 CheckScreendump
1098
Bram Moolenaar402502d2019-05-30 22:07:36 +02001099 let lines =<< trim END
1100 call setline(1, range(1, 100))
1101 let winid = popup_create(
1102 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001103 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001104 END
1105 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001106 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001107 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
1108
1109 " clean up
1110 call StopVimInTerminal(buf)
1111 call delete('XtestPopup')
1112endfunc
1113
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001114func Test_popup_with_showbreak()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001115 CheckScreendump
1116
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001117 let lines =<< trim END
1118 set showbreak=>>\
1119 call setline(1, range(1, 20))
1120 let winid = popup_dialog(
Bram Moolenaar8ae54372019-09-15 18:11:16 +02001121 \ 'a long line here that wraps',
1122 \ #{filter: 'popup_filter_yesno',
1123 \ maxwidth: 12})
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001124 END
1125 call writefile(lines, 'XtestPopupShowbreak')
1126 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
1127 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
1128
1129 " clean up
1130 call term_sendkeys(buf, "y")
1131 call StopVimInTerminal(buf)
1132 call delete('XtestPopupShowbreak')
1133endfunc
1134
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001135func Test_popup_time()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001136 CheckFeature timers
1137
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001138 topleft vnew
1139 call setline(1, 'hello')
1140
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001141 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001142 \ line: 1,
1143 \ col: 1,
1144 \ minwidth: 20,
1145 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001146 \})
1147 redraw
1148 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1149 call assert_equal('world', line)
1150
Bram Moolenaarb4f06282019-07-12 21:07:54 +02001151 call assert_equal(winid, popup_locate(1, 1))
1152 call assert_equal(winid, popup_locate(1, 20))
1153 call assert_equal(0, popup_locate(1, 21))
1154 call assert_equal(0, popup_locate(2, 1))
1155
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001156 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +02001157 redraw
Bram Moolenaar196b4662019-09-06 21:34:30 +02001158 let line = join(map(range(1, 5), '1->screenstring(v:val)'), '')
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001159 call assert_equal('hello', line)
1160
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001161 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001162 \ line: &lines,
1163 \ col: 10,
1164 \ minwidth: 20,
1165 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001166 \})
1167 redraw
1168 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1169 call assert_match('.*on the command line.*', line)
1170
1171 sleep 700m
1172 redraw
1173 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1174 call assert_notmatch('.*on the command line.*', line)
1175
1176 bwipe!
1177endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001178
1179func Test_popup_hide()
1180 topleft vnew
1181 call setline(1, 'hello')
1182
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001183 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001184 \ line: 1,
1185 \ col: 1,
1186 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001187 \})
1188 redraw
1189 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1190 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001191 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001192 " buffer is still listed and active
1193 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001194
1195 call popup_hide(winid)
1196 redraw
1197 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1198 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001199 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001200 " buffer is still listed but hidden
Bram Moolenaarca7c0782020-01-14 20:42:48 +01001201 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001202
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001203 eval winid->popup_show()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001204 redraw
1205 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1206 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001207 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001208
1209
1210 call popup_close(winid)
1211 redraw
1212 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1213 call assert_equal('hello', line)
1214
1215 " error is given for existing non-popup window
1216 call assert_fails('call popup_hide(win_getid())', 'E993:')
1217
1218 " no error non-existing window
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001219 eval 1234234->popup_hide()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001220 call popup_show(41234234)
1221
1222 bwipe!
1223endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001224
1225func Test_popup_move()
1226 topleft vnew
1227 call setline(1, 'hello')
1228
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001229 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001230 \ line: 1,
1231 \ col: 1,
1232 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001233 \})
1234 redraw
1235 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1236 call assert_equal('world ', line)
1237
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001238 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001239 redraw
1240 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1241 call assert_equal('hello ', line)
1242 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
1243 call assert_equal('~world', line)
1244
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001245 eval winid->popup_move(#{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001246 redraw
1247 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1248 call assert_equal('hworld', line)
1249
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001250 call assert_fails('call popup_move(winid, [])', 'E715:')
1251 call assert_fails('call popup_move(winid, test_null_dict())', 'E715:')
1252
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001253 call popup_close(winid)
1254
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001255 call assert_equal(0, popup_move(-1, {}))
1256
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001257 bwipe!
1258endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +02001259
Bram Moolenaar402502d2019-05-30 22:07:36 +02001260func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001261 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001262 \ line: 2,
1263 \ col: 3,
1264 \ minwidth: 10,
1265 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +02001266 \})
1267 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001268 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001269 call assert_equal(2, res.line)
1270 call assert_equal(3, res.col)
1271 call assert_equal(10, res.width)
1272 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001273 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001274
1275 call popup_close(winid)
1276endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001277
1278func Test_popup_width_longest()
1279 let tests = [
1280 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
1281 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
1282 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
1283 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
1284 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
1285 \ ]
1286
1287 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001288 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001289 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001290 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001291 call assert_equal(test[1], position.width)
1292 call popup_close(winid)
1293 endfor
1294endfunc
1295
1296func Test_popup_wraps()
1297 let tests = [
1298 \ ['nowrap', 6, 1],
1299 \ ['a line that wraps once', 12, 2],
1300 \ ['a line that wraps two times', 12, 3],
1301 \ ]
1302 for test in tests
1303 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001304 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001305 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001306 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001307 call assert_equal(test[1], position.width)
1308 call assert_equal(test[2], position.height)
1309
1310 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001311 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001312 endfor
1313endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001314
1315func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001316 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001317 \ line: 2,
1318 \ col: 3,
1319 \ minwidth: 10,
1320 \ minheight: 11,
1321 \ maxwidth: 20,
1322 \ maxheight: 21,
1323 \ zindex: 100,
1324 \ time: 5000,
1325 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001326 \})
1327 redraw
1328 let res = popup_getoptions(winid)
1329 call assert_equal(2, res.line)
1330 call assert_equal(3, res.col)
1331 call assert_equal(10, res.minwidth)
1332 call assert_equal(11, res.minheight)
1333 call assert_equal(20, res.maxwidth)
1334 call assert_equal(21, res.maxheight)
1335 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001336 call assert_equal(1, res.fixed)
Bram Moolenaarb8350ab2019-08-04 17:59:49 +02001337 call assert_equal(1, res.mapping)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001338 if has('timers')
1339 call assert_equal(5000, res.time)
1340 endif
1341 call popup_close(winid)
1342
1343 let winid = popup_create('hello', {})
1344 redraw
1345 let res = popup_getoptions(winid)
1346 call assert_equal(0, res.line)
1347 call assert_equal(0, res.col)
1348 call assert_equal(0, res.minwidth)
1349 call assert_equal(0, res.minheight)
1350 call assert_equal(0, res.maxwidth)
1351 call assert_equal(0, res.maxheight)
1352 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001353 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001354 if has('timers')
1355 call assert_equal(0, res.time)
1356 endif
1357 call popup_close(winid)
1358 call assert_equal({}, popup_getoptions(winid))
1359endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001360
1361func Test_popup_option_values()
1362 new
1363 " window-local
1364 setlocal number
1365 setlocal nowrap
1366 " buffer-local
1367 setlocal omnifunc=Something
1368 " global/buffer-local
1369 setlocal path=/there
1370 " global/window-local
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001371 setlocal statusline=2
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001372
1373 let winid = popup_create('hello', {})
1374 call assert_equal(0, getwinvar(winid, '&number'))
1375 call assert_equal(1, getwinvar(winid, '&wrap'))
1376 call assert_equal('', getwinvar(winid, '&omnifunc'))
1377 call assert_equal(&g:path, getwinvar(winid, '&path'))
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001378 call assert_equal(&g:statusline, getwinvar(winid, '&statusline'))
1379
1380 " 'scrolloff' is reset to zero
1381 call assert_equal(5, &scrolloff)
1382 call assert_equal(0, getwinvar(winid, '&scrolloff'))
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001383
1384 call popup_close(winid)
1385 bwipe
1386endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001387
1388func Test_popup_atcursor()
1389 topleft vnew
1390 call setline(1, [
1391 \ 'xxxxxxxxxxxxxxxxx',
1392 \ 'xxxxxxxxxxxxxxxxx',
1393 \ 'xxxxxxxxxxxxxxxxx',
1394 \])
1395
1396 call cursor(2, 2)
1397 redraw
1398 let winid = popup_atcursor('vim', {})
1399 redraw
1400 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1401 call assert_equal('xvimxxxxxxxxxxxxx', line)
1402 call popup_close(winid)
1403
1404 call cursor(3, 4)
1405 redraw
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001406 let winid = 'vim'->popup_atcursor({})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001407 redraw
1408 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
1409 call assert_equal('xxxvimxxxxxxxxxxx', line)
1410 call popup_close(winid)
1411
1412 call cursor(1, 1)
1413 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001414 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001415 \ line: 'cursor+2',
1416 \ col: 'cursor+1',
1417 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001418 redraw
1419 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1420 call assert_equal('xvimxxxxxxxxxxxxx', line)
1421 call popup_close(winid)
1422
1423 call cursor(3, 3)
1424 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001425 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001426 \ line: 'cursor-2',
1427 \ col: 'cursor-1',
1428 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001429 redraw
1430 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1431 call assert_equal('xvimxxxxxxxxxxxxx', line)
1432 call popup_close(winid)
1433
Bram Moolenaar402502d2019-05-30 22:07:36 +02001434 " just enough room above
1435 call cursor(3, 3)
1436 redraw
1437 let winid = popup_atcursor(['vim', 'is great'], {})
1438 redraw
1439 let pos = popup_getpos(winid)
1440 call assert_equal(1, pos.line)
1441 call popup_close(winid)
1442
1443 " not enough room above, popup goes below the cursor
1444 call cursor(3, 3)
1445 redraw
1446 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1447 redraw
1448 let pos = popup_getpos(winid)
1449 call assert_equal(4, pos.line)
1450 call popup_close(winid)
1451
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001452 " cursor in first line, popup in line 2
1453 call cursor(1, 1)
1454 redraw
1455 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1456 redraw
1457 let pos = popup_getpos(winid)
1458 call assert_equal(2, pos.line)
1459 call popup_close(winid)
1460
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001461 bwipe!
1462endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001463
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001464func Test_popup_atcursor_pos()
1465 CheckScreendump
Dominique Pellec60e9592021-07-03 21:41:38 +02001466 CheckFeature conceal
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001467
1468 let lines =<< trim END
1469 call setline(1, repeat([repeat('-', 60)], 15))
1470 set so=0
1471
1472 normal 9G3|r#
1473 let winid1 = popup_atcursor(['first', 'second'], #{
1474 \ moved: [0, 0, 0],
1475 \ })
1476 normal 9G21|r&
1477 let winid1 = popup_atcursor(['FIrsT', 'SEcoND'], #{
1478 \ pos: 'botright',
1479 \ moved: [0, 0, 0],
1480 \ })
1481 normal 3G27|r%
1482 let winid1 = popup_atcursor(['fiRSt', 'seCOnd'], #{
1483 \ pos: 'topleft',
1484 \ moved: [0, 0, 0],
1485 \ })
1486 normal 3G45|r@
1487 let winid1 = popup_atcursor(['First', 'SeconD'], #{
1488 \ pos: 'topright',
Bram Moolenaarb0992022020-01-30 14:55:42 +01001489 \ moved: range(3),
1490 \ mousemoved: range(3),
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001491 \ })
Bram Moolenaarea042672021-06-29 20:22:32 +02001492
1493 normal 9G27|Rconcealed X
1494 syn match Hidden /concealed/ conceal
1495 set conceallevel=2 concealcursor=n
1496 redraw
1497 normal 0fX
1498 call popup_atcursor('mark', {})
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001499 END
1500 call writefile(lines, 'XtestPopupAtcursorPos')
1501 let buf = RunVimInTerminal('-S XtestPopupAtcursorPos', #{rows: 12})
1502 call VerifyScreenDump(buf, 'Test_popupwin_atcursor_pos', {})
1503
1504 " clean up
1505 call StopVimInTerminal(buf)
1506 call delete('XtestPopupAtcursorPos')
1507endfunc
1508
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001509func Test_popup_beval()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001510 CheckScreendump
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001511 CheckFeature balloon_eval_term
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001512
1513 let lines =<< trim END
1514 call setline(1, range(1, 20))
1515 call setline(5, 'here is some text to hover over')
1516 set balloonevalterm
1517 set balloonexpr=BalloonExpr()
1518 set balloondelay=100
1519 func BalloonExpr()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001520 let s:winid = [v:beval_text]->popup_beval({})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001521 return ''
1522 endfunc
1523 func Hover()
1524 call test_setmouse(5, 15)
1525 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1526 sleep 100m
1527 endfunc
1528 func MoveOntoPopup()
1529 call test_setmouse(4, 17)
1530 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1531 endfunc
1532 func MoveAway()
1533 call test_setmouse(5, 13)
1534 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1535 endfunc
1536 END
1537 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001538 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001539 call TermWait(buf, 50)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001540 call term_sendkeys(buf, 'j')
1541 call term_sendkeys(buf, ":call Hover()\<CR>")
1542 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1543
1544 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1545 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1546
1547 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1548 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1549
1550 " clean up
1551 call StopVimInTerminal(buf)
1552 call delete('XtestPopupBeval')
1553endfunc
1554
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001555func Test_popup_filter()
1556 new
1557 call setline(1, 'some text')
1558
1559 func MyPopupFilter(winid, c)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001560 if a:c == 'e' || a:c == "\<F9>"
1561 let g:eaten = a:c
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001562 return 1
1563 endif
1564 if a:c == '0'
1565 let g:ignored = '0'
1566 return 0
1567 endif
1568 if a:c == 'x'
1569 call popup_close(a:winid)
1570 return 1
1571 endif
1572 return 0
1573 endfunc
1574
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001575 let winid = 'something'->popup_create(#{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001576 redraw
1577
1578 " e is consumed by the filter
Bram Moolenaar999db232021-07-04 14:00:55 +02001579 let g:eaten = ''
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001580 call feedkeys('e', 'xt')
1581 call assert_equal('e', g:eaten)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001582 call feedkeys("\<F9>", 'xt')
1583 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001584
1585 " 0 is ignored by the filter
Bram Moolenaar999db232021-07-04 14:00:55 +02001586 let g:ignored = ''
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001587 normal $
1588 call assert_equal(9, getcurpos()[2])
1589 call feedkeys('0', 'xt')
1590 call assert_equal('0', g:ignored)
Bram Moolenaar999db232021-07-04 14:00:55 +02001591
1592 if has('win32') && has('gui_running')
1593 echo "FIXME: this check is very flaky on MS-Windows GUI, the cursor doesn't move"
1594 else
1595 call assert_equal(1, getcurpos()[2])
1596 endif
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001597
1598 " x closes the popup
1599 call feedkeys('x', 'xt')
Bram Moolenaarec084d32020-02-28 22:44:47 +01001600 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001601 call assert_equal(-1, winbufnr(winid))
1602
Bram Moolenaar999db232021-07-04 14:00:55 +02001603 unlet g:eaten
1604 unlet g:ignored
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001605 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001606 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001607endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001608
Bram Moolenaar4934ad02020-09-28 22:29:58 +02001609" this tests that the filter is not used for :normal command
Bram Moolenaar189832b2020-09-23 12:29:11 +02001610func Test_popup_filter_normal_cmd()
1611 CheckScreendump
1612
1613 let lines =<< trim END
Bram Moolenaar4934ad02020-09-28 22:29:58 +02001614 let text = range(1, 20)->map({_, v -> string(v)})
1615 let g:winid = popup_create(text, #{maxheight: 5, minwidth: 3, filter: 'invalidfilter'})
1616 call timer_start(0, {-> win_execute(g:winid, 'norm! 10Gzz')})
Bram Moolenaar189832b2020-09-23 12:29:11 +02001617 END
1618 call writefile(lines, 'XtestPopupNormal')
1619 let buf = RunVimInTerminal('-S XtestPopupNormal', #{rows: 10})
1620 call TermWait(buf, 100)
1621 call VerifyScreenDump(buf, 'Test_popupwin_normal_cmd', {})
1622
1623 call StopVimInTerminal(buf)
1624 call delete('XtestPopupNormal')
1625endfunc
1626
Bram Moolenaar10476522020-09-24 22:57:31 +02001627" test that cursor line highlight is updated after using win_execute()
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001628func Test_popup_filter_win_execute()
1629 CheckScreendump
1630
1631 let lines =<< trim END
Bram Moolenaar10476522020-09-24 22:57:31 +02001632 let lines = range(1, &lines * 2)->map({_, v -> string(v)})
1633 let g:id = popup_create(lines, #{
1634 \ minheight: &lines - 5,
1635 \ maxheight: &lines - 5,
1636 \ cursorline: 1,
1637 \ })
1638 redraw
1639 END
1640 call writefile(lines, 'XtestPopupWinExecute')
1641 let buf = RunVimInTerminal('-S XtestPopupWinExecute', #{rows: 14})
1642
1643 call term_sendkeys(buf, ":call win_execute(g:id, ['normal 17Gzz'])\<CR>")
1644 call term_sendkeys(buf, ":\<CR>")
1645
1646 call VerifyScreenDump(buf, 'Test_popupwin_win_execute_cursorline', {})
1647
1648 call StopVimInTerminal(buf)
1649 call delete('XtestPopupWinExecute')
1650endfunc
1651
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001652func Test_popup_set_firstline()
1653 CheckScreendump
1654
1655 let lines =<< trim END
1656 let lines = range(1, 50)->map({_, v -> string(v)})
1657 let g:id = popup_create(lines, #{
1658 \ minwidth: 20,
1659 \ maxwidth: 20,
1660 \ minheight: &lines - 5,
1661 \ maxheight: &lines - 5,
1662 \ cursorline: 1,
1663 \ })
1664 call popup_setoptions(g:id, #{firstline: 10})
1665 redraw
1666 END
1667 call writefile(lines, 'XtestPopupWinSetFirstline')
1668 let buf = RunVimInTerminal('-S XtestPopupWinSetFirstline', #{rows: 16})
1669
1670 call VerifyScreenDump(buf, 'Test_popupwin_set_firstline_1', {})
1671
1672 call term_sendkeys(buf, ":call popup_setoptions(g:id, #{firstline: 5})\<CR>")
1673 call term_sendkeys(buf, ":\<CR>")
1674 call VerifyScreenDump(buf, 'Test_popupwin_set_firstline_2', {})
1675
1676 call StopVimInTerminal(buf)
1677 call delete('XtestPopupWinSetFirstline')
1678endfunc
1679
Bram Moolenaar10476522020-09-24 22:57:31 +02001680" this tests that we don't get stuck with an error in "win_execute()"
1681func Test_popup_filter_win_execute_error()
1682 CheckScreendump
1683
1684 let lines =<< trim END
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001685 let g:winid = popup_create('some text', {'filter': 'invalidfilter'})
1686 call timer_start(0, {-> win_execute(g:winid, 'invalidCommand')})
1687 END
Bram Moolenaar10476522020-09-24 22:57:31 +02001688 call writefile(lines, 'XtestPopupWinExecuteError')
1689 let buf = RunVimInTerminal('-S XtestPopupWinExecuteError', #{rows: 10, wait_for_ruler: 0})
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001690
1691 call WaitFor({-> term_getline(buf, 9) =~ 'Not an editor command: invalidCommand'})
1692 call term_sendkeys(buf, "\<CR>")
1693 call WaitFor({-> term_getline(buf, 9) =~ 'Unknown function: invalidfilter'})
1694 call term_sendkeys(buf, "\<CR>")
1695 call WaitFor({-> term_getline(buf, 9) =~ 'Not allowed in a popup window'})
1696 call term_sendkeys(buf, "\<CR>")
1697 call term_sendkeys(buf, "\<CR>")
1698 call VerifyScreenDump(buf, 'Test_popupwin_win_execute', {})
1699
1700 call StopVimInTerminal(buf)
Bram Moolenaar10476522020-09-24 22:57:31 +02001701 call delete('XtestPopupWinExecuteError')
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001702endfunc
1703
Bram Moolenaara42d9452019-06-15 21:46:30 +02001704func ShowDialog(key, result)
1705 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001706 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001707 \ filter: 'popup_filter_yesno',
1708 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001709 \ })
1710 redraw
1711 call feedkeys(a:key, "xt")
1712 call assert_equal(winid, s:cb_winid)
1713 call assert_equal(a:result, s:cb_res)
1714endfunc
1715
1716func Test_popup_dialog()
1717 func QuitCallback(id, res)
1718 let s:cb_winid = a:id
1719 let s:cb_res = a:res
1720 endfunc
1721
1722 let winid = ShowDialog("y", 1)
1723 let winid = ShowDialog("Y", 1)
1724 let winid = ShowDialog("n", 0)
1725 let winid = ShowDialog("N", 0)
1726 let winid = ShowDialog("x", 0)
1727 let winid = ShowDialog("X", 0)
1728 let winid = ShowDialog("\<Esc>", 0)
1729 let winid = ShowDialog("\<C-C>", -1)
1730
1731 delfunc QuitCallback
1732endfunc
1733
Bram Moolenaara730e552019-06-16 19:05:31 +02001734func ShowMenu(key, result)
1735 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001736 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001737 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001738 \ })
1739 redraw
1740 call feedkeys(a:key, "xt")
1741 call assert_equal(winid, s:cb_winid)
1742 call assert_equal(a:result, s:cb_res)
1743endfunc
1744
1745func Test_popup_menu()
1746 func QuitCallback(id, res)
1747 let s:cb_winid = a:id
1748 let s:cb_res = a:res
1749 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001750 " mapping won't be used in popup
1751 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001752
1753 let winid = ShowMenu(" ", 1)
1754 let winid = ShowMenu("j \<CR>", 2)
1755 let winid = ShowMenu("JjK \<CR>", 2)
1756 let winid = ShowMenu("jjjjjj ", 3)
1757 let winid = ShowMenu("kkk ", 1)
1758 let winid = ShowMenu("x", -1)
1759 let winid = ShowMenu("X", -1)
1760 let winid = ShowMenu("\<Esc>", -1)
1761 let winid = ShowMenu("\<C-C>", -1)
1762
1763 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001764 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001765endfunc
1766
1767func Test_popup_menu_screenshot()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001768 CheckScreendump
Bram Moolenaara730e552019-06-16 19:05:31 +02001769
1770 let lines =<< trim END
1771 call setline(1, range(1, 20))
1772 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001773 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001774 func MenuDone(id, res)
1775 echomsg "selected " .. a:res
1776 endfunc
1777 END
1778 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001779 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001780 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1781
1782 call term_sendkeys(buf, "jj")
1783 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1784
1785 call term_sendkeys(buf, " ")
1786 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1787
1788 " clean up
1789 call StopVimInTerminal(buf)
1790 call delete('XtestPopupMenu')
1791endfunc
1792
Bram Moolenaarf914a332019-07-20 15:09:56 +02001793func Test_popup_menu_narrow()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001794 CheckScreendump
Bram Moolenaarf914a332019-07-20 15:09:56 +02001795
1796 let lines =<< trim END
1797 call setline(1, range(1, 20))
1798 hi PopupSelected ctermbg=green
1799 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1800 func MenuDone(id, res)
1801 echomsg "selected " .. a:res
1802 endfunc
1803 END
1804 call writefile(lines, 'XtestPopupNarrowMenu')
1805 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1806 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1807
1808 " clean up
1809 call term_sendkeys(buf, "x")
1810 call StopVimInTerminal(buf)
1811 call delete('XtestPopupNarrowMenu')
1812endfunc
1813
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001814func Test_popup_title()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001815 CheckScreendump
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001816
1817 " Create a popup without title or border, a line of padding will be added to
1818 " put the title on.
1819 let lines =<< trim END
1820 call setline(1, range(1, 20))
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001821 let winid = popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001822 END
1823 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001824 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001825 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1826
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001827 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxwidth: 20, title: 'a very long title that is not going to fit'})\<CR>")
1828 call term_sendkeys(buf, ":\<CR>")
1829 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_1', {})
1830
1831 call term_sendkeys(buf, ":call popup_setoptions(winid, #{border: []})\<CR>")
1832 call term_sendkeys(buf, ":\<CR>")
1833 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_2', {})
1834
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01001835 call term_sendkeys(buf, ":call popup_clear()\<CR>")
1836 call term_sendkeys(buf, ":call popup_create(['aaa', 'bbb'], #{title: 'Title', minwidth: 12, padding: [2, 2, 2, 2]})\<CR>")
1837 call term_sendkeys(buf, ":\<CR>")
1838 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_3', {})
1839
1840 call term_sendkeys(buf, ":call popup_clear()\<CR>")
1841 call term_sendkeys(buf, ":call popup_create(['aaa', 'bbb'], #{title: 'Title', minwidth: 12, border: [], padding: [2, 2, 2, 2]})\<CR>")
1842 call term_sendkeys(buf, ":\<CR>")
1843 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_4', {})
1844
Ralf Schandlbc869872021-05-28 14:12:14 +02001845 call term_sendkeys(buf, ":call popup_clear()\<CR>")
rbtnnc6119412021-08-07 13:08:45 +02001846 call term_sendkeys(buf, ":call popup_menu(['This is a line', 'and another line'], #{title: '▶Äあいうえお◀', })\<CR>")
Ralf Schandlbc869872021-05-28 14:12:14 +02001847 call VerifyScreenDump(buf, 'Test_popupwin_multibytetitle', {})
1848 call term_sendkeys(buf, "x")
1849
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001850 " clean up
1851 call StopVimInTerminal(buf)
1852 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001853
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001854 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001855 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001856 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001857 call assert_equal('Another Title', popup_getoptions(winid).title)
1858
1859 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001860endfunc
1861
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001862func Test_popup_close_callback()
1863 func PopupDone(id, result)
1864 let g:result = a:result
1865 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001866 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001867 redraw
1868 call popup_close(winid, 'done')
1869 call assert_equal('done', g:result)
1870endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001871
1872func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001873 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001874 redraw
1875 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001876 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001877 call assert_equal(5, pos.height)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001878 call popup_close(winid)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001879
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001880 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001881 redraw
1882 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001883 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001884 call assert_equal(3, pos.height)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001885 call popup_close(winid)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001886endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001887
1888func Test_popup_never_behind()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001889 CheckScreendump
1890
Bram Moolenaar988c4332019-06-02 14:12:11 +02001891 " +-----------------------------+
1892 " | | |
1893 " | | |
1894 " | | |
1895 " | line1 |
1896 " |------------line2------------|
1897 " | line3 |
1898 " | line4 |
1899 " | |
1900 " | |
1901 " +-----------------------------+
1902 let lines =<< trim END
Bram Moolenaar988c4332019-06-02 14:12:11 +02001903 split
1904 vsplit
1905 let info_window1 = getwininfo()[0]
1906 let line = info_window1['height']
1907 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001908 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001909 \ line : line,
1910 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001911 \ })
1912 END
1913 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001914 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001915 call term_sendkeys(buf, "\<C-W>w")
1916 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1917
1918 " clean up
1919 call StopVimInTerminal(buf)
1920 call delete('XtestPopupBehind')
1921endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001922
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001923func s:VerifyPosition(p, msg, line, col, width, height)
1924 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1925 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1926 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1927 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001928endfunc
1929
1930func Test_popup_position_adjust()
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001931 " Anything placed past the last cell on the right of the screen is moved to
1932 " the left.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001933 "
1934 " When wrapping is disabled, we also shift to the left to display on the
1935 " screen, unless fixed is set.
1936
1937 " Entries for cases which don't vary based on wrapping.
1938 " Format is per tests described below
1939 let both_wrap_tests = [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001940 \ ['a', 5, &columns, 5, &columns, 1, 1],
1941 \ ['b', 5, &columns + 1, 5, &columns, 1, 1],
1942 \ ['c', 5, &columns - 1, 5, &columns - 1, 1, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001943 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001944 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1]]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001945
1946 " these test groups are dicts with:
1947 " - comment: something to identify the group of tests by
1948 " - options: dict of options to merge with the row/col in tests
1949 " - tests: list of cases. Each one is a list with elements:
1950 " - text
1951 " - row
1952 " - col
1953 " - expected row
1954 " - expected col
1955 " - expected width
1956 " - expected height
1957 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001958 \ #{
1959 \ comment: 'left-aligned with wrapping',
1960 \ options: #{
1961 \ wrap: 1,
1962 \ pos: 'botleft',
1963 \ },
1964 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001965 \ ['aa', 5, &columns, 4, &columns, 1, 2],
1966 \ ['bb', 5, &columns + 1, 4, &columns, 1, 2],
1967 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1968 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1969 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1970 \
1971 \ ['aaa', 5, &columns, 3, &columns, 1, 3],
1972 \ ['bbb', 5, &columns + 1, 3, &columns, 1, 3],
1973 \ ['ccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
1974 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1975 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1976 \
1977 \ ['aaaa', 5, &columns, 2, &columns, 1, 4],
1978 \ ['bbbb', 5, &columns + 1, 2, &columns, 1, 4],
1979 \ ['cccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001980 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
1981 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001982 \ ['eeee', 5, &columns - 4, 5, &columns - 4, 4, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001983 \ ],
1984 \ },
1985 \ #{
1986 \ comment: 'left aligned without wrapping',
1987 \ options: #{
1988 \ wrap: 0,
1989 \ pos: 'botleft',
1990 \ },
1991 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001992 \ ['aa', 5, &columns, 5, &columns - 1, 2, 1],
1993 \ ['bb', 5, &columns + 1, 5, &columns - 1, 2, 1],
1994 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1995 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1996 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1997 \
1998 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
1999 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
2000 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
2001 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
2002 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
2003 \
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002004 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
2005 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
2006 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
2007 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
2008 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
2009 \ ],
2010 \ },
2011 \ #{
2012 \ comment: 'left aligned with fixed position',
2013 \ options: #{
2014 \ wrap: 0,
2015 \ fixed: 1,
2016 \ pos: 'botleft',
2017 \ },
2018 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01002019 \ ['aa', 5, &columns, 5, &columns, 1, 1],
2020 \ ['bb', 5, &columns + 1, 5, &columns, 1, 1],
2021 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
2022 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
2023 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
2024 \
2025 \ ['aaa', 5, &columns, 5, &columns, 1, 1],
2026 \ ['bbb', 5, &columns + 1, 5, &columns, 1, 1],
2027 \ ['ccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
2028 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
2029 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
2030 \
2031 \ ['aaaa', 5, &columns, 5, &columns, 1, 1],
2032 \ ['bbbb', 5, &columns + 1, 5, &columns, 1, 1],
2033 \ ['cccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002034 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
2035 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
2036 \ ],
2037 \ },
2038 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002039
2040 for test_group in tests
2041 for test in test_group.tests
2042 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002043 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002044 \ line: line,
2045 \ col: col,
2046 \ }
2047 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002048
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002049 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002050
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002051 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002052 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
2053 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002054 endfor
2055 endfor
2056
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002057 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002058 %bwipe!
2059endfunc
2060
Bram Moolenaar3397f742019-06-02 18:40:06 +02002061func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002062 " width of screen
2063 let X = join(map(range(&columns), {->'X'}), '')
2064
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002065 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
2066 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002067
2068 redraw
2069 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2070 call assert_equal(X, line)
2071
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002072 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002073 redraw
2074
2075 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002076 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
2077 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002078
2079 redraw
2080 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2081 call assert_equal(X, line)
2082
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002083 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002084 redraw
2085
2086 " Extend so > window width
2087 let X .= 'x'
2088
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002089 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
2090 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002091
2092 redraw
2093 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2094 call assert_equal(X[ : -2 ], line)
2095
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002096 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002097 redraw
2098
2099 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002100 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
2101 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002102
2103 redraw
2104 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2105 call assert_equal(X[ : -2 ], line)
2106
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002107 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002108 redraw
2109
2110 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002111 let p = popup_create(X,
2112 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
2113 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002114
2115 redraw
2116 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2117 let e_line = ' ' . X[ 1 : -2 ]
2118 call assert_equal(e_line, line)
2119
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002120 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002121 redraw
2122
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002123 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002124 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02002125endfunc
2126
2127func Test_popup_moved()
2128 new
2129 call test_override('char_avail', 1)
2130 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
2131
2132 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002133 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002134 redraw
2135 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002136 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002137 " trigger the check for last_cursormoved by going into insert mode
2138 call feedkeys("li\<Esc>", 'xt')
2139 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002140 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002141
2142 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002143 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002144 redraw
2145 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002146 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002147 call feedkeys("hi\<Esc>", 'xt')
2148 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002149 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002150
2151 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002152 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002153 redraw
2154 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002155 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002156 call feedkeys("li\<Esc>", 'xt')
2157 call assert_equal(1, popup_getpos(winid).visible)
2158 call feedkeys("ei\<Esc>", 'xt')
2159 call assert_equal(1, popup_getpos(winid).visible)
2160 call feedkeys("eli\<Esc>", 'xt')
2161 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002162 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002163
Bram Moolenaar17627312019-06-02 19:53:44 +02002164 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02002165 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02002166 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002167 redraw
2168 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002169 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002170 call feedkeys("eli\<Esc>", 'xt')
2171 call assert_equal(1, popup_getpos(winid).visible)
2172 call feedkeys("wi\<Esc>", 'xt')
2173 call assert_equal(1, popup_getpos(winid).visible)
2174 call feedkeys("Eli\<Esc>", 'xt')
2175 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002176 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002177
2178 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002179 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002180 redraw
2181 call assert_equal(1, popup_getpos(winid).visible)
2182 call feedkeys("eli\<Esc>", 'xt')
2183 call feedkeys("ei\<Esc>", 'xt')
2184 call assert_equal(1, popup_getpos(winid).visible)
2185 call feedkeys("eli\<Esc>", 'xt')
2186 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002187 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002188
2189 bwipe!
2190 call test_override('ALL', 0)
2191endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002192
2193func Test_notifications()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002194 CheckFeature timers
2195 CheckScreendump
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002196
Bram Moolenaar0fdddee2019-09-01 15:26:23 +02002197 let lines =<< trim END
2198 call setline(1, range(1, 20))
2199 hi Notification ctermbg=lightblue
2200 call popup_notification('first notification', {})
2201 END
2202 call writefile(lines, 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002203 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002204 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
2205
2206 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002207 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
2208 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002209 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
2210
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002211 " clean up
2212 call StopVimInTerminal(buf)
2213 call delete('XtestNotifications')
2214endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002215
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002216func Test_popup_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002217 CheckScreendump
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002218
2219 let lines =<< trim END
2220 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02002221 hi ScrollThumb ctermbg=blue
2222 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002223 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002224 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002225 \ minwidth: 8,
2226 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002227 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002228 func ScrollUp()
2229 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
2230 endfunc
2231 func ScrollDown()
2232 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
2233 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002234 func ClickTop()
2235 call feedkeys("\<F4>\<LeftMouse>", "xt")
2236 endfunc
2237 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002238 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002239 call feedkeys("\<F5>\<LeftMouse>", "xt")
2240 endfunc
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002241 func Popup_filter(winid, key)
2242 if a:key == 'j'
Bram Moolenaar371806e2020-10-22 13:44:54 +02002243 silent! this_throws_an_error_but_is_ignored
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002244 let line = popup_getoptions(a:winid).firstline
2245 let nlines = line('$', a:winid)
2246 let newline = line < nlines ? (line + 1) : nlines
2247 call popup_setoptions(a:winid, #{firstline: newline})
2248 return v:true
2249 elseif a:key == 'x'
2250 call popup_close(a:winid)
2251 return v:true
2252 endif
2253 endfunc
2254
Bram Moolenaar6c542f72020-09-27 21:16:45 +02002255 def CreatePopup(text: list<string>)
Bram Moolenaare0de1712020-12-02 17:36:54 +01002256 popup_create(text, {
Bram Moolenaar6c542f72020-09-27 21:16:45 +02002257 \ minwidth: 30,
2258 \ maxwidth: 30,
2259 \ minheight: 4,
2260 \ maxheight: 4,
2261 \ firstline: 1,
2262 \ lastline: 4,
2263 \ wrap: true,
2264 \ scrollbar: true,
2265 \ mapping: false,
2266 \ filter: Popup_filter,
2267 \ })
2268 enddef
2269
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002270 func PopupScroll()
2271 call popup_clear()
2272 let text =<< trim END
2273 1
2274 2
2275 3
2276 4
2277 long line long line long line long line long line long line
2278 long line long line long line long line long line long line
2279 long line long line long line long line long line long line
2280 END
Bram Moolenaar6c542f72020-09-27 21:16:45 +02002281 call CreatePopup(text)
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002282 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002283 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002284 map <silent> <F4> :call test_setmouse(4, 42)<CR>
2285 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002286 END
2287 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002288 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002289 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
2290
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002291 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002292 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002293 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
2294
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002295 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002296 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002297 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
2298
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002299 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002300 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002301 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
2302
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02002303 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb', firstline: 5})\<CR>")
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002304 " this scrolls two lines (half the window height)
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002305 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
2306 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
2307
2308 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2309 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
2310
2311 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02002312 " wait a bit, otherwise it fails sometimes (double click recognized?)
2313 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002314 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2315 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
2316
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002317 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2318 sleep 100m
2319 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2320 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
2321
2322 call term_sendkeys(buf, ":call ClickBot()\<CR>")
2323 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
2324
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002325 " remove the minwidth and maxheight
2326 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxheight: 0, minwidth: 0})\<CR>")
Bram Moolenaar7e0f4622019-09-17 21:23:39 +02002327 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002328 call VerifyScreenDump(buf, 'Test_popupwin_scroll_10', {})
2329
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002330 " check size with non-wrapping lines
2331 call term_sendkeys(buf, ":call PopupScroll()\<CR>")
2332 call VerifyScreenDump(buf, 'Test_popupwin_scroll_11', {})
2333
2334 " check size with wrapping lines
2335 call term_sendkeys(buf, "j")
2336 call VerifyScreenDump(buf, 'Test_popupwin_scroll_12', {})
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002337
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002338 " clean up
Bram Moolenaar20298ce2020-06-19 21:46:52 +02002339 call term_sendkeys(buf, "x")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002340 call StopVimInTerminal(buf)
2341 call delete('XtestPopupScroll')
2342endfunc
2343
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02002344func Test_popup_too_high_scrollbar()
2345 CheckScreendump
2346
2347 let lines =<< trim END
2348 call setline(1, range(1, 20)->map({i, v -> repeat(v, 10)}))
2349 set scrolloff=0
2350 func ShowPopup()
2351 let winid = popup_atcursor(['one', 'two', 'three', 'four', 'five',
2352 \ 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve'], #{
2353 \ minwidth: 8,
2354 \ border: [],
2355 \ })
2356 endfunc
2357 normal 3G$
2358 call ShowPopup()
2359 END
2360 call writefile(lines, 'XtestPopupToohigh')
2361 let buf = RunVimInTerminal('-S XtestPopupToohigh', #{rows: 10})
2362 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_1', {})
2363
2364 call term_sendkeys(buf, ":call popup_clear()\<CR>")
2365 call term_sendkeys(buf, "8G$")
2366 call term_sendkeys(buf, ":call ShowPopup()\<CR>")
2367 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_2', {})
2368
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02002369 call term_sendkeys(buf, ":call popup_clear()\<CR>")
2370 call term_sendkeys(buf, "gg$")
2371 call term_sendkeys(buf, ":call ShowPopup()\<CR>")
2372 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_3', {})
2373
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02002374 " clean up
2375 call StopVimInTerminal(buf)
2376 call delete('XtestPopupToohigh')
2377endfunc
2378
Bram Moolenaar437a7462019-07-05 20:17:22 +02002379func Test_popup_fitting_scrollbar()
2380 " this was causing a crash, divide by zero
2381 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002382 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002383 \ scrollbar: 1,
2384 \ maxwidth: 10,
2385 \ maxheight: 5,
2386 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02002387 redraw
2388 call popup_clear()
2389endfunc
2390
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002391func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002392 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002393
2394 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002395 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002396 let p = popup_create('test', opts)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002397 eval p->popup_settext('this is a text')
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002398 END
2399
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002400 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002401 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002402 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
2403
2404 " Setting to empty string clears it
2405 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
2406 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
2407
2408 " Setting a list
2409 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2410 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2411
2412 " Shrinking with a list
2413 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
2414 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
2415
2416 " Growing with a list
2417 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2418 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2419
2420 " Empty list clears
2421 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
2422 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
2423
2424 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002425 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002426 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
2427
Bram Moolenaarb0992022020-01-30 14:55:42 +01002428 " range() (doesn't work)
2429 call term_sendkeys(buf, ":call popup_settext(p, range(4, 8))\<CR>")
2430 call VerifyScreenDump(buf, 'Test_popup_settext_07', {})
2431
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002432 " clean up
2433 call StopVimInTerminal(buf)
2434 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002435endfunc
2436
Bram Moolenaar1f42f5a2020-09-03 18:52:24 +02002437func Test_popup_settext_getline()
2438 let id = popup_create('', #{ tabpage: 0 })
2439 call popup_settext(id, ['a','b'])
2440 call assert_equal(2, line('$', id)) " OK :)
2441 call popup_close(id)
2442
2443 let id = popup_create('', #{ tabpage: -1 })
2444 call popup_settext(id, ['a','b'])
2445 call assert_equal(2, line('$', id)) " Fails :(
2446 call popup_close(id)
2447endfunc
2448
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02002449func Test_popup_settext_null()
2450 let id = popup_create('', #{ tabpage: 0 })
2451 call popup_settext(id, test_null_list())
2452 call popup_close(id)
2453
2454 let id = popup_create('', #{ tabpage: 0 })
2455 call popup_settext(id, test_null_string())
2456 call popup_close(id)
2457endfunc
2458
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002459func Test_popup_hidden()
2460 new
2461
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002462 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002463 redraw
2464 call assert_equal(0, popup_getpos(winid).visible)
2465 call popup_close(winid)
2466
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002467 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002468 redraw
2469 call assert_equal(0, popup_getpos(winid).visible)
2470 call popup_close(winid)
2471
2472 func QuitCallback(id, res)
2473 let s:cb_winid = a:id
2474 let s:cb_res = a:res
2475 endfunc
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002476 let winid = 'make a choice'->popup_dialog(#{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002477 \ filter: 'popup_filter_yesno',
2478 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002479 \ })
2480 redraw
2481 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02002482 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
2483 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002484 exe "normal anot used by filter\<Esc>"
2485 call assert_equal('not used by filter', getline(1))
2486
2487 call popup_show(winid)
2488 call feedkeys('y', "xt")
2489 call assert_equal(1, s:cb_res)
2490
2491 bwipe!
2492 delfunc QuitCallback
2493endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02002494
2495" Test options not checked elsewhere
2496func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002497 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002498 let options = popup_getoptions(winid)
2499 call assert_equal(1, options.wrap)
2500 call assert_equal(0, options.drag)
2501 call assert_equal('Beautiful', options.highlight)
2502
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002503 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002504 let options = popup_getoptions(winid)
2505 call assert_equal(0, options.wrap)
2506 call assert_equal(1, options.drag)
2507 call assert_equal('Another', options.highlight)
2508
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002509 call assert_fails('call popup_setoptions(winid, [])', 'E715:')
2510 call assert_fails('call popup_setoptions(winid, test_null_dict())', 'E715:')
2511
Bram Moolenaarae943152019-06-16 22:54:14 +02002512 call popup_close(winid)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002513 call assert_equal(0, popup_setoptions(winid, options.wrap))
Bram Moolenaarae943152019-06-16 22:54:14 +02002514endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002515
2516func Test_popupwin_garbage_collect()
2517 func MyPopupFilter(x, winid, c)
2518 " NOP
2519 endfunc
2520
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002521 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002522 call test_garbagecollect_now()
2523 redraw
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02002524 " Must not crash caused by invalid memory access
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002525 call feedkeys('j', 'xt')
2526 call assert_true(v:true)
2527
2528 call popup_close(winid)
2529 delfunc MyPopupFilter
2530endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002531
Bram Moolenaar581ba392019-09-03 22:08:33 +02002532func Test_popupwin_filter_mode()
2533 func MyPopupFilter(winid, c)
2534 let s:typed = a:c
2535 if a:c == ':' || a:c == "\r" || a:c == 'v'
2536 " can start cmdline mode, get out, and start/stop Visual mode
2537 return 0
2538 endif
2539 return 1
2540 endfunc
2541
2542 " Normal, Visual and Insert mode
2543 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'nvi'})
2544 redraw
2545 call feedkeys('x', 'xt')
2546 call assert_equal('x', s:typed)
2547
2548 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2549 call assert_equal(':', s:typed)
2550 call assert_equal('foo', g:foo)
2551
2552 let @x = 'something'
2553 call feedkeys('v$"xy', 'xt')
2554 call assert_equal('y', s:typed)
2555 call assert_equal('something', @x) " yank command is filtered out
2556 call feedkeys('v', 'xt') " end Visual mode
2557
2558 call popup_close(winid)
2559
2560 " only Normal mode
2561 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'n'})
2562 redraw
2563 call feedkeys('x', 'xt')
2564 call assert_equal('x', s:typed)
2565
2566 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2567 call assert_equal(':', s:typed)
2568 call assert_equal('foo', g:foo)
2569
2570 let @x = 'something'
2571 call feedkeys('v$"xy', 'xt')
2572 call assert_equal('v', s:typed)
2573 call assert_notequal('something', @x)
2574
2575 call popup_close(winid)
2576
2577 " default: all modes
2578 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
2579 redraw
2580 call feedkeys('x', 'xt')
2581 call assert_equal('x', s:typed)
2582
2583 let g:foo = 'bar'
2584 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2585 call assert_equal("\r", s:typed)
2586 call assert_equal('bar', g:foo)
2587
2588 let @x = 'something'
2589 call feedkeys('v$"xy', 'xt')
2590 call assert_equal('y', s:typed)
2591 call assert_equal('something', @x) " yank command is filtered out
2592 call feedkeys('v', 'xt') " end Visual mode
2593
2594 call popup_close(winid)
2595 delfunc MyPopupFilter
2596endfunc
2597
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002598func Test_popupwin_filter_mouse()
2599 func MyPopupFilter(winid, c)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002600 let g:got_mousepos = getmousepos()
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002601 return 0
2602 endfunc
2603
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002604 call setline(1, ['.'->repeat(25)]->repeat(10))
2605 let winid = popup_create(['short', 'long line that will wrap', 'other'], #{
2606 \ line: 2,
2607 \ col: 4,
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002608 \ maxwidth: 12,
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002609 \ padding: [],
2610 \ border: [],
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002611 \ filter: 'MyPopupFilter',
2612 \ })
2613 redraw
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002614 " 123456789012345678901
2615 " 1 .....................
2616 " 2 ...+--------------+..
2617 " 3 ...| |..
2618 " 4 ...| short |..
2619 " 5 ...| long line th |..
2620 " 6 ...| at will wrap |..
2621 " 7 ...| other |..
2622 " 8 ...| |..
2623 " 9 ...+--------------+..
2624 " 10 .....................
2625 let tests = []
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002626
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002627 func AddItemOutsidePopup(tests, row, col)
2628 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2629 \ screenrow: a:row, screencol: a:col,
2630 \ winid: win_getid(), winrow: a:row, wincol: a:col,
2631 \ line: a:row, column: a:col,
2632 \ }})
2633 endfunc
2634 func AddItemInPopupBorder(tests, winid, row, col)
2635 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2636 \ screenrow: a:row, screencol: a:col,
2637 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2638 \ line: 0, column: 0,
2639 \ }})
2640 endfunc
2641 func AddItemInPopupText(tests, winid, row, col, textline, textcol)
2642 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2643 \ screenrow: a:row, screencol: a:col,
2644 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2645 \ line: a:textline, column: a:textcol,
2646 \ }})
2647 endfunc
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002648
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002649 " above and below popup
2650 for c in range(1, 21)
2651 call AddItemOutsidePopup(tests, 1, c)
2652 call AddItemOutsidePopup(tests, 10, c)
2653 endfor
2654 " left and right of popup
2655 for r in range(1, 10)
2656 call AddItemOutsidePopup(tests, r, 3)
2657 call AddItemOutsidePopup(tests, r, 20)
2658 endfor
2659 " top and bottom in popup
2660 for c in range(4, 19)
2661 call AddItemInPopupBorder(tests, winid, 2, c)
2662 call AddItemInPopupBorder(tests, winid, 3, c)
2663 call AddItemInPopupBorder(tests, winid, 8, c)
2664 call AddItemInPopupBorder(tests, winid, 9, c)
2665 endfor
2666 " left and right margin in popup
2667 for r in range(2, 9)
2668 call AddItemInPopupBorder(tests, winid, r, 4)
2669 call AddItemInPopupBorder(tests, winid, r, 5)
2670 call AddItemInPopupBorder(tests, winid, r, 18)
2671 call AddItemInPopupBorder(tests, winid, r, 19)
2672 endfor
2673 " text "short"
2674 call AddItemInPopupText(tests, winid, 4, 6, 1, 1)
2675 call AddItemInPopupText(tests, winid, 4, 10, 1, 5)
2676 call AddItemInPopupText(tests, winid, 4, 11, 1, 6)
2677 call AddItemInPopupText(tests, winid, 4, 17, 1, 6)
2678 " text "long line th"
2679 call AddItemInPopupText(tests, winid, 5, 6, 2, 1)
2680 call AddItemInPopupText(tests, winid, 5, 10, 2, 5)
2681 call AddItemInPopupText(tests, winid, 5, 17, 2, 12)
2682 " text "at will wrap"
2683 call AddItemInPopupText(tests, winid, 6, 6, 2, 13)
2684 call AddItemInPopupText(tests, winid, 6, 10, 2, 17)
2685 call AddItemInPopupText(tests, winid, 6, 17, 2, 24)
2686 " text "other"
2687 call AddItemInPopupText(tests, winid, 7, 6, 3, 1)
2688 call AddItemInPopupText(tests, winid, 7, 10, 3, 5)
2689 call AddItemInPopupText(tests, winid, 7, 11, 3, 6)
2690 call AddItemInPopupText(tests, winid, 7, 17, 3, 6)
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002691
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002692 for item in tests
2693 call test_setmouse(item.clickrow, item.clickcol)
2694 call feedkeys("\<LeftMouse>", 'xt')
2695 call assert_equal(item.result, g:got_mousepos)
2696 endfor
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002697
2698 call popup_close(winid)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002699 enew!
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002700 delfunc MyPopupFilter
2701endfunc
2702
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002703func Test_popupwin_with_buffer()
2704 call writefile(['some text', 'in a buffer'], 'XsomeFile')
2705 let buf = bufadd('XsomeFile')
2706 call assert_equal(0, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002707
2708 setlocal number
2709 call setbufvar(buf, "&wrapmargin", 13)
2710
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002711 let winid = popup_create(buf, {})
2712 call assert_notequal(0, winid)
2713 let pos = popup_getpos(winid)
2714 call assert_equal(2, pos.height)
2715 call assert_equal(1, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002716
2717 " window-local option is set to default, buffer-local is not
2718 call assert_equal(0, getwinvar(winid, '&number'))
2719 call assert_equal(13, getbufvar(buf, '&wrapmargin'))
2720
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002721 call popup_close(winid)
2722 call assert_equal({}, popup_getpos(winid))
2723 call assert_equal(1, bufloaded(buf))
2724 exe 'bwipe! ' .. buf
Bram Moolenaar46451042019-08-24 15:50:46 +02002725 setlocal nonumber
Bram Moolenaar7866b872019-07-01 22:21:01 +02002726
2727 edit test_popupwin.vim
2728 let winid = popup_create(bufnr(''), {})
2729 redraw
2730 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002731 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002732endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02002733
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002734func Test_popupwin_terminal_buffer()
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002735 CheckFeature terminal
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002736 CheckUnix
Bram Moolenaare06a28f2020-05-13 23:24:12 +02002737 " Starting a terminal to run a shell in is considered flaky.
2738 let g:test_is_flaky = 1
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002739
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002740 let origwin = win_getid()
Bram Moolenaar349f6092020-10-06 20:46:49 +02002741
2742 " open help window to test that :help below fails
2743 help
2744
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002745 let termbuf = term_start(&shell, #{hidden: 1})
Bram Moolenaarf5452692020-11-28 21:56:06 +01002746 let winid = popup_create(termbuf, #{minwidth: 40, minheight: 10, border: []})
Bram Moolenaar0353f562020-12-17 22:27:38 +01002747 " Wait for shell to start
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002748 call WaitForAssert({-> assert_equal("run", job_status(term_getjob(termbuf)))})
Bram Moolenaar0353f562020-12-17 22:27:38 +01002749 " Wait for a prompt (see border char first, then space after prompt)
2750 call WaitForAssert({ -> assert_equal(' ', screenstring(screenrow(), screencol() - 1))})
Bram Moolenaarf5452692020-11-28 21:56:06 +01002751
2752 " When typing a character, the cursor is after it.
2753 call feedkeys("x", 'xt')
Bram Moolenaar0353f562020-12-17 22:27:38 +01002754 call term_wait(termbuf)
Bram Moolenaarf5452692020-11-28 21:56:06 +01002755 redraw
2756 call WaitForAssert({ -> assert_equal('x', screenstring(screenrow(), screencol() - 1))})
2757 call feedkeys("\<BS>", 'xt')
2758
Bram Moolenaar631ebc42020-02-03 22:15:26 +01002759 " Check this doesn't crash
2760 call assert_equal(winnr(), winnr('j'))
2761 call assert_equal(winnr(), winnr('k'))
2762 call assert_equal(winnr(), winnr('h'))
2763 call assert_equal(winnr(), winnr('l'))
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002764
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002765 " Cannot quit while job is running
2766 call assert_fails('call feedkeys("\<C-W>:quit\<CR>", "xt")', 'E948:')
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002767
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002768 " Cannot enter Terminal-Normal mode. (TODO: but it works...)
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002769 call feedkeys("xxx\<C-W>N", 'xt')
2770 call assert_fails('call feedkeys("gf", "xt")', 'E863:')
2771 call feedkeys("a\<C-U>", 'xt')
2772
Bram Moolenaar3f65c662020-05-27 23:15:16 +02002773 " Cannot escape from terminal window
2774 call assert_fails('tab drop xxx', 'E863:')
Bram Moolenaar349f6092020-10-06 20:46:49 +02002775 call assert_fails('help', 'E994:')
Bram Moolenaar3f65c662020-05-27 23:15:16 +02002776
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002777 " Cannot open a second one.
2778 let termbuf2 = term_start(&shell, #{hidden: 1})
2779 call assert_fails('call popup_create(termbuf2, #{})', 'E861:')
2780 call term_sendkeys(termbuf2, "exit\<CR>")
2781
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002782 " Exiting shell closes popup window
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002783 call feedkeys("exit\<CR>", 'xt')
2784 " Wait for shell to exit
Bram Moolenaarb2b218d2020-06-22 20:22:19 +02002785 call WaitForAssert({-> assert_equal("dead", job_status(term_getjob(termbuf)))})
Bram Moolenaar373c6512020-06-22 19:24:23 +02002786
Bram Moolenaar349f6092020-10-06 20:46:49 +02002787 helpclose
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002788 call feedkeys(":quit\<CR>", 'xt')
2789 call assert_equal(origwin, win_getid())
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002790endfunc
2791
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002792func Test_popupwin_close_prevwin()
2793 CheckFeature terminal
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002794 call Popupwin_close_prevwin()
2795endfunc
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002796
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002797def Popupwin_close_prevwin()
2798 assert_equal(1, winnr('$'))
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002799 split
2800 wincmd b
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002801 assert_equal(2, winnr())
Bram Moolenaare0de1712020-12-02 17:36:54 +01002802 var buf = term_start(&shell, {hidden: 1})
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002803 popup_create(buf, {})
2804 TermWait(buf, 100)
2805 popup_clear(true)
2806 assert_equal(2, winnr())
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002807
2808 quit
2809 exe 'bwipe! ' .. buf
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002810enddef
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002811
Bram Moolenaar934470e2019-09-01 23:27:05 +02002812func Test_popupwin_with_buffer_and_filter()
2813 new Xwithfilter
2814 call setline(1, range(100))
2815 let bufnr = bufnr()
2816 hide
2817
2818 func BufferFilter(win, key)
2819 if a:key == 'G'
2820 " recursive use of "G" does not cause problems.
2821 call win_execute(a:win, 'normal! G')
2822 return 1
2823 endif
2824 return 0
2825 endfunc
2826
2827 let winid = popup_create(bufnr, #{maxheight: 5, filter: 'BufferFilter'})
2828 call assert_equal(1, popup_getpos(winid).firstline)
2829 redraw
2830 call feedkeys("G", 'xt')
2831 call assert_equal(99, popup_getpos(winid).firstline)
2832
2833 call popup_close(winid)
2834 exe 'bwipe! ' .. bufnr
2835endfunc
2836
Bram Moolenaare296e312019-07-03 23:20:18 +02002837func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002838 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002839 \ maxwidth: 40,
2840 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02002841 \ })
2842 for top in range(1, 20)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002843 eval winid->popup_setoptions(#{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02002844 redraw
2845 call assert_equal(19, popup_getpos(winid).width)
2846 endfor
2847 call popup_clear()
2848endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02002849
2850func Test_popupwin_buf_close()
2851 let buf = bufadd('Xtestbuf')
2852 call bufload(buf)
2853 call setbufline(buf, 1, ['just', 'some', 'lines'])
2854 let winid = popup_create(buf, {})
2855 redraw
2856 call assert_equal(3, popup_getpos(winid).height)
2857 let bufinfo = getbufinfo(buf)[0]
2858 call assert_equal(1, bufinfo.changed)
2859 call assert_equal(0, bufinfo.hidden)
2860 call assert_equal(0, bufinfo.listed)
2861 call assert_equal(1, bufinfo.loaded)
2862 call assert_equal([], bufinfo.windows)
2863 call assert_equal([winid], bufinfo.popups)
2864
2865 call popup_close(winid)
2866 call assert_equal({}, popup_getpos(winid))
2867 let bufinfo = getbufinfo(buf)[0]
2868 call assert_equal(1, bufinfo.changed)
2869 call assert_equal(1, bufinfo.hidden)
2870 call assert_equal(0, bufinfo.listed)
2871 call assert_equal(1, bufinfo.loaded)
2872 call assert_equal([], bufinfo.windows)
2873 call assert_equal([], bufinfo.popups)
2874 exe 'bwipe! ' .. buf
2875endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02002876
2877func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002878 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02002879
2880 let lines =<< trim END
2881 call setline(1, range(1, 10))
2882 hi ScrollThumb ctermbg=blue
2883 hi ScrollBar ctermbg=red
2884 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002885 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002886 \ maxwidth: 10,
2887 \ maxheight: 3,
2888 \ pos : 'topleft',
2889 \ col : a:col,
2890 \ line : a:line,
2891 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02002892 \ })
2893 endfunc
2894 call PopupMenu(['x'], 1, 1)
2895 call PopupMenu(['123456789|'], 1, 16)
2896 call PopupMenu(['123456789|' .. ' '], 7, 1)
2897 call PopupMenu([repeat('123456789|', 100)], 7, 16)
2898 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
2899 END
2900 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002901 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02002902 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
2903
2904 " close the menu popupwin.
2905 call term_sendkeys(buf, " ")
2906 call term_sendkeys(buf, " ")
2907 call term_sendkeys(buf, " ")
2908 call term_sendkeys(buf, " ")
2909 call term_sendkeys(buf, " ")
2910
2911 " clean up
2912 call StopVimInTerminal(buf)
2913 call delete('XtestPopupMenuMaxWidth')
2914endfunc
2915
Bram Moolenaara901a372019-07-13 16:38:50 +02002916func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002917 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02002918
2919 let lines =<< trim END
2920 call setline(1, range(1, 20))
2921 hi ScrollThumb ctermbg=blue
2922 hi ScrollBar ctermbg=red
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002923 eval ['one', 'two', 'three', 'four', 'five',
2924 \ 'six', 'seven', 'eight', 'nine']
2925 \ ->popup_menu(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002926 \ minwidth: 8,
2927 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02002928 \ })
2929 END
2930 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002931 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02002932
2933 call term_sendkeys(buf, "j")
2934 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
2935
2936 call term_sendkeys(buf, "jjj")
2937 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
2938
2939 " if the cursor is the bottom line, it stays at the bottom line.
2940 call term_sendkeys(buf, repeat("j", 20))
2941 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
2942
2943 call term_sendkeys(buf, "kk")
2944 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
2945
2946 call term_sendkeys(buf, "k")
2947 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
2948
2949 " if the cursor is in the top line, it stays in the top line.
2950 call term_sendkeys(buf, repeat("k", 20))
2951 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
2952
2953 " close the menu popupwin.
2954 call term_sendkeys(buf, " ")
2955
2956 " clean up
2957 call StopVimInTerminal(buf)
2958 call delete('XtestPopupMenuScroll')
2959endfunc
2960
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002961func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002962 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002963
2964 let lines =<< trim END
2965 function! MyFilter(winid, key) abort
2966 if a:key == "0"
2967 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
2968 return 1
2969 endif
2970 if a:key == "G"
2971 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
2972 return 1
2973 endif
2974 if a:key == "j"
2975 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
2976 return 1
2977 endif
2978 if a:key == "k"
2979 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
2980 return 1
2981 endif
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002982 if a:key == ':'
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002983 call popup_close(a:winid)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002984 return 0
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002985 endif
2986 return 0
2987 endfunction
2988 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
2989 \ maxheight : 3,
2990 \ filter : 'MyFilter'
2991 \ })
2992 END
2993 call writefile(lines, 'XtestPopupMenuFilter')
2994 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
2995
2996 call term_sendkeys(buf, "j")
2997 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
2998
2999 call term_sendkeys(buf, "k")
3000 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
3001
3002 call term_sendkeys(buf, "G")
3003 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
3004
3005 call term_sendkeys(buf, "0")
3006 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
3007
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02003008 " check that when the popup is closed in the filter the screen is redrawn
3009 call term_sendkeys(buf, ":")
3010 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_5', {})
3011 call term_sendkeys(buf, "\<CR>")
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003012
3013 " clean up
3014 call StopVimInTerminal(buf)
3015 call delete('XtestPopupMenuFilter')
3016endfunc
3017
3018func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003019 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003020
3021 let winid = popup_create('some text', {})
3022 call assert_equal(0, popup_getoptions(winid).cursorline)
3023 call popup_close(winid)
3024
3025 let winid = popup_create('some text', #{ cursorline: 1, })
3026 call assert_equal(1, popup_getoptions(winid).cursorline)
3027 call popup_close(winid)
3028
Bram Moolenaar6bfc4752021-02-21 23:12:18 +01003029 let winid = popup_create('some text', #{ cursorline: v:true, })
3030 call assert_equal(1, popup_getoptions(winid).cursorline)
3031 call popup_close(winid)
3032
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003033 let winid = popup_create('some text', #{ cursorline: 0, })
3034 call assert_equal(0, popup_getoptions(winid).cursorline)
3035 call popup_close(winid)
3036
3037 let winid = popup_menu('some text', {})
3038 call assert_equal(1, popup_getoptions(winid).cursorline)
3039 call popup_close(winid)
3040
3041 let winid = popup_menu('some text', #{ cursorline: 1, })
3042 call assert_equal(1, popup_getoptions(winid).cursorline)
3043 call popup_close(winid)
3044
3045 let winid = popup_menu('some text', #{ cursorline: 0, })
3046 call assert_equal(0, popup_getoptions(winid).cursorline)
3047 call popup_close(winid)
3048
3049 " ---------
3050 " Pattern 1
3051 " ---------
3052 let lines =<< trim END
3053 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
3054 END
3055 call writefile(lines, 'XtestPopupCursorLine')
3056 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3057 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
3058 call term_sendkeys(buf, ":call popup_clear()\<cr>")
3059 call StopVimInTerminal(buf)
3060
3061 " ---------
3062 " Pattern 2
3063 " ---------
3064 let lines =<< trim END
3065 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
3066 END
3067 call writefile(lines, 'XtestPopupCursorLine')
3068 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3069 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
3070 call term_sendkeys(buf, ":call popup_clear()\<cr>")
3071 call StopVimInTerminal(buf)
3072
3073 " ---------
3074 " Pattern 3
3075 " ---------
3076 let lines =<< trim END
3077 function! MyFilter(winid, key) abort
3078 if a:key == "j"
3079 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
3080 return 1
3081 endif
3082 if a:key == 'x'
3083 call popup_close(a:winid)
3084 return 1
3085 endif
3086 return 0
3087 endfunction
3088 call popup_menu(['111', '222', '333'], #{
3089 \ cursorline : 0,
3090 \ maxheight : 2,
3091 \ filter : 'MyFilter',
3092 \ })
3093 END
3094 call writefile(lines, 'XtestPopupCursorLine')
3095 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3096 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
3097 call term_sendkeys(buf, "j")
3098 call term_sendkeys(buf, "j")
3099 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
3100 call term_sendkeys(buf, "x")
3101 call StopVimInTerminal(buf)
3102
3103 " ---------
3104 " Pattern 4
3105 " ---------
3106 let lines =<< trim END
3107 function! MyFilter(winid, key) abort
3108 if a:key == "j"
3109 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
3110 return 1
3111 endif
3112 if a:key == 'x'
3113 call popup_close(a:winid)
3114 return 1
3115 endif
3116 return 0
3117 endfunction
3118 call popup_menu(['111', '222', '333'], #{
3119 \ cursorline : 1,
3120 \ maxheight : 2,
3121 \ filter : 'MyFilter',
3122 \ })
3123 END
3124 call writefile(lines, 'XtestPopupCursorLine')
3125 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3126 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
3127 call term_sendkeys(buf, "j")
3128 call term_sendkeys(buf, "j")
3129 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
3130 call term_sendkeys(buf, "x")
3131 call StopVimInTerminal(buf)
3132
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003133 " ---------
3134 " Cursor in second line when creating the popup
3135 " ---------
3136 let lines =<< trim END
3137 let winid = popup_create(['111', '222', '333'], #{
3138 \ cursorline : 1,
3139 \ })
3140 call win_execute(winid, "2")
3141 END
3142 call writefile(lines, 'XtestPopupCursorLine')
3143 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3144 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_7', {})
3145 call StopVimInTerminal(buf)
3146
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003147 call delete('XtestPopupCursorLine')
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01003148
3149 " ---------
3150 " Use current buffer for popupmenu
3151 " ---------
3152 let lines =<< trim END
3153 call setline(1, ['one', 'two', 'three'])
3154 let winid = popup_create(bufnr('%'), #{
3155 \ cursorline : 1,
3156 \ })
3157 call win_execute(winid, "2")
3158 END
3159 call writefile(lines, 'XtestPopupCursorLine')
3160 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3161 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_8', {})
3162 call StopVimInTerminal(buf)
3163
3164 call delete('XtestPopupCursorLine')
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003165endfunc
3166
Bram Moolenaar6bfc4752021-02-21 23:12:18 +01003167def Test_popup_cursorline_vim9()
3168 var winid = popup_create('some text', { cursorline: true, })
3169 assert_equal(1, popup_getoptions(winid).cursorline)
3170 popup_close(winid)
3171
3172 assert_fails("popup_create('some text', { cursorline: 2, })", 'E1023:')
3173 popup_clear()
3174enddef
3175
Bram Moolenaarf914a332019-07-20 15:09:56 +02003176func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003177 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003178 CheckFeature quickfix
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003179
Bram Moolenaarf914a332019-07-20 15:09:56 +02003180 call writefile([
3181 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
3182 \ "another\tXtagfile\t/^this is another",
3183 \ "theword\tXtagfile\t/^theword"],
3184 \ 'Xtags')
3185 call writefile(range(1,20)
3186 \ + ['theword is here']
3187 \ + range(22, 27)
3188 \ + ['this is another place']
3189 \ + range(29, 40),
3190 \ "Xtagfile")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003191 call writefile(range(1,10)
3192 \ + ['searched word is here']
3193 \ + range(12, 20),
3194 \ "Xheader.h")
Bram Moolenaarf914a332019-07-20 15:09:56 +02003195 let lines =<< trim END
3196 set tags=Xtags
3197 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003198 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003199 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003200 \ 'three',
3201 \ 'four',
3202 \ 'five',
3203 \ 'six',
3204 \ 'seven',
3205 \ 'find theword somewhere',
3206 \ 'nine',
3207 \ 'this is another word',
3208 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02003209 set previewpopup=height:4,width:40
Bram Moolenaar60577482021-03-04 21:35:07 +01003210 hi OtherColor ctermbg=lightcyan guibg=lightcyan
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003211 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02003212 END
3213 call writefile(lines, 'XtestPreviewPopup')
3214 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
3215
3216 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
3217 call term_sendkeys(buf, ":\<CR>")
3218 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
3219
Bram Moolenaar60577482021-03-04 21:35:07 +01003220 call term_sendkeys(buf, ":set previewpopup+=highlight:OtherColor\<CR>")
Bram Moolenaarf914a332019-07-20 15:09:56 +02003221 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
3222 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
3223
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003224 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003225 call term_sendkeys(buf, ":\<CR>")
3226 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
3227
3228 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
3229 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
3230
Bram Moolenaar799439a2020-02-11 21:44:17 +01003231 call term_sendkeys(buf, ":silent cd ..\<CR>:\<CR>")
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003232 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar799439a2020-02-11 21:44:17 +01003233 call term_sendkeys(buf, ":silent cd testdir\<CR>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003234
Bram Moolenaar60577482021-03-04 21:35:07 +01003235 call term_sendkeys(buf, ":set previewpopup-=highlight:OtherColor\<CR>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003236 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02003237 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003238 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
3239
3240 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
3241 call term_sendkeys(buf, ":\<CR>")
3242 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
3243
3244 call term_sendkeys(buf, ":pclose\<CR>")
3245 call term_sendkeys(buf, ":psearch searched\<CR>")
3246 call term_sendkeys(buf, ":\<CR>")
3247 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003248
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01003249 call term_sendkeys(buf, "\<C-W>p")
3250 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_9', {})
3251
3252 call term_sendkeys(buf, ":call win_execute(popup_findpreview(), 'call popup_clear()')\<CR>")
3253 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_10', {})
3254
Bram Moolenaarf914a332019-07-20 15:09:56 +02003255 call StopVimInTerminal(buf)
3256 call delete('Xtags')
3257 call delete('Xtagfile')
3258 call delete('XtestPreviewPopup')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003259 call delete('Xheader.h')
Bram Moolenaarf914a332019-07-20 15:09:56 +02003260endfunc
3261
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003262func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003263 let lines =<< trim END
3264 set completeopt+=preview,popup
3265 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02003266 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003267
3268 func CompleteFuncDict(findstart, base)
3269 if a:findstart
3270 if col('.') > 10
3271 return col('.') - 10
3272 endif
3273 return 0
3274 endif
3275
3276 return {
3277 \ 'words': [
3278 \ {
3279 \ 'word': 'aword',
3280 \ 'abbr': 'wrd',
3281 \ 'menu': 'extra text',
3282 \ 'info': 'words are cool',
3283 \ 'kind': 'W',
3284 \ 'user_data': 'test'
3285 \ },
3286 \ {
3287 \ 'word': 'anotherword',
3288 \ 'abbr': 'anotwrd',
3289 \ 'menu': 'extra text',
3290 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
3291 \ 'kind': 'W',
3292 \ 'user_data': 'notest'
3293 \ },
3294 \ {
3295 \ 'word': 'noinfo',
3296 \ 'abbr': 'noawrd',
3297 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02003298 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003299 \ 'kind': 'W',
3300 \ 'user_data': 'notest'
3301 \ },
3302 \ {
3303 \ 'word': 'thatword',
3304 \ 'abbr': 'thatwrd',
3305 \ 'menu': 'extra text',
3306 \ 'info': 'that word is cool',
3307 \ 'kind': 'W',
3308 \ 'user_data': 'notest'
3309 \ },
3310 \ ]
3311 \ }
3312 endfunc
3313 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003314 func ChangeColor()
3315 let id = popup_findinfo()
Bram Moolenaard356fc62020-12-09 18:13:44 +01003316 if buflisted(winbufnr(id))
3317 call setline(1, 'buffer is listed')
3318 endif
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003319 eval id->popup_setoptions(#{highlight: 'InfoPopup'})
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003320 endfunc
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003321
3322 func InfoHidden()
3323 set completepopup=height:4,border:off,align:menu
3324 set completeopt-=popup completeopt+=popuphidden
3325 au CompleteChanged * call HandleChange()
3326 endfunc
3327
3328 let s:counter = 0
3329 func HandleChange()
3330 let s:counter += 1
3331 let selected = complete_info(['selected']).selected
3332 if selected <= 0
3333 " First time: do nothing, info remains hidden
3334 return
3335 endif
3336 if selected == 1
3337 " Second time: show info right away
3338 let id = popup_findinfo()
3339 if id
3340 call popup_settext(id, 'immediate info ' .. s:counter)
3341 call popup_show(id)
3342 endif
3343 else
3344 " Third time: show info after a short delay
3345 call timer_start(100, 'ShowInfo')
3346 endif
3347 endfunc
3348
3349 func ShowInfo(...)
3350 let id = popup_findinfo()
3351 if id
3352 call popup_settext(id, 'async info ' .. s:counter)
3353 call popup_show(id)
3354 endif
3355 endfunc
Bram Moolenaar2dfae042020-11-15 14:09:37 +01003356
Bakudankun65555002021-11-17 20:40:16 +00003357 func OpenOtherPopups()
3358 call popup_create([
3359 \ 'popup below',
3360 \ 'popup below',
3361 \ 'popup below',
3362 \ 'popup below',
3363 \ ], #{
3364 \ line: 'cursor',
3365 \ col: 'cursor+3',
3366 \ highlight: 'ErrorMsg',
3367 \ minwidth: 17,
3368 \ zindex: 50,
3369 \ })
3370 call popup_create([
3371 \ 'popup on top',
3372 \ 'popup on top',
3373 \ 'popup on top',
3374 \ ], #{
3375 \ line: 'cursor+3',
3376 \ col: 'cursor-10',
3377 \ highlight: 'Search',
3378 \ minwidth: 10,
3379 \ zindex: 200,
3380 \ })
3381 endfunc
3382
Bram Moolenaar2dfae042020-11-15 14:09:37 +01003383 " Check that no autocommands are triggered for the info popup
3384 au WinEnter * if win_gettype() == 'popup' | call setline(2, 'WinEnter') | endif
3385 au WinLeave * if win_gettype() == 'popup' | call setline(2, 'WinLeave') | endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003386 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003387 return lines
3388endfunc
3389
3390func Test_popupmenu_info_border()
3391 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003392 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003393
3394 let lines = Get_popupmenu_lines()
3395 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003396 call writefile(lines, 'XtestInfoPopup')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003397
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003398 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003399 call TermWait(buf, 25)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003400
3401 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3402 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
3403
3404 call term_sendkeys(buf, "\<C-N>")
3405 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
3406
3407 call term_sendkeys(buf, "\<C-N>")
3408 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
3409
3410 call term_sendkeys(buf, "\<C-N>\<C-N>")
3411 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
3412
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02003413 " info on the left with scrollbar
3414 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
3415 call term_sendkeys(buf, "\<C-N>\<C-N>")
3416 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
3417
Bram Moolenaar202c3f72019-11-21 12:12:35 +01003418 " Test that the popupmenu's scrollbar and infopopup do not overlap
3419 call term_sendkeys(buf, "\<Esc>")
3420 call term_sendkeys(buf, ":set pumheight=3\<CR>")
3421 call term_sendkeys(buf, "cc\<C-X>\<C-U>")
3422 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_6', {})
3423
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003424 " Hide the info popup, cycle through buffers, make sure it didn't get
Bram Moolenaarca7c0782020-01-14 20:42:48 +01003425 " deleted.
3426 call term_sendkeys(buf, "\<Esc>")
3427 call term_sendkeys(buf, ":set hidden\<CR>")
3428 call term_sendkeys(buf, ":bn\<CR>")
3429 call term_sendkeys(buf, ":bn\<CR>")
3430 call term_sendkeys(buf, "otest text test text\<C-X>\<C-U>")
3431 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_7', {})
3432
Bram Moolenaar447bfba2020-07-18 16:07:16 +02003433 " Test that when the option is changed the popup changes.
3434 call term_sendkeys(buf, "\<Esc>")
3435 call term_sendkeys(buf, ":set completepopup=border:off\<CR>")
3436 call term_sendkeys(buf, "a\<C-X>\<C-U>")
3437 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_8', {})
3438
Bram Moolenaar6d585f42020-07-26 22:20:54 +02003439 call term_sendkeys(buf, " \<Esc>")
3440 call term_sendkeys(buf, ":set completepopup+=width:10\<CR>")
3441 call term_sendkeys(buf, "a\<C-X>\<C-U>")
3442 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_9', {})
3443
Bram Moolenaarca7c0782020-01-14 20:42:48 +01003444 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003445 call StopVimInTerminal(buf)
3446 call delete('XtestInfoPopup')
3447endfunc
3448
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003449func Test_popupmenu_info_noborder()
3450 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003451 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003452
3453 let lines = Get_popupmenu_lines()
3454 call add(lines, 'set completepopup=height:4,border:off')
3455 call writefile(lines, 'XtestInfoPopupNb')
3456
3457 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003458 call TermWait(buf, 25)
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003459
3460 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3461 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
3462
3463 call StopVimInTerminal(buf)
3464 call delete('XtestInfoPopupNb')
3465endfunc
3466
Bram Moolenaar258cef52019-08-21 17:29:29 +02003467func Test_popupmenu_info_align_menu()
3468 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003469 CheckFeature quickfix
Bram Moolenaar258cef52019-08-21 17:29:29 +02003470
3471 let lines = Get_popupmenu_lines()
3472 call add(lines, 'set completepopup=height:4,border:off,align:menu')
3473 call writefile(lines, 'XtestInfoPopupNb')
3474
3475 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003476 call TermWait(buf, 25)
Bram Moolenaar258cef52019-08-21 17:29:29 +02003477
3478 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3479 call term_sendkeys(buf, "\<C-N>")
3480 call term_sendkeys(buf, "\<C-N>")
3481 call term_sendkeys(buf, "\<C-N>")
3482 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
3483
3484 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
3485 call term_sendkeys(buf, "\<C-N>")
3486 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
3487
3488 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003489 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02003490 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
3491 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
3492 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
3493
3494 call StopVimInTerminal(buf)
3495 call delete('XtestInfoPopupNb')
3496endfunc
3497
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003498func Test_popupmenu_info_hidden()
3499 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003500 CheckFeature quickfix
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003501
3502 let lines = Get_popupmenu_lines()
3503 call add(lines, 'call InfoHidden()')
3504 call writefile(lines, 'XtestInfoPopupHidden')
3505
3506 let buf = RunVimInTerminal('-S XtestInfoPopupHidden', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003507 call TermWait(buf, 25)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003508
3509 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3510 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_1', {})
3511
3512 call term_sendkeys(buf, "\<C-N>")
3513 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_2', {})
3514
3515 call term_sendkeys(buf, "\<C-N>")
3516 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_3', {})
3517
3518 call term_sendkeys(buf, "\<Esc>")
3519 call StopVimInTerminal(buf)
3520 call delete('XtestInfoPopupHidden')
3521endfunc
3522
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003523func Test_popupmenu_info_too_wide()
3524 CheckScreendump
3525 CheckFeature quickfix
3526
3527 let lines =<< trim END
3528 call setline(1, range(10))
3529
3530 set completeopt+=preview,popup
3531 set completepopup=align:menu
3532 set omnifunc=OmniFunc
3533 hi InfoPopup ctermbg=lightgrey
3534
3535 func OmniFunc(findstart, base)
3536 if a:findstart
3537 return 0
3538 endif
3539
3540 let menuText = 'some long text to make sure the menu takes up all of the width of the window'
3541 return #{
3542 \ words: [
3543 \ #{
3544 \ word: 'scrap',
3545 \ menu: menuText,
3546 \ info: "other words are\ncooler than this and some more text\nto make wrap",
3547 \ },
3548 \ #{
3549 \ word: 'scappier',
3550 \ menu: menuText,
3551 \ info: 'words are cool',
3552 \ },
3553 \ #{
3554 \ word: 'scrappier2',
3555 \ menu: menuText,
3556 \ info: 'words are cool',
3557 \ },
3558 \ ]
3559 \ }
3560 endfunc
3561 END
3562
3563 call writefile(lines, 'XtestInfoPopupWide')
3564 let buf = RunVimInTerminal('-S XtestInfoPopupWide', #{rows: 8})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003565 call TermWait(buf, 25)
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003566
3567 call term_sendkeys(buf, "Ascr\<C-X>\<C-O>")
3568 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_wide_1', {})
3569
3570 call term_sendkeys(buf, "\<Esc>")
3571 call StopVimInTerminal(buf)
3572 call delete('XtestInfoPopupWide')
3573endfunc
3574
Bakudankun65555002021-11-17 20:40:16 +00003575func Test_popupmenu_masking()
3576 " Test that popup windows that are opened while popup menu is open are
3577 " properly displayed.
3578 CheckScreendump
3579 CheckFeature quickfix
3580
3581 let lines = Get_popupmenu_lines()
3582 call add(lines, 'inoremap <C-A> <Cmd>call OpenOtherPopups()<CR>')
3583 call writefile(lines, 'XtestPopupmenuMasking')
3584
3585 let buf = RunVimInTerminal('-S XtestPopupmenuMasking', #{rows: 14})
3586 call TermWait(buf, 25)
3587
3588 call term_sendkeys(buf, "A\<C-X>\<C-U>\<C-A>")
3589 call VerifyScreenDump(buf, 'Test_popupwin_popupmenu_masking_1', {})
3590
3591 call term_sendkeys(buf, "\<Esc>")
3592 call VerifyScreenDump(buf, 'Test_popupwin_popupmenu_masking_2', {})
3593
3594 call StopVimInTerminal(buf)
3595 call delete('XtestPopupmenuMasking')
3596endfunc
3597
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003598func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02003599 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003600 call popup_clear()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003601 let winid = 'nothing wrong'->popup_notification({})
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003602 call assert_equal(bufnr, winbufnr(winid))
3603 call popup_clear()
3604endfunc
3605
Bram Moolenaar1824f452019-10-02 23:06:46 +02003606func Test_popupwin_getoptions_tablocal()
3607 topleft split
3608 let win1 = popup_create('nothing', #{maxheight: 8})
3609 let win2 = popup_create('something', #{maxheight: 10})
3610 let win3 = popup_create('something', #{maxheight: 15})
3611 call assert_equal(8, popup_getoptions(win1).maxheight)
3612 call assert_equal(10, popup_getoptions(win2).maxheight)
3613 call assert_equal(15, popup_getoptions(win3).maxheight)
3614 call popup_clear()
3615 quit
3616endfunc
3617
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02003618func Test_popupwin_cancel()
3619 let win1 = popup_create('one', #{line: 5, filter: {... -> 0}})
3620 let win2 = popup_create('two', #{line: 10, filter: {... -> 0}})
3621 let win3 = popup_create('three', #{line: 15, filter: {... -> 0}})
3622 call assert_equal(5, popup_getpos(win1).line)
3623 call assert_equal(10, popup_getpos(win2).line)
3624 call assert_equal(15, popup_getpos(win3).line)
3625 " TODO: this also works without patch 8.1.2110
3626 call feedkeys("\<C-C>", 'xt')
3627 call assert_equal(5, popup_getpos(win1).line)
3628 call assert_equal(10, popup_getpos(win2).line)
3629 call assert_equal({}, popup_getpos(win3))
3630 call feedkeys("\<C-C>", 'xt')
3631 call assert_equal(5, popup_getpos(win1).line)
3632 call assert_equal({}, popup_getpos(win2))
3633 call assert_equal({}, popup_getpos(win3))
3634 call feedkeys("\<C-C>", 'xt')
3635 call assert_equal({}, popup_getpos(win1))
3636 call assert_equal({}, popup_getpos(win2))
3637 call assert_equal({}, popup_getpos(win3))
3638endfunc
3639
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003640func Test_popupwin_filter_redraw()
3641 " Create two popups with a filter that closes the popup when typing "0".
3642 " Both popups should close, even though the redraw also calls
3643 " popup_reset_handled()
3644
3645 func CloseFilter(winid, key)
3646 if a:key == '0'
3647 call popup_close(a:winid)
3648 redraw
3649 endif
3650 return 0 " pass the key
3651 endfunc
3652
3653 let id1 = popup_create('first one', #{
3654 \ line: 1,
3655 \ col: 1,
3656 \ filter: 'CloseFilter',
3657 \ })
3658 let id2 = popup_create('second one', #{
3659 \ line: 9,
3660 \ col: 1,
3661 \ filter: 'CloseFilter',
3662 \ })
3663 call assert_equal(1, popup_getpos(id1).line)
3664 call assert_equal(9, popup_getpos(id2).line)
3665
3666 call feedkeys('0', 'xt')
3667 call assert_equal({}, popup_getpos(id1))
3668 call assert_equal({}, popup_getpos(id2))
3669
3670 call popup_clear()
3671 delfunc CloseFilter
3672endfunc
3673
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003674func Test_popupwin_double_width()
3675 CheckScreendump
3676
3677 let lines =<< trim END
3678 call setline(1, 'x你好世界你好世你好世界你好')
3679 call setline(2, '你好世界你好世你好世界你好')
3680 call setline(3, 'x你好世界你好世你好世界你好')
3681 call popup_create('你好,世界 - 你好,世界xxxxx', #{line: 1, col: 3, maxwidth: 14})
3682 END
3683 call writefile(lines, 'XtestPopupWide')
3684
3685 let buf = RunVimInTerminal('-S XtestPopupWide', #{rows: 10})
3686 call VerifyScreenDump(buf, 'Test_popupwin_doublewidth_1', {})
3687
3688 call StopVimInTerminal(buf)
3689 call delete('XtestPopupWide')
3690endfunc
3691
3692func Test_popupwin_sign()
3693 CheckScreendump
3694
3695 let lines =<< trim END
3696 call setline(1, range(10))
3697 call sign_define('Current', {
3698 \ 'text': '>>',
3699 \ 'texthl': 'WarningMsg',
3700 \ 'linehl': 'Error',
3701 \ })
3702 call sign_define('Other', {
3703 \ 'text': '#!',
3704 \ 'texthl': 'Error',
3705 \ 'linehl': 'Search',
3706 \ })
3707 let winid = popup_create(['hello', 'bright', 'world'], {
3708 \ 'minwidth': 20,
3709 \ })
3710 call setwinvar(winid, "&signcolumn", "yes")
3711 let winbufnr = winbufnr(winid)
3712
3713 " add sign to current buffer, shows
3714 call sign_place(1, 'Selected', 'Current', bufnr('%'), {'lnum': 1})
3715 " add sign to current buffer, does not show
3716 call sign_place(2, 'PopUpSelected', 'Other', bufnr('%'), {'lnum': 2})
3717
3718 " add sign to popup buffer, shows
3719 call sign_place(3, 'PopUpSelected', 'Other', winbufnr, {'lnum': 1})
3720 " add sign to popup buffer, does not show
3721 call sign_place(4, 'Selected', 'Current', winbufnr, {'lnum': 2})
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02003722
3723 func SetOptions()
3724 call setwinvar(g:winid, '&number', 1)
3725 call setwinvar(g:winid, '&foldcolumn', 2)
3726 call popup_settext(g:winid, 'a longer line to check the width')
3727 endfunc
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003728 END
3729 call writefile(lines, 'XtestPopupSign')
3730
3731 let buf = RunVimInTerminal('-S XtestPopupSign', #{rows: 10})
3732 call VerifyScreenDump(buf, 'Test_popupwin_sign_1', {})
3733
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02003734 " set more options to check the width is adjusted
3735 call term_sendkeys(buf, ":call SetOptions()\<CR>")
3736 call VerifyScreenDump(buf, 'Test_popupwin_sign_2', {})
3737
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003738 call StopVimInTerminal(buf)
3739 call delete('XtestPopupSign')
3740endfunc
3741
Bram Moolenaar99ebf222019-12-10 23:44:48 +01003742func Test_popupwin_bufnr()
3743 let popwin = popup_create(['blah'], #{})
3744 let popbuf = winbufnr(popwin)
3745 split asdfasdf
3746 let newbuf = bufnr()
3747 call assert_true(newbuf > popbuf, 'New buffer number is higher')
3748 call assert_equal(newbuf, bufnr('$'))
3749 call popup_clear()
3750 let popwin = popup_create(['blah'], #{})
3751 " reuses previous buffer number
3752 call assert_equal(popbuf, winbufnr(popwin))
3753 call assert_equal(newbuf, bufnr('$'))
3754
3755 call popup_clear()
3756 bwipe!
3757endfunc
3758
Bram Moolenaarec084d32020-02-28 22:44:47 +01003759func Test_popupwin_filter_input_multibyte()
3760 func MyPopupFilter(winid, c)
3761 let g:bytes = range(a:c->strlen())->map({i -> char2nr(a:c[i])})
3762 return 0
3763 endfunc
3764 let winid = popup_create('', #{mapping: 0, filter: 'MyPopupFilter'})
3765
3766 " UTF-8: E3 80 80, including K_SPECIAL(0x80)
3767 call feedkeys("\u3000", 'xt')
3768 call assert_equal([0xe3, 0x80, 0x80], g:bytes)
3769
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01003770 " UTF-8: E3 80 9B, including CSI(0x9B)
3771 call feedkeys("\u301b", 'xt')
3772 call assert_equal([0xe3, 0x80, 0x9b], g:bytes)
Bram Moolenaarec084d32020-02-28 22:44:47 +01003773
Bram Moolenaar20298ce2020-06-19 21:46:52 +02003774 if has('unix')
3775 " with modifyOtherKeys <M-S-a> does not include a modifier sequence
Bram Moolenaarb326edf2020-06-20 15:03:38 +02003776 if has('gui_running')
3777 call feedkeys("\x9b\xfc\x08A", 'Lx!')
3778 else
3779 call feedkeys("\<Esc>[27;4;65~", 'Lx!')
3780 endif
Bram Moolenaar20298ce2020-06-19 21:46:52 +02003781 call assert_equal([0xc3, 0x81], g:bytes)
3782 endif
3783
Bram Moolenaarec084d32020-02-28 22:44:47 +01003784 call popup_clear()
3785 delfunc MyPopupFilter
3786 unlet g:bytes
3787endfunc
3788
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003789func Test_popupwin_filter_close_ctrl_c()
3790 CheckScreendump
3791
3792 let lines =<< trim END
3793 vsplit
3794 set laststatus=2
3795 set statusline=%!Statusline()
3796
3797 function Statusline() abort
3798 return '%<%f %h%m%r%=%-14.(%l,%c%V%) %P'
3799 endfunction
3800
3801 call popup_create('test test test test...', {'filter': {-> 0}})
3802 END
3803 call writefile(lines, 'XtestPopupCtrlC')
3804
3805 let buf = RunVimInTerminal('-S XtestPopupCtrlC', #{rows: 10})
3806
3807 call term_sendkeys(buf, "\<C-C>")
3808 call VerifyScreenDump(buf, 'Test_popupwin_ctrl_c', {})
3809
3810 call StopVimInTerminal(buf)
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003811 call delete('XtestPopupCtrlC')
3812endfunc
3813
3814func Test_popupwin_filter_close_wrong_name()
3815 CheckScreendump
3816
3817 let lines =<< trim END
3818 call popup_create('one two three...', {'filter': 'NoSuchFunc'})
3819 END
3820 call writefile(lines, 'XtestPopupWrongName')
3821
3822 let buf = RunVimInTerminal('-S XtestPopupWrongName', #{rows: 10})
3823
3824 call term_sendkeys(buf, "j")
3825 call VerifyScreenDump(buf, 'Test_popupwin_wrong_name', {})
3826
3827 call StopVimInTerminal(buf)
3828 call delete('XtestPopupWrongName')
3829endfunc
3830
3831func Test_popupwin_filter_close_three_errors()
3832 CheckScreendump
3833
3834 let lines =<< trim END
3835 set cmdheight=2
3836 call popup_create('one two three...', {'filter': 'filter'})
3837 END
3838 call writefile(lines, 'XtestPopupThreeErrors')
3839
3840 let buf = RunVimInTerminal('-S XtestPopupThreeErrors', #{rows: 10})
3841
3842 call term_sendkeys(buf, "jj")
3843 call VerifyScreenDump(buf, 'Test_popupwin_three_errors_1', {})
3844 call term_sendkeys(buf, "j")
3845 call VerifyScreenDump(buf, 'Test_popupwin_three_errors_2', {})
3846
3847 call StopVimInTerminal(buf)
3848 call delete('XtestPopupThreeErrors')
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003849endfunc
3850
Bram Moolenaar927495b2020-11-06 17:58:35 +01003851func Test_popupwin_latin1_encoding()
3852 CheckScreendump
3853 CheckUnix
3854
3855 " When 'encoding' is a single-byte encoding a terminal window will mess up
3856 " the display. Check that showing a popup on top of that doesn't crash.
3857 let lines =<< trim END
3858 set encoding=latin1
3859 terminal cat Xmultibyte
3860 call popup_create(['one', 'two', 'three', 'four'], #{line: 1, col: 10})
Bram Moolenaar4c5bdb92020-12-17 17:45:59 +01003861 redraw
Bram Moolenaarb125b532020-12-17 21:56:09 +01003862 " wait for "cat" to finish
3863 while execute('ls!') !~ 'finished'
3864 sleep 10m
3865 endwhile
Bram Moolenaar4c5bdb92020-12-17 17:45:59 +01003866 echo "Done"
Bram Moolenaar927495b2020-11-06 17:58:35 +01003867 END
3868 call writefile(lines, 'XtestPopupLatin')
3869 call writefile([repeat("\u3042 ", 120)], 'Xmultibyte')
3870
3871 let buf = RunVimInTerminal('-S XtestPopupLatin', #{rows: 10})
Bram Moolenaar4c5bdb92020-12-17 17:45:59 +01003872 call WaitForAssert({-> assert_match('Done', term_getline(buf, 10))})
Bram Moolenaar927495b2020-11-06 17:58:35 +01003873
3874 call term_sendkeys(buf, ":q\<CR>")
3875 call StopVimInTerminal(buf)
3876 call delete('XtestPopupLatin')
3877 call delete('Xmultibyte')
3878endfunc
3879
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003880func Test_popupwin_atcursor_far_right()
3881 new
3882
3883 " this was getting stuck
3884 set signcolumn=yes
3885 call setline(1, repeat('=', &columns))
3886 normal! ggg$
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003887 let winid = popup_atcursor(repeat('x', 500), #{moved: 'any', border: []})
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003888
Bram Moolenaar6d585f42020-07-26 22:20:54 +02003889 " 'signcolumn' was getting reset
3890 call setwinvar(winid, '&signcolumn', 'yes')
3891 call popup_setoptions(winid, {'zindex': 1000})
3892 call assert_equal('yes', getwinvar(winid, '&signcolumn'))
3893
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003894 call popup_close(winid)
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003895 bwipe!
3896 set signcolumn&
3897endfunc
3898
Bram Moolenaar0f1563f2020-03-20 21:15:51 +01003899func Test_popupwin_splitmove()
3900 vsplit
3901 let win2 = win_getid()
3902 let popup_winid = popup_dialog('hello', {})
3903 call assert_fails('call win_splitmove(popup_winid, win2)', 'E957:')
3904 call assert_fails('call win_splitmove(win2, popup_winid)', 'E957:')
3905
3906 call popup_clear()
3907 bwipe
3908endfunc
3909
Bram Moolenaarcbcd9cb2020-11-07 16:58:59 +01003910func Test_popupwin_exiting_terminal()
3911 CheckFeature terminal
3912
3913 " Tests that when creating a popup right after closing a terminal window does
3914 " not make the popup the current window.
3915 let winid = win_getid()
3916 try
3917 augroup Test_popupwin_exiting_terminal
3918 autocmd!
3919 autocmd WinEnter * :call popup_create('test', {})
3920 augroup END
3921 let bnr = term_start(&shell, #{term_finish: 'close'})
3922 call term_sendkeys(bnr, "exit\r\n")
3923 call WaitForAssert({-> assert_equal(winid, win_getid())})
3924 finally
3925 call popup_clear(1)
3926 augroup Test_popupwin_exiting_terminal
3927 autocmd!
3928 augroup END
3929 endtry
3930endfunc
Bram Moolenaar0f1563f2020-03-20 21:15:51 +01003931
Bram Moolenaar014f6982021-01-04 13:18:30 +01003932func Test_popup_filter_menu()
3933 let colors = ['red', 'green', 'blue']
3934 call popup_menu(colors, #{callback: {_, result -> assert_equal('green', colors[result - 1])}})
3935 call feedkeys("\<c-n>\<c-n>\<c-p>\<cr>", 'xt')
3936endfunc
3937
Bram Moolenaar65026432021-02-06 14:22:32 +01003938func Test_popup_getoptions_other_tab()
3939 new
3940 call setline(1, 'some text')
3941 call prop_type_add('textprop', {})
3942 call prop_add(1, 1, #{type: 'textprop', length: 1})
3943 let id = popup_create('TEST', #{textprop: 'textprop', highlight: 'ErrorMsg', tabpage: 1})
3944 tab sp
3945 call assert_equal(['textprop', 'textpropid', 'textpropwin'], popup_getoptions(id)->keys()->filter({_, v -> v =~ 'textprop'}))
3946
3947 tabclose
Bram Moolenaarefa19232021-02-06 14:59:27 +01003948 call popup_close(id)
Bram Moolenaar65026432021-02-06 14:22:32 +01003949 bwipe!
3950 call prop_type_delete('textprop')
3951endfunc
3952
Bram Moolenaarefa19232021-02-06 14:59:27 +01003953
3954func Test_popup_setoptions_other_tab()
3955 new Xfile
3956 let winid = win_getid()
3957 call setline(1, 'some text')
3958 call prop_type_add('textprop', {})
3959 call prop_add(1, 1, #{type: 'textprop', length: 1})
3960 let id = popup_create('TEST', #{textprop: 'textprop'})
3961 tab sp
3962 call popup_setoptions(id, #{textprop: 'textprop', textpropwin: winid})
3963 call assert_equal(winid, popup_getoptions(id).textpropwin)
3964
3965 tabclose
3966 call popup_close(id)
3967 bwipe! Xfile
3968 call prop_type_delete('textprop')
3969endfunc
3970
Bram Moolenaar82db31c2021-02-10 14:56:11 +01003971func Test_popup_prop_not_visible()
3972 CheckScreendump
3973
3974 let lines =<< trim END
3975 vim9script
3976 set nowrap stal=2
3977 rightbelow :31vnew
3978 setline(1, ['', 'some text', '', 'other text'])
3979 prop_type_add('someprop', {})
3980 prop_add(2, 9, {type: 'someprop', length: 5})
3981 popup_create('attached to "some"', {
3982 textprop: 'someprop',
3983 highlight: 'ErrorMsg',
3984 line: -1,
3985 wrap: false,
3986 fixed: true,
3987 })
3988 prop_type_add('otherprop', {})
3989 prop_add(4, 10, {type: 'otherprop', length: 5})
3990 popup_create('attached to "other"', {
3991 textprop: 'otherprop',
3992 highlight: 'ErrorMsg',
3993 line: -1,
3994 wrap: false,
3995 fixed: false,
3996 })
3997 END
3998 call writefile(lines, 'XtestPropNotVisble')
3999 let buf = RunVimInTerminal('-S XtestPropNotVisble', #{rows: 10})
4000 call VerifyScreenDump(buf, 'Test_popup_prop_not_visible_01', {})
4001
4002 call term_sendkeys(buf, ":vert resize -14\<CR>")
4003 call VerifyScreenDump(buf, 'Test_popup_prop_not_visible_02', {})
4004
4005 call term_sendkeys(buf, ":vert resize -8\<CR>")
4006 call VerifyScreenDump(buf, 'Test_popup_prop_not_visible_03', {})
4007
4008 " clean up
4009 call StopVimInTerminal(buf)
4010 call delete('XtestPropNotVisble')
4011endfunction
4012
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02004013func Test_bufdel_skips_popupwin_buffer()
4014 let id = popup_create("Some text", {})
4015 %bd
4016 call popup_close(id)
4017endfunc
4018
Bram Moolenaar82db31c2021-02-10 14:56:11 +01004019
Bram Moolenaar331bafd2019-07-20 17:46:05 +02004020" vim: shiftwidth=2 sts=2