refactor(#2826): multi instance nvim-tree.view

This commit is contained in:
Mateusz Russak
2024-08-04 13:31:11 +02:00
parent 0f2cda6ce0
commit fa051cf990
22 changed files with 390 additions and 297 deletions

View File

@@ -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 = {}
@@ -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)
@@ -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())
@@ -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())
@@ -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())
@@ -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)
@@ -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
@@ -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
@@ -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
@@ -404,7 +421,10 @@ 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
@@ -412,7 +432,10 @@ function M.fn(mode, filename)
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