summaryrefslogtreecommitdiff
path: root/test/completion/test_lsp_completion_messages.vader
blob: 35b167b641189170ce59744f38abae5439533975 (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
Before:
  Save g:ale_completion_delay
  Save g:ale_completion_max_suggestions
  Save g:ale_completion_info
  Save &l:omnifunc
  Save &l:completeopt

  let g:ale_completion_enabled = 1

  call ale#test#SetDirectory('/testplugin/test/completion')
  call ale#test#SetFilename('dummy.txt')

  runtime autoload/ale/lsp.vim

  let g:message_list = []
  let g:capability_checked = ''
  let g:conn_id = v:null
  let g:Callback = ''
  let g:init_callback_list = []

  function! ale#lsp_linter#StartLSP(buffer, linter, Callback) abort
    let g:conn_id = ale#lsp#Register('executable', '/foo/bar', {})
    call ale#lsp#MarkDocumentAsOpen(g:conn_id, a:buffer)

    let l:details = {
    \ 'buffer': a:buffer,
    \ 'connection_id': g:conn_id,
    \ 'project_root': '/foo/bar',
    \ 'language_id': 'python',
    \}

    call add(g:init_callback_list, {-> a:Callback(a:linter, l:details)})
  endfunction

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

  function! ale#lsp#HasCapability(conn_id, capability) abort
    let g:capability_checked = a:capability

    return 1
  endfunction

  function! ale#lsp#RegisterCallback(conn_id, callback) abort
    let g:Callback = a:callback
  endfunction

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

    return 1
  endfunction

After:
  Restore

  if g:conn_id isnot v:null
    call ale#lsp#RemoveConnectionWithID(g:conn_id)
  endif

  unlet! g:message_list
  unlet! g:capability_checked
  unlet! g:init_callback_list
  unlet! g:conn_id
  unlet! g:Callback
  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
  unlet! b:ale_linters
  unlet! b:ale_tsserver_completion_names

  " Reset the function.
  function! ale#util#Mode(...) abort
    return call('mode', a:000)
  endfunction

  call ale#test#RestoreDirectory()
  call ale#linter#Reset()

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

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

Given typescript(Some typescript file):
  foo
  somelongerline
  bazxyzxyzxyz

Execute(The right message should be sent for the initial tsserver request):
  runtime ale_linters/typescript/tsserver.vim
  let b:ale_linters = ['tsserver']
  " The cursor position needs to match what was saved before.
  call setpos('.', [bufnr(''), 1, 3, 0])

  call ale#completion#GetCompletions()

  " We shouldn't register the callback yet.
  AssertEqual '''''', string(g:Callback)

  AssertEqual 1, len(g:init_callback_list)
  call map(g:init_callback_list, 'v:val()')

  AssertEqual 'completion', g:capability_checked

  " We should send the right callback.
  AssertEqual
  \ 'function(''ale#completion#HandleTSServerResponse'')',
  \ string(g:Callback)
  " We should send the right message.
  AssertEqual
  \ [[0, 'ts@completions', {'file': expand('%:p'), 'line': 1, 'offset': 3, 'prefix': 'fo'}]],
  \ g:message_list
  " We should set up the completion info correctly.
  AssertEqual
  \ {
  \   'line_length': 3,
  \   'conn_id': g:conn_id,
  \   'column': 3,
  \   'request_id': 1,
  \   'line': 1,
  \   'prefix': 'fo',
  \ },
  \ get(b:, 'ale_completion_info', {})

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

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

  " We should save the names we got in the buffer, as TSServer doesn't return
  " details for every name.
  AssertEqual
  \ ['Foo', 'FooBar', 'frazzle'],
  \ get(b:, 'ale_tsserver_completion_names', [])

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

Given python(Some Python file):
  foo
  somelongerline
  bazxyzxyzxyz

Execute(The right message should be sent for the initial LSP request):
  runtime ale_linters/python/pyls.vim
  let b:ale_linters = ['pyls']
  " The cursor position needs to match what was saved before.
  call setpos('.', [bufnr(''), 1, 5, 0])

  call ale#completion#GetCompletions()

  " We shouldn't register the callback yet.
  AssertEqual '''''', string(g:Callback)

  AssertEqual 1, len(g:init_callback_list)
  call map(g:init_callback_list, 'v:val()')

  AssertEqual 'completion', g:capability_checked

  " We should send the right callback.
  AssertEqual
  \ 'function(''ale#completion#HandleLSPResponse'')',
  \ string(g:Callback)
  " We should send the right message.
  " The character index needs to be at most the index of the last character on
  " the line, or integration with pyls will be broken.
  "
  " We need to send the message for changing the document first.
  AssertEqual
  \ [
  \   [1, 'textDocument/didChange', {
  \     'textDocument': {
  \         'uri': ale#path#ToURI(expand('%:p')),
  \         'version': g:ale_lsp_next_version_id - 1,
  \     },
  \     'contentChanges': [{'text': join(getline(1, '$'), "\n") . "\n"}]
  \   }],
  \   [0, 'textDocument/completion', {
  \   'textDocument': {'uri': ale#path#ToURI(expand('%:p'))},
  \   'position': {'line': 0, 'character': 3},
  \   }],
  \ ],
  \ g:message_list
  " We should set up the completion info correctly.
  AssertEqual
  \ {
  \   'line_length': 3,
  \   'conn_id': g:conn_id,
  \   'column': 3,
  \   'request_id': 1,
  \   'line': 1,
  \   'prefix': 'fo',
  \   'completion_filter': 'ale#completion#python#CompletionItemFilter',
  \ },
  \ get(b:, 'ale_completion_info', {})

Execute(Two completion requests shouldn't be sent in a row):
  call ale#linter#PreventLoading('python')
  call ale#linter#Define('python', {
  \   'name': 'foo',
  \   'lsp': 'stdio',
  \   'executable': 'foo',
  \   'command': 'foo',
  \   'project_root_callback': {-> '/foo/bar'},
  \})
  call ale#linter#Define('python', {
  \   'name': 'bar',
  \   'lsp': 'stdio',
  \   'executable': 'foo',
  \   'command': 'foo',
  \   'project_root_callback': {-> '/foo/bar'},
  \})
  let b:ale_linters = ['foo', 'bar']

  " The cursor position needs to match what was saved before.
  call setpos('.', [bufnr(''), 1, 5, 0])

  call ale#completion#GetCompletions()

  " We shouldn't register the callback yet.
  AssertEqual '''''', string(g:Callback)

  AssertEqual 2, len(g:init_callback_list)
  call map(g:init_callback_list, 'v:val()')

  AssertEqual 'completion', g:capability_checked

  " We should only send one completion message for two LSP servers.
  AssertEqual
  \ [
  \   [1, 'textDocument/didChange', {
  \     'textDocument': {
  \         'uri': ale#path#ToURI(expand('%:p')),
  \         'version': g:ale_lsp_next_version_id - 1,
  \     },
  \     'contentChanges': [{'text': join(getline(1, '$'), "\n") . "\n"}]
  \   }],
  \   [0, 'textDocument/completion', {
  \   'textDocument': {'uri': ale#path#ToURI(expand('%:p'))},
  \   'position': {'line': 0, 'character': 3},
  \   }],
  \ ],
  \ g:message_list