macro_spec.lua 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local eq = t.eq
  4. local eval = n.eval
  5. local feed = n.feed
  6. local clear = n.clear
  7. local expect = n.expect
  8. local command = n.command
  9. local fn = n.fn
  10. local api = n.api
  11. local insert = n.insert
  12. describe('macros with default mappings', function()
  13. before_each(function()
  14. clear({ args_rm = { '--cmd' } })
  15. end)
  16. it('can be recorded and replayed', function()
  17. feed('qiahello<esc>q')
  18. expect('hello')
  19. eq('ahello', eval('@i'))
  20. feed('@i')
  21. expect('hellohello')
  22. eq('ahello', eval('@i'))
  23. end)
  24. it('applies maps', function()
  25. command('imap x l')
  26. command('nmap l a')
  27. feed('qilxxx<esc>q')
  28. expect('lll')
  29. eq('lxxx', eval('@i'))
  30. feed('@i')
  31. expect('llllll')
  32. eq('lxxx', eval('@i'))
  33. end)
  34. it('can be replayed with Q', function()
  35. insert [[
  36. hello
  37. hello
  38. hello]]
  39. feed [[gg]]
  40. feed [[qqAFOO<esc>q]]
  41. expect [[
  42. helloFOO
  43. hello
  44. hello]]
  45. feed [[Q]]
  46. expect [[
  47. helloFOOFOO
  48. hello
  49. hello]]
  50. feed [[G3Q]]
  51. expect [[
  52. helloFOOFOO
  53. hello
  54. helloFOOFOOFOO]]
  55. feed [[ggV3jQ]]
  56. expect [[
  57. helloFOOFOOFOO
  58. helloFOO
  59. helloFOOFOOFOOFOO]]
  60. end)
  61. it('can be replayed with Q and @@', function()
  62. insert [[
  63. hello
  64. hello
  65. hello]]
  66. feed [[gg]]
  67. feed [[qqAFOO<esc>q]]
  68. expect [[
  69. helloFOO
  70. hello
  71. hello]]
  72. feed [[Q]]
  73. expect [[
  74. helloFOOFOO
  75. hello
  76. hello]]
  77. feed [[G3@@]]
  78. expect [[
  79. helloFOOFOO
  80. hello
  81. helloFOOFOOFOO]]
  82. feed [[ggV2j@@]]
  83. expect [[
  84. helloFOOFOOFOO
  85. helloFOO
  86. helloFOOFOOFOOFOO]]
  87. end)
  88. it('can be replayed with @ in linewise Visual mode', function()
  89. insert [[
  90. hello
  91. hello
  92. hello]]
  93. feed [[gg]]
  94. feed [[qqAFOO<esc>qu]]
  95. expect [[
  96. hello
  97. hello
  98. hello]]
  99. feed [[qwA123<esc>qu]]
  100. expect [[
  101. hello
  102. hello
  103. hello]]
  104. feed [[V3j@q]]
  105. expect [[
  106. helloFOO
  107. helloFOO
  108. helloFOO]]
  109. feed [[ggVj@w]]
  110. expect [[
  111. helloFOO123
  112. helloFOO123
  113. helloFOO]]
  114. end)
  115. it('can be recorded and replayed in Visual mode', function()
  116. insert('foo BAR BAR foo BAR foo BAR BAR BAR foo BAR BAR')
  117. feed('0vqifofRq')
  118. eq({ 0, 1, 7, 0 }, fn.getpos('.'))
  119. eq({ 0, 1, 1, 0 }, fn.getpos('v'))
  120. feed('Q')
  121. eq({ 0, 1, 19, 0 }, fn.getpos('.'))
  122. eq({ 0, 1, 1, 0 }, fn.getpos('v'))
  123. feed('Q')
  124. eq({ 0, 1, 27, 0 }, fn.getpos('.'))
  125. eq({ 0, 1, 1, 0 }, fn.getpos('v'))
  126. feed('@i')
  127. eq({ 0, 1, 43, 0 }, fn.getpos('.'))
  128. eq({ 0, 1, 1, 0 }, fn.getpos('v'))
  129. end)
  130. it('can be recorded and replayed in Visual mode when ignorecase', function()
  131. command('set ignorecase')
  132. insert('foo BAR BAR foo BAR foo BAR BAR BAR foo BAR BAR')
  133. feed('0vqifofRq')
  134. eq({ 0, 1, 7, 0 }, fn.getpos('.'))
  135. eq({ 0, 1, 1, 0 }, fn.getpos('v'))
  136. feed('Q')
  137. eq({ 0, 1, 19, 0 }, fn.getpos('.'))
  138. eq({ 0, 1, 1, 0 }, fn.getpos('v'))
  139. feed('Q')
  140. eq({ 0, 1, 27, 0 }, fn.getpos('.'))
  141. eq({ 0, 1, 1, 0 }, fn.getpos('v'))
  142. feed('@i')
  143. eq({ 0, 1, 43, 0 }, fn.getpos('.'))
  144. eq({ 0, 1, 1, 0 }, fn.getpos('v'))
  145. end)
  146. it('can be replayed with @ in blockwise Visual mode', function()
  147. insert [[
  148. hello
  149. hello
  150. hello]]
  151. feed [[gg]]
  152. feed [[qqAFOO<esc>qu]]
  153. expect [[
  154. hello
  155. hello
  156. hello]]
  157. feed [[qwA123<esc>qu]]
  158. expect [[
  159. hello
  160. hello
  161. hello]]
  162. feed [[0<C-v>3jl@q]]
  163. expect [[
  164. heFOOllo
  165. heFOOllo
  166. heFOOllo]]
  167. feed [[gg0<C-v>j@w]]
  168. expect [[
  169. h123eFOOllo
  170. h123eFOOllo
  171. heFOOllo]]
  172. end)
  173. end)
  174. describe('immediately after a macro has finished executing,', function()
  175. before_each(function()
  176. clear()
  177. command([[let @a = 'gg0']])
  178. end)
  179. describe('reg_executing() from RPC returns an empty string', function()
  180. it('if the macro does not end with a <Nop> mapping', function()
  181. feed('@a')
  182. eq('', fn.reg_executing())
  183. end)
  184. it('if the macro ends with a <Nop> mapping', function()
  185. command('nnoremap 0 <Nop>')
  186. feed('@a')
  187. eq('', fn.reg_executing())
  188. end)
  189. end)
  190. describe('characters from a mapping are not treated as a part of the macro #18015', function()
  191. before_each(function()
  192. command('nnoremap s qa')
  193. end)
  194. it('if the macro does not end with a <Nop> mapping', function()
  195. feed('@asq') -- "q" from "s" mapping should start recording a macro instead of being no-op
  196. eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
  197. expect('')
  198. eq('', eval('@a'))
  199. end)
  200. it('if the macro ends with a <Nop> mapping', function()
  201. command('nnoremap 0 <Nop>')
  202. feed('@asq') -- "q" from "s" mapping should start recording a macro instead of being no-op
  203. eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
  204. expect('')
  205. eq('', eval('@a'))
  206. end)
  207. end)
  208. end)
  209. describe('reg_recorded()', function()
  210. before_each(clear)
  211. it('returns the correct value', function()
  212. feed [[qqyyq]]
  213. eq('q', eval('reg_recorded()'))
  214. end)
  215. end)