highlight.lua 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. local api = vim.api
  2. local M = {}
  3. M.priorities = {
  4. syntax = 50,
  5. treesitter = 100,
  6. diagnostics = 150,
  7. user = 200,
  8. }
  9. ---@private
  10. function M.create(higroup, hi_info, default)
  11. local options = {}
  12. -- TODO: Add validation
  13. for k, v in pairs(hi_info) do
  14. table.insert(options, string.format("%s=%s", k, v))
  15. end
  16. vim.cmd(string.format([[highlight %s %s %s]], default and "default" or "", higroup, table.concat(options, " ")))
  17. end
  18. ---@private
  19. function M.link(higroup, link_to, force)
  20. vim.cmd(string.format([[highlight%s link %s %s]], force and "!" or " default", higroup, link_to))
  21. end
  22. --- Highlight range between two positions
  23. ---
  24. ---@param bufnr number of buffer to apply highlighting to
  25. ---@param ns namespace to add highlight to
  26. ---@param higroup highlight group to use for highlighting
  27. ---@param start first position (tuple {line,col})
  28. ---@param finish second position (tuple {line,col})
  29. ---@param opts table with options:
  30. -- - regtype type of range (:help setreg, default charwise)
  31. -- - inclusive boolean indicating whether the range is end-inclusive (default false)
  32. -- - priority number indicating priority of highlight (default priorities.user)
  33. function M.range(bufnr, ns, higroup, start, finish, opts)
  34. opts = opts or {}
  35. local regtype = opts.regtype or "v"
  36. local inclusive = opts.inclusive or false
  37. local priority = opts.priority or M.priorities.user
  38. -- sanity check
  39. if start[2] < 0 or finish[1] < start[1] then
  40. return
  41. end
  42. local region = vim.region(bufnr, start, finish, regtype, inclusive)
  43. for linenr, cols in pairs(region) do
  44. local end_row
  45. if cols[2] == -1 then
  46. end_row = linenr + 1
  47. cols[2] = 0
  48. end
  49. api.nvim_buf_set_extmark(bufnr, ns, linenr, cols[1], {
  50. hl_group = higroup,
  51. end_row = end_row,
  52. end_col = cols[2],
  53. priority = priority,
  54. strict = false,
  55. })
  56. end
  57. end
  58. local yank_ns = api.nvim_create_namespace('hlyank')
  59. local yank_timer
  60. --- Highlight the yanked region
  61. ---
  62. --- use from init.vim via
  63. --- au TextYankPost * lua vim.highlight.on_yank()
  64. --- customize highlight group and timeout via
  65. --- au TextYankPost * lua vim.highlight.on_yank {higroup="IncSearch", timeout=150}
  66. --- customize conditions (here: do not highlight a visual selection) via
  67. --- au TextYankPost * lua vim.highlight.on_yank {on_visual=false}
  68. ---
  69. -- @param opts table with options controlling the highlight:
  70. -- - higroup highlight group for yanked region (default "IncSearch")
  71. -- - timeout time in ms before highlight is cleared (default 150)
  72. -- - on_macro highlight when executing macro (default false)
  73. -- - on_visual highlight when yanking visual selection (default true)
  74. -- - event event structure (default vim.v.event)
  75. function M.on_yank(opts)
  76. vim.validate({
  77. opts = {
  78. opts,
  79. function(t)
  80. if t == nil then
  81. return true
  82. else
  83. return type(t) == "table"
  84. end
  85. end,
  86. "a table or nil to configure options (see `:h highlight.on_yank`)",
  87. },
  88. })
  89. opts = opts or {}
  90. local event = opts.event or vim.v.event
  91. local on_macro = opts.on_macro or false
  92. local on_visual = (opts.on_visual ~= false)
  93. if not on_macro and vim.fn.reg_executing() ~= "" then
  94. return
  95. end
  96. if event.operator ~= "y" or event.regtype == "" then
  97. return
  98. end
  99. if not on_visual and event.visual then
  100. return
  101. end
  102. local higroup = opts.higroup or "IncSearch"
  103. local timeout = opts.timeout or 150
  104. local bufnr = api.nvim_get_current_buf()
  105. api.nvim_buf_clear_namespace(bufnr, yank_ns, 0, -1)
  106. if yank_timer then
  107. yank_timer:close()
  108. end
  109. local pos1 = vim.fn.getpos("'[")
  110. local pos2 = vim.fn.getpos("']")
  111. pos1 = { pos1[2] - 1, pos1[3] - 1 + pos1[4] }
  112. pos2 = { pos2[2] - 1, pos2[3] - 1 + pos2[4] }
  113. M.range(
  114. bufnr,
  115. yank_ns,
  116. higroup,
  117. pos1,
  118. pos2,
  119. { regtype = event.regtype, inclusive = event.inclusive, priority = M.priorities.user }
  120. )
  121. yank_timer = vim.defer_fn(function()
  122. yank_timer = nil
  123. if api.nvim_buf_is_valid(bufnr) then
  124. api.nvim_buf_clear_namespace(bufnr, yank_ns, 0, -1)
  125. end
  126. end, timeout)
  127. end
  128. return M