Skip to content

Commit

Permalink
feat: next/previous API. Implements #44
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Jun 17, 2021
1 parent 639e31d commit a2a7dbf
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,18 @@ vim.api.nvim_set_keymap("n", "gR", "<cmd>Trouble lsp_references<cr>",
)
```

### API

You can use the following functions in your keybindings:

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

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

### Telescope

You can easily open any search results in **Trouble**, by defining a custom action:
Expand Down
12 changes: 12 additions & 0 deletions lua/trouble/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,18 @@ function Trouble.action(action)
return Trouble
end

function Trouble.next(opts)
if view then
view:next_item(opts)
end
end

function Trouble.previous(opts)
if view then
view:previous_item(opts)
end
end

function Trouble.get_items()
if view ~= nil then
return view.items
Expand Down
18 changes: 13 additions & 5 deletions lua/trouble/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ local util = require("trouble.util")

local highlight = vim.api.nvim_buf_add_highlight

---@class View
---@class TroubleView
---@field buf number
---@field win number
---@field items Item[]
Expand Down Expand Up @@ -362,21 +362,29 @@ function View:current_item()
return item
end

function View:next_item()
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
if self.items[i] then
if self.items[i] and (not opts.skip_groups or not 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
end
end

function View:previous_item()
function View:previous_item(opts)
opts = opts or { skip_groups = false }
local line = self:get_line()
for i = line - 1, 0, -1 do
if self.items[i] then
if self.items[i] and (not opts.skip_groups or not 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
end
Expand Down

0 comments on commit a2a7dbf

Please sign in to comment.