diff --git a/lua/trouble/util.lua b/lua/trouble/util.lua index 59a397a3..54988750 100644 --- a/lua/trouble/util.lua +++ b/lua/trouble/util.lua @@ -64,6 +64,19 @@ function M.debug(msg, ...) end end +---@param buf number +---@param row number +---@param ns number +---@param col number +---@param opts vim.api.keyset.set_extmark +---@param debug_info? any +function M.set_extmark(buf, ns, row, col, opts, debug_info) + local ok, err = pcall(vim.api.nvim_buf_set_extmark, buf, ns, row, col, opts) + if not ok and Config.debug then + M.debug("Failed to set extmark for preview", { info = debug_info, row = row, col = col, opts = opts, error = err }) + end +end + ---@param str string ---@param sep? string function M.camel(str, sep) diff --git a/lua/trouble/view/preview.lua b/lua/trouble/view/preview.lua index 99374b28..15449e63 100644 --- a/lua/trouble/view/preview.lua +++ b/lua/trouble/view/preview.lua @@ -1,3 +1,4 @@ +local Config = require("trouble.config") local Render = require("trouble.view.render") local Util = require("trouble.util") @@ -63,7 +64,7 @@ function M.create(item) end -- highlight the line - vim.api.nvim_buf_set_extmark(buf, Render.ns, item.pos[1] - 1, 0, { + Util.set_extmark(buf, Render.ns, item.pos[1] - 1, 0, { end_row = end_pos[1], hl_group = "CursorLine", hl_eol = true, @@ -71,7 +72,7 @@ function M.create(item) }) -- highlight the range - vim.api.nvim_buf_set_extmark(buf, Render.ns, item.pos[1] - 1, item.pos[2], { + Util.set_extmark(buf, Render.ns, item.pos[1] - 1, item.pos[2], { end_row = end_pos[1] - 1, end_col = end_pos[2], hl_group = "TroublePreview", diff --git a/lua/trouble/view/text.lua b/lua/trouble/view/text.lua index 314adff2..a506a071 100644 --- a/lua/trouble/view/text.lua +++ b/lua/trouble/view/text.lua @@ -166,7 +166,10 @@ function M:render(buf) col + width + 1, }) elseif segment.hl then - M.set_extmark(buf, row, col, { hl_group = segment.hl, end_col = col + width }) + Util.set_extmark(buf, M.ns, row, col, { + hl_group = segment.hl, + end_col = col + width, + }) end col = col + width end @@ -179,21 +182,6 @@ function M:render(buf) vim.bo[buf].modifiable = false end ----@param buf number ----@param row number ----@param col number ----@param opts vim.api.keyset.set_extmark ----@param debug_info? any -function M.set_extmark(buf, row, col, opts, debug_info) - local ok, err = pcall(vim.api.nvim_buf_set_extmark, buf, M.ns, row, col, opts) - if not ok then - Util.error( - "Failed to set extmark. Please report a bug with this info:\n" - .. vim.inspect({ info = debug_info, row = row, col = col, opts = opts, error = err }) - ) - end -end - function M:trim() while #self._lines > 0 and #self._lines[#self._lines] == 0 do table.remove(self._lines) diff --git a/lua/trouble/view/treesitter.lua b/lua/trouble/view/treesitter.lua index a53782aa..cbf35321 100644 --- a/lua/trouble/view/treesitter.lua +++ b/lua/trouble/view/treesitter.lua @@ -1,3 +1,5 @@ +local Util = require("trouble.util") + local M = {} ---@param buf number @@ -40,7 +42,7 @@ function M.highlight(buf, lang, regions) end if hl and name ~= "spell" then - pcall(vim.api.nvim_buf_set_extmark, buf, Render.ns, start_row, start_col, { + Util.set_extmark(buf, Render.ns, start_row, start_col, { end_line = end_row, end_col = end_col, hl_group = hl,