summaryrefslogtreecommitdiff
path: root/test/test_completion.vader
blob: 18e50f5b7462fce6a8b4965a8a6ab99bbf5402c6 (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
Before:
  Save g:ale_completion_enabled
  Save g:ale_completion_delay
  Save g:ale_completion_max_suggestions
  Save &l:omnifunc
  Save &l:completeopt

  let g:test_vars = {
  \ 'feedkeys_calls': [],
  \}

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

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

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

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

    AssertEqual a:expect_success, g:test_vars.get_completions_called
  endfunction

After:
  Restore

  unlet! g:test_vars
  unlet! b:ale_old_omnifunc
  unlet! b:ale_old_completopt
  unlet! b:ale_completion_info
  unlet! b:ale_completion_response
  unlet! b:ale_completion_parser
  unlet! b:ale_complete_done_time

  delfunction CheckCompletionCalled

  runtime autoload/ale/completion.vim
  runtime autoload/ale/lsp.vim

  if g:ale_completion_enabled
    call ale#completion#Enable()
  else
    call ale#completion#Disable()
  endif

Execute(TypeScript completions responses should be parsed correctly):
  AssertEqual [],
  \ ale#completion#ParseTSServerCompletions({
  \ 'body': [],
  \})
  AssertEqual ['foo', 'bar', 'baz'],
  \ ale#completion#ParseTSServerCompletions({
  \ 'body': [
  \   {'name': 'foo'},
  \   {'name': 'bar'},
  \   {'name': 'baz'},
  \ ],
  \})

Execute(TypeScript completion details responses should be parsed correctly):
  AssertEqual
  \ [
  \   {
  \     'word': 'abc',
  \     'menu': '(property) Foo.abc: number',
  \     'info': '',
  \     'kind': 'f',
  \     'icase': 1,
  \   },
  \   {
  \     'word': 'def',
  \     'menu': '(property) Foo.def: number',
  \     'info': 'foo bar baz',
  \     'kind': 'f',
  \     'icase': 1,
  \   },
  \ ],
  \ ale#completion#ParseTSServerCompletionEntryDetails({
  \ 'body': [
  \   {
  \     'name': 'abc',
  \     'kind': 'parameterName',
  \     'displayParts': [
  \       {'text': '('},
  \       {'text': 'property'},
  \       {'text': ')'},
  \       {'text': ' '},
  \       {'text': 'Foo'},
  \       {'text': '.'},
  \       {'text': 'abc'},
  \       {'text': ':'},
  \       {'text': ' '},
  \       {'text': 'number'},
  \     ],
  \   },
  \   {
  \     'name': 'def',
  \     'kind': 'parameterName',
  \     'displayParts': [
  \       {'text': '('},
  \       {'text': 'property'},
  \       {'text': ')'},
  \       {'text': ' '},
  \       {'text': 'Foo'},
  \       {'text': '.'},
  \       {'text': 'def'},
  \       {'text': ':'},
  \       {'text': ' '},
  \       {'text': 'number'},
  \     ],
  \     'documentation': [
  \       {'text': 'foo'},
  \       {'text': ' '},
  \       {'text': 'bar'},
  \       {'text': ' '},
  \       {'text': 'baz'},
  \     ],
  \   },
  \ ],
  \})

Execute(Prefix filtering should work for Lists of strings):
  AssertEqual
  \ ['FooBar', 'foo'],
  \ ale#completion#Filter(['FooBar', 'FongBar', 'baz', 'foo'], 'foo')
  AssertEqual
  \ ['FooBar', 'FongBar', 'baz', 'foo'],
  \ ale#completion#Filter(['FooBar', 'FongBar', 'baz', 'foo'], '.')

Execute(Prefix filtering should work for completion items):
  AssertEqual
  \ [{'word': 'FooBar'}, {'word': 'foo'}],
  \ ale#completion#Filter(
  \   [
  \     {'word': 'FooBar'},
  \     {'word': 'FongBar'},
  \     {'word': 'baz'},
  \     {'word': 'foo'},
  \   ],
  \   'foo'
  \ )
  AssertEqual
  \ [
  \   {'word': 'FooBar'},
  \   {'word': 'FongBar'},
  \   {'word': 'baz'},
  \   {'word': 'foo'},
  \ ],
  \ ale#completion#Filter(
  \   [
  \     {'word': 'FooBar'},
  \     {'word': 'FongBar'},
  \     {'word': 'baz'},
  \     {'word': 'foo'},
  \   ],
  \   '.'
  \ )

Execute(The right message sent to the tsserver LSP when the first completion message is received):
  " The cursor position needs to match what was saved before.
  call setpos('.', [bufnr(''), 1, 1, 0])
  let b:ale_completion_info = {
  \ 'conn_id': 123,
  \ 'prefix': 'f',
  \ 'request_id': 4,
  \ 'line': 1,
  \ 'column': 1,
  \}
  " We should only show up to this many suggestions.
  let g:ale_completion_max_suggestions = 3

  " Replace the Send function for LSP, so we can monitor calls to it.
  function! ale#lsp#Send(conn_id, message) abort
    let g:test_vars.message = a:message
  endfunction

  " Handle the response for completions.
  call ale#completion#HandleTSServerLSPResponse(123, {
  \ 'request_seq': 4,
  \ 'command': 'completions',
  \ 'body': [
  \   {'name': 'Baz'},
  \   {'name': 'dingDong'},
  \   {'name': 'Foo'},
  \   {'name': 'FooBar'},
  \   {'name': 'frazzle'},
  \   {'name': 'FFS'},
  \ ],
  \})

  " The entry details messages should have been sent.
  AssertEqual
  \ [
  \   0,
  \   'ts@completionEntryDetails',
  \   {
  \     'file': expand('%:p'),
  \     'entryNames': ['Foo', 'FooBar', 'frazzle'],
  \     'offset': 1,
  \     'line': 1,
  \   },
  \ ],
  \ g:test_vars.message

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

Execute(Completion should be done after dots in TypeScript):
  AssertEqual '.', ale#completion#GetPrefix(&filetype, 1, 13)

Execute(Completion should be done after words in TypeScript):
  AssertEqual 'ab', ale#completion#GetPrefix(&filetype, 2, 13)

Execute(Completion should be done after words in parens in TypeScript):
  AssertEqual 'ab', ale#completion#GetPrefix(&filetype, 3, 14)

Execute(Completion should not be done after parens in TypeScript):
  AssertEqual '', ale#completion#GetPrefix(&filetype, 3, 15)

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

  call ale#completion#Show('Response', 'Parser')

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

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

  call ale#completion#Show('Response', 'Parser')

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

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

  call ale#completion#OmniFunc(0, '')

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

Execute(ale#completion#Show() should make the correct feedkeys() call):
  call ale#completion#Show('Response', 'Parser')

  AssertEqual [["\<C-x>\<C-o>", 'n']], g:test_vars.feedkeys_calls

Execute(ale#completion#Show() should set up the response and parser):
  call ale#completion#Show('Response', 'Parser')

  AssertEqual 'Response', b:ale_completion_response
  AssertEqual 'Parser', b:ale_completion_parser

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_completopt = 'menu'
  let &l:completeopt = 'menu,menuone,preview,noselect,noinsert'

  call ale#completion#Done()

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

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):
  call setpos('.', [bufnr(''), 3, 14, 0])
  call ale#completion#GetCompletions()

  AssertEqual
  \ {
  \   'request_id': 0,
  \   'conn_id': 0,
  \   'column': 14,
  \   'line': 3,
  \   'prefix': 'ab',
  \ },
  \ b:ale_completion_info

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 when the cursor position changes):
  call setpos('.', [bufnr(''), 1, 2, 0])

  let g:test_vars.get_completions_called = 0

  " We just want to check if the function is called.
  function! ale#completion#GetCompletions()
    let g:test_vars.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:test_vars.get_completions_called

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