omni_completer_test.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  1. # encoding: utf-8
  2. #
  3. # Copyright (C) 2016-2018 YouCompleteMe contributors
  4. #
  5. # This file is part of YouCompleteMe.
  6. #
  7. # YouCompleteMe is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # YouCompleteMe is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
  19. from __future__ import unicode_literals
  20. from __future__ import print_function
  21. from __future__ import division
  22. from __future__ import absolute_import
  23. # Not installing aliases from python-future; it's unreliable and slow.
  24. from builtins import * # noqa
  25. from hamcrest import assert_that, contains, empty, has_entries
  26. from ycm.tests.test_utils import ( MockVimBuffers, MockVimModule, ToBytesOnPY2,
  27. VimBuffer )
  28. MockVimModule()
  29. from ycm import vimsupport
  30. from ycm.tests import YouCompleteMeInstance
  31. FILETYPE = 'ycmtest'
  32. TRIGGERS = {
  33. 'ycmtest': [ '.' ]
  34. }
  35. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 1,
  36. 'g:ycm_semantic_triggers': TRIGGERS } )
  37. def OmniCompleter_GetCompletions_Cache_List_test( ycm ):
  38. def Omnifunc( findstart, base ):
  39. if findstart:
  40. return 5
  41. return [ 'a', 'b', 'cdef' ]
  42. current_buffer = VimBuffer( 'buffer',
  43. contents = [ 'test.' ],
  44. filetype = FILETYPE,
  45. omnifunc = Omnifunc )
  46. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 5 ) ):
  47. ycm.SendCompletionRequest()
  48. assert_that(
  49. ycm.GetCompletionResponse(),
  50. has_entries( {
  51. 'completions': ToBytesOnPY2( [
  52. { 'word': 'a' },
  53. { 'word': 'b' },
  54. { 'word': 'cdef' }
  55. ] ),
  56. 'completion_start_column': 6
  57. } )
  58. )
  59. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 1,
  60. 'g:ycm_semantic_triggers': TRIGGERS } )
  61. def OmniCompleter_GetCompletions_Cache_ListFilter_test( ycm ):
  62. def Omnifunc( findstart, base ):
  63. if findstart:
  64. return 5
  65. return [ 'a', 'b', 'cdef' ]
  66. current_buffer = VimBuffer( 'buffer',
  67. contents = [ 'test.t' ],
  68. filetype = FILETYPE,
  69. omnifunc = Omnifunc )
  70. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 6 ) ):
  71. ycm.SendCompletionRequest()
  72. assert_that(
  73. ycm.GetCompletionResponse(),
  74. has_entries( {
  75. 'completions': empty(),
  76. 'completion_start_column': 6
  77. } )
  78. )
  79. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 0,
  80. 'g:ycm_semantic_triggers': TRIGGERS } )
  81. def OmniCompleter_GetCompletions_NoCache_List_test( ycm ):
  82. def Omnifunc( findstart, base ):
  83. if findstart:
  84. return 5
  85. return [ 'a', 'b', 'cdef' ]
  86. current_buffer = VimBuffer( 'buffer',
  87. contents = [ 'test.' ],
  88. filetype = FILETYPE,
  89. omnifunc = Omnifunc )
  90. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 5 ) ):
  91. ycm.SendCompletionRequest()
  92. assert_that(
  93. ycm.GetCompletionResponse(),
  94. has_entries( {
  95. 'completions': ToBytesOnPY2( [
  96. { 'word': 'a' },
  97. { 'word': 'b' },
  98. { 'word': 'cdef' }
  99. ] ),
  100. 'completion_start_column': 6
  101. } )
  102. )
  103. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 0,
  104. 'g:ycm_semantic_triggers': TRIGGERS } )
  105. def OmniCompleter_GetCompletions_NoCache_ListFilter_test( ycm ):
  106. def Omnifunc( findstart, base ):
  107. if findstart:
  108. return 5
  109. return [ 'a', 'b', 'cdef' ]
  110. current_buffer = VimBuffer( 'buffer',
  111. contents = [ 'test.t' ],
  112. filetype = FILETYPE,
  113. omnifunc = Omnifunc )
  114. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 6 ) ):
  115. ycm.SendCompletionRequest()
  116. # Actual result is that the results are not filtered, as we expect the
  117. # omnifunc or vim itself to do this filtering.
  118. assert_that(
  119. ycm.GetCompletionResponse(),
  120. has_entries( {
  121. 'completions': ToBytesOnPY2( [
  122. { 'word': 'a' },
  123. { 'word': 'b' },
  124. { 'word': 'cdef' }
  125. ] ),
  126. 'completion_start_column': 6
  127. } )
  128. )
  129. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 0,
  130. 'g:ycm_semantic_triggers': TRIGGERS } )
  131. def OmniCompleter_GetCompletions_NoCache_UseFindStart_test( ycm ):
  132. def Omnifunc( findstart, base ):
  133. if findstart:
  134. return 0
  135. return [ 'a', 'b', 'cdef' ]
  136. current_buffer = VimBuffer( 'buffer',
  137. contents = [ 'test.t' ],
  138. filetype = FILETYPE,
  139. omnifunc = Omnifunc )
  140. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 6 ) ):
  141. ycm.SendCompletionRequest()
  142. # Actual result is that the results are not filtered, as we expect the
  143. # omnifunc or vim itself to do this filtering.
  144. assert_that(
  145. ycm.GetCompletionResponse(),
  146. has_entries( {
  147. 'completions': ToBytesOnPY2( [
  148. { 'word': 'a' },
  149. { 'word': 'b' },
  150. { 'word': 'cdef' }
  151. ] ),
  152. 'completion_start_column': 1
  153. } )
  154. )
  155. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 1,
  156. 'g:ycm_semantic_triggers': TRIGGERS } )
  157. def OmniCompleter_GetCompletions_Cache_UseFindStart_test( ycm ):
  158. def Omnifunc( findstart, base ):
  159. if findstart:
  160. return 0
  161. return [ 'a', 'b', 'cdef' ]
  162. current_buffer = VimBuffer( 'buffer',
  163. contents = [ 'test.t' ],
  164. filetype = FILETYPE,
  165. omnifunc = Omnifunc )
  166. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 6 ) ):
  167. ycm.SendCompletionRequest()
  168. # There are no results because the query 'test.t' doesn't match any
  169. # candidate (and cache_omnifunc=1, so we FilterAndSortCandidates).
  170. assert_that(
  171. ycm.GetCompletionResponse(),
  172. has_entries( {
  173. 'completions': empty(),
  174. 'completion_start_column': 1
  175. } )
  176. )
  177. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 1,
  178. 'g:ycm_semantic_triggers': TRIGGERS } )
  179. def OmniCompleter_GetCompletions_Cache_Object_test( ycm ):
  180. def Omnifunc( findstart, base ):
  181. if findstart:
  182. return 5
  183. return { 'words': [ 'a', 'b', 'CDtEF' ] }
  184. current_buffer = VimBuffer( 'buffer',
  185. contents = [ 'test.t' ],
  186. filetype = FILETYPE,
  187. omnifunc = Omnifunc )
  188. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 6 ) ):
  189. ycm.SendCompletionRequest()
  190. assert_that(
  191. ycm.GetCompletionResponse(),
  192. has_entries( {
  193. 'completions': [ { 'word': 'CDtEF' } ],
  194. 'completion_start_column': 6
  195. } )
  196. )
  197. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 1,
  198. 'g:ycm_semantic_triggers': TRIGGERS } )
  199. def OmniCompleter_GetCompletions_Cache_ObjectList_test( ycm ):
  200. def Omnifunc( findstart, base ):
  201. if findstart:
  202. return 5
  203. return [
  204. {
  205. 'word': 'a',
  206. 'abbr': 'ABBR',
  207. 'menu': 'MENU',
  208. 'info': 'INFO',
  209. 'kind': 'K'
  210. },
  211. {
  212. 'word': 'test',
  213. 'abbr': 'ABBRTEST',
  214. 'menu': 'MENUTEST',
  215. 'info': 'INFOTEST',
  216. 'kind': 'T'
  217. }
  218. ]
  219. current_buffer = VimBuffer( 'buffer',
  220. contents = [ 'test.tt' ],
  221. filetype = FILETYPE,
  222. omnifunc = Omnifunc )
  223. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 7 ) ):
  224. ycm.SendCompletionRequest()
  225. assert_that(
  226. ycm.GetCompletionResponse(),
  227. has_entries( {
  228. 'completions': contains( {
  229. 'word': 'test',
  230. 'abbr': 'ABBRTEST',
  231. 'menu': 'MENUTEST',
  232. 'info': 'INFOTEST',
  233. 'kind': 'T'
  234. } ),
  235. 'completion_start_column': 6
  236. } )
  237. )
  238. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 0,
  239. 'g:ycm_semantic_triggers': TRIGGERS } )
  240. def OmniCompleter_GetCompletions_NoCache_ObjectList_test( ycm ):
  241. def Omnifunc( findstart, base ):
  242. if findstart:
  243. return 5
  244. return [
  245. {
  246. 'word': 'a',
  247. 'abbr': 'ABBR',
  248. 'menu': 'MENU',
  249. 'info': 'INFO',
  250. 'kind': 'K'
  251. },
  252. {
  253. 'word': 'test',
  254. 'abbr': 'ABBRTEST',
  255. 'menu': 'MENUTEST',
  256. 'info': 'INFOTEST',
  257. 'kind': 'T'
  258. }
  259. ]
  260. current_buffer = VimBuffer( 'buffer',
  261. contents = [ 'test.tt' ],
  262. filetype = FILETYPE,
  263. omnifunc = Omnifunc )
  264. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 7 ) ):
  265. ycm.SendCompletionRequest()
  266. # We don't filter the result - we expect the omnifunc to do that
  267. # based on the query we supplied (Note: that means no fuzzy matching!).
  268. assert_that(
  269. ycm.GetCompletionResponse(),
  270. has_entries( {
  271. 'completions': ToBytesOnPY2( [ {
  272. 'word': 'a',
  273. 'abbr': 'ABBR',
  274. 'menu': 'MENU',
  275. 'info': 'INFO',
  276. 'kind': 'K'
  277. }, {
  278. 'word': 'test',
  279. 'abbr': 'ABBRTEST',
  280. 'menu': 'MENUTEST',
  281. 'info': 'INFOTEST',
  282. 'kind': 'T'
  283. } ] ),
  284. 'completion_start_column': 6
  285. } )
  286. )
  287. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 1,
  288. 'g:ycm_semantic_triggers': TRIGGERS } )
  289. def OmniCompleter_GetCompletions_Cache_ObjectListObject_test( ycm ):
  290. def Omnifunc( findstart, base ):
  291. if findstart:
  292. return 5
  293. return { 'words': [
  294. {
  295. 'word': 'a',
  296. 'abbr': 'ABBR',
  297. 'menu': 'MENU',
  298. 'info': 'INFO',
  299. 'kind': 'K'
  300. },
  301. {
  302. 'word': 'test',
  303. 'abbr': 'ABBRTEST',
  304. 'menu': 'MENUTEST',
  305. 'info': 'INFOTEST',
  306. 'kind': 'T'
  307. }
  308. ] }
  309. current_buffer = VimBuffer( 'buffer',
  310. contents = [ 'test.tt' ],
  311. filetype = FILETYPE,
  312. omnifunc = Omnifunc )
  313. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 7 ) ):
  314. ycm.SendCompletionRequest()
  315. assert_that(
  316. ycm.GetCompletionResponse(),
  317. has_entries( {
  318. 'completions': ToBytesOnPY2( [ {
  319. 'word': 'test',
  320. 'abbr': 'ABBRTEST',
  321. 'menu': 'MENUTEST',
  322. 'info': 'INFOTEST',
  323. 'kind': 'T'
  324. } ] ),
  325. 'completion_start_column': 6
  326. } )
  327. )
  328. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 0,
  329. 'g:ycm_semantic_triggers': TRIGGERS } )
  330. def OmniCompleter_GetCompletions_NoCache_ObjectListObject_test( ycm ):
  331. def Omnifunc( findstart, base ):
  332. if findstart:
  333. return 5
  334. return { 'words': [
  335. {
  336. 'word': 'a',
  337. 'abbr': 'ABBR',
  338. 'menu': 'MENU',
  339. 'info': 'INFO',
  340. 'kind': 'K'
  341. },
  342. {
  343. 'word': 'test',
  344. 'abbr': 'ABBRTEST',
  345. 'menu': 'MENUTEST',
  346. 'info': 'INFOTEST',
  347. 'kind': 'T'
  348. }
  349. ] }
  350. current_buffer = VimBuffer( 'buffer',
  351. contents = [ 'test.tt' ],
  352. filetype = FILETYPE,
  353. omnifunc = Omnifunc )
  354. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 7 ) ):
  355. ycm.SendCompletionRequest()
  356. # No FilterAndSortCandidates for cache_omnifunc=0 (we expect the omnifunc
  357. # to do the filtering?)
  358. assert_that(
  359. ycm.GetCompletionResponse(),
  360. has_entries( {
  361. 'completions': ToBytesOnPY2( [ {
  362. 'word': 'a',
  363. 'abbr': 'ABBR',
  364. 'menu': 'MENU',
  365. 'info': 'INFO',
  366. 'kind': 'K'
  367. }, {
  368. 'word': 'test',
  369. 'abbr': 'ABBRTEST',
  370. 'menu': 'MENUTEST',
  371. 'info': 'INFOTEST',
  372. 'kind': 'T'
  373. } ] ),
  374. 'completion_start_column': 6
  375. } )
  376. )
  377. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 1,
  378. 'g:ycm_semantic_triggers': TRIGGERS } )
  379. def OmniCompleter_GetCompletions_Cache_List_Unicode_test( ycm ):
  380. def Omnifunc( findstart, base ):
  381. if findstart:
  382. return 12
  383. return [ '†est', 'å_unicode_identifier', 'πππππππ yummy πie' ]
  384. current_buffer = VimBuffer( 'buffer',
  385. contents = [ '†åsty_π.' ],
  386. filetype = FILETYPE,
  387. omnifunc = Omnifunc )
  388. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 12 ) ):
  389. ycm.SendCompletionRequest()
  390. assert_that(
  391. ycm.GetCompletionResponse(),
  392. has_entries( {
  393. 'completions': [
  394. { 'word': 'å_unicode_identifier' },
  395. { 'word': 'πππππππ yummy πie' },
  396. { 'word': '†est' }
  397. ],
  398. 'completion_start_column': 13
  399. } )
  400. )
  401. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 0,
  402. 'g:ycm_semantic_triggers': TRIGGERS } )
  403. def OmniCompleter_GetCompletions_NoCache_List_Unicode_test( ycm ):
  404. def Omnifunc( findstart, base ):
  405. if findstart:
  406. return 12
  407. return [ '†est', 'å_unicode_identifier', 'πππππππ yummy πie' ]
  408. current_buffer = VimBuffer( 'buffer',
  409. contents = [ '†åsty_π.' ],
  410. filetype = FILETYPE,
  411. omnifunc = Omnifunc )
  412. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 12 ) ):
  413. ycm.SendCompletionRequest()
  414. assert_that(
  415. ycm.GetCompletionResponse(),
  416. has_entries( {
  417. 'completions': ToBytesOnPY2( [
  418. { 'word': '†est' },
  419. { 'word': 'å_unicode_identifier' },
  420. { 'word': 'πππππππ yummy πie' }
  421. ] ),
  422. 'completion_start_column': 13
  423. } )
  424. )
  425. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 1,
  426. 'g:ycm_semantic_triggers': TRIGGERS } )
  427. def OmniCompleter_GetCompletions_Cache_List_Filter_Unicode_test( ycm ):
  428. def Omnifunc( findstart, base ):
  429. if findstart:
  430. return 12
  431. return [ '†est', 'å_unicode_identifier', 'πππππππ yummy πie' ]
  432. current_buffer = VimBuffer( 'buffer',
  433. contents = [ '†åsty_π.ππ' ],
  434. filetype = FILETYPE,
  435. omnifunc = Omnifunc )
  436. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 17 ) ):
  437. ycm.SendCompletionRequest()
  438. assert_that(
  439. ycm.GetCompletionResponse(),
  440. has_entries( {
  441. 'completions': [ { 'word': 'πππππππ yummy πie' } ],
  442. 'completion_start_column': 13
  443. } )
  444. )
  445. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 0,
  446. 'g:ycm_semantic_triggers': TRIGGERS } )
  447. def OmniCompleter_GetCompletions_NoCache_List_Filter_Unicode_test( ycm ):
  448. def Omnifunc( findstart, base ):
  449. if findstart:
  450. return 12
  451. return [ 'πππππππ yummy πie' ]
  452. current_buffer = VimBuffer( 'buffer',
  453. contents = [ '†åsty_π.ππ' ],
  454. filetype = FILETYPE,
  455. omnifunc = Omnifunc )
  456. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 17 ) ):
  457. ycm.SendCompletionRequest()
  458. assert_that(
  459. ycm.GetCompletionResponse(),
  460. has_entries( {
  461. 'completions': ToBytesOnPY2( [ { 'word': 'πππππππ yummy πie' } ] ),
  462. 'completion_start_column': 13
  463. } )
  464. )
  465. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 1,
  466. 'g:ycm_semantic_triggers': TRIGGERS } )
  467. def OmniCompleter_GetCompletions_Cache_ObjectList_Unicode_test( ycm ):
  468. def Omnifunc( findstart, base ):
  469. if findstart:
  470. return 12
  471. return [
  472. {
  473. 'word': 'ålpha∫et',
  474. 'abbr': 'å∫∫®',
  475. 'menu': 'µ´~¨á',
  476. 'info': '^~fo',
  477. 'kind': '˚'
  478. },
  479. {
  480. 'word': 'π†´ß†π',
  481. 'abbr': 'ÅııÂʉÍÊ',
  482. 'menu': '˜‰ˆËʉÍÊ',
  483. 'info': 'ȈÏØʉÍÊ',
  484. 'kind': 'Ê'
  485. }
  486. ]
  487. current_buffer = VimBuffer( 'buffer',
  488. contents = [ '†åsty_π.ππ' ],
  489. filetype = FILETYPE,
  490. omnifunc = Omnifunc )
  491. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 17 ) ):
  492. ycm.SendCompletionRequest()
  493. assert_that(
  494. ycm.GetCompletionResponse(),
  495. has_entries( {
  496. 'completions': [ {
  497. 'word': 'π†´ß†π',
  498. 'abbr': 'ÅııÂʉÍÊ',
  499. 'menu': '˜‰ˆËʉÍÊ',
  500. 'info': 'ȈÏØʉÍÊ',
  501. 'kind': 'Ê'
  502. } ],
  503. 'completion_start_column': 13
  504. } )
  505. )
  506. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 1,
  507. 'g:ycm_semantic_triggers': TRIGGERS } )
  508. def OmniCompleter_GetCompletions_Cache_ObjectListObject_Unicode_test( ycm ):
  509. def Omnifunc( findstart, base ):
  510. if findstart:
  511. return 12
  512. return {
  513. 'words': [
  514. {
  515. 'word': 'ålpha∫et',
  516. 'abbr': 'å∫∫®',
  517. 'menu': 'µ´~¨á',
  518. 'info': '^~fo',
  519. 'kind': '˚'
  520. },
  521. {
  522. 'word': 'π†´ß†π',
  523. 'abbr': 'ÅııÂʉÍÊ',
  524. 'menu': '˜‰ˆËʉÍÊ',
  525. 'info': 'ȈÏØʉÍÊ',
  526. 'kind': 'Ê'
  527. },
  528. {
  529. 'word': 'test',
  530. 'abbr': 'ÅııÂʉÍÊ',
  531. 'menu': '˜‰ˆËʉÍÊ',
  532. 'info': 'ȈÏØʉÍÊ',
  533. 'kind': 'Ê'
  534. }
  535. ]
  536. }
  537. current_buffer = VimBuffer( 'buffer',
  538. contents = [ '†åsty_π.t' ],
  539. filetype = FILETYPE,
  540. omnifunc = Omnifunc )
  541. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 13 ) ):
  542. ycm.SendCompletionRequest()
  543. assert_that(
  544. ycm.GetCompletionResponse(),
  545. has_entries( {
  546. 'completions': contains( {
  547. 'word': 'test',
  548. 'abbr': 'ÅııÂʉÍÊ',
  549. 'menu': '˜‰ˆËʉÍÊ',
  550. 'info': 'ȈÏØʉÍÊ',
  551. 'kind': 'Ê'
  552. }, {
  553. 'word': 'ålpha∫et',
  554. 'abbr': 'å∫∫®',
  555. 'menu': 'µ´~¨á',
  556. 'info': '^~fo',
  557. 'kind': '˚'
  558. } ),
  559. 'completion_start_column': 13
  560. } )
  561. )
  562. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 1,
  563. 'g:ycm_semantic_triggers': TRIGGERS } )
  564. def OmniCompleter_GetCompletions_RestoreCursorPositionAfterOmnifuncCall_test(
  565. ycm ):
  566. # This omnifunc moves the cursor to the test definition like
  567. # ccomplete#Complete would.
  568. def Omnifunc( findstart, base ):
  569. vimsupport.SetCurrentLineAndColumn( 0, 0 )
  570. if findstart:
  571. return 5
  572. return [ 'length' ]
  573. current_buffer = VimBuffer( 'buffer',
  574. contents = [ 'String test',
  575. '',
  576. 'test.' ],
  577. filetype = FILETYPE,
  578. omnifunc = Omnifunc )
  579. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 3, 5 ) ):
  580. ycm.SendCompletionRequest()
  581. assert_that(
  582. vimsupport.CurrentLineAndColumn(),
  583. contains( 2, 5 )
  584. )
  585. assert_that(
  586. ycm.GetCompletionResponse(),
  587. has_entries( {
  588. 'completions': ToBytesOnPY2( [ { 'word': 'length' } ] ),
  589. 'completion_start_column': 6
  590. } )
  591. )
  592. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 1,
  593. 'g:ycm_semantic_triggers': TRIGGERS } )
  594. def OmniCompleter_GetCompletions_MoveCursorPositionAtStartColumn_test( ycm ):
  595. # This omnifunc relies on the cursor being moved at the start column when
  596. # called the second time like LanguageClient#complete from the
  597. # LanguageClient-neovim plugin.
  598. def Omnifunc( findstart, base ):
  599. if findstart:
  600. return 5
  601. if vimsupport.CurrentColumn() == 5:
  602. return [ 'length' ]
  603. return []
  604. current_buffer = VimBuffer( 'buffer',
  605. contents = [ 'String test',
  606. '',
  607. 'test.le' ],
  608. filetype = FILETYPE,
  609. omnifunc = Omnifunc )
  610. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 3, 7 ) ):
  611. ycm.SendCompletionRequest()
  612. assert_that(
  613. vimsupport.CurrentLineAndColumn(),
  614. contains( 2, 7 )
  615. )
  616. assert_that(
  617. ycm.GetCompletionResponse(),
  618. has_entries( {
  619. 'completions': ToBytesOnPY2( [ { 'word': 'length' } ] ),
  620. 'completion_start_column': 6
  621. } )
  622. )
  623. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 1 } )
  624. def StartColumnCompliance( ycm,
  625. omnifunc_start_column,
  626. ycm_completions,
  627. ycm_start_column ):
  628. def Omnifunc( findstart, base ):
  629. if findstart:
  630. return omnifunc_start_column
  631. return [ 'foo' ]
  632. current_buffer = VimBuffer( 'buffer',
  633. contents = [ 'fo' ],
  634. filetype = FILETYPE,
  635. omnifunc = Omnifunc )
  636. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 2 ) ):
  637. ycm.SendCompletionRequest( force_semantic = True )
  638. assert_that(
  639. ycm.GetCompletionResponse(),
  640. has_entries( {
  641. 'completions': ToBytesOnPY2( ycm_completions ),
  642. 'completion_start_column': ycm_start_column
  643. } )
  644. )
  645. def OmniCompleter_GetCompletions_StartColumnCompliance_test():
  646. yield StartColumnCompliance, -4, [ { 'word': 'foo' } ], 3
  647. yield StartColumnCompliance, -3, [], 1
  648. yield StartColumnCompliance, -2, [], 1
  649. yield StartColumnCompliance, -1, [ { 'word': 'foo' } ], 3
  650. yield StartColumnCompliance, 0, [ { 'word': 'foo' } ], 1
  651. yield StartColumnCompliance, 1, [ { 'word': 'foo' } ], 2
  652. yield StartColumnCompliance, 2, [ { 'word': 'foo' } ], 3
  653. yield StartColumnCompliance, 3, [ { 'word': 'foo' } ], 3
  654. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 0,
  655. 'g:ycm_semantic_triggers': TRIGGERS } )
  656. def OmniCompleter_GetCompletions_NoCache_NoSemanticTrigger_test( ycm ):
  657. def Omnifunc( findstart, base ):
  658. if findstart:
  659. return 0
  660. return [ 'test' ]
  661. current_buffer = VimBuffer( 'buffer',
  662. contents = [ 'te' ],
  663. filetype = FILETYPE,
  664. omnifunc = Omnifunc )
  665. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 3 ) ):
  666. ycm.SendCompletionRequest()
  667. assert_that(
  668. ycm.GetCompletionResponse(),
  669. has_entries( {
  670. 'completions': empty(),
  671. 'completion_start_column': 1
  672. } )
  673. )
  674. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 0,
  675. 'g:ycm_semantic_triggers': TRIGGERS } )
  676. def OmniCompleter_GetCompletions_NoCache_ForceSemantic_test( ycm ):
  677. def Omnifunc( findstart, base ):
  678. if findstart:
  679. return 0
  680. return [ 'test' ]
  681. current_buffer = VimBuffer( 'buffer',
  682. contents = [ 'te' ],
  683. filetype = FILETYPE,
  684. omnifunc = Omnifunc )
  685. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 3 ) ):
  686. ycm.SendCompletionRequest( force_semantic = True )
  687. assert_that(
  688. ycm.GetCompletionResponse(),
  689. has_entries( {
  690. 'completions': ToBytesOnPY2( [ { 'word': 'test' } ] ),
  691. 'completion_start_column': 1
  692. } )
  693. )
  694. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 1,
  695. 'g:ycm_semantic_triggers': TRIGGERS } )
  696. def OmniCompleter_GetCompletions_ConvertStringsToDictionaries_test( ycm ):
  697. def Omnifunc( findstart, base ):
  698. if findstart:
  699. return 5
  700. return [
  701. { 'word': 'a' },
  702. 'b'
  703. ]
  704. current_buffer = VimBuffer( 'buffer',
  705. contents = [ 'test.' ],
  706. filetype = FILETYPE,
  707. omnifunc = Omnifunc )
  708. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 7 ) ):
  709. ycm.SendCompletionRequest()
  710. assert_that(
  711. ycm.GetCompletionResponse(),
  712. has_entries( {
  713. 'completions': ToBytesOnPY2( [
  714. { 'word': 'a' },
  715. { 'word': 'b' }
  716. ] ),
  717. 'completion_start_column': 6
  718. } )
  719. )
  720. @YouCompleteMeInstance( {
  721. 'g:ycm_cache_omnifunc': 0,
  722. 'g:ycm_filetype_specific_completion_to_disable': { FILETYPE: 1 },
  723. 'g:ycm_semantic_triggers': TRIGGERS } )
  724. def OmniCompleter_GetCompletions_FiletypeDisabled_SemanticTrigger_test( ycm ):
  725. def Omnifunc( findstart, base ):
  726. if findstart:
  727. return 5
  728. return [ 'a', 'b', 'cdef' ]
  729. current_buffer = VimBuffer( 'buffer',
  730. contents = [ 'test.' ],
  731. filetype = FILETYPE,
  732. omnifunc = Omnifunc )
  733. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 6 ) ):
  734. ycm.SendCompletionRequest()
  735. assert_that(
  736. ycm.GetCompletionResponse(),
  737. has_entries( {
  738. 'completions': empty(),
  739. 'completion_start_column': 6
  740. } )
  741. )
  742. @YouCompleteMeInstance( {
  743. 'g:ycm_cache_omnifunc': 0,
  744. 'g:ycm_filetype_specific_completion_to_disable': { '*': 1 },
  745. 'g:ycm_semantic_triggers': TRIGGERS } )
  746. def OmniCompleter_GetCompletions_AllFiletypesDisabled_SemanticTrigger_test(
  747. ycm ):
  748. def Omnifunc( findstart, base ):
  749. if findstart:
  750. return 5
  751. return [ 'a', 'b', 'cdef' ]
  752. current_buffer = VimBuffer( 'buffer',
  753. contents = [ 'test.' ],
  754. filetype = FILETYPE,
  755. omnifunc = Omnifunc )
  756. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 6 ) ):
  757. ycm.SendCompletionRequest()
  758. assert_that(
  759. ycm.GetCompletionResponse(),
  760. has_entries( {
  761. 'completions': empty(),
  762. 'completion_start_column': 6
  763. } )
  764. )
  765. @YouCompleteMeInstance( {
  766. 'g:ycm_cache_omnifunc': 0,
  767. 'g:ycm_filetype_specific_completion_to_disable': { FILETYPE: 1 },
  768. 'g:ycm_semantic_triggers': TRIGGERS } )
  769. def OmniCompleter_GetCompletions_FiletypeDisabled_ForceSemantic_test( ycm ):
  770. def Omnifunc( findstart, base ):
  771. if findstart:
  772. return 5
  773. return [ 'a', 'b', 'cdef' ]
  774. current_buffer = VimBuffer( 'buffer',
  775. contents = [ 'test.' ],
  776. filetype = FILETYPE,
  777. omnifunc = Omnifunc )
  778. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 6 ) ):
  779. ycm.SendCompletionRequest( force_semantic = True )
  780. assert_that(
  781. ycm.GetCompletionResponse(),
  782. has_entries( {
  783. 'completions': ToBytesOnPY2( [
  784. { 'word': 'a' },
  785. { 'word': 'b' },
  786. { 'word': 'cdef' }
  787. ] ),
  788. 'completion_start_column': 6
  789. } )
  790. )
  791. @YouCompleteMeInstance( {
  792. 'g:ycm_cache_omnifunc': 0,
  793. 'g:ycm_filetype_specific_completion_to_disable': { '*': 1 },
  794. 'g:ycm_semantic_triggers': TRIGGERS } )
  795. def OmniCompleter_GetCompletions_AllFiletypesDisabled_ForceSemantic_test( ycm ):
  796. def Omnifunc( findstart, base ):
  797. if findstart:
  798. return 5
  799. return [ 'a', 'b', 'cdef' ]
  800. current_buffer = VimBuffer( 'buffer',
  801. contents = [ 'test.' ],
  802. filetype = FILETYPE,
  803. omnifunc = Omnifunc )
  804. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 6 ) ):
  805. ycm.SendCompletionRequest( force_semantic = True )
  806. assert_that(
  807. ycm.GetCompletionResponse(),
  808. has_entries( {
  809. 'completions': ToBytesOnPY2( [
  810. { 'word': 'a' },
  811. { 'word': 'b' },
  812. { 'word': 'cdef' }
  813. ] ),
  814. 'completion_start_column': 6
  815. } )
  816. )