Skip to content

Commit ddd80af

Browse files
kyazdani42jim-fx
authored andcommitted
chore: refacto view setup and simplify the code
1 parent e19eb5c commit ddd80af

File tree

4 files changed

+211
-256
lines changed

4 files changed

+211
-256
lines changed

README.md

Lines changed: 64 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,26 @@ require'nvim-tree'.setup {
7171
-- the command arguments as a list
7272
args = {}
7373
},
74+
75+
view = {
76+
-- width of the window, can be either a number (columns) or a string in `%`
77+
width = 30,
78+
-- side of the tree, can be one of 'left' | 'right' | 'top' | 'bottom'
79+
side = 'left',
80+
-- if true the tree will resize itself after opening a file
81+
auto_resize = false,
82+
mappings = {
83+
-- custom only false will merge the list with the default mappings
84+
-- if true, it will only use your list to set the mappings
85+
custom_only = false,
86+
-- list of mappings to set on the tree manually
87+
list = {}
88+
}
89+
}
7490
}
7591
```
7692

7793
```vim
78-
let g:nvim_tree_side = 'right' "left by default
79-
let g:nvim_tree_width = 40 "30 by default, can be width_in_columns or 'width_in_percent%'
8094
let g:nvim_tree_ignore = [ '.git', 'node_modules', '.cache' ] "empty by default
8195
let g:nvim_tree_gitignore = 1 "0 by default
8296
let g:nvim_tree_quit_on_open = 1 "0 by default, closes the tree when you open a file
@@ -86,7 +100,6 @@ let g:nvim_tree_git_hl = 1 "0 by default, will enable file highlight for git att
86100
let g:nvim_tree_highlight_opened_files = 1 "0 by default, will enable folder and file icon highlight for opened files/directories.
87101
let g:nvim_tree_hide_root_folder = 1 "0 by default, will hide the current working directory on top of the tree"
88102
let g:nvim_tree_root_folder_modifier = ':~' "This is the default. See :help filename-modifiers for more options
89-
let g:nvim_tree_auto_resize = 0 "1 by default, will resize the tree to its saved width when opening a file
90103
let g:nvim_tree_add_trailing = 1 "0 by default, append a trailing slash to folder names
91104
let g:nvim_tree_group_empty = 1 " 0 by default, compact folders that only contain a single folder into one node in the file tree
92105
let g:nvim_tree_disable_window_picker = 1 "0 by default, will disable the window picker.
@@ -156,7 +169,7 @@ let g:nvim_tree_icons = {
156169
nnoremap <C-n> :NvimTreeToggle<CR>
157170
nnoremap <leader>r :NvimTreeRefresh<CR>
158171
nnoremap <leader>n :NvimTreeFindFile<CR>
159-
" NvimTreeOpen, NvimTreeClose and NvimTreeFocus are also available if you need them
172+
" NvimTreeOpen, NvimTreeClose, NvimTreeFocus and NvimTreeResize are also available if you need them
160173
161174
set termguicolors " this variable must be enabled for colors to be applied properly
162175
@@ -168,7 +181,6 @@ highlight NvimTreeFolderIcon guibg=blue
168181

169182
### Default actions
170183

171-
- move around like in any vim buffer
172184
- `<CR>` or `o` on `..` will cd in the above directory
173185
- `<C-]>` will cd in the directory under the cursor
174186
- `<BS>` will close current opened directory or parent
@@ -199,88 +211,61 @@ highlight NvimTreeFolderIcon guibg=blue
199211
- Double left click acts like `<CR>`
200212
- Double right click acts like `<C-]>`
201213

202-
### Setup
203-
204-
You can disable default mappings with
205-
206-
```vim
207-
" let g:nvim_tree_disable_keybindings=1
208-
```
209-
210-
But you won't be able to map any keys from the setup with nvim_tree_bindings. Use with caution.
214+
### Settings
211215

212-
You can use only your mappings with
213-
214-
```vim
215-
let g:nvim_tree_disable_default_keybindings = 1
216-
```
217-
218-
You can define your own keymaps with this syntax:
219-
220-
```vim
221-
lua <<EOF
222-
vim.g.nvim_tree_bindings = {
223-
{ key = {"<CR>", "o" }, cb = ":lua some_func()<cr>"}
224-
{ key = "<Tab>", mode = "v", cb = ":lua some_func()<cr>"}
225-
}
216+
The `list` option in `view.mappings.list` is a table of
217+
```lua
218+
-- key can be either a string or a table of string (lhs)
219+
-- cb is the callback that will be called
220+
-- mode is normal by default
221+
local list = {
222+
{ key = {"<CR>", "o" }, cb = ":lua some_func()<cr>", mode = "n"}
223+
}
226224
EOF
227225
```
228226

229-
Notes:
230-
231-
- `key` can be either a string or a table of strings
232-
- `mode` is `n` by default if you don't specify it
233-
- `cb` is the command that will be called when the keymap is triggered
234-
235-
If you don't use one of the options above, your keymaps will be added to the default keymaps.
236-
237-
```vim
238-
lua <<EOF
239-
local tree_cb = require'nvim-tree.config'.nvim_tree_callback
240-
-- default mappings
241-
vim.g.nvim_tree_bindings = {
242-
{ key = {"<CR>", "o", "<2-LeftMouse>"}, cb = tree_cb("edit") },
243-
{ key = {"<2-RightMouse>", "<C-]>"}, cb = tree_cb("cd") },
244-
{ key = "<C-v>", cb = tree_cb("vsplit") },
245-
{ key = "<C-x>", cb = tree_cb("split") },
246-
{ key = "<C-t>", cb = tree_cb("tabnew") },
247-
{ key = "<", cb = tree_cb("prev_sibling") },
248-
{ key = ">", cb = tree_cb("next_sibling") },
249-
{ key = "P", cb = tree_cb("parent_node") },
250-
{ key = "<BS>", cb = tree_cb("close_node") },
251-
{ key = "<S-CR>", cb = tree_cb("close_node") },
252-
{ key = "<Tab>", cb = tree_cb("preview") },
253-
{ key = "K", cb = tree_cb("first_sibling") },
254-
{ key = "J", cb = tree_cb("last_sibling") },
255-
{ key = "I", cb = tree_cb("toggle_ignored") },
256-
{ key = "H", cb = tree_cb("toggle_dotfiles") },
257-
{ key = "R", cb = tree_cb("refresh") },
258-
{ key = "a", cb = tree_cb("create") },
259-
{ key = "d", cb = tree_cb("remove") },
260-
{ key = "r", cb = tree_cb("rename") },
261-
{ key = "<C-r>", cb = tree_cb("full_rename") },
262-
{ key = "x", cb = tree_cb("cut") },
263-
{ key = "c", cb = tree_cb("copy") },
264-
{ key = "p", cb = tree_cb("paste") },
265-
{ key = "y", cb = tree_cb("copy_name") },
266-
{ key = "Y", cb = tree_cb("copy_path") },
267-
{ key = "gy", cb = tree_cb("copy_absolute_path") },
268-
{ key = "[c", cb = tree_cb("prev_git_item") },
269-
{ key = "]c", cb = tree_cb("next_git_item") },
270-
{ key = "-", cb = tree_cb("dir_up") },
271-
{ key = "s", cb = tree_cb("system_open") },
272-
{ key = "q", cb = tree_cb("close") },
273-
{ key = "g?", cb = tree_cb("toggle_help") },
274-
}
275-
EOF
227+
These are the default bindings:
228+
```lua
229+
local tree_cb = require'nvim-tree.config'.nvim_tree_callback
230+
-- default mappings
231+
local list = {
232+
{ key = {"<CR>", "o", "<2-LeftMouse>"}, cb = tree_cb("edit") },
233+
{ key = {"<2-RightMouse>", "<C-]>"}, cb = tree_cb("cd") },
234+
{ key = "<C-v>", cb = tree_cb("vsplit") },
235+
{ key = "<C-x>", cb = tree_cb("split") },
236+
{ key = "<C-t>", cb = tree_cb("tabnew") },
237+
{ key = "<", cb = tree_cb("prev_sibling") },
238+
{ key = ">", cb = tree_cb("next_sibling") },
239+
{ key = "P", cb = tree_cb("parent_node") },
240+
{ key = "<BS>", cb = tree_cb("close_node") },
241+
{ key = "<S-CR>", cb = tree_cb("close_node") },
242+
{ key = "<Tab>", cb = tree_cb("preview") },
243+
{ key = "K", cb = tree_cb("first_sibling") },
244+
{ key = "J", cb = tree_cb("last_sibling") },
245+
{ key = "I", cb = tree_cb("toggle_ignored") },
246+
{ key = "H", cb = tree_cb("toggle_dotfiles") },
247+
{ key = "R", cb = tree_cb("refresh") },
248+
{ key = "a", cb = tree_cb("create") },
249+
{ key = "d", cb = tree_cb("remove") },
250+
{ key = "r", cb = tree_cb("rename") },
251+
{ key = "<C-r>", cb = tree_cb("full_rename") },
252+
{ key = "x", cb = tree_cb("cut") },
253+
{ key = "c", cb = tree_cb("copy") },
254+
{ key = "p", cb = tree_cb("paste") },
255+
{ key = "y", cb = tree_cb("copy_name") },
256+
{ key = "Y", cb = tree_cb("copy_path") },
257+
{ key = "gy", cb = tree_cb("copy_absolute_path") },
258+
{ key = "[c", cb = tree_cb("prev_git_item") },
259+
{ key = "]c", cb = tree_cb("next_git_item") },
260+
{ key = "-", cb = tree_cb("dir_up") },
261+
{ key = "s", cb = tree_cb("system_open") },
262+
{ key = "q", cb = tree_cb("close") },
263+
{ key = "g?", cb = tree_cb("toggle_help") },
264+
}
276265
```
277266

278267
You can toggle the help UI by pressing `g?`.
279268

280-
## Note
281-
282-
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.
283-
284269
## Features
285270

286271
- Open file in current buffer or in split with FzF like bindings (`<CR>`, `<C-v>`, `<C-x>`, `<C-t>`)

0 commit comments

Comments
 (0)