Skip to content

Commit 4660041

Browse files
authored
feat: add keep_altfile option (#1912)
1 parent 0d0b29a commit 4660041

File tree

4 files changed

+67
-54
lines changed

4 files changed

+67
-54
lines changed

lua/neo-tree/defaults.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ local config = {
3434
hide_root_node = false, -- Hide the root node.
3535
retain_hidden_root_indent = false, -- IF the root node is hidden, keep the indentation anyhow.
3636
-- This is needed if you use expanders because they render in the indent.
37+
keep_altfile = false, -- Whether the `:h alternate-file` should stay as the file used before opening Neo-tree
3738
-- The minimum level of log statements that should be logged to the log file.
3839
log_level = vim.log.levels.INFO, -- or other vim.log.levels (up to .ERROR), or "trace", "debug", "info", "warn", "error", "fatal"
3940
-- For usabiliity, the minimum console log level = max(log_level, INFO) unless set explicitly using a table:

lua/neo-tree/health/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ function M.check_config(config)
157157
end)
158158
validate("hide_root_node", cfg.hide_root_node, "boolean")
159159
validate("retain_hidden_root_indent", cfg.retain_hidden_root_indent, "boolean")
160+
validate("keep_altfile", cfg.keep_altfile, "boolean")
160161
validate("log_level", cfg.log_level, schema.ConfigLogLevel, true)
161162
validate("log_to_file", cfg.log_to_file, { "boolean", "string" })
162163
validate("open_files_in_last_window", cfg.open_files_in_last_window, "boolean")

lua/neo-tree/types/config.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
---@field git_status_async boolean
122122
---@field git_status_async_options neotree.Config.GitStatusAsync
123123
---@field hide_root_node boolean
124+
---@field keep_altfile boolean
124125
---@field retain_hidden_root_indent boolean
125126
---@field log_level neotree.Logger.Config.Level
126127
---@field log_to_file boolean|string

lua/neo-tree/utils/init.lua

Lines changed: 64 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -783,65 +783,75 @@ M.open_file = function(state, path, open_cmd, bufnr)
783783
end
784784
end
785785

786-
if M.truthy(path) then
787-
local relative = require("neo-tree").config.open_files_using_relative_paths
788-
local escaped_path = M.escape_path_for_cmd(relative and vim.fn.fnamemodify(path, ":.") or path)
789-
local bufnr_or_path = bufnr or escaped_path
790-
local events = require("neo-tree.events")
791-
local result = true
792-
local err = nil
793-
local event_result = events.fire_event(events.FILE_OPEN_REQUESTED, {
794-
state = state,
795-
path = path,
796-
open_cmd = open_cmd,
797-
bufnr = bufnr,
798-
}) or {}
799-
if event_result.handled then
800-
events.fire_event(events.FILE_OPENED, path)
801-
return
802-
end
803-
if state.current_position == "current" then
804-
---@diagnostic disable-next-line: param-type-mismatch
805-
result, err = pcall(vim.cmd, open_cmd .. " " .. bufnr_or_path)
806-
else
807-
local winid, is_neo_tree_window = M.get_appropriate_window(state)
808-
vim.api.nvim_set_current_win(winid)
809-
-- TODO: make this configurable, see issue #43
810-
if is_neo_tree_window then
811-
local width = vim.api.nvim_win_get_width(0)
812-
if width == vim.o.columns then
813-
-- Neo-tree must be the only window, restore it's status as a sidebar
814-
width = M.get_value(state, "window.width", 40, false)
815-
width = M.resolve_width(width)
816-
end
817-
result, err = M.force_new_split(state.current_position, escaped_path)
818-
vim.api.nvim_win_set_width(winid, width)
819-
else
820-
---@diagnostic disable-next-line: param-type-mismatch
821-
result, err = pcall(vim.cmd, open_cmd .. " " .. bufnr_or_path)
822-
end
823-
end
824-
if not result and string.find(err or "", "winfixbuf") and M.is_winfixbuf() then
825-
local winid, is_neo_tree_window = M.get_appropriate_window(state, true)
826-
-- Rescan window list to find a window that is not winfixbuf.
827-
-- If found, retry executing command in that window,
828-
-- otherwise, all windows are either neo-tree or winfixbuf so we make a new split.
829-
if not is_neo_tree_window and not M.is_winfixbuf(winid) then
830-
vim.api.nvim_set_current_win(winid)
831-
---@diagnostic disable-next-line: param-type-mismatch
832-
result, err = pcall(vim.cmd, open_cmd .. " " .. bufnr_or_path)
833-
else
834-
result, err = M.force_new_split(state.current_position, escaped_path)
786+
if not M.truthy(path) then
787+
return
788+
end
789+
790+
local config = require("neo-tree").config
791+
local relative = config.open_files_using_relative_paths
792+
local escaped_path = M.escape_path_for_cmd(relative and vim.fn.fnamemodify(path, ":.") or path)
793+
local bufnr_or_path = bufnr or escaped_path
794+
local events = require("neo-tree.events")
795+
local event_result = events.fire_event(events.FILE_OPEN_REQUESTED, {
796+
state = state,
797+
path = path,
798+
open_cmd = open_cmd,
799+
bufnr = bufnr,
800+
}) or {}
801+
if event_result.handled then
802+
events.fire_event(events.FILE_OPENED, path)
803+
return
804+
end
805+
806+
local command = open_cmd .. " " .. bufnr_or_path
807+
if config.keep_altfile then
808+
-- see `:h g:netrw_altfile`
809+
command = "keepalt " .. command
810+
end
811+
812+
local result, err = true, nil
813+
if state.current_position == "current" then
814+
---@diagnostic disable-next-line: param-type-mismatch
815+
result, err = pcall(vim.cmd, command)
816+
else
817+
local winid, is_neo_tree_window = M.get_appropriate_window(state)
818+
vim.api.nvim_set_current_win(winid)
819+
-- TODO: make this configurable, see issue #43
820+
if is_neo_tree_window then
821+
local width = vim.api.nvim_win_get_width(0)
822+
if width == vim.o.columns then
823+
-- Neo-tree must be the only window, restore it's status as a sidebar
824+
width = M.get_value(state, "window.width", 40, false)
825+
width = M.resolve_width(width)
835826
end
827+
result, err = M.force_new_split(state.current_position, escaped_path)
828+
vim.api.nvim_win_set_width(winid, width)
829+
else
830+
---@diagnostic disable-next-line: param-type-mismatch
831+
result, err = pcall(vim.cmd, command)
836832
end
837-
if result or err == "Vim(edit):E325: ATTENTION" then
838-
-- fixes #321
839-
vim.bo[0].buflisted = true
840-
events.fire_event(events.FILE_OPENED, path)
833+
end
834+
835+
if not result and string.find(err or "", "winfixbuf") and M.is_winfixbuf() then
836+
local winid, is_neo_tree_window = M.get_appropriate_window(state, true)
837+
-- Rescan window list to find a window that is not winfixbuf.
838+
-- If found, retry executing command in that window,
839+
-- otherwise, all windows are either neo-tree or winfixbuf so we make a new split.
840+
if not is_neo_tree_window and not M.is_winfixbuf(winid) then
841+
vim.api.nvim_set_current_win(winid)
842+
---@diagnostic disable-next-line: param-type-mismatch
843+
result, err = pcall(vim.cmd, command)
841844
else
842-
log.error("Error opening file:", err)
845+
result, err = M.force_new_split(state.current_position, escaped_path)
843846
end
844847
end
848+
if result or err == "Vim(edit):E325: ATTENTION" then
849+
-- fixes #321
850+
vim.bo[0].buflisted = true
851+
events.fire_event(events.FILE_OPENED, path)
852+
else
853+
log.error("Error opening file:", err)
854+
end
845855
end
846856

847857
M.reduce = function(list, memo, func)

0 commit comments

Comments
 (0)