Skip to content

Commit

Permalink
feat: enable looping during next/prev (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrnxf committed Feb 20, 2023
1 parent c825300 commit fc4c0f8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
18 changes: 8 additions & 10 deletions lua/trouble/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ local Trouble = {}

local view

local function is_open()
return view and view:is_valid()
function Trouble.is_open()
return view and view:is_valid() or false
end

function Trouble.setup(options)
Expand All @@ -24,7 +24,7 @@ function Trouble.setup(options)
end

function Trouble.close()
if is_open() then
if Trouble.is_open() then
view:close()
end
end
Expand Down Expand Up @@ -62,7 +62,7 @@ function Trouble.open(...)
end
opts.focus = true

if is_open() then
if Trouble.is_open() then
Trouble.refresh(opts)
elseif not opts.auto and vim.tbl_contains(config.options.auto_jump, opts.mode) then
require("trouble.providers").get(vim.api.nvim_get_current_win(), vim.api.nvim_get_current_buf(), function(results)
Expand All @@ -86,7 +86,7 @@ function Trouble.toggle(...)
return
end

if is_open() then
if Trouble.is_open() then
Trouble.close()
else
Trouble.open(...)
Expand Down Expand Up @@ -114,7 +114,7 @@ end

local updater = util.debounce(100, function()
-- buff might have been closed during the debounce
if not is_open() then
if not Trouble.is_open() then
util.debug("refresh: not open anymore")
return
end
Expand Down Expand Up @@ -142,7 +142,7 @@ function Trouble.refresh(opts)
end
end

if is_open() then
if Trouble.is_open() then
if opts.auto then
updater()
else
Expand Down Expand Up @@ -171,7 +171,7 @@ function Trouble.action(action)
if view and action == "on_win_enter" then
view:on_win_enter()
end
if not is_open() then
if not Trouble.is_open() then
return Trouble
end
if action == "hover" then
Expand Down Expand Up @@ -282,6 +282,4 @@ function Trouble.get_items()
end
end

Trouble.is_open = is_open

return Trouble
28 changes: 22 additions & 6 deletions lua/trouble/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -381,20 +381,36 @@ end
function View:next_item(opts)
opts = opts or { skip_groups = false }
local line = opts.first and 0 or self:get_line() + 1
for i = line, vim.api.nvim_buf_line_count(self.buf), 1 do
if self.items[i] and not (opts.skip_groups and self.items[i].is_file) then
vim.api.nvim_win_set_cursor(self.win, { i, self:get_col() })
if opts.jump then
self:jump()

if line > #self.items then
self:first_item(opts)
else
for i = line, vim.api.nvim_buf_line_count(self.buf), 1 do
if self.items[i] and not (opts.skip_groups and self.items[i].is_file) then
vim.api.nvim_win_set_cursor(self.win, { i, self:get_col() })
if opts.jump then
self:jump()
end
return
end
return
end
end
end

function View:previous_item(opts)
opts = opts or { skip_groups = false }
local line = opts.last and vim.api.nvim_buf_line_count(self.buf) or self:get_line() - 1

for i = 0, vim.api.nvim_buf_line_count(self.buf), 1 do
if self.items[i] then
if line < i + (opts.skip_groups and 1 or 0) then
self:last_item(opts)
return
end
break
end
end

for i = line, 0, -1 do
if self.items[i] and not (opts.skip_groups and self.items[i].is_file) then
vim.api.nvim_win_set_cursor(self.win, { i, self:get_col() })
Expand Down

0 comments on commit fc4c0f8

Please sign in to comment.