Skip to content

Commit

Permalink
feat(health): move health check to separate health file
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Apr 17, 2023
1 parent c9c430a commit b56c512
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 47 deletions.
3 changes: 0 additions & 3 deletions autoload/health/which_key.vim

This file was deleted.

55 changes: 55 additions & 0 deletions lua/which-key/health.lua
@@ -0,0 +1,55 @@
local Keys = require("which-key.keys")

local M = {}

local start = vim.health.start or vim.health.report_start
-- local ok = vim.health.ok or vim.health.report_ok
local warn = vim.health.warn or vim.health.report_warn
local error = vim.health.error or vim.health.report_error
local info = vim.health.info or vim.health.report_info

function M.check()
start("WhichKey: checking conflicting keymaps")
for _, tree in pairs(Keys.mappings) do
Keys.update_keymaps(tree.mode, tree.buf)
tree.tree:walk(
---@param node Node
function(node)
local count = 0
for _ in pairs(node.children) do
count = count + 1
end

local auto_prefix = not node.mapping or (node.mapping.group == true and not node.mapping.cmd)
if node.prefix_i ~= "" and count > 0 and not auto_prefix then
local msg = ("conflicting keymap exists for mode **%q**, lhs: **%q**"):format(tree.mode, node.mapping.prefix)
warn(msg)
local cmd = node.mapping.cmd or " "
info(("rhs: `%s`"):format(cmd))
end
end
)
end
if next(Keys.duplicates) == nil then
vim.fn["health#report_ok"]("No conflicting keymaps found")
return
end
for _, dup in pairs(Keys.duplicates) do
local msg = ""
if dup.buf == dup.other.buffer then
msg = "duplicate keymap"
else
msg = "buffer-local keymap overriding global"
end
msg = (msg .. " for mode **%q**, buf: %d, lhs: **%q**"):format(dup.mode, dup.buf or 0, dup.prefix)
if dup.buf == dup.other.buffer then
error(msg)
else
warn(msg)
end
info(("old rhs: `%s`"):format(dup.other.rhs or ""))
info(("new rhs: `%s`"):format(dup.cmd or ""))
end
end

return M
44 changes: 0 additions & 44 deletions lua/which-key/keys.lua
Expand Up @@ -368,50 +368,6 @@ function M.dump()
return todo
end

function M.check_health()
vim.health.report_start("WhichKey: checking conflicting keymaps")
for _, tree in pairs(M.mappings) do
M.update_keymaps(tree.mode, tree.buf)
tree.tree:walk(
---@param node Node
function(node)
local count = 0
for _ in pairs(node.children) do
count = count + 1
end

local auto_prefix = not node.mapping or (node.mapping.group == true and not node.mapping.cmd)
if node.prefix_i ~= "" and count > 0 and not auto_prefix then
local msg = ("conflicting keymap exists for mode **%q**, lhs: **%q**"):format(tree.mode, node.mapping.prefix)
vim.health.report_warn(msg)
local cmd = node.mapping.cmd or " "
vim.health.report_info(("rhs: `%s`"):format(cmd))
end
end
)
end
if next(M.duplicates) == nil then
vim.fn["health#report_ok"]("No conflicting keymaps found")
return
end
for _, dup in pairs(M.duplicates) do
local msg = ""
if dup.buf == dup.other.buffer then
msg = "duplicate keymap"
else
msg = "buffer-local keymap overriding global"
end
msg = (msg .. " for mode **%q**, buf: %d, lhs: **%q**"):format(dup.mode, dup.buf or 0, dup.prefix)
if dup.buf == dup.other.buffer then
vim.health.report_error(msg)
else
vim.health.report_warn(msg)
end
vim.health.report_info(("old rhs: `%s`"):format(dup.other.rhs or ""))
vim.health.report_info(("new rhs: `%s`"):format(dup.cmd or ""))
end
end

---@param mode string
---@param buf? buffer
function M.get_tree(mode, buf)
Expand Down

0 comments on commit b56c512

Please sign in to comment.