Skip to content

Commit

Permalink
feat: Api to go to first and last items (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
bellini666 committed Oct 14, 2022
1 parent 929315e commit 0649811
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ require("trouble").next({skip_groups = true, jump = true});

-- jump to the previous item, skipping the groups
require("trouble").previous({skip_groups = true, jump = true});

-- jump to the first item, skipping the groups
require("trouble").first({skip_groups = true, jump = true});

-- jump to the last item, skipping the groups
require("trouble").last({skip_groups = true, jump = true});
```

### Telescope
Expand Down
22 changes: 22 additions & 0 deletions lua/trouble/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ function Trouble.action(action)
view:previous_item()
return Trouble
end
if action == "first" then
view:first_item()
return Trouble
end
if action == "last" then
view:last_item()
return Trouble
end

if action == "toggle_preview" then
config.options.auto_preview = not config.options.auto_preview
Expand Down Expand Up @@ -242,6 +250,20 @@ function Trouble.previous(opts)
end
end

function Trouble.first(opts)
util.fix_mode(opts)
if view then
view:first_item(opts)
end
end

function Trouble.last(opts)
util.fix_mode(opts)
if view then
view:last_item(opts)
end
end

function Trouble.get_items()
if view ~= nil then
return view.items
Expand Down
20 changes: 16 additions & 4 deletions lua/trouble/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,8 @@ end

function View:next_item(opts)
opts = opts or { skip_groups = false }
local line = self:get_line()
for i = line + 1, vim.api.nvim_buf_line_count(self.buf), 1 do
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
Expand All @@ -390,8 +390,8 @@ end

function View:previous_item(opts)
opts = opts or { skip_groups = false }
local line = self:get_line()
for i = line - 1, 0, -1 do
local line = opts.last and vim.api.nvim_buf_line_count(self.buf) or self:get_line() - 1
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() })
if opts.jump then
Expand All @@ -402,6 +402,18 @@ function View:previous_item(opts)
end
end

function View:first_item(opts)
opts = opts or {}
opts.first = true
return self:next_item(opts)
end

function View:last_item(opts)
opts = opts or {}
opts.last = true
return self:previous_item(opts)
end

function View:hover(opts)
opts = opts or {}
local item = opts.item or self:current_item()
Expand Down

0 comments on commit 0649811

Please sign in to comment.