Skip to content

Commit

Permalink
feat(preview): use a float to show preview in the main window instead…
Browse files Browse the repository at this point in the history
… of messing with the main window itself
  • Loading branch information
folke committed May 30, 2024
1 parent 3525169 commit 9e0311d
Showing 1 changed file with 12 additions and 36 deletions.
48 changes: 12 additions & 36 deletions lua/trouble/view/preview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,7 @@ function M.open(view, item)

local buf = M.create(item)

if view.opts.preview.win == "main" then
M.preview = M.preview_main(buf, view)
else
assert(type(view.opts.preview.win) == "table", "Invalid preview window")
M.preview = M.preview_win(buf, view)
end
M.preview = M.preview_win(buf, view)

M.preview.buf = buf
M.preview.item = item
Expand All @@ -107,7 +102,17 @@ end
---@param buf number
---@param view trouble.View
function M.preview_win(buf, view)
view.preview_win.opts.win = view.win.win
if view.opts.preview.win.type == "main" then
local main = view:main()
if not main then
Util.debug("No main window")
return
end
view.preview_win.opts.win = main.win
else
view.preview_win.opts.win = view.win.win
end

view.preview_win:open()
Util.noautocmd(function()
view.preview_win:set_buf(buf)
Expand All @@ -122,33 +127,4 @@ function M.preview_win(buf, view)
}
end

---@param buf number
---@param view trouble.View
function M.preview_main(buf, view)
local main = view:main()
if not main then
Util.debug("No main window")
return
end

local main_view = vim.api.nvim_win_call(main.win, vim.fn.winsaveview)
Util.noautocmd(function()
vim.api.nvim_win_set_buf(main.win, buf)
end)

return {
win = main.win,
close = function()
if vim.api.nvim_win_is_valid(main.win) then
Util.noautocmd(function()
vim.api.nvim_win_set_buf(main.win, main.buf)
vim.api.nvim_win_call(main.win, function()
vim.fn.winrestview(main_view)
end)
end)
end
end,
}
end

return M

0 comments on commit 9e0311d

Please sign in to comment.