Skip to content

Commit

Permalink
feat: only open trouble when results (optionally). Fixes #450
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed May 30, 2024
1 parent 8c03e13 commit 8fbd2ab
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 24 deletions.
4 changes: 3 additions & 1 deletion lua/trouble/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ function M.open(opts)
if view then
view:open()
if _opts.focus ~= false then
view.win:focus()
view:wait(function()
view.win:focus()
end)
end
end
return view
Expand Down
2 changes: 2 additions & 0 deletions lua/trouble/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ local defaults = {
max_items = 200, -- limit number of items that can be displayed per section
multiline = true, -- render multi-line messages
pinned = false, -- When pinned, the opened trouble window will be bound to the current buffer
warn_no_results = true, -- show a warning when there are no results
open_no_results = false, -- open the trouble window when there are no results
---@type trouble.Window.opts
win = {}, -- window options for the results window. Can be a split or a floating window.
-- Window options for the preview window. Can be a split, floating window,
Expand Down
94 changes: 73 additions & 21 deletions lua/trouble/view/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ local Window = require("trouble.view.window")
---@field opts trouble.Mode
---@field sections trouble.Section[]
---@field renderer trouble.Render
---@field first_update? boolean
---@field first_render? boolean
---@field moving uv_timer_t
---@field opening? boolean
---@field state table<string,any>
---@field _waiting (fun())[]
---@field private _main? trouble.Main
local M = {}
M.__index = M
Expand All @@ -36,7 +37,7 @@ function M.new(opts)
M._views[self] = _idx
self.state = {}
self.opts = opts or {}
self.first_update = true
self._waiting = {}
self.first_render = true
self.opts.win = self.opts.win or {}
self.opts.win.on_mount = function()
Expand Down Expand Up @@ -85,7 +86,7 @@ function M.get(filter)
local ret = {}
for view, idx in pairs(M._views) do
local is_open = view.win:valid()
local ok = is_open or view.opts.auto_open
local ok = is_open or view.opts.auto_open or view.opening
ok = ok and (not filter.mode or filter.mode == view.opts.mode)
ok = ok and (not filter.open or is_open)
if ok then
Expand Down Expand Up @@ -177,6 +178,8 @@ function M:on_mount()
for k, v in pairs(self.opts.keys) do
self:map(k, v)
end

self.opening = false
end

---@param node? trouble.Node
Expand Down Expand Up @@ -248,6 +251,14 @@ function M:jump(item, opts)
return item
end

function M:wait(fn)
if self.opening then
table.insert(self._waiting, fn)
else
fn()
end
end

---@param item? trouble.Item
function M:preview(item)
item = item or self:at().item
Expand Down Expand Up @@ -354,17 +365,18 @@ end
---@param action trouble.Action
---@param opts? table
function M:action(action, opts)
local at = self:at() or {}
action(self, {
item = at.item,
node = at.node,
opts = type(opts) == "table" and opts or {},
})
self:wait(function()
local at = self:at() or {}
action(self, {
item = at.item,
node = at.node,
opts = type(opts) == "table" and opts or {},
})
end)
end

function M:refresh()
local is_open = self.win:valid()
if not is_open and not self.opts.auto_open then
if not (self.opening or self.win:valid() or self.opts.auto_open) then
return
end
for _, section in ipairs(self.sections) do
Expand Down Expand Up @@ -412,14 +424,16 @@ end

function M:open()
if self.win:valid() then
return
return self
end
self.win:open()
self.opening = true
-- self.win:open()
self:refresh()
return self
end

function M:close()
self.opening = false
self:goto_main()
Preview.close()
self.win:close()
Expand All @@ -444,26 +458,64 @@ function M:flatten()
return ret
end

-- called when results are updated
function M:update()
if self.opts.auto_close and self:count() == 0 then
return self:close()
local is_open = self.win:valid()
self.opening = self.opening and not is_open
local count = self:count()

local did_first_update = true
for _, section in ipairs(self.sections) do
if not section.first_update then
did_first_update = false
break
end
end
if self.opts.auto_open and not self.win:valid() then
if self:count() == 0 then

if count == 0 then
if self.opening and not self.opts.open_no_results then
if did_first_update and self.opts.warn_no_results then
Util.warn("No results for **" .. self.opts.mode .. "**")
end
self.opening = not did_first_update
return
end
self:open()

if is_open and self.opts.auto_close then
return self:close()
end
end
if self.win:valid() and self.first_update then
self.first_update = false
if self.opts.auto_jump and self:count() == 1 then

if self.opening and did_first_update then
if self.opts.auto_jump and count == 1 then
self.opening = false
self:jump(self:flatten()[1])
return self:close()
end
end

if self.opts.auto_open and not is_open and count > 0 then
self.win:open()
is_open = true
end

if self.opening then
self.win:open()
is_open = true
end

if not (self.opening or is_open) then
return
end

self:render()

while #self._waiting > 0 do
Util.try(table.remove(self._waiting, 1))
end
end

-- render the results
function M:render()
if not self.win:valid() then
return
Expand Down
1 change: 0 additions & 1 deletion lua/trouble/view/preview.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local Config = require("trouble.config")
local Render = require("trouble.view.render")
local Util = require("trouble.util")

Expand Down
4 changes: 3 additions & 1 deletion lua/trouble/view/section.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ local Util = require("trouble.util")
---@field node? trouble.Node
---@field fetching boolean
---@field filter? trouble.Filter
---@field first_update boolean
---@field on_refresh? fun(self: trouble.Section)
---@field on_update? fun(self: trouble.Section)
local M = {}
Expand Down Expand Up @@ -60,7 +61,7 @@ function M:refresh()
end
-- mark as completed after 2 seconds to avoid
-- errors staling the fetching count
vim.defer_fn(complete, 1000)
vim.defer_fn(complete, 2000)

self:main_call(function(main)
local ctx = { opts = self.opts, main = main }
Expand Down Expand Up @@ -108,6 +109,7 @@ function M:main_call(fn)
end

function M:update()
self.first_update = true
if self.on_update then
self:on_update()
end
Expand Down

0 comments on commit 8fbd2ab

Please sign in to comment.