hl.lua 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. local api = vim.api
  2. local M = {}
  3. --- Table with default priorities used for highlighting:
  4. --- - `syntax`: `50`, used for standard syntax highlighting
  5. --- - `treesitter`: `100`, used for treesitter-based highlighting
  6. --- - `semantic_tokens`: `125`, used for LSP semantic token highlighting
  7. --- - `diagnostics`: `150`, used for code analysis such as diagnostics
  8. --- - `user`: `200`, used for user-triggered highlights such as LSP document
  9. --- symbols or `on_yank` autocommands
  10. M.priorities = {
  11. syntax = 50,
  12. treesitter = 100,
  13. semantic_tokens = 125,
  14. diagnostics = 150,
  15. user = 200,
  16. }
  17. --- @class vim.hl.range.Opts
  18. --- @inlinedoc
  19. ---
  20. --- Type of range. See [getregtype()]
  21. --- (default: `'v'` i.e. charwise)
  22. --- @field regtype? string
  23. ---
  24. --- Indicates whether the range is end-inclusive
  25. --- (default: `false`)
  26. --- @field inclusive? boolean
  27. ---
  28. --- Highlight priority
  29. --- (default: `vim.hl.priorities.user`)
  30. --- @field priority? integer
  31. ---
  32. --- Time in ms before highlight is cleared
  33. --- (default: -1 no timeout)
  34. --- @field timeout? integer
  35. --- Apply highlight group to range of text.
  36. ---
  37. ---@param bufnr integer Buffer number to apply highlighting to
  38. ---@param ns integer Namespace to add highlight to
  39. ---@param higroup string Highlight group to use for highlighting
  40. ---@param start [integer,integer]|string Start of region as a (line, column) tuple or string accepted by |getpos()|
  41. ---@param finish [integer,integer]|string End of region as a (line, column) tuple or string accepted by |getpos()|
  42. ---@param opts? vim.hl.range.Opts
  43. --- @return uv.uv_timer_t? range_timer A timer which manages how much time the
  44. --- highlight has left
  45. --- @return fun()? range_clear A function which allows clearing the highlight manually.
  46. --- nil is returned if timeout is not specified
  47. function M.range(bufnr, ns, higroup, start, finish, opts)
  48. opts = opts or {}
  49. local regtype = opts.regtype or 'v'
  50. local inclusive = opts.inclusive or false
  51. local priority = opts.priority or M.priorities.user
  52. local timeout = opts.timeout or -1
  53. local v_maxcol = vim.v.maxcol
  54. local pos1 = type(start) == 'string' and vim.fn.getpos(start)
  55. or {
  56. bufnr,
  57. start[1] + 1,
  58. start[2] ~= -1 and start[2] ~= v_maxcol and start[2] + 1 or v_maxcol,
  59. 0,
  60. }
  61. local pos2 = type(finish) == 'string' and vim.fn.getpos(finish)
  62. or {
  63. bufnr,
  64. finish[1] + 1,
  65. finish[2] ~= -1 and start[2] ~= v_maxcol and finish[2] + 1 or v_maxcol,
  66. 0,
  67. }
  68. local buf_line_count = vim.api.nvim_buf_line_count(bufnr)
  69. pos1[2] = math.min(pos1[2], buf_line_count)
  70. pos2[2] = math.min(pos2[2], buf_line_count)
  71. if pos1[2] <= 0 or pos1[3] <= 0 or pos2[2] <= 0 or pos2[3] <= 0 then
  72. return
  73. end
  74. vim._with({ buf = bufnr }, function()
  75. if pos1[3] ~= v_maxcol then
  76. local max_col1 = vim.fn.col({ pos1[2], '$' })
  77. pos1[3] = math.min(pos1[3], max_col1)
  78. end
  79. if pos2[3] ~= v_maxcol then
  80. local max_col2 = vim.fn.col({ pos2[2], '$' })
  81. pos2[3] = math.min(pos2[3], max_col2)
  82. end
  83. end)
  84. local region = vim.fn.getregionpos(pos1, pos2, {
  85. type = regtype,
  86. exclusive = not inclusive,
  87. eol = true,
  88. })
  89. -- For non-blockwise selection, use a single extmark.
  90. if regtype == 'v' or regtype == 'V' then
  91. --- @type [ [integer, integer, integer, integer], [integer, integer, integer, integer]][]
  92. region = { { assert(region[1])[1], assert(region[#region])[2] } }
  93. local region1 = assert(region[1])
  94. if
  95. regtype == 'V'
  96. or region1[2][2] == pos1[2] and pos1[3] == v_maxcol
  97. or region1[2][2] == pos2[2] and pos2[3] == v_maxcol
  98. then
  99. region1[2][2] = region1[2][2] + 1
  100. region1[2][3] = 0
  101. end
  102. end
  103. local extmarks = {} --- @type integer[]
  104. for _, res in ipairs(region) do
  105. local start_row = res[1][2] - 1
  106. local start_col = res[1][3] - 1
  107. local end_row = res[2][2] - 1
  108. local end_col = res[2][3]
  109. table.insert(
  110. extmarks,
  111. api.nvim_buf_set_extmark(bufnr, ns, start_row, start_col, {
  112. hl_group = higroup,
  113. end_row = end_row,
  114. end_col = end_col,
  115. priority = priority,
  116. strict = false,
  117. })
  118. )
  119. end
  120. local range_hl_clear = function()
  121. if not api.nvim_buf_is_valid(bufnr) then
  122. return
  123. end
  124. for _, mark in ipairs(extmarks) do
  125. api.nvim_buf_del_extmark(bufnr, ns, mark)
  126. end
  127. end
  128. if timeout ~= -1 then
  129. local range_timer = vim.defer_fn(range_hl_clear, timeout)
  130. return range_timer, range_hl_clear
  131. end
  132. end
  133. local yank_timer --- @type uv.uv_timer_t?
  134. local yank_hl_clear --- @type fun()?
  135. local yank_ns = api.nvim_create_namespace('nvim.hlyank')
  136. --- Highlight the yanked text during a |TextYankPost| event.
  137. ---
  138. --- Add the following to your `init.vim`:
  139. ---
  140. --- ```vim
  141. --- autocmd TextYankPost * silent! lua vim.hl.on_yank {higroup='Visual', timeout=300}
  142. --- ```
  143. ---
  144. --- @param opts table|nil Optional parameters
  145. --- - higroup highlight group for yanked region (default "IncSearch")
  146. --- - timeout time in ms before highlight is cleared (default 150)
  147. --- - on_macro highlight when executing macro (default false)
  148. --- - on_visual highlight when yanking visual selection (default true)
  149. --- - event event structure (default vim.v.event)
  150. --- - priority integer priority (default |vim.hl.priorities|`.user`)
  151. function M.on_yank(opts)
  152. vim.validate('opts', opts, 'table', true)
  153. opts = opts or {}
  154. local event = opts.event or vim.v.event
  155. local on_macro = opts.on_macro or false
  156. local on_visual = (opts.on_visual ~= false)
  157. if not on_macro and vim.fn.reg_executing() ~= '' then
  158. return
  159. end
  160. if event.operator ~= 'y' or event.regtype == '' then
  161. return
  162. end
  163. if not on_visual and event.visual then
  164. return
  165. end
  166. local higroup = opts.higroup or 'IncSearch'
  167. local bufnr = vim.api.nvim_get_current_buf()
  168. local winid = vim.api.nvim_get_current_win()
  169. if yank_timer and not yank_timer:is_closing() then
  170. yank_timer:close()
  171. assert(yank_hl_clear)
  172. yank_hl_clear()
  173. end
  174. vim.api.nvim__ns_set(yank_ns, { wins = { winid } })
  175. yank_timer, yank_hl_clear = M.range(bufnr, yank_ns, higroup, "'[", "']", {
  176. regtype = event.regtype,
  177. inclusive = true,
  178. priority = opts.priority or M.priorities.user,
  179. timeout = opts.timeout or 150,
  180. })
  181. end
  182. return M