hl_spec.lua 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. end)
  28. it('works with charwise selection', function()
  29. exec_lua(function()
  30. local ns = vim.api.nvim_create_namespace('')
  31. vim.hl.range(0, ns, 'Search', { 1, 5 }, { 3, 10 })
  32. end)
  33. screen:expect([[
  34. ^asdfghjkl{1:$} |
  35. «口{10:=口»}{100:$} |
  36. {10:qwertyuiop}{100:$} |
  37. {10:口口=口}口{1:$} |
  38. zxcvbnm{1:$} |
  39. |
  40. ]])
  41. end)
  42. it('works with linewise selection', function()
  43. exec_lua(function()
  44. local ns = vim.api.nvim_create_namespace('')
  45. vim.hl.range(0, ns, 'Search', { 0, 0 }, { 4, 0 }, { regtype = 'V' })
  46. end)
  47. screen:expect([[
  48. {10:^asdfghjkl}{100:$} |
  49. {10:«口=口»}{100:$} |
  50. {10:qwertyuiop}{100:$} |
  51. {10:口口=口口}{100:$} |
  52. {10:zxcvbnm}{100:$} |
  53. |
  54. ]])
  55. end)
  56. it('works with blockwise selection', function()
  57. exec_lua(function()
  58. local ns = vim.api.nvim_create_namespace('')
  59. vim.hl.range(0, ns, 'Search', { 0, 0 }, { 4, 4 }, { regtype = '\022' })
  60. end)
  61. screen:expect([[
  62. {10:^asdf}ghjkl{1:$} |
  63. {10:«口=}口»{1:$} |
  64. {10:qwer}tyuiop{1:$} |
  65. {10:口口}=口口{1:$} |
  66. {10:zxcv}bnm{1:$} |
  67. |
  68. ]])
  69. end)
  70. it('works with blockwise selection with width', function()
  71. exec_lua(function()
  72. local ns = vim.api.nvim_create_namespace('')
  73. vim.hl.range(0, ns, 'Search', { 0, 4 }, { 4, 7 }, { regtype = '\0226' })
  74. end)
  75. screen:expect([[
  76. ^asdf{10:ghjkl}{1:$} |
  77. «口={10:口»}{1:$} |
  78. qwer{10:tyuiop}{1:$} |
  79. 口口{10:=口口}{1:$} |
  80. zxcv{10:bnm}{1:$} |
  81. |
  82. ]])
  83. end)
  84. it('can use -1 or v:maxcol to indicate end of line', function()
  85. exec_lua(function()
  86. local ns = vim.api.nvim_create_namespace('')
  87. vim.hl.range(0, ns, 'Search', { 0, 4 }, { 1, -1 }, {})
  88. vim.hl.range(0, ns, 'Search', { 2, 6 }, { 3, vim.v.maxcol }, {})
  89. end)
  90. screen:expect([[
  91. ^asdf{10:ghjkl}{100:$} |
  92. {10:«口=口»}{100:$} |
  93. qwerty{10:uiop}{100:$} |
  94. {10:口口=口口}{1:$} |
  95. zxcvbnm{1:$} |
  96. |
  97. ]])
  98. end)
  99. end)
  100. describe('vim.hl.on_yank', function()
  101. before_each(function()
  102. clear()
  103. end)
  104. it('does not show errors even if buffer is wiped before timeout', function()
  105. command('new')
  106. exec_lua(function()
  107. vim.hl.on_yank({
  108. timeout = 10,
  109. on_macro = true,
  110. event = { operator = 'y', regtype = 'v' },
  111. })
  112. vim.cmd('bwipeout!')
  113. end)
  114. vim.uv.sleep(10)
  115. n.feed('<cr>') -- avoid hang if error message exists
  116. eq('', eval('v:errmsg'))
  117. end)
  118. it('does not close timer twice', function()
  119. exec_lua(function()
  120. vim.hl.on_yank({ timeout = 10, on_macro = true, event = { operator = 'y' } })
  121. vim.uv.sleep(10)
  122. vim.schedule(function()
  123. vim.hl.on_yank({ timeout = 0, on_macro = true, event = { operator = 'y' } })
  124. end)
  125. end)
  126. eq('', eval('v:errmsg'))
  127. end)
  128. it('does not show in another window', function()
  129. command('vsplit')
  130. exec_lua(function()
  131. vim.api.nvim_buf_set_mark(0, '[', 1, 1, {})
  132. vim.api.nvim_buf_set_mark(0, ']', 1, 1, {})
  133. vim.hl.on_yank({ timeout = math.huge, on_macro = true, event = { operator = 'y' } })
  134. end)
  135. local ns = api.nvim_create_namespace('hlyank')
  136. local win = api.nvim_get_current_win()
  137. eq({ win }, api.nvim__ns_get(ns).wins)
  138. command('wincmd w')
  139. eq({ win }, api.nvim__ns_get(ns).wins)
  140. end)
  141. it('removes old highlight if new one is created before old one times out', function()
  142. command('vnew')
  143. exec_lua(function()
  144. vim.api.nvim_buf_set_mark(0, '[', 1, 1, {})
  145. vim.api.nvim_buf_set_mark(0, ']', 1, 1, {})
  146. vim.hl.on_yank({ timeout = math.huge, on_macro = true, event = { operator = 'y' } })
  147. end)
  148. local ns = api.nvim_create_namespace('hlyank')
  149. eq(api.nvim_get_current_win(), api.nvim__ns_get(ns).wins[1])
  150. command('wincmd w')
  151. exec_lua(function()
  152. vim.api.nvim_buf_set_mark(0, '[', 1, 1, {})
  153. vim.api.nvim_buf_set_mark(0, ']', 1, 1, {})
  154. vim.hl.on_yank({ timeout = math.huge, on_macro = true, event = { operator = 'y' } })
  155. end)
  156. local win = api.nvim_get_current_win()
  157. eq({ win }, api.nvim__ns_get(ns).wins)
  158. command('wincmd w')
  159. eq({ win }, api.nvim__ns_get(ns).wins)
  160. end)
  161. end)