Skip to content

Commit

Permalink
fix(#2794): sshfs compatibility (#2893)
Browse files Browse the repository at this point in the history
* add type fallback for nil types

* add PR suggestions

* Update lua/nvim-tree/explorer/explore.lua

Co-authored-by: Alexander Courtis <[email protected]>

* use type from fs_stat for sshfs compatibility

---------

Co-authored-by: Alexander Courtis <[email protected]>
  • Loading branch information
mxple and alex-courtis authored Sep 9, 2024
1 parent cb57691 commit 2d6e64d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
9 changes: 7 additions & 2 deletions lua/nvim-tree/actions/fs/remove-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,18 @@ local function remove_dir(cwd)
end

while true do
local name, t = vim.loop.fs_scandir_next(handle)
local name, _ = vim.loop.fs_scandir_next(handle)
if not name then
break
end

local new_cwd = utils.path_join { cwd, name }
if t == "directory" then

-- Type must come from fs_stat and not fs_scandir_next to maintain sshfs compatibility
local stat = vim.loop.fs_stat(new_cwd)
local type = stat and stat.type or nil

if type == "directory" then
local success = remove_dir(new_cwd)
if not success then
return false
Expand Down
12 changes: 8 additions & 4 deletions lua/nvim-tree/explorer/explore.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ local function populate_children(handle, cwd, node, git_status, parent)
})

while true do
local name, t = vim.loop.fs_scandir_next(handle)
local name, _ = vim.loop.fs_scandir_next(handle)
if not name then
break
end
Expand All @@ -41,14 +41,18 @@ local function populate_children(handle, cwd, node, git_status, parent)

---@type uv.fs_stat.result|nil
local stat = vim.loop.fs_stat(abs)

-- Type must come from fs_stat and not fs_scandir_next to maintain sshfs compatibility
local type = stat and stat.type or nil

local filter_reason = parent.filters:should_filter_as_reason(abs, stat, filter_status)
if filter_reason == FILTER_REASON.none and not nodes_by_path[abs] then
local child = nil
if t == "directory" and vim.loop.fs_access(abs, "R") then
if type == "directory" and vim.loop.fs_access(abs, "R") then
child = builders.folder(node, abs, name, stat)
elseif t == "file" then
elseif type == "file" then
child = builders.file(node, abs, name, stat)
elseif t == "link" then
elseif type == "link" then
local link = builders.link(node, abs, name, stat)
if link.link_to ~= nil then
child = link
Expand Down
13 changes: 8 additions & 5 deletions lua/nvim-tree/explorer/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ function Explorer:reload(node, git_status)
})

while true do
local name, t = vim.loop.fs_scandir_next(handle)
local name, _ = vim.loop.fs_scandir_next(handle)
if not name then
break
end
Expand All @@ -132,11 +132,14 @@ function Explorer:reload(node, git_status)
if filter_reason == FILTER_REASON.none then
remain_childs[abs] = true

-- Type must come from fs_stat and not fs_scandir_next to maintain sshfs compatibility
local type = stat and stat.type or nil

-- Recreate node if type changes.
if nodes_by_path[abs] then
local n = nodes_by_path[abs]

if n.type ~= t then
if n.type ~= type then
utils.array_remove(node.nodes, n)
explorer_node.node_destroy(n)
nodes_by_path[abs] = nil
Expand All @@ -145,11 +148,11 @@ function Explorer:reload(node, git_status)

if not nodes_by_path[abs] then
local new_child = nil
if t == "directory" and vim.loop.fs_access(abs, "R") and Watcher.is_fs_event_capable(abs) then
if type == "directory" and vim.loop.fs_access(abs, "R") and Watcher.is_fs_event_capable(abs) then
new_child = builders.folder(node, abs, name, stat)
elseif t == "file" then
elseif type == "file" then
new_child = builders.file(node, abs, name, stat)
elseif t == "link" then
elseif type == "link" then
local link = builders.link(node, abs, name, stat)
if link.link_to ~= nil then
new_child = link
Expand Down

0 comments on commit 2d6e64d

Please sign in to comment.