Skip to content

Commit

Permalink
feat(config): added auto_jump to jump to the item when there's only…
Browse files Browse the repository at this point in the history
… one. Fixes #409
  • Loading branch information
folke committed May 30, 2024
1 parent 35c0236 commit 94a84ab
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions lua/trouble/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ local defaults = {
auto_open = false, -- auto open when there are items
auto_preview = true, -- automatically open preview when on an item
auto_refresh = true, -- auto refresh when open
auto_jump = false, -- auto jump to the item when there's only one
focus = false, -- Focus the window when opened
restore = true, -- restores the last location in the list when opening
follow = true, -- Follow the current item
Expand Down
1 change: 1 addition & 0 deletions lua/trouble/sources/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ M.config = {

for _, mode in ipairs({ "definitions", "references", "implementations", "type_definitions", "declarations" }) do
M.config.modes["lsp_" .. mode] = {
auto_jump = true,
mode = "lsp_base",
title = "{hl:Title}" .. Util.camel(mode, " ") .. "{hl} {count}",
source = "lsp." .. mode,
Expand Down
9 changes: 9 additions & 0 deletions lua/trouble/view/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ local Window = require("trouble.view.window")
---@field opts trouble.Mode
---@field sections trouble.Section[]
---@field renderer trouble.Render
---@field first_update? boolean
---@field private _main? trouble.Main
local M = {}
M.__index = M
Expand All @@ -29,6 +30,7 @@ function M.new(opts)
_idx = _idx + 1
M._views[self] = _idx
self.opts = opts or {}
self.first_update = true
self.opts.win = self.opts.win or {}
self.opts.win.on_mount = function()
self:on_mount()
Expand Down Expand Up @@ -424,6 +426,13 @@ function M:update()
end
self:open()
end
if self.win:valid() and self.first_update then
self.first_update = false
if self.opts.auto_jump and self:count() == 1 then
self:jump(self:flatten()[1])
return self:close()
end
end
self:render()
end

Expand Down

0 comments on commit 94a84ab

Please sign in to comment.