From 0d4678e0be8d7759007f424fc2ee065ef9ce467f Mon Sep 17 00:00:00 2001 From: Micah Halter Date: Mon, 28 Oct 2024 12:29:30 -0400 Subject: [PATCH] feat(health): add health check for conflicting mappings --- lua/astrocore/health.lua | 65 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 lua/astrocore/health.lua diff --git a/lua/astrocore/health.lua b/lua/astrocore/health.lua new file mode 100644 index 0000000..c0144ed --- /dev/null +++ b/lua/astrocore/health.lua @@ -0,0 +1,65 @@ +-- ### AstroCore Health Checks +-- +-- use with `:checkhealth astrocore` +-- +-- copyright 2024 +-- license GNU General Public License v3.0 + +local M = {} + +local astrocore = require "astrocore" +local health = vim.health + +local function check_mapping_conflicts(all_maps) + local set_mappings, any_duplicates = {}, false + for mode, mappings in pairs(all_maps) do + for lhs, rhs in pairs(mappings) do + if rhs then + if not set_mappings[mode] then set_mappings[mode] = {} end + local normalized_lhs = vim.api.nvim_replace_termcodes(lhs, true, true, true) + if set_mappings[mode][normalized_lhs] then + set_mappings[mode][normalized_lhs][lhs] = rhs + set_mappings[mode][normalized_lhs][1] = true + any_duplicates = true + else + set_mappings[mode][normalized_lhs] = { [1] = false, [lhs] = rhs } + end + end + end + end + + if any_duplicates then + local msg = "" + for mode, mappings in pairs(set_mappings) do + local mode_msg + for _, duplicate_mappings in pairs(mappings) do + if duplicate_mappings[1] then + if not mode_msg then + mode_msg = ("Conflicting mappings detected in mode `%s`:\n"):format(mode) + else + mode_msg = mode_msg .. "\n" + end + for lhs, rhs in pairs(duplicate_mappings) do + if type(lhs) == "string" then + mode_msg = mode_msg .. ("- %s: %s\n"):format(lhs, type(rhs) == "table" and (rhs.desc or rhs[1]) or rhs) + end + end + end + end + if mode_msg then msg = msg .. mode_msg end + end + health.warn( + msg, + "Make sure to normalize the left hand side of mappings to what is used in :h keycodes. This includes making sure to capitalize and ." + ) + else + health.ok "No duplicate mappings detected" + end +end + +function M.check() + health.start "Checking for conflicting mappings" + check_mapping_conflicts(astrocore.config.mappings) +end + +return M