Skip to content

Commit

Permalink
feat: make file grouping and padding configurable (#66)
Browse files Browse the repository at this point in the history
* Use arguments passed to `TroubleToggle`

Arguments passed to `:TroubleToggle` were being ignored. Process them
just like arguments passed to `:Trouble`.

* Implement "group" and "padding" flags

The "group" flags avoids grouping results, which is very useful when
only seeing results for a single file (where collapsing/expanding makes
less sense).

The "padding" flag controls whitespace around results, which also plays
well with "group=false" too.
  • Loading branch information
Hugo Osvaldo Barrera committed Oct 22, 2021
1 parent afb300f commit ff40475
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 19 deletions.
2 changes: 2 additions & 0 deletions lua/trouble/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ M.namespace = vim.api.nvim_create_namespace("Trouble")
local defaults = {
debug = false,
cmd_options = {},
group = true, -- group results by file
padding = 1, -- padding to use inside results window
position = "bottom", -- position of the list can be: bottom, top, left, right
height = 10, -- height of the trouble list when position is top or bottom
width = 50, -- width of the list when position is left or right
Expand Down
4 changes: 2 additions & 2 deletions lua/trouble/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ function Trouble.toggle(...)

if opts.mode and (opts.mode ~= config.options.mode) then
config.options.mode = opts.mode
Trouble.open()
Trouble.open(...)
return
end

if is_open() then
Trouble.close()
else
Trouble.open()
Trouble.open(...)
end
end

Expand Down
45 changes: 28 additions & 17 deletions lua/trouble/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ function renderer.render(view, opts)
local text = Text:new()
view.items = {}

text:nl()
if config.options.padding == 1 then
text:nl()
end

-- render file groups
for filename, group_items in pairs(grouped) do
Expand All @@ -82,24 +84,26 @@ end
function renderer.render_file(view, text, filename, items)
view.items[text.lineNr + 1] = { filename = filename, is_file = true }

local count = util.count(items)
if view.group == true then
local count = util.count(items)

text:render(" ")
text:render(" ")

if folds.is_folded(filename) then
text:render(config.options.fold_closed, "FoldIcon", " ")
else
text:render(config.options.fold_open, "FoldIcon", " ")
end
if folds.is_folded(filename) then
text:render(config.options.fold_closed, "FoldIcon", " ")
else
text:render(config.options.fold_open, "FoldIcon", " ")
end

if config.options.icons then
local icon, icon_hl = get_icon(filename)
text:render(icon, icon_hl, { exact = true, append = " " })
end
if config.options.icons then
local icon, icon_hl = get_icon(filename)
text:render(icon, icon_hl, { exact = true, append = " " })
end

text:render(vim.fn.fnamemodify(filename, ":p:."), "File", " ")
text:render(" " .. count .. " ", "Count")
text:nl()
text:render(vim.fn.fnamemodify(filename, ":p:."), "File", " ")
text:render(" " .. count .. " ", "Count")
text:nl()
end

if not folds.is_folded(filename) then
renderer.render_diagnostics(view, text, items)
Expand All @@ -119,8 +123,15 @@ function renderer.render_diagnostics(view, text, items)
end

local indent = " "
if config.options.indent_lines then
indent = ""
if config.options.padding == 1 then
indent = " "
if config.options.indent_lines then
indent = ""
end
else
if config.options.indent_lines then
indent = ""
end
end

local sign_hl = diag.sign_hl or ("TroubleSign" .. diag.type)
Expand Down
9 changes: 9 additions & 0 deletions lua/trouble/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,20 @@ end

function View:new(opts)
opts = opts or {}

local group
if opts.group ~= nil then
group = opts.group
else
group = config.options.group
end

local this = {
buf = vim.api.nvim_get_current_buf(),
win = opts.win or vim.api.nvim_get_current_win(),
parent = opts.parent,
items = {},
group = group,
}
setmetatable(this, self)
return this
Expand Down

0 comments on commit ff40475

Please sign in to comment.