marks_spec.lua 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. -- ShaDa marks saving/reading support
  2. local t = require('test.testutil')
  3. local n = require('test.functional.testnvim')()
  4. local t_shada = require('test.functional.shada.testutil')
  5. local api, nvim_command, fn, eq = n.api, n.command, n.fn, t.eq
  6. local feed = n.feed
  7. local exc_exec, exec_capture = n.exc_exec, n.exec_capture
  8. local expect_exit = n.expect_exit
  9. local reset, clear = t_shada.reset, t_shada.clear
  10. local nvim_current_line = function()
  11. return api.nvim_win_get_cursor(0)[1]
  12. end
  13. describe('ShaDa support code', function()
  14. local testfilename = 'Xtestfile-functional-shada-marks'
  15. local testfilename_2 = 'Xtestfile-functional-shada-marks-2'
  16. local non_existent_testfilename = testfilename .. '.nonexistent'
  17. before_each(function()
  18. reset()
  19. os.remove(non_existent_testfilename)
  20. local fd = io.open(testfilename, 'w')
  21. fd:write('test\n')
  22. fd:write('test2\n')
  23. fd:close()
  24. fd = io.open(testfilename_2, 'w')
  25. fd:write('test3\n')
  26. fd:write('test4\n')
  27. fd:close()
  28. end)
  29. after_each(function()
  30. clear()
  31. os.remove(testfilename)
  32. os.remove(testfilename_2)
  33. end)
  34. it('is able to dump and read back global mark', function()
  35. nvim_command('edit ' .. testfilename)
  36. nvim_command('mark A')
  37. nvim_command('2')
  38. nvim_command('kB')
  39. nvim_command('wshada')
  40. reset()
  41. nvim_command('rshada')
  42. nvim_command('normal! `A')
  43. eq(testfilename, fn.fnamemodify(api.nvim_buf_get_name(0), ':t'))
  44. eq(1, nvim_current_line())
  45. nvim_command('normal! `B')
  46. eq(2, nvim_current_line())
  47. end)
  48. it('does not dump global mark with `f0` in shada', function()
  49. nvim_command('set shada+=f0')
  50. nvim_command('edit ' .. testfilename)
  51. nvim_command('mark A')
  52. nvim_command('2')
  53. nvim_command('kB')
  54. nvim_command('wshada')
  55. reset()
  56. nvim_command('language C')
  57. eq('Vim(normal):E20: Mark not set', exc_exec('normal! `A'))
  58. end)
  59. it("does read back global mark even with `'0` and `f0` in shada", function()
  60. nvim_command('edit ' .. testfilename)
  61. nvim_command('mark A')
  62. nvim_command('2')
  63. nvim_command('kB')
  64. nvim_command('wshada')
  65. reset("set shada='0,f0")
  66. nvim_command('language C')
  67. nvim_command('normal! `A')
  68. eq(testfilename, fn.fnamemodify(api.nvim_buf_get_name(0), ':t'))
  69. eq(1, nvim_current_line())
  70. end)
  71. it('is able to dump and read back local mark', function()
  72. nvim_command('edit ' .. testfilename)
  73. nvim_command('mark a')
  74. nvim_command('2')
  75. nvim_command('kb')
  76. expect_exit(nvim_command, 'qall')
  77. reset()
  78. nvim_command('edit ' .. testfilename)
  79. nvim_command('normal! `a')
  80. eq(testfilename, fn.fnamemodify(api.nvim_buf_get_name(0), ':t'))
  81. eq(1, nvim_current_line())
  82. nvim_command('normal! `b')
  83. eq(2, nvim_current_line())
  84. end)
  85. it('is able to dump and read back mark "', function()
  86. nvim_command('edit ' .. testfilename)
  87. nvim_command('2')
  88. expect_exit(nvim_command, 'qall')
  89. reset()
  90. nvim_command('edit ' .. testfilename)
  91. nvim_command('normal! `"')
  92. eq(2, nvim_current_line())
  93. end)
  94. it('is able to dump and read back mark " from a closed tab', function()
  95. nvim_command('edit ' .. testfilename)
  96. nvim_command('tabedit ' .. testfilename_2)
  97. nvim_command('2')
  98. nvim_command('q!')
  99. expect_exit(nvim_command, 'qall')
  100. reset()
  101. nvim_command('edit ' .. testfilename_2)
  102. nvim_command('normal! `"')
  103. eq(2, nvim_current_line())
  104. end)
  105. it('is able to populate v:oldfiles', function()
  106. nvim_command('edit ' .. testfilename)
  107. local tf_full = api.nvim_buf_get_name(0)
  108. nvim_command('edit ' .. testfilename_2)
  109. local tf_full_2 = api.nvim_buf_get_name(0)
  110. expect_exit(nvim_command, 'qall')
  111. reset()
  112. local oldfiles = api.nvim_get_vvar('oldfiles')
  113. table.sort(oldfiles)
  114. eq(2, #oldfiles)
  115. eq(testfilename, oldfiles[1]:sub(-#testfilename))
  116. eq(testfilename_2, oldfiles[2]:sub(-#testfilename_2))
  117. eq(tf_full, oldfiles[1])
  118. eq(tf_full_2, oldfiles[2])
  119. nvim_command('rshada!')
  120. oldfiles = api.nvim_get_vvar('oldfiles')
  121. table.sort(oldfiles)
  122. eq(2, #oldfiles)
  123. eq(testfilename, oldfiles[1]:sub(-#testfilename))
  124. eq(testfilename_2, oldfiles[2]:sub(-#testfilename_2))
  125. eq(tf_full, oldfiles[1])
  126. eq(tf_full_2, oldfiles[2])
  127. end)
  128. it('is able to dump and restore jump list', function()
  129. nvim_command('edit ' .. testfilename_2)
  130. nvim_command('normal! G')
  131. nvim_command('normal! gg')
  132. nvim_command('edit ' .. testfilename)
  133. nvim_command('normal! G')
  134. nvim_command('normal! gg')
  135. nvim_command('enew')
  136. nvim_command('normal! gg')
  137. local saved = exec_capture('jumps')
  138. expect_exit(nvim_command, 'qall')
  139. reset()
  140. eq(saved, exec_capture('jumps'))
  141. end)
  142. it('when dumping jump list also dumps current position', function()
  143. nvim_command('edit ' .. testfilename)
  144. nvim_command('normal! G')
  145. nvim_command('split ' .. testfilename_2)
  146. nvim_command('normal! G')
  147. nvim_command('wshada')
  148. nvim_command('quit')
  149. nvim_command('rshada')
  150. nvim_command('normal! \15') -- <C-o>
  151. eq(testfilename_2, fn.bufname('%'))
  152. eq({ 2, 0 }, api.nvim_win_get_cursor(0))
  153. end)
  154. it('is able to dump and restore jump list with different times', function()
  155. nvim_command('edit ' .. testfilename_2)
  156. nvim_command('sleep 10m')
  157. nvim_command('normal! G')
  158. nvim_command('sleep 10m')
  159. nvim_command('normal! gg')
  160. nvim_command('sleep 10m')
  161. nvim_command('edit ' .. testfilename)
  162. nvim_command('sleep 10m')
  163. nvim_command('normal! G')
  164. nvim_command('sleep 10m')
  165. nvim_command('normal! gg')
  166. expect_exit(nvim_command, 'qall')
  167. reset()
  168. nvim_command('redraw')
  169. nvim_command('edit ' .. testfilename)
  170. eq(testfilename, fn.bufname('%'))
  171. eq(1, nvim_current_line())
  172. nvim_command('execute "normal! \\<C-o>"')
  173. eq(testfilename, fn.bufname('%'))
  174. eq(2, nvim_current_line())
  175. nvim_command('execute "normal! \\<C-o>"')
  176. eq(testfilename_2, fn.bufname('%'))
  177. eq(1, nvim_current_line())
  178. nvim_command('execute "normal! \\<C-o>"')
  179. eq(testfilename_2, fn.bufname('%'))
  180. eq(2, nvim_current_line())
  181. nvim_command('execute "normal! \\<C-o>"')
  182. eq(testfilename_2, fn.bufname('%'))
  183. eq(2, nvim_current_line())
  184. end)
  185. it('is able to dump and restore change list', function()
  186. nvim_command('edit ' .. testfilename)
  187. nvim_command('normal! Gra')
  188. nvim_command('normal! ggrb')
  189. expect_exit(nvim_command, 'qall!')
  190. reset()
  191. nvim_command('edit ' .. testfilename)
  192. nvim_command('normal! Gg;')
  193. -- Note: without “sync” “commands” test has good changes to fail for unknown
  194. -- reason (in first eq expected 1 is compared with 2). Any command inserted
  195. -- causes this to work properly.
  196. nvim_command('" sync')
  197. eq(1, nvim_current_line())
  198. nvim_command('normal! g;')
  199. nvim_command('" sync 2')
  200. eq(2, nvim_current_line())
  201. end)
  202. -- -c temporary sets lnum to zero to make `+/pat` work, so calling setpcmark()
  203. -- during -c used to add item with zero lnum to jump list.
  204. it('does not create incorrect file for non-existent buffers when writing from -c', function()
  205. local argv = n.new_argv {
  206. args_rm = {
  207. '-i',
  208. '--embed', -- no --embed
  209. },
  210. args = {
  211. '-i',
  212. api.nvim_get_var('tmpname'), -- Use same shada file as parent.
  213. '--cmd',
  214. 'silent edit ' .. non_existent_testfilename,
  215. '-c',
  216. 'qall',
  217. },
  218. }
  219. eq('', fn.system(argv))
  220. eq(0, exc_exec('rshada'))
  221. end)
  222. it('does not create incorrect file for non-existent buffers opened from -c', function()
  223. local argv = n.new_argv {
  224. args_rm = {
  225. '-i',
  226. '--embed', -- no --embed
  227. },
  228. args = {
  229. '-i',
  230. api.nvim_get_var('tmpname'), -- Use same shada file as parent.
  231. '-c',
  232. 'silent edit ' .. non_existent_testfilename,
  233. '-c',
  234. 'autocmd VimEnter * qall',
  235. },
  236. }
  237. eq('', fn.system(argv))
  238. eq(0, exc_exec('rshada'))
  239. end)
  240. it('updates deleted marks with :delmarks', function()
  241. nvim_command('edit ' .. testfilename)
  242. nvim_command('mark A')
  243. nvim_command('mark a')
  244. -- create a change to set the '.' mark,
  245. -- since it can't be set via :mark
  246. feed('ggifoobar<esc>')
  247. nvim_command('wshada')
  248. reset()
  249. nvim_command('edit ' .. testfilename)
  250. nvim_command('normal! `A`a`.')
  251. nvim_command('delmarks A a .')
  252. nvim_command('wshada')
  253. reset()
  254. nvim_command('edit ' .. testfilename)
  255. eq('Vim(normal):E20: Mark not set', exc_exec('normal! `A'))
  256. eq('Vim(normal):E20: Mark not set', exc_exec('normal! `a'))
  257. eq('Vim(normal):E20: Mark not set', exc_exec('normal! `.'))
  258. end)
  259. it('updates deleted marks with :delmarks!', function()
  260. nvim_command('edit ' .. testfilename)
  261. nvim_command('mark A')
  262. nvim_command('mark a')
  263. feed('ggifoobar<esc>')
  264. nvim_command('wshada')
  265. reset()
  266. nvim_command('edit ' .. testfilename)
  267. nvim_command('normal! `A`a`.')
  268. nvim_command('delmarks!')
  269. nvim_command('wshada')
  270. reset()
  271. nvim_command('edit ' .. testfilename)
  272. eq('Vim(normal):E20: Mark not set', exc_exec('normal! `a'))
  273. eq('Vim(normal):E20: Mark not set', exc_exec('normal! `.'))
  274. -- Make sure that uppercase marks aren't deleted.
  275. nvim_command('normal! `A')
  276. end)
  277. end)