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

refactor: rewrite to use mappings (for most modes) #59

Merged
merged 11 commits into from
Jul 4, 2024
Prev Previous commit
Next Next commit
cleanup
  • Loading branch information
max397574 committed Jun 9, 2024
commit bc06b24d63bc59868e1bfd1e25de655b22e6010d
40 changes: 4 additions & 36 deletions lua/better_escape.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ local uv = vim.uv

local settings = {
timeout = vim.o.timeoutlen,
-- deprecated
-- mapping = nil,
mappings = {
i = {
j = {
Expand All @@ -27,27 +25,14 @@ local settings = {
v = {
j = {
k = "<Esc>",
-- WIP: is this a good idea for visual mode?
-- j = "<Esc>",
},
},
s = {
j = {
k = "<Esc>",
},
},
n = {
j = {
k = "<Esc>",
},
},
},
-- WIP: still not added in the pr,
-- i feel like this option should be removed and a method to get the behaviour with the old should be added to the readme
clear_empty_lines = false,
-- deprecated
---@type string|function
-- keys = "<Esc>",
}

local function clear_mappings()
Expand Down Expand Up @@ -80,7 +65,7 @@ end

vim.on_key(function()
if recorded_key then
recorded_key = false
recorded_key = false
return
end
-- make sure to only store a sequence when a key is actually pressed
Expand All @@ -93,26 +78,13 @@ local undo_key = {
c = "<bs>",
t = "<bs>",
v = "",
n = "",
s = "",
o = "",
}
local parent_keys = {}
local function map_keys()
parent_keys = {}
for mode, keys in pairs(settings.mappings) do
local map_opts = { expr = true }
if mode == "o" then
-- WIP: got any ideas?
-- i can't just map keys because you can't easily undo every motion i think
-- and you can't press 2 operators at the same time you would have to leave operator pending mode
vim.notify(
"[better-escape.nvim]: operator-pending mode is not supported yet as it needs discussion",
vim.log.levels.WARN,
{}
)
goto continue
end
local map_opts = { expr = true, nowait = true }
for key, subkeys in pairs(keys) do
vim.keymap.set(mode, key, function()
log_key(key)
Expand Down Expand Up @@ -144,16 +116,12 @@ local function map_keys()
)
if type(mapping) == "string" then
vim.api.nvim_input(mapping)
else
local user_keys = mapping()
if type(user_keys) == "string" then
vim.api.nvim_input(user_keys)
end
elseif type(mapping) == "function" then
mapping()
end
end, map_opts)
end
end
::continue::
end
end

Expand Down