Skip to content

Commit

Permalink
feat: extend kemaps (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
luckasRanarison committed Dec 5, 2023
1 parent 69b1d18 commit 7534615
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ The default configuration:
mappings = {
-- The values can either be a string or a string tuple (with description)
-- example: "<leader>aq" | { "<leader>aq", "Quickfix" }
-- Or if you want more control: { key = "leader<aq>", mode = { "n" }, options = { desc = "Quickfix" } }
-- `options` accetps the same keys as vim.keymap.set()
code_action = nil, -- a modified version of `vim.lsp.buf.code_action`
apply_first = nil, -- directly applies the first code action
-- These are just basically `vim.lsp.buf.code_action` with the `apply` option with some filters
Expand Down
40 changes: 24 additions & 16 deletions lua/clear-action/mappings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,51 @@ local config = require("clear-action.config")
local actions = require("clear-action.actions")

local function parse_value(value)
local key, desc
if not vim.tbl_islist(value) then return value end

local opts = {
options = {},
}

if type(value) == "string" then
key = value
opts.key = value
elseif type(value) == "table" then
key = value[1]
desc = value[2]
opts.key = value[1]
opts.options.desc = value[2]
end

return key, desc
return opts
end

M.on_attach = function(bufnr, client)
local mappings = config.options.mappings
local client_keymaps = mappings.actions[client.name]
local quickfix_filters = config.options.quickfix_filters[client.name]

local function set(mode, lhs, rhs, desc)
vim.keymap.set(mode, lhs, rhs, {
silent = true,
buffer = bufnr,
desc = desc,
})
local function set(opts, action)
vim.keymap.set(
opts.mode or { "n", "v" },
opts.key,
action,
vim.tbl_extend("force", {
silent = true,
buffer = bufnr,
}, opts.options or {})
)
end

for name, value in pairs(mappings) do
local key, desc = parse_value(value)
if name ~= "actions" and key and actions[name] then
local opts = parse_value(value)
if name ~= "actions" and opts.key and actions[name] then
local arg = vim.startswith(name, "quickfix") and quickfix_filters
set({ "n", "v" }, key, function() actions[name](arg) end, desc)
set(opts, function() actions[name](arg) end)
end
end

if client_keymaps then
for action_prefix, value in pairs(client_keymaps) do
local key, desc = parse_value(value)
set({ "n", "v" }, key, function() actions.apply(action_prefix) end, desc)
local opts = parse_value(value)
set(opts, function() actions.apply(action_prefix) end)
end
end
end
Expand Down

0 comments on commit 7534615

Please sign in to comment.