Skip to content

Commit

Permalink
feat: config for auto_preview
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Apr 22, 2021
1 parent f7db1c2 commit 0ad97fb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ Trouble comes with the following defaults:
refresh = "r", -- manually refresh
jump = "<cr>", -- jump to the diagnostic or open / close folds
toggle_mode = "m", -- toggle between "workspace" and "document" mode
toggle_preview = "P", -- toggle auto_preview
preview = "p", -- preview the diagnostic location
close_folds = "zM", -- close all folds
cancel = "<esc>", -- cancel the preview and get back to your last window / buffer / cursor
open_folds = "zR", -- open all folds
Expand All @@ -86,6 +88,7 @@ Trouble comes with the following defaults:
indent_lines = true, -- add an indent guide below the fold icons
auto_open = false, -- automatically open the list when you have diagnostics
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
signs = {
-- icons / text used for a diagnostic
error = "",
Expand Down
3 changes: 3 additions & 0 deletions lua/trouble/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ local defaults = {
refresh = "r", -- manually refresh
jump = "<cr>", -- jump to the diagnostic or open / close folds
toggle_mode = "m", -- toggle between "workspace" and "document" mode
toggle_preview = "P", -- toggle auto_preview
preview = "p", -- preview the diagnostic location
close_folds = "zM", -- close all folds
cancel = "<esc>", -- cancel the preview and get back to your last window / buffer / cursor
open_folds = "zR", -- open all folds
Expand All @@ -23,6 +25,7 @@ local defaults = {
indent_lines = true, -- add an indent guide below the fold icons
auto_open = false, -- automatically open the list when you have diagnostics
auto_close = false, -- automatically close the list when you have no diagnostics
auto_preview = true, -- automatyically preview the location of the diagnostic. <esc> to close preview and go back to last window
signs = {
-- icons / text used for a diagnostic
error = "",
Expand Down
12 changes: 12 additions & 0 deletions lua/trouble/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,19 @@ function Trouble.action(action)
if action == "next" then view:next_item() end
if action == "previous" then view:previous_item() end

if action == "toggle_preview" then
config.options.auto_preview = not config.options.auto_preview
if not config.options.auto_preview then
view:close_preview()
else
action = "preview"
end
end
if action == "auto_preview" and config.options.auto_preview then
action = "preview"
end
if action == "preview" then view:preview() end

if Trouble[action] then Trouble[action]() end
end

Expand Down
8 changes: 5 additions & 3 deletions lua/trouble/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ function View:setup(opts)
vim.api.nvim_exec([[
augroup LspTroubleHighlights
autocmd! * <buffer>
autocmd CursorMoved <buffer> nested lua require("trouble").action("preview")
autocmd CursorMoved <buffer> nested lua require("trouble").action("auto_preview")
autocmd BufEnter <buffer> lua require("trouble").action("on_enter")
autocmd BufLeave <buffer> lua require("trouble").action("on_leave")
augroup END
Expand All @@ -164,12 +164,14 @@ function View:on_enter()
}
end

function View:on_leave()
function View:on_leave() self:close_preview() end

function View:close_preview()
-- Clear preview highlights
for buf, _ in pairs(buffersHl) do clear_hl(buf) end
buffersHl = {}

-- Reset parent stats
-- Reset parent state
if self.parent_state then
vim.api.nvim_win_set_buf(self.parent, self.parent_state.buf)
vim.api.nvim_win_set_cursor(self.parent, self.parent_state.cursor)
Expand Down

0 comments on commit 0ad97fb

Please sign in to comment.