From ec608a74e903c4992b968171c375a49bfde6cff4 Mon Sep 17 00:00:00 2001 From: Iordanis Petkakis <12776461+dpetka2001@users.noreply.github.com> Date: Mon, 25 May 2026 22:39:35 +0300 Subject: [PATCH] feat(view): allow `next_file`/`prev_file` navigation --- lua/trouble/config/actions.lua | 7 +++++++ lua/trouble/config/init.lua | 2 ++ lua/trouble/view/init.lua | 27 ++++++++++++++++++++++++--- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/lua/trouble/config/actions.lua b/lua/trouble/config/actions.lua index 6b0264b5..734f2862 100644 --- a/lua/trouble/config/actions.lua +++ b/lua/trouble/config/actions.lua @@ -87,6 +87,13 @@ local M = { last = function(self, ctx) self:move({ idx = -vim.v.count1, jump = ctx.opts.jump }) end, + -- next/prev file + next_file = function(self) + self:move({ next_file = true }) + end, + prev_file = function(self) + self:move({ prev_file = true }) + end, -- Jump to the item if on an item, otherwise do nothing jump_only = function(self, ctx) if ctx.item then diff --git a/lua/trouble/config/init.lua b/lua/trouble/config/init.lua index eb37a1a5..e6ab8bbb 100644 --- a/lua/trouble/config/init.lua +++ b/lua/trouble/config/init.lua @@ -70,6 +70,8 @@ local defaults = { -- k = "prev", ["{"] = "prev", ["[["] = "prev", + ["]f"] = "next_file", + ["[f"] = "prev_file", dd = "delete", d = { action = "delete", mode = "v" }, i = "inspect", diff --git a/lua/trouble/view/init.lua b/lua/trouble/view/init.lua index 34310092..5bed858d 100644 --- a/lua/trouble/view/init.lua +++ b/lua/trouble/view/init.lua @@ -367,7 +367,7 @@ function M:map(key, action) end, { desc = action.desc, mode = action.mode }) end ----@param opts? {idx?: number, up?:number, down?:number, jump?:boolean} +---@param opts? {idx?: number, up?:number, down?:number, next_file?:boolean, prev_file?:boolean, jump?:boolean} function M:move(opts) -- start the moving timer. Will stop any previous timers, -- so this acts as a debounce. @@ -383,13 +383,34 @@ function M:move(opts) if opts.idx and opts.idx < 0 then from, to = to, 1 todo = math.abs(todo) - elseif opts.down then + elseif opts.down or opts.next_file then from = cursor[1] + 1 - elseif opts.up then + elseif opts.up or opts.prev_file then from = cursor[1] - 1 to = 1 end + if opts.next_file or opts.prev_file then + local cur_loc = self:at() + local cur_filename = (cur_loc.item and cur_loc.item.filename) + or (cur_loc.node and cur_loc.node.item and cur_loc.node.item.filename) + + if not cur_filename then + return Util.warn("No file found for current item") + end + + for row = from, to, opts.next_file and 1 or -1 do + local info = self.renderer:at(row) + if info.node and info.node.group and info.node.item and info.node.item.filename ~= cur_filename then + vim.api.nvim_win_set_cursor(self.win.win, { row, cursor[2] }) + return + end + end + + local file = opts.next_file and "next" or "previous" + return Util.warn("No " .. file .. " file found") + end + for row = from, to, from > to and -1 or 1 do local info = self.renderer:at(row) if info.item and info.first_line then