1
0

completion.common.vim 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. scriptencoding utf-8
  2. function! Test_Compl_After_Trigger()
  3. call youcompleteme#test#setup#OpenFile(
  4. \ '/third_party/ycmd/ycmd/tests/clangd/testdata/basic.cpp', {} )
  5. call setpos( '.', [ 0, 11, 6 ] )
  6. " Required to trigger TextChangedI
  7. " https://github.com/vim/vim/issues/4665#event-2480928194
  8. call test_override( 'char_avail', 1 )
  9. " Must do the checks in a timer callback because we need to stay in insert
  10. " mode until done.
  11. function! Check( id ) closure
  12. call WaitForCompletion()
  13. call feedkeys( "\<ESC>" )
  14. endfunction
  15. call FeedAndCheckMain( 'cl.', funcref( 'Check' ) )
  16. " Checks run in insert mode, then exit insert mode.
  17. call assert_false( pumvisible(), 'pumvisible()' )
  18. call test_override( 'ALL', 0 )
  19. endfunction
  20. function! Test_Force_Semantic_TopLevel()
  21. call youcompleteme#test#setup#OpenFile(
  22. \ '/third_party/ycmd/ycmd/tests/clangd/testdata/basic.cpp', {} )
  23. call setpos( '.', [ 0, 17, 5 ] )
  24. function! Check( id )
  25. cal WaitForCompletion()
  26. let items = complete_info( [ 'items' ] )[ 'items' ]
  27. call assert_equal( 1, len( filter( items, 'v:val.abbr ==# "Foo"' ) ),
  28. \ 'Foo should be in the suggestions' )
  29. let items = complete_info( [ 'items' ] )[ 'items' ]
  30. call assert_equal( 1,
  31. \ len( filter( items, 'v:val.word ==# "__FUNCTION__"' ) ),
  32. \ '__FUNCTION__ should be in the suggestions' )
  33. call feedkeys( "\<ESC>" )
  34. endfunction
  35. call FeedAndCheckMain( "i\<C-Space>", funcref( 'Check' ) )
  36. " Checks run in insert mode, then exit insert mode.
  37. call assert_false( pumvisible(), 'pumvisible()' )
  38. call test_override( 'ALL', 0 )
  39. endfunction
  40. function! Test_Select_Next_Previous()
  41. call youcompleteme#test#setup#OpenFile(
  42. \ '/third_party/ycmd/ycmd/tests/clangd/testdata/basic.cpp', {} )
  43. call setpos( '.', [ 0, 11, 6 ] )
  44. " Required to trigger TextChangedI
  45. " https://github.com/vim/vim/issues/4665#event-2480928194
  46. call test_override( 'char_avail', 1 )
  47. function! Check( id )
  48. call WaitForCompletion()
  49. call CheckCurrentLine( ' foo.' )
  50. call CheckCompletionItemsContainsExactly( [ 'c', 'x', 'y' ] )
  51. call FeedAndCheckAgain( "\<Tab>", funcref( 'Check2' ) )
  52. endfunction
  53. function! Check2( id )
  54. call WaitForCompletion()
  55. call CheckCurrentLine( ' foo.c' )
  56. call CheckCompletionItemsContainsExactly( [ 'c', 'x', 'y' ] )
  57. call FeedAndCheckAgain( "\<Tab>", funcref( 'Check3' ) )
  58. endfunction
  59. function! Check3( id )
  60. call WaitForCompletion()
  61. call CheckCurrentLine( ' foo.x' )
  62. call CheckCompletionItemsContainsExactly( [ 'c', 'x', 'y' ] )
  63. call FeedAndCheckAgain( "\<BS>y", funcref( 'Check4' ) )
  64. endfunction
  65. function! Check4( id )
  66. call WaitForCompletion()
  67. call CheckCurrentLine( ' foo.y' )
  68. call CheckCompletionItemsContainsExactly( [ 'y' ] )
  69. call feedkeys( "\<ESC>" )
  70. endfunction
  71. call FeedAndCheckMain( 'cl.', funcref( 'Check' ) )
  72. " Checks run in insert mode, then exit insert mode.
  73. call assert_false( pumvisible(), 'pumvisible()' )
  74. call test_override( 'ALL', 0 )
  75. endfunction
  76. function! Test_Enter_Delete_Chars_Updates_Filter()
  77. call youcompleteme#test#setup#OpenFile(
  78. \ 'test/testdata/cpp/completion.cc', {} )
  79. call setpos( '.', [ 0, 23, 31 ] )
  80. " Required to trigger TextChangedI
  81. " https://github.com/vim/vim/issues/4665#event-2480928194
  82. call test_override( 'char_avail', 1 )
  83. function! Check1( id )
  84. call WaitForCompletion()
  85. call CheckCompletionItemsContainsExactly( [ 'colourOfLine', 'lengthOfLine' ] )
  86. call FeedAndCheckAgain( "\<BS>", funcref( 'Check2' ) )
  87. endfunction
  88. function! Check2( id )
  89. call WaitForCompletion()
  90. call CheckCompletionItemsContainsExactly( [
  91. \ 'operator=(…)',
  92. \ 'colourOfLine',
  93. \ 'lengthOfLine',
  94. \ 'RED_AND_YELLOW' ] )
  95. call FeedAndCheckAgain( 'w', funcref( 'Check3' ) )
  96. endfunction
  97. function! Check3( id )
  98. call WaitForCompletion()
  99. call CheckCompletionItemsContainsExactly( [ 'RED_AND_YELLOW' ] )
  100. " now type something that doesnt match
  101. call FeedAndCheckAgain( 'this_does_not_match', funcref( 'Check4' ) )
  102. endfunction
  103. function! Check4( id )
  104. call WaitForAssert( { -> assert_false( pumvisible() ) } )
  105. call CheckCurrentLine(
  106. \ ' p->line.colourOfLine = Line::owthis_does_not_match' )
  107. call CheckCompletionItemsContainsExactly( [] )
  108. call feedkeys( "\<Esc>" )
  109. endfunction
  110. call FeedAndCheckMain( 'cl:ol', funcref( 'Check1' ) )
  111. " Checks run in insert mode, then exit insert mode.
  112. call assert_false( pumvisible(), 'pumvisible()' )
  113. call test_override( 'ALL', 0 )
  114. endfunction
  115. function! SetUp_Test_Compl_No_Filetype()
  116. call youcompleteme#test#setup#PushGlobal( 'ycm_filetype_whitelist', {
  117. \ '*': 1,
  118. \ 'ycm_nofiletype': 1
  119. \ } )
  120. call youcompleteme#test#setup#PushGlobal( 'ycm_filetype_blacklist', {} )
  121. endfunction
  122. function! Test_Compl_No_Filetype()
  123. call assert_false( has_key( g:ycm_filetype_blacklist, 'ycm_nofiletype' ) )
  124. enew
  125. call setline( '.', 'hello this is some text ' )
  126. " Even when fileytpe is set to '', the filetype autocommand is triggered, but
  127. " apparently, _not_ within this function.
  128. doautocmd FileType
  129. call assert_equal( 1, b:ycm_completing )
  130. " Required to trigger TextChangedI
  131. " https://github.com/vim/vim/issues/4665#event-2480928194
  132. call test_override( 'char_avail', 1 )
  133. " Must do the checks in a timer callback because we need to stay in insert
  134. " mode until done.
  135. function! Check( id ) closure
  136. call assert_equal( getline( '2' ), 'hell' )
  137. call WaitForCompletion()
  138. let items = complete_info().items
  139. call map( items, {index, value -> value.word} )
  140. call assert_equal( [ 'hello' ], items )
  141. call feedkeys( "\<ESC>" )
  142. endfunction
  143. call FeedAndCheckMain( 'ohell', funcref( 'Check' ) )
  144. " Checks run in insert mode, then exit insert mode.
  145. call assert_false( pumvisible(), 'pumvisible()' )
  146. call test_override( 'ALL', 0 )
  147. delfunc! Check
  148. endfunction
  149. function! TearDown_Test_Compl_No_Filetype()
  150. call youcompleteme#test#setup#PopGlobal( 'ycm_filetype_whitelist' )
  151. call youcompleteme#test#setup#PopGlobal( 'ycm_filetype_blacklist' )
  152. endfunction
  153. function! Test_Compl_No_Filetype_Blacklisted()
  154. call assert_true( has_key( g:ycm_filetype_blacklist, 'ycm_nofiletype' ) )
  155. enew
  156. call setline( '.', 'hello this is some text ' )
  157. " Even when fileytpe is set to '', the filetype autocommand is triggered, but
  158. " apparently, _not_ within this function.
  159. doautocmd FileType
  160. call assert_false( exists( 'b:ycm_completing' ) )
  161. " Required to trigger TextChangedI
  162. " https://github.com/vim/vim/issues/4665#event-2480928194
  163. call test_override( 'char_avail', 1 )
  164. " Must do the checks in a timer callback because we need to stay in insert
  165. " mode until done.
  166. function! Check( id ) closure
  167. call assert_false( pumvisible() )
  168. call feedkeys( "\<ESC>" )
  169. endfunction
  170. call FeedAndCheckMain( 'ohell', funcref( 'Check' ) )
  171. " Checks run in insert mode, then exit insert mode.
  172. call assert_false( pumvisible(), 'pumvisible()' )
  173. call test_override( 'ALL', 0 )
  174. delfunc! Check
  175. endfunction
  176. function! OmniFuncTester( findstart, query )
  177. if a:findstart
  178. return s:omnifunc_start_col
  179. endif
  180. return s:omnifunc_items
  181. endfunction
  182. function! SetUp_Test_OmniComplete_Filter()
  183. call youcompleteme#test#setup#PushGlobal( 'ycm_semantic_triggers', {
  184. \ 'omnifunc_test': [ ':', '.' ]
  185. \ } )
  186. endfunction
  187. function! Test_OmniComplete_Filter()
  188. enew
  189. setf omnifunc_test
  190. set omnifunc=OmniFuncTester
  191. let s:omnifunc_start_col = 3
  192. let s:omnifunc_items = [ 'test', 'testing', 'testy' ]
  193. function! Check1( id )
  194. call WaitForCompletion()
  195. call CheckCurrentLine( 'te:te' )
  196. call CheckCompletionItemsContainsExactly( [ 'test', 'testy', 'testing' ],
  197. \ 'word' )
  198. call FeedAndCheckAgain( 'y', funcref( 'Check2' ) )
  199. endfunction
  200. function! Check2( id )
  201. call WaitForCompletion()
  202. call CheckCurrentLine( 'te:tey' )
  203. call CheckCompletionItemsContainsExactly( [ 'testy' ], 'word' )
  204. call FeedAndCheckAgain( "\<C-n>", funcref( 'Check3' ) )
  205. endfunction
  206. function! Check3( id )
  207. call WaitForCompletion()
  208. call CheckCurrentLine( 'te:testy' )
  209. call CheckCompletionItemsContainsExactly( [ 'testy' ], 'word' )
  210. call FeedAndCheckAgain( "\<C-p>", funcref( 'Check4' ) )
  211. endfunction
  212. function! Check4( id )
  213. call WaitForCompletion()
  214. call CheckCurrentLine( 'te:tey' )
  215. call CheckCompletionItemsContainsExactly( [ 'testy' ], 'word' )
  216. call feedkeys( "\<Esc>" )
  217. endfunction
  218. call setline(1, 'te:' )
  219. call setpos( '.', [ 0, 1, 3 ] )
  220. call FeedAndCheckMain( 'ate', 'Check1' )
  221. endfunction
  222. function! TearDown_Test_OmniComplete_Filter()
  223. call youcompleteme#test#setup#PopGlobal( 'ycm_semantic_triggers' )
  224. endfunction
  225. function! Test_OmniComplete_Force()
  226. enew
  227. setf omnifunc_test
  228. set omnifunc=OmniFuncTester
  229. let s:omnifunc_start_col = 0
  230. let s:omnifunc_items = [ 'test', 'testing', 'testy' ]
  231. function! Check1( id )
  232. call WaitForCompletion()
  233. call CheckCurrentLine( 'te' )
  234. call CheckCompletionItemsContainsExactly( [ 'test', 'testy', 'testing' ],
  235. \ 'word' )
  236. call FeedAndCheckAgain( 'y', funcref( 'Check2' ) )
  237. endfunction
  238. function! Check2( id )
  239. call WaitForCompletion()
  240. call CheckCurrentLine( 'tey' )
  241. call CheckCompletionItemsContainsExactly( [ 'testy' ], 'word' )
  242. call FeedAndCheckAgain( "\<C-n>", funcref( 'Check3' ) )
  243. endfunction
  244. function! Check3( id )
  245. call WaitForCompletion()
  246. call CheckCurrentLine( 'testy' )
  247. call CheckCompletionItemsContainsExactly( [ 'testy' ], 'word' )
  248. call FeedAndCheckAgain( "\<C-p>", funcref( 'Check4' ) )
  249. endfunction
  250. function! Check4( id )
  251. call WaitForCompletion()
  252. call CheckCurrentLine( 'tey' )
  253. call CheckCompletionItemsContainsExactly( [ 'testy' ], 'word' )
  254. call feedkeys( "\<Esc>" )
  255. endfunction
  256. call setline(1, 'te' )
  257. call setpos( '.', [ 0, 1, 3 ] )
  258. call FeedAndCheckMain( "a\<C-Space>", 'Check1' )
  259. endfunction
  260. function! Test_Completion_FixIt()
  261. " There's a bug in clangd where you have to open a file which includes the
  262. " file you want to auto-include before it will actually auto-include that
  263. " file, auto_include_workaround #includes auto_include.h, so that clangd knows
  264. " about it
  265. call youcompleteme#test#setup#OpenFile(
  266. \ 'test/testdata/cpp/auto_include_workaround.cc', {} )
  267. call youcompleteme#test#setup#OpenFile(
  268. \ 'test/testdata/cpp/auto_include.cc', {} )
  269. function! Check1( id )
  270. call WaitForCompletion()
  271. call CheckCurrentLine( 'do_a' )
  272. call CheckCompletionItemsHasItems( [ 'do_a_thing(Thing thing)',
  273. \ 'do_another_thing()' ] )
  274. call FeedAndCheckAgain( "\<Tab>" , funcref( 'Check2' ) )
  275. endfunction
  276. function! Check2( id )
  277. call WaitForCompletion()
  278. call CheckCurrentLine( 'do_a_thing' )
  279. call CheckCompletionItemsHasItems( [ 'do_a_thing(Thing thing)',
  280. \ 'do_another_thing()' ] )
  281. call FeedAndCheckAgain( '(' , funcref( 'Check3' ) )
  282. endfunction
  283. function! Check3( id )
  284. call WaitForAssert( {-> assert_false( pumvisible(), 'pumvisible()' ) } )
  285. call CheckCurrentLine( 'do_a_thing(' )
  286. call assert_equal( '#include "auto_include.h"', getline( 1 ) )
  287. call feedkeys( "\<Esc>" )
  288. endfunction
  289. call setpos( '.', [ 0, 3, 1 ] )
  290. call FeedAndCheckMain( "Ado_a\<C-Space>", funcref( 'Check1' ) )
  291. endfunction
  292. function! Test_Select_Next_Previous_InsertModeMapping()
  293. call youcompleteme#test#setup#OpenFile(
  294. \ '/third_party/ycmd/ycmd/tests/clangd/testdata/basic.cpp', {} )
  295. call setpos( '.', [ 0, 11, 6 ] )
  296. inoremap <C-n> <Down>
  297. " Required to trigger TextChangedI
  298. " https://github.com/vim/vim/issues/4665#event-2480928194
  299. call test_override( 'char_avail', 1 )
  300. function! Check( id )
  301. call WaitForCompletion()
  302. call CheckCurrentLine( ' foo.' )
  303. call CheckCompletionItemsContainsExactly( [ 'c', 'x', 'y' ] )
  304. call FeedAndCheckAgain( "\<C-n>", funcref( 'Check2' ) )
  305. endfunction
  306. function! Check2( id )
  307. call WaitForCompletion()
  308. call CheckCurrentLine( ' foo.c' )
  309. call CheckCompletionItemsContainsExactly( [ 'c', 'x', 'y' ] )
  310. call FeedAndCheckAgain( "\<C-n>", funcref( 'Check3' ) )
  311. endfunction
  312. function! Check3( id )
  313. call WaitForCompletion()
  314. call CheckCurrentLine( ' foo.x' )
  315. call CheckCompletionItemsContainsExactly( [ 'c', 'x', 'y' ] )
  316. call FeedAndCheckAgain( "\<BS>a", funcref( 'Check4' ) )
  317. endfunction
  318. function! Check4( id )
  319. call CheckCurrentLine( ' foo.a' )
  320. call CheckCompletionItemsContainsExactly( [] )
  321. call FeedAndCheckAgain( "\<C-n>", funcref( 'Check5' ) )
  322. endfunction
  323. function! Check5( id )
  324. " The last ctrl-n moved to the next line
  325. call CheckCurrentLine( '}' )
  326. call assert_equal( [ 0, 12, 2, 0 ], getpos( '.' ) )
  327. call CheckCompletionItemsContainsExactly( [] )
  328. call feedkeys( "\<Esc>" )
  329. endfunction
  330. call FeedAndCheckMain( 'cl.', funcref( 'Check' ) )
  331. " Checks run in insert mode, then exit insert mode.
  332. call assert_false( pumvisible(), 'pumvisible()' )
  333. call test_override( 'ALL', 0 )
  334. iunmap <C-n>
  335. endfunction
  336. function! Test_Completion_WorksWithoutMovingCursor()
  337. call youcompleteme#test#setup#OpenFile(
  338. \ 'test/testdata/cpp/auto_include_workaround.cc', {} )
  339. function! Check( id )
  340. call WaitForCompletion() " We don't care about completion
  341. " items, just that we didn't error
  342. " while opening the completion pum
  343. " without typing anything first.
  344. call feedkeys( "\<Esc>" )
  345. endfunction
  346. call setpos( '.', [ 0, 3, 1 ] )
  347. call FeedAndCheckMain( "i\<C-Space>", funcref( 'Check' ) )
  348. endfunction
  349. function! SetUp_Test_Manual_Trigger()
  350. call youcompleteme#test#setup#PushGlobal( 'ycm_auto_trigger', 0 )
  351. endfunction
  352. function! Test_Manual_Trigger()
  353. call youcompleteme#test#setup#OpenFile(
  354. \ '/third_party/ycmd/ycmd/tests/clangd/testdata/basic.cpp', {} )
  355. call setpos( '.', [ 0, 11, 6 ] )
  356. imap <C-d> <plug>(YCMComplete)
  357. " Required to trigger TextChangedI
  358. " https://github.com/vim/vim/issues/4665#event-2480928194
  359. call test_override( 'char_avail', 1 )
  360. function! Check( id )
  361. call WaitForCompletion()
  362. call CheckCurrentLine( ' tfthne' )
  363. call CheckCompletionItemsContainsExactly( [
  364. \ 'test_function_that_has_no_errors' ], 'word' )
  365. call FeedAndCheckAgain( "\<BS>", funcref( 'Check2' ) )
  366. endfunction
  367. function! Check2( id )
  368. call WaitForCompletion()
  369. call CheckCurrentLine( ' tfthn' )
  370. call CheckCompletionItemsContainsExactly( [
  371. \ 'test_function_that_has_no_errors' ], 'word' )
  372. call feedkeys( "\<Esc>" )
  373. endfunction
  374. call FeedAndCheckMain( "Otfthne\<C-d>", funcref( 'Check' ) )
  375. " Checks run in insert mode, then exit insert mode.
  376. call assert_false( pumvisible(), 'pumvisible()' )
  377. call test_override( 'ALL', 0 )
  378. iunmap <C-d>
  379. endfunction
  380. function! TearDown_Test_Manual_Trigger()
  381. call youcompleteme#test#setup#PopGlobal( 'ycm_auto_trigger' )
  382. endfunction
  383. function! SetUp_Test_Manual_Trigger_CompleteFunc()
  384. call youcompleteme#test#setup#PushGlobal( 'ycm_auto_trigger', 0 )
  385. endfunction
  386. function! Test_Manual_Trigger_CompleteFunc()
  387. call youcompleteme#test#setup#OpenFile(
  388. \ '/third_party/ycmd/ycmd/tests/clangd/testdata/basic.cpp', {} )
  389. call setpos( '.', [ 0, 11, 6 ] )
  390. set completefunc=youcompleteme#CompleteFunc
  391. " Required to trigger TextChangedI
  392. " https://github.com/vim/vim/issues/4665#event-2480928194
  393. call test_override( 'char_avail', 1 )
  394. function! Check( id )
  395. call WaitForCompletion()
  396. call CheckCurrentLine( ' tfthne' )
  397. call CheckCompletionItemsContainsExactly( [
  398. \ 'test_function_that_has_no_errors' ], 'word' )
  399. call FeedAndCheckAgain( "\<BS>", funcref( 'Check2' ) )
  400. endfunction
  401. function! Check2( id )
  402. call WaitForCompletion()
  403. call CheckCurrentLine( ' tfthn' )
  404. call CheckCompletionItemsContainsExactly( [
  405. \ 'test_function_that_has_no_errors' ], 'word' )
  406. call feedkeys( "\<Esc>" )
  407. endfunction
  408. call FeedAndCheckMain( "Otfthne\<C-x>\<C-u>", funcref( 'Check' ) )
  409. " Checks run in insert mode, then exit insert mode.
  410. call assert_false( pumvisible(), 'pumvisible()' )
  411. call test_override( 'ALL', 0 )
  412. set completefunc=
  413. endfunction
  414. function! TearDown_Test_Manual_Trigger_CompleteFunc()
  415. call youcompleteme#test#setup#PopGlobal( 'ycm_auto_trigger' )
  416. endfunction