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
|
||||
"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 shows no icon by default
|
||||
let g:nvim_tree_icons = {
|
||||
@ -108,8 +77,10 @@ highlight NvimTreeFolderIcon guibg=blue
|
||||
|
||||
## KeyBindings
|
||||
|
||||
### Default actions
|
||||
|
||||
- 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
|
||||
- `<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.
|
||||
@ -135,6 +106,28 @@ highlight NvimTreeFolderIcon guibg=blue
|
||||
- Double left click acts like `<CR>`
|
||||
- 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
|
||||
|
||||
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*
|
||||
|
||||
you can change default keybindings by defining this variable.
|
||||
default keybindings will be applied to undefined keys.
|
||||
Default keybindings can be overriden. You can also define your own keymappings for the tree view:
|
||||
>
|
||||
let g:nvim_tree_bindings = {
|
||||
\ edit: ['<cr>', 'o'], // Multiple keys provided via list
|
||||
\ edit_vsplit: '<c-v>',
|
||||
\ edit_split: '<c-x>',
|
||||
\ edit_tab: '<c-t>',
|
||||
\ close_node: ['<s-cr>', '<bs>'],
|
||||
\ cd: '<c-]>',
|
||||
\ preview: '<Tab>',
|
||||
\ create: 'a',
|
||||
\ remove: 'd',
|
||||
\ rename: 'r',
|
||||
\ cut: 'x',
|
||||
\ copy: 'c',
|
||||
\ paste: 'p',
|
||||
\ prev_git_item: '[c',
|
||||
\ next_git_item: ']c',
|
||||
\ dir_up: '-',
|
||||
\ }
|
||||
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`.
|
||||
|
||||
|Features| *nvim-tree-features*
|
||||
|
||||
|
||||
@ -51,31 +51,39 @@ function M.get_icon_state()
|
||||
}
|
||||
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()
|
||||
local keybindings = vim.g.nvim_tree_bindings or {}
|
||||
return {
|
||||
edit = keybindings.edit or {'<CR>', 'o'},
|
||||
edit_vsplit = keybindings.edit_vsplit or '<C-v>',
|
||||
edit_split = keybindings.edit_split or '<C-x>',
|
||||
edit_tab = keybindings.edit_tab or '<C-t>',
|
||||
close_node = keybindings.close_node or {'<S-CR>', '<BS>'},
|
||||
preview = keybindings.preview or '<Tab>',
|
||||
toggle_ignored = keybindings.toggle_ignored or 'I',
|
||||
toggle_dotfiles = keybindings.toggle_dotfiles or 'H',
|
||||
refresh = keybindings.refresh or 'R',
|
||||
cd = keybindings.cd or '<C-]>',
|
||||
create = keybindings.create or 'a',
|
||||
remove = keybindings.remove or 'd',
|
||||
rename = keybindings.rename or 'r',
|
||||
full_rename = keybindings.full_rename or '<C-r>',
|
||||
cut = keybindings.cut or 'x',
|
||||
copy = keybindings.copy or 'c',
|
||||
paste = keybindings.paste or 'p',
|
||||
prev_git_item = keybindings.prev_git_item or '[c',
|
||||
next_git_item = keybindings.next_git_item or ']c',
|
||||
dir_up = keybindings.dir_up or '-',
|
||||
close = keybindings.close or 'q',
|
||||
}
|
||||
return vim.tbl_extend('force', {
|
||||
["<CR>"] = get_lua_cb("edit"),
|
||||
["o"] = get_lua_cb("edit"),
|
||||
["<2-LeftMouse>"] = get_lua_cb("edit"),
|
||||
["<2-RightMouse>"] = get_lua_cb("cd"),
|
||||
["<C-]>"] = get_lua_cb("cd"),
|
||||
["<C-v>"] = get_lua_cb("vsplit"),
|
||||
["<C-x>"] = get_lua_cb("split"),
|
||||
["<C-t>"] = get_lua_cb("tabnew"),
|
||||
["<BS>"] = get_lua_cb("close_node"),
|
||||
["<S-CR>"] = get_lua_cb("close_node"),
|
||||
["<Tab>"] = get_lua_cb("preview"),
|
||||
["I"] = get_lua_cb("toggle_ignored"),
|
||||
["H"] = get_lua_cb("toggle_dotfiles"),
|
||||
["R"] = get_lua_cb("refresh"),
|
||||
["a"] = get_lua_cb("create"),
|
||||
["d"] = get_lua_cb("remove"),
|
||||
["r"] = get_lua_cb("rename"),
|
||||
["<C-r>"] = get_lua_cb("full_rename"),
|
||||
["x"] = get_lua_cb("cut"),
|
||||
["c"] = get_lua_cb("copy"),
|
||||
["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
|
||||
|
||||
function M.window_options()
|
||||
|
||||
@ -211,9 +211,9 @@ function M.open_file(mode, filename)
|
||||
end
|
||||
|
||||
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
|
||||
ecmd()
|
||||
ecmd()
|
||||
end
|
||||
|
||||
if mode == 'preview' then
|
||||
@ -243,54 +243,22 @@ function M.change_dir(foldername)
|
||||
M.init(false, M.Tree.bufnr ~= nil)
|
||||
end
|
||||
|
||||
local function set_mapping(buf, key, fn)
|
||||
api.nvim_buf_set_keymap(buf, 'n', key, ':lua require"nvim-tree".'..fn..'<cr>', {
|
||||
nowait = true, noremap = true, silent = true
|
||||
})
|
||||
local function set_mapping(buf, key, cb)
|
||||
api.nvim_buf_set_keymap(buf, 'n', key, cb, {
|
||||
nowait = true, noremap = true, silent = true
|
||||
})
|
||||
end
|
||||
|
||||
local function set_mappings()
|
||||
if vim.g.nvim_tree_disable_keybindings == 1 then
|
||||
return
|
||||
return
|
||||
end
|
||||
|
||||
local buf = M.Tree.bufnr
|
||||
local bindings = config.get_bindings()
|
||||
|
||||
local mappings = {
|
||||
['<2-LeftMouse>'] = 'on_keypress("edit")';
|
||||
['<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
|
||||
for key,cb in pairs(bindings) do
|
||||
set_mapping(buf, key, cb)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user