]> git.madduck.net Git - etc/vim.git/blob - test/completion/test_ale_import_command.vader

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Squashed '.vim/bundle/ale/' content from commit 22185c4c
[etc/vim.git] / test / completion / test_ale_import_command.vader
1 Before:
2   Save g:ale_enabled
3   Save b:ale_enabled
4   Save g:ale_lint_on_text_changed
5   Save g:ale_completion_enabled
6   Save g:ale_completion_autoimport
7   Save g:ale_completion_max_suggestions
8   Save g:ale_linters
9   Save b:ale_linters
10
11   let g:ale_enabled = 0
12   let b:ale_enabled = 0
13   let g:ale_lint_on_text_changed = 'always'
14   let g:ale_completion_enabled = 0
15   let g:ale_completion_autoimport = 0
16   let g:ale_completion_max_suggestions = 50
17   let g:ale_linters = {'typescript': ['tsserver'], 'python': ['pyre']}
18   unlet! b:ale_linters
19
20   let g:server_started_value = 1
21   let g:request_id = 0
22   let g:LastCallback = v:null
23   let g:LastHandleCallback = v:null
24   let g:sent_message_list = []
25   let g:code_action_list = []
26   let g:execute_list = []
27   let g:ale_queue_call_list = []
28
29   runtime autoload/ale.vim
30   runtime autoload/ale/util.vim
31   runtime autoload/ale/code_action.vim
32   runtime autoload/ale/lsp.vim
33   runtime autoload/ale/lsp_linter.vim
34
35   function! ale#util#Execute(expr) abort
36     call add(g:execute_list, a:expr)
37   endfunction
38
39   function! ale#Queue(...) abort
40     call add(g:ale_queue_call_list, a:000)
41   endfunction
42
43   function! ale#lsp#RegisterCallback(id, Callback) abort
44     let g:LastHandleCallback = a:Callback
45   endfunction
46
47   function! ale#lsp#NotifyForChanges(id, buffer) abort
48   endfunction
49
50   function! ale#lsp#HasCapability(id, capability) abort
51     return 1
52   endfunction
53
54   function! ale#lsp#Send(id, message) abort
55     let g:request_id += 1
56
57     call add(g:sent_message_list, a:message)
58
59     return g:request_id
60   endfunction
61
62   function! ale#lsp_linter#StartLSP(buffer, linter, Callback) abort
63     let g:LastCallback = a:Callback
64
65     return g:server_started_value
66   endfunction
67
68   function! ale#code_action#HandleCodeAction(code_action, options) abort
69     Assert !get(a:options, 'should_save')
70
71     call add(g:code_action_list, a:code_action)
72   endfunction
73
74   function GetLastMessage()
75     return get(g:execute_list, -1, '')
76   endfunction
77
78   function CheckLintStates(conn_id, message)
79     " Check that we request more linter results after adding completions.
80     AssertEqual [[0, '']], g:ale_queue_call_list
81
82     let g:ale_enabled = 0
83
84     let g:ale_queue_call_list = []
85     call g:LastHandleCallback(a:conn_id, a:message)
86     AssertEqual [], g:ale_queue_call_list
87
88     let g:ale_enabled = 1
89     let g:ale_lint_on_text_changed = 1
90
91     let g:ale_queue_call_list = []
92     call g:LastHandleCallback(a:conn_id, a:message)
93     AssertEqual [[0, '']], g:ale_queue_call_list
94
95     let g:ale_lint_on_text_changed = v:true
96
97     let g:ale_queue_call_list = []
98     call g:LastHandleCallback(a:conn_id, a:message)
99     AssertEqual [[0, '']], g:ale_queue_call_list
100
101     let g:ale_lint_on_text_changed = 'normal'
102
103     let g:ale_queue_call_list = []
104     call g:LastHandleCallback(a:conn_id, a:message)
105     AssertEqual [[0, '']], g:ale_queue_call_list
106
107     let g:ale_lint_on_text_changed = 'insert'
108
109     let g:ale_queue_call_list = []
110     call g:LastHandleCallback(a:conn_id, a:message)
111     AssertEqual [[0, '']], g:ale_queue_call_list
112
113     let g:ale_queue_call_list = []
114     let g:ale_lint_on_text_changed = 'never'
115
116     call g:LastHandleCallback(a:conn_id, a:message)
117     AssertEqual [], g:ale_queue_call_list
118
119     let g:ale_lint_on_text_changed = '0'
120
121     call g:LastHandleCallback(a:conn_id, a:message)
122     AssertEqual [], g:ale_queue_call_list
123
124     let g:ale_lint_on_text_changed = 0
125
126     call g:LastHandleCallback(a:conn_id, a:message)
127     AssertEqual [], g:ale_queue_call_list
128
129     let g:ale_lint_on_text_changed = 'xxx'
130
131     call g:LastHandleCallback(a:conn_id, a:message)
132     AssertEqual [], g:ale_queue_call_list
133   endfunction
134
135 After:
136   call ale#linter#Reset()
137
138   Restore
139
140   delfunction GetLastMessage
141   delfunction CheckLintStates
142
143   unlet! g:LastCallback
144   unlet! g:LastHandleCallback
145   unlet! g:request_id
146   unlet! g:server_started_value
147   unlet! g:sent_message_list
148   unlet! g:code_action_list
149   unlet! g:ale_queue_call_list
150   unlet! g:execute_list
151   unlet! g:received_message
152   unlet! b:ale_old_omnifunc
153   unlet! b:ale_old_completeopt
154   unlet! b:ale_completion_info
155   unlet! b:ale_completion_result
156   unlet! b:ale_complete_done_time
157
158   runtime autoload/ale.vim
159   runtime autoload/ale/util.vim
160   runtime autoload/ale/code_action.vim
161   runtime autoload/ale/lsp.vim
162   runtime autoload/ale/lsp_linter.vim
163
164 Given typescript(Some example TypeScript code):
165   let xyz = 123
166   let foo = missingword
167
168   let abc = 456
169
170 Execute(ALEImport should complain when there's no word at the cursor):
171   call setpos('.', [bufnr(''), 3, 1, 0])
172   ALEImport
173
174   AssertEqual 'echom ''Nothing to complete at cursor!''', GetLastMessage()
175
176 Execute(ALEImport should tell the user if no LSP is available):
177   let g:server_started_value = 0
178
179   call setpos('.', [bufnr(''), 2, 16, 0])
180   ALEImport
181
182   AssertEqual
183   \ 'echom ''No completion providers are available.''',
184   \ GetLastMessage()
185
186 Execute(ALEImport should request imports correctly for tsserver):
187   call setpos('.', [bufnr(''), 2, 16, 0])
188
189   ALEImport
190
191   AssertEqual
192   \ {
193   \   'conn_id': 0,
194   \   'request_id': 0,
195   \   'source': 'ale-import',
196   \   'column': 21,
197   \   'line': 2,
198   \   'line_length': 21,
199   \   'prefix': 'missingword',
200   \   'additional_edits_only': 1,
201   \ },
202   \ b:ale_completion_info
203   Assert g:LastCallback isnot v:null
204
205   call g:LastCallback(ale#linter#Get(&filetype)[0], {
206   \ 'connection_id': 347,
207   \ 'buffer': bufnr(''),
208   \})
209
210   AssertEqual
211   \ {
212   \   'conn_id': 347,
213   \   'request_id': 1,
214   \   'source': 'ale-import',
215   \   'column': 21,
216   \   'line': 2,
217   \   'line_length': 21,
218   \   'prefix': 'missingword',
219   \   'additional_edits_only': 1,
220   \ },
221   \ b:ale_completion_info
222   Assert g:LastHandleCallback isnot v:null
223
224   call g:LastHandleCallback(347, {
225   \ 'request_seq': 1,
226   \ 'command': 'completions',
227   \ 'body': [
228   \   {'name': 'missingwordIgnoreMe'},
229   \   {'name': 'missingword'},
230   \ ],
231   \})
232
233   AssertEqual
234   \ [
235   \   [0, 'ts@completions', {
236   \     'file': expand('%:p'),
237   \     'includeExternalModuleExports': v:true,
238   \     'offset': 21,
239   \     'line': 2,
240   \     'prefix': 'missingword',
241   \   }],
242   \   [0, 'ts@completionEntryDetails', {
243   \     'file': expand('%:p'),
244   \     'entryNames': [{'name': 'missingword'}],
245   \     'offset': 21,
246   \     'line': 2,
247   \   }]
248   \ ],
249   \ g:sent_message_list
250   AssertEqual 2, b:ale_completion_info.request_id
251
252   let g:ale_enabled = 1
253   let g:received_message = {
254   \ 'request_seq': 2,
255   \ 'command': 'completionEntryDetails',
256   \ 'body': [
257   \   {
258   \     'name': 'missingword',
259   \     'kind': 'className',
260   \     'displayParts': [],
261   \     'codeActions': [{
262   \        'description': 'import { missingword } from "./Something";',
263   \        'changes': [],
264   \     }],
265   \   },
266   \ ],
267   \}
268   call g:LastHandleCallback(347, g:received_message)
269
270   AssertEqual
271   \ [
272   \   {
273   \     'description': 'import { missingword } from "./Something";',
274   \     'changes': [],
275   \   },
276   \ ],
277   \ g:code_action_list
278
279   call CheckLintStates(347, g:received_message)
280
281 Execute(ALEImport should tell the user when no completions were found from tsserver):
282   call setpos('.', [bufnr(''), 2, 16, 0])
283
284   ALEImport
285
286   AssertEqual
287   \ {
288   \   'conn_id': 0,
289   \   'request_id': 0,
290   \   'source': 'ale-import',
291   \   'column': 21,
292   \   'line': 2,
293   \   'line_length': 21,
294   \   'prefix': 'missingword',
295   \   'additional_edits_only': 1,
296   \ },
297   \ b:ale_completion_info
298   Assert g:LastCallback isnot v:null
299
300   call g:LastCallback(ale#linter#Get(&filetype)[0], {
301   \ 'connection_id': 347,
302   \ 'buffer': bufnr(''),
303   \})
304
305   AssertEqual
306   \ {
307   \   'conn_id': 347,
308   \   'request_id': 1,
309   \   'source': 'ale-import',
310   \   'column': 21,
311   \   'line': 2,
312   \   'line_length': 21,
313   \   'prefix': 'missingword',
314   \   'additional_edits_only': 1,
315   \ },
316   \ b:ale_completion_info
317   Assert g:LastHandleCallback isnot v:null
318
319   call g:LastHandleCallback(347, {
320   \ 'request_seq': 1,
321   \ 'command': 'completions',
322   \ 'body': [
323   \   {'name': 'missingwordIgnoreMe'},
324   \ ],
325   \})
326
327   AssertEqual 'echom ''No possible imports found.''', GetLastMessage()
328
329 Given python(Some example Python code):
330   xyz = 123
331   foo = missingword
332
333   abc = 456
334
335 Execute(ALEImport should request imports correctly for language servers):
336   call setpos('.', [bufnr(''), 2, 12, 0])
337
338   ALEImport
339
340   AssertEqual
341   \ {
342   \   'conn_id': 0,
343   \   'request_id': 0,
344   \   'source': 'ale-import',
345   \   'column': 17,
346   \   'line': 2,
347   \   'line_length': 17,
348   \   'prefix': 'missingword',
349   \   'additional_edits_only': 1,
350   \ },
351   \ b:ale_completion_info
352   Assert g:LastCallback isnot v:null
353
354   call g:LastCallback(ale#linter#Get(&filetype)[0], {
355   \ 'connection_id': 347,
356   \ 'buffer': bufnr(''),
357   \})
358
359   AssertEqual
360   \ {
361   \   'conn_id': 347,
362   \   'request_id': 1,
363   \   'source': 'ale-import',
364   \   'column': 17,
365   \   'line': 2,
366   \   'line_length': 17,
367   \   'prefix': 'missingword',
368   \   'additional_edits_only': 1,
369   \   'completion_filter': 'ale#completion#python#CompletionItemFilter',
370   \ },
371   \ b:ale_completion_info
372   Assert g:LastHandleCallback isnot v:null
373
374   AssertEqual
375   \ [
376   \   [0, 'textDocument/completion', {
377   \     'textDocument': {'uri': ale#path#ToFileURI(expand('%:p'))},
378   \     'position': {'character': 16, 'line': 1}
379   \   }],
380   \ ],
381   \ g:sent_message_list
382   AssertEqual 1, b:ale_completion_info.request_id
383
384   let g:ale_enabled = 1
385   let g:received_message = {
386   \ 'id': 1,
387   \ 'jsonrpc': '2.0',
388   \ 'result': {
389   \   'isIncomplete': v:false,
390   \   'items': [
391   \     {
392   \       'detail': 'Some other word we should ignore',
393   \       'filterText': 'missingwordIgnoreMe',
394   \       'insertText': 'missingwordIgnoreMe',
395   \       'insertTextFormat': 1,
396   \       'kind': 6,
397   \       'label': ' missingwordIgnoreMe',
398   \       'sortText': '3ee19999missingword',
399   \       'additionalTextEdits': [
400   \         {
401   \           'range': {
402   \             'start': {'line': 1, 'character': 1},
403   \             'end': {'line': 2, 'character': 1},
404   \           },
405   \           'newText': 'from something import missingwordIgnoreMe',
406   \         },
407   \       ],
408   \     },
409   \     {
410   \       'detail': 'Some word without text edits',
411   \       'filterText': 'missingword',
412   \       'insertText': 'missingword',
413   \       'insertTextFormat': 1,
414   \       'kind': 6,
415   \       'label': ' missingword',
416   \       'sortText': '3ee19999missingword',
417   \     },
418   \     {
419   \       'detail': 'The word we should use',
420   \       'filterText': 'missingword',
421   \       'insertText': 'missingword',
422   \       'insertTextFormat': 1,
423   \       'kind': 6,
424   \       'label': ' missingword',
425   \       'sortText': '3ee19999missingword',
426   \       'additionalTextEdits': [
427   \         {
428   \           'range': {
429   \             'start': {'line': 1, 'character': 1},
430   \             'end': {'line': 2, 'character': 1},
431   \           },
432   \           'newText': 'from something import missingword',
433   \         },
434   \       ],
435   \     },
436   \     {
437   \       'detail': 'The other word we should not use',
438   \       'filterText': 'missingword',
439   \       'insertText': 'missingword',
440   \       'insertTextFormat': 1,
441   \       'kind': 6,
442   \       'label': ' missingword',
443   \       'sortText': '3ee19999missingword',
444   \       'additionalTextEdits': [
445   \         {
446   \           'range': {
447   \             'start': {'line': 1, 'character': 1},
448   \             'end': {'line': 2, 'character': 1},
449   \           },
450   \           'newText': 'from something_else import missingword',
451   \         },
452   \       ],
453   \     },
454   \   ],
455   \ },
456   \}
457   call g:LastHandleCallback(347, g:received_message)
458
459   AssertEqual
460   \ [
461   \   {
462   \     'description': 'completion',
463   \     'changes': [
464   \       {
465   \         'fileName': expand('%:p'),
466   \         'textChanges': [
467   \           {
468   \             'start': {'line': 2, 'offset': 2},
469   \             'end': {'line': 3, 'offset': 2},
470   \             'newText': 'from something import missingword',
471   \           },
472   \         ],
473   \       },
474   \     ],
475   \   },
476   \ ],
477   \ g:code_action_list
478
479   call CheckLintStates(347, g:received_message)
480
481 Execute(ALEImport should tell the user when no completions were found from a language server):
482   call setpos('.', [bufnr(''), 2, 12, 0])
483
484   ALEImport
485
486   AssertEqual
487   \ {
488   \   'conn_id': 0,
489   \   'request_id': 0,
490   \   'source': 'ale-import',
491   \   'column': 17,
492   \   'line': 2,
493   \   'line_length': 17,
494   \   'prefix': 'missingword',
495   \   'additional_edits_only': 1,
496   \ },
497   \ b:ale_completion_info
498   Assert g:LastCallback isnot v:null
499
500   call g:LastCallback(ale#linter#Get(&filetype)[0], {
501   \ 'connection_id': 347,
502   \ 'buffer': bufnr(''),
503   \})
504
505   AssertEqual
506   \ {
507   \   'conn_id': 347,
508   \   'request_id': 1,
509   \   'source': 'ale-import',
510   \   'column': 17,
511   \   'line': 2,
512   \   'line_length': 17,
513   \   'prefix': 'missingword',
514   \   'additional_edits_only': 1,
515   \   'completion_filter': 'ale#completion#python#CompletionItemFilter',
516   \ },
517   \ b:ale_completion_info
518   Assert g:LastHandleCallback isnot v:null
519
520   AssertEqual
521   \ [
522   \   [0, 'textDocument/completion', {
523   \     'textDocument': {'uri': ale#path#ToFileURI(expand('%:p'))},
524   \     'position': {'character': 16, 'line': 1}
525   \   }],
526   \ ],
527   \ g:sent_message_list
528   AssertEqual 1, b:ale_completion_info.request_id
529
530   let g:received_message = {
531   \ 'id': 1,
532   \ 'jsonrpc': '2.0',
533   \ 'result': {
534   \   'isIncomplete': v:false,
535   \   'items': [
536   \     {
537   \       'detail': 'Some other word we should ignore',
538   \       'filterText': 'missingwordIgnoreMe',
539   \       'insertText': 'missingwordIgnoreMe',
540   \       'insertTextFormat': 1,
541   \       'kind': 6,
542   \       'label': ' missingwordIgnoreMe',
543   \       'sortText': '3ee19999missingword',
544   \       'additionalTextEdits': [
545   \         {
546   \           'range': {
547   \             'start': {'line': 1, 'character': 1},
548   \             'end': {'line': 2, 'character': 1},
549   \           },
550   \           'newText': 'from something import missingwordIgnoreMe',
551   \         },
552   \       ],
553   \     },
554   \     {
555   \       'detail': 'Some word without text edits',
556   \       'filterText': 'missingword',
557   \       'insertText': 'missingword',
558   \       'insertTextFormat': 1,
559   \       'kind': 6,
560   \       'label': ' missingword',
561   \       'sortText': '3ee19999missingword',
562   \     },
563   \   ],
564   \ },
565   \}
566   call g:LastHandleCallback(347, g:received_message)
567
568   AssertEqual 'echom ''No possible imports found.''', GetLastMessage()