Skip to content

Commit 311fbd1

Browse files
committed
feat: add toggle_node command, open command no longer expands nested files, closes #201
1 parent 45c724f commit 311fbd1

File tree

7 files changed

+34
-18
lines changed

7 files changed

+34
-18
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ use {
119119
position = "left",
120120
width = 40,
121121
mappings = {
122+
["<space>"] = "toggle_node",
122123
["<2-LeftMouse>"] = "open",
123124
["<cr>"] = "open",
124125
["S"] = "open_split",

doc/neo-tree.txt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ are defined by default.
146146

147147
Note: The "selected" item is the line the cursor is currently on.
148148

149+
<space> = toggle_node Expand or collapse a node with children, which
150+
may be a directory or a nested file.
151+
149152
<2-LeftMouse> = open: Expand or collapse a folder. If a file is selected,
150153
open it in the window closest to the tree.
151154

@@ -633,21 +636,24 @@ component:
633636

634637
FILE NESTING *neo-tree-file-nesting*
635638

636-
By default, file nesting is disabled since `nesting_rules` table is empty,
637-
there is not files to apply the nesting, this can be enabled filling the
638-
`nesting_rules` table with the following structure:
639+
By default, file nesting is disabled since the `nesting_rules` table is empty.
640+
To enable this feature, fill in the `nesting_rules` table with the following
641+
structure:
639642
>
640643
require("neo-tree").setup({
641644
nesting_rules = {
642-
["js"] = { "js.map" }
645+
["js"] = { "js.map" },
643646
}
644647
})
645648
<
646649
This will render:
647650
>
648651
FILENAME.js
649652
FILENAME.js.map
653+
650654
<
655+
The default mapping to expand/collapse nested files is <space>.
656+
651657

652658
HIGHLIGHTS *neo-tree-highlights*
653659

lua/neo-tree/defaults.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ local config = {
107107
-- Mappings for tree window. See `:h nep-tree-mappings` for a list of built-in commands.
108108
-- You can also create your own commands by providing a function instead of a string.
109109
mappings = {
110+
["<space>"] = "toggle_node",
110111
["<2-LeftMouse>"] = "open",
111112
["<cr>"] = "open",
112113
["S"] = "open_split",

lua/neo-tree/sources/buffers/commands.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,6 @@ M.set_root = function(state)
7272
end
7373
end
7474

75+
M.toggle_node = cc.toggle_node
76+
7577
return M

lua/neo-tree/sources/common/commands.lua

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -181,19 +181,10 @@ local open_with_cmd = function(state, open_cmd, toggle_directory)
181181
return
182182
end
183183

184-
local function open()
185-
local path = node:get_id()
186-
utils.open_file(state, path, open_cmd)
187-
end
188-
189-
if utils.is_expandable(node) then
190-
if toggle_directory and node.type == "directory" then
184+
if node.type == "directory" then
185+
if toggle_directory then
191186
toggle_directory(node)
192187
elseif node:has_children() then
193-
if node:is_expanded() and node.type == "file" then
194-
return open()
195-
end
196-
197188
local updated = false
198189
if node:is_expanded() then
199190
updated = node:collapse()
@@ -205,7 +196,8 @@ local open_with_cmd = function(state, open_cmd, toggle_directory)
205196
end
206197
end
207198
else
208-
open()
199+
local path = node:get_id()
200+
utils.open_file(state, path, open_cmd)
209201
end
210202
end
211203

@@ -240,10 +232,10 @@ M.rename = function(state, callback)
240232
end
241233

242234
---Expands or collapses the current node.
243-
M.toggle_directory = function(state)
235+
M.toggle_node = function(state)
244236
local tree = state.tree
245237
local node = tree:get_node()
246-
if node.type ~= "directory" then
238+
if not utils.is_expandable(node) then
247239
return
248240
end
249241
if node.loaded == false then
@@ -263,4 +255,14 @@ M.toggle_directory = function(state)
263255
end
264256
end
265257

258+
---Expands or collapses the current node.
259+
M.toggle_directory = function(state)
260+
local tree = state.tree
261+
local node = tree:get_node()
262+
if node.type ~= "directory" then
263+
return
264+
end
265+
M.toggle_node(state)
266+
end
267+
266268
return M

lua/neo-tree/sources/filesystem/commands.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,6 @@ M.toggle_gitignore = function(state)
123123
M.toggle_hidden(state)
124124
end
125125

126+
M.toggle_node = cc.toggle_node
127+
126128
return M

lua/neo-tree/sources/git_status/commands.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,6 @@ M.rename = function(state)
129129
cc.rename(state, refresh)
130130
end
131131

132+
M.toggle_node = cc.toggle_node
133+
132134
return M

0 commit comments

Comments
 (0)