Skip to content

Commit

Permalink
refactor(#2826): multi instance nvim-tree.view
Browse files Browse the repository at this point in the history
  • Loading branch information
raaymax committed Aug 4, 2024
1 parent 0f2cda6 commit fa051cf
Show file tree
Hide file tree
Showing 22 changed files with 390 additions and 297 deletions.
44 changes: 33 additions & 11 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ local lib = require "nvim-tree.lib"
local log = require "nvim-tree.log"
local appearance = require "nvim-tree.appearance"
local renderer = require "nvim-tree.renderer"
local view = require "nvim-tree.view"
local commands = require "nvim-tree.commands"
local utils = require "nvim-tree.utils"
local actions = require "nvim-tree.actions"
Expand Down Expand Up @@ -81,7 +80,11 @@ function M.change_root(path, bufnr)
end

function M.tab_enter()
if view.is_visible { any_tabpage = true } then
local explorer = core.get_explorer();
if not explorer then
return
end
if explorer.view:is_visible { any_tabpage = true } then
local bufname = vim.api.nvim_buf_get_name(0)

local ft
Expand All @@ -96,13 +99,17 @@ function M.tab_enter()
return
end
end
view.open { focus_tree = false }
explorer.view:open { focus_tree = false }
renderer.draw()
end
end

function M.open_on_directory()
local should_proceed = _config.hijack_directories.auto_open or view.is_visible()
local explorer = core.get_explorer();
if not explorer then
return
end
local should_proceed = _config.hijack_directories.auto_open or explorer.view:is_visible()
if not should_proceed then
return
end
Expand Down Expand Up @@ -177,8 +184,12 @@ local function setup_autocommands(opts)
-- reset and draw (highlights) when colorscheme is changed
create_nvim_tree_autocmd("ColorScheme", {
callback = function()
local explorer = core.get_explorer();
if not explorer then
return
end
appearance.setup()
view.reset_winhl()
explorer.view:reset_winhl()
renderer.draw()
end,
})
Expand All @@ -190,10 +201,14 @@ local function setup_autocommands(opts)
if not utils.is_nvim_tree_buf(0) then
return
end
local explorer = core.get_explorer();
if not explorer then
return
end
if opts.actions.open_file.eject then
view._prevent_buffer_override()
explorer.view:_prevent_buffer_override()
else
view.abandon_current_window()
explorer.view:abandon_current_window()
end
end,
})
Expand Down Expand Up @@ -331,8 +346,12 @@ local function setup_autocommands(opts)
create_nvim_tree_autocmd("WinLeave", {
pattern = "NvimTree_*",
callback = function()
local explorer = core.get_explorer()
if not explorer then
return
end
if utils.is_nvim_tree_buf(0) then
view.close()
explorer.view:close()
end
end,
})
Expand Down Expand Up @@ -783,8 +802,12 @@ end

function M.purge_all_state()
require("nvim-tree.watcher").purge_watchers()
view.close_all_tabs()
view.abandon_all_windows()
local explorer = core.get_explorer()
if not explorer then
return
end
explorer.view:close_all_tabs()
explorer.view:abandon_all_windows()
if core.get_explorer() ~= nil then
git.purge_state()
core.reset_explorer()
Expand Down Expand Up @@ -834,7 +857,6 @@ function M.setup(conf)
require("nvim-tree.explorer").setup(opts)
require("nvim-tree.git").setup(opts)
require("nvim-tree.git.utils").setup(opts)
require("nvim-tree.view").setup(opts)
require("nvim-tree.lib").setup(opts)
require("nvim-tree.renderer").setup(opts)
require("nvim-tree.marks").setup(opts)
Expand Down
8 changes: 4 additions & 4 deletions lua/nvim-tree/actions/finders/find-file.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local log = require "nvim-tree.log"
local view = require "nvim-tree.view"
local utils = require "nvim-tree.utils"
local renderer = require "nvim-tree.renderer"
local reload = require "nvim-tree.explorer.reload"
Expand All @@ -13,7 +12,8 @@ local running = {}
---Find a path in the tree, expand it and focus it
---@param path string relative or absolute
function M.fn(path)
if not core.get_explorer() or not view.is_visible() then
local explorer = core.get_explorer()
if not explorer or not explorer.view:is_visible() then
return
end

Expand Down Expand Up @@ -75,9 +75,9 @@ function M.fn(path)
end)
:iterate()

if found and view.is_visible() then
if found and explorer.view:is_visible() then
renderer.draw()
view.set_cursor { line, 0 }
explorer.view:set_cursor { line, 0 }
end

running[path_real] = false
Expand Down
15 changes: 11 additions & 4 deletions lua/nvim-tree/actions/fs/remove-file.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local utils = require "nvim-tree.utils"
local events = require "nvim-tree.events"
local view = require "nvim-tree.view"
local lib = require "nvim-tree.lib"
local notify = require "nvim-tree.notify"

Expand All @@ -10,10 +9,14 @@ local M = {

---@param windows integer[]
local function close_windows(windows)
local explorer = require "nvim-tree.core".get_explorer()
if not explorer then
return
end
-- Prevent from closing when the win count equals 1 or 2,
-- where the win to remove could be the last opened.
-- For details see #2503.
if view.View.float.enable and #vim.api.nvim_list_wins() < 3 then
if explorer.view.View.float.enable and #vim.api.nvim_list_wins() < 3 then
return
end

Expand All @@ -26,16 +29,20 @@ end

---@param absolute_path string
local function clear_buffer(absolute_path)
local explorer = require "nvim-tree.core".get_explorer()
if not explorer then
return
end
local bufs = vim.fn.getbufinfo { bufloaded = 1, buflisted = 1 }
for _, buf in pairs(bufs) do
if buf.name == absolute_path then
local tree_winnr = vim.api.nvim_get_current_win()
if buf.hidden == 0 and (#bufs > 1 or view.View.float.enable) then
if buf.hidden == 0 and (#bufs > 1 or explorer.view.View.float.enable) then
vim.api.nvim_set_current_win(buf.windows[1])
vim.cmd ":bn"
end
vim.api.nvim_buf_delete(buf.bufnr, { force = true })
if not view.View.float.quit_on_focus_loss then
if not explorer.view.View.float.quit_on_focus_loss then
vim.api.nvim_set_current_win(tree_winnr)
end
if M.config.actions.remove_file.close_window then
Expand Down
19 changes: 14 additions & 5 deletions lua/nvim-tree/actions/moves/item.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local utils = require "nvim-tree.utils"
local view = require "nvim-tree.view"
local core = require "nvim-tree.core"
local lib = require "nvim-tree.lib"
local explorer_node = require "nvim-tree.explorer.node"
Expand Down Expand Up @@ -60,10 +59,14 @@ local function move(where, what, skip_gitignored)
end
end

local explorer = core.get_explorer()
if not explorer then
return
end
if nex then
view.set_cursor { nex, 0 }
explorer.view:set_cursor { nex, 0 }
elseif vim.o.wrapscan and first then
view.set_cursor { first, 0 }
explorer.view:set_cursor { first, 0 }
end
end

Expand Down Expand Up @@ -182,13 +185,19 @@ local function move_prev_recursive(what, skip_gitignored)

-- 4.3)
if node_init.name == ".." then -- root node
view.set_cursor { 1, 0 } -- move to root node (position 1)
local explorer = core.get_explorer()
if explorer then
explorer.view:set_cursor { 1, 0 } -- move to root node (position 1)
end
else
local node_init_line = utils.find_node_line(node_init)
if node_init_line < 0 then
return
end
view.set_cursor { node_init_line, 0 }
local explorer = core.get_explorer()
if explorer then
explorer.view:set_cursor { node_init_line, 0 } -- move to root node (position 1)
end
end

-- 4.4)
Expand Down
11 changes: 8 additions & 3 deletions lua/nvim-tree/actions/moves/parent.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local renderer = require "nvim-tree.renderer"
local view = require "nvim-tree.view"
local utils = require "nvim-tree.utils"
local core = require "nvim-tree.core"
local lib = require "nvim-tree.lib"
Expand All @@ -21,14 +20,20 @@ function M.fn(should_close)
local parent = utils.get_parent_of_group(node).parent

if not parent or not parent.parent then
return view.set_cursor { 1, 0 }
local explorer = core.get_explorer()
if explorer then
return explorer.view:set_cursor { 1, 0 }
end
end

local _, line = utils.find_node(core.get_explorer().nodes, function(n)
return n.absolute_path == parent.absolute_path
end)

view.set_cursor { line + 1, 0 }
local explorer = core.get_explorer()
if explorer then
explorer.view:set_cursor { line + 1, 0 }
end
if should_close then
parent.open = false
renderer.draw()
Expand Down
45 changes: 34 additions & 11 deletions lua/nvim-tree/actions/node/open-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
local lib = require "nvim-tree.lib"
local notify = require "nvim-tree.notify"
local utils = require "nvim-tree.utils"
local view = require "nvim-tree.view"

local M = {}

Expand All @@ -19,9 +18,11 @@ end
---Get all windows in the current tabpage that aren't NvimTree.
---@return table with valid win_ids
local function usable_win_ids()
local explorer = require "nvim-tree.core".get_explorer()

local tabpage = vim.api.nvim_get_current_tabpage()
local win_ids = vim.api.nvim_tabpage_list_wins(tabpage)
local tree_winid = view.get_winnr(tabpage)
local tree_winid = explorer and explorer.view:get_winnr(tabpage)

return vim.tbl_filter(function(id)
local bufid = vim.api.nvim_win_get_buf(id)
Expand Down Expand Up @@ -188,7 +189,10 @@ end

local function open_file_in_tab(filename)
if M.quit_on_open then
view.close()
local explorer = require "nvim-tree.core".get_explorer()
if explorer then
explorer.view:close()
end
end
if M.relative_path then
filename = utils.path_relative(filename, vim.fn.getcwd())
Expand All @@ -198,7 +202,10 @@ end

local function drop(filename)
if M.quit_on_open then
view.close()
local explorer = require"nvim-tree.core".get_explorer()
if explorer then
explorer.view:close()
end
end
if M.relative_path then
filename = utils.path_relative(filename, vim.fn.getcwd())
Expand All @@ -208,7 +215,10 @@ end

local function tab_drop(filename)
if M.quit_on_open then
view.close()
local explorer = require"nvim-tree.core".get_explorer()
if explorer then
explorer.view:close()
end
end
if M.relative_path then
filename = utils.path_relative(filename, vim.fn.getcwd())
Expand All @@ -229,7 +239,10 @@ local function on_preview(buf_loaded)
once = true,
})
end
view.focus()
local explorer = require"nvim-tree.core".get_explorer()
if explorer then
explorer.view:focus()
end
end

local function get_target_winid(mode)
Expand Down Expand Up @@ -287,7 +300,8 @@ local function open_in_new_window(filename, mode)
end, vim.api.nvim_list_wins())

local create_new_window = #win_ids == 1 -- This implies that the nvim-tree window is the only one
local new_window_side = (view.View.side == "right") and "aboveleft" or "belowright"
local explorer = require"nvim-tree.core".get_explorer()
local new_window_side = (explorer and view.View.side == "right") and "aboveleft" or "belowright"

-- Target is invalid: create new window
if not vim.tbl_contains(win_ids, target_winid) then
Expand Down Expand Up @@ -319,7 +333,7 @@ local function open_in_new_window(filename, mode)
end
end

if (mode == "preview" or mode == "preview_no_picker") and view.View.float.enable then
if (mode == "preview" or mode == "preview_no_picker") and explorer and explorer.view.View.float.enable then
-- ignore "WinLeave" autocmd on preview
-- because the registered "WinLeave"
-- will kill the floating window immediately
Expand Down Expand Up @@ -359,7 +373,10 @@ local function is_already_loaded(filename)
end

local function edit_in_current_buf(filename)
require("nvim-tree.view").abandon_current_window()
local explorer = require"nvim-tree.core".get_explorer()
if explorer then
explorer.view:abandon_current_window()
end
if M.relative_path then
filename = utils.path_relative(filename, vim.fn.getcwd())
end
Expand Down Expand Up @@ -404,15 +421,21 @@ function M.fn(mode, filename)
end

if M.resize_window then
view.resize()
local explorer = require"nvim-tree.core".get_explorer()
if explorer then
explorer.view:resize()
end
end

if mode == "preview" or mode == "preview_no_picker" then
return on_preview(buf_loaded)
end

if M.quit_on_open then
view.close()
local explorer = require"nvim-tree.core".get_explorer()
if explorer then
explorer.view:close()
end
end
end

Expand Down
Loading

0 comments on commit fa051cf

Please sign in to comment.