Skip to content

Commit

Permalink
feat(mappings): Allows existing mappings to not conflict with defaults
Browse files Browse the repository at this point in the history
- README updated to reflect the new feature
  • Loading branch information
Adam Figgins committed Apr 10, 2023
1 parent d3da67d commit 5e96dee
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,15 @@ To trigger the lsp-overloads signature popup manually when in normal mode, you c
```
vim.api.nvim_set_keymap("n", "<A-s>", ":LspOverloadsSignature<CR>", { noremap = true, silent = true })
```
To instead toggle the display on and off, you can use

It is also useful to create the corresponding trigger mapping for insert mode too (helps when toggling the popup while in insert mode)
```
:LspOverloadsSignatureDisplayToggle
vim.api.nvim_set_keymap("i", "<A-s>", "<cmd>LspOverloadsSignature<CR>", { noremap = true, silent = true })
```

Closing the popup while typing can be done using the pre-configured `close_signature` keybind created when the signature window is created (see [Keybinds](#keybinds))
- It is recommended to override this to match the keybind you use to trigger the overload popup manually, so toggling is more intuitive

#### Toggling automatic display
lsp-verloads automatically shows itself by default when you are inside of a function signature and begin typing.
You can toggle this feature using
Expand All @@ -121,6 +125,8 @@ The default mappings are used to navigate between various signature overloads an
- `previous_parameter = "<C-h>"`
- `close_signature = "<A-s>"`

**NOTE: If you already have a keybinding that matches one of the above, it will only get overwritten when the signature popup is open. When the popup is closed, your original keybinding will be restored in the buffer. If you still need to keep your original mappings while the signature popup is open, you will need to modify these bindings so they no longer conflict**

### Additional Tips

- Any calls to `vim.lsp.buf.signature_help()` made while the plugin's signature popup is displayed, will behave
Expand Down
33 changes: 31 additions & 2 deletions lua/lsp-overloads/models/signature.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ local Signature = {
ctx = {},
config = {},
mappings = {},
original_buf_mappings = {},
bufnr = nil,
fwin = nil,
signature_content = SignatureContent:new(),
Expand Down Expand Up @@ -75,11 +76,32 @@ function Signature:add_mapping(mapName, default_lhs, rhs, opts)
self.mappings[self.bufnr] = {}
end

if self.original_buf_mappings[self.bufnr] == nil then
self.original_buf_mappings[self.bufnr] = {}
end

local config_lhs = self.mappings[self.bufnr][mapName] or default_lhs
if config_lhs == nil then
return
end

-- Check if we have already stored the users original keymapping value before
-- If we haven't, get it from the list of buf keymaps and store it, so that when the signature window is destroyed later,
-- we can restore the users original keymapping.
if self.original_buf_mappings[self.bufnr][config_lhs] == nil then
local original_buf_keymaps = vim.api.nvim_buf_get_keymap(self.bufnr, self.mode)

-- If the user has mapped <A-...> to something, then the keymap will be stored as <M-...>
local alt_modified_lhs = string.gsub(config_lhs, "<A%-", "<M%-")

for _, keymap in ipairs(original_buf_keymaps) do
if keymap.lhs == config_lhs or keymap.lhs == alt_modified_lhs then
self.original_buf_mappings[self.bufnr][config_lhs] = keymap
vim.keymap.del(self.mode, keymap.lhs, { buffer = self.bufnr })
end
end
end

vim.keymap.set(self.mode, config_lhs, function()
rhs(opts)
end, { buffer = self.bufnr, expr = true, nowait = true })
Expand All @@ -89,8 +111,15 @@ end

function Signature:remove_mappings(bufnr, mode)
for _, buf_local_mappings in pairs(self.mappings) do
for _, value in pairs(buf_local_mappings) do
vim.keymap.del(mode, value, { buffer = bufnr, silent = true })
for _, lhs in pairs(buf_local_mappings) do
vim.keymap.del(mode, lhs, { buffer = bufnr, silent = true })

-- Restore the original mapping if it existed
local original_buf_map = self.original_buf_mappings[bufnr][lhs]

if original_buf_map ~= nil then
vim.fn.mapset(mode, 0, original_buf_map)
end
end
end

Expand Down
2 changes: 2 additions & 0 deletions lua/lsp-overloads/types/signature-types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@
---@field ctx table The context
---@field config table The configuration
---@field signatures table The signatures
---@field mappings table<number, table<string, string>> The mapping list, indexed first by buffer number, then by mapping name, giving the lhs value of the mapping
---@field original_buf_mappings table<number, table<string, table>> The mapping list, indexed first by buffer number, then by lhs, giving the maparg() dictionary value of the mapping
---@field signature_content SignatureContent The signature content

0 comments on commit 5e96dee

Please sign in to comment.