-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sources): added support for loading external sources
- Loading branch information
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |