vimsupport_test.py 65 KB

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