Skip to content

Commit

Permalink
refactor: got rid of views. only modes and sections now
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Oct 28, 2023
1 parent 7309018 commit 355fad1
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 51 deletions.
17 changes: 7 additions & 10 deletions lua/trouble/config/init.lua
@@ -1,14 +1,12 @@
---@class trouble.Config.mod: trouble.Config
local M = {}

---@class trouble.Mode: trouble.Config
---@field filter? trouble.Filter.spec Optional filter to apply to items in all sections
---@class trouble.Mode: trouble.Config,trouble.Section.spec
---@field sections? string[]

---@class trouble.Config
---@field mode? string
---@field config? fun(opts:trouble.Config)
---@field sections table<string,trouble.Section.spec>
---@field formatters table<string,trouble.Formatter>
local defaults = {
debug = false,
Expand All @@ -29,8 +27,6 @@ local defaults = {
filters = {}, -- custom filters
---@type table<string, trouble.SorterFn>
sorters = {}, -- custom sorters
---@type table<string, trouble.Section.spec>
views = {}, -- custom sections
---@type table<string, string|trouble.Action>
keys = {
["?"] = "help",
Expand Down Expand Up @@ -175,8 +171,12 @@ end

function M.modes()
require("trouble.source").load()
---@type string[]
local ret = vim.tbl_keys(options.modes)
local ret = {} ---@type string[]
for k, v in pairs(options.modes) do
if v.source or v.mode or v.sections then
ret[#ret + 1] = k
end
end
table.sort(ret)
return ret
end
Expand Down Expand Up @@ -227,9 +227,6 @@ function M.get(...)
ret.config(ret)
end
ret.mode = first_mode
if ret.mode then
ret.modes = {}
end

return ret
end
Expand Down
6 changes: 0 additions & 6 deletions lua/trouble/source.lua
Expand Up @@ -30,12 +30,6 @@ function M.register(name, source)
end
require("trouble.config.highlights").source(name, source.highlights)
if source.config then
source.config.modes = source.config.modes or {}
for view in pairs(source.config.views or {}) do
source.config.modes[view] = source.config.modes[view] or {
sections = { view },
}
end
Config.defaults(source.config)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lua/trouble/sources/diagnostics.lua
Expand Up @@ -11,7 +11,7 @@ M.highlights = {
}

M.config = {
views = {
modes = {
diagnostics = {
events = { "DiagnosticChanged", "BufEnter" },
-- Trouble classic for other buffers,
Expand Down
25 changes: 13 additions & 12 deletions lua/trouble/sources/lsp.lua
@@ -1,6 +1,7 @@
---@diagnostic disable: inject-field
local methods = vim.lsp.protocol.Methods
local Item = require("trouble.item")
local Util = require("trouble.util")
local get_col = vim.lsp.util._get_line_byte_from_position

---@type trouble.Source
Expand All @@ -9,7 +10,7 @@ local M = {}

---@diagnostic disable-next-line: missing-fields
M.config = {
views = {
modes = {
lsp_document_symbols = {
title = "{hl:Title}Document Symbols{hl} {count}",
events = { "BufEnter", { event = "TextChanged", main = true } },
Expand All @@ -21,8 +22,14 @@ M.config = {
-- sort = { { buf = 0 }, { kind = "Function" }, "filename", "pos", "text" },
format = "{kind_icon} {symbol.name} {text:Comment} {pos}",
},
},
modes = {
lsp_base = {
events = { { event = "CursorHold", main = true } },
groups = {
{ "filename", format = "{file_icon} {filename} {count}" },
},
sort = { { buf = 0 }, "filename", "pos", "text" },
format = "{text:ts} ({item.client}) {pos}",
},
lsp = {
sections = {
"lsp_definitions",
Expand All @@ -36,16 +43,10 @@ M.config = {
}

for _, mode in ipairs({ "definitions", "references", "implementations", "type_definitions", "declarations" }) do
M.config.views["lsp_" .. mode] = {
title = "{hl:Title}" .. mode:gsub("^%l", string.upper) .. "{hl} {count}",
events = { { event = "CursorHold", main = true } },
-- events = { "CursorHold", "CursorMoved" },
M.config.modes["lsp_" .. mode] = {
mode = "lsp_base",
title = "{hl:Title}" .. Util.camel(mode, " ") .. "{hl} {count}",
source = "lsp." .. mode,
groups = {
{ "filename", format = "{file_icon} {filename} {count}" },
},
sort = { { buf = 0 }, "filename", "pos", "text" },
format = "{text:ts} ({item.client}) {pos}",
}
end

Expand Down
4 changes: 2 additions & 2 deletions lua/trouble/sources/qf.lua
Expand Up @@ -22,7 +22,7 @@ local Item = require("trouble.item")
local M = {}

M.config = {
views = {
modes = {
qflist = {
events = { "BufEnter", "QuickFixCmdPost" },
source = "qf.qflist",
Expand All @@ -43,7 +43,7 @@ M.config = {
},
},
}
M.config.views.quickfix = M.config.views.qflist
M.config.modes.quickfix = M.config.modes.qflist

local severities = {
E = vim.diagnostic.severity.ERROR,
Expand Down
20 changes: 16 additions & 4 deletions lua/trouble/spec.lua
@@ -1,3 +1,5 @@
local Config = require("trouble.config")

---@alias trouble.SorterFn fun(item: trouble.Item): any?

---@alias trouble.Sort.spec string|trouble.SorterFn|(string|trouble.SorterFn|trouble.Filter.spec)[]
Expand Down Expand Up @@ -81,11 +83,21 @@ function M.section(spec)
return ret
end

---@param spec trouble.Section.spec|trouble.Section.spec[]
---@param mode trouble.Mode
---@return trouble.Section[]
function M.sections(spec)
spec = vim.tbl_islist(spec) and spec or { spec }
return vim.tbl_map(M.section, spec)
function M.sections(mode)
local ret = {} ---@type trouble.Section[]

if mode.sections then
for _, s in ipairs(mode.sections) do
ret[#ret + 1] = M.section(Config.get(mode, { sections = false }, s) --[[@as trouble.Mode]])
end
else
local section = M.section(mode)
section.max_items = section.max_items or mode.max_items
ret[#ret + 1] = section
end
return ret
end

---@param spec trouble.Sort.spec
Expand Down
12 changes: 8 additions & 4 deletions lua/trouble/util.lua
Expand Up @@ -40,12 +40,16 @@ function M.debug(msg, ...)
end

---@param str string
function M.camel(str)
---@param sep? string
function M.camel(str, sep)
local parts = vim.split(str, "[%.%-%_]")
---@diagnostic disable-next-line: no-unknown
return table.concat(vim.tbl_map(function(part)
return part:sub(1, 1):upper() .. part:sub(2)
end, parts))
return table.concat(
vim.tbl_map(function(part)
return part:sub(1, 1):upper() .. part:sub(2)
end, parts),
sep or ""
)
end

-- throttle with trailing execution
Expand Down
17 changes: 5 additions & 12 deletions lua/trouble/view/init.lua
@@ -1,3 +1,4 @@
local Config = require("trouble.config")
local Filter = require("trouble.filter")
local Preview = require("trouble.view.preview")
local Render = require("trouble.view.render")
Expand Down Expand Up @@ -38,18 +39,10 @@ function M.new(opts)
self.items = {}
self.fetching = 0
self.nodes = {}
self.sections = {}
for _, view in ipairs(self.opts.sections or {}) do
local spec = self.opts.views[view]
if spec then
local section = Spec.section(self.opts.views[view])
section.max_items = section.max_items or self.opts.max_items
table.insert(self.sections, section)
table.insert(self.items, {})
table.insert(self.nodes, {})
else
Util.error("View not found: " .. view)
end
self.sections = Spec.sections(self.opts)
for _ in ipairs(self.sections) do
self.items[#self.items + 1] = {}
self.nodes[#self.nodes + 1] = {}
end
self.win = Window.new(opts.win)
self.opts.win = self.win.opts
Expand Down

0 comments on commit 355fad1

Please sign in to comment.