vimsupport_test.py 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788
  1. # coding: utf-8
  2. #
  3. # Copyright (C) 2015-2016 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. # Intentionally not importing 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 ycm.tests import PathToTestFile
  26. from ycm.tests.test_utils import ( CurrentWorkingDirectory, ExtendedMock,
  27. MockVimBuffers, MockVimCommand,
  28. MockVimModule, VimBuffer, VimError )
  29. MockVimModule()
  30. from ycm import vimsupport
  31. from nose.tools import eq_
  32. from hamcrest import assert_that, calling, contains, equal_to, has_entry, raises
  33. from mock import MagicMock, call, patch
  34. from ycmd.utils import ToBytes
  35. import os
  36. import json
  37. @patch( 'vim.eval', new_callable = ExtendedMock )
  38. def SetLocationList_test( vim_eval ):
  39. diagnostics = [ {
  40. 'bufnr': 3,
  41. 'filename': 'some_filename',
  42. 'lnum': 5,
  43. 'col': 22,
  44. 'type': 'E',
  45. 'valid': 1
  46. } ]
  47. vimsupport.SetLocationList( diagnostics )
  48. vim_eval.assert_called_once_with(
  49. 'setloclist( 0, {0} )'.format( json.dumps( diagnostics ) ) )
  50. @patch( 'ycm.vimsupport.VariableExists', return_value = True )
  51. @patch( 'ycm.vimsupport.SetFittingHeightForCurrentWindow' )
  52. @patch( 'vim.command', new_callable = ExtendedMock )
  53. def OpenLocationList_test( vim_command, fitting_height, variable_exists ):
  54. vimsupport.OpenLocationList( focus = False, autoclose = True )
  55. vim_command.assert_has_exact_calls( [
  56. call( 'lopen' ),
  57. call( 'au WinLeave <buffer> q' ),
  58. call( 'doautocmd User YcmLocationOpened' ),
  59. call( 'silent! wincmd p' )
  60. ] )
  61. fitting_height.assert_called_once_with()
  62. variable_exists.assert_called_once_with( '#User#YcmLocationOpened' )
  63. @patch( 'ycm.vimsupport.GetIntValue', return_value = 120 )
  64. @patch( 'vim.command' )
  65. def SetFittingHeightForCurrentWindow_test( vim_command, *args ):
  66. # Create a buffer with one line that is longer than the window width.
  67. current_buffer = VimBuffer( 'buffer',
  68. contents = [ 'a' * 140 ] )
  69. with MockVimBuffers( [ current_buffer ], current_buffer ):
  70. vimsupport.SetFittingHeightForCurrentWindow()
  71. vim_command.assert_called_once_with( '2wincmd _' )
  72. def AssertBuffersAreEqualAsBytes( result_buffer, expected_buffer ):
  73. eq_( len( result_buffer ), len( expected_buffer ) )
  74. for result_line, expected_line in zip( result_buffer, expected_buffer ):
  75. eq_( ToBytes( result_line ), ToBytes( expected_line ) )
  76. def ReplaceChunk_SingleLine_Repl_1_test():
  77. # Replace with longer range
  78. result_buffer = [ 'This is a string' ]
  79. start, end = _BuildLocations( 1, 1, 1, 5 )
  80. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  81. end,
  82. 'How long',
  83. 0,
  84. 0,
  85. result_buffer )
  86. AssertBuffersAreEqualAsBytes( [ 'How long is a string' ], result_buffer )
  87. eq_( line_offset, 0 )
  88. eq_( char_offset, 4 )
  89. # and replace again, using delta
  90. start, end = _BuildLocations( 1, 10, 1, 11 )
  91. ( new_line_offset, new_char_offset ) = vimsupport.ReplaceChunk(
  92. start,
  93. end,
  94. ' piece of ',
  95. line_offset,
  96. char_offset,
  97. result_buffer )
  98. line_offset += new_line_offset
  99. char_offset += new_char_offset
  100. AssertBuffersAreEqualAsBytes( [ 'How long is a piece of string' ],
  101. result_buffer )
  102. eq_( new_line_offset, 0 )
  103. eq_( new_char_offset, 9 )
  104. eq_( line_offset, 0 )
  105. eq_( char_offset, 13 )
  106. # and once more, for luck
  107. start, end = _BuildLocations( 1, 11, 1, 17 )
  108. ( new_line_offset, new_char_offset ) = vimsupport.ReplaceChunk(
  109. start,
  110. end,
  111. 'pie',
  112. line_offset,
  113. char_offset,
  114. result_buffer )
  115. line_offset += new_line_offset
  116. char_offset += new_char_offset
  117. AssertBuffersAreEqualAsBytes( [ 'How long is a piece of pie' ],
  118. result_buffer )
  119. eq_( new_line_offset, 0 )
  120. eq_( new_char_offset, -3 )
  121. eq_( line_offset, 0 )
  122. eq_( char_offset, 10 )
  123. def ReplaceChunk_SingleLine_Repl_2_test():
  124. # Replace with shorter range
  125. result_buffer = [ 'This is a string' ]
  126. start, end = _BuildLocations( 1, 11, 1, 17 )
  127. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  128. end,
  129. 'test',
  130. 0,
  131. 0,
  132. result_buffer )
  133. AssertBuffersAreEqualAsBytes( [ 'This is a test' ], result_buffer )
  134. eq_( line_offset, 0 )
  135. eq_( char_offset, -2 )
  136. def ReplaceChunk_SingleLine_Repl_3_test():
  137. # Replace with equal range
  138. result_buffer = [ 'This is a string' ]
  139. start, end = _BuildLocations( 1, 6, 1, 8 )
  140. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  141. end,
  142. 'be',
  143. 0,
  144. 0,
  145. result_buffer )
  146. AssertBuffersAreEqualAsBytes( [ 'This be a string' ], result_buffer )
  147. eq_( line_offset, 0 )
  148. eq_( char_offset, 0 )
  149. def ReplaceChunk_SingleLine_Add_1_test():
  150. # Insert at start
  151. result_buffer = [ 'is a string' ]
  152. start, end = _BuildLocations( 1, 1, 1, 1 )
  153. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  154. end,
  155. 'This ',
  156. 0,
  157. 0,
  158. result_buffer )
  159. AssertBuffersAreEqualAsBytes( [ 'This is a string' ], result_buffer )
  160. eq_( line_offset, 0 )
  161. eq_( char_offset, 5 )
  162. def ReplaceChunk_SingleLine_Add_2_test():
  163. # Insert at end
  164. result_buffer = [ 'This is a ' ]
  165. start, end = _BuildLocations( 1, 11, 1, 11 )
  166. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  167. end,
  168. 'string',
  169. 0,
  170. 0,
  171. result_buffer )
  172. AssertBuffersAreEqualAsBytes( [ 'This is a string' ], result_buffer )
  173. eq_( line_offset, 0 )
  174. eq_( char_offset, 6 )
  175. def ReplaceChunk_SingleLine_Add_3_test():
  176. # Insert in the middle
  177. result_buffer = [ 'This is a string' ]
  178. start, end = _BuildLocations( 1, 8, 1, 8 )
  179. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  180. end,
  181. ' not',
  182. 0,
  183. 0,
  184. result_buffer )
  185. AssertBuffersAreEqualAsBytes( [ 'This is not a string' ], result_buffer )
  186. eq_( line_offset, 0 )
  187. eq_( char_offset, 4 )
  188. def ReplaceChunk_SingleLine_Del_1_test():
  189. # Delete from start
  190. result_buffer = [ 'This is a string' ]
  191. start, end = _BuildLocations( 1, 1, 1, 6 )
  192. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  193. end,
  194. '',
  195. 0,
  196. 0,
  197. result_buffer )
  198. AssertBuffersAreEqualAsBytes( [ 'is a string' ], result_buffer )
  199. eq_( line_offset, 0 )
  200. eq_( char_offset, -5 )
  201. def ReplaceChunk_SingleLine_Del_2_test():
  202. # Delete from end
  203. result_buffer = [ 'This is a string' ]
  204. start, end = _BuildLocations( 1, 10, 1, 18 )
  205. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  206. end,
  207. '',
  208. 0,
  209. 0,
  210. result_buffer )
  211. AssertBuffersAreEqualAsBytes( [ 'This is a' ], result_buffer )
  212. eq_( line_offset, 0 )
  213. eq_( char_offset, -8 )
  214. def ReplaceChunk_SingleLine_Del_3_test():
  215. # Delete from middle
  216. result_buffer = [ 'This is not a string' ]
  217. start, end = _BuildLocations( 1, 9, 1, 13 )
  218. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  219. end,
  220. '',
  221. 0,
  222. 0,
  223. result_buffer )
  224. AssertBuffersAreEqualAsBytes( [ 'This is a string' ], result_buffer )
  225. eq_( line_offset, 0 )
  226. eq_( char_offset, -4 )
  227. def ReplaceChunk_SingleLine_Unicode_ReplaceUnicodeChars_test():
  228. # Replace Unicode characters.
  229. result_buffer = [ 'This Uniçø∂‰ string is in the middle' ]
  230. start, end = _BuildLocations( 1, 6, 1, 20 )
  231. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  232. end,
  233. 'Unicode ',
  234. 0,
  235. 0,
  236. result_buffer )
  237. AssertBuffersAreEqualAsBytes( [ 'This Unicode string is in the middle' ],
  238. result_buffer )
  239. eq_( line_offset, 0 )
  240. eq_( char_offset, -6 )
  241. def ReplaceChunk_SingleLine_Unicode_ReplaceAfterUnicode_test():
  242. # Replace ASCII characters after Unicode characters in the line.
  243. result_buffer = [ 'This Uniçø∂‰ string is in the middle' ]
  244. start, end = _BuildLocations( 1, 30, 1, 43 )
  245. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  246. end,
  247. 'fåke',
  248. 0,
  249. 0,
  250. result_buffer )
  251. AssertBuffersAreEqualAsBytes( [ 'This Uniçø∂‰ string is fåke' ],
  252. result_buffer )
  253. eq_( line_offset, 0 )
  254. eq_( char_offset, -8 )
  255. def ReplaceChunk_SingleLine_Unicode_Grown_test():
  256. # Replace ASCII characters after Unicode characters in the line.
  257. result_buffer = [ 'a' ]
  258. start, end = _BuildLocations( 1, 1, 1, 2 )
  259. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  260. end,
  261. 'å',
  262. 0,
  263. 0,
  264. result_buffer )
  265. AssertBuffersAreEqualAsBytes( [ 'å' ], result_buffer )
  266. eq_( line_offset, 0 )
  267. eq_( char_offset, 1 ) # Note: byte difference (a = 1 byte, å = 2 bytes)
  268. def ReplaceChunk_RemoveSingleLine_test():
  269. result_buffer = [ 'aAa',
  270. 'aBa',
  271. 'aCa' ]
  272. start, end = _BuildLocations( 2, 1, 3, 1 )
  273. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, '',
  274. 0, 0, result_buffer )
  275. # First line is not affected.
  276. expected_buffer = [ 'aAa',
  277. 'aCa' ]
  278. AssertBuffersAreEqualAsBytes( expected_buffer, result_buffer )
  279. eq_( line_offset, -1 )
  280. eq_( char_offset, 0 )
  281. def ReplaceChunk_SingleToMultipleLines_test():
  282. result_buffer = [ 'aAa',
  283. 'aBa',
  284. 'aCa' ]
  285. start, end = _BuildLocations( 2, 2, 2, 2 )
  286. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, 'Eb\nbF',
  287. 0, 0, result_buffer )
  288. expected_buffer = [ 'aAa',
  289. 'aEb',
  290. 'bFBa',
  291. 'aCa' ]
  292. AssertBuffersAreEqualAsBytes( expected_buffer, result_buffer )
  293. eq_( line_offset, 1 )
  294. eq_( char_offset, 1 )
  295. # now make another change to the "2nd" line
  296. start, end = _BuildLocations( 2, 3, 2, 4 )
  297. ( new_line_offset, new_char_offset ) = vimsupport.ReplaceChunk(
  298. start,
  299. end,
  300. 'cccc',
  301. line_offset,
  302. char_offset,
  303. result_buffer )
  304. line_offset += new_line_offset
  305. char_offset += new_char_offset
  306. AssertBuffersAreEqualAsBytes( [ 'aAa',
  307. 'aEb',
  308. 'bFBcccc',
  309. 'aCa' ], result_buffer )
  310. eq_( line_offset, 1 )
  311. eq_( char_offset, 4 )
  312. def ReplaceChunk_SingleToMultipleLines2_test():
  313. result_buffer = [ 'aAa', 'aBa', 'aCa' ]
  314. start, end = _BuildLocations( 2, 2, 2, 2 )
  315. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  316. end,
  317. 'Eb\nbFb\nG',
  318. 0,
  319. 0,
  320. result_buffer )
  321. expected_buffer = [ 'aAa',
  322. 'aEb',
  323. 'bFb',
  324. 'GBa',
  325. 'aCa' ]
  326. AssertBuffersAreEqualAsBytes( expected_buffer, result_buffer )
  327. eq_( line_offset, 2 )
  328. eq_( char_offset, 0 )
  329. def ReplaceChunk_SingleToMultipleLines3_test():
  330. result_buffer = [ 'aAa', 'aBa', 'aCa' ]
  331. start, end = _BuildLocations( 2, 2, 2, 2 )
  332. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  333. end,
  334. 'Eb\nbFb\nbGb',
  335. 0,
  336. 0,
  337. result_buffer )
  338. expected_buffer = [ 'aAa',
  339. 'aEb',
  340. 'bFb',
  341. 'bGbBa',
  342. 'aCa' ]
  343. AssertBuffersAreEqualAsBytes( expected_buffer, result_buffer )
  344. eq_( line_offset, 2 )
  345. eq_( char_offset, 2 )
  346. def ReplaceChunk_SingleToMultipleLinesReplace_test():
  347. result_buffer = [ 'aAa', 'aBa', 'aCa' ]
  348. start, end = _BuildLocations( 1, 2, 1, 4 )
  349. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  350. end,
  351. 'Eb\nbFb\nbGb',
  352. 0,
  353. 0,
  354. result_buffer )
  355. expected_buffer = [ 'aEb',
  356. 'bFb',
  357. 'bGb',
  358. 'aBa',
  359. 'aCa' ]
  360. AssertBuffersAreEqualAsBytes( expected_buffer, result_buffer )
  361. eq_( line_offset, 2 )
  362. eq_( char_offset, 0 )
  363. def ReplaceChunk_SingleToMultipleLinesReplace_2_test():
  364. result_buffer = [ 'aAa',
  365. 'aBa',
  366. 'aCa' ]
  367. start, end = _BuildLocations( 1, 2, 1, 4 )
  368. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  369. end,
  370. 'Eb\nbFb\nbGb',
  371. 0,
  372. 0,
  373. result_buffer )
  374. expected_buffer = [ 'aEb',
  375. 'bFb',
  376. 'bGb',
  377. 'aBa',
  378. 'aCa' ]
  379. AssertBuffersAreEqualAsBytes( expected_buffer, result_buffer )
  380. eq_( line_offset, 2 )
  381. eq_( char_offset, 0 )
  382. # now do a subsequent change (insert at end of line "1")
  383. start, end = _BuildLocations( 1, 4, 1, 4 )
  384. ( new_line_offset, new_char_offset ) = vimsupport.ReplaceChunk(
  385. start,
  386. end,
  387. 'cccc',
  388. line_offset,
  389. char_offset,
  390. result_buffer )
  391. line_offset += new_line_offset
  392. char_offset += new_char_offset
  393. AssertBuffersAreEqualAsBytes( [ 'aEb',
  394. 'bFb',
  395. 'bGbcccc',
  396. 'aBa',
  397. 'aCa' ], result_buffer )
  398. eq_( line_offset, 2 )
  399. eq_( char_offset, 4 )
  400. def ReplaceChunk_MultipleLinesToSingleLine_test():
  401. result_buffer = [ 'aAa', 'aBa', 'aCaaaa' ]
  402. start, end = _BuildLocations( 2, 2, 3, 2 )
  403. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, 'E',
  404. 0, 0, result_buffer )
  405. expected_buffer = [ 'aAa', 'aECaaaa' ]
  406. AssertBuffersAreEqualAsBytes( expected_buffer, result_buffer )
  407. eq_( line_offset, -1 )
  408. eq_( char_offset, 1 )
  409. # make another modification applying offsets
  410. start, end = _BuildLocations( 3, 3, 3, 4 )
  411. ( new_line_offset, new_char_offset ) = vimsupport.ReplaceChunk(
  412. start,
  413. end,
  414. 'cccc',
  415. line_offset,
  416. char_offset,
  417. result_buffer )
  418. line_offset += new_line_offset
  419. char_offset += new_char_offset
  420. AssertBuffersAreEqualAsBytes( [ 'aAa',
  421. 'aECccccaaa' ], result_buffer )
  422. eq_( line_offset, -1 )
  423. eq_( char_offset, 4 )
  424. # and another, for luck
  425. start, end = _BuildLocations( 3, 4, 3, 5 )
  426. ( new_line_offset, new_char_offset ) = vimsupport.ReplaceChunk(
  427. start,
  428. end,
  429. 'dd\ndd',
  430. line_offset,
  431. char_offset,
  432. result_buffer )
  433. line_offset += new_line_offset
  434. char_offset += new_char_offset
  435. AssertBuffersAreEqualAsBytes( [ 'aAa',
  436. 'aECccccdd',
  437. 'ddaa' ], result_buffer )
  438. eq_( line_offset, 0 )
  439. eq_( char_offset, -2 )
  440. def ReplaceChunk_MultipleLinesToSameMultipleLines_test():
  441. result_buffer = [ 'aAa',
  442. 'aBa',
  443. 'aCa',
  444. 'aDe' ]
  445. start, end = _BuildLocations( 2, 2, 3, 2 )
  446. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, 'Eb\nbF',
  447. 0, 0, result_buffer )
  448. expected_buffer = [ 'aAa',
  449. 'aEb',
  450. 'bFCa',
  451. 'aDe' ]
  452. AssertBuffersAreEqualAsBytes( expected_buffer, result_buffer )
  453. eq_( line_offset, 0 )
  454. eq_( char_offset, 1 )
  455. def ReplaceChunk_MultipleLinesToMoreMultipleLines_test():
  456. result_buffer = [ 'aAa',
  457. 'aBa',
  458. 'aCa',
  459. 'aDe' ]
  460. start, end = _BuildLocations( 2, 2, 3, 2 )
  461. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  462. end,
  463. 'Eb\nbFb\nbG',
  464. 0,
  465. 0,
  466. result_buffer )
  467. expected_buffer = [ 'aAa',
  468. 'aEb',
  469. 'bFb',
  470. 'bGCa',
  471. 'aDe' ]
  472. AssertBuffersAreEqualAsBytes( expected_buffer, result_buffer )
  473. eq_( line_offset, 1 )
  474. eq_( char_offset, 1 )
  475. def ReplaceChunk_MultipleLinesToLessMultipleLines_test():
  476. result_buffer = [ 'aAa',
  477. 'aBa',
  478. 'aCa',
  479. 'aDe' ]
  480. start, end = _BuildLocations( 1, 2, 3, 2 )
  481. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, 'Eb\nbF',
  482. 0, 0, result_buffer )
  483. expected_buffer = [ 'aEb', 'bFCa', 'aDe' ]
  484. AssertBuffersAreEqualAsBytes( expected_buffer, result_buffer )
  485. eq_( line_offset, -1 )
  486. eq_( char_offset, 1 )
  487. def ReplaceChunk_MultipleLinesToEvenLessMultipleLines_test():
  488. result_buffer = [ 'aAa',
  489. 'aBa',
  490. 'aCa',
  491. 'aDe' ]
  492. start, end = _BuildLocations( 1, 2, 4, 2 )
  493. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, 'Eb\nbF',
  494. 0, 0, result_buffer )
  495. expected_buffer = [ 'aEb', 'bFDe' ]
  496. AssertBuffersAreEqualAsBytes( expected_buffer, result_buffer )
  497. eq_( line_offset, -2 )
  498. eq_( char_offset, 1 )
  499. def ReplaceChunk_SpanBufferEdge_test():
  500. result_buffer = [ 'aAa',
  501. 'aBa',
  502. 'aCa' ]
  503. start, end = _BuildLocations( 1, 1, 1, 3 )
  504. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, 'bDb',
  505. 0, 0, result_buffer )
  506. expected_buffer = [ 'bDba',
  507. 'aBa',
  508. 'aCa' ]
  509. AssertBuffersAreEqualAsBytes( expected_buffer, result_buffer )
  510. eq_( line_offset, 0 )
  511. eq_( char_offset, 1 )
  512. def ReplaceChunk_DeleteTextInLine_test():
  513. result_buffer = [ 'aAa',
  514. 'aBa',
  515. 'aCa' ]
  516. start, end = _BuildLocations( 2, 2, 2, 3 )
  517. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, '',
  518. 0, 0, result_buffer )
  519. expected_buffer = [ 'aAa',
  520. 'aa',
  521. 'aCa' ]
  522. AssertBuffersAreEqualAsBytes( expected_buffer, result_buffer )
  523. eq_( line_offset, 0 )
  524. eq_( char_offset, -1 )
  525. def ReplaceChunk_AddTextInLine_test():
  526. result_buffer = [ 'aAa',
  527. 'aBa',
  528. 'aCa' ]
  529. start, end = _BuildLocations( 2, 2, 2, 2 )
  530. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, 'bDb',
  531. 0, 0, result_buffer )
  532. expected_buffer = [ 'aAa',
  533. 'abDbBa',
  534. 'aCa' ]
  535. AssertBuffersAreEqualAsBytes( expected_buffer, result_buffer )
  536. eq_( line_offset, 0 )
  537. eq_( char_offset, 3 )
  538. def ReplaceChunk_ReplaceTextInLine_test():
  539. result_buffer = [ 'aAa',
  540. 'aBa',
  541. 'aCa' ]
  542. start, end = _BuildLocations( 2, 2, 2, 3 )
  543. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, 'bDb',
  544. 0, 0, result_buffer )
  545. expected_buffer = [ 'aAa',
  546. 'abDba',
  547. 'aCa' ]
  548. AssertBuffersAreEqualAsBytes( expected_buffer, result_buffer )
  549. eq_( line_offset, 0 )
  550. eq_( char_offset, 2 )
  551. def ReplaceChunk_SingleLineOffsetWorks_test():
  552. result_buffer = [ 'aAa',
  553. 'aBa',
  554. 'aCa' ]
  555. start, end = _BuildLocations( 1, 1, 1, 2 )
  556. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, 'bDb',
  557. 1, 1, result_buffer )
  558. expected_buffer = [ 'aAa',
  559. 'abDba',
  560. 'aCa' ]
  561. AssertBuffersAreEqualAsBytes( expected_buffer, result_buffer )
  562. eq_( line_offset, 0 )
  563. eq_( char_offset, 2 )
  564. def ReplaceChunk_SingleLineToMultipleLinesOffsetWorks_test():
  565. result_buffer = [ 'aAa',
  566. 'aBa',
  567. 'aCa' ]
  568. start, end = _BuildLocations( 1, 1, 1, 2 )
  569. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, 'Db\nE',
  570. 1, 1, result_buffer )
  571. expected_buffer = [ 'aAa',
  572. 'aDb',
  573. 'Ea',
  574. 'aCa' ]
  575. AssertBuffersAreEqualAsBytes( expected_buffer, result_buffer )
  576. eq_( line_offset, 1 )
  577. eq_( char_offset, -1 )
  578. def ReplaceChunk_MultipleLinesToSingleLineOffsetWorks_test():
  579. result_buffer = [ 'aAa',
  580. 'aBa',
  581. 'aCa' ]
  582. start, end = _BuildLocations( 1, 1, 2, 2 )
  583. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start, end, 'bDb',
  584. 1, 1, result_buffer )
  585. expected_buffer = [ 'aAa',
  586. 'abDbCa' ]
  587. AssertBuffersAreEqualAsBytes( expected_buffer, result_buffer )
  588. eq_( line_offset, -1 )
  589. eq_( char_offset, 3 )
  590. def ReplaceChunk_MultipleLineOffsetWorks_test():
  591. result_buffer = [ 'aAa',
  592. 'aBa',
  593. 'aCa' ]
  594. start, end = _BuildLocations( 3, 1, 4, 3 )
  595. ( line_offset, char_offset ) = vimsupport.ReplaceChunk( start,
  596. end,
  597. 'bDb\nbEb\nbFb',
  598. -1,
  599. 1,
  600. result_buffer )
  601. expected_buffer = [ 'aAa',
  602. 'abDb',
  603. 'bEb',
  604. 'bFba' ]
  605. AssertBuffersAreEqualAsBytes( expected_buffer, result_buffer )
  606. eq_( line_offset, 1 )
  607. eq_( char_offset, 1 )
  608. def _BuildLocations( start_line, start_column, end_line, end_column ):
  609. return {
  610. 'line_num' : start_line,
  611. 'column_num': start_column,
  612. }, {
  613. 'line_num' : end_line,
  614. 'column_num': end_column,
  615. }
  616. def ReplaceChunksInBuffer_SortedChunks_test():
  617. chunks = [
  618. _BuildChunk( 1, 4, 1, 4, '(' ),
  619. _BuildChunk( 1, 11, 1, 11, ')' )
  620. ]
  621. result_buffer = [ 'CT<10 >> 2> ct' ]
  622. vimsupport.ReplaceChunksInBuffer( chunks, result_buffer, None )
  623. expected_buffer = [ 'CT<(10 >> 2)> ct' ]
  624. AssertBuffersAreEqualAsBytes( expected_buffer, result_buffer )
  625. def ReplaceChunksInBuffer_UnsortedChunks_test():
  626. chunks = [
  627. _BuildChunk( 1, 11, 1, 11, ')' ),
  628. _BuildChunk( 1, 4, 1, 4, '(' )
  629. ]
  630. result_buffer = [ 'CT<10 >> 2> ct' ]
  631. vimsupport.ReplaceChunksInBuffer( chunks, result_buffer, None )
  632. expected_buffer = [ 'CT<(10 >> 2)> ct' ]
  633. AssertBuffersAreEqualAsBytes( expected_buffer, result_buffer )
  634. @patch( 'ycm.vimsupport.VariableExists', return_value = False )
  635. @patch( 'ycm.vimsupport.SetFittingHeightForCurrentWindow' )
  636. @patch( 'ycm.vimsupport.GetBufferNumberForFilename',
  637. return_value = 1,
  638. new_callable = ExtendedMock )
  639. @patch( 'ycm.vimsupport.BufferIsVisible',
  640. return_value = True,
  641. new_callable = ExtendedMock )
  642. @patch( 'ycm.vimsupport.OpenFilename' )
  643. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  644. @patch( 'vim.eval', new_callable = ExtendedMock )
  645. @patch( 'vim.command', new_callable = ExtendedMock )
  646. def ReplaceChunks_SingleFile_Open_test( vim_command,
  647. vim_eval,
  648. post_vim_message,
  649. open_filename,
  650. buffer_is_visible,
  651. get_buffer_number_for_filename,
  652. set_fitting_height,
  653. variable_exists ):
  654. single_buffer_name = os.path.realpath( 'single_file' )
  655. chunks = [
  656. _BuildChunk( 1, 1, 2, 1, 'replacement', single_buffer_name )
  657. ]
  658. result_buffer = VimBuffer(
  659. single_buffer_name,
  660. contents = [
  661. 'line1',
  662. 'line2',
  663. 'line3'
  664. ]
  665. )
  666. with patch( 'vim.buffers', [ None, result_buffer, None ] ):
  667. vimsupport.ReplaceChunks( chunks )
  668. # Ensure that we applied the replacement correctly
  669. eq_( result_buffer.GetLines(), [
  670. 'replacementline2',
  671. 'line3',
  672. ] )
  673. # GetBufferNumberForFilename is called twice:
  674. # - once to the check if we would require opening the file (so that we can
  675. # raise a warning)
  676. # - once whilst applying the changes
  677. get_buffer_number_for_filename.assert_has_exact_calls( [
  678. call( single_buffer_name ),
  679. call( single_buffer_name ),
  680. ] )
  681. # BufferIsVisible is called twice for the same reasons as above
  682. buffer_is_visible.assert_has_exact_calls( [
  683. call( 1 ),
  684. call( 1 ),
  685. ] )
  686. # we don't attempt to open any files
  687. open_filename.assert_not_called()
  688. # But we do set the quickfix list
  689. vim_eval.assert_has_exact_calls( [
  690. call( 'setqflist( {0} )'.format( json.dumps( [ {
  691. 'bufnr': 1,
  692. 'filename': single_buffer_name,
  693. 'lnum': 1,
  694. 'col': 1,
  695. 'text': 'replacement',
  696. 'type': 'F'
  697. } ] ) ) ),
  698. ] )
  699. vim_command.assert_has_exact_calls( [
  700. call( 'botright copen' ),
  701. call( 'silent! wincmd p' )
  702. ] )
  703. set_fitting_height.assert_called_once_with()
  704. # And it is ReplaceChunks that prints the message showing the number of
  705. # changes
  706. post_vim_message.assert_has_exact_calls( [
  707. call( 'Applied 1 changes', warning = False ),
  708. ] )
  709. @patch( 'ycm.vimsupport.VariableExists', return_value = False )
  710. @patch( 'ycm.vimsupport.SetFittingHeightForCurrentWindow' )
  711. @patch( 'ycm.vimsupport.GetBufferNumberForFilename',
  712. side_effect = [ -1, -1, 1 ],
  713. new_callable = ExtendedMock )
  714. @patch( 'ycm.vimsupport.BufferIsVisible',
  715. side_effect = [ False, False, True ],
  716. new_callable = ExtendedMock )
  717. @patch( 'ycm.vimsupport.OpenFilename',
  718. new_callable = ExtendedMock )
  719. @patch( 'ycm.vimsupport.PostVimMessage', new_callable = ExtendedMock )
  720. @patch( 'ycm.vimsupport.Confirm',
  721. return_value = True,
  722. new_callable = ExtendedMock )
  723. @patch( 'vim.eval', return_value = 10, new_callable = ExtendedMock )
  724. @patch( 'vim.command', new_callable = ExtendedMock )
  725. def ReplaceChunks_SingleFile_NotOpen_test( vim_command,
  726. vim_eval,
  727. confirm,
  728. post_vim_message,
  729. open_filename,
  730. buffer_is_visible,
  731. get_buffer_number_for_filename,
  732. set_fitting_height,
  733. variable_exists ):
  734. single_buffer_name = os.path.realpath( 'single_file' )
  735. chunks = [
  736. _BuildChunk( 1, 1, 2, 1, 'replacement', single_buffer_name )
  737. ]
  738. result_buffer = VimBuffer(
  739. single_buffer_name,
  740. contents = [
  741. 'line1',
  742. 'line2',
  743. 'line3'
  744. ]
  745. )
  746. with patch( 'vim.buffers', [ None, result_buffer, None ] ):
  747. vimsupport.ReplaceChunks( chunks )
  748. # We checked if it was OK to open the file
  749. confirm.assert_has_exact_calls( [
  750. call( vimsupport.FIXIT_OPENING_BUFFERS_MESSAGE_FORMAT.format( 1 ) )
  751. ] )
  752. # Ensure that we applied the replacement correctly
  753. eq_( result_buffer.GetLines(), [
  754. 'replacementline2',
  755. 'line3',
  756. ] )
  757. # GetBufferNumberForFilename is called 3 times. The return values are set in
  758. # the @patch call above:
  759. # - once to the check if we would require opening the file (so that we can
  760. # raise a warning) (-1 return)
  761. # - once whilst applying the changes (-1 return)
  762. # - finally after calling OpenFilename (1 return)
  763. get_buffer_number_for_filename.assert_has_exact_calls( [
  764. call( single_buffer_name ),
  765. call( single_buffer_name ),
  766. call( single_buffer_name ),
  767. ] )
  768. # BufferIsVisible is called 3 times for the same reasons as above, with the
  769. # return of each one
  770. buffer_is_visible.assert_has_exact_calls( [
  771. call( -1 ),
  772. call( -1 ),
  773. call( 1 ),
  774. ] )
  775. # We open 'single_file' as expected.
  776. open_filename.assert_called_with( single_buffer_name, {
  777. 'focus': True,
  778. 'fix': True,
  779. 'size': 10
  780. } )
  781. # And close it again, then show the quickfix window.
  782. vim_command.assert_has_exact_calls( [
  783. call( 'lclose' ),
  784. call( 'hide' ),
  785. call( 'botright copen' ),
  786. call( 'silent! wincmd p' )
  787. ] )
  788. set_fitting_height.assert_called_once_with()
  789. # And update the quickfix list
  790. vim_eval.assert_has_exact_calls( [
  791. call( '&previewheight' ),
  792. call( 'setqflist( {0} )'.format( json.dumps( [ {
  793. 'bufnr': 1,
  794. 'filename': single_buffer_name,
  795. 'lnum': 1,
  796. 'col': 1,
  797. 'text': 'replacement',
  798. 'type': 'F'
  799. } ] ) ) ),
  800. ] )
  801. # And it is ReplaceChunks that prints the message showing the number of
  802. # changes
  803. post_vim_message.assert_has_exact_calls( [
  804. call( 'Applied 1 changes', warning = False ),
  805. ] )
  806. @patch( 'ycm.vimsupport.GetBufferNumberForFilename',
  807. side_effect = [ -1, -1, 1 ],
  808. new_callable = ExtendedMock )
  809. @patch( 'ycm.vimsupport.BufferIsVisible',
  810. side_effect = [ False, False, True ],
  811. new_callable = ExtendedMock )
  812. @patch( 'ycm.vimsupport.OpenFilename',
  813. new_callable = ExtendedMock )
  814. @patch( 'ycm.vimsupport.PostVimMessage',
  815. new_callable = ExtendedMock )
  816. @patch( 'ycm.vimsupport.Confirm',
  817. return_value = False,
  818. new_callable = ExtendedMock )
  819. @patch( 'vim.eval',
  820. return_value = 10,
  821. new_callable = ExtendedMock )
  822. @patch( 'vim.command', new_callable = ExtendedMock )
  823. def ReplaceChunks_User_Declines_To_Open_File_test(
  824. vim_command,
  825. vim_eval,
  826. confirm,
  827. post_vim_message,
  828. open_filename,
  829. buffer_is_visible,
  830. get_buffer_number_for_filename ):
  831. # Same as above, except the user selects Cancel when asked if they should
  832. # allow us to open lots of (ahem, 1) file.
  833. single_buffer_name = os.path.realpath( 'single_file' )
  834. chunks = [
  835. _BuildChunk( 1, 1, 2, 1, 'replacement', single_buffer_name )
  836. ]
  837. result_buffer = VimBuffer(
  838. single_buffer_name,
  839. contents = [
  840. 'line1',
  841. 'line2',
  842. 'line3'
  843. ]
  844. )
  845. with patch( 'vim.buffers', [ None, result_buffer, None ] ):
  846. vimsupport.ReplaceChunks( chunks )
  847. # We checked if it was OK to open the file
  848. confirm.assert_has_exact_calls( [
  849. call( vimsupport.FIXIT_OPENING_BUFFERS_MESSAGE_FORMAT.format( 1 ) )
  850. ] )
  851. # Ensure that buffer is not changed
  852. eq_( result_buffer.GetLines(), [
  853. 'line1',
  854. 'line2',
  855. 'line3',
  856. ] )
  857. # GetBufferNumberForFilename is called once. The return values are set in
  858. # the @patch call above:
  859. # - once to the check if we would require opening the file (so that we can
  860. # raise a warning) (-1 return)
  861. get_buffer_number_for_filename.assert_has_exact_calls( [
  862. call( single_buffer_name ),
  863. ] )
  864. # BufferIsVisible is called once for the above file, which wasn't visible.
  865. buffer_is_visible.assert_has_exact_calls( [
  866. call( -1 ),
  867. ] )
  868. # We don't attempt to open any files or update any quickfix list or anything
  869. # like that
  870. open_filename.assert_not_called()
  871. vim_eval.assert_not_called()
  872. vim_command.assert_not_called()
  873. post_vim_message.assert_not_called()
  874. @patch( 'ycm.vimsupport.GetBufferNumberForFilename',
  875. side_effect = [ -1, -1, 1 ],
  876. new_callable = ExtendedMock )
  877. # Key difference is here: In the final check, BufferIsVisible returns False
  878. @patch( 'ycm.vimsupport.BufferIsVisible',
  879. side_effect = [ False, False, False ],
  880. new_callable = ExtendedMock )
  881. @patch( 'ycm.vimsupport.OpenFilename',
  882. new_callable = ExtendedMock )
  883. @patch( 'ycm.vimsupport.PostVimMessage',
  884. new_callable = ExtendedMock )
  885. @patch( 'ycm.vimsupport.Confirm',
  886. return_value = True,
  887. new_callable = ExtendedMock )
  888. @patch( 'vim.eval',
  889. return_value = 10,
  890. new_callable = ExtendedMock )
  891. @patch( 'vim.command',
  892. new_callable = ExtendedMock )
  893. def ReplaceChunks_User_Aborts_Opening_File_test(
  894. vim_command,
  895. vim_eval,
  896. confirm,
  897. post_vim_message,
  898. open_filename,
  899. buffer_is_visible,
  900. get_buffer_number_for_filename ):
  901. # Same as above, except the user selects Abort or Quick during the
  902. # "swap-file-found" dialog
  903. single_buffer_name = os.path.realpath( 'single_file' )
  904. chunks = [
  905. _BuildChunk( 1, 1, 2, 1, 'replacement', single_buffer_name )
  906. ]
  907. result_buffer = VimBuffer(
  908. single_buffer_name,
  909. contents = [
  910. 'line1',
  911. 'line2',
  912. 'line3'
  913. ]
  914. )
  915. with patch( 'vim.buffers', [ None, result_buffer, None ] ):
  916. assert_that( calling( vimsupport.ReplaceChunks ).with_args( chunks ),
  917. raises( RuntimeError,
  918. 'Unable to open file: .+single_file\n'
  919. 'FixIt/Refactor operation aborted prior to completion. '
  920. 'Your files have not been fully updated. '
  921. 'Please use undo commands to revert the applied changes.' ) )
  922. # We checked if it was OK to open the file
  923. confirm.assert_has_exact_calls( [
  924. call( vimsupport.FIXIT_OPENING_BUFFERS_MESSAGE_FORMAT.format( 1 ) )
  925. ] )
  926. # Ensure that buffer is not changed
  927. eq_( result_buffer.GetLines(), [
  928. 'line1',
  929. 'line2',
  930. 'line3',
  931. ] )
  932. # We tried to open this file
  933. open_filename.assert_called_with( single_buffer_name, {
  934. 'focus': True,
  935. 'fix': True,
  936. 'size': 10
  937. } )
  938. vim_eval.assert_called_with( "&previewheight" )
  939. # But raised an exception before issuing the message at the end
  940. post_vim_message.assert_not_called()
  941. @patch( 'ycm.vimsupport.VariableExists', return_value = False )
  942. @patch( 'ycm.vimsupport.SetFittingHeightForCurrentWindow' )
  943. @patch( 'ycm.vimsupport.GetBufferNumberForFilename', side_effect = [
  944. 22, # first_file (check)
  945. -1, # second_file (check)
  946. 22, # first_file (apply)
  947. -1, # second_file (apply)
  948. 19, # second_file (check after open)
  949. ],
  950. new_callable = ExtendedMock )
  951. @patch( 'ycm.vimsupport.BufferIsVisible', side_effect = [
  952. True, # first_file (check)
  953. False, # second_file (check)
  954. True, # first_file (apply)
  955. False, # second_file (apply)
  956. True, # side_effect (check after open)
  957. ],
  958. new_callable = ExtendedMock)
  959. @patch( 'ycm.vimsupport.OpenFilename',
  960. new_callable = ExtendedMock)
  961. @patch( 'ycm.vimsupport.PostVimMessage',
  962. new_callable = ExtendedMock)
  963. @patch( 'ycm.vimsupport.Confirm', return_value = True,
  964. new_callable = ExtendedMock)
  965. @patch( 'vim.eval', return_value = 10,
  966. new_callable = ExtendedMock)
  967. @patch( 'vim.command',
  968. new_callable = ExtendedMock)
  969. def ReplaceChunks_MultiFile_Open_test( vim_command,
  970. vim_eval,
  971. confirm,
  972. post_vim_message,
  973. open_filename,
  974. buffer_is_visible,
  975. get_buffer_number_for_filename,
  976. set_fitting_height,
  977. variable_exists ):
  978. # Chunks are split across 2 files, one is already open, one isn't
  979. first_buffer_name = os.path.realpath( '1_first_file' )
  980. second_buffer_name = os.path.realpath( '2_second_file' )
  981. chunks = [
  982. _BuildChunk( 1, 1, 2, 1, 'first_file_replacement ', first_buffer_name ),
  983. _BuildChunk( 2, 1, 2, 1, 'second_file_replacement ', second_buffer_name ),
  984. ]
  985. first_file = VimBuffer(
  986. first_buffer_name,
  987. number = 22,
  988. contents = [
  989. 'line1',
  990. 'line2',
  991. 'line3',
  992. ]
  993. )
  994. second_file = VimBuffer(
  995. second_buffer_name,
  996. number = 19,
  997. contents = [
  998. 'another line1',
  999. 'ACME line2',
  1000. ]
  1001. )
  1002. vim_buffers = [ None ] * 23
  1003. vim_buffers[ 22 ] = first_file
  1004. vim_buffers[ 19 ] = second_file
  1005. with patch( 'vim.buffers', vim_buffers ):
  1006. vimsupport.ReplaceChunks( chunks )
  1007. # We checked for the right file names
  1008. get_buffer_number_for_filename.assert_has_exact_calls( [
  1009. call( first_buffer_name ),
  1010. call( second_buffer_name ),
  1011. call( first_buffer_name ),
  1012. call( second_buffer_name ),
  1013. call( second_buffer_name ),
  1014. ] )
  1015. # We checked if it was OK to open the file
  1016. confirm.assert_has_exact_calls( [
  1017. call( vimsupport.FIXIT_OPENING_BUFFERS_MESSAGE_FORMAT.format( 1 ) )
  1018. ] )
  1019. # Ensure that buffers are updated
  1020. eq_( second_file.GetLines(), [
  1021. 'another line1',
  1022. 'second_file_replacement ACME line2',
  1023. ] )
  1024. eq_( first_file.GetLines(), [
  1025. 'first_file_replacement line2',
  1026. 'line3',
  1027. ] )
  1028. # We open '2_second_file' as expected.
  1029. open_filename.assert_called_with( second_buffer_name, {
  1030. 'focus': True,
  1031. 'fix': True,
  1032. 'size': 10
  1033. } )
  1034. # And close it again, then show the quickfix window.
  1035. vim_command.assert_has_exact_calls( [
  1036. call( 'lclose' ),
  1037. call( 'hide' ),
  1038. call( 'botright copen' ),
  1039. call( 'silent! wincmd p' )
  1040. ] )
  1041. set_fitting_height.assert_called_once_with()
  1042. # And update the quickfix list with each entry
  1043. vim_eval.assert_has_exact_calls( [
  1044. call( '&previewheight' ),
  1045. call( 'setqflist( {0} )'.format( json.dumps( [ {
  1046. 'bufnr': 22,
  1047. 'filename': first_buffer_name,
  1048. 'lnum': 1,
  1049. 'col': 1,
  1050. 'text': 'first_file_replacement ',
  1051. 'type': 'F'
  1052. }, {
  1053. 'bufnr': 19,
  1054. 'filename': second_buffer_name,
  1055. 'lnum': 2,
  1056. 'col': 1,
  1057. 'text': 'second_file_replacement ',
  1058. 'type': 'F'
  1059. } ] ) ) ),
  1060. ] )
  1061. # And it is ReplaceChunks that prints the message showing the number of
  1062. # changes
  1063. post_vim_message.assert_has_exact_calls( [
  1064. call( 'Applied 2 changes', warning = False ),
  1065. ] )
  1066. def _BuildChunk( start_line,
  1067. start_column,
  1068. end_line,
  1069. end_column,
  1070. replacement_text, filepath='test_file_name' ):
  1071. return {
  1072. 'range': {
  1073. 'start': {
  1074. 'filepath': filepath,
  1075. 'line_num': start_line,
  1076. 'column_num': start_column,
  1077. },
  1078. 'end': {
  1079. 'filepath': filepath,
  1080. 'line_num': end_line,
  1081. 'column_num': end_column,
  1082. },
  1083. },
  1084. 'replacement_text': replacement_text
  1085. }
  1086. @patch( 'vim.eval', new_callable = ExtendedMock )
  1087. def AddDiagnosticSyntaxMatch_ErrorInMiddleOfLine_test( vim_eval ):
  1088. current_buffer = VimBuffer(
  1089. 'some_file',
  1090. contents = [ 'Highlight this error please' ]
  1091. )
  1092. with patch( 'vim.current.buffer', current_buffer ):
  1093. vimsupport.AddDiagnosticSyntaxMatch( 1, 16, 1, 21 )
  1094. vim_eval.assert_called_once_with(
  1095. r"matchadd('YcmErrorSection', '\%1l\%16c\_.\{-}\%1l\%21c')" )
  1096. @patch( 'vim.eval', new_callable = ExtendedMock )
  1097. def AddDiagnosticSyntaxMatch_WarningAtEndOfLine_test( vim_eval ):
  1098. current_buffer = VimBuffer(
  1099. 'some_file',
  1100. contents = [ 'Highlight this warning' ]
  1101. )
  1102. with patch( 'vim.current.buffer', current_buffer ):
  1103. vimsupport.AddDiagnosticSyntaxMatch( 1, 16, 1, 23, is_error = False )
  1104. vim_eval.assert_called_once_with(
  1105. r"matchadd('YcmWarningSection', '\%1l\%16c\_.\{-}\%1l\%23c')" )
  1106. @patch( 'vim.command', new_callable=ExtendedMock )
  1107. @patch( 'vim.current', new_callable=ExtendedMock)
  1108. def WriteToPreviewWindow_test( vim_current, vim_command ):
  1109. vim_current.window.options.__getitem__ = MagicMock( return_value = True )
  1110. vimsupport.WriteToPreviewWindow( "test" )
  1111. vim_command.assert_has_exact_calls( [
  1112. call( 'silent! pclose!' ),
  1113. call( 'silent! pedit! _TEMP_FILE_' ),
  1114. call( 'silent! wincmd P' ),
  1115. call( 'silent! wincmd p' ) ] )
  1116. vim_current.buffer.__setitem__.assert_called_with(
  1117. slice( None, None, None ), [ 'test' ] )
  1118. vim_current.buffer.options.__setitem__.assert_has_exact_calls( [
  1119. call( 'modifiable', True ),
  1120. call( 'readonly', False ),
  1121. call( 'buftype', 'nofile' ),
  1122. call( 'bufhidden', 'wipe' ),
  1123. call( 'buflisted', False ),
  1124. call( 'swapfile', False ),
  1125. call( 'modifiable', False ),
  1126. call( 'modified', False ),
  1127. call( 'readonly', True ),
  1128. ], any_order = True )
  1129. @patch( 'vim.current' )
  1130. def WriteToPreviewWindow_MultiLine_test( vim_current ):
  1131. vim_current.window.options.__getitem__ = MagicMock( return_value = True )
  1132. vimsupport.WriteToPreviewWindow( "test\ntest2" )
  1133. vim_current.buffer.__setitem__.assert_called_with(
  1134. slice( None, None, None ), [ 'test', 'test2' ] )
  1135. @patch( 'vim.command', new_callable=ExtendedMock )
  1136. @patch( 'vim.current', new_callable=ExtendedMock )
  1137. def WriteToPreviewWindow_JumpFail_test( vim_current, vim_command ):
  1138. vim_current.window.options.__getitem__ = MagicMock( return_value = False )
  1139. vimsupport.WriteToPreviewWindow( "test" )
  1140. vim_command.assert_has_exact_calls( [
  1141. call( 'silent! pclose!' ),
  1142. call( 'silent! pedit! _TEMP_FILE_' ),
  1143. call( 'silent! wincmd P' ),
  1144. call( 'redraw' ),
  1145. call( "echo 'test'" ),
  1146. ] )
  1147. vim_current.buffer.__setitem__.assert_not_called()
  1148. vim_current.buffer.options.__setitem__.assert_not_called()
  1149. @patch( 'vim.command', new_callable=ExtendedMock )
  1150. @patch( 'vim.current', new_callable=ExtendedMock )
  1151. def WriteToPreviewWindow_JumpFail_MultiLine_test( vim_current, vim_command ):
  1152. vim_current.window.options.__getitem__ = MagicMock( return_value = False )
  1153. vimsupport.WriteToPreviewWindow( "test\ntest2" )
  1154. vim_command.assert_has_exact_calls( [
  1155. call( 'silent! pclose!' ),
  1156. call( 'silent! pedit! _TEMP_FILE_' ),
  1157. call( 'silent! wincmd P' ),
  1158. call( 'redraw' ),
  1159. call( "echo 'test'" ),
  1160. call( "echo 'test2'" ),
  1161. ] )
  1162. vim_current.buffer.__setitem__.assert_not_called()
  1163. vim_current.buffer.options.__setitem__.assert_not_called()
  1164. def BufferIsVisibleForFilename_test():
  1165. vim_buffers = [
  1166. VimBuffer( 'visible_filename', number = 1, window = 1 ),
  1167. VimBuffer( 'hidden_filename', number = 2, window = None )
  1168. ]
  1169. with patch( 'vim.buffers', vim_buffers ):
  1170. eq_( vimsupport.BufferIsVisibleForFilename( 'visible_filename' ), True )
  1171. eq_( vimsupport.BufferIsVisibleForFilename( 'hidden_filename' ), False )
  1172. eq_( vimsupport.BufferIsVisibleForFilename( 'another_filename' ), False )
  1173. @patch( 'vim.command',
  1174. side_effect = MockVimCommand,
  1175. new_callable = ExtendedMock )
  1176. def CloseBuffersForFilename_test( vim_command, *args ):
  1177. vim_buffers = [
  1178. VimBuffer( 'some_filename', number = 2 ),
  1179. VimBuffer( 'some_filename', number = 5 )
  1180. ]
  1181. with patch( 'vim.buffers', vim_buffers ):
  1182. vimsupport.CloseBuffersForFilename( 'some_filename' )
  1183. vim_command.assert_has_exact_calls( [
  1184. call( 'silent! bwipeout! 2' ),
  1185. call( 'silent! bwipeout! 5' )
  1186. ], any_order = True )
  1187. @patch( 'vim.command', new_callable = ExtendedMock )
  1188. @patch( 'vim.current', new_callable = ExtendedMock )
  1189. def OpenFilename_test( vim_current, vim_command ):
  1190. # Options used to open a logfile.
  1191. options = {
  1192. 'size': vimsupport.GetIntValue( '&previewheight' ),
  1193. 'fix': True,
  1194. 'focus': False,
  1195. 'watch': True,
  1196. 'position': 'end'
  1197. }
  1198. vimsupport.OpenFilename( __file__, options )
  1199. vim_command.assert_has_exact_calls( [
  1200. call( '12split {0}'.format( __file__ ) ),
  1201. call( "exec "
  1202. "'au BufEnter <buffer> :silent! checktime {0}'".format( __file__ ) ),
  1203. call( 'silent! normal! Gzz' ),
  1204. call( 'silent! wincmd p' )
  1205. ] )
  1206. vim_current.buffer.options.__setitem__.assert_has_exact_calls( [
  1207. call( 'autoread', True ),
  1208. ] )
  1209. vim_current.window.options.__setitem__.assert_has_exact_calls( [
  1210. call( 'winfixheight', True )
  1211. ] )
  1212. def GetUnsavedAndSpecifiedBufferData_EncodedUnicodeCharsInBuffers_test():
  1213. filepath = os.path.realpath( 'filename' )
  1214. contents = [ ToBytes( u'abc' ), ToBytes( u'fДa' ) ]
  1215. vim_buffer = VimBuffer( filepath, contents = contents )
  1216. with patch( 'vim.buffers', [ vim_buffer ] ):
  1217. assert_that( vimsupport.GetUnsavedAndSpecifiedBufferData( filepath ),
  1218. has_entry( filepath,
  1219. has_entry( u'contents', u'abc\nfДa\n' ) ) )
  1220. def GetBufferFilepath_NoBufferName_UnicodeWorkingDirectory_test():
  1221. vim_buffer = VimBuffer( '', number = 42 )
  1222. unicode_dir = PathToTestFile( u'uni¢𐍈d€' )
  1223. with CurrentWorkingDirectory( unicode_dir ):
  1224. assert_that( vimsupport.GetBufferFilepath( vim_buffer ),
  1225. equal_to( os.path.join( unicode_dir, '42' ) ) )
  1226. # NOTE: Vim returns byte offsets for columns, not actual character columns. This
  1227. # makes 'ДД' have 4 columns: column 0, column 2 and column 4.
  1228. @patch( 'vim.current.line', ToBytes( 'ДДaa' ) )
  1229. @patch( 'ycm.vimsupport.CurrentColumn', side_effect = [ 4 ] )
  1230. def TextBeforeCursor_EncodedUnicode_test( *args ):
  1231. eq_( vimsupport.TextBeforeCursor(), u'ДД' )
  1232. # NOTE: Vim returns byte offsets for columns, not actual character columns. This
  1233. # makes 'ДД' have 4 columns: column 0, column 2 and column 4.
  1234. @patch( 'vim.current.line', ToBytes( 'aaДД' ) )
  1235. @patch( 'ycm.vimsupport.CurrentColumn', side_effect = [ 2 ] )
  1236. def TextAfterCursor_EncodedUnicode_test( *args ):
  1237. eq_( vimsupport.TextAfterCursor(), u'ДД' )
  1238. @patch( 'vim.current.line', ToBytes( 'fДa' ) )
  1239. def CurrentLineContents_EncodedUnicode_test( *args ):
  1240. eq_( vimsupport.CurrentLineContents(), u'fДa' )
  1241. @patch( 'vim.eval', side_effect = lambda x: x )
  1242. def VimExpressionToPythonType_IntAsUnicode_test( *args ):
  1243. eq_( vimsupport.VimExpressionToPythonType( '123' ), 123 )
  1244. @patch( 'vim.eval', side_effect = lambda x: x )
  1245. def VimExpressionToPythonType_IntAsBytes_test( *args ):
  1246. eq_( vimsupport.VimExpressionToPythonType( ToBytes( '123' ) ), 123 )
  1247. @patch( 'vim.eval', side_effect = lambda x: x )
  1248. def VimExpressionToPythonType_StringAsUnicode_test( *args ):
  1249. eq_( vimsupport.VimExpressionToPythonType( 'foo' ), 'foo' )
  1250. @patch( 'vim.eval', side_effect = lambda x: x )
  1251. def VimExpressionToPythonType_StringAsBytes_test( *args ):
  1252. eq_( vimsupport.VimExpressionToPythonType( ToBytes( 'foo' ) ), 'foo' )
  1253. @patch( 'vim.eval', side_effect = lambda x: x )
  1254. def VimExpressionToPythonType_ListPassthrough_test( *args ):
  1255. eq_( vimsupport.VimExpressionToPythonType( [ 1, 2 ] ), [ 1, 2 ] )
  1256. @patch( 'vim.eval', side_effect = lambda x: x )
  1257. def VimExpressionToPythonType_ObjectPassthrough_test( *args ):
  1258. eq_( vimsupport.VimExpressionToPythonType( { 1: 2 } ), { 1: 2 } )
  1259. @patch( 'vim.eval', side_effect = lambda x: x )
  1260. def VimExpressionToPythonType_GeneratorPassthrough_test( *args ):
  1261. gen = ( x**2 for x in [ 1, 2, 3 ] )
  1262. eq_( vimsupport.VimExpressionToPythonType( gen ), gen )
  1263. @patch( 'vim.eval',
  1264. new_callable = ExtendedMock,
  1265. side_effect = [ None, 2, None ] )
  1266. def SelectFromList_LastItem_test( vim_eval ):
  1267. eq_( vimsupport.SelectFromList( 'test', [ 'a', 'b' ] ),
  1268. 1 )
  1269. vim_eval.assert_has_exact_calls( [
  1270. call( 'inputsave()' ),
  1271. call( 'inputlist( ["test", "1: a", "2: b"] )' ),
  1272. call( 'inputrestore()' )
  1273. ] )
  1274. @patch( 'vim.eval',
  1275. new_callable = ExtendedMock,
  1276. side_effect = [ None, 1, None ] )
  1277. def SelectFromList_FirstItem_test( vim_eval ):
  1278. eq_( vimsupport.SelectFromList( 'test', [ 'a', 'b' ] ),
  1279. 0 )
  1280. vim_eval.assert_has_exact_calls( [
  1281. call( 'inputsave()' ),
  1282. call( 'inputlist( ["test", "1: a", "2: b"] )' ),
  1283. call( 'inputrestore()' )
  1284. ] )
  1285. @patch( 'vim.eval', side_effect = [ None, 3, None ] )
  1286. def SelectFromList_OutOfRange_test( vim_eval ):
  1287. assert_that( calling( vimsupport.SelectFromList).with_args( 'test',
  1288. [ 'a', 'b' ] ),
  1289. raises( RuntimeError, vimsupport.NO_SELECTION_MADE_MSG ) )
  1290. @patch( 'vim.eval', side_effect = [ None, 0, None ] )
  1291. def SelectFromList_SelectPrompt_test( vim_eval ):
  1292. assert_that( calling( vimsupport.SelectFromList ).with_args( 'test',
  1293. [ 'a', 'b' ] ),
  1294. raises( RuntimeError, vimsupport.NO_SELECTION_MADE_MSG ) )
  1295. @patch( 'vim.eval', side_effect = [ None, -199, None ] )
  1296. def SelectFromList_Negative_test( vim_eval ):
  1297. assert_that( calling( vimsupport.SelectFromList ).with_args( 'test',
  1298. [ 'a', 'b' ] ),
  1299. raises( RuntimeError, vimsupport.NO_SELECTION_MADE_MSG ) )
  1300. def Filetypes_IntegerFiletype_test():
  1301. current_buffer = VimBuffer( 'buffer', number = 1, filetype = '42' )
  1302. with MockVimBuffers( [ current_buffer ], current_buffer ):
  1303. assert_that( vimsupport.CurrentFiletypes(), contains( '42' ) )
  1304. assert_that( vimsupport.GetBufferFiletypes( 1 ), contains( '42' ) )
  1305. assert_that( vimsupport.FiletypesForBuffer( current_buffer ),
  1306. contains( '42' ) )
  1307. @patch( 'ycm.vimsupport.VariableExists', return_value = False )
  1308. @patch( 'ycm.vimsupport.SearchInCurrentBuffer', return_value = 0 )
  1309. @patch( 'vim.current' )
  1310. def InsertNamespace_insert_test( vim_current, *args ):
  1311. contents = [ '',
  1312. 'namespace Taqueria {',
  1313. '',
  1314. ' int taco = Math' ]
  1315. vim_current.buffer = VimBuffer( '', contents = contents )
  1316. vimsupport.InsertNamespace( 'System' )
  1317. expected_buffer = [ 'using System;',
  1318. '',
  1319. 'namespace Taqueria {',
  1320. '',
  1321. ' int taco = Math' ]
  1322. AssertBuffersAreEqualAsBytes( expected_buffer, vim_current.buffer )
  1323. @patch( 'ycm.vimsupport.VariableExists', return_value = False )
  1324. @patch( 'ycm.vimsupport.SearchInCurrentBuffer', return_value = 2 )
  1325. @patch( 'vim.current' )
  1326. def InsertNamespace_append_test( vim_current, *args ):
  1327. contents = [ 'namespace Taqueria {',
  1328. ' using System;',
  1329. '',
  1330. ' class Tasty {',
  1331. ' int taco;',
  1332. ' List salad = new List' ]
  1333. vim_current.buffer = VimBuffer( '', contents = contents )
  1334. vimsupport.InsertNamespace( 'System.Collections' )
  1335. expected_buffer = [ 'namespace Taqueria {',
  1336. ' using System;',
  1337. ' using System.Collections;',
  1338. '',
  1339. ' class Tasty {',
  1340. ' int taco;',
  1341. ' List salad = new List' ]
  1342. AssertBuffersAreEqualAsBytes( expected_buffer, vim_current.buffer )
  1343. def EscapedFilepath_test():
  1344. eq_( vimsupport.EscapedFilepath( '/path/ with /sp ac es' ),
  1345. '/path/\ with\ /sp\ ac\ es' )
  1346. eq_( vimsupport.EscapedFilepath( ' relative path/ with / spaces ' ),
  1347. '\ relative\ path/\ with\ /\ spaces\ ' )
  1348. @patch( 'ycmd.user_options_store._USER_OPTIONS',
  1349. { 'goto_buffer_command': 'same-buffer' } )
  1350. @patch( 'vim.command', new_callable = ExtendedMock )
  1351. def JumpToLocation_SameFile_SameBuffer_NoSwapFile_test( vim_command ):
  1352. # No 'u' prefix for the current buffer name string to simulate Vim returning
  1353. # bytes on Python 2 but unicode on Python 3.
  1354. current_buffer = VimBuffer( 'uni¢𐍈d€' )
  1355. with MockVimBuffers( [ current_buffer ], current_buffer ) as vim:
  1356. vimsupport.JumpToLocation( os.path.realpath( u'uni¢𐍈d€' ), 2, 5 )
  1357. assert_that( vim.current.window.cursor, equal_to( ( 2, 4 ) ) )
  1358. vim_command.assert_has_exact_calls( [
  1359. call( 'normal! m\'' ),
  1360. call( 'normal! zz' )
  1361. ] )
  1362. @patch( 'ycmd.user_options_store._USER_OPTIONS',
  1363. { 'goto_buffer_command': 'same-buffer' } )
  1364. @patch( 'vim.command', new_callable = ExtendedMock )
  1365. def JumpToLocation_DifferentFile_SameBuffer_Unmodified_test( vim_command ):
  1366. current_buffer = VimBuffer( 'uni¢𐍈d€' )
  1367. with MockVimBuffers( [ current_buffer ], current_buffer ) as vim:
  1368. target_name = os.path.realpath( u'different_uni¢𐍈d€' )
  1369. vimsupport.JumpToLocation( target_name, 2, 5 )
  1370. assert_that( vim.current.window.cursor, equal_to( ( 2, 4 ) ) )
  1371. vim_command.assert_has_exact_calls( [
  1372. call( 'normal! m\'' ),
  1373. call( u'keepjumps edit {0}'.format( target_name ) ),
  1374. call( 'normal! zz' )
  1375. ] )
  1376. @patch( 'ycmd.user_options_store._USER_OPTIONS',
  1377. { 'goto_buffer_command': 'same-buffer' } )
  1378. @patch( 'vim.command', new_callable = ExtendedMock )
  1379. def JumpToLocation_DifferentFile_SameBuffer_Modified_CannotHide_test(
  1380. vim_command ):
  1381. current_buffer = VimBuffer( 'uni¢𐍈d€', modified = True )
  1382. with MockVimBuffers( [ current_buffer ], current_buffer ) as vim:
  1383. target_name = os.path.realpath( u'different_uni¢𐍈d€' )
  1384. vimsupport.JumpToLocation( target_name, 2, 5 )
  1385. assert_that( vim.current.window.cursor, equal_to( ( 2, 4 ) ) )
  1386. vim_command.assert_has_exact_calls( [
  1387. call( 'normal! m\'' ),
  1388. call( u'keepjumps split {0}'.format( target_name ) ),
  1389. call( 'normal! zz' )
  1390. ] )
  1391. @patch( 'ycmd.user_options_store._USER_OPTIONS',
  1392. { 'goto_buffer_command': 'same-buffer' } )
  1393. @patch( 'vim.command', new_callable = ExtendedMock )
  1394. def JumpToLocation_DifferentFile_SameBuffer_Modified_CanHide_test(
  1395. vim_command ):
  1396. current_buffer = VimBuffer( 'uni¢𐍈d€', modified = True, bufhidden = "hide" )
  1397. with MockVimBuffers( [ current_buffer ], current_buffer ) as vim:
  1398. target_name = os.path.realpath( u'different_uni¢𐍈d€' )
  1399. vimsupport.JumpToLocation( target_name, 2, 5 )
  1400. assert_that( vim.current.window.cursor, equal_to( ( 2, 4 ) ) )
  1401. vim_command.assert_has_exact_calls( [
  1402. call( 'normal! m\'' ),
  1403. call( u'keepjumps edit {0}'.format( target_name ) ),
  1404. call( 'normal! zz' )
  1405. ] )
  1406. @patch( 'ycmd.user_options_store._USER_OPTIONS',
  1407. { 'goto_buffer_command': 'same-buffer' } )
  1408. @patch( 'vim.error', VimError )
  1409. @patch( 'vim.command',
  1410. side_effect = [ None, VimError( 'Unknown code' ), None ] )
  1411. def JumpToLocation_DifferentFile_SameBuffer_SwapFile_Unexpected_test(
  1412. vim_command ):
  1413. current_buffer = VimBuffer( 'uni¢𐍈d€' )
  1414. with MockVimBuffers( [ current_buffer ], current_buffer ):
  1415. assert_that(
  1416. calling( vimsupport.JumpToLocation ).with_args(
  1417. os.path.realpath( u'different_uni¢𐍈d€' ), 2, 5 ),
  1418. raises( VimError, 'Unknown code' )
  1419. )
  1420. @patch( 'ycmd.user_options_store._USER_OPTIONS',
  1421. { 'goto_buffer_command': 'same-buffer' } )
  1422. @patch( 'vim.error', VimError )
  1423. @patch( 'vim.command',
  1424. new_callable = ExtendedMock,
  1425. side_effect = [ None, VimError( 'E325' ), None ] )
  1426. def JumpToLocation_DifferentFile_SameBuffer_SwapFile_Quit_test( vim_command ):
  1427. current_buffer = VimBuffer( 'uni¢𐍈d€' )
  1428. with MockVimBuffers( [ current_buffer ], current_buffer ):
  1429. target_name = os.path.realpath( u'different_uni¢𐍈d€' )
  1430. vimsupport.JumpToLocation( target_name, 2, 5 )
  1431. vim_command.assert_has_exact_calls( [
  1432. call( 'normal! m\'' ),
  1433. call( u'keepjumps edit {0}'.format( target_name ) )
  1434. ] )
  1435. @patch( 'ycmd.user_options_store._USER_OPTIONS',
  1436. { 'goto_buffer_command': 'same-buffer' } )
  1437. @patch( 'vim.error', VimError )
  1438. @patch( 'vim.command',
  1439. new_callable = ExtendedMock,
  1440. side_effect = [ None, KeyboardInterrupt, None ] )
  1441. def JumpToLocation_DifferentFile_SameBuffer_SwapFile_Abort_test( vim_command ):
  1442. current_buffer = VimBuffer( 'uni¢𐍈d€' )
  1443. with MockVimBuffers( [ current_buffer ], current_buffer ):
  1444. target_name = os.path.realpath( u'different_uni¢𐍈d€' )
  1445. vimsupport.JumpToLocation( target_name, 2, 5 )
  1446. vim_command.assert_has_exact_calls( [
  1447. call( 'normal! m\'' ),
  1448. call( u'keepjumps edit {0}'.format( target_name ) )
  1449. ] )
  1450. @patch( 'ycmd.user_options_store._USER_OPTIONS',
  1451. { 'goto_buffer_command': 'new-or-existing-tab' } )
  1452. @patch( 'vim.command', new_callable = ExtendedMock )
  1453. def JumpToLocation_DifferentFile_NewOrExistingTab_NotAlreadyOpened_test(
  1454. vim_command ):
  1455. current_buffer = VimBuffer( 'uni¢𐍈d€' )
  1456. with MockVimBuffers( [ current_buffer ], current_buffer ):
  1457. target_name = os.path.realpath( u'different_uni¢𐍈d€' )
  1458. vimsupport.JumpToLocation( target_name, 2, 5 )
  1459. vim_command.assert_has_exact_calls( [
  1460. call( 'normal! m\'' ),
  1461. call( u'keepjumps tabedit {0}'.format( target_name ) ),
  1462. call( 'normal! zz' )
  1463. ] )
  1464. @patch( 'ycmd.user_options_store._USER_OPTIONS',
  1465. { 'goto_buffer_command': 'new-or-existing-tab' } )
  1466. @patch( 'vim.command', new_callable = ExtendedMock )
  1467. def JumpToLocation_DifferentFile_NewOrExistingTab_AlreadyOpened_test(
  1468. vim_command ):
  1469. current_buffer = VimBuffer( 'uni¢𐍈d€' )
  1470. different_buffer = VimBuffer( 'different_uni¢𐍈d€' )
  1471. current_window = MagicMock( buffer = current_buffer )
  1472. different_window = MagicMock( buffer = different_buffer )
  1473. current_tab = MagicMock( windows = [ current_window, different_window ] )
  1474. with patch( 'vim.tabpages', [ current_tab ] ):
  1475. with MockVimBuffers( [ current_buffer, different_buffer ],
  1476. current_buffer ) as vim:
  1477. vimsupport.JumpToLocation( os.path.realpath( u'different_uni¢𐍈d€' ),
  1478. 2, 5 )
  1479. assert_that( vim.current.tabpage, equal_to( current_tab ) )
  1480. assert_that( vim.current.window, equal_to( different_window ) )
  1481. assert_that( vim.current.window.cursor, equal_to( ( 2, 4 ) ) )
  1482. vim_command.assert_has_exact_calls( [
  1483. call( 'normal! m\'' ),
  1484. call( 'normal! zz' )
  1485. ] )