Skip to content

Commit

Permalink
fix(preview): better preview for multiline items
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed May 30, 2024
1 parent b25ef53 commit 60c9fdc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lua/trouble/config/highlights.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ local defaults = {
-- General
Normal = "NormalFloat",
Text = "Normal",
Preview = "CurSearch",
Preview = "Visual",
-- Item
FileName = "Directory",
Source = "Comment",
Expand Down
20 changes: 13 additions & 7 deletions lua/trouble/view/preview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local Render = require("trouble.view.render")
local Util = require("trouble.util")

local M = {}
M.preview = nil ---@type {win:number,buf:number,view:table}?
M.preview = nil ---@type {win:number,buf:number,view:table,cursor:number[]}?

function M.close()
local preview = M.preview
Expand Down Expand Up @@ -44,13 +44,17 @@ function M.open(view, item)
win = main.win,
buf = main.buf,
view = vim.api.nvim_win_call(main.win, vim.fn.winsaveview),
cursor = vim.api.nvim_win_get_cursor(main.win),
}

-- prevent the buffer from being loaded with eventignore
-- highlight with treesitter if possible, otherwise use syntax
-- no autocmds should be triggered. So LSP's etc won't attach in the preview
Util.noautocmd(function()
if not vim.api.nvim_buf_is_loaded(item.buf) then
vim.bo[item.buf].swapfile = false
pcall(vim.fn.bufload, item.buf)
-- vim.api.nvim_del_autocmd(autocmd)
vim.b[item.buf].trouble_preview = true
local ft = vim.filetype.match({ buf = item.buf })
if ft then
Expand All @@ -70,19 +74,21 @@ function M.open(view, item)
if end_pos[1] == item.pos[1] and end_pos[2] == item.pos[2] then
end_pos[2] = end_pos[2] + 1
end
vim.api.nvim_buf_set_extmark(item.buf, Render.ns, item.pos[1] - 1, item.pos[2], {
end_row = end_pos[1] - 1,
end_col = end_pos[2],
hl_group = "TroublePreview",
strict = false,
})

vim.api.nvim_buf_set_extmark(item.buf, Render.ns, item.pos[1] - 1, 0, {
end_row = end_pos[1],
-- end_col = end_pos[2],
hl_group = "CursorLine",
hl_eol = true,
strict = false,
})
-- only highlight the range if it's on the same line
vim.api.nvim_buf_set_extmark(item.buf, Render.ns, item.pos[1] - 1, item.pos[2], {
end_row = end_pos[1] - 1,
end_col = end_pos[2],
hl_group = "TroublePreview",
strict = false,
})
return item
end

Expand Down

0 comments on commit 60c9fdc

Please sign in to comment.