fix: Better mappings handling

This commit is contained in:
TJ DeVries
2021-11-19 14:09:43 -05:00
committed by Simon Hauser
parent ac38730da1
commit f68d0c2477
4 changed files with 92 additions and 39 deletions
+62 -26
View File
@@ -87,7 +87,7 @@
--- }
--- </code>
---
--- There are three main places you can configure |telescope.mappings|. These are
--- There are four main places you can configure |telescope.mappings|. These are
--- ordered from the lowest priority to the highest priority.
---
--- 1. |telescope.defaults.mappings|
@@ -105,7 +105,19 @@
--- },
--- }
--- </code>
--- 3. `attach_mappings` function for a particular picker.
--- 3. The `mappings` key for a particular picker.
--- <code>
--- require("telescope.builtin").fd {
--- mappings = {
--- i = {
--- asdf = function()
--- print "You typed asdf"
--- end,
--- },
--- },
--- }
--- </code>
--- 4. `attach_mappings` function for a particular picker.
--- <code>
--- require("telescope.builtin").find_files {
--- attach_mappings = function(_, map)
@@ -222,6 +234,8 @@ local telescope_map = function(prompt_bufnr, mode, key_bind, key_func, opts)
return
end
key_bind = a.nvim_replace_termcodes(key_bind, true, true, true)
opts = opts or {}
if opts.noremap == nil then
opts.noremap = true
@@ -278,14 +292,52 @@ local extract_keymap_opts = function(key_func)
return {}
end
mappings.apply_keymap = function(prompt_bufnr, attach_mappings, buffer_keymap)
local applied_mappings = { n = {}, i = {} }
local termcode_mt = {
__index = function(t, k)
return rawget(t, a.nvim_replace_termcodes(k, true, true, true))
end,
__newindex = function(t, k, v)
rawset(t, a.nvim_replace_termcodes(k, true, true, true), v)
end,
}
local mode_mt = {
__index = function(t, k)
k = string.lower(k)
if rawget(t, k) then
return rawget(t, k)
end
local val = setmetatable({}, termcode_mt)
rawset(t, k, val)
return val
end,
__newindex = function(t, k, v)
rawset(t, string.lower(k), v)
end,
}
-- Apply the keymaps for a given set of configurations
mappings.apply_keymap = function(prompt_bufnr, attach_mappings, ...)
local mappings_applied = setmetatable({}, mode_mt)
-- Set the mappings_config, and make sure that all transformations are applied
local mappings_config = setmetatable({}, mode_mt)
for mode, mode_map in pairs(vim.tbl_deep_extend("force", mappings.default_mappings or {}, ...)) do
for key_bind, val in pairs(mode_map) do
mappings_config[mode][key_bind] = val
end
end
local map = function(mode, key_bind, key_func, opts)
mode = string.lower(mode)
local key_bind_internal = a.nvim_replace_termcodes(key_bind, true, true, true)
applied_mappings[mode][key_bind_internal] = true
-- Skip maps that are disabled by the user
if mappings_config[mode][key_bind] == false then
return
end
mappings_applied[mode][key_bind] = true
telescope_map(prompt_bufnr, mode, key_bind, key_func, opts)
end
@@ -304,26 +356,10 @@ mappings.apply_keymap = function(prompt_bufnr, attach_mappings, buffer_keymap)
end
end
for mode, mode_map in pairs(buffer_keymap or {}) do
mode = string.lower(mode)
for mode, mode_map in pairs(mappings_config) do
for key_bind, key_func in pairs(mode_map) do
local key_bind_internal = a.nvim_replace_termcodes(key_bind, true, true, true)
if not applied_mappings[mode][key_bind_internal] then
applied_mappings[mode][key_bind_internal] = true
telescope_map(prompt_bufnr, mode, key_bind, key_func, extract_keymap_opts(key_func))
end
end
end
-- TODO: Probably should not overwrite any keymaps
for mode, mode_map in pairs(mappings.default_mappings) do
mode = string.lower(mode)
for key_bind, key_func in pairs(mode_map) do
local key_bind_internal = a.nvim_replace_termcodes(key_bind, true, true, true)
if not applied_mappings[mode][key_bind_internal] then
applied_mappings[mode][key_bind_internal] = true
if not mappings_applied[mode][key_bind] then
mappings_applied[mode][key_bind] = true
telescope_map(prompt_bufnr, mode, key_bind, key_func, extract_keymap_opts(key_func))
end
end