Skip to content

Commit

Permalink
feat: better preview and mode
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Apr 22, 2021
1 parent f9e6930 commit 160fa6c
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 53 deletions.
9 changes: 6 additions & 3 deletions lua/trouble/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@ M.namespace = vim.api.nvim_create_namespace('LspTrouble')
local defaults = {
height = 10,
icons = true,
mode = "document", -- "workspace" or "document"
fold_open = "",
fold_closed = "",
actions = {
["<cr>"] = "jump",
["<tab>"] = "jump",
["<esc>"] = "cancel",
q = "close",
r = "refresh",
zR = "open_folds",
zM = "close_folds",
p = "preview",
P = "toggle_preview"
j = "next",
k = "previous",
m = "toggle_mode"
},
indent_lines = false,
auto_open = false,
auto_close = true,
auto_preview = false,
signs = {error = "", warning = "", hint = "", information = ""},
use_lsp_diagnostic_signs = false
}
Expand Down
25 changes: 16 additions & 9 deletions lua/trouble/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,27 @@ function Trouble.refresh(opts)
end

function Trouble.action(action)
if action == "toggle_mode" then
if config.options.mode == "document" then
config.options.mode = "workspace"
else
config.options.mode = "document"
end
action = "refresh"
end

if view and action == "on_win_enter" then view:on_win_enter() end
if not is_open() then return end
if action == "jump" then view:jump() end
if action == "open_folds" then Trouble.refresh({open_folds = true}) end
if action == "close_folds" then Trouble.refresh({close_folds = true}) end
if action == "on_enter" then view:on_enter() end
if action == "on_leave" then view:on_leave() end
if action == "cancel" then view:switch_to_parent() end
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 config.options.auto_preview then action = "auto_preview" end
end

if action == "preview" or
(action == "auto_preview" and config.options.auto_preview) then
view:preview()
end
if action == "preview" then view:preview() end
if Trouble[action] then Trouble[action]() end
end

Expand Down
14 changes: 10 additions & 4 deletions lua/trouble/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@ end
---@param view View
function renderer.render(view, opts)
opts = opts or {}
local diagnostics = lsp.diagnostics(config.options)
local lsp_opts = {}
if config.options.mode == "document" then
lsp_opts.bufnr = vim.api.nvim_win_get_buf(view.parent)
end
local diagnostics = lsp.diagnostics(lsp_opts)
local grouped = lsp.group(diagnostics)
local count = util.count(grouped)

-- check for auto close
if opts.auto and config.options.auto_close then
local count = util.count(grouped)
if count == 0 then
view:close()
return
Expand All @@ -45,6 +49,8 @@ function renderer.render(view, opts)
local text = Text:new()
view.items = {}

text:nl()

-- render file groups
for filename, items in pairs(grouped) do
if opts.open_folds then folds.open(filename) end
Expand All @@ -60,7 +66,7 @@ end
---@param items Diagnostics[]
---@param filename string
function renderer.render_file(view, text, filename, items)
view.items[text.lineNr] = {filename = filename, is_file = true}
view.items[text.lineNr + 1] = {filename = filename, is_file = true}

local count = util.count(items)

Expand Down Expand Up @@ -91,7 +97,7 @@ end
---@param items Diagnostics[]
function renderer.render_diagnostics(view, text, items)
for _, diag in ipairs(items) do
view.items[text.lineNr] = diag
view.items[text.lineNr + 1] = diag

local sign = signs[string.lower(diag.type)]
if not sign then sign = diag.type end
Expand Down
Loading

0 comments on commit 160fa6c

Please sign in to comment.