hl.lua 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. --- Apply highlight group to range of text.
  32. ---
  33. ---@param bufnr integer Buffer number to apply highlighting to
  34. ---@param ns integer Namespace to add highlight to
  35. ---@param higroup string Highlight group to use for highlighting
  36. ---@param start integer[]|string Start of region as a (line, column) tuple or string accepted by |getpos()|
  37. ---@param finish integer[]|string End of region as a (line, column) tuple or string accepted by |getpos()|
  38. ---@param opts? vim.hl.range.Opts
  39. function M.range(bufnr, ns, higroup, start, finish, opts)
  40. opts = opts or {}
  41. local regtype = opts.regtype or 'v'
  42. local inclusive = opts.inclusive or false
  43. local priority = opts.priority or M.priorities.user
  44. local v_maxcol = vim.v.maxcol
  45. local pos1 = type(start) == 'string' and vim.fn.getpos(start)
  46. or {
  47. bufnr,
  48. start[1] + 1,
  49. start[2] ~= -1 and start[2] ~= v_maxcol and start[2] + 1 or v_maxcol,
  50. 0,
  51. }
  52. local pos2 = type(finish) == 'string' and vim.fn.getpos(finish)
  53. or {
  54. bufnr,
  55. finish[1] + 1,
  56. finish[2] ~= -1 and start[2] ~= v_maxcol and finish[2] + 1 or v_maxcol,
  57. 0,
  58. }
  59. local buf_line_count = vim.api.nvim_buf_line_count(bufnr)
  60. pos1[2] = math.min(pos1[2], buf_line_count)
  61. pos2[2] = math.min(pos2[2], buf_line_count)
  62. if pos1[2] <= 0 or pos1[3] <= 0 or pos2[2] <= 0 or pos2[3] <= 0 then
  63. return
  64. end
  65. vim._with({ buf = bufnr }, function()
  66. if pos1[3] ~= v_maxcol then
  67. local max_col1 = vim.fn.col({ pos1[2], '$' })
  68. pos1[3] = math.min(pos1[3], max_col1)
  69. end
  70. if pos2[3] ~= v_maxcol then
  71. local max_col2 = vim.fn.col({ pos2[2], '$' })
  72. pos2[3] = math.min(pos2[3], max_col2)
  73. end
  74. end)
  75. local region = vim.fn.getregionpos(pos1, pos2, {
  76. type = regtype,
  77. exclusive = not inclusive,
  78. eol = true,
  79. })
  80. -- For non-blockwise selection, use a single extmark.
  81. if regtype == 'v' or regtype == 'V' then
  82. region = { { region[1][1], region[#region][2] } }
  83. if
  84. regtype == 'V'
  85. or region[1][2][2] == pos1[2] and pos1[3] == v_maxcol
  86. or region[1][2][2] == pos2[2] and pos2[3] == v_maxcol
  87. then
  88. region[1][2][2] = region[1][2][2] + 1
  89. region[1][2][3] = 0
  90. end
  91. end
  92. for _, res in ipairs(region) do
  93. local start_row = res[1][2] - 1
  94. local start_col = res[1][3] - 1
  95. local end_row = res[2][2] - 1
  96. local end_col = res[2][3]
  97. api.nvim_buf_set_extmark(bufnr, ns, start_row, start_col, {
  98. hl_group = higroup,
  99. end_row = end_row,
  100. end_col = end_col,
  101. priority = priority,
  102. strict = false,
  103. })
  104. end
  105. end
  106. local yank_ns = api.nvim_create_namespace('hlyank')
  107. local yank_timer --- @type uv.uv_timer_t?
  108. local yank_cancel --- @type fun()?
  109. --- Highlight the yanked text during a |TextYankPost| event.
  110. ---
  111. --- Add the following to your `init.vim`:
  112. ---
  113. --- ```vim
  114. --- autocmd TextYankPost * silent! lua vim.hl.on_yank {higroup='Visual', timeout=300}
  115. --- ```
  116. ---
  117. --- @param opts table|nil Optional parameters
  118. --- - higroup highlight group for yanked region (default "IncSearch")
  119. --- - timeout time in ms before highlight is cleared (default 150)
  120. --- - on_macro highlight when executing macro (default false)
  121. --- - on_visual highlight when yanking visual selection (default true)
  122. --- - event event structure (default vim.v.event)
  123. --- - priority integer priority (default |vim.hl.priorities|`.user`)
  124. function M.on_yank(opts)
  125. vim.validate('opts', opts, 'table', true)
  126. opts = opts or {}
  127. local event = opts.event or vim.v.event
  128. local on_macro = opts.on_macro or false
  129. local on_visual = (opts.on_visual ~= false)
  130. if not on_macro and vim.fn.reg_executing() ~= '' then
  131. return
  132. end
  133. if event.operator ~= 'y' or event.regtype == '' then
  134. return
  135. end
  136. if not on_visual and event.visual then
  137. return
  138. end
  139. local higroup = opts.higroup or 'IncSearch'
  140. local timeout = opts.timeout or 150
  141. local bufnr = vim.api.nvim_get_current_buf()
  142. local winid = vim.api.nvim_get_current_win()
  143. if yank_timer then
  144. yank_timer:close()
  145. assert(yank_cancel)
  146. yank_cancel()
  147. end
  148. vim.api.nvim__ns_set(yank_ns, { wins = { winid } })
  149. M.range(bufnr, yank_ns, higroup, "'[", "']", {
  150. regtype = event.regtype,
  151. inclusive = event.inclusive,
  152. priority = opts.priority or M.priorities.user,
  153. })
  154. yank_cancel = function()
  155. yank_timer = nil
  156. yank_cancel = nil
  157. pcall(vim.api.nvim_buf_clear_namespace, bufnr, yank_ns, 0, -1)
  158. pcall(vim.api.nvim__ns_set, { wins = {} })
  159. end
  160. yank_timer = vim.defer_fn(yank_cancel, timeout)
  161. end
  162. return M