Skip to content

Commit

Permalink
feat(view): when toggling a trouble list, restore to the last location.
Browse files Browse the repository at this point in the history
Fixes #367
  • Loading branch information
folke committed Mar 29, 2024
1 parent 853f8a8 commit f3b3a82
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions lua/trouble/config/init.lua
Expand Up @@ -18,6 +18,7 @@ local defaults = {
auto_preview = true, -- automatically open preview when on an item
auto_refresh = true, -- auto refresh when open
focus = false, -- Focus the window when opened
restore = true, -- restores the last location in the list when opening
follow = true, -- Follow the current item
indent_guides = true, -- show indent guides
max_items = 200, -- limit number of items that can be displayed per section
Expand Down
14 changes: 13 additions & 1 deletion lua/trouble/view/init.lua
Expand Up @@ -20,6 +20,9 @@ local _idx = 0
---@type table<trouble.View, number>
M._views = setmetatable({}, { __mode = "k" })

---@type table<string, trouble.Render.Location>
M._last = {}

---@param opts trouble.Mode
function M.new(opts)
local self = setmetatable({}, M)
Expand All @@ -30,6 +33,9 @@ function M.new(opts)
self.opts.win.on_mount = function()
self:on_mount()
end
self.opts.win.on_close = function()
M._last[self.opts.mode or ""] = self:at()
end

self.sections = {}
for _, s in ipairs(Spec.sections(self.opts)) do
Expand Down Expand Up @@ -408,7 +414,13 @@ function M:render()
if not self.win:valid() then
return
end

local loc = self:at()
local restore_loc = self.opts.restore and M._last[self.opts.mode or ""]
if restore_loc then
loc = restore_loc
M._last[self.opts.mode or ""] = nil
end

-- render sections
self.renderer:clear()
Expand Down Expand Up @@ -441,7 +453,7 @@ function M:render()
end

-- when window is at top, dont move cursor
if view.topline == 1 then
if not restore_loc and view.topline == 1 then
return
end

Expand Down
3 changes: 2 additions & 1 deletion lua/trouble/view/render.lua
Expand Up @@ -4,8 +4,9 @@ local Indent = require("trouble.view.indent")
local Text = require("trouble.view.text")
local Util = require("trouble.util")

---@alias trouble.Render.Location {item?: trouble.Item, node?: trouble.Node, first_line?:boolean}
---@class trouble.Render: trouble.Text
---@field _locations {item?: trouble.Item, node?: trouble.Node, first_line?:boolean}[] Maps line numbers to items.
---@field _locations trouble.Render.Location[] Maps line numbers to items.
---@field _folded table<string, true>
---@field root_nodes trouble.Node[]
---@field foldlevel? number
Expand Down
4 changes: 4 additions & 0 deletions lua/trouble/view/window.lua
Expand Up @@ -32,6 +32,7 @@ local Util = require("trouble.util")
---@field minimal? boolean (defaults to true)
---@field win? number
---@field on_mount? fun(self: trouble.Window)
---@field on_close? fun(self: trouble.Window)

---@alias trouble.Window.opts trouble.Window.base|trouble.Window.split|trouble.Window.float|trouble.Window.main

Expand Down Expand Up @@ -205,6 +206,9 @@ function M:mount()
self:on({ "BufWinLeave" }, vim.schedule_wrap(self.check_alien))

self:on("WinClosed", function()
if self.opts.on_close then
self.opts.on_close(self)
end
self:augroup(true)
end, { win = true })

Expand Down

0 comments on commit f3b3a82

Please sign in to comment.