Skip to content

Commit 72404b9

Browse files
committed
Restore original position of edit function
1 parent 114fde0 commit 72404b9

File tree

1 file changed

+52
-53
lines changed

1 file changed

+52
-53
lines changed

lua/nvim-tree/api.lua

Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -121,45 +121,6 @@ local function wrap_explorer_member(explorer_member, member_method)
121121
end)
122122
end
123123

124-
---
125-
---@class NodeEditOpts
126-
---@field quit_on_open boolean|nil default false
127-
---@field focus boolean|nil default true
128-
129-
---@param mode string
130-
---@param node Node
131-
---@param edit_opts NodeEditOpts?
132-
local function edit(mode, node, edit_opts)
133-
local file_link = node:as(FileLinkNode)
134-
local path = file_link and file_link.link_to or node.absolute_path
135-
local cur_tabpage = vim.api.nvim_get_current_tabpage()
136-
137-
local explorer = core.get_explorer()
138-
139-
actions.node.open_file.fn(mode, path)
140-
141-
edit_opts = edit_opts or {}
142-
143-
local mode_unsupported_quit_on_open = mode == "drop" or mode == "tab_drop" or mode == "edit_in_place"
144-
if not mode_unsupported_quit_on_open and edit_opts.quit_on_open then
145-
if explorer then
146-
explorer.view:close(cur_tabpage, "api.edit " .. mode)
147-
end
148-
end
149-
150-
local mode_unsupported_focus = mode == "drop" or mode == "tab_drop" or mode == "edit_in_place"
151-
local focus = edit_opts.focus == nil or edit_opts.focus == true
152-
if not mode_unsupported_focus and not focus then
153-
-- if mode == "tabnew" a new tab will be opened and we need to focus back to the previous tab
154-
if mode == "tabnew" then
155-
vim.cmd(":tabprev")
156-
end
157-
if explorer then
158-
explorer.view:focus()
159-
end
160-
end
161-
end
162-
163124
---@class ApiTreeOpenOpts
164125
---@field path string|nil path
165126
---@field current_window boolean|nil default false
@@ -230,20 +191,6 @@ Api.tree.collapse_all = wrap(actions.tree.modifiers.collapse.all)
230191
---@field descend_until (fun(expansion_count: integer, node: Node): boolean)|nil
231192

232193
Api.tree.expand_all = wrap_node(actions.tree.modifiers.expand.all)
233-
234-
Api.tree.toggle_descend_until = wrap_node(function(node, descend_until)
235-
local dir = node:as(DirectoryNode)
236-
if dir then
237-
if node.open then
238-
dir:expand_or_collapse(nil)
239-
else
240-
actions.tree.modifiers.expand.all(node, { descend_until = descend_until })
241-
end
242-
else
243-
edit("edit", node)
244-
end
245-
end)
246-
247194
Api.tree.toggle_enable_filters = wrap_explorer_member("filters", "toggle")
248195
Api.tree.toggle_gitignore_filter = wrap_explorer_member_args("filters", "toggle", "git_ignored")
249196
Api.tree.toggle_git_clean_filter = wrap_explorer_member_args("filters", "toggle", "git_clean")
@@ -283,6 +230,58 @@ Api.fs.copy.filename = wrap_node(wrap_explorer_member("clipboard", "copy_filenam
283230
Api.fs.copy.basename = wrap_node(wrap_explorer_member("clipboard", "copy_basename"))
284231
Api.fs.copy.relative_path = wrap_node(wrap_explorer_member("clipboard", "copy_path"))
285232

233+
---
234+
---@class NodeEditOpts
235+
---@field quit_on_open boolean|nil default false
236+
---@field focus boolean|nil default true
237+
238+
---@param mode string
239+
---@param node Node
240+
---@param edit_opts NodeEditOpts?
241+
local function edit(mode, node, edit_opts)
242+
local file_link = node:as(FileLinkNode)
243+
local path = file_link and file_link.link_to or node.absolute_path
244+
local cur_tabpage = vim.api.nvim_get_current_tabpage()
245+
246+
local explorer = core.get_explorer()
247+
248+
actions.node.open_file.fn(mode, path)
249+
250+
edit_opts = edit_opts or {}
251+
252+
local mode_unsupported_quit_on_open = mode == "drop" or mode == "tab_drop" or mode == "edit_in_place"
253+
if not mode_unsupported_quit_on_open and edit_opts.quit_on_open then
254+
if explorer then
255+
explorer.view:close(cur_tabpage, "api.edit " .. mode)
256+
end
257+
end
258+
259+
local mode_unsupported_focus = mode == "drop" or mode == "tab_drop" or mode == "edit_in_place"
260+
local focus = edit_opts.focus == nil or edit_opts.focus == true
261+
if not mode_unsupported_focus and not focus then
262+
-- if mode == "tabnew" a new tab will be opened and we need to focus back to the previous tab
263+
if mode == "tabnew" then
264+
vim.cmd(":tabprev")
265+
end
266+
if explorer then
267+
explorer.view:focus()
268+
end
269+
end
270+
end
271+
272+
Api.tree.toggle_descend_until = wrap_node(function(node, descend_until)
273+
local dir = node:as(DirectoryNode)
274+
if dir then
275+
if node.open then
276+
dir:expand_or_collapse(nil)
277+
else
278+
actions.tree.modifiers.expand.all(node, { descend_until = descend_until })
279+
end
280+
else
281+
edit("edit", node)
282+
end
283+
end)
284+
286285
---@param mode string
287286
---@param toggle_group boolean?
288287
---@return fun(node: Node, edit_opts: NodeEditOpts?)

0 commit comments

Comments
 (0)