Skip to content

Commit da6fcd2

Browse files
alex-courtisbaahrenschomosukegegounerishabhjain9191
authored
on_attach is default or user only, legacy and generation includes defaults (#1777)
* chore(mappings): remove mappings via vim.keymap.del instead of filtering mappings, to allow for multiple ways of specifying a key * doc: specify that the terminal emulator must be configured to use the patched font * feat(renderer): add NvimTreeOpenedFolderIcon NvimTreeClosedFolderIcon (#1768) * feat: Add highlight group for opened folder closes #1674 * docs: Add NvimTreeOpenedFolderIcon default * feat: Add NvimTreeClosedFolderIcon highlight group Defaults to NvimTreeFolderIcon * feat: add diagnostics.show_on_open_dirs git.show_on_open_dirs (#1778) * feat(diagnostics): only show diagnostic on closed folder * feat(git): only show git icon on closed folder * docs: Update feature_request.md (#1788) * Update feature_request.md Closes #1654 * Update feature_request.md Co-authored-by: Alexander Courtis <[email protected]> * 1786 git next prev land on dirs (#1787) * Filtered dir with git status that are open when show_on_open_dir is false * refactored for single source of truth of existence of git status on a node Putting `has_git_status()` in `explorer.common` because that's where node.status is constructed Or at least I think that's where it's constructed * 1786 semantic nit Co-authored-by: Alexander Courtis <[email protected]> * fix(git): git rename not showing up for the renamed file (#1783) * fixed git rename not showing up for the renamed file * considered " -> " being a part of the filename Fixed -> pattern to escape - Fixed "\"" and "\\" in filename * using string.find(, , true) to match plain -> * Using -z and removed unnecessary logic * feat(view): always enable cursorline, users may change this behaviour via Event.TreeOpen (#1814) * Update view.lua * set cursorline to true * feat(event): dispatch Event.NodeRenamed on cut-paste (#1817) * feat(view): add filters.git_clean, filters.no_buffer (#1784) * feat(view): add filters.git_clean * feat(view): add filters.git_clean * feat(view): add filters.no_buffer * feat(view): filters.no_buffer misses unloaded, handles buffer in/out * feat(view): filters.no_buffer matches directories specifically * feat(view): filters.no_buffer clarify targets * feat: add placeholder filters.diagnostics_ok, refactor filters * feat(view): remove placeholder filters.diagnostics_ok * doc: consolidate and clarify :help examples * doc: format help * feat: paste and create always target closed folder, remove create_in_closed_folder (#1802) * Fix default for file creation in closed directories * Make paste in closed directories consistent with create * doc: clarify create_in_closed_folder * Remove create_in_closed_folder option * doc: clarify create_in_closed_folder removal message (whoops) Co-authored-by: Alexander Courtis <[email protected]> * on_attach is user's or default, nothing else; legacy generated on_attach includes defaults Co-authored-by: baahrens <[email protected]> Co-authored-by: Richard Li <[email protected]> Co-authored-by: gegoune <[email protected]> Co-authored-by: rishabhjain9191 <[email protected]> Co-authored-by: Anton <[email protected]> Co-authored-by: Eric Haynes <[email protected]>
1 parent 1978dc1 commit da6fcd2

File tree

24 files changed

+342
-205
lines changed

24 files changed

+342
-205
lines changed

.github/ISSUE_TEMPLATE/feature_request.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ assignees: ''
99
**Is this a question?**
1010
Please start a new [Q&A discussion](https://github.com/nvim-tree/nvim-tree.lua/discussions/new) instead of raising a feature request.
1111

12+
**Can this functionality be implemented utilising API?**
13+
nvim-tree exposes extensive API (see `:h nvim-tree-api`). Can it be used to achieve your goal? Is there a missing API that would make it possible?
14+
Given stable status of nvim-tree it's preferred to add new API than new functionality.
15+
1216
**Is your feature request related to a problem? Please describe.**
1317
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
1418

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Take a look at the [wiki](https://github.com/nvim-tree/nvim-tree.lua/wiki) for S
3030

3131
[neovim >=0.7.0](https://github.com/neovim/neovim/wiki/Installing-Neovim)
3232

33-
[nvim-web-devicons](https://github.com/nvim-tree/nvim-web-devicons) is optional and used to display file icons. It requires a [patched font](https://www.nerdfonts.com/).
33+
[nvim-web-devicons](https://github.com/nvim-tree/nvim-web-devicons) is optional and used to display file icons. It requires a [patched font](https://www.nerdfonts.com/). Your terminal emulator must be configured to use that font, usually "Hack Nerd Font"
3434

3535
## Install
3636

doc/nvim-tree-lua.txt

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ Features
3131

3232
File Icons
3333

34-
https://github.com/nvim-tree/nvim-web-devicons is optional and used to display file icons. It requires a patched font: https://www.nerdfonts.com
34+
https://github.com/nvim-tree/nvim-web-devicons is optional and used to display file icons.
35+
It requires a patched font: https://www.nerdfonts.com
36+
Your terminal emulator must be configured to use that font, usually "Hack Nerd Font"
3537

3638
 should look like an open folder.
3739

@@ -169,7 +171,6 @@ Subsequent calls to setup will replace the previous configuration.
169171
>
170172
require("nvim-tree").setup { -- BEGIN_DEFAULT_OPTS
171173
auto_reload_on_write = true,
172-
create_in_closed_folder = false,
173174
disable_netrw = false,
174175
hijack_cursor = false,
175176
hijack_netrw = true,
@@ -290,10 +291,11 @@ Subsequent calls to setup will replace the previous configuration.
290291
diagnostics = {
291292
enable = false,
292293
show_on_dirs = false,
294+
show_on_open_dirs = true,
293295
debounce_delay = 50,
294296
severity = {
295297
min = vim.diagnostic.severity.HINT,
296-
max = vim.diagnostic.severity.ERROR
298+
max = vim.diagnostic.severity.ERROR,
297299
},
298300
icons = {
299301
hint = "",
@@ -304,6 +306,8 @@ Subsequent calls to setup will replace the previous configuration.
304306
},
305307
filters = {
306308
dotfiles = false,
309+
git_clean = false,
310+
no_buffer = false,
307311
custom = {},
308312
exclude = {},
309313
},
@@ -316,6 +320,7 @@ Subsequent calls to setup will replace the previous configuration.
316320
enable = true,
317321
ignore = true,
318322
show_on_dirs = true,
323+
show_on_open_dirs = true,
319324
timeout = 400,
320325
},
321326
actions = {
@@ -430,11 +435,6 @@ in some scenarios (eg using vim startify).
430435
Reloads the explorer every time a buffer is written to.
431436
Type: `boolean`, Default: `true`
432437

433-
*nvim-tree.create_in_closed_folder*
434-
Creating a file when the cursor is on a closed folder will set the
435-
path to be inside the closed folder, otherwise the parent folder.
436-
Type: `boolean`, Default: `false`
437-
438438
*nvim-tree.sort_by*
439439
Changes how files within the same directory are sorted.
440440
Can be one of `name`, `case_sensitive`, `modification_time`, `extension` or a
@@ -555,6 +555,11 @@ Show LSP and COC diagnostics in the signcolumn
555555
Show diagnostic icons on parent directories.
556556
Type: `boolean`, Default: `false`
557557

558+
*nvim-tree.diagnostics.show_on_open_dirs*
559+
Show diagnostics icons on directories that are open.
560+
Only relevant when `diagnostics.show_on_dirs` is `true`.
561+
Type: `boolean`, Default: `true`
562+
558563
*nvim-tree.diagnostics.icons*
559564
Icons for diagnostic severity.
560565
Type: `table`, Default: `{ hint = "", info = "", warning = "", error = "" }`
@@ -586,6 +591,11 @@ Git integration with icons and colors.
586591
Show status icons of children when directory itself has no status icon.
587592
Type: `boolean`, Default: `true`
588593

594+
*nvim-tree.git.show_on_open_dirs*
595+
Show status icons on directories that are open.
596+
Only relevant when `git.show_on_dirs` is `true`.
597+
Type: `boolean`, Default: `true`
598+
589599
*nvim-tree.git.timeout*
590600
Kills the git process after some time if it takes too long.
591601
Type: `number`, Default: `400` (ms)
@@ -627,16 +637,14 @@ Function ran when creating the nvim-tree buffer.
627637
This can be used to attach keybindings to the tree buffer.
628638
When on_attach is not a function, |nvim-tree.view.mappings| will be used.
629639
Type: `function(bufnr)`, Default: `"disable"`
640+
e.g. >
641+
local api = require('nvim-tree.api')
630642
631-
Example: >
632-
local Api = require('nvim-tree.api')
633-
local Lib = require('nvim-tree.lib')
634-
635-
local my_on_attach = function(bufnr)
636-
vim.keymap.set('n', '?', Api.tree.toggle_help, { buffer = bufnr, noremap = true, silent = true, nowait = true, desc = 'Help' })
637-
vim.keymap.set('n', 'h', Api.tree.toggle_help, { buffer = bufnr, noremap = true, silent = true, nowait = true, desc = 'Help' })
643+
local function on_attach(bufnr)
644+
vim.keymap.set('n', '?', api.tree.toggle_help, { buffer = bufnr, noremap = true, silent = true, nowait = true, desc = 'Help' })
645+
vim.keymap.set('n', 'h', api.tree.toggle_help, { buffer = bufnr, noremap = true, silent = true, nowait = true, desc = 'Help' })
638646
vim.keymap.set("n", "<C-P>", function()
639-
local node = Lib.get_node_at_cursor()
647+
local node = api.tree.get_node_under_cursor()
640648
print(node.absolute_path)
641649
end, { buffer = bufnr, noremap = true, silent = true, nowait = true, desc = "my description" })
642650
end
@@ -704,6 +712,7 @@ Window / buffer setup.
704712
*nvim-tree.view.mappings.custom_only*
705713
Will use only the provided user mappings and not the default otherwise,
706714
extends the default mappings with the provided user mappings.
715+
Overrides |nvim-tree.remove_keymaps|
707716
Type: `boolean`, Default: `false`
708717

709718
*nvim-tree.view.mappings.list*
@@ -888,6 +897,19 @@ Filtering options.
888897
Toggle via the `toggle_dotfiles` action, default mapping `H`.
889898
Type: `boolean`, Default: `false`
890899

900+
*nvim-tree.filters.git_clean*
901+
Do not show files with no git status. This will show ignored files when
902+
|nvim-tree.git.ignore| is set, as they are effectively dirty.
903+
Toggle via the `toggle_git_clean` action, default mapping `C`.
904+
Type: `boolean`, Default: `false`
905+
906+
*nvim-tree.filters.no_buffer*
907+
Do not show files that have no listed buffer.
908+
Toggle via the `toggle_no_buffer` action, default mapping `B`.
909+
For performance reasons this may not immediately update on buffer
910+
delete/wipe. A reload or filesystem event will result in an update.
911+
Type: `boolean`, Default: `false`
912+
891913
*nvim-tree.filters.custom*
892914
Custom list of vim regex for file/directory names that will not be shown.
893915
Backslashes must be escaped e.g. "^\\.git". See |string-match|.
@@ -1142,15 +1164,15 @@ A good functionality to enable is |nvim-tree.hijack_directories|.
11421164

11431165
Nvim-tree's public API can be used to access features.
11441166
>
1145-
local nt_api = require("nvim-tree.api")
1146-
1147-
nt_api.tree.toggle()
1167+
e.g. >
1168+
local api = require("nvim-tree.api")
1169+
api.tree.toggle()
11481170
<
11491171
This module exposes stable functionalities, it is advised to use this in order
11501172
to avoid breaking configurations due to internal breaking changes.
11511173

11521174
The api is separated in multiple modules, which can be accessed with
1153-
`require("nvim-tree.api").moduleName.functionality`.
1175+
`api.<module>.<function>`
11541176

11551177
Functions that needs a tree node parameter are exposed with an abstraction
11561178
that injects the node from the cursor position in the tree when calling
@@ -1173,6 +1195,8 @@ exists.
11731195
- collapse_all `(keep_buffers?: bool)`
11741196
- expand_all
11751197
- toggle_gitignore_filter
1198+
- toggle_git_clean_filter
1199+
- toggle_no_buffer_filter
11761200
- toggle_custom_filter
11771201
- toggle_hidden_filter
11781202
- toggle_help
@@ -1294,7 +1318,7 @@ DEFAULT MAPPINGS *nvim-tree-default-mappings
12941318
`O` Open: No Window Picker Open file with no window picker.
12951319
`<C-]>` CD cd in the directory under the cursor.
12961320
`<2-RightMouse>`
1297-
`<ctrL-v>` Open: Vertical Split Open file in a vertical split.
1321+
`<C-v>` Open: Vertical Split Open file in a vertical split.
12981322
`<C-x>` Open: Horizontal Split Open file in a horizontal split.
12991323
`<C-t>` Open: New Tab Open file in a new tab.
13001324
`<` Previous Sibling Navigate to the previous sibling.
@@ -1348,7 +1372,7 @@ DEFAULT MAPPINGS *nvim-tree-default-mappings
13481372
vim.keymap.set('n', 'O', Api.node.open.no_window_picker, { buffer = bufnr, noremap = true, silent = true, nowait = true, desc = 'Open: No Window Picker', })
13491373
vim.keymap.set('n', '<C-]>', Api.tree.change_root_to_node, { buffer = bufnr, noremap = true, silent = true, nowait = true, desc = 'CD', })
13501374
vim.keymap.set('n', '<2-RightMouse>', Api.tree.change_root_to_node, { buffer = bufnr, noremap = true, silent = true, nowait = true, desc = 'CD', })
1351-
vim.keymap.set('n', '<ctrL-v>', Api.node.open.vertical, { buffer = bufnr, noremap = true, silent = true, nowait = true, desc = 'Open: Vertical Split', })
1375+
vim.keymap.set('n', '<C-v>', Api.node.open.vertical, { buffer = bufnr, noremap = true, silent = true, nowait = true, desc = 'Open: Vertical Split', })
13521376
vim.keymap.set('n', '<C-x>', Api.node.open.horizontal, { buffer = bufnr, noremap = true, silent = true, nowait = true, desc = 'Open: Horizontal Split', })
13531377
vim.keymap.set('n', '<C-t>', Api.node.open.tab, { buffer = bufnr, noremap = true, silent = true, nowait = true, desc = 'Open: New Tab', })
13541378
vim.keymap.set('n', '<', Api.node.navigate.sibling.prev, { buffer = bufnr, noremap = true, silent = true, nowait = true, desc = 'Previous Sibling', })
@@ -1416,6 +1440,8 @@ NvimTreeSymlink
14161440
NvimTreeFolderName (Directory)
14171441
NvimTreeRootFolder
14181442
NvimTreeFolderIcon
1443+
NvimTreeOpenedFolderIcon (NvimTreeFolderIcon)
1444+
NvimTreeClosedFolderIcon (NvimTreeFolderIcon)
14191445
NvimTreeFileIcon
14201446
NvimTreeEmptyFolderName (Directory)
14211447
NvimTreeOpenedFolderName (Directory)
@@ -1487,27 +1513,19 @@ to |nvim_tree_registering_handlers| for more information.
14871513

14881514
|nvim_tree_registering_handlers|
14891515

1490-
Handlers are registered by calling the `events.subscribe` function available in the
1491-
`require("nvim-tree.api")` module.
1516+
Handlers are registered by calling |nvim-tree-api| `events.subscribe`
1517+
function with an `events.Event` kind.
14921518

1493-
For example, registering a handler for when a node is renamed is done like this:
1494-
>
1495-
local api = require('nvim-tree.api')
1519+
e.g. handler for node renamed: >
1520+
local api = require("nvim-tree.api")
14961521
local Event = api.events.Event
14971522
14981523
api.events.subscribe(Event.NodeRenamed, function(data)
14991524
print("Node renamed from " .. data.old_name .. " to " .. data.new_name)
15001525
end)
15011526
<
1502-
15031527
|nvim_tree_events_kind|
15041528

1505-
You can access the event enum with:
1506-
>
1507-
local Event = require('nvim-tree.api').events.Event
1508-
<
1509-
Here is the list of available variant of this enum:
1510-
15111529
- Event.Ready
15121530
When NvimTree has been initialized
15131531
• Note: Handler takes no parameter.

lua/nvim-tree.lua

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ local reloaders = require "nvim-tree.actions.reloaders.reloaders"
1111
local copy_paste = require "nvim-tree.actions.fs.copy-paste"
1212
local collapse_all = require "nvim-tree.actions.tree-modifiers.collapse-all"
1313
local git = require "nvim-tree.git"
14+
local filters = require "nvim-tree.explorer.filters"
1415

1516
local _config = {}
1617

@@ -351,6 +352,22 @@ local function setup_autocommands(opts)
351352
create_nvim_tree_autocmd("BufWritePost", { callback = reloaders.reload_explorer })
352353
end
353354

355+
create_nvim_tree_autocmd("BufReadPost", {
356+
callback = function()
357+
if filters.config.filter_no_buffer then
358+
reloaders.reload_explorer()
359+
end
360+
end,
361+
})
362+
363+
create_nvim_tree_autocmd("BufUnload", {
364+
callback = function(data)
365+
if filters.config.filter_no_buffer then
366+
reloaders.reload_explorer(nil, data.buf)
367+
end
368+
end,
369+
})
370+
354371
if not has_watchers and opts.git.enable then
355372
create_nvim_tree_autocmd("User", {
356373
pattern = { "FugitiveChanged", "NeogitStatusRefreshed" },
@@ -445,7 +462,6 @@ end
445462

446463
local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
447464
auto_reload_on_write = true,
448-
create_in_closed_folder = false,
449465
disable_netrw = false,
450466
hijack_cursor = false,
451467
hijack_netrw = true,
@@ -566,6 +582,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
566582
diagnostics = {
567583
enable = false,
568584
show_on_dirs = false,
585+
show_on_open_dirs = true,
569586
debounce_delay = 50,
570587
severity = {
571588
min = vim.diagnostic.severity.HINT,
@@ -580,6 +597,8 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
580597
},
581598
filters = {
582599
dotfiles = false,
600+
git_clean = false,
601+
no_buffer = false,
583602
custom = {},
584603
exclude = {},
585604
},
@@ -592,6 +611,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
592611
enable = true,
593612
ignore = true,
594613
show_on_dirs = true,
614+
show_on_open_dirs = true,
595615
timeout = 400,
596616
},
597617
actions = {
@@ -757,7 +777,7 @@ function M.setup(conf)
757777
log.line("config", "default config + user")
758778
log.raw("config", "%s\n", vim.inspect(opts))
759779

760-
legacy.move_mappings_to_keymap(opts)
780+
legacy.generate_legacy_on_attach(opts)
761781

762782
require("nvim-tree.actions").setup(opts)
763783
require("nvim-tree.keymap").setup(opts)

lua/nvim-tree/actions/finders/search-node.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ local function search(search_dir, input_path)
1414
local function iter(dir)
1515
local realpath, path, name, stat, handle, _
1616

17+
local filter_status = filters.prepare()
18+
1719
handle, _ = vim.loop.fs_scandir(dir)
1820
if not handle then
1921
return
@@ -34,7 +36,7 @@ local function search(search_dir, input_path)
3436
break
3537
end
3638

37-
if not filters.should_ignore(path) then
39+
if not filters.should_filter(path, filter_status) then
3840
if string.find(path, "/" .. input_path .. "$") then
3941
return path
4042
end

lua/nvim-tree/actions/fs/copy-paste.lua

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local lib = require "nvim-tree.lib"
22
local log = require "nvim-tree.log"
33
local utils = require "nvim-tree.utils"
44
local core = require "nvim-tree.core"
5+
local events = require "nvim-tree.events"
56
local notify = require "nvim-tree.notify"
67

78
local M = {}
@@ -160,11 +161,8 @@ local function do_paste(node, action_type, action_fn)
160161
return
161162
end
162163
local is_dir = stats and stats.type == "directory"
163-
164164
if not is_dir then
165165
destination = vim.fn.fnamemodify(destination, ":p:h")
166-
elseif not node.open then
167-
destination = vim.fn.fnamemodify(destination, ":p:h:h")
168166
end
169167

170168
for _, _node in ipairs(clip) do
@@ -192,6 +190,7 @@ local function do_cut(source, destination)
192190
return false, errmsg
193191
end
194192
utils.rename_loaded_buffers(source, destination)
193+
events._dispatch_node_renamed(source, destination)
195194
return true
196195
end
197196

lua/nvim-tree/actions/fs/create-file.lua

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ local function get_num_nodes(iter)
4242
end
4343

4444
local function get_containing_folder(node)
45-
local is_open = M.create_in_closed_folder or node.open
46-
if node.nodes ~= nil and is_open then
45+
if node.nodes ~= nil then
4746
return utils.path_add_trailing(node.absolute_path)
4847
end
4948
local node_name_size = #(node.name or "")
@@ -113,7 +112,6 @@ function M.fn(node)
113112
end
114113

115114
function M.setup(opts)
116-
M.create_in_closed_folder = opts.create_in_closed_folder
117115
M.enable_reload = not opts.filesystem_watchers.enable
118116
end
119117

lua/nvim-tree/actions/moves/item.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local utils = require "nvim-tree.utils"
22
local view = require "nvim-tree.view"
33
local core = require "nvim-tree.core"
44
local lib = require "nvim-tree.lib"
5+
local explorer_common = require "nvim-tree.explorer.common"
56

67
local M = {}
78

@@ -14,7 +15,7 @@ function M.fn(where, what)
1415
for line, node in pairs(nodes_by_line) do
1516
local valid = false
1617
if what == "git" then
17-
valid = node.git_status ~= nil
18+
valid = explorer_common.shows_git_status(node)
1819
elseif what == "diag" then
1920
valid = node.diag_status ~= nil
2021
end

0 commit comments

Comments
 (0)