Skip to content

Commit

Permalink
feat(config): make it easier to specify plugin paths in library
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Jun 2, 2024
1 parent 2e715cd commit 890c7d5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ return {
ft = "lua", -- only load on lua files
opts = {
library = {
vim.env.LAZY .. "/luvit-meta/library", -- see below
-- You can also add plugins you always want to have loaded.
-- Useful if the plugin has globals or types you want to use
-- vim.env.LAZY .. "/LazyVim", -- see below
-- Library items can be absolute paths
-- "~/projects/my-awesome-lib",
-- Or relative, which means they will be resolved as a plugin
-- "LazyVim",
-- When relative, you can also provide a path to the library in the plugin dir
"luvit-meta/library", -- see below
},
},
},
Expand Down
11 changes: 10 additions & 1 deletion lua/lazydev/buf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ end
---@param path string
function M.add(path)
path = vim.fs.normalize(path)
-- try to resolve to a plugin path
if path:sub(1, 1) ~= "/" and not vim.uv.fs_stat(path) then
local name, extra = path:match("([^/]+)(/?.*)")
if name then
local pp = Pkg.get_plugin_path(name)
path = pp and (pp .. extra) or path
end
end
-- append /lua if it exists
if not path:find("/lua/?$") and vim.uv.fs_stat(path .. "/lua") then
path = path .. "/lua"
end
Expand Down Expand Up @@ -198,7 +207,7 @@ function M.debug()
local lines = {}
for _, lib in ipairs(M.library) do
local plugin = Plugin.find(lib .. "/")
table.insert(lines, "- " .. (plugin and "**" .. plugin.name .. "**" or "`" .. lib .. "`"))
table.insert(lines, "- " .. (plugin and "**" .. plugin.name .. "** " or "") .. ("`" .. lib .. "`"))
end
Util.info(lines, { title = "lazydev.nvim" })
end
Expand Down
30 changes: 27 additions & 3 deletions lua/lazydev/pkg.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ end
---@type string[]
local packs = nil

---@param modname string
function M.pack_unloaded(modname)
function M.pack_unloaded()
if packs then
return packs
end
Expand All @@ -43,7 +42,32 @@ function M.pack_unloaded(modname)
return packs
end

M.get_unloaded = is_lazy and M.lazy_unloaded or M.pack_unloaded
---@param modname string
---@return string[]
function M.get_unloaded(modname)
return is_lazy and M.lazy_unloaded(modname) or M.pack_unloaded()
end

function M.get_plugin_path(name)
if is_lazy then
local Config = require("lazy.core.config")
local plugin = Config.spec.plugins[name]
return plugin and plugin.dir
else
for _, dir in ipairs(vim.opt.rtp:get()) do
local basename = vim.fs.basename(dir)
if basename == name then
return dir
end
end
for _, dir in ipairs(M.pack_unloaded()) do
local basename = vim.fs.basename(dir)
if basename == name then
return dir
end
end
end
end

---@param modname string
---@return string[]
Expand Down

0 comments on commit 890c7d5

Please sign in to comment.