Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add string array support to open_mapping setting. #557

Merged
merged 3 commits into from
Apr 22, 2024

Conversation

sbkkalex
Copy link
Contributor

I sometime use a Japanese keyboard, and it don't have the \ key, instead it have a ¥ key.

It would be helpful for people like me who what to bind multiple keys for toggle terminal. So I modified the open_mapping setting to also take an array of string as parameter.

Copy link
Owner

@akinsho akinsho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sbkkalex thanks for this PR, happy to have this more flexible to accommodate other folks 👍🏾, I've just made a few comments mainly around typos and the duplication

README.md Outdated
@@ -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_mappint` can be a key string or an array of key strings.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Setting the `open_mapping` key to use for toggling the terminal(s) will set up mappings for _normal_ mode. The `open_mappint` can be a key string or an array of key strings.
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.

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.
mappings for _normal_ mode. The `open_mappint` can be a key string or an array
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
mappings for _normal_ mode. The `open_mappint` can be a key string or an array
mappings for _normal_ mode. The `open_mapping` can be a key string or an array


-- Key mapping function
---@param key string
local map = function(key)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer the style

Suggested change
local map = function(key)
local function map(key)

desc = "Toggle Terminal",
silent = true,
})
end
end

if type(mapping) == "string" then map(mapping) end
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although it's impossible (🤞🏾) I'd rather this was an if then else if I feel uncomfortable with the idea that it could possible fall into the next clause even though it can't

Comment on lines 135 to 148

-- Key mapping function
---@param key string
local map = function(key)
vim.keymap.set("t", key, "<Cmd>ToggleTerm<CR>", { buffer = bufnr, silent = true })
end

if config.terminal_mappings then
if type(mapping) == "string" then map(mapping) end
if type(mapping) == "table" then
for _, key in pairs(mapping) do
map(key)
end
end
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks to me like this logic is just being repeated maybe it's best to do this once in utils.lua generically and use that helper function in both of the places you've made this change

@sbkkalex
Copy link
Contributor Author

@akinsho
Thank you for your advise. I extracted the logic to utils.

@sbkkalex sbkkalex requested a review from akinsho April 20, 2024 14:36
@akinsho akinsho merged commit 5ec59c3 into akinsho:main Apr 22, 2024
3 of 4 checks passed
@sbkkalex sbkkalex deleted the feat/multi-keymaps branch April 22, 2024 23:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants