hl.lua 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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[]|string Start of region as a (line, column) tuple or string accepted by |getpos()|
  41. ---@param finish 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. region = { { region[1][1], region[#region][2] } }
  92. if
  93. regtype == 'V'
  94. or region[1][2][2] == pos1[2] and pos1[3] == v_maxcol
  95. or region[1][2][2] == pos2[2] and pos2[3] == v_maxcol
  96. then
  97. region[1][2][2] = region[1][2][2] + 1
  98. region[1][2][3] = 0
  99. end
  100. end
  101. local extmarks = {} --- @type integer[]
  102. for _, res in ipairs(region) do
  103. local start_row = res[1][2] - 1
  104. local start_col = res[1][3] - 1
  105. local end_row = res[2][2] - 1
  106. local end_col = res[2][3]
  107. table.insert(
  108. extmarks,
  109. api.nvim_buf_set_extmark(bufnr, ns, start_row, start_col, {
  110. hl_group = higroup,
  111. end_row = end_row,
  112. end_col = end_col,
  113. priority = priority,
  114. strict = false,
  115. })
  116. )
  117. end
  118. local range_hl_clear = function()
  119. if not api.nvim_buf_is_valid(bufnr) then
  120. return
  121. end
  122. for _, mark in ipairs(extmarks) do
  123. api.nvim_buf_del_extmark(bufnr, ns, mark)
  124. end
  125. end
  126. if timeout ~= -1 then
  127. local range_timer = vim.defer_fn(range_hl_clear, timeout)
  128. return range_timer, range_hl_clear
  129. end
  130. end
  131. local yank_timer --- @type uv.uv_timer_t?
  132. local yank_hl_clear --- @type fun()?
  133. local yank_ns = api.nvim_create_namespace('nvim.hlyank')
  134. --- Highlight the yanked text during a |TextYankPost| event.
  135. ---
  136. --- Add the following to your `init.vim`:
  137. ---
  138. --- ```vim
  139. --- autocmd TextYankPost * silent! lua vim.hl.on_yank {higroup='Visual', timeout=300}
  140. --- ```
  141. ---
  142. --- @param opts table|nil Optional parameters
  143. --- - higroup highlight group for yanked region (default "IncSearch")
  144. --- - timeout time in ms before highlight is cleared (default 150)
  145. --- - on_macro highlight when executing macro (default false)
  146. --- - on_visual highlight when yanking visual selection (default true)
  147. --- - event event structure (default vim.v.event)
  148. --- - priority integer priority (default |vim.hl.priorities|`.user`)
  149. function M.on_yank(opts)
  150. vim.validate('opts', opts, 'table', true)
  151. opts = opts or {}
  152. local event = opts.event or vim.v.event
  153. local on_macro = opts.on_macro or false
  154. local on_visual = (opts.on_visual ~= false)
  155. if not on_macro and vim.fn.reg_executing() ~= '' then
  156. return
  157. end
  158. if event.operator ~= 'y' or event.regtype == '' then
  159. return
  160. end
  161. if not on_visual and event.visual then
  162. return
  163. end
  164. local higroup = opts.higroup or 'IncSearch'
  165. local bufnr = vim.api.nvim_get_current_buf()
  166. local winid = vim.api.nvim_get_current_win()
  167. if yank_timer and not yank_timer:is_closing() then
  168. yank_timer:close()
  169. assert(yank_hl_clear)
  170. yank_hl_clear()
  171. end
  172. vim.api.nvim__ns_set(yank_ns, { wins = { winid } })
  173. yank_timer, yank_hl_clear = M.range(bufnr, yank_ns, higroup, "'[", "']", {
  174. regtype = event.regtype,
  175. inclusive = true,
  176. priority = opts.priority or M.priorities.user,
  177. timeout = opts.timeout or 150,
  178. })
  179. end
  180. return M