Skip to content

Commit

Permalink
feat(sources): added support for loading external sources
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed May 30, 2024
1 parent 332b25b commit 89ac6f1
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions lua/trouble/sources/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
local Config = require("trouble.config")
local Util = require("trouble.util")

---@class trouble.Source
---@field highlights? table<string, string>
---@field config? trouble.Config
---@field setup? fun()
---@field get trouble.Source.get|table<string, trouble.Source.get>

---@alias trouble.Source.ctx {main: trouble.Main, opts:trouble.Mode}
---@alias trouble.Source.Callback fun(items:trouble.Item[])
---@alias trouble.Source.get fun(cb:trouble.Source.Callback, ctx:trouble.Source.ctx)

local M = {}
---@type table<string, trouble.Source>
M.sources = {}

---@param name string
---@param source? trouble.Source
function M.register(name, source)
if M.sources[name] then
error("source already registered: " .. name)
end
source = source or require("trouble.sources." .. name)
if source then
if source.setup then
source.setup()
end
require("trouble.config.highlights").source(name, source.highlights)
if source.config then
Config.defaults(source.config)
end
end
M.sources[name] = source
return source
end

---@param source string
function M.get(source)
local parent, child = source:match("^(.-)%.(.*)$")
source = parent or source
local s = M.sources[source] or M.register(source)
if child and type(s.get) ~= "table" then
error("source does not support sub-sources: " .. source)
elseif child and type(s.get[child]) ~= "function" then
error("source does not support sub-source: " .. source .. "." .. child)
end
return child and s.get[child] or s.get
end

function M.load()
local rtp = vim.api.nvim_get_runtime_file("lua/trouble/sources/*.lua", true)
for _, file in ipairs(rtp) do
local name = file:match("lua/trouble/sources/(.*)%.lua")
if name and name ~= "init" and not M.sources[name] and package.loaded["trouble.sources." .. name] == nil then
Util.try(function()
M.register(name)
end, { msg = "Error loading source: " .. name })
end
end
end

return M

0 comments on commit 89ac6f1

Please sign in to comment.