Skip to content

Commit 5e5cdaa

Browse files
committed
simplify create-file
1 parent cd89c75 commit 5e5cdaa

File tree

1 file changed

+9
-22
lines changed

1 file changed

+9
-22
lines changed

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

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ local notify = require("nvim-tree.notify")
55

66
local find_file = require("nvim-tree.actions.finders.find-file").fn
77

8+
local FileNode = require("nvim-tree.node.file")
89
local DirectoryNode = require("nvim-tree.node.directory")
910

1011
local M = {}
@@ -31,35 +32,21 @@ local function get_num_nodes(iter)
3132
return i
3233
end
3334

34-
---@param node Node
35-
---@return string
36-
local function get_containing_folder(node)
37-
if node.nodes ~= nil then
38-
return utils.path_add_trailing(node.absolute_path)
39-
end
40-
local node_name_size = #(node.name or "")
41-
return node.absolute_path:sub(0, -node_name_size - 1)
42-
end
43-
4435
---@param node Node?
4536
function M.fn(node)
46-
local cwd = core.get_cwd()
47-
if cwd == nil then
37+
node = node or core.get_explorer() --[[@as Node]]
38+
if not node then
4839
return
4940
end
5041

51-
if not node or node.name == ".." then
52-
node = {
53-
absolute_path = cwd,
54-
name = "",
55-
nodes = core.get_explorer().nodes,
56-
open = true,
57-
}
58-
elseif node:is(DirectoryNode) then
59-
node = node:last_group_node()
42+
local dir = node:is(FileNode) and node.parent or node:as(DirectoryNode)
43+
if not dir then
44+
return
6045
end
6146

62-
local containing_folder = get_containing_folder(node)
47+
dir = dir:last_group_node()
48+
49+
local containing_folder = utils.path_add_trailing(dir.absolute_path)
6350

6451
local input_opts = {
6552
prompt = "Create file ",

0 commit comments

Comments
 (0)