Skip to content

Commit fee5845

Browse files
authored
feat(command): add source last refers to the last executed source (#1081)
closes #775
1 parent 2c99276 commit fee5845

File tree

5 files changed

+29
-2
lines changed

5 files changed

+29
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ What to show. Can be one of:
482482
| filesystem | Show a file browser. DEFAULT |
483483
| buffers | Show a list of currently open buffers. |
484484
| git_status | Show the output of `git status` in a tree layout. |
485+
| last | Equivalent to the last source used |
485486

486487
#### `position`
487488
Where to show it, can be one of:

lua/neo-tree/command/init.lua

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ local M = {
1111
complete_args = completion.complete_args,
1212
}
1313

14+
-- Store the last source used for `M.execute`
15+
M._last = {
16+
source = nil,
17+
position = nil,
18+
}
19+
1420
---Executes a Neo-tree action from outside of a Neo-tree window,
1521
---such as show, hide, navigate, etc.
1622
---@param args table The action to execute. The table can have the following keys:
@@ -61,6 +67,23 @@ M.execute = function(args)
6167
-- The rest of the actions require a source
6268
args.source = args.source or nt.config.default_source
6369

70+
-- Handle source=last
71+
if args.source == "last" then
72+
args.source = M._last.source or nt.config.default_source
73+
74+
-- Restore last position if it was not specified
75+
if args.position == nil then
76+
args.position = M._last.position
77+
end
78+
79+
-- Prevent the default source from being set to "last"
80+
if args.source == "last" then
81+
args.source = nt.config.sources[1]
82+
end
83+
end
84+
M._last.source = args.source
85+
M._last.position = args.position
86+
6487
-- If position=current was requested, but we are currently in a neo-tree window,
6588
-- then we need to override that.
6689
if args.position == "current" and vim.bo.filetype == "neo-tree" then

lua/neo-tree/command/parser.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ M.setup = function(all_source_names)
1111
local source_names = utils.table_copy(all_source_names)
1212
table.insert(source_names, "migrations")
1313

14+
-- A special source referring to the last used source.
15+
table.insert(source_names, "last")
16+
1417
-- For lists, the first value is the default value.
1518
local arguments = {
1619
action = {

lua/neo-tree/defaults.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ local config = {
1515
-- popup_border_style is for input and confirmation dialogs.
1616
-- Configurtaion of floating window is done in the individual source sections.
1717
-- "NC" is a special style that works well with NormalNC set
18-
default_source = "filesystem",
18+
default_source = "filesystem", -- you can choose a specific source `last` here which indicates the last used source
1919
enable_diagnostics = true,
2020
enable_git_status = true,
2121
enable_modified_markers = true, -- Show markers for files with unsaved changes.

lua/neo-tree/setup/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ M.merge_config = function(user_config, is_auto_config)
642642
break
643643
end
644644
end
645-
if not match then
645+
if not match and M.config.default_source ~= "last" then
646646
M.config.default_source = M.config.sources[1]
647647
log.warn(string.format("Invalid default source found in configuration. Using first available source: %s", M.config.default_source))
648648
end

0 commit comments

Comments
 (0)