nvim-neorocks / lz.n

🦥 A dead simple lazy-loading Lua library for Neovim plugins.
GNU General Public License v2.0
47 stars 5 forks source link

[Feature] API for querying plugin status #24

Open mrcjkb opened 1 week ago

mrcjkb commented 1 week ago

It says a UI is non-goal for this plugin. Maybe a separate extension plugin for it. But for now, to help debug a config, perhaps an API that can give a list of all plugins managed by lz.n and full spec of each plugin plus an is_loaded boolean?

Originally posted by @pseudoparenchymatous in https://github.com/nvim-neorocks/lz.n/discussions/18

BirdeeHub commented 1 week ago

This could be implemented as a handler + using require('lz.n.state')

Whenever a plugin is loaded, del gets called. Simply have the handler keep an internal state of the plugin names that were called, and a function to require('lz.n.state') and zip in a is_loaded field into the ones that were loaded already when it returns the list.

BirdeeHub commented 1 week ago

Probably only like 50 lines or so. Should it be added to the plugin or should it just be posted in discussions?

BirdeeHub commented 1 week ago

Kinda quick and dirty but this works:

---@type table<string, string>
local states = {}

local M = {
  ---@type lz.n.Handler
  handler = {
    -- this field does nothing but it does stop others from using is_loaded,
    -- which is good because we overwrite the value in getAllPlugins
    spec_field = "is_loaded",
    ---@param plugin lz.n.Plugin
    del = function (plugin)
      states[plugin.name] = plugin.name
    end,
    add = function(_) end,
  },
}

function M.get_all_plugins()
  local result = vim.deepcopy(require("lz.n.state").plugins)
  for _, name in pairs(states) do
    if result[name] ~= nil then
      ---@diagnostic disable-next-line: inject-field
      result[name].is_loaded = true
    end
  end
  return result
end

function M.get_a_plugin(name)
  local result = vim.deepcopy(require("lz.n.state").plugins[name])
  if result ~= nil and states[name] ~= nil then
    ---@diagnostic disable-next-line: inject-field
    result.is_loaded = true
  end
  return result
end

return M

Simply add require('lz.n').register_handler(require('the.file').handler) to the start of your config

and then you can call :lua print(vim.inspect(require('the.file').get_all_plugins())) and it will print out all the specs and whether they were loaded or not or you can :lua print(vim.inspect(require('the.file').get_a_plugin("apluginname"))) for just 1