File: hover.test.vim

package info (click to toggle)
vim-youcompleteme 0%2B20230109%2Bgit7620d87%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,404 kB
  • sloc: python: 10,569; sh: 203; cpp: 121; makefile: 24; f90: 5; xml: 1
file content (440 lines) | stat: -rw-r--r-- 14,108 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
function! s:WaitForCommandRequestComplete()
  return youcompleteme#test#commands#WaitForCommandRequestComplete()
endfunction

function! s:CheckNoCommandRequest()
  return youcompleteme#test#commands#CheckNoCommandRequest()
endfunction

function! s:CheckPopupVisible( row, col, text, syntax )
  " Takes a buffer position, converts it to a screen position and checks the
  " popup found at that location
  redraw
  let loc = screenpos( win_getid(), a:row, a:col )
  return s:CheckPopupVisibleScreenPos( loc, a:text, a:syntax )
endfunction

function! s:CheckPopupVisibleScreenPos( loc, text, syntax )
  " Takes a position dict like the one returned by screenpos() and verifies it
  " has 'text' (a list of lines) and 'syntax' the &syntax setting
  " popup found at that location
  redraw
  call s:WaitForCommandRequestComplete()
  call WaitForAssert( { ->
        \   assert_notequal( 0,
        \                    popup_locate( a:loc.row, a:loc.col ),
        \                    'Locate popup at ('
        \                    . a:loc.row
        \                    . ','
        \                    . a:loc.col
        \                    . ')' )
       \ } )
  let popup = popup_locate( a:loc.row, a:loc.col )
  if a:text isnot v:none
    let popcontent = getbufline( winbufnr( popup ), 1, '$' )
    call assert_equal( len(a:text), len(popcontent) )
    let i = 0
    while i < len(a:text)
      call assert_match( a:text[i], popcontent[i] )
      let i += 1
    endwhile
  endif
  call assert_equal( a:syntax, getbufvar( winbufnr( popup ), '&syntax' ) )
endfunction

function! s:CheckPopupNotVisible( row, col, with_request=v:true )
  " Takes a buffer position and ensures there is no popup visible at that
  " position. Like CheckPopupVisible, the position must be valid (i.e. there
  " must be buffer text at that position). Otherwise, you need to pass the
  " _screen_ position to CheckPopupNotVisibleScreenPos
  redraw
  let loc = screenpos( win_getid(), a:row, a:col )
  return s:CheckPopupNotVisibleScreenPos( loc, a:with_request )
endfunction

function! s:CheckPopupNotVisibleScreenPos( loc, with_request=v:true )
  " Takes a position dict like the one returned by screenpos() and verifies it
  " does not have a popup drawn on it.
  redraw
  if a:with_request
    call s:WaitForCommandRequestComplete()
  else
    call s:CheckNoCommandRequest()
  endif
  call WaitForAssert( { ->
        \   assert_equal( 0,
        \                 popup_locate( a:loc.row, a:loc.col ) )
        \ } )
endfunction

let s:python_oneline = {
      \ 'GetDoc': [ '^Test_OneLine()$', '^$', '^This is the one line output.$' ],
      \ 'GetType': [ '^def Test_OneLine()$' ],
      \ }
let s:cpp_lifetime = {
      \ 'GetDoc': [ '^field lifetime$',
      \             '^$',
      \             '^Type: char$',
      \             '^Offset: \d* bytes$',
      \             '^Size: 1 byte (+\d* padding)$',
      \             '^nobody will live > 128 years$',
      \             '^$',
      \             '^// In PointInTime$',
      \             '^public: char lifetime$' ],
      \ 'GetType': [ '^public: char lifetime; // In PointInTime$' ],
      \ }

function! SetUp()
  let g:ycm_use_clangd = 1
  let g:ycm_keep_logfiles = 1
  let g:ycm_log_level = 'DEBUG'
  set signcolumn=no
  nmap <leader>D <Plug>(YCMHover)
  call youcompleteme#test#setup#SetUp()
endfunction

function! TearDown()
  let g:ycm_auto_hover='CursorHold'

  call assert_equal( -1, youcompleteme#Test_GetPollers().command.id )
endfunction

function! Test_Hover_Uses_GetDoc()
  call youcompleteme#test#setup#OpenFile( '/test/testdata/python/doc.py', {} )

  call assert_equal( 'python', &syntax )

  " no doc
  call setpos( '.', [ 0, 1, 1 ] )
  doautocmd CursorHold
  call assert_equal( { 'command': 'GetDoc', 'syntax': '' }, b:ycm_hover )

  call s:CheckPopupNotVisible( 2, 1 )
  call s:CheckPopupNotVisible( 2, 2 )

  " some doc - autocommand
  call setpos( '.', [ 0, 12, 3 ] )
  doautocmd CursorHold
  call s:CheckPopupVisible( 11, 4, s:python_oneline.GetDoc, '' )
  call popup_clear()

  " some doc - mapping
  call setpos( '.', [ 0, 12, 3 ] )
  normal \D
  call s:CheckPopupVisible( 11, 4, s:python_oneline.GetDoc, '' )
  call popup_clear()
endfunction

function! Test_Hover_Uses_GetHover()
  call youcompleteme#test#setup#OpenFile( '/test/testdata/python/doc.py', {} )
  py3 <<EOPYTHON
from unittest import mock
with mock.patch.object( ycm_state,
                        'GetDefinedSubcommands',
                        return_value = [ 'GetHover' ] ):
  vim.command( 'doautocmd CursorHold' )
EOPYTHON

  call assert_equal( { 'command': 'GetHover', 'syntax': 'markdown' },
                   \ b:ycm_hover )

  " Only the generic LSP completer supports the GetHover response, so i guess we
  " test the error condition here...

  " Python desn't support GetHover
  call setpos( '.', [ 0, 12, 3 ] )
  normal \D
  call s:CheckPopupNotVisible( 11, 4 )
  call popup_clear()

endfunction

function! Test_Hover_Uses_None()
  call youcompleteme#test#setup#OpenFile( '/test/testdata/python/doc.py', {} )
  py3 <<EOPYTHON
from unittest import mock
with mock.patch.object( ycm_state, 'GetDefinedSubcommands', return_value = [] ):
  vim.command( 'doautocmd CursorHold' )
EOPYTHON

  call assert_equal( {}, b:ycm_hover )

  call setpos( '.', [ 0, 12, 3 ] )
  normal \D
  call s:CheckPopupNotVisible( 11, 4, v:false )

  call popup_clear()
endfunction

function! Test_Hover_Uses_GetType()
  call youcompleteme#test#setup#OpenFile( '/test/testdata/python/doc.py', {} )

  py3 <<EOPYTHON
from unittest import mock
with mock.patch.object( ycm_state,
                        'GetDefinedSubcommands',
                        return_value = [ 'GetType' ] ):
  vim.command( 'doautocmd CursorHold' )
EOPYTHON

  call assert_equal( { 'command': 'GetType', 'syntax': 'python' }, b:ycm_hover )

  call s:CheckPopupNotVisible( 2, 1, v:none )
  call s:CheckPopupNotVisible( 2, 2, v:none )

  " some doc - autocommand
  call setpos( '.', [ 0, 12, 3 ] )
  doautocmd CursorHold
  call s:CheckPopupVisible( 11, 4, s:python_oneline.GetType, 'python' )
  call popup_clear()

  " some doc - mapping
  call setpos( '.', [ 0, 12, 3 ] )
  normal \D
  call s:CheckPopupVisible( 11, 4, s:python_oneline.GetType, 'python' )

  " hide it again
  normal \D
  call s:CheckPopupNotVisible( 11, 4 )

  " show it again
  normal \D
  call s:CheckPopupVisible( 11, 4, s:python_oneline.GetType, 'python' )
  call popup_clear()

endfunction

function! Test_Hover_NonNative()
  call youcompleteme#test#setup#OpenFile( '_not_a_file', { 'native_ft': 0 } )
  setfiletype NoASupportedFileType
  let messages_before = execute( 'messages' )
  doautocmd CursorHold
  call s:CheckNoCommandRequest()
  call assert_false( exists( 'b:ycm_hover' ) )
  call assert_equal( messages_before, execute( 'messages' ) )

  normal \D
  call s:CheckNoCommandRequest()
  call assert_false( exists( 'b:ycm_hover' ) )
  call assert_equal( messages_before, execute( 'messages' ) )

  call popup_clear()
endfunction

function SetUp_Test_Hover_Disabled_NonNative()
  let g:ycm_auto_hover = ''
endfunction

function! Test_Hover_Disabled_NonNative()
  call youcompleteme#test#setup#OpenFile( '_not_a_file', { 'native_ft': 0 } )
  setfiletype NoASupportedFileType
  let messages_before = execute( 'messages' )
  silent! doautocmd CursorHold
  call s:CheckNoCommandRequest()
  call assert_false( exists( 'b:ycm_hover' ) )
  call assert_equal( messages_before, execute( 'messages' ) )

  call popup_clear()
endfunction

function! SetUp_Test_AutoHover_Disabled()
  let g:ycm_auto_hover = ''
endfunction

function! Test_AutoHover_Disabled()
  call youcompleteme#test#setup#OpenFile( '/test/testdata/python/doc.py', {} )

  let messages_before = execute( 'messages' )

  call assert_false( exists( 'b:ycm_hover' ) )

  call setpos( '.', [ 0, 12, 3 ] )
  silent! doautocmd CursorHold
  call s:CheckPopupNotVisible( 11, 4, v:false )
  call assert_equal( messages_before, execute( 'messages' ) )

  " Manual hover is still supported
  normal \D
  call assert_true( exists( 'b:ycm_hover' ) )
  call s:CheckPopupVisible( 11, 4, s:python_oneline.GetDoc, '' )
  call assert_equal( messages_before, execute( 'messages' ) )

  " Manual close hover is still supported
  normal \D
  call s:CheckPopupNotVisible( 11, 4, v:false )
  call assert_equal( messages_before, execute( 'messages' ) )

  call popup_clear()
endfunction

function! Test_Hover_MoveCursor()
  call youcompleteme#test#setup#OpenFile( '/test/testdata/python/doc.py', {} )
  " needed so that the feedkeys calls actually trigger vim to notice the cursor
  " moving. We also need to enter/exit insert mode as Vim only checks for these
  " cursor moved events in very specific times. In particular, _not_ while
  " running a script (like we are here), but it _does_ on enter/exit insert
  " mode.
  call test_override( 'char_avail', 1 )

  call setpos( '.', [ 0, 12, 3 ] )
  doautocmd CursorHold
  call s:CheckPopupVisible( 11, 3, s:python_oneline.GetDoc, '' )

  call feedkeys( "li\<Esc>", 'xt' )
  call s:CheckPopupVisible( 11, 3, s:python_oneline.GetDoc, '' )

  " letters within word
  call feedkeys( "4li\<Esc>", 'xt' )
  call s:CheckPopupVisible( 11, 3, s:python_oneline.GetDoc, '' )

  " word
  call feedkeys( "wi\<Esc>", 'xt' )
  call s:CheckPopupNotVisible( 11, 3 )

  call feedkeys( "b\\D", 'xt' )
  call s:CheckPopupVisible( 11, 3, s:python_oneline.GetDoc, '' )

  call test_override( 'ALL', 0 )

  call popup_clear()
endfunction

function! Test_Hover_Dismiss()
  call youcompleteme#test#setup#OpenFile( '/test/testdata/python/doc.py', {} )
  " needed so that the feedkeys calls actually trigger vim to notice the cursor
  " moving. We also need to enter/exit insert mode as Vim only checks for these
  " cursor moved events in very specific times. In particular, _not_ while
  " running a script (like we are here), but it _does_ on enter/exit insert
  " mode.
  call test_override( 'char_avail', 1 )

  call setpos( '.', [ 0, 12, 3 ] )
  doautocmd CursorHold
  call s:CheckPopupVisible( 11, 3, s:python_oneline.GetDoc, '' )

  " Dismiss
  normal \D
  call s:CheckPopupNotVisible( 11, 3, v:false )

  " Make sure it doesn't come back
  silent! doautocmd CursorHold
  call s:CheckPopupNotVisible( 11, 3, v:false )

  " Move the cursor (again this is tricky). I couldn't find any tests in vim's
  " own code that trigger CursorMoved, so we just cheat. (for the record, just
  " moving the cursor in the middle of this script does not trigger CursorMoved)
  doautocmd CursorMoved
  doautocmd CursorHold
  call s:CheckPopupVisible( 11, 3, s:python_oneline.GetDoc, '' )

  call popup_clear()
endfunction

function! SetUp_Test_Hover_Custom_Syntax()
  augroup MyYCMCustom
    autocmd!
    autocmd FileType cpp let b:ycm_hover = {
      \ 'command': 'GetDoc',
      \ 'syntax': 'cpp',
      \ }
  augroup END
endfunction

function! Test_Hover_Custom_Syntax()
  call youcompleteme#test#setup#OpenFile( '/test/testdata/cpp/completion.cc',
                                        \ {} )
  call assert_equal( 'cpp', &filetype )
  call assert_equal( { 'command': 'GetDoc', 'syntax': 'cpp' }, b:ycm_hover )

  call setpos( '.', [ 0, 6, 8 ] )
  doautocmd CursorHold
  call assert_equal( { 'command': 'GetDoc', 'syntax': 'cpp' }, b:ycm_hover )
  call s:CheckPopupVisibleScreenPos( { 'row': 7, 'col': 9 },
                                   \ s:cpp_lifetime.GetDoc,
                                   \ 'cpp' )

  normal \D
  call s:CheckPopupNotVisibleScreenPos( { 'row': 7, 'col': 9 }, v:false )

  call popup_clear()
endfunction

function! TearDown_Test_Hover_Custom_Syntax()
  silent! au! MyYCMCustom
endfunction

function! SetUp_Test_Hover_Custom_Command()
  augroup MyYCMCustom
    autocmd!
    autocmd FileType cpp let b:ycm_hover = {
      \ 'command': 'GetType',
      \ 'syntax': 'cpp',
      \ }
  augroup END
endfunction

function! Test_Hover_Custom_Command()
  call youcompleteme#test#setup#OpenFile( '/test/testdata/cpp/completion.cc',
                                        \ {} )
  call assert_equal( 'cpp', &filetype )
  call assert_equal( { 'command': 'GetType', 'syntax': 'cpp' }, b:ycm_hover )

  call setpos( '.', [ 0, 6, 8 ] )
  doautocmd CursorHold
  call assert_equal( { 'command': 'GetType', 'syntax': 'cpp' }, b:ycm_hover )

  call s:CheckPopupVisible( 5, 9, s:cpp_lifetime.GetType, 'cpp' )

  call popup_clear()
endfunction

function! TearDown_Test_Hover_Custom_Command()
  silent! au! MyYCMCustom
endfunction

function! Test_Long_Single_Line()
  call youcompleteme#test#setup#OpenFile( '/test/testdata/python/doc.py', {} )
  call cursor( [ 37, 3 ] )
  normal \D

  " The popup should cover at least the whole of the line above, and not the
  " current line
  call s:CheckPopupVisible( 36, 1, v:none, '' )
  call s:CheckPopupVisible( 36, &columns, v:none, '' )

  call s:CheckPopupNotVisible( 37, 1, v:false )
  call s:CheckPopupNotVisible( 37, &columns, v:false )

  " Also wrap is ON so it should cover at least 2 lines + 2 for the header/empty
  " line
  call s:CheckPopupVisible( 35, 1, v:none, '' )
  call s:CheckPopupVisible( 35, &columns, v:none, '' )
  call s:CheckPopupVisible( 33, 1, v:none, '' )
  call s:CheckPopupVisible( 33, &columns, v:none, '' )

  call popup_clear()
endfunction

function! Test_Long_Wrapped()
  call youcompleteme#test#setup#OpenFile( '/test/testdata/python/doc.py', {} )
  call cursor( [ 38, 22 ] )
  normal \D

  " The popup should cover at least the whole of the line above, and not the
  " current line. In this case, it's because the popup was shifted.
  call s:CheckPopupVisible( 37, 1, v:none, '' )
  call s:CheckPopupVisible( 37, &columns, v:none, '' )

  call s:CheckPopupNotVisible( 38, 1, v:false )
  call s:CheckPopupNotVisible( 38, &columns, v:false )

  " Also, wrap is off, so it should be _exactly_ 9 lines + 2 for the signature
  " and the empty line
  call s:CheckPopupVisible( 27, 1, v:none, '' )
  call s:CheckPopupVisible( 27, &columns, v:none, '' )

  call s:CheckPopupNotVisible( 26, 1, v:false )
  call s:CheckPopupNotVisible( 26, &columns, v:false )

  call popup_clear()
endfunction