2 # Unit tests for language server protocol offset encoding using clangd
6 # Start the C language server. Returns true on success and false on failure.
7 def g:StartLangServer(): bool
8 if has('patch-9.0.1629')
9 return g:StartLangServerWithFile('Xtest.c')
14 if !has('patch-9.0.1629')
15 # Need patch 9.0.1629 to properly encode/decode the UTF-16 offsets
19 var lspOpts = {autoComplete: false}
20 g:LspOptionsSet(lspOpts)
23 filetype: ['c', 'cpp'],
24 path: (exepath('clangd-15') ?? exepath('clangd')),
25 args: ['--background-index',
27 $'--offset-encoding={$LSP_OFFSET_ENCODING}']
29 call LspAddServer(lspServers)
31 # Test for :LspCodeAction with symbols containing multibyte and composing
33 def g:Test_LspCodeAction_multibyte()
34 silent! edit XLspCodeAction_mb.c
36 var lines =<< trim END
40 printf("aVar = %d\n", aVar);
41 printf("馃槉馃槉馃槉馃槉 = %d\n", aVar):
42 printf("a虂b虂a虂b虂 = %d\n", aVar):
43 printf("a台虂a台虂a台虂a台虂 = %d\n", aVar):
47 g:WaitForServerFileLoad(3)
52 assert_equal(' printf("馃槉馃槉馃槉馃槉 = %d\n", aVar);', getline(5))
56 assert_equal(' printf("a虂b虂a虂b虂 = %d\n", aVar);', getline(6))
60 assert_equal(' printf("a台虂a台虂a台虂a台虂 = %d\n", aVar);', getline(7))
65 # Test for ":LspDiag show" when using multibyte and composing characters
66 def g:Test_LspDiagShow_multibyte()
67 :silent! edit XLspDiagShow_mb.c
69 var lines =<< trim END
73 printf("aVar = %d\n", aVar);
74 printf("馃槉馃槉馃槉馃槉 = %d\n". aVar);
75 printf("a虂b虂a虂b虂 = %d\n". aVar);
76 printf("a台虂a台虂a台虂a台虂 = %d\n". aVar);
80 g:WaitForServerFileLoad(3)
83 var qfl: list<dict<any>> = getloclist(0)
84 assert_equal([5, 37], [qfl[0].lnum, qfl[0].col])
85 assert_equal([6, 33], [qfl[1].lnum, qfl[1].col])
86 assert_equal([7, 41], [qfl[2].lnum, qfl[2].col])
91 # Test for :LspFormat when using multibyte and composing characters
92 def g:Test_LspFormat_multibyte()
93 :silent! edit XLspFormat_mb.c
95 var lines =<< trim END
98 int 馃槉馃槉馃槉馃槉 = aVar + 1;
99 int a虂b虂a虂b虂 = aVar + 1;
100 int a台虂a台虂a台虂a台虂 = aVar + 1;
104 g:WaitForServerFileLoad(0)
107 var expected =<< trim END
109 int 馃槉馃槉馃槉馃槉 = aVar + 1;
110 int a虂b虂a虂b虂 = aVar + 1;
111 int a台虂a台虂a台虂a台虂 = aVar + 1;
114 assert_equal(expected, getline(1, '$'))
118 # Test for :LspGotoDefinition when using multibyte and composing characters
119 def g:Test_LspGotoDefinition_multibyte()
120 :silent! edit XLspGotoDefinition_mb.c
122 var lines: list<string> =<< trim END
126 printf("aVar = %d\n", aVar);
127 printf("馃槉馃槉馃槉馃槉 = %d\n", aVar);
128 printf("a虂b虂a虂b虂 = %d\n", aVar);
129 printf("a台虂a台虂a台虂a台虂 = %d\n", aVar);
133 g:WaitForServerFileLoad(0)
136 for [lnum, colnr] in [[4, 27], [5, 39], [6, 35], [7, 43]]
139 assert_equal([2, 13], [line('.'), col('.')])
145 # Test for :LspGotoDefinition when using multibyte and composing characters
146 def g:Test_LspGotoDefinition_after_multibyte()
147 :silent! edit XLspGotoDef_after_mb.c
149 var lines =<< trim END
152 /* 伪尾纬未, 馃槉馃槉馃槉馃槉, a虂b虂a虂b虂, a台虂a台虂a台虂a台虂 */ int 伪尾纬未, bVar;
153 /* 伪尾纬未, 馃槉馃槉馃槉馃槉, a虂b虂a虂b虂, a台虂a台虂a台虂a台虂 */ int 馃槉馃槉馃槉馃槉, cVar;
154 /* 伪尾纬未, 馃槉馃槉馃槉馃槉, a虂b虂a虂b虂, a台虂a台虂a台虂a台虂 */ int a虂b虂a虂b虂, dVar;
155 /* 伪尾纬未, 馃槉馃槉馃槉馃槉, a虂b虂a虂b虂, a台虂a台虂a台虂a台虂 */ int a台虂a台虂a台虂a台虂, eVar;
160 aVar = 伪尾纬未 + 馃槉馃槉馃槉馃槉 + a虂b虂a虂b虂 + a台虂a台虂a台虂a台虂 + bVar;
164 g:WaitForServerFileLoad(0)
168 assert_equal([3, 88], [line('.'), col('.')])
171 assert_equal([4, 96], [line('.'), col('.')])
174 assert_equal([5, 92], [line('.'), col('.')])
177 assert_equal([6, 100], [line('.'), col('.')])
180 assert_equal([3, 78], [line('.'), col('.')])
183 assert_equal([4, 78], [line('.'), col('.')])
186 assert_equal([5, 78], [line('.'), col('.')])
189 assert_equal([6, 78], [line('.'), col('.')])
194 # Test for doing omni completion for symbols with multibyte and composing
196 def g:Test_OmniComplete_multibyte()
197 :silent! edit XOmniComplete_mb.c
199 var lines: list<string> =<< trim END
204 int a台虂a台虂a台虂a台虂, cVar;
211 g:WaitForServerFileLoad(0)
215 feedkeys("aaV\<C-X>\<C-O> = 馃槉馃槉\<C-X>\<C-O>;", 'xt')
216 assert_equal(' aVar = 馃槉馃槉馃槉馃槉;', getline('.'))
218 feedkeys("abV\<C-X>\<C-O> = a虂b虂\<C-X>\<C-O>;", 'xt')
219 assert_equal(' bVar = a虂b虂a虂b虂;', getline('.'))
221 feedkeys("acV\<C-X>\<C-O> = a台虂a台虂\<C-X>\<C-O>;", 'xt')
222 assert_equal(' cVar = a台虂a台虂a台虂a台虂;', getline('.'))
223 feedkeys("oa虂b虂\<C-X>\<C-O> = a台虂a台虂\<C-X>\<C-O>;", 'xt')
224 assert_equal(' a虂b虂a虂b虂 = a台虂a台虂a台虂a台虂;', getline('.'))
225 feedkeys("oa台虂a台虂\<C-X>\<C-O> = a虂b虂\<C-X>\<C-O>;", 'xt')
226 assert_equal(' a台虂a台虂a台虂a台虂 = a虂b虂a虂b虂;', getline('.'))
230 # Test for :LspOutline with multibyte and composing characters
231 def g:Test_Outline_multibyte()
232 silent! edit XLspOutline_mb.c
234 var lines: list<string> =<< trim END
235 typedef void 馃槉馃槉馃槉馃槉;
236 typedef void a虂b虂a虂b虂;
237 typedef void a台虂a台虂a台虂a台虂;
252 g:WaitForServerFileLoad(0)
257 assert_equal(2, winnr('$'))
261 feedkeys("\<CR>", 'xt')
262 assert_equal([2, 5, 18], [winnr(), line('.'), col('.')])
266 feedkeys("\<CR>", 'xt')
267 assert_equal([2, 9, 14], [winnr(), line('.'), col('.')])
271 feedkeys("\<CR>", 'xt')
272 assert_equal([2, 13, 22], [winnr(), line('.'), col('.')])
276 feedkeys("\<CR>", 'xt')
277 assert_equal([2, 1, 14], [winnr(), line('.'), col('.')])
281 feedkeys("\<CR>", 'xt')
282 assert_equal([2, 2, 14], [winnr(), line('.'), col('.')])
286 feedkeys("\<CR>", 'xt')
287 assert_equal([2, 3, 14], [winnr(), line('.'), col('.')])
292 # Test for :LspRename with multibyte and composing characters
293 def g:Test_LspRename_multibyte()
294 silent! edit XLspRename_mb.c
296 var lines: list<string> =<< trim END
300 printf("aVar = %d\n", aVar);
301 printf("馃槉馃槉馃槉馃槉 = %d\n", aVar);
302 printf("a虂b虂a虂b虂 = %d\n", aVar);
303 printf("a台虂a台虂a台虂a台虂 = %d\n", aVar);
307 g:WaitForServerFileLoad(0)
312 var expected: list<string> =<< trim END
316 printf("aVar = %d\n", bVar);
317 printf("馃槉馃槉馃槉馃槉 = %d\n", bVar);
318 printf("a虂b虂a虂b虂 = %d\n", bVar);
319 printf("a台虂a台虂a台虂a台虂 = %d\n", bVar);
322 assert_equal(expected, getline(1, '$'))
326 # Test for :LspShowReferences when using multibyte and composing characters
327 def g:Test_LspShowReferences_multibyte()
328 :silent! edit XLspShowReferences_mb.c
330 var lines: list<string> =<< trim END
334 printf("aVar = %d\n", aVar);
335 printf("馃槉馃槉馃槉馃槉 = %d\n", aVar);
336 printf("a虂b虂a虂b虂 = %d\n", aVar);
337 printf("a台虂a台虂a台虂a台虂 = %d\n", aVar);
341 g:WaitForServerFileLoad(0)
345 var qfl: list<dict<any>> = getloclist(0)
346 assert_equal([2, 13], [qfl[0].lnum, qfl[0].col])
347 assert_equal([4, 27], [qfl[1].lnum, qfl[1].col])
348 assert_equal([5, 39], [qfl[2].lnum, qfl[2].col])
349 assert_equal([6, 35], [qfl[3].lnum, qfl[3].col])
350 assert_equal([7, 43], [qfl[4].lnum, qfl[4].col])
356 # Test for :LspSymbolSearch when using multibyte and composing characters
357 def g:Test_LspSymbolSearch_multibyte()
358 silent! edit XLspSymbolSearch_mb.c
360 var lines: list<string> =<< trim END
361 typedef void 馃槉馃槉馃槉馃槉;
362 typedef void a虂b虂a虂b虂;
363 typedef void a台虂a台虂a台虂a台虂;
378 g:WaitForServerFileLoad(0)
381 feedkeys(":LspSymbolSearch Func1\<CR>", "xt")
382 assert_equal([5, 18], [line('.'), col('.')])
384 feedkeys(":LspSymbolSearch Func2\<CR>", "xt")
385 assert_equal([9, 14], [line('.'), col('.')])
387 feedkeys(":LspSymbolSearch Func3\<CR>", "xt")
388 assert_equal([13, 22], [line('.'), col('.')])
393 # Test for setting the 'tagfunc' with multibyte and composing characters in
395 def g:Test_LspTagFunc_multibyte()
396 var lines =<< trim END
401 int a台虂a台虂a台虂a台虂, dVar;
407 writefile(lines, 'Xtagfunc_mb.c')
408 :silent! edit! Xtagfunc_mb.c
409 g:WaitForServerFileLoad(0)
410 :setlocal tagfunc=lsp#lsp#TagFunc
413 assert_equal([3, 27], [line('.'), col('.')])
416 assert_equal([4, 23], [line('.'), col('.')])
419 assert_equal([5, 31], [line('.'), col('.')])
423 delete('Xtagfunc_mb.c')
426 # Test for the :LspSuperTypeHierarchy and :LspSubTypeHierarchy commands with
427 # multibyte and composing characters
428 def g:Test_LspTypeHier_multibyte()
429 silent! edit XLspTypeHier_mb.cpp
431 var lines =<< trim END
432 /* 伪尾馃槉馃槉a虂a虂a台虂a台虂 */ class parent {
435 /* 伪尾馃槉馃槉a虂a虂a台虂a台虂 */ class child : public parent {
438 /* 伪尾馃槉馃槉a虂a虂a台虂a台虂 */ class grandchild : public child {
442 g:WaitForServerFileLoad(0)
447 call feedkeys("\<CR>", 'xt')
448 assert_equal([1, 36], [line('.'), col('.')])
452 call feedkeys("\<Down>\<CR>", 'xt')
453 assert_equal([4, 42], [line('.'), col('.')])
457 call feedkeys("\<Down>\<Down>\<CR>", 'xt')
458 assert_equal([7, 42], [line('.'), col('.')])
461 :LspSuperTypeHierarchy
462 call feedkeys("\<CR>", 'xt')
463 assert_equal([7, 36], [line('.'), col('.')])
466 :LspSuperTypeHierarchy
467 call feedkeys("\<Down>\<CR>", 'xt')
468 assert_equal([4, 36], [line('.'), col('.')])
471 :LspSuperTypeHierarchy
472 call feedkeys("\<Down>\<Down>\<CR>", 'xt')
473 assert_equal([1, 36], [line('.'), col('.')])
478 # vim: shiftwidth=2 softtabstop=2 noexpandtab