Skip to content

Commit

Permalink
feat: option to automatically jump when there is only one result (fixes
Browse files Browse the repository at this point in the history
#57) (#79)

* Add auto_jump option

* Allow for configuration of auto_jump on a per-provider basis
  • Loading branch information
elkowar committed Oct 22, 2021
1 parent a736b8d commit 09fafb2
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ Trouble comes with the following defaults:
auto_close = false, -- automatically close the list when you have no diagnostics
auto_preview = true, -- automatically preview the location of the diagnostic. <esc> to close preview and go back to last window
auto_fold = false, -- automatically fold a file trouble list at creation
auto_jump = {"lsp_definitions"}, -- for the given modes, automatically jump if there is only a single result
signs = {
-- icons / text used for a diagnostic
error = "",
Expand Down
1 change: 1 addition & 0 deletions lua/trouble/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ local defaults = {
auto_close = false, -- automatically close the list when you have no diagnostics
auto_preview = true, -- automatically preview the location of the diagnostic. <esc> to close preview and go back to last window
auto_fold = false, -- automatically fold a file trouble list at creation
auto_jump = {"lsp_definitions"}, -- for the given modes, automatically jump if there is only a single result
signs = {
-- icons / text used for a diagnostic
error = "",
Expand Down
8 changes: 8 additions & 0 deletions lua/trouble/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ function Trouble.open(...)

if 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)
if #results == 1 then
util.jump_to_item(opts.win, opts.precmd, results[1])
elseif #results > 0 then
view = View.create(opts)
end
end, config.options)
else
view = View.create(opts)
end
Expand Down
16 changes: 16 additions & 0 deletions lua/trouble/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@ local config = require("trouble.config")

local M = {}

function M.jump_to_item(win, precmd, item)
-- requiring here, as otherwise we run into a circular dependency
local View = require("trouble.view")

View.switch_to(win)
if precmd then
vim.cmd(precmd)
end
if vim.api.nvim_buf_get_option(item.bufnr, "buflisted") == false then
vim.cmd("edit #" .. item.bufnr)
else
vim.cmd("buffer " .. item.bufnr)
end
vim.api.nvim_win_set_cursor(win, { item.start.line + 1, item.start.character })
end

function M.count(tab)
local count = 0
for _ in pairs(tab) do
Expand Down
11 changes: 1 addition & 10 deletions lua/trouble/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -427,16 +427,7 @@ function View:jump(opts)
folds.toggle(item.filename)
self:update()
else
View.switch_to(opts.win or self.parent)
if opts.precmd then
vim.cmd(opts.precmd)
end
if vim.api.nvim_buf_get_option(item.bufnr, "buflisted") == false then
vim.cmd("edit #" .. item.bufnr)
else
vim.cmd("buffer " .. item.bufnr)
end
vim.api.nvim_win_set_cursor(self.parent, { item.start.line + 1, item.start.character })
util.jump_to_item(opts.win or self.parent, opts.precmd, item)
end
end

Expand Down

0 comments on commit 09fafb2

Please sign in to comment.