feat: add some new items and make mappings easier

This commit is contained in:
TJ DeVries
2020-09-02 00:06:03 -04:00
parent 9f906f0392
commit 061307233c
8 changed files with 242 additions and 43 deletions

View File

@@ -29,14 +29,14 @@ end
--[[
Usage:
mappings.apply_keymap(42, {
mappings.apply_keymap(42, <function>, {
n = {
["<leader>x"] = "just do this string",
["<CR>"] = function(picker, prompt_bufnr)
actions.close_prompt()
local filename = ...
> local filename = ...
vim.cmd(string.format(":e %s", filename))
end,
},
@@ -45,41 +45,75 @@ mappings.apply_keymap(42, {
}
})
--]]
mappings.apply_keymap = function(prompt_bufnr, buffer_keymap)
local telescope_map = function(prompt_bufnr, mode, key_bind, key_func, opts)
opts = opts or {
silent = true
}
if type(key_func) == "string" then
a.nvim_buf_set_keymap(
prompt_bufnr,
mode,
key_bind,
key_func,
opts or {
silent = true
}
)
else
local key_id = assign_function(prompt_bufnr, key_func)
local prefix = ""
local map_string
if opts.expr then
map_string = string.format(
[[luaeval("require('telescope.mappings').execute_keymap(%s, %s)")]],
prompt_bufnr,
key_id
)
else
if mode == "i" and not opts.expr then
prefix = "<C-O>"
end
map_string = string.format(
"%s:lua require('telescope.mappings').execute_keymap(%s, %s)<CR>",
prefix,
prompt_bufnr,
key_id
)
end
a.nvim_buf_set_keymap(
prompt_bufnr,
mode,
key_bind,
map_string,
opts
)
end
end
mappings.apply_keymap = function(prompt_bufnr, attach_mappings, buffer_keymap)
local applied_mappings = { n = {}, i = {} }
local map = function(mode, key_bind, key_func, opts)
applied_mappings[mode][key_bind] = true
telescope_map(prompt_bufnr, mode, key_bind, key_func, opts)
end
if attach_mappings and not attach_mappings(map) then
return
end
for mode, mode_map in pairs(buffer_keymap) do
-- TODO: Probalby should not overwrite any keymaps
-- local buffer_keymaps
for key_bind, key_func in pairs(mode_map) do
if type(key_func) == "string" then
a.nvim_buf_set_keymap(
prompt_bufnr,
mode,
key_bind,
key_func,
{
silent = true
}
)
else
local key_id = assign_function(prompt_bufnr, key_func)
local prefix = ""
if mode == "i" then
prefix = "<C-O>"
end
a.nvim_buf_set_keymap(
prompt_bufnr,
mode,
key_bind,
string.format(
"%s:lua require('telescope.mappings').execute_keymap(%s, %s)<CR>",
prefix,
prompt_bufnr,
key_id
),
{
silent = true
}
)
if not applied_mappings[mode][key_bind] then
telescope_map(prompt_bufnr, mode, key_bind, key_func)
end
end
end