Skip to content

Commit

Permalink
feat(preview): option to force loading real buffers in preview. Fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed May 30, 2024
1 parent 8a07fac commit ccacba2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
8 changes: 7 additions & 1 deletion lua/trouble/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ local defaults = {
-- Window options for the preview window. Can be a split, floating window,
-- or `main` to show the preview in the main editor window.
---@type trouble.Window.opts
preview = { type = "main" },
preview = {
type = "main",
-- when a buffer is not yet loaded, the preview window will be created
-- in a scratch buffer with only syntax highlighting enabled.
-- Set to false, if you want the preview to always be a real loaded buffer.
scratch = true,
},
-- Throttle/Debounce settings. Should usually not be changed.
---@type table<string, number|{ms:number, debounce?:boolean}>
throttle = {
Expand Down
35 changes: 21 additions & 14 deletions lua/trouble/view/preview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ end
--- If the item has a loaded buffer, use that,
--- otherwise create a new buffer.
---@param item trouble.Item
function M.create(item)
---@param opts? {scratch?:boolean}
function M.create(item, opts)
opts = opts or {}

local buf = item.buf or vim.fn.bufnr(item.filename)

if item.filename and vim.fn.isdirectory(item.filename) == 1 then
Expand All @@ -40,20 +43,24 @@ function M.create(item)

-- create a scratch preview buffer when needed
if not (buf and vim.api.nvim_buf_is_loaded(buf)) then
buf = vim.api.nvim_create_buf(false, true)
vim.bo[buf].bufhidden = "wipe"
vim.bo[buf].buftype = "nofile"
local lines = Util.get_lines({ path = item.filename, buf = item.buf })
if not lines then
return
end
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
local ft = item:get_ft(buf)
if ft then
local lang = vim.treesitter.language.get_lang(ft)
if not pcall(vim.treesitter.start, buf, lang) then
vim.bo[buf].syntax = ft
if opts.scratch then
buf = vim.api.nvim_create_buf(false, true)
vim.bo[buf].bufhidden = "wipe"
vim.bo[buf].buftype = "nofile"
local lines = Util.get_lines({ path = item.filename, buf = item.buf })
if not lines then
return
end
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
local ft = item:get_ft(buf)
if ft then
local lang = vim.treesitter.language.get_lang(ft)
if not pcall(vim.treesitter.start, buf, lang) then
vim.bo[buf].syntax = ft
end
end
else
vim.fn.bufload(buf)
end
end

Expand Down

0 comments on commit ccacba2

Please sign in to comment.