-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(keymaps): add mappings for easier navigation
To make it easier to navigate the buffer and manipulate its contents, this commit adds a set of default keymaps, which provide basic navigation: - <c-i>, sets the cursor to the start - <c-a>, sets the cursor to the end - <c-e>, sets the cursor to the word end - <c-b>, sets the cursor to the word start - <c-c>, clears the line - <c-u>, undo changes - <c-r>, redo undone changes Closes: GH-3 Signed-off-by: Filip Dutescu <filip.dutescu@gmail.com>
- Loading branch information
1 parent
e1be48f
commit 2eae961
Showing
12 changed files
with
514 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
local utils = require 'renamer.mappings.utils' | ||
|
||
--- @class Mappings | ||
--- @field public bindings table | ||
--- @field public keymap_opts table | ||
local mappings = { | ||
bindings = { | ||
['<c-i>'] = utils.set_cursor_to_start, | ||
['<c-a>'] = utils.set_cursor_to_end, | ||
['<c-e>'] = utils.set_cursor_to_word_end, | ||
['<c-b>'] = utils.set_cursor_to_word_start, | ||
['<c-c>'] = utils.clear_line, | ||
['<c-u>'] = utils.undo, | ||
['<c-r>'] = utils.redo, | ||
}, | ||
keymap_opts = { | ||
noremap = true, | ||
silent = true, | ||
}, | ||
} | ||
|
||
mappings.register_bindings = function(buf_id) | ||
if buf_id and mappings.bindings then | ||
for binding, _ in pairs(mappings.bindings) do | ||
local action = string.format( | ||
'<cmd>lua require("renamer.mappings").exec_keymap_action("%s")<cr>', | ||
binding:gsub('<', '<lt>') | ||
) | ||
vim.api.nvim_buf_set_keymap(buf_id, 'i', binding, action, mappings.keymap_opts) | ||
end | ||
end | ||
end | ||
|
||
mappings.exec_keymap_action = function(binding) | ||
if binding and mappings.bindings and mappings.bindings[binding] then | ||
mappings.bindings[binding]() | ||
end | ||
end | ||
|
||
return mappings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
local utils = {} | ||
|
||
utils.exec_in_normal = function(callback, opts, ...) | ||
vim.cmd [[stopinsert]] | ||
callback(...) | ||
|
||
if opts and opts.keep_insert then | ||
vim.api.nvim_feedkeys('i', 'n', true) | ||
end | ||
end | ||
|
||
utils.set_cursor_to_end = function() | ||
utils.exec_in_normal(vim.api.nvim_feedkeys, {}, 'A', 'n', true) | ||
end | ||
|
||
utils.set_cursor_to_start = function() | ||
utils.exec_in_normal(vim.api.nvim_feedkeys, {}, 'I', 'n', true) | ||
end | ||
|
||
utils.set_cursor_to_word_end = function() | ||
utils.exec_in_normal(vim.api.nvim_feedkeys, { keep_insert = true }, 'e', 'n', true) | ||
end | ||
|
||
utils.set_cursor_to_word_start = function() | ||
utils.exec_in_normal(vim.api.nvim_feedkeys, { keep_insert = true }, 'b', 'n', true) | ||
end | ||
|
||
utils.clear_line = function() | ||
utils.exec_in_normal(vim.api.nvim_feedkeys, { keep_insert = true }, '0C', 'n', true) | ||
end | ||
|
||
utils.undo = function() | ||
utils.exec_in_normal(vim.api.nvim_feedkeys, { keep_insert = true }, 'u', 't', true) | ||
end | ||
|
||
utils.redo = function() | ||
utils.exec_in_normal(function() | ||
local key = vim.api.nvim_replace_termcodes('<c-r>', true, false, true) | ||
vim.api.nvim_feedkeys(key, 't', true) | ||
end, { | ||
keep_insert = true, | ||
}) | ||
end | ||
|
||
return utils |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
local mappings = require 'renamer.mappings' | ||
|
||
local eq = assert.are.same | ||
|
||
describe('mappings', function() | ||
it('should have actions initialized for all `bindings`', function() | ||
local bindings = mappings.bindings | ||
|
||
for key, value in pairs(bindings) do | ||
assert(value, string.format('No action mapped to "%s".', key)) | ||
end | ||
end) | ||
|
||
it('should have `keymap_opts` initialized', function() | ||
local expected_opts = { noremap = true, silent = true } | ||
local keymap_opts = mappings.keymap_opts | ||
|
||
eq(expected_opts, keymap_opts) | ||
end) | ||
end) |
Oops, something went wrong.