chore: refacto binding initialization (inverse mapping table)
This commit is contained in:
parent
491fd68d62
commit
905afba209
57
README.md
57
README.md
@ -43,37 +43,6 @@ let g:nvim_tree_show_icons = {
|
|||||||
"1 by default, notice that if 'files' is 1, it will only display
|
"1 by default, notice that if 'files' is 1, it will only display
|
||||||
"if nvim-web-devicons is installed and on your runtimepath
|
"if nvim-web-devicons is installed and on your runtimepath
|
||||||
|
|
||||||
" You can edit keybindings be defining this variable
|
|
||||||
" You don't have to define all keys.
|
|
||||||
" NOTE: the 'edit' key will wrap/unwrap a folder and open a file
|
|
||||||
let g:nvim_tree_bindings = {
|
|
||||||
\ 'edit': ['<CR>', 'o'],
|
|
||||||
\ 'edit_vsplit': '<C-v>',
|
|
||||||
\ 'edit_split': '<C-x>',
|
|
||||||
\ 'edit_tab': '<C-t>',
|
|
||||||
\ 'close_node': ['<S-CR>', '<BS>'],
|
|
||||||
\ 'toggle_ignored': 'I',
|
|
||||||
\ 'toggle_dotfiles': 'H',
|
|
||||||
\ 'refresh': 'R',
|
|
||||||
\ 'preview': '<Tab>',
|
|
||||||
\ 'cd': '<C-]>',
|
|
||||||
\ 'create': 'a',
|
|
||||||
\ 'remove': 'd',
|
|
||||||
\ 'rename': 'r',
|
|
||||||
\ 'full_rename': '<C-r>',
|
|
||||||
\ 'cut': 'x',
|
|
||||||
\ 'copy': 'c',
|
|
||||||
\ 'paste': 'p',
|
|
||||||
\ 'prev_git_item': '[c',
|
|
||||||
\ 'next_git_item': ']c',
|
|
||||||
\ 'dir_up': '-',
|
|
||||||
\ 'close': 'q',
|
|
||||||
\ }
|
|
||||||
|
|
||||||
" Disable default mappings by plugin
|
|
||||||
" Bindings are enable by default, disabled on any non-zero value
|
|
||||||
" let nvim_tree_disable_keybindings=1
|
|
||||||
|
|
||||||
" default will show icon by default if no icon is provided
|
" default will show icon by default if no icon is provided
|
||||||
" default shows no icon by default
|
" default shows no icon by default
|
||||||
let g:nvim_tree_icons = {
|
let g:nvim_tree_icons = {
|
||||||
@ -108,8 +77,10 @@ highlight NvimTreeFolderIcon guibg=blue
|
|||||||
|
|
||||||
## KeyBindings
|
## KeyBindings
|
||||||
|
|
||||||
|
### Default actions
|
||||||
|
|
||||||
- move around like in any vim buffer
|
- move around like in any vim buffer
|
||||||
- `<CR>` on `..` will cd in the above directory
|
- `<CR>` or `o` on `..` will cd in the above directory
|
||||||
- `<C-]>` will cd in the directory under the cursor
|
- `<C-]>` will cd in the directory under the cursor
|
||||||
- `<BS>` will close current opened directory or parent
|
- `<BS>` will close current opened directory or parent
|
||||||
- type `a` to add a file. Adding a directory requires leaving a leading `/` at the end of the path.
|
- type `a` to add a file. Adding a directory requires leaving a leading `/` at the end of the path.
|
||||||
@ -135,6 +106,28 @@ highlight NvimTreeFolderIcon guibg=blue
|
|||||||
- Double left click acts like `<CR>`
|
- Double left click acts like `<CR>`
|
||||||
- Double right click acts like `<C-]>`
|
- Double right click acts like `<C-]>`
|
||||||
|
|
||||||
|
### Setup
|
||||||
|
|
||||||
|
You can disable default mappings with
|
||||||
|
```vim
|
||||||
|
" let nvim_tree_disable_keybindings=1
|
||||||
|
```
|
||||||
|
But you won't be able to map any keys from the setup with nvim_tree_bindings. Use with caution.
|
||||||
|
|
||||||
|
|
||||||
|
Default keybindings can be overriden. You can also define your own keymappings for the tree view:
|
||||||
|
```vim
|
||||||
|
lua <<EOF
|
||||||
|
vim.g.nvim_tree_bindings = {
|
||||||
|
["<CR>"] = ":YourVimFunction()<cr>",
|
||||||
|
["u"] = ":lua require'some_module'.some_function()<cr>",
|
||||||
|
}
|
||||||
|
EOF
|
||||||
|
```
|
||||||
|
You can check the default binding table in `nvim-tree/config.lua`, under the `get_bindings` function.
|
||||||
|
This basically maps the default keys to the `on_keypress` function with different string parameters.
|
||||||
|
All mappings are set in `normal mode`.
|
||||||
|
|
||||||
## Note
|
## Note
|
||||||
|
|
||||||
This plugin is very fast because it uses the `libuv` `scandir` and `scandir_next` functions instead of spawning an `ls` process which can get slow on large files when combining with `stat` to get file informations.
|
This plugin is very fast because it uses the `libuv` `scandir` and `scandir_next` functions instead of spawning an `ls` process which can get slow on large files when combining with `stat` to get file informations.
|
||||||
|
|||||||
@ -236,27 +236,18 @@ INFORMATIONS *nvim-tree-info*
|
|||||||
|
|
||||||
|g:nvim_tree_bindings| *g:nvim_tree_bindings*
|
|g:nvim_tree_bindings| *g:nvim_tree_bindings*
|
||||||
|
|
||||||
you can change default keybindings by defining this variable.
|
Default keybindings can be overriden. You can also define your own keymappings for the tree view:
|
||||||
default keybindings will be applied to undefined keys.
|
|
||||||
>
|
>
|
||||||
let g:nvim_tree_bindings = {
|
lua <<EOF
|
||||||
\ edit: ['<cr>', 'o'], // Multiple keys provided via list
|
vim.g.nvim_tree_bindings = {
|
||||||
\ edit_vsplit: '<c-v>',
|
["<CR>"] = ":YourVimFunction()<cr>",
|
||||||
\ edit_split: '<c-x>',
|
["u"] = ":lua require'some_module'.some_function()<cr>",
|
||||||
\ edit_tab: '<c-t>',
|
}
|
||||||
\ close_node: ['<s-cr>', '<bs>'],
|
EOF
|
||||||
\ cd: '<c-]>',
|
|
||||||
\ preview: '<Tab>',
|
You can check the default binding table in `nvim-tree/config.lua`, under the `get_bindings` function.
|
||||||
\ create: 'a',
|
This basically maps the default keys to the `on_keypress` function with different string parameters.
|
||||||
\ remove: 'd',
|
All mappings are set in `normal` `mode`.
|
||||||
\ rename: 'r',
|
|
||||||
\ cut: 'x',
|
|
||||||
\ copy: 'c',
|
|
||||||
\ paste: 'p',
|
|
||||||
\ prev_git_item: '[c',
|
|
||||||
\ next_git_item: ']c',
|
|
||||||
\ dir_up: '-',
|
|
||||||
\ }
|
|
||||||
|
|
||||||
|Features| *nvim-tree-features*
|
|Features| *nvim-tree-features*
|
||||||
|
|
||||||
|
|||||||
@ -51,31 +51,39 @@ function M.get_icon_state()
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function get_lua_cb(cb_name)
|
||||||
|
return string.format(":lua require'nvim-tree'.on_keypress('%s')<CR>", cb_name)
|
||||||
|
end
|
||||||
|
|
||||||
function M.get_bindings()
|
function M.get_bindings()
|
||||||
local keybindings = vim.g.nvim_tree_bindings or {}
|
local keybindings = vim.g.nvim_tree_bindings or {}
|
||||||
return {
|
return vim.tbl_extend('force', {
|
||||||
edit = keybindings.edit or {'<CR>', 'o'},
|
["<CR>"] = get_lua_cb("edit"),
|
||||||
edit_vsplit = keybindings.edit_vsplit or '<C-v>',
|
["o"] = get_lua_cb("edit"),
|
||||||
edit_split = keybindings.edit_split or '<C-x>',
|
["<2-LeftMouse>"] = get_lua_cb("edit"),
|
||||||
edit_tab = keybindings.edit_tab or '<C-t>',
|
["<2-RightMouse>"] = get_lua_cb("cd"),
|
||||||
close_node = keybindings.close_node or {'<S-CR>', '<BS>'},
|
["<C-]>"] = get_lua_cb("cd"),
|
||||||
preview = keybindings.preview or '<Tab>',
|
["<C-v>"] = get_lua_cb("vsplit"),
|
||||||
toggle_ignored = keybindings.toggle_ignored or 'I',
|
["<C-x>"] = get_lua_cb("split"),
|
||||||
toggle_dotfiles = keybindings.toggle_dotfiles or 'H',
|
["<C-t>"] = get_lua_cb("tabnew"),
|
||||||
refresh = keybindings.refresh or 'R',
|
["<BS>"] = get_lua_cb("close_node"),
|
||||||
cd = keybindings.cd or '<C-]>',
|
["<S-CR>"] = get_lua_cb("close_node"),
|
||||||
create = keybindings.create or 'a',
|
["<Tab>"] = get_lua_cb("preview"),
|
||||||
remove = keybindings.remove or 'd',
|
["I"] = get_lua_cb("toggle_ignored"),
|
||||||
rename = keybindings.rename or 'r',
|
["H"] = get_lua_cb("toggle_dotfiles"),
|
||||||
full_rename = keybindings.full_rename or '<C-r>',
|
["R"] = get_lua_cb("refresh"),
|
||||||
cut = keybindings.cut or 'x',
|
["a"] = get_lua_cb("create"),
|
||||||
copy = keybindings.copy or 'c',
|
["d"] = get_lua_cb("remove"),
|
||||||
paste = keybindings.paste or 'p',
|
["r"] = get_lua_cb("rename"),
|
||||||
prev_git_item = keybindings.prev_git_item or '[c',
|
["<C-r>"] = get_lua_cb("full_rename"),
|
||||||
next_git_item = keybindings.next_git_item or ']c',
|
["x"] = get_lua_cb("cut"),
|
||||||
dir_up = keybindings.dir_up or '-',
|
["c"] = get_lua_cb("copy"),
|
||||||
close = keybindings.close or 'q',
|
["p"] = get_lua_cb("paste"),
|
||||||
}
|
["[c"] = get_lua_cb("prev_git_item"),
|
||||||
|
["]c"] = get_lua_cb("next_git_item"),
|
||||||
|
["-"] = get_lua_cb("dir_up"),
|
||||||
|
["q"] = get_lua_cb("close"),
|
||||||
|
}, keybindings)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.window_options()
|
function M.window_options()
|
||||||
|
|||||||
@ -211,9 +211,9 @@ function M.open_file(mode, filename)
|
|||||||
end
|
end
|
||||||
|
|
||||||
if type(ecmd) == 'string' then
|
if type(ecmd) == 'string' then
|
||||||
api.nvim_command(string.format('%s %s', ecmd, vim.fn.fnameescape(filename)))
|
api.nvim_command(string.format('%s %s', ecmd, vim.fn.fnameescape(filename)))
|
||||||
else
|
else
|
||||||
ecmd()
|
ecmd()
|
||||||
end
|
end
|
||||||
|
|
||||||
if mode == 'preview' then
|
if mode == 'preview' then
|
||||||
@ -243,54 +243,22 @@ function M.change_dir(foldername)
|
|||||||
M.init(false, M.Tree.bufnr ~= nil)
|
M.init(false, M.Tree.bufnr ~= nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function set_mapping(buf, key, fn)
|
local function set_mapping(buf, key, cb)
|
||||||
api.nvim_buf_set_keymap(buf, 'n', key, ':lua require"nvim-tree".'..fn..'<cr>', {
|
api.nvim_buf_set_keymap(buf, 'n', key, cb, {
|
||||||
nowait = true, noremap = true, silent = true
|
nowait = true, noremap = true, silent = true
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
local function set_mappings()
|
local function set_mappings()
|
||||||
if vim.g.nvim_tree_disable_keybindings == 1 then
|
if vim.g.nvim_tree_disable_keybindings == 1 then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local buf = M.Tree.bufnr
|
local buf = M.Tree.bufnr
|
||||||
local bindings = config.get_bindings()
|
local bindings = config.get_bindings()
|
||||||
|
|
||||||
local mappings = {
|
for key,cb in pairs(bindings) do
|
||||||
['<2-LeftMouse>'] = 'on_keypress("edit")';
|
set_mapping(buf, key, cb)
|
||||||
['<2-RightMouse>'] = 'on_keypress("cd")';
|
|
||||||
[bindings.cd] = 'on_keypress("cd")';
|
|
||||||
[bindings.edit] = 'on_keypress("edit")';
|
|
||||||
[bindings.edit_vsplit] = 'on_keypress("vsplit")';
|
|
||||||
[bindings.edit_split] = 'on_keypress("split")';
|
|
||||||
[bindings.edit_tab] = 'on_keypress("tabnew")';
|
|
||||||
[bindings.close_node] = 'on_keypress("close_node")';
|
|
||||||
[bindings.toggle_ignored] = 'on_keypress("toggle_ignored")';
|
|
||||||
[bindings.toggle_dotfiles] = 'on_keypress("toggle_dotfiles")';
|
|
||||||
[bindings.refresh] = 'on_keypress("refresh")';
|
|
||||||
[bindings.create] = 'on_keypress("create")';
|
|
||||||
[bindings.remove] = 'on_keypress("remove")';
|
|
||||||
[bindings.rename] = 'on_keypress("rename")';
|
|
||||||
[bindings.full_rename] = 'on_keypress("full_rename")';
|
|
||||||
[bindings.preview] = 'on_keypress("preview")';
|
|
||||||
[bindings.cut] = 'on_keypress("cut")';
|
|
||||||
[bindings.copy] = 'on_keypress("copy")';
|
|
||||||
[bindings.paste] = 'on_keypress("paste")';
|
|
||||||
[bindings.prev_git_item] = 'on_keypress("prev_git_item")';
|
|
||||||
[bindings.next_git_item] = 'on_keypress("next_git_item")';
|
|
||||||
[bindings.dir_up] = 'on_keypress("dir_up")';
|
|
||||||
[bindings.close] = 'on_keypress("close")';
|
|
||||||
}
|
|
||||||
|
|
||||||
for k,v in pairs(mappings) do
|
|
||||||
if type(k) == 'table' then
|
|
||||||
for _, key in pairs(k) do
|
|
||||||
set_mapping(buf, key, v)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
set_mapping(buf, k, v)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user