Skip to content

Commit

Permalink
feat(view): follow the current item in the list
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Mar 28, 2024
1 parent 2bc9975 commit 6c15a27
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 6 deletions.
4 changes: 3 additions & 1 deletion lua/trouble/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ local M = {}
local defaults = {
debug = false,
pinned = false, -- When pinned, the opened trouble window will be bound to the current buffer
focus = true, -- Focus the window when opened. Defaults to true.
focus = true, -- Focus the window when opened
follow = true, -- Follow the current item
results = {
---@type trouble.Window.opts
win = {}, -- window options for the results window. Can be a split or a floating window.
Expand All @@ -38,6 +39,7 @@ local defaults = {
refresh = 20, -- fetches new data when needed
update = 10, -- updates the window
render = 10, -- renders the window
follow = 10, -- follows the current item
preview = { ms = 100, debounce = true }, -- shows the preview for the current item
},
-- Key mappings can be set to the name of a builtin action,
Expand Down
7 changes: 6 additions & 1 deletion lua/trouble/filter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ M.filters = {
end,
range = function(item, buf, ctx)
local main = ctx.main
if not main then
return false
end
local range = item.range --[[@as trouble.Item]]
if range and main then
if range then
return main.cursor[1] >= range.pos[1] and main.cursor[1] <= range.end_pos[1]
else
return main.cursor[1] >= item.pos[1] and main.cursor[1] <= item.end_pos[1]
end
return false
end,
Expand Down
2 changes: 1 addition & 1 deletion lua/trouble/sources/diagnostics.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---@diagnostic disable: inject-field
local Item = require("trouble.item")

---@type trouble.Source
---@class trouble.Source.diagnostics: trouble.Source
local M = {}

M.highlights = {
Expand Down
2 changes: 1 addition & 1 deletion lua/trouble/sources/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ local Util = require("trouble.util")
local get_col = vim.lsp.util._get_line_byte_from_position
local Cache = require("trouble.cache")

---@type trouble.Source
---@class trouble.Source.lsp: trouble.Source
---@diagnostic disable-next-line: missing-fields
local M = {}

Expand Down
2 changes: 1 addition & 1 deletion lua/trouble/sources/qf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ local Item = require("trouble.item")
---@field user_data? any Any user data associated with the item.
---@field vcol? number Visual column number. Indicates if the column number is a visual column number (when set to 1) or a byte index (when set to 0).

---@type trouble.Source
---@class trouble.Source.qf: trouble.Source
local M = {}

M.config = {
Expand Down
2 changes: 1 addition & 1 deletion lua/trouble/sources/telescope.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ local Item = require("trouble.item")
---@field filename? string The filename of the item.
---@field cwd? string The current working directory of the item.

---@type trouble.Source
---@class trouble.Source.telescope: trouble.Source
local M = {}

---@type trouble.Item[]
Expand Down
43 changes: 43 additions & 0 deletions lua/trouble/view/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function M.new(opts)
})
self.update = Util.throttle(M.update, Util.throttle_opts(self.opts.throttle.update, { ms = 10 }))
self.render = Util.throttle(M.render, Util.throttle_opts(self.opts.throttle.render, { ms = 10 }))
self.follow = Util.throttle(M.follow, Util.throttle_opts(self.opts.throttle.follow, { ms = 10 }))

if self.opts.results.auto_open then
self:listen()
Expand Down Expand Up @@ -109,6 +110,19 @@ function M:on_mount()
end
end)

if self.opts.follow then
-- tracking of the current item
self.win:on("CursorMoved", function()
local this = _self()
if not this then
return true
end
if this.win:valid() then
this:follow()
end
end, { buffer = false })
end

self.win:on("OptionSet", function()
local this = _self()
if not this then
Expand Down Expand Up @@ -400,6 +414,10 @@ function M:render()
vim.fn.winrestview(view)
end)

if self.opts.follow and self:follow() then
return
end

-- when window is at top, dont move cursor
if view.topline == 1 then
return
Expand Down Expand Up @@ -430,4 +448,29 @@ function M:render()
end
end

-- When not in the trouble window, try to show the range
function M:follow()
local current_win = vim.api.nvim_get_current_win()
---@type number[]|nil
local cursor = nil
if current_win ~= self.win.win then
local Filter = require("trouble.filter")
local ctx = { opts = self.opts, main = self:main() }
for row, l in pairs(self.renderer._locations) do
local is_current = l.item and Filter.is(l.item, { range = true }, ctx)
if is_current then
cursor = { row, 1 }
-- return
end
end
end
if cursor then
-- make sure the cursorline is visible
vim.wo[self.win.win].cursorline = true
vim.api.nvim_win_set_cursor(self.win.win, cursor)
return true
end
return false
end

return M

0 comments on commit 6c15a27

Please sign in to comment.