From 2adbc17e00061073f2c2a40b6420ee2a80ea458d Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Tue, 6 Jul 2021 10:40:07 +0200 Subject: [PATCH] fix: dont show errors about loading order of setup and register --- lua/which-key/init.lua | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/lua/which-key/init.lua b/lua/which-key/init.lua index 7cbb13ad..c7f461f4 100644 --- a/lua/which-key/init.lua +++ b/lua/which-key/init.lua @@ -4,15 +4,24 @@ local Util = require("which-key.util") ---@class WhichKey local M = {} -local did_setup = false -function M.setup(options) - did_setup = true - require("which-key.config").setup(options) +local loaded = false -- once we loaded everything +local scheduled = false + +local function schedule_load() + if scheduled then + return + end if vim.v.vim_did_enter == 0 then vim.cmd([[au VimEnter * ++once lua require("which-key").load()]]) else M.load() end + scheduled = true +end + +function M.setup(options) + require("which-key.config").setup(options) + schedule_load() end function M.execute(id) @@ -58,13 +67,10 @@ function M.show_command(keys, mode) end local queue = {} -local loaded = false -- once we loaded everything -- Defer registering keymaps until VimEnter function M.register(mappings, opts) - if not did_setup then - Util.error("Did you forget to run WhichKey setup?") - end + schedule_load() if loaded then Keys.register(mappings, opts) Keys.update()