Skip to content

Commit 1bfd7d1

Browse files
committed
feat(files): cleanup netrw hijack to work with [v]split ., fixes #121
1 parent eb7053a commit 1bfd7d1

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

lua/neo-tree.lua

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,22 @@ local check_source = function(source_name)
8181
return source_name
8282
end
8383

84+
local hijack_netrw = function()
85+
local bufname = vim.api.nvim_buf_get_name(0)
86+
local stats = vim.loop.fs_stat(bufname)
87+
local is_dir = stats and stats.type == "directory"
88+
if is_dir then
89+
local bufnr = vim.api.nvim_get_current_buf()
90+
vim.cmd("silent! b#") -- to make the alternate buffer correct after wiping the directory buffer
91+
manager.navigate("filesystem", bufname, nil, function()
92+
vim.cmd("bwipeout! " .. bufnr)
93+
end)
94+
return true
95+
else
96+
return false
97+
end
98+
end
99+
84100
local get_position = function(source_name)
85101
local pos = utils.get_value(M, "config." .. source_name .. ".window.position", "left")
86102
return pos
@@ -154,19 +170,6 @@ M.focus = function(source_name, close_others, toggle_if_open)
154170
manager.focus(source_name)
155171
end
156172

157-
M.hijack_netrw = function()
158-
local bufname = vim.api.nvim_buf_get_name(0)
159-
local stats = vim.loop.fs_stat(bufname)
160-
local is_dir = stats and stats.type == "directory"
161-
if is_dir then
162-
vim.cmd("bwipeout!")
163-
manager.navigate("filesystem", bufname)
164-
return true
165-
else
166-
return false
167-
end
168-
end
169-
170173
M.reveal_current_file = function(source_name, toggle_if_open)
171174
source_name = check_source(source_name)
172175
if get_position(source_name) == "split" then
@@ -280,7 +283,7 @@ M.buffer_enter_event = function(args)
280283
end
281284

282285
-- if vim is trying to open a dir, then we hijack it
283-
if M.hijack_netrw() then
286+
if hijack_netrw() then
284287
return
285288
end
286289

lua/neo-tree/sources/manager.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ M.focus = function(source_name, path_to_reveal, callback)
317317
end
318318

319319
---Navigate to the given path.
320+
---@param state_or_source_name string|table The state or source name to navigate.
320321
---@param path string Path to navigate to. If empty, will navigate to the cwd.
321322
---@param path_to_reveal string Node to focus after the items are loaded.
322323
---@param callback function Callback to call after the items are loaded.

0 commit comments

Comments
 (0)