summaryrefslogtreecommitdiff
path: root/test/completion/test_completion_events.vader
blob: 87bd10ade1d9e2d92b1261c3918bf4ad17093e70 (plain)
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
441
442
443
444
445
446
447
Before:
  Save g:ale_completion_enabled
  Save g:ale_completion_delay
  Save g:ale_completion_max_suggestions
  Save &l:omnifunc
  Save &l:completeopt

  unlet! b:ale_completion_enabled
  let g:ale_completion_enabled = 1
  let g:get_completions_called = 0
  let g:feedkeys_calls = []
  let g:fake_mode = 'i'

  let &l:completeopt = 'menu,menuone,preview,noselect,noinsert'

  runtime autoload/ale/util.vim

  function! ale#util#FeedKeys(string) abort
    call add(g:feedkeys_calls, [a:string])
  endfunction

  " Pretend we're in insert mode for most tests.
  function! ale#util#Mode(...) abort
    return g:fake_mode
  endfunction

  function! CheckCompletionCalled(expect_success) abort
    let g:get_completions_called = 0

    " We just want to check if the function is called.
    function! ale#completion#GetCompletions(source)
      let g:get_completions_called = 1
    endfunction

    let g:ale_completion_delay = 0

    " Run this check a few times, as it can fail randomly.
    for l:i in range(has('nvim-0.3') || has('win32') ? 5 : 1)
      call ale#completion#Queue()
      sleep 1m

      if g:get_completions_called is a:expect_success
        break
      endif
    endfor

    AssertEqual a:expect_success, g:get_completions_called
  endfunction

  let g:handle_code_action_called = 0
  function! MockHandleCodeAction() abort
    " delfunction! ale#code_action#HandleCodeAction
    function! ale#code_action#HandleCodeAction(action, should_save) abort
      AssertEqual v:false, a:should_save
      let g:handle_code_action_called += 1
    endfunction
  endfunction

After:
  Restore

  unlet! b:ale_completion_enabled
  unlet! g:output
  unlet! g:fake_mode
  unlet! g:get_completions_called
  unlet! g:handle_code_action_called
  unlet! b:ale_old_omnifunc
  unlet! b:ale_old_completeopt
  unlet! b:ale_completion_info
  unlet! b:ale_completion_result
  unlet! b:ale_complete_done_time

  delfunction CheckCompletionCalled
  delfunction ale#code_action#HandleCodeAction
  delfunction MockHandleCodeAction

  if exists('*CompleteCallback')
    delfunction CompleteCallback
  endif

  " Stop any timers we left behind.
  " This stops the tests from failing randomly.
  call ale#completion#StopTimer()

  " Reset the function. The runtime command below should fix this, but doesn't
  " seem to fix it.
  function! ale#util#Mode(...) abort
    return call('mode', a:000)
  endfunction

  runtime autoload/ale/completion.vim
  runtime autoload/ale/code_action.vim
  runtime autoload/ale/util.vim

Execute(ale#completion#GetCompletions should be called when the cursor position stays the same):
  call CheckCompletionCalled(1)

Execute(ale#completion#GetCompletions should not be called if the global setting is disabled):
  let g:ale_completion_enabled = 0
  call CheckCompletionCalled(0)

Execute(ale#completion#GetCompletions should not be called if the buffer setting is disabled):
  let b:ale_completion_enabled = 0
  call CheckCompletionCalled(0)

Given typescript():
  let abc = y.
  let foo = ab
  let foo = (ab)

Execute(ale#completion#GetCompletions should not be called when the cursor position changes):
  call setpos('.', [bufnr(''), 1, 2, 0])

  " We just want to check if the function is called.
  function! ale#completion#GetCompletions(source)
    let g:get_completions_called = 1
  endfunction

  let g:ale_completion_delay = 0
  call ale#completion#Queue()

  " Change the cursor position before the callback is triggered.
  call setpos('.', [bufnr(''), 2, 2, 0])

  sleep 1m

  Assert !g:get_completions_called

Execute(ale#completion#GetCompletions should not be called if you switch to normal mode):
  let &l:completeopt = 'menu,preview'
  let g:fake_mode = 'n'

  " We just want to check if the function is called.
  function! ale#completion#GetCompletions(source)
    let g:get_completions_called = 1
  endfunction

  let g:ale_completion_delay = 0
  call ale#completion#Queue()

  sleep 1m

  Assert !g:get_completions_called

Execute(Completion should not be done shortly after the CompleteDone function):
  call CheckCompletionCalled(1)
  call ale#completion#Done()
  call CheckCompletionCalled(0)

Execute(ale#completion#Show() should remember the omnifunc setting and replace it):
  let &l:omnifunc = 'FooBar'

  let b:ale_completion_info = {'source': 'ale-automatic'}
  call ale#completion#Show([{'word': 'x', 'kind': 'v', 'icase': 1}])

  AssertEqual 'FooBar', b:ale_old_omnifunc
  AssertEqual 'ale#completion#AutomaticOmniFunc', &l:omnifunc

  AssertEqual [], g:feedkeys_calls
  sleep 1ms
  AssertEqual [["\<Plug>(ale_show_completion_menu)"]], g:feedkeys_calls

Execute(ale#completion#Show() should remember the completeopt setting and replace it):
  let &l:completeopt = 'menu'

  let b:ale_completion_info = {'source': 'ale-automatic'}
  call ale#completion#Show([{'word': 'x', 'kind': 'v', 'icase': 1}])

  AssertEqual 'menu', b:ale_old_completeopt
  AssertEqual 'menu,menuone,noselect,noinsert', &l:completeopt

  AssertEqual [], g:feedkeys_calls
  sleep 1ms
  AssertEqual [["\<Plug>(ale_show_completion_menu)"]], g:feedkeys_calls

Execute(ale#completion#Show() should set the preview option if it's set):
  let &l:completeopt = 'menu,preview'

  let b:ale_completion_info = {'source': 'ale-automatic'}
  call ale#completion#Show([{'word': 'x', 'kind': 'v', 'icase': 1}])

  AssertEqual 'menu,preview', b:ale_old_completeopt
  AssertEqual 'menu,menuone,preview,noselect,noinsert', &l:completeopt

  AssertEqual [], g:feedkeys_calls
  sleep 1ms
  AssertEqual [["\<Plug>(ale_show_completion_menu)"]], g:feedkeys_calls

Execute(ale#completion#Show() should not replace the completeopt setting for manual completion):
  let b:ale_completion_info = {'source': 'ale-manual'}

  let &l:completeopt = 'menu,preview'

  call ale#completion#Show([{'word': 'x', 'kind': 'v', 'icase': 1}])

  Assert !exists('b:ale_old_completeopt')

  AssertEqual [], g:feedkeys_calls
  sleep 1ms
  AssertEqual [["\<Plug>(ale_show_completion_menu)"]], g:feedkeys_calls

Execute(ale#completion#AutomaticOmniFunc() should also remember the completeopt setting and replace it):
  let &l:completeopt = 'menu'

  let b:ale_completion_info = {'source': 'ale-automatic'}
  call ale#completion#AutomaticOmniFunc(0, '')

  AssertEqual 'menu', b:ale_old_completeopt
  AssertEqual 'menu,menuone,noselect,noinsert', &l:completeopt

Execute(ale#completion#AutomaticOmniFunc() should set the preview option if it's set):
  let &l:completeopt = 'menu,preview'

  let b:ale_completion_info = {'source': 'ale-automatic'}
  call ale#completion#AutomaticOmniFunc(0, '')

  AssertEqual 'menu,preview', b:ale_old_completeopt
  AssertEqual 'menu,menuone,preview,noselect,noinsert', &l:completeopt

Execute(ale#completion#Show() should make the correct feedkeys() call for automatic completion):
  let b:ale_completion_info = {'source': 'ale-automatic'}
  call ale#completion#Show([{'word': 'x', 'kind': 'v', 'icase': 1}])

  AssertEqual [], g:feedkeys_calls
  sleep 1ms
  AssertEqual [["\<Plug>(ale_show_completion_menu)"]], g:feedkeys_calls

Execute(ale#completion#Show() should make the correct feedkeys() call for manual completion):
  let b:ale_completion_info = {'source': 'ale-automatic'}
  call ale#completion#Show([{'word': 'x', 'kind': 'v', 'icase': 1}])

  AssertEqual [], g:feedkeys_calls
  sleep 1ms
  AssertEqual [["\<Plug>(ale_show_completion_menu)"]], g:feedkeys_calls

Execute(ale#completion#Show() should not call feedkeys() for other sources):
  let b:ale_completion_info = {'source': 'other-source'}
  call ale#completion#Show([{'word': 'x', 'kind': 'v', 'icase': 1}])

  sleep 1ms
  AssertEqual [], g:feedkeys_calls

Execute(ale#completion#Show() shouldn't do anything if you switch back to normal mode):
  let &l:completeopt = 'menu,preview'
  let g:fake_mode = 'n'

  call ale#completion#Show([{'word': 'x', 'kind': 'v', 'icase': 1}])

  AssertEqual 'menu,preview', &l:completeopt
  Assert !exists('b:ale_old_omnifunc')
  Assert !exists('b:ale_old_completeopt')
  Assert !exists('b:ale_completion_result')
  AssertEqual [], g:feedkeys_calls

Execute(ale#completion#Show() should save the result it is given):
  call ale#completion#Show([])

  AssertEqual [], b:ale_completion_result

  call ale#completion#Show([{'word': 'x', 'kind': 'v', 'icase': 1}])

  AssertEqual [{'word': 'x', 'kind': 'v', 'icase': 1}], b:ale_completion_result

Execute(ale#completion#Done() should restore old omnifunc values):
  let b:ale_old_omnifunc = 'FooBar'

  call ale#completion#Done()

  " We reset the old omnifunc setting and remove the buffer variable.
  AssertEqual 'FooBar', &l:omnifunc
  Assert !has_key(b:, 'ale_old_omnifunc')

Execute(ale#completion#Done() should restore the old completeopt setting):
  let b:ale_old_completeopt = 'menu'

  call ale#completion#Done()

  AssertEqual 'menu', &l:completeopt
  Assert !has_key(b:, 'ale_old_completeopt')

Execute(ale#completion#Done() should leave settings alone when none were remembered):
  let &l:omnifunc = 'BazBoz'
  let &l:completeopt = 'menu'

  call ale#completion#Done()

  AssertEqual 'BazBoz', &l:omnifunc
  AssertEqual 'menu', &l:completeopt

Execute(The completion request_id should be reset when queuing again):
  let b:ale_completion_info = {'request_id': 123}

  let g:ale_completion_delay = 0
  call ale#completion#Queue()
  sleep 1m

  AssertEqual 0, b:ale_completion_info.request_id

Execute(b:ale_completion_info should be set up correctly when requesting completions automatically):
  let b:ale_completion_result = []
  call setpos('.', [bufnr(''), 3, 14, 0])
  call ale#completion#GetCompletions('ale-automatic')

  AssertEqual
  \ {
  \   'request_id': 0,
  \   'conn_id': 0,
  \   'column': 14,
  \   'line_length': 14,
  \   'line': 3,
  \   'prefix': 'ab',
  \   'source': 'ale-automatic',
  \ },
  \ b:ale_completion_info
  Assert !exists('b:ale_completion_result')

Execute(b:ale_completion_info should be set up correctly when requesting completions manually):
  let b:ale_completion_result = []
  call setpos('.', [bufnr(''), 3, 14, 0])
  ALEComplete

  AssertEqual
  \ {
  \   'request_id': 0,
  \   'conn_id': 0,
  \   'column': 14,
  \   'line_length': 14,
  \   'line': 3,
  \   'prefix': 'ab',
  \   'source': 'ale-manual',
  \ },
  \ b:ale_completion_info
  Assert !exists('b:ale_completion_result')

Execute(b:ale_completion_info should be set up correctly for other sources):
  let b:ale_completion_result = []
  call setpos('.', [bufnr(''), 3, 14, 0])
  call ale#completion#GetCompletions('ale-callback')

  AssertEqual
  \ {
  \   'request_id': 0,
  \   'conn_id': 0,
  \   'column': 14,
  \   'line_length': 14,
  \   'line': 3,
  \   'prefix': 'ab',
  \   'source': 'ale-callback',
  \ },
  \ b:ale_completion_info
  Assert !exists('b:ale_completion_result')

Execute(b:ale_completion_info should be set up correctly when requesting completions via callback):
  let b:ale_completion_result = []
  call setpos('.', [bufnr(''), 3, 14, 0])

  function! CompleteCallback() abort
    echo 'Called'
  endfunction


  call ale#completion#GetCompletions('ale-callback', {'callback': funcref('CompleteCallback')})

  AssertEqual
  \ {
  \   'request_id': 0,
  \   'conn_id': 0,
  \   'column': 14,
  \   'line_length': 14,
  \   'line': 3,
  \   'prefix': 'ab',
  \   'source': 'ale-callback',
  \ },
  \ b:ale_completion_info
  Assert !exists('b:ale_completion_result')

Execute(The correct keybinds should be configured):
  redir => g:output
    silent map <Plug>(ale_show_completion_menu)
  redir END

  AssertEqual
  \ [
  \   'n  <Plug>(ale_show_completion_menu) * :call ale#completion#RestoreCompletionOptions()<CR>',
  \   'o  <Plug>(ale_show_completion_menu) * <Nop>',
  \   'v  <Plug>(ale_show_completion_menu) * <Nop>',
  \ ],
  \ sort(split(g:output, "\n"))

Execute(Running the normal mode <Plug> keybind should reset the settings):
  let b:ale_old_omnifunc = 'FooBar'
  let b:ale_old_completeopt = 'menu'

  " We can't run the keybind, but we can call the function.
  call ale#completion#RestoreCompletionOptions()

  AssertEqual 'FooBar', &l:omnifunc
  AssertEqual 'menu', &l:completeopt
  Assert !has_key(b:, 'ale_old_omnifunc')
  Assert !has_key(b:, 'ale_old_completeopt')

Execute(HandleUserData should call ale#code_action#HandleCodeAction):
  let b:ale_completion_info = {'source': 'ale-manual'}
  call MockHandleCodeAction()

  call ale#completion#HandleUserData({})
  AssertEqual g:handle_code_action_called, 0

  call ale#completion#HandleUserData({
  \  'user_data': ''
  \})
  AssertEqual g:handle_code_action_called, 0

  call ale#completion#HandleUserData({
  \  'user_data': '{}'
  \})
  AssertEqual g:handle_code_action_called, 0

  call ale#completion#HandleUserData({
  \  'user_data': '{"codeActions": []}'
  \})
  AssertEqual g:handle_code_action_called, 0

  call ale#completion#HandleUserData({
  \  'user_data': '{"codeActions": [{"description":"", "changes": []}]}'
  \})
  AssertEqual g:handle_code_action_called, 1

  let b:ale_completion_info = {'source': 'ale-automatic'}
  call ale#completion#HandleUserData({
  \  'user_data': '{"codeActions": [{"description":"", "changes": []}]}'
  \})
  AssertEqual g:handle_code_action_called, 2

  let b:ale_completion_info = {'source': 'ale-callback'}
  call ale#completion#HandleUserData({
  \  'user_data': '{"codeActions": [{"description":"", "changes": []}]}'
  \})
  AssertEqual g:handle_code_action_called, 3

Execute(ale#code_action#HandleCodeAction should not be called when when source is not ALE):
  call MockHandleCodeAction()
  let b:ale_completion_info = {'source': 'syntastic'}
  call ale#completion#HandleUserData({
  \  'user_data': '{"codeActions": [{"description":"", "changes": []}]}'
  \})
  AssertEqual g:handle_code_action_called, 0