Skip to content

Commit

Permalink
feat: added a LazyDev debugging command that shows libraries and se…
Browse files Browse the repository at this point in the history
…ttings for the current buffer
  • Loading branch information
folke committed Jun 4, 2024
1 parent 39eb1f8 commit 3f6320d
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
62 changes: 62 additions & 0 deletions lua/lazydev/cmd.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
local Util = require("lazydev.util")
local Workspace = require("lazydev.workspace")

local M = {}

---@type table<string, fun(args: string[])>
M.commands = {
debug = function()
local buf = vim.api.nvim_get_current_buf()
local ws = Workspace.find(buf)
if not ws then
return Util.warn("No **LuaLS** workspace found.\nUse `:LazyDev lsp` to see settings of attached LSP clients.")
end
ws:debug({ details = true })
end,
lsp = function()
local clients = vim.lsp.get_clients({ bufnr = 0 })
local lines = {} ---@type string[]
for _, client in ipairs(clients) do
lines[#lines + 1] = "## " .. client.name
lines[#lines + 1] = "```lua"
lines[#lines + 1] = "settings = " .. vim.inspect(client.settings)
lines[#lines + 1] = "```"
end
Util.info(lines)
end,
}

function M.execute(input)
local prefix, args = M.parse(input.args)
prefix = prefix and prefix ~= "" and prefix or "debug"
if not M.commands[prefix or ""] then
return Util.error("Invalid command")
end
M.commands[prefix](args)
end

function M.complete(_, line)
local prefix, args = M.parse(line)
if #args > 0 then
return {}
end

---@param key string
return vim.tbl_filter(function(key)
return key:find(prefix, 1, true) == 1
end, vim.tbl_keys(M.commands))
end

---@return string, string[]
function M.parse(args)
local parts = vim.split(vim.trim(args), "%s+")
if parts[1]:find("LazyDev") then
table.remove(parts, 1)
end
if args:sub(-1) == " " then
parts[#parts + 1] = ""
end
return table.remove(parts, 1) or "", parts
end

return M
10 changes: 10 additions & 0 deletions lua/lazydev/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ function M.setup(opts)
end
end

vim.api.nvim_create_user_command("LazyDev", function(...)
require("lazydev.cmd").execute(...)
end, {
nargs = "*",
complete = function(...)
return require("lazydev.cmd").complete(...)
end,
desc = "lazydev.nvim",
})

vim.schedule(function()
require("lazydev.buf").setup()
require("lazydev.cmp").setup()
Expand Down
9 changes: 8 additions & 1 deletion lua/lazydev/workspace.lua
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ function M:update()
end
end

function M:debug()
---@param opts? {details: boolean}
function M:debug(opts)
opts = opts or {}
local root = M.is_special(self.root) and "[" .. self.root .. "]" or vim.fn.fnamemodify(self.root, ":~")
local lines = { "## " .. root }
---@type string[]
Expand All @@ -175,6 +177,11 @@ function M:debug()
local plugin = Pkg.get_plugin_name(lib .. "/")
table.insert(lines, "- " .. (plugin and "**" .. plugin .. "** " or "") .. ("`" .. lib .. "`"))
end
if opts.details then
lines[#lines + 1] = "```lua"
lines[#lines + 1] = "settings = " .. vim.inspect(self.settings)
lines[#lines + 1] = "```"
end
Util.info(lines)
end

Expand Down

0 comments on commit 3f6320d

Please sign in to comment.