Skip to content

feat(even): add autocommands NvimTreeRequired, NvimTreeSetup #1910

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# Broken master 2023/01/10

An incorrectly executed force push to master resulted in lost changes and conflicting commit history. The changes have been manually restored: https://github.com/nvim-tree/nvim-tree.lua/issues/1906

If your plugin manager experiences issues updating delete/reinstall nvim-tree.

Please accept my apologies for the inconvenience.

Alex

# A File Explorer For Neovim Written In Lua

[![CI](https://github.com/nvim-tree/nvim-tree.lua/actions/workflows/ci.yml/badge.svg)](https://github.com/nvim-tree/nvim-tree.lua/actions/workflows/ci.yml)
Expand Down
15 changes: 15 additions & 0 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1674,6 +1674,21 @@ e.g. handler for node renamed: >
handler parameters: ~
{buf} `{number} `API buffer handle (buffer number)

|nvim_tree_events_startup|

There are two special startup events in the form of User autocommands:

`NvimTreeRequired` first `require("nvim-tree")`
`NvimTreeSetup` `setup({})` completed

Example subscription: >
vim.api.nvim_create_autocmd("User", {
pattern = "NvimTreeRequired",
callback = function(data)
---
end,
}
<
==============================================================================
9. BOOKMARKS *nvim-tree-bookmarks*

Expand Down
3 changes: 3 additions & 0 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,10 @@ function M.setup(conf)

vim.schedule(function()
M.on_enter(netrw_disabled)
vim.api.nvim_exec_autocmds("User", { pattern = "NvimTreeSetup" })
end)
end

vim.api.nvim_exec_autocmds("User", { pattern = "NvimTreeRequired" })

return M
8 changes: 7 additions & 1 deletion lua/nvim-tree/explorer/explore.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ local filters = require "nvim-tree.explorer.filters"
local live_filter = require "nvim-tree.live-filter"
local log = require "nvim-tree.log"

local Watcher = require "nvim-tree.watcher"

local M = {}

local function get_type_from(type_, cwd)
Expand All @@ -28,7 +30,11 @@ local function populate_children(handle, cwd, node, git_status)
local ps = log.profile_start(pn)

t = get_type_from(t, abs)
if not filters.should_filter(abs, filter_status) and not nodes_by_path[abs] then
if
not filters.should_filter(abs, filter_status)
and not nodes_by_path[abs]
and Watcher.is_fs_event_capable(abs)
then
local child = nil
if t == "directory" and vim.loop.fs_access(abs, "R") then
child = builders.folder(node, abs, name)
Expand Down
3 changes: 2 additions & 1 deletion lua/nvim-tree/explorer/reload.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ local git = require "nvim-tree.git"
local log = require "nvim-tree.log"

local NodeIterator = require "nvim-tree.iterators.node-iterator"
local Watcher = require "nvim-tree.watcher"

local M = {}

Expand Down Expand Up @@ -86,7 +87,7 @@ function M.reload(node, git_status, unloaded_bufnr)
end

if not nodes_by_path[abs] then
if t == "directory" and vim.loop.fs_access(abs, "R") then
if t == "directory" and vim.loop.fs_access(abs, "R") and Watcher.is_fs_event_capable(abs) then
local folder = builders.folder(node, abs, name)
nodes_by_path[abs] = folder
table.insert(node.nodes, folder)
Expand Down
25 changes: 25 additions & 0 deletions lua/nvim-tree/watcher.lua
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,29 @@ function M.purge_watchers()
end
end

--- Windows NT will present directories that cannot be enumerated.
--- Detect these by attempting to start an event monitor.
--- @param path string
--- @return boolean
function M.is_fs_event_capable(path)
if not utils.is_windows then
return true
end

local fs_event = vim.loop.new_fs_event()
if not fs_event then
return false
end

if fs_event:start(path, FS_EVENT_FLAGS, function() end) ~= 0 then
return false
end

if fs_event:stop() ~= 0 then
return false
end

return true
end

return M