Skip to content

Commit 9cb0e24

Browse files
committed
feat: add which-key integration to mappings table
1 parent b455099 commit 9cb0e24

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

lua/astrolsp/init.lua

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,18 +202,30 @@ M.on_attach = function(client, bufnr)
202202
end
203203
end
204204

205+
local wk_avail, wk = pcall(require, "which-key")
205206
for mode, maps in pairs(M.config.mappings) do
206207
for lhs, map_opts in pairs(maps) do
207208
if
208-
map_opts.cond == nil
209+
type(map_opts) ~= "table"
210+
or map_opts.cond == nil
209211
or type(map_opts.cond) == "boolean" and map_opts.cond
210212
or type(map_opts.cond) == "function" and map_opts.cond(client, bufnr)
211213
or type(map_opts.cond) == "string" and client.supports_method(map_opts.cond)
212214
then
213-
local rhs = map_opts[1]
214-
map_opts = vim.tbl_deep_extend("force", map_opts, { buffer = bufnr })
215-
map_opts[1], map_opts.cond = nil, nil
216-
vim.keymap.set(mode, lhs, rhs, map_opts)
215+
local rhs = map_opts
216+
if type(map_opts) == "table" then
217+
rhs = map_opts[1]
218+
map_opts = assert(vim.tbl_deep_extend("force", map_opts, { buffer = bufnr }))
219+
map_opts[1], map_opts.cond = nil, nil
220+
else
221+
map_opts = { buffer = bufnr }
222+
end
223+
if not rhs or map_opts.name then
224+
if not map_opts.name then map_opts.name = map_opts.desc end
225+
if wk_avail then wk.register({ [lhs] = map_opts }, { mode = mode }) end
226+
else
227+
vim.keymap.set(mode, lhs, rhs, map_opts)
228+
end
217229
end
218230
end
219231
end

0 commit comments

Comments
 (0)