omni_completer_test.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. # encoding: utf-8
  2. #
  3. # Copyright (C) 2016-2019 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', 'equal': 1 },
  53. { 'word': 'b', 'equal': 1 },
  54. { 'word': 'cdef', 'equal': 1 }
  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', 'equal': 1 },
  97. { 'word': 'b', 'equal': 1 },
  98. { 'word': 'cdef', 'equal': 1 }
  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', 'equal': 1 },
  123. { 'word': 'b', 'equal': 1 },
  124. { 'word': 'cdef', 'equal': 1 }
  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', 'equal': 1 },
  149. { 'word': 'b', 'equal': 1 },
  150. { 'word': 'cdef', 'equal': 1 }
  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', 'equal': 1 } ],
  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. 'equal': 1
  235. } ),
  236. 'completion_start_column': 6
  237. } )
  238. )
  239. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 0,
  240. 'g:ycm_semantic_triggers': TRIGGERS } )
  241. def OmniCompleter_GetCompletions_NoCache_ObjectList_test( ycm ):
  242. def Omnifunc( findstart, base ):
  243. if findstart:
  244. return 5
  245. return [
  246. {
  247. 'word': 'a',
  248. 'abbr': 'ABBR',
  249. 'menu': 'MENU',
  250. 'info': 'INFO',
  251. 'kind': 'K'
  252. },
  253. {
  254. 'word': 'test',
  255. 'abbr': 'ABBRTEST',
  256. 'menu': 'MENUTEST',
  257. 'info': 'INFOTEST',
  258. 'kind': 'T'
  259. }
  260. ]
  261. current_buffer = VimBuffer( 'buffer',
  262. contents = [ 'test.tt' ],
  263. filetype = FILETYPE,
  264. omnifunc = Omnifunc )
  265. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 7 ) ):
  266. ycm.SendCompletionRequest()
  267. # We don't filter the result - we expect the omnifunc to do that
  268. # based on the query we supplied (Note: that means no fuzzy matching!).
  269. assert_that(
  270. ycm.GetCompletionResponse(),
  271. has_entries( {
  272. 'completions': ToBytesOnPY2( [ {
  273. 'word' : 'a',
  274. 'abbr' : 'ABBR',
  275. 'menu' : 'MENU',
  276. 'info' : 'INFO',
  277. 'kind' : 'K',
  278. 'equal': 1
  279. }, {
  280. 'word' : 'test',
  281. 'abbr' : 'ABBRTEST',
  282. 'menu' : 'MENUTEST',
  283. 'info' : 'INFOTEST',
  284. 'kind' : 'T',
  285. 'equal': 1
  286. } ] ),
  287. 'completion_start_column': 6
  288. } )
  289. )
  290. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 1,
  291. 'g:ycm_semantic_triggers': TRIGGERS } )
  292. def OmniCompleter_GetCompletions_Cache_ObjectListObject_test( ycm ):
  293. def Omnifunc( findstart, base ):
  294. if findstart:
  295. return 5
  296. return { 'words': [
  297. {
  298. 'word': 'a',
  299. 'abbr': 'ABBR',
  300. 'menu': 'MENU',
  301. 'info': 'INFO',
  302. 'kind': 'K'
  303. },
  304. {
  305. 'word': 'test',
  306. 'abbr': 'ABBRTEST',
  307. 'menu': 'MENUTEST',
  308. 'info': 'INFOTEST',
  309. 'kind': 'T'
  310. }
  311. ] }
  312. current_buffer = VimBuffer( 'buffer',
  313. contents = [ 'test.tt' ],
  314. filetype = FILETYPE,
  315. omnifunc = Omnifunc )
  316. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 7 ) ):
  317. ycm.SendCompletionRequest()
  318. assert_that(
  319. ycm.GetCompletionResponse(),
  320. has_entries( {
  321. 'completions': ToBytesOnPY2( [ {
  322. 'word' : 'test',
  323. 'abbr' : 'ABBRTEST',
  324. 'menu' : 'MENUTEST',
  325. 'info' : 'INFOTEST',
  326. 'kind' : 'T',
  327. 'equal': 1
  328. } ] ),
  329. 'completion_start_column': 6
  330. } )
  331. )
  332. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 0,
  333. 'g:ycm_semantic_triggers': TRIGGERS } )
  334. def OmniCompleter_GetCompletions_NoCache_ObjectListObject_test( ycm ):
  335. def Omnifunc( findstart, base ):
  336. if findstart:
  337. return 5
  338. return { 'words': [
  339. {
  340. 'word': 'a',
  341. 'abbr': 'ABBR',
  342. 'menu': 'MENU',
  343. 'info': 'INFO',
  344. 'kind': 'K'
  345. },
  346. {
  347. 'word': 'test',
  348. 'abbr': 'ABBRTEST',
  349. 'menu': 'MENUTEST',
  350. 'info': 'INFOTEST',
  351. 'kind': 'T'
  352. }
  353. ] }
  354. current_buffer = VimBuffer( 'buffer',
  355. contents = [ 'test.tt' ],
  356. filetype = FILETYPE,
  357. omnifunc = Omnifunc )
  358. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 7 ) ):
  359. ycm.SendCompletionRequest()
  360. # No FilterAndSortCandidates for cache_omnifunc=0 (we expect the omnifunc
  361. # to do the filtering?)
  362. assert_that(
  363. ycm.GetCompletionResponse(),
  364. has_entries( {
  365. 'completions': ToBytesOnPY2( [ {
  366. 'word' : 'a',
  367. 'abbr' : 'ABBR',
  368. 'menu' : 'MENU',
  369. 'info' : 'INFO',
  370. 'kind' : 'K',
  371. 'equal': 1
  372. }, {
  373. 'word' : 'test',
  374. 'abbr' : 'ABBRTEST',
  375. 'menu' : 'MENUTEST',
  376. 'info' : 'INFOTEST',
  377. 'kind' : 'T',
  378. 'equal': 1
  379. } ] ),
  380. 'completion_start_column': 6
  381. } )
  382. )
  383. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 1,
  384. 'g:ycm_semantic_triggers': TRIGGERS } )
  385. def OmniCompleter_GetCompletions_Cache_List_Unicode_test( ycm ):
  386. def Omnifunc( findstart, base ):
  387. if findstart:
  388. return 12
  389. return [ '†est', 'å_unicode_identifier', 'πππππππ yummy πie' ]
  390. current_buffer = VimBuffer( 'buffer',
  391. contents = [ '†åsty_π.' ],
  392. filetype = FILETYPE,
  393. omnifunc = Omnifunc )
  394. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 12 ) ):
  395. ycm.SendCompletionRequest()
  396. assert_that(
  397. ycm.GetCompletionResponse(),
  398. has_entries( {
  399. 'completions': [
  400. { 'word': 'å_unicode_identifier', 'equal': 1 },
  401. { 'word': 'πππππππ yummy πie', 'equal': 1 },
  402. { 'word': '†est', 'equal': 1 }
  403. ],
  404. 'completion_start_column': 13
  405. } )
  406. )
  407. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 0,
  408. 'g:ycm_semantic_triggers': TRIGGERS } )
  409. def OmniCompleter_GetCompletions_NoCache_List_Unicode_test( ycm ):
  410. def Omnifunc( findstart, base ):
  411. if findstart:
  412. return 12
  413. return [ '†est', 'å_unicode_identifier', 'πππππππ yummy πie' ]
  414. current_buffer = VimBuffer( 'buffer',
  415. contents = [ '†åsty_π.' ],
  416. filetype = FILETYPE,
  417. omnifunc = Omnifunc )
  418. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 12 ) ):
  419. ycm.SendCompletionRequest()
  420. assert_that(
  421. ycm.GetCompletionResponse(),
  422. has_entries( {
  423. 'completions': ToBytesOnPY2( [
  424. { 'word': '†est', 'equal': 1 },
  425. { 'word': 'å_unicode_identifier', 'equal': 1 },
  426. { 'word': 'πππππππ yummy πie', 'equal': 1 }
  427. ] ),
  428. 'completion_start_column': 13
  429. } )
  430. )
  431. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 1,
  432. 'g:ycm_semantic_triggers': TRIGGERS } )
  433. def OmniCompleter_GetCompletions_Cache_List_Filter_Unicode_test( ycm ):
  434. def Omnifunc( findstart, base ):
  435. if findstart:
  436. return 12
  437. return [ '†est', 'å_unicode_identifier', 'πππππππ yummy πie' ]
  438. current_buffer = VimBuffer( 'buffer',
  439. contents = [ '†åsty_π.ππ' ],
  440. filetype = FILETYPE,
  441. omnifunc = Omnifunc )
  442. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 17 ) ):
  443. ycm.SendCompletionRequest()
  444. assert_that(
  445. ycm.GetCompletionResponse(),
  446. has_entries( {
  447. 'completions': [ { 'word': 'πππππππ yummy πie', 'equal': 1 } ],
  448. 'completion_start_column': 13
  449. } )
  450. )
  451. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 0,
  452. 'g:ycm_semantic_triggers': TRIGGERS } )
  453. def OmniCompleter_GetCompletions_NoCache_List_Filter_Unicode_test( ycm ):
  454. def Omnifunc( findstart, base ):
  455. if findstart:
  456. return 12
  457. return [ 'πππππππ yummy πie' ]
  458. current_buffer = VimBuffer( 'buffer',
  459. contents = [ '†åsty_π.ππ' ],
  460. filetype = FILETYPE,
  461. omnifunc = Omnifunc )
  462. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 17 ) ):
  463. ycm.SendCompletionRequest()
  464. assert_that(
  465. ycm.GetCompletionResponse(),
  466. has_entries( {
  467. 'completions': ToBytesOnPY2(
  468. [ { 'word': 'πππππππ yummy πie', 'equal': 1 } ]
  469. ),
  470. 'completion_start_column': 13
  471. } )
  472. )
  473. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 1,
  474. 'g:ycm_semantic_triggers': TRIGGERS } )
  475. def OmniCompleter_GetCompletions_Cache_ObjectList_Unicode_test( ycm ):
  476. def Omnifunc( findstart, base ):
  477. if findstart:
  478. return 12
  479. return [
  480. {
  481. 'word': 'ålpha∫et',
  482. 'abbr': 'å∫∫®',
  483. 'menu': 'µ´~¨á',
  484. 'info': '^~fo',
  485. 'kind': '˚'
  486. },
  487. {
  488. 'word': 'π†´ß†π',
  489. 'abbr': 'ÅııÂʉÍÊ',
  490. 'menu': '˜‰ˆËʉÍÊ',
  491. 'info': 'ȈÏØʉÍÊ',
  492. 'kind': 'Ê'
  493. }
  494. ]
  495. current_buffer = VimBuffer( 'buffer',
  496. contents = [ '†åsty_π.ππ' ],
  497. filetype = FILETYPE,
  498. omnifunc = Omnifunc )
  499. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 17 ) ):
  500. ycm.SendCompletionRequest()
  501. assert_that(
  502. ycm.GetCompletionResponse(),
  503. has_entries( {
  504. 'completions': [ {
  505. 'word' : 'π†´ß†π',
  506. 'abbr' : 'ÅııÂʉÍÊ',
  507. 'menu' : '˜‰ˆËʉÍÊ',
  508. 'info' : 'ȈÏØʉÍÊ',
  509. 'kind' : 'Ê',
  510. 'equal': 1
  511. } ],
  512. 'completion_start_column': 13
  513. } )
  514. )
  515. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 1,
  516. 'g:ycm_semantic_triggers': TRIGGERS } )
  517. def OmniCompleter_GetCompletions_Cache_ObjectListObject_Unicode_test( ycm ):
  518. def Omnifunc( findstart, base ):
  519. if findstart:
  520. return 12
  521. return {
  522. 'words': [
  523. {
  524. 'word': 'ålpha∫et',
  525. 'abbr': 'å∫∫®',
  526. 'menu': 'µ´~¨á',
  527. 'info': '^~fo',
  528. 'kind': '˚'
  529. },
  530. {
  531. 'word': 'π†´ß†π',
  532. 'abbr': 'ÅııÂʉÍÊ',
  533. 'menu': '˜‰ˆËʉÍÊ',
  534. 'info': 'ȈÏØʉÍÊ',
  535. 'kind': 'Ê'
  536. },
  537. {
  538. 'word': 'test',
  539. 'abbr': 'ÅııÂʉÍÊ',
  540. 'menu': '˜‰ˆËʉÍÊ',
  541. 'info': 'ȈÏØʉÍÊ',
  542. 'kind': 'Ê'
  543. }
  544. ]
  545. }
  546. current_buffer = VimBuffer( 'buffer',
  547. contents = [ '†åsty_π.t' ],
  548. filetype = FILETYPE,
  549. omnifunc = Omnifunc )
  550. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 13 ) ):
  551. ycm.SendCompletionRequest()
  552. assert_that(
  553. ycm.GetCompletionResponse(),
  554. has_entries( {
  555. 'completions': contains( {
  556. 'word' : 'test',
  557. 'abbr' : 'ÅııÂʉÍÊ',
  558. 'menu' : '˜‰ˆËʉÍÊ',
  559. 'info' : 'ȈÏØʉÍÊ',
  560. 'kind' : 'Ê',
  561. 'equal': 1
  562. }, {
  563. 'word' : 'ålpha∫et',
  564. 'abbr' : 'å∫∫®',
  565. 'menu' : 'µ´~¨á',
  566. 'info' : '^~fo',
  567. 'kind' : '˚',
  568. 'equal': 1
  569. } ),
  570. 'completion_start_column': 13
  571. } )
  572. )
  573. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 1,
  574. 'g:ycm_semantic_triggers': TRIGGERS } )
  575. def OmniCompleter_GetCompletions_RestoreCursorPositionAfterOmnifuncCall_test(
  576. ycm ):
  577. # This omnifunc moves the cursor to the test definition like
  578. # ccomplete#Complete would.
  579. def Omnifunc( findstart, base ):
  580. vimsupport.SetCurrentLineAndColumn( 0, 0 )
  581. if findstart:
  582. return 5
  583. return [ 'length' ]
  584. current_buffer = VimBuffer( 'buffer',
  585. contents = [ 'String test',
  586. '',
  587. 'test.' ],
  588. filetype = FILETYPE,
  589. omnifunc = Omnifunc )
  590. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 3, 5 ) ):
  591. ycm.SendCompletionRequest()
  592. assert_that(
  593. vimsupport.CurrentLineAndColumn(),
  594. contains( 2, 5 )
  595. )
  596. assert_that(
  597. ycm.GetCompletionResponse(),
  598. has_entries( {
  599. 'completions': ToBytesOnPY2( [ { 'word': 'length', 'equal': 1 } ] ),
  600. 'completion_start_column': 6
  601. } )
  602. )
  603. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 1,
  604. 'g:ycm_semantic_triggers': TRIGGERS } )
  605. def OmniCompleter_GetCompletions_MoveCursorPositionAtStartColumn_test( ycm ):
  606. # This omnifunc relies on the cursor being moved at the start column when
  607. # called the second time like LanguageClient#complete from the
  608. # LanguageClient-neovim plugin.
  609. def Omnifunc( findstart, base ):
  610. if findstart:
  611. return 5
  612. if vimsupport.CurrentColumn() == 5:
  613. return [ 'length' ]
  614. return []
  615. current_buffer = VimBuffer( 'buffer',
  616. contents = [ 'String test',
  617. '',
  618. 'test.le' ],
  619. filetype = FILETYPE,
  620. omnifunc = Omnifunc )
  621. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 3, 7 ) ):
  622. ycm.SendCompletionRequest()
  623. assert_that(
  624. vimsupport.CurrentLineAndColumn(),
  625. contains( 2, 7 )
  626. )
  627. assert_that(
  628. ycm.GetCompletionResponse(),
  629. has_entries( {
  630. 'completions': ToBytesOnPY2( [ { 'word': 'length', 'equal': 1 } ] ),
  631. 'completion_start_column': 6
  632. } )
  633. )
  634. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 1 } )
  635. def StartColumnCompliance( ycm,
  636. omnifunc_start_column,
  637. ycm_completions,
  638. ycm_start_column ):
  639. def Omnifunc( findstart, base ):
  640. if findstart:
  641. return omnifunc_start_column
  642. return [ 'foo' ]
  643. current_buffer = VimBuffer( 'buffer',
  644. contents = [ 'fo' ],
  645. filetype = FILETYPE,
  646. omnifunc = Omnifunc )
  647. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 2 ) ):
  648. ycm.SendCompletionRequest( force_semantic = True )
  649. assert_that(
  650. ycm.GetCompletionResponse(),
  651. has_entries( {
  652. 'completions': ToBytesOnPY2( ycm_completions ),
  653. 'completion_start_column': ycm_start_column
  654. } )
  655. )
  656. def OmniCompleter_GetCompletions_StartColumnCompliance_test():
  657. yield StartColumnCompliance, -4, [ { 'word': 'foo', 'equal': 1 } ], 3
  658. yield StartColumnCompliance, -3, [], 1
  659. yield StartColumnCompliance, -2, [], 1
  660. yield StartColumnCompliance, -1, [ { 'word': 'foo', 'equal': 1 } ], 3
  661. yield StartColumnCompliance, 0, [ { 'word': 'foo', 'equal': 1 } ], 1
  662. yield StartColumnCompliance, 1, [ { 'word': 'foo', 'equal': 1 } ], 2
  663. yield StartColumnCompliance, 2, [ { 'word': 'foo', 'equal': 1 } ], 3
  664. yield StartColumnCompliance, 3, [ { 'word': 'foo', 'equal': 1 } ], 3
  665. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 0,
  666. 'g:ycm_semantic_triggers': TRIGGERS } )
  667. def OmniCompleter_GetCompletions_NoCache_NoSemanticTrigger_test( ycm ):
  668. def Omnifunc( findstart, base ):
  669. if findstart:
  670. return 0
  671. return [ 'test' ]
  672. current_buffer = VimBuffer( 'buffer',
  673. contents = [ 'te' ],
  674. filetype = FILETYPE,
  675. omnifunc = Omnifunc )
  676. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 3 ) ):
  677. ycm.SendCompletionRequest()
  678. assert_that(
  679. ycm.GetCompletionResponse(),
  680. has_entries( {
  681. 'completions': empty(),
  682. 'completion_start_column': 1
  683. } )
  684. )
  685. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 0,
  686. 'g:ycm_semantic_triggers': TRIGGERS } )
  687. def OmniCompleter_GetCompletions_NoCache_ForceSemantic_test( ycm ):
  688. def Omnifunc( findstart, base ):
  689. if findstart:
  690. return 0
  691. return [ 'test' ]
  692. current_buffer = VimBuffer( 'buffer',
  693. contents = [ 'te' ],
  694. filetype = FILETYPE,
  695. omnifunc = Omnifunc )
  696. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 3 ) ):
  697. ycm.SendCompletionRequest( force_semantic = True )
  698. assert_that(
  699. ycm.GetCompletionResponse(),
  700. has_entries( {
  701. 'completions': ToBytesOnPY2( [ { 'word': 'test', 'equal': 1 } ] ),
  702. 'completion_start_column': 1
  703. } )
  704. )
  705. @YouCompleteMeInstance( { 'g:ycm_cache_omnifunc': 1,
  706. 'g:ycm_semantic_triggers': TRIGGERS } )
  707. def OmniCompleter_GetCompletions_ConvertStringsToDictionaries_test( ycm ):
  708. def Omnifunc( findstart, base ):
  709. if findstart:
  710. return 5
  711. return [
  712. { 'word': 'a' },
  713. 'b'
  714. ]
  715. current_buffer = VimBuffer( 'buffer',
  716. contents = [ 'test.' ],
  717. filetype = FILETYPE,
  718. omnifunc = Omnifunc )
  719. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 7 ) ):
  720. ycm.SendCompletionRequest()
  721. assert_that(
  722. ycm.GetCompletionResponse(),
  723. has_entries( {
  724. 'completions': ToBytesOnPY2( [
  725. { 'word': 'a', 'equal': 1 },
  726. { 'word': 'b', 'equal': 1 }
  727. ] ),
  728. 'completion_start_column': 6
  729. } )
  730. )
  731. @YouCompleteMeInstance( {
  732. 'g:ycm_cache_omnifunc': 0,
  733. 'g:ycm_filetype_specific_completion_to_disable': { FILETYPE: 1 },
  734. 'g:ycm_semantic_triggers': TRIGGERS } )
  735. def OmniCompleter_GetCompletions_FiletypeDisabled_SemanticTrigger_test( ycm ):
  736. def Omnifunc( findstart, base ):
  737. if findstart:
  738. return 5
  739. return [ 'a', 'b', 'cdef' ]
  740. current_buffer = VimBuffer( 'buffer',
  741. contents = [ 'test.' ],
  742. filetype = FILETYPE,
  743. omnifunc = Omnifunc )
  744. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 6 ) ):
  745. ycm.SendCompletionRequest()
  746. assert_that(
  747. ycm.GetCompletionResponse(),
  748. has_entries( {
  749. 'completions': empty(),
  750. 'completion_start_column': 6
  751. } )
  752. )
  753. @YouCompleteMeInstance( {
  754. 'g:ycm_cache_omnifunc': 0,
  755. 'g:ycm_filetype_specific_completion_to_disable': { '*': 1 },
  756. 'g:ycm_semantic_triggers': TRIGGERS } )
  757. def OmniCompleter_GetCompletions_AllFiletypesDisabled_SemanticTrigger_test(
  758. ycm ):
  759. def Omnifunc( findstart, base ):
  760. if findstart:
  761. return 5
  762. return [ 'a', 'b', 'cdef' ]
  763. current_buffer = VimBuffer( 'buffer',
  764. contents = [ 'test.' ],
  765. filetype = FILETYPE,
  766. omnifunc = Omnifunc )
  767. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 6 ) ):
  768. ycm.SendCompletionRequest()
  769. assert_that(
  770. ycm.GetCompletionResponse(),
  771. has_entries( {
  772. 'completions': empty(),
  773. 'completion_start_column': 6
  774. } )
  775. )
  776. @YouCompleteMeInstance( {
  777. 'g:ycm_cache_omnifunc': 0,
  778. 'g:ycm_filetype_specific_completion_to_disable': { FILETYPE: 1 },
  779. 'g:ycm_semantic_triggers': TRIGGERS } )
  780. def OmniCompleter_GetCompletions_FiletypeDisabled_ForceSemantic_test( ycm ):
  781. def Omnifunc( findstart, base ):
  782. if findstart:
  783. return 5
  784. return [ 'a', 'b', 'cdef' ]
  785. current_buffer = VimBuffer( 'buffer',
  786. contents = [ 'test.' ],
  787. filetype = FILETYPE,
  788. omnifunc = Omnifunc )
  789. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 6 ) ):
  790. ycm.SendCompletionRequest( force_semantic = True )
  791. assert_that(
  792. ycm.GetCompletionResponse(),
  793. has_entries( {
  794. 'completions': ToBytesOnPY2( [
  795. { 'word': 'a', 'equal': 1 },
  796. { 'word': 'b', 'equal': 1 },
  797. { 'word': 'cdef', 'equal': 1 }
  798. ] ),
  799. 'completion_start_column': 6
  800. } )
  801. )
  802. @YouCompleteMeInstance( {
  803. 'g:ycm_cache_omnifunc': 0,
  804. 'g:ycm_filetype_specific_completion_to_disable': { '*': 1 },
  805. 'g:ycm_semantic_triggers': TRIGGERS } )
  806. def OmniCompleter_GetCompletions_AllFiletypesDisabled_ForceSemantic_test( ycm ):
  807. def Omnifunc( findstart, base ):
  808. if findstart:
  809. return 5
  810. return [ 'a', 'b', 'cdef' ]
  811. current_buffer = VimBuffer( 'buffer',
  812. contents = [ 'test.' ],
  813. filetype = FILETYPE,
  814. omnifunc = Omnifunc )
  815. with MockVimBuffers( [ current_buffer ], [ current_buffer ], ( 1, 6 ) ):
  816. ycm.SendCompletionRequest( force_semantic = True )
  817. assert_that(
  818. ycm.GetCompletionResponse(),
  819. has_entries( {
  820. 'completions': ToBytesOnPY2( [
  821. { 'word': 'a', 'equal': 1 },
  822. { 'word': 'b', 'equal': 1 },
  823. { 'word': 'cdef', 'equal': 1 }
  824. ] ),
  825. 'completion_start_column': 6
  826. } )
  827. )