refactor(#2826): move view to instanced window class (#3153)

* refactor(#2826): singleton View class, WIP

* refactor(#2826): singleton View class, WIP

* refactor(#2826): singleton View class, WIP

* refactor(#2826): singleton View class, WIP

* refactor(#2826): singleton View class, WIP

* refactor(#2826): singleton View class, WIP

* refactor(#2826): singleton View class, WIP

* refactor(#2826): singleton View class

* refactor(#2826): View is an Explorer member

* refactor(#2826): move autocmds to Explorer

* refactor(#2826): API uses Explorer's View

* refactor(#2826): move View into Explorer package

* refactor(#2826): retain necessary view globals

* refactor(#2826): move all winhl to appearance constants

* refactor(#2826): add lifecycle logging to all Explorer members

* refactor(#2826): fix bad cherry-pick

* refactor(#2826): better enumerate_options function

* refactor(#2826): add View.tab_line for debugging

* refactor(#2826): default lifecycle log off

* refactor(#2826): add experimental.multi_instance_debug, split globals out of view, move diagnostics to its own module

* refactor(#2826): instrument View:get_winnr

* refactor(#2826): instrument View:setup_tabpage

* refactor(#2826): instrument View:set_current_win, View:prevent_buffer_override

* refactor(#2826): instrument View:get_bufnr

* refactor(#2826): track member bufnr -> winid with global

* refactor(#2826): tidy experiment names and logs

* vim: nvim-tree: track bufnr via buffer-update channel

* vim: nvim-tree: more logging

* vim: nvim-tree: revert: track bufnr via buffer-update channel

* refactor(#2826): notify error on view winid and bufnr mismatches

* refactor(#2826): notify error on view winid and bufnr mismatches

* refactor(#2826): explorer init logging
This commit is contained in:
Alexander Courtis
2025-06-19 15:45:55 +10:00
committed by GitHub
parent d54a1875a9
commit 0a06f65bf0
35 changed files with 1249 additions and 830 deletions

View File

@@ -2,7 +2,7 @@
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 core = require("nvim-tree.core")
local M = {}
@@ -19,9 +19,10 @@ 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 = 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, "open-file.usable_win_ids")
return vim.tbl_filter(function(id)
local bufid = vim.api.nvim_win_get_buf(id)
@@ -187,7 +188,10 @@ end
local function open_file_in_tab(filename)
if M.quit_on_open then
view.close()
local explorer = core.get_explorer()
if explorer then
explorer.view:close(nil, "open-file.open_file_in_tab")
end
end
if M.relative_path then
filename = utils.path_relative(filename, vim.fn.getcwd())
@@ -197,7 +201,10 @@ end
local function drop(filename)
if M.quit_on_open then
view.close()
local explorer = core.get_explorer()
if explorer then
explorer.view:close(nil, "open-file.drop")
end
end
if M.relative_path then
filename = utils.path_relative(filename, vim.fn.getcwd())
@@ -207,7 +214,10 @@ end
local function tab_drop(filename)
if M.quit_on_open then
view.close()
local explorer = core.get_explorer()
if explorer then
explorer.view:close(nil, "open-file.tab_drop")
end
end
if M.relative_path then
filename = utils.path_relative(filename, vim.fn.getcwd())
@@ -228,7 +238,10 @@ local function on_preview(buf_loaded)
once = true,
})
end
view.focus()
local explorer = core.get_explorer()
if explorer then
explorer.view:focus()
end
end
local function get_target_winid(mode)
@@ -273,6 +286,8 @@ local function set_current_win_no_autocmd(winid, autocmd)
end
local function open_in_new_window(filename, mode)
local explorer = core.get_explorer()
if type(mode) ~= "string" then
mode = ""
end
@@ -295,7 +310,11 @@ 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 new_window_side = "belowright"
if explorer and (explorer.view.side == "right") then
new_window_side = "aboveleft"
end
-- Target is invalid: create new window
if not vim.tbl_contains(win_ids, target_winid) then
@@ -327,7 +346,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.float.enable then
-- ignore "WinLeave" autocmd on preview
-- because the registered "WinLeave"
-- will kill the floating window immediately
@@ -367,7 +386,12 @@ local function is_already_loaded(filename)
end
local function edit_in_current_buf(filename)
require("nvim-tree.view").abandon_current_window()
local explorer = 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
@@ -378,6 +402,8 @@ end
---@param filename string
---@return nil
function M.fn(mode, filename)
local explorer = core.get_explorer()
if type(mode) ~= "string" then
mode = ""
end
@@ -412,16 +438,16 @@ function M.fn(mode, filename)
vim.bo.bufhidden = ""
end
if M.resize_window then
view.resize()
if M.resize_window and explorer then
explorer.view:resize()
end
if mode == "preview" or mode == "preview_no_picker" then
return on_preview(buf_loaded)
end
if M.quit_on_open then
view.close()
if M.quit_on_open and explorer then
explorer.view:close(nil, "open-file.fn")
end
end