Skip to content

Commit b6d8bee

Browse files
authored
fix(commands)!: open nested files default to open instead of expand (#1220)
Use `["<cr>"] = { "open", config = { expand_nested_files = true } }` to revert to previous behavior.
1 parent f053f09 commit b6d8bee

File tree

2 files changed

+63
-76
lines changed

2 files changed

+63
-76
lines changed

lua/neo-tree/defaults.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ local config = {
365365
},
366366
["<2-LeftMouse>"] = "open",
367367
["<cr>"] = "open",
368-
-- ["<cr>"] = { "open", config = { no_expand_file = true } }, -- open top file instead of expanding when nested
368+
-- ["<cr>"] = { "open", config = { expand_nested_files = true } }, -- expand nested file takes precedence
369369
["<esc>"] = "cancel", -- close preview or floating neo-tree window
370370
["P"] = { "toggle_preview", config = { use_float = true, use_image_nvim = false } },
371371
["l"] = "focus_preview",

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

Lines changed: 62 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,13 @@ M.expand_all_nodes = function(state, node, prefetcher)
122122

123123
renderer.position.set(state, nil)
124124

125-
local task = function ()
125+
local task = function()
126126
node_expander.expand_directory_recursively(state, node, prefetcher)
127127
end
128-
async.run(
129-
task,
130-
function ()
131-
log.debug("All nodes expanded - redrawing")
132-
renderer.redraw(state)
133-
end
134-
)
128+
async.run(task, function()
129+
log.debug("All nodes expanded - redrawing")
130+
renderer.redraw(state)
131+
end)
135132
end
136133

137134
M.close_node = function(state, callback)
@@ -421,7 +418,7 @@ M.prev_source = function(state)
421418
end
422419

423420
local function set_sort(state, label)
424-
local sort = state.sort or { label = "Name", direction = -1}
421+
local sort = state.sort or { label = "Name", direction = -1 }
425422
if sort.label == label then
426423
sort.direction = sort.direction * -1
427424
else
@@ -431,59 +428,59 @@ local function set_sort(state, label)
431428
state.sort = sort
432429
end
433430

434-
M.order_by_created = function (state)
431+
M.order_by_created = function(state)
435432
set_sort(state, "Created")
436-
state.sort_field_provider = function (node)
433+
state.sort_field_provider = function(node)
437434
local stat = utils.get_stat(node)
438435
return stat.birthtime and stat.birthtime.sec or 0
439436
end
440437
require("neo-tree.sources.manager").refresh(state.name)
441438
end
442439

443-
M.order_by_modified = function (state)
440+
M.order_by_modified = function(state)
444441
set_sort(state, "Last Modified")
445-
state.sort_field_provider = function (node)
442+
state.sort_field_provider = function(node)
446443
local stat = utils.get_stat(node)
447444
return stat.mtime and stat.mtime.sec or 0
448445
end
449446
require("neo-tree.sources.manager").refresh(state.name)
450447
end
451448

452-
M.order_by_name = function (state)
449+
M.order_by_name = function(state)
453450
set_sort(state, "Name")
454451
state.sort_field_provider = nil
455452
require("neo-tree.sources.manager").refresh(state.name)
456453
end
457454

458-
M.order_by_size = function (state)
455+
M.order_by_size = function(state)
459456
set_sort(state, "Size")
460-
state.sort_field_provider = function (node)
457+
state.sort_field_provider = function(node)
461458
local stat = utils.get_stat(node)
462459
return stat.size or 0
463460
end
464461
require("neo-tree.sources.manager").refresh(state.name)
465462
end
466463

467-
M.order_by_type = function (state)
464+
M.order_by_type = function(state)
468465
set_sort(state, "Type")
469-
state.sort_field_provider = function (node)
466+
state.sort_field_provider = function(node)
470467
return node.ext or node.type
471468
end
472469
require("neo-tree.sources.manager").refresh(state.name)
473470
end
474471

475-
M.order_by_git_status = function (state)
472+
M.order_by_git_status = function(state)
476473
set_sort(state, "Git Status")
477474

478-
state.sort_field_provider = function (node)
475+
state.sort_field_provider = function(node)
479476
local git_status_lookup = state.git_status_lookup or {}
480477
local git_status = git_status_lookup[node.path]
481478
if git_status then
482479
return git_status
483480
end
484481

485482
if node.filtered_by and node.filtered_by.gitignored then
486-
return "!!"
483+
return "!!"
487484
else
488485
return ""
489486
end
@@ -492,10 +489,10 @@ M.order_by_git_status = function (state)
492489
require("neo-tree.sources.manager").refresh(state.name)
493490
end
494491

495-
M.order_by_diagnostics = function (state)
492+
M.order_by_diagnostics = function(state)
496493
set_sort(state, "Diagnostics")
497494

498-
state.sort_field_provider = function (node)
495+
state.sort_field_provider = function(node)
499496
local diag = state.diagnostics_lookup or {}
500497
local diagnostics = diag[node.path]
501498
if not diagnostics then
@@ -515,7 +512,7 @@ M.show_debug_info = function(state)
515512
print(vim.inspect(state))
516513
end
517514

518-
M.show_file_details = function (state)
515+
M.show_file_details = function(state)
519516
local node = state.tree:get_node()
520517
if node.type == "message" then
521518
return
@@ -671,6 +668,38 @@ M.focus_preview = function()
671668
Preview.focus()
672669
end
673670

671+
---Expands or collapses the current node.
672+
M.toggle_node = function(state, toggle_directory)
673+
local tree = state.tree
674+
local node = tree:get_node()
675+
if not utils.is_expandable(node) then
676+
return
677+
end
678+
if node.type == "directory" and toggle_directory then
679+
toggle_directory(node)
680+
elseif node:has_children() then
681+
local updated = false
682+
if node:is_expanded() then
683+
updated = node:collapse()
684+
else
685+
updated = node:expand()
686+
end
687+
if updated then
688+
renderer.redraw(state)
689+
end
690+
end
691+
end
692+
693+
---Expands or collapses the current node.
694+
M.toggle_directory = function(state, toggle_directory)
695+
local tree = state.tree
696+
local node = tree:get_node()
697+
if node.type ~= "directory" then
698+
return
699+
end
700+
M.toggle_node(state, toggle_directory)
701+
end
702+
674703
---Open file or directory
675704
---@param state table The state of the source
676705
---@param open_cmd string The vim command to use to open the file
@@ -709,25 +738,15 @@ local open_with_cmd = function(state, open_cmd, toggle_directory, open_file)
709738
end
710739
end
711740

712-
if utils.is_expandable(node) then
713-
if toggle_directory and node.type == "directory" then
714-
toggle_directory(node)
715-
elseif node:has_children() then
716-
local no_expand_file = (state.config or {}).no_expand_file
717-
if node.type ~= "directory" and (no_expand_file or node:is_expanded()) then
718-
return open()
719-
end
720-
721-
local updated = false
722-
if node:is_expanded() then
723-
updated = node:collapse()
724-
else
725-
updated = node:expand()
726-
end
727-
if updated then
728-
renderer.redraw(state)
729-
end
730-
end
741+
local config = state.config or {}
742+
if node.type ~= "directory" and config.no_expand_file ~= nil then
743+
log.warn("`no_expand_file` options is deprecated, move to `expand_nested_files` (OPPOSITE)")
744+
config.expand_nested_files = not config.no_expand_file
745+
end
746+
if node.type == "directory" then
747+
M.toggle_node(state, toggle_directory)
748+
elseif node:has_children() and config.expand_nested_files and not node:is_expanded() then
749+
M.toggle_node(state, toggle_directory)
731750
else
732751
open()
733752
end
@@ -790,38 +809,6 @@ M.rename = function(state, callback)
790809
fs_actions.rename_node(node.path, callback)
791810
end
792811

793-
---Expands or collapses the current node.
794-
M.toggle_node = function(state, toggle_directory)
795-
local tree = state.tree
796-
local node = tree:get_node()
797-
if not utils.is_expandable(node) then
798-
return
799-
end
800-
if node.type == "directory" and toggle_directory then
801-
toggle_directory(node)
802-
elseif node:has_children() then
803-
local updated = false
804-
if node:is_expanded() then
805-
updated = node:collapse()
806-
else
807-
updated = node:expand()
808-
end
809-
if updated then
810-
renderer.redraw(state)
811-
end
812-
end
813-
end
814-
815-
---Expands or collapses the current node.
816-
M.toggle_directory = function(state, toggle_directory)
817-
local tree = state.tree
818-
local node = tree:get_node()
819-
if node.type ~= "directory" then
820-
return
821-
end
822-
M.toggle_node(state, toggle_directory)
823-
end
824-
825812
---Marks potential windows with letters and will open the give node in the picked window.
826813
---@param state table The state of the source
827814
---@param path string The path to open

0 commit comments

Comments
 (0)