feat: add string array support to open_mapping setting. (#557)

* Add an array type for open_mapping.

* Fix typos and minor fixes.

* Add keymap function to utils.
This commit is contained in:
Liu(**348**) 2024-04-22 20:10:39 +09:00 committed by GitHub
parent 17b4f1eeec
commit 5ec59c3a8a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 27 additions and 12 deletions

View file

@ -126,7 +126,7 @@ I'm also going to be pretty conservative about what I add.
This plugin must be explicitly enabled by using `require("toggleterm").setup{}`
Setting the `open_mapping` key to use for toggling the terminal(s) will set up mappings for _normal_ mode.
Setting the `open_mapping` key to use for toggling the terminal(s) will set up mappings for _normal_ mode. The `open_mapping` can be a key string or an array of key strings.
If you prefix the mapping with a number that particular terminal will be opened. Otherwise if a prefix is not set, then the last toggled terminal will be opened. In case there are multiple terminals opened they'll all be closed, and on the next mapping key they'll be restored.
If you set the `insert_mappings` key to `true`, the mapping will also take effect in insert mode; similarly setting `terminal_mappings` to `true` will have the mappings take effect in the opened terminal.
@ -161,7 +161,7 @@ require("toggleterm").setup{
return vim.o.columns * 0.4
end
end,
open_mapping = [[<c-\>]],
open_mapping = [[<c-\>]], -- or { [[<c-\>]], [[<c-¥>]] } if you also use a Japanese keyboard.
on_create = fun(t: Terminal), -- function to run when the terminal is first created
on_open = fun(t: Terminal), -- function to run when the terminal opens
on_close = fun(t: Terminal), -- function to run when the terminal closes

View file

@ -103,11 +103,11 @@ SETUP ~
This plugin must be explicitly enabled by using `require("toggleterm").setup{}`
Setting the `open_mapping` key to use for toggling the terminal(s) will set up
mappings for _normal_ mode. If you prefix the mapping with a number that
particular terminal will be opened. Otherwise if a prefix is not set, then the
last toggled terminal will be opened. In case there are multiple terminals
opened theyll all be closed, and on the next mapping key theyll be
restored.
mappings for _normal_ mode. The `open_mapping` can be a key string or an array
of key strings. If you prefix the mapping with a number that particular
terminal will be opened. Otherwise if a prefix is not set, then the last
toggled terminal will be opened. In case there are multiple terminals opened
theyll all be closed, and on the next mapping key theyll be restored.
If you set the `insert_mappings` key to `true`, the mapping will also take
effect in insert mode; similarly setting `terminal_mappings` to `true` will
@ -148,7 +148,7 @@ what options are available. It is not written to be used as is.
return vim.o.columns * 0.4
end
end,
open_mapping = [[<c-\>]],
open_mapping = [[<c-\>]], -- or { [[<c-\>]], [[<c-¥>]] } if you also use a Japanese keyboard.
on_create = fun(t: Terminal), -- function to run when the terminal is first created
on_open = fun(t: Terminal), -- function to run when the terminal opens
on_close = fun(t: Terminal), -- function to run when the terminal closes

View file

@ -37,12 +37,12 @@ local function setup_global_mappings()
local mapping = config.open_mapping
-- v:count defaults the count to 0 but if a count is passed in uses that instead
if mapping then
vim.keymap.set("n", mapping, '<Cmd>execute v:count . "ToggleTerm"<CR>', {
utils.key_map("n", mapping, '<Cmd>execute v:count . "ToggleTerm"<CR>', {
desc = "Toggle Terminal",
silent = true,
})
if config.insert_mappings then
vim.keymap.set("i", mapping, "<Esc><Cmd>ToggleTerm<CR>", {
utils.key_map("i", mapping, "<Esc><Cmd>ToggleTerm<CR>", {
desc = "Toggle Terminal",
silent = true,
})

View file

@ -18,7 +18,7 @@ local function shade(color, factor) return colors.shade_color(color, factor) end
--- @field size number
--- @field shade_filetypes string[]
--- @field hide_numbers boolean
--- @field open_mapping string
--- @field open_mapping string | string[]
--- @field shade_terminals boolean
--- @field insert_mappings boolean
--- @field terminal_mappings boolean

View file

@ -141,7 +141,7 @@ end
local function setup_buffer_mappings(bufnr)
local mapping = config.open_mapping
if mapping and config.terminal_mappings then
vim.keymap.set("t", mapping, "<Cmd>ToggleTerm<CR>", { buffer = bufnr, silent = true })
utils.key_map("t", mapping, "<Cmd>ToggleTerm<CR>", { buffer = bufnr, silent = true })
end
end

View file

@ -48,6 +48,21 @@ end
---@param sep string
function M.concat_without_empty(tbl, sep) return table.concat(M.tbl_filter_empty(tbl), sep) end
-- Key mapping function
---@param mod string | string[]
---@param lhs string | string[]
---@param rhs string | function
---@param opts table?
function M.key_map(mod, lhs, rhs, opts)
if type(lhs) == "string" then
vim.keymap.set(mod, lhs, rhs, opts)
elseif type(lhs) == "table" then
for _, key in pairs(lhs) do
vim.keymap.set(mod, key, rhs, opts)
end
end
end
---@param mode "visual" | "motion"
---@return table
function M.get_line_selection(mode)