Skip to content

Commit

Permalink
fix(ui): better deal with invalid items positions and extmarks. Fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed May 12, 2024
1 parent aadce80 commit 24d05bd
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
13 changes: 13 additions & 0 deletions lua/trouble/util.lua
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions 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")

Expand Down Expand Up @@ -63,15 +64,15 @@ 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,
strict = false,
})

-- 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",
Expand Down
20 changes: 4 additions & 16 deletions lua/trouble/view/text.lua
Expand Up @@ -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
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion lua/trouble/view/treesitter.lua
@@ -1,3 +1,5 @@
local Util = require("trouble.util")

local M = {}

---@param buf number
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 24d05bd

Please sign in to comment.