hl_spec.lua 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local Screen = require('test.functional.ui.screen')
  4. local exec_lua = n.exec_lua
  5. local eq = t.eq
  6. local eval = n.eval
  7. local command = n.command
  8. local clear = n.clear
  9. local api = n.api
  10. describe('vim.hl.range', function()
  11. local screen
  12. before_each(function()
  13. clear()
  14. screen = Screen.new(60, 6)
  15. screen:add_extra_attr_ids({
  16. [100] = { foreground = Screen.colors.Blue, background = Screen.colors.Yellow, bold = true },
  17. })
  18. api.nvim_set_option_value('list', true, {})
  19. api.nvim_set_option_value('listchars', 'eol:$', {})
  20. api.nvim_buf_set_lines(0, 0, -1, true, {
  21. 'asdfghjkl',
  22. '«口=口»',
  23. 'qwertyuiop',
  24. '口口=口口',
  25. 'zxcvbnm',
  26. })
  27. screen:expect([[
  28. ^asdfghjkl{1:$} |
  29. «口=口»{1:$} |
  30. qwertyuiop{1:$} |
  31. 口口=口口{1:$} |
  32. zxcvbnm{1:$} |
  33. |
  34. ]])
  35. end)
  36. it('works with charwise selection', function()
  37. exec_lua(function()
  38. local ns = vim.api.nvim_create_namespace('')
  39. vim.hl.range(0, ns, 'Search', { 1, 5 }, { 3, 10 })
  40. end)
  41. screen:expect([[
  42. ^asdfghjkl{1:$} |
  43. «口{10:=口»}{100:$} |
  44. {10:qwertyuiop}{100:$} |
  45. {10:口口=口}口{1:$} |
  46. zxcvbnm{1:$} |
  47. |
  48. ]])
  49. end)
  50. it('works with linewise selection', function()
  51. exec_lua(function()
  52. local ns = vim.api.nvim_create_namespace('')
  53. vim.hl.range(0, ns, 'Search', { 0, 0 }, { 4, 0 }, { regtype = 'V' })
  54. end)
  55. screen:expect([[
  56. {10:^asdfghjkl}{100:$} |
  57. {10:«口=口»}{100:$} |
  58. {10:qwertyuiop}{100:$} |
  59. {10:口口=口口}{100:$} |
  60. {10:zxcvbnm}{100:$} |
  61. |
  62. ]])
  63. end)
  64. it('works with blockwise selection', function()
  65. exec_lua(function()
  66. local ns = vim.api.nvim_create_namespace('')
  67. vim.hl.range(0, ns, 'Search', { 0, 0 }, { 4, 4 }, { regtype = '\022' })
  68. end)
  69. screen:expect([[
  70. {10:^asdf}ghjkl{1:$} |
  71. {10:«口=}口»{1:$} |
  72. {10:qwer}tyuiop{1:$} |
  73. {10:口口}=口口{1:$} |
  74. {10:zxcv}bnm{1:$} |
  75. |
  76. ]])
  77. end)
  78. it('works with blockwise selection with width', function()
  79. exec_lua(function()
  80. local ns = vim.api.nvim_create_namespace('')
  81. vim.hl.range(0, ns, 'Search', { 0, 4 }, { 4, 7 }, { regtype = '\0226' })
  82. end)
  83. screen:expect([[
  84. ^asdf{10:ghjkl}{1:$} |
  85. «口={10:口»}{1:$} |
  86. qwer{10:tyuiop}{1:$} |
  87. 口口{10:=口口}{1:$} |
  88. zxcv{10:bnm}{1:$} |
  89. |
  90. ]])
  91. end)
  92. it('can use -1 or v:maxcol to indicate end of line', function()
  93. exec_lua(function()
  94. local ns = vim.api.nvim_create_namespace('')
  95. vim.hl.range(0, ns, 'Search', { 0, 4 }, { 1, -1 }, {})
  96. vim.hl.range(0, ns, 'Search', { 2, 6 }, { 3, vim.v.maxcol }, {})
  97. end)
  98. screen:expect([[
  99. ^asdf{10:ghjkl}{100:$} |
  100. {10:«口=口»}{100:$} |
  101. qwerty{10:uiop}{100:$} |
  102. {10:口口=口口}{1:$} |
  103. zxcvbnm{1:$} |
  104. |
  105. ]])
  106. end)
  107. it('removes highlight after given `timeout`', function()
  108. local timeout = 300
  109. exec_lua(function()
  110. local ns = vim.api.nvim_create_namespace('')
  111. vim.hl.range(0, ns, 'Search', { 0, 0 }, { 4, 0 }, { timeout = timeout })
  112. end)
  113. screen:expect({
  114. grid = [[
  115. {10:^asdfghjkl}{100:$} |
  116. {10:«口=口»}{100:$} |
  117. {10:qwertyuiop}{100:$} |
  118. {10:口口=口口}{1:$} |
  119. zxcvbnm{1:$} |
  120. |
  121. ]],
  122. timeout = timeout / 3,
  123. })
  124. screen:expect([[
  125. ^asdfghjkl{1:$} |
  126. «口=口»{1:$} |
  127. qwertyuiop{1:$} |
  128. 口口=口口{1:$} |
  129. zxcvbnm{1:$} |
  130. |
  131. ]])
  132. end)
  133. end)
  134. describe('vim.hl.on_yank', function()
  135. before_each(function()
  136. clear()
  137. end)
  138. it('does not show errors even if buffer is wiped before timeout', function()
  139. command('new')
  140. exec_lua(function()
  141. vim.hl.on_yank({
  142. timeout = 10,
  143. on_macro = true,
  144. event = { operator = 'y', regtype = 'v' },
  145. })
  146. vim.cmd('bwipeout!')
  147. end)
  148. vim.uv.sleep(10)
  149. n.feed('<cr>') -- avoid hang if error message exists
  150. eq('', eval('v:errmsg'))
  151. end)
  152. it('does not close timer twice', function()
  153. exec_lua(function()
  154. vim.hl.on_yank({ timeout = 10, on_macro = true, event = { operator = 'y' } })
  155. vim.uv.sleep(10)
  156. vim.schedule(function()
  157. vim.hl.on_yank({ timeout = 0, on_macro = true, event = { operator = 'y' } })
  158. end)
  159. end)
  160. eq('', eval('v:errmsg'))
  161. end)
  162. it('does not show in another window', function()
  163. command('vsplit')
  164. exec_lua(function()
  165. vim.api.nvim_buf_set_mark(0, '[', 1, 1, {})
  166. vim.api.nvim_buf_set_mark(0, ']', 1, 1, {})
  167. vim.hl.on_yank({ timeout = math.huge, on_macro = true, event = { operator = 'y' } })
  168. end)
  169. local ns = api.nvim_create_namespace('nvim.hlyank')
  170. local win = api.nvim_get_current_win()
  171. eq({ win }, api.nvim__ns_get(ns).wins)
  172. command('wincmd w')
  173. eq({ win }, api.nvim__ns_get(ns).wins)
  174. end)
  175. it('removes old highlight if new one is created before old one times out', function()
  176. command('vnew')
  177. exec_lua(function()
  178. vim.api.nvim_buf_set_mark(0, '[', 1, 1, {})
  179. vim.api.nvim_buf_set_mark(0, ']', 1, 1, {})
  180. vim.hl.on_yank({ timeout = math.huge, on_macro = true, event = { operator = 'y' } })
  181. end)
  182. local ns = api.nvim_create_namespace('nvim.hlyank')
  183. eq(api.nvim_get_current_win(), api.nvim__ns_get(ns).wins[1])
  184. command('wincmd w')
  185. exec_lua(function()
  186. vim.api.nvim_buf_set_mark(0, '[', 1, 1, {})
  187. vim.api.nvim_buf_set_mark(0, ']', 1, 1, {})
  188. vim.hl.on_yank({ timeout = math.huge, on_macro = true, event = { operator = 'y' } })
  189. end)
  190. local win = api.nvim_get_current_win()
  191. eq({ win }, api.nvim__ns_get(ns).wins)
  192. command('wincmd w')
  193. eq({ win }, api.nvim__ns_get(ns).wins)
  194. end)
  195. end)