completion.common.vim 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. endfunctio
  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 CheckCompletionItems( [ '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 CheckCompletionItems( [ '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 CheckCompletionItems( [ '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 CheckCompletionItems( [ '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 CheckCompletionItems( [ 'colourOfLine', 'lengthOfLine' ] )
  86. call FeedAndCheckAgain( "\<BS>", funcref( 'Check2' ) )
  87. endfunction
  88. function! Check2( id )
  89. call WaitForCompletion()
  90. call CheckCompletionItems( [
  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 CheckCompletionItems( [ '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 CheckCompletionItems( [] )
  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 CheckCompletionItems( [ 'test', 'testy', 'testing' ], 'word' )
  197. call FeedAndCheckAgain( 'y', funcref( 'Check2' ) )
  198. endfunction
  199. function! Check2( id )
  200. call WaitForCompletion()
  201. call CheckCurrentLine( 'te:tey' )
  202. call CheckCompletionItems( [ 'testy' ], 'word' )
  203. call FeedAndCheckAgain( "\<C-n>", funcref( 'Check3' ) )
  204. endfunction
  205. function! Check3( id )
  206. call WaitForCompletion()
  207. call CheckCurrentLine( 'te:testy' )
  208. call CheckCompletionItems( [ 'testy' ], 'word' )
  209. call FeedAndCheckAgain( "\<C-p>", funcref( 'Check4' ) )
  210. endfunction
  211. function! Check4( id )
  212. call WaitForCompletion()
  213. call CheckCurrentLine( 'te:tey' )
  214. call CheckCompletionItems( [ 'testy' ], 'word' )
  215. call feedkeys( "\<Esc>" )
  216. endfunction
  217. call setline(1, 'te:' )
  218. call setpos( '.', [ 0, 1, 3 ] )
  219. call FeedAndCheckMain( 'ate', 'Check1' )
  220. endfunction
  221. function! TearDown_Test_OmniComplete_Filter()
  222. call youcompleteme#test#setup#PopGlobal( 'ycm_semantic_triggers' )
  223. endfunction
  224. function! Test_OmniComplete_Force()
  225. enew
  226. setf omnifunc_test
  227. set omnifunc=OmniFuncTester
  228. let s:omnifunc_start_col = 0
  229. let s:omnifunc_items = [ 'test', 'testing', 'testy' ]
  230. function! Check1( id )
  231. call WaitForCompletion()
  232. call CheckCurrentLine( 'te' )
  233. call CheckCompletionItems( [ 'test', 'testy', 'testing' ], 'word' )
  234. call FeedAndCheckAgain( 'y', funcref( 'Check2' ) )
  235. endfunction
  236. function! Check2( id )
  237. call WaitForCompletion()
  238. call CheckCurrentLine( 'tey' )
  239. call CheckCompletionItems( [ 'testy' ], 'word' )
  240. call FeedAndCheckAgain( "\<C-n>", funcref( 'Check3' ) )
  241. endfunction
  242. function! Check3( id )
  243. call WaitForCompletion()
  244. call CheckCurrentLine( 'testy' )
  245. call CheckCompletionItems( [ 'testy' ], 'word' )
  246. call FeedAndCheckAgain( "\<C-p>", funcref( 'Check4' ) )
  247. endfunction
  248. function! Check4( id )
  249. call WaitForCompletion()
  250. call CheckCurrentLine( 'tey' )
  251. call CheckCompletionItems( [ 'testy' ], 'word' )
  252. call feedkeys( "\<Esc>" )
  253. endfunction
  254. call setline(1, 'te' )
  255. call setpos( '.', [ 0, 1, 3 ] )
  256. call FeedAndCheckMain( "a\<C-Space>", 'Check1' )
  257. endfunction
  258. function! Test_Completion_FixIt()
  259. " There's a bug in clangd where you have to open a file which includes the
  260. " file you want to auto-include before it will actually auto-include that
  261. " file, auto_include_workaround #includes auto_include.h, so that clangd knows
  262. " about it
  263. call youcompleteme#test#setup#OpenFile(
  264. \ 'test/testdata/cpp/auto_include_workaround.cc', {} )
  265. call youcompleteme#test#setup#OpenFile(
  266. \ 'test/testdata/cpp/auto_include.cc', {} )
  267. function Check1( id )
  268. call WaitForCompletion()
  269. call CheckCurrentLine( 'do_a' )
  270. call CheckCompletionItems( [ 'do_a_thing(Thing thing)',
  271. \ 'do_another_thing()' ] )
  272. call FeedAndCheckAgain( "\<Tab>" , funcref( 'Check2' ) )
  273. endfunction
  274. function Check2( id )
  275. call WaitForCompletion()
  276. call CheckCurrentLine( 'do_a_thing' )
  277. call CheckCompletionItems( [ 'do_a_thing(Thing thing)',
  278. \ 'do_another_thing()' ] )
  279. call FeedAndCheckAgain( '(' , funcref( 'Check3' ) )
  280. endfunction
  281. function Check3( id )
  282. call WaitForAssert( {-> assert_false( pumvisible(), 'pumvisible()' ) } )
  283. call CheckCurrentLine( 'do_a_thing(' )
  284. call assert_equal( '#include "auto_include.h"', getline( 1 ) )
  285. call feedkeys( "\<Esc>" )
  286. endfunction
  287. call setpos( '.', [ 0, 3, 1 ] )
  288. call FeedAndCheckMain( "Ado_a\<C-Space>", funcref( 'Check1' ) )
  289. endfunction
  290. function! Test_Select_Next_Previous_InsertModeMapping()
  291. call youcompleteme#test#setup#OpenFile(
  292. \ '/third_party/ycmd/ycmd/tests/clangd/testdata/basic.cpp', {} )
  293. call setpos( '.', [ 0, 11, 6 ] )
  294. inoremap <C-n> <Down>
  295. " Required to trigger TextChangedI
  296. " https://github.com/vim/vim/issues/4665#event-2480928194
  297. call test_override( 'char_avail', 1 )
  298. function! Check( id )
  299. call WaitForCompletion()
  300. call CheckCurrentLine( ' foo.' )
  301. call CheckCompletionItems( [ 'c', 'x', 'y' ] )
  302. call FeedAndCheckAgain( "\<C-n>", funcref( 'Check2' ) )
  303. endfunction
  304. function! Check2( id )
  305. call WaitForCompletion()
  306. call CheckCurrentLine( ' foo.c' )
  307. call CheckCompletionItems( [ 'c', 'x', 'y' ] )
  308. call FeedAndCheckAgain( "\<C-n>", funcref( 'Check3' ) )
  309. endfunction
  310. function! Check3( id )
  311. call WaitForCompletion()
  312. call CheckCurrentLine( ' foo.x' )
  313. call CheckCompletionItems( [ 'c', 'x', 'y' ] )
  314. call FeedAndCheckAgain( "\<BS>a", funcref( 'Check4' ) )
  315. endfunction
  316. function! Check4( id )
  317. call CheckCurrentLine( ' foo.a' )
  318. call CheckCompletionItems( [] )
  319. call FeedAndCheckAgain( "\<C-n>", funcref( 'Check5' ) )
  320. endfunction
  321. function! Check5( id )
  322. " The last ctrl-n moved to the next line
  323. call CheckCurrentLine( '}' )
  324. call assert_equal( [ 0, 12, 2, 0 ], getpos( '.' ) )
  325. call CheckCompletionItems( [] )
  326. call feedkeys( "\<Esc>" )
  327. endfunction
  328. call FeedAndCheckMain( 'cl.', funcref( 'Check' ) )
  329. " Checks run in insert mode, then exit insert mode.
  330. call assert_false( pumvisible(), 'pumvisible()' )
  331. call test_override( 'ALL', 0 )
  332. iunmap <C-n>
  333. endfunction