Skip to content

Commit

Permalink
fix(follow): improve the way follow works
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed May 30, 2024
1 parent 51bf510 commit cf81aac
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lua/trouble/filter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ local function overlaps(pos, item)
and (pos[1] < item.end_pos[1] or (pos[1] == item.end_pos[1] and pos[2] <= item.end_pos[2]))
end

local function overlaps_lines(pos, item)
return pos[1] >= item.pos[1] and pos[1] <= item.end_pos[1]
end

---@alias trouble.Filter.ctx {opts:trouble.Config, main?:trouble.Main}
---@alias trouble.FilterFn fun(item:trouble.Item, value: any, ctx:trouble.Filter.ctx): boolean
---@class trouble.Filters: {[string]: trouble.FilterFn}
Expand All @@ -28,9 +32,9 @@ M.filters = {
end
local range = item.range --[[@as trouble.Item]]
if range then
return overlaps(main.cursor, range)
return overlaps_lines(main.cursor, range)
else
return overlaps(main.cursor, item)
return overlaps_lines(main.cursor, item)
end
end,
["not"] = function(item, filter, ctx)
Expand Down
10 changes: 9 additions & 1 deletion lua/trouble/view/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -551,16 +551,24 @@ function M:follow()
local Filter = require("trouble.filter")
local ctx = { opts = self.opts, main = self:main() }
local fname = vim.api.nvim_buf_get_name(ctx.main.buf or 0)
local loc = self:at()

-- check if we're already in the file group
local in_group = loc.node and loc.node.item and loc.node.item.filename == fname

---@type number[]|nil
local cursor_item = nil
local cursor_group = cursor_item

for row, l in pairs(self.renderer._locations) do
local is_group = l.node and l.node.group and l.node.item and l.node.item.filename == fname
-- only return the group if we're not yet in the group
-- and the group's filename matches the current file
local is_group = not in_group and l.node and l.node.group and l.node.item and l.node.item.filename == fname
if is_group then
cursor_group = { row, 1 }
end

-- prefer a full match
local is_current = l.item and Filter.is(l.item, { range = true }, ctx)
if is_current then
cursor_item = { row, 1 }
Expand Down

0 comments on commit cf81aac

Please sign in to comment.