Skip to content

Commit e29ffa9

Browse files
authored
feat!: add config to prevent replacing specific filetypes (#803)
default is set to not replace { "terminal", "trouble", "qf" } set `open_files_do_not_replace_filetypes = {}` to restore previous behavior. closes #564, closes #802
1 parent 2458cb7 commit e29ffa9

File tree

4 files changed

+59
-34
lines changed

4 files changed

+59
-34
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ use {
123123
popup_border_style = "rounded",
124124
enable_git_status = true,
125125
enable_diagnostics = true,
126+
open_files_do_not_replace_filetypes = { "terminal", "trouble", "qf" }, -- when opening files, do not use windows containing these filetypes
126127
sort_case_insensitive = false, -- used when sorting files and directories in the tree
127128
sort_function = nil , -- use a custom function for sorting files and directories in the tree
128129
-- sort_function = function (a,b)

lua/neo-tree.lua

+7-3
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,13 @@ M.show_in_split = function(source_name, toggle_if_open)
160160
manager.show_in_split(source_name)
161161
end
162162

163-
M.get_prior_window = function()
163+
M.get_prior_window = function(ignore_filetypes)
164+
ignore_filetypes = ignore_filetypes or {}
165+
local ignore = utils.list_to_dict(ignore_filetypes)
166+
ignore["neo-tree"] = true
167+
164168
local tabnr = vim.api.nvim_get_current_tabpage()
165-
local wins = utils.get_value(M, "config.prior_windows", {})[tabnr]
169+
local wins = utils.get_value(M, "config.prior_windows", {}, true)[tabnr]
166170
if wins == nil then
167171
return -1
168172
end
@@ -174,7 +178,7 @@ M.get_prior_window = function()
174178
if success and is_valid then
175179
local buf = vim.api.nvim_win_get_buf(last_win)
176180
local ft = vim.api.nvim_buf_get_option(buf, "filetype")
177-
if ft ~= "neo-tree" then
181+
if ignore[ft] ~= true then
178182
return last_win
179183
end
180184
end

lua/neo-tree/defaults.lua

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ local config = {
3434
log_level = "info", -- "trace", "debug", "info", "warn", "error", "fatal"
3535
log_to_file = false, -- true, false, "/path/to/file.log", use :NeoTreeLogs to show the file
3636
open_files_in_last_window = true, -- false = open files in top left window
37+
open_files_do_not_replace_filetypes = { "terminal", "trouble", "qf" }, -- when opening files, do not use windows containing these filetypes
3738
popup_border_style = "NC", -- "double", "none", "rounded", "shadow", "single" or "solid"
3839
resize_timer_interval = 500, -- in ms, needed for containers to redraw right aligned and faded content
3940
-- set to -1 to disable the resize timer entirely

lua/neo-tree/utils.lua

+50-31
Original file line numberDiff line numberDiff line change
@@ -461,8 +461,11 @@ M.get_appropriate_window = function(state)
461461
-- use last window if possible
462462
local suitable_window_found = false
463463
local nt = require("neo-tree")
464+
local ignore_ft = nt.config.open_files_do_not_replace_filetypes
465+
local ignore = M.list_to_dict(ignore_ft)
466+
ignore["neo-tree"] = true
464467
if nt.config.open_files_in_last_window then
465-
local prior_window = nt.get_prior_window()
468+
local prior_window = nt.get_prior_window(ignore)
466469
if prior_window > 0 then
467470
local success = pcall(vim.api.nvim_set_current_win, prior_window)
468471
if success then
@@ -480,13 +483,17 @@ M.get_appropriate_window = function(state)
480483
end
481484
local attempts = 0
482485
while attempts < 5 and not suitable_window_found do
483-
if vim.bo.filetype == "neo-tree" or M.is_floating() then
486+
if ignore[vim.bo.filetype] or M.is_floating() then
484487
attempts = attempts + 1
485488
vim.cmd("wincmd w")
486489
else
487490
suitable_window_found = true
488491
end
489492
end
493+
if not suitable_window_found then
494+
-- go back to the neotree window, this will forve it to open a new split
495+
vim.api.nvim_set_current_win(current_window)
496+
end
490497

491498
local winid = vim.api.nvim_get_current_win()
492499
local is_neo_tree_window = vim.bo.filetype == "neo-tree"
@@ -515,7 +522,8 @@ M.open_file = function(state, path, open_cmd, bufnr)
515522
end
516523

517524
if M.truthy(path) then
518-
local escaped_path = bufnr or vim.fn.fnameescape(path)
525+
local escaped_path = vim.fn.fnameescape(path)
526+
local bufnr_or_path = bufnr or escaped_path
519527
local events = require("neo-tree.events")
520528
local result = true
521529
local err = nil
@@ -530,47 +538,58 @@ M.open_file = function(state, path, open_cmd, bufnr)
530538
return
531539
end
532540
if state.current_position == "current" then
533-
result, err = pcall(vim.cmd, open_cmd .. " " .. escaped_path)
541+
result, err = pcall(vim.cmd, open_cmd .. " " .. bufnr_or_path)
534542
else
535543
local winid, is_neo_tree_window = M.get_appropriate_window(state)
536544
vim.api.nvim_set_current_win(winid)
537545
-- TODO: make this configurable, see issue #43
538546
if is_neo_tree_window then
539-
-- Neo-tree must be the only window, restore it's status as a sidebar
540-
local default_width = 40
541-
local width = M.get_value(state, "window.width", default_width, false)
542-
local available_width = vim.api.nvim_win_get_width(0)
543-
if type(width) == "string" then
544-
if string.sub(width, -1) == "%" then
545-
width = tonumber(string.sub(width, 1, #width - 1)) / 100
547+
local width = vim.api.nvim_win_get_width(0)
548+
if width == vim.o.columns then
549+
-- Neo-tree must be the only window, restore it's status as a sidebar
550+
local default_width = 40
551+
width = M.get_value(state, "window.width", default_width, false)
552+
local available_width = vim.api.nvim_win_get_width(0)
553+
if type(width) == "string" then
554+
if string.sub(width, -1) == "%" then
555+
width = tonumber(string.sub(width, 1, #width - 1)) / 100
556+
else
557+
width = tonumber(width)
558+
end
559+
width = math.floor(available_width * width)
560+
elseif type(width) == "function" then
561+
width = width()
562+
if type(width) ~= "number" then
563+
width = default_width
564+
else
565+
width = math.floor(width)
566+
end
567+
elseif type(width) == "number" then
568+
width = math.floor(width)
546569
else
547-
width = tonumber(width)
548-
end
549-
width = math.floor(available_width * width)
550-
elseif type(width) == "function" then
551-
width = width()
552-
if type(width) ~= "number" then
553570
width = default_width
554-
else
555-
width = math.floor(width)
556571
end
557-
elseif type(width) == "number" then
558-
width = math.floor(width)
559-
else
560-
width = default_width
561572
end
562-
local nt = require("neo-tree")
563-
local split_command = "vsplit "
573+
574+
local split_command = "vsplit"
564575
-- respect window position in user config when Neo-tree is the only window
565-
if nt.config.window.position == "left" then
566-
split_command = "rightbelow vs "
567-
elseif nt.config.window.position == "right" then
568-
split_command = "leftabove vs "
576+
if state.current_position == "left" then
577+
split_command = "rightbelow vs"
578+
elseif state.current_position == "right" then
579+
split_command = "leftabove vs"
580+
end
581+
if path == "[No Name]" then
582+
result, err = pcall(vim.cmd, split_command)
583+
if result then
584+
vim.cmd("b" .. bufnr)
585+
end
586+
else
587+
result, err = pcall(vim.cmd, split_command .. escaped_path)
569588
end
570-
result, err = pcall(vim.cmd, split_command .. escaped_path)
589+
571590
vim.api.nvim_win_set_width(winid, width)
572591
else
573-
result, err = pcall(vim.cmd, open_cmd .. " " .. escaped_path)
592+
result, err = pcall(vim.cmd, open_cmd .. " " .. bufnr_or_path)
574593
end
575594
end
576595
if result or err == "Vim(edit):E325: ATTENTION" then

0 commit comments

Comments
 (0)