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
No known key found for this signature in database
GPG key ID: D3C8879C12AC0A62
22 changed files with 390 additions and 297 deletions

View file

@ -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"
@ -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
@ -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
@ -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,
})
@ -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,
})
@ -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,
})
@ -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()
@ -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)

View file

@ -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"
@ -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
@ -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

View file

@ -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"
@ -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
@ -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

View file

@ -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"
@ -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
@ -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)

View file

@ -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"
@ -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()

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

View file

@ -1,5 +1,4 @@
local git = require "nvim-tree.git"
local view = require "nvim-tree.view"
local renderer = require "nvim-tree.renderer"
local explorer_module = require "nvim-tree.explorer"
local core = require "nvim-tree.core"
@ -43,14 +42,15 @@ end
local event_running = false
function M.reload_explorer()
if event_running or not core.get_explorer() or vim.v.exiting ~= vim.NIL then
local explorer = core.get_explorer()
if event_running or not explorer or vim.v.exiting ~= vim.NIL then
return
end
event_running = true
local projects = git.reload()
refresh_nodes(core.get_explorer(), projects)
if view.is_visible() then
if explorer.view:is_visible() then
renderer.draw()
end
event_running = false

View file

@ -1,6 +1,5 @@
local core = require "nvim-tree.core"
local lib = require "nvim-tree.lib"
local view = require "nvim-tree.view"
local finders_find_file = require "nvim-tree.actions.finders.find-file"
local M = {}
@ -41,11 +40,12 @@ function M.fn(opts)
return
end
if view.is_visible() then
local explorer = core.get_explorer()
if explorer and explorer.view:is_visible() then
-- focus
if opts.focus then
lib.set_target_win()
view.focus()
explorer.view:focus()
end
elseif opts.open then
-- open

View file

@ -1,5 +1,4 @@
local lib = require "nvim-tree.lib"
local view = require "nvim-tree.view"
local finders_find_file = require "nvim-tree.actions.finders.find-file"
local M = {}
@ -23,10 +22,11 @@ function M.fn(opts)
opts.path = nil
end
if view.is_visible() then
local explorer = require"nvim-tree.core".get_explorer()
if explorer and explorer.view:is_visible() then
-- focus
lib.set_target_win()
view.focus()
explorer.view:focus()
else
-- open
lib.open {

View file

@ -1,14 +1,18 @@
local view = require "nvim-tree.view"
local M = {}
---Resize the tree, persisting the new size.
---@param opts ApiTreeResizeOpts|nil
function M.fn(opts)
local explorer = require"nvim-tree.core".get_explorer()
if not explorer then
return
end
if opts == nil then
-- reset to config values
view.configure_width()
view.resize()
explorer.view:configure_width()
explorer.view:resize()
return
end
@ -16,19 +20,19 @@ function M.fn(opts)
local width_cfg = options.width
if width_cfg ~= nil then
view.configure_width(width_cfg)
view.resize()
explorer.view:configure_width(width_cfg)
explorer.view:resize()
return
end
if not view.is_width_determined() then
if not explorer.view:is_width_determined() then
-- {absolute} and {relative} do nothing when {width} is a function.
return
end
local absolute = options.absolute
if type(absolute) == "number" then
view.resize(absolute)
explorer.view:resize(absolute)
return
end
@ -39,7 +43,7 @@ function M.fn(opts)
relative_size = "+" .. relative_size
end
view.resize(relative_size)
explorer.view:resize(relative_size)
return
end
end

View file

@ -1,5 +1,4 @@
local lib = require "nvim-tree.lib"
local view = require "nvim-tree.view"
local finders_find_file = require "nvim-tree.actions.finders.find-file"
local M = {}
@ -40,9 +39,14 @@ function M.fn(opts, no_focus, cwd, bang)
opts.path = nil
end
if view.is_visible() then
local explorer = require"nvim-tree.core".get_explorer()
if not explorer then
return
end
if explorer.view:is_visible() then
-- close
view.close()
explorer.view:close()
else
-- open
lib.open {

View file

@ -1,6 +1,5 @@
local lib = require "nvim-tree.lib"
local core = require "nvim-tree.core"
local view = require "nvim-tree.view"
local utils = require "nvim-tree.utils"
local actions = require "nvim-tree.actions"
local appearance_diagnostics = require "nvim-tree.appearance.diagnostics"
@ -121,9 +120,9 @@ Api.tree.focus = Api.tree.open
---@field focus boolean|nil default true
Api.tree.toggle = wrap(actions.tree.toggle.fn)
Api.tree.close = wrap(view.close)
Api.tree.close_in_this_tab = wrap(view.close_this_tab_only)
Api.tree.close_in_all_tabs = wrap(view.close_all_tabs)
Api.tree.close = wrap_explorer_member("view", "close")
Api.tree.close_in_this_tab = wrap_explorer_member("view", "close_this_tab_only")
Api.tree.close_in_all_tabs = wrap_explorer_member("view", "close_all_tabs")
Api.tree.reload = wrap(actions.reloaders.reload_explorer)
---@class ApiTreeResizeOpts
@ -175,12 +174,12 @@ Api.tree.is_tree_buf = wrap(utils.is_nvim_tree_buf)
---@field tabpage number|nil
---@field any_tabpage boolean|nil default false
Api.tree.is_visible = wrap(view.is_visible)
Api.tree.is_visible = wrap_explorer_member("view", "is_visible")
---@class ApiTreeWinIdOpts
---@field tabpage number|nil default nil
Api.tree.winid = wrap(view.winid)
Api.tree.winid = wrap_explorer_member("view", "winid")
Api.fs.create = wrap_node_or_nil(actions.fs.create_file.fn)
Api.fs.remove = wrap_node(actions.fs.remove_file.fn)

View file

@ -1,5 +1,4 @@
local api = require "nvim-tree.api"
local view = require "nvim-tree.view"
local M = {}
@ -111,7 +110,11 @@ local CMDS = {
bar = true,
},
command = function(c)
view.resize(c.args)
local explorer = require "nvim-tree.core".get_explorer();
if not explorer then
return
end
explorer.view:resize(c.args)
end,
},
{

View file

@ -1,6 +1,5 @@
local events = require "nvim-tree.events"
local explorer = require "nvim-tree.explorer"
local view = require "nvim-tree.view"
local log = require "nvim-tree.log"
local M = {}
@ -41,7 +40,7 @@ end
---@return integer
function M.get_nodes_starting_line()
local offset = 1
if view.is_root_folder_visible(M.get_cwd()) then
if TreeExplorer and TreeExplorer.view:is_root_folder_visible(M.get_cwd()) then
offset = offset + 1
end
if TreeExplorer and TreeExplorer.live_filter.filter then

View file

@ -1,5 +1,4 @@
local utils = require "nvim-tree.utils"
local view = require "nvim-tree.view"
local log = require "nvim-tree.log"
local M = {}
@ -164,7 +163,9 @@ function M.update()
end
end
log.profile_end(profile)
if view.is_buf_valid(view.get_bufnr()) then
local explorer = require "nvim-tree.core".get_explorer()
if explorer and explorer.view:is_buf_valid(explorer.view:get_bufnr()) then
require("nvim-tree.renderer").draw()
end
end)

View file

@ -6,6 +6,7 @@ local Filters = require "nvim-tree.explorer.filters"
local Marks = require "nvim-tree.marks"
local LiveFilter = require "nvim-tree.explorer.live-filter"
local Sorters = require "nvim-tree.explorer.sorters"
local View = require "nvim-tree.explorer.view"
local M = {}
@ -46,6 +47,7 @@ function Explorer.new(path)
open = true,
marks = Marks:new(),
sorters = Sorters:new(M.config),
view = View:new(M.config),
}, Explorer)
explorer.watcher = watch.create_watcher(explorer)
explorer.filters = Filters:new(M.config, explorer)

View file

@ -1,4 +1,3 @@
local view = require "nvim-tree.view"
local utils = require "nvim-tree.utils"
local Iterator = require "nvim-tree.iterators.node-iterator"
@ -47,14 +46,14 @@ local overlay_bufnr = 0
local overlay_winnr = 0
local function remove_overlay(self)
if view.View.float.enable and view.View.float.quit_on_focus_loss then
if self.explorer.view.View.float.enable and self.explorer.view.View.float.quit_on_focus_loss then
-- return to normal nvim-tree float behaviour when filter window is closed
vim.api.nvim_create_autocmd("WinLeave", {
pattern = "NvimTree_*",
group = vim.api.nvim_create_augroup("NvimTree", { clear = false }),
callback = function()
if utils.is_nvim_tree_buf(0) then
view.close()
self.explorer.view:close()
end
end,
})
@ -141,7 +140,7 @@ end
---@return integer
local function calculate_overlay_win_width(self)
local wininfo = vim.fn.getwininfo(view.get_winnr())[1]
local wininfo = vim.fn.getwininfo(self.explorer.view:get_winnr())[1]
if wininfo then
return wininfo.width - wininfo.textoff - #self.prefix
@ -151,7 +150,7 @@ local function calculate_overlay_win_width(self)
end
local function create_overlay(self)
if view.View.float.enable then
if self.explorer.view.View.float.enable then
-- don't close nvim-tree float when focus is changed to filter window
vim.api.nvim_clear_autocmds {
event = "WinLeave",
@ -183,13 +182,13 @@ local function create_overlay(self)
end
function LiveFilter:start_filtering()
view.View.live_filter.prev_focused_node = require("nvim-tree.lib").get_node_at_cursor()
self.explorer.view.View.live_filter.prev_focused_node = require("nvim-tree.lib").get_node_at_cursor()
self.filter = self.filter or ""
redraw()
local row = require("nvim-tree.core").get_nodes_starting_line() - 1
local col = #self.prefix > 0 and #self.prefix - 1 or 1
view.set_cursor { row, col }
self.explorer.view:set_cursor { row, col }
-- needs scheduling to let the cursor move before initializing the window
vim.schedule(function()
return create_overlay(self)
@ -198,7 +197,7 @@ end
function LiveFilter:clear_filter()
local node = require("nvim-tree.lib").get_node_at_cursor()
local last_node = view.View.live_filter.prev_focused_node
local last_node = self.explorer.view.View.live_filter.prev_focused_node
self.filter = nil
reset_filter(self)

View file

@ -2,57 +2,77 @@ local events = require "nvim-tree.events"
local utils = require "nvim-tree.utils"
local log = require "nvim-tree.log"
---@class OpenInWinOpts
---@field hijack_current_buf boolean|nil default true
---@field resize boolean|nil default true
---@field winid number|nil 0 or nil for current
local M = {}
local ExplorerView = {}
local DEFAULT_MIN_WIDTH = 30
local DEFAULT_MAX_WIDTH = -1
local DEFAULT_PADDING = 1
M.View = {
adaptive_size = false,
centralize_selection = false,
tabpages = {},
cursors = {},
hide_root_folder = false,
live_filter = {
prev_focused_node = nil,
},
winopts = {
relativenumber = false,
number = false,
list = false,
foldenable = false,
winfixwidth = true,
winfixheight = true,
spell = false,
signcolumn = "yes",
foldmethod = "manual",
foldcolumn = "0",
cursorcolumn = false,
cursorline = true,
cursorlineopt = "both",
colorcolumn = "0",
wrap = false,
winhl = table.concat({
"EndOfBuffer:NvimTreeEndOfBuffer",
"CursorLine:NvimTreeCursorLine",
"CursorLineNr:NvimTreeCursorLineNr",
"LineNr:NvimTreeLineNr",
"WinSeparator:NvimTreeWinSeparator",
"StatusLine:NvimTreeStatusLine",
"StatusLineNC:NvimTreeStatuslineNC",
"SignColumn:NvimTreeSignColumn",
"Normal:NvimTreeNormal",
"NormalNC:NvimTreeNormalNC",
"NormalFloat:NvimTreeNormalFloat",
}, ","),
},
}
function ExplorerView:new(opts)
local o = {
View = {
adaptive_size = false,
centralize_selection = false,
tabpages = {},
cursors = {},
hide_root_folder = false,
live_filter = {
prev_focused_node = nil,
},
winopts = {
relativenumber = false,
number = false,
list = false,
foldenable = false,
winfixwidth = true,
winfixheight = true,
spell = false,
signcolumn = "yes",
foldmethod = "manual",
foldcolumn = "0",
cursorcolumn = false,
cursorline = true,
cursorlineopt = "both",
colorcolumn = "0",
wrap = false,
winhl = table.concat({
"EndOfBuffer:NvimTreeEndOfBuffer",
"CursorLine:NvimTreeCursorLine",
"CursorLineNr:NvimTreeCursorLineNr",
"LineNr:NvimTreeLineNr",
"WinSeparator:NvimTreeWinSeparator",
"StatusLine:NvimTreeStatusLine",
"StatusLineNC:NvimTreeStatuslineNC",
"SignColumn:NvimTreeSignColumn",
"Normal:NvimTreeNormal",
"NormalNC:NvimTreeNormalNC",
"NormalFloat:NvimTreeNormalFloat",
}, ","),
},
}
}
local options = opts.view or {}
o.View.centralize_selection = options.centralize_selection
o.View.side = (options.side == "right") and "right" or "left"
o.View.height = options.height
o.View.hide_root_folder = opts.renderer.root_folder_label == false
o.View.tab = opts.tab
o.View.preserve_window_proportions = options.preserve_window_proportions
o.View.winopts.cursorline = options.cursorline
o.View.winopts.number = options.number
o.View.winopts.relativenumber = options.relativenumber
o.View.winopts.signcolumn = options.signcolumn
o.View.float = options.float
o.on_attach = opts.on_attach
o.config = vim.deepcopy(options)
setmetatable(o, self)
self.__index = self
o:configure_width(options.width)
o.View.initial_width = o:get_width()
end
-- The initial state of a tab
local tabinitial = {
@ -92,20 +112,20 @@ local function wipe_rogue_buffer()
end
---@param bufnr integer|boolean|nil
local function create_buffer(bufnr)
local function create_buffer(self, bufnr)
wipe_rogue_buffer()
local tab = vim.api.nvim_get_current_tabpage()
BUFNR_PER_TAB[tab] = bufnr or vim.api.nvim_create_buf(false, false)
vim.api.nvim_buf_set_name(M.get_bufnr(), "NvimTree_" .. tab)
vim.api.nvim_buf_set_name(self:get_bufnr(), "NvimTree_" .. tab)
for option, value in pairs(BUFFER_OPTIONS) do
vim.bo[M.get_bufnr()][option] = value
vim.bo[self:get_bufnr()][option] = value
end
require("nvim-tree.keymap").on_attach(M.get_bufnr())
require("nvim-tree.keymap").on_attach(self:get_bufnr())
events._dispatch_tree_attached_post(M.get_bufnr())
events._dispatch_tree_attached_post(self:get_bufnr())
end
---@param size (fun():integer)|integer|string
@ -122,11 +142,11 @@ local function get_size(size)
end
---@param size (fun():integer)|integer|nil
local function get_width(size)
function ExplorerView:get_width(size)
if size then
return get_size(size)
else
return get_size(M.View.width)
return get_size(self.View.width)
end
end
@ -137,39 +157,39 @@ local move_tbl = {
-- setup_tabpage sets up the initial state of a tab
---@param tabpage integer
local function setup_tabpage(tabpage)
local function setup_tabpage(self, tabpage)
local winnr = vim.api.nvim_get_current_win()
M.View.tabpages[tabpage] = vim.tbl_extend("force", M.View.tabpages[tabpage] or tabinitial, { winnr = winnr })
self.View.tabpages[tabpage] = vim.tbl_extend("force", self.View.tabpages[tabpage] or tabinitial, { winnr = winnr })
end
local function set_window_options_and_buffer()
pcall(vim.api.nvim_command, "buffer " .. M.get_bufnr())
local function set_window_options_and_buffer(self)
pcall(vim.api.nvim_command, "buffer " .. self:get_bufnr())
local eventignore = vim.opt.eventignore:get()
vim.opt.eventignore = "all"
for k, v in pairs(M.View.winopts) do
for k, v in pairs(self.View.winopts) do
vim.opt_local[k] = v
end
vim.opt.eventignore = eventignore
end
---@return table
local function open_win_config()
if type(M.View.float.open_win_config) == "function" then
return M.View.float.open_win_config()
local function open_win_config(self)
if type(self.View.float.open_win_config) == "function" then
return self.View.float.open_win_config(self)
else
return M.View.float.open_win_config
return self.View.float.open_win_config
end
end
local function open_window()
if M.View.float.enable then
vim.api.nvim_open_win(0, true, open_win_config())
local function open_window(self)
if self.View.float.enable then
vim.api.nvim_open_win(0, true, open_win_config(self))
else
vim.api.nvim_command "vsp"
M.reposition_window()
self:reposition_window()
end
setup_tabpage(vim.api.nvim_get_current_tabpage())
set_window_options_and_buffer()
setup_tabpage(self, vim.api.nvim_get_current_tabpage())
set_window_options_and_buffer(self)
end
---@param buf integer
@ -205,19 +225,19 @@ end
-- save_tab_state saves any state that should be preserved across redraws.
---@param tabnr integer
local function save_tab_state(tabnr)
local function save_tab_state(self, tabnr)
local tabpage = tabnr or vim.api.nvim_get_current_tabpage()
M.View.cursors[tabpage] = vim.api.nvim_win_get_cursor(M.get_winnr(tabpage) or 0)
self.View.cursors[tabpage] = vim.api.nvim_win_get_cursor(self:get_winnr(tabpage) or 0)
end
---@param tabpage integer
local function close(tabpage)
if not M.is_visible { tabpage = tabpage } then
local function close(self, tabpage)
if not self:is_visible { tabpage = tabpage } then
return
end
save_tab_state(tabpage)
save_tab_state(self, tabpage)
switch_buf_if_last_buf()
local tree_win = M.get_winnr(tabpage)
local tree_win = self:get_winnr(tabpage)
local current_win = vim.api.nvim_get_current_win()
for _, win in pairs(vim.api.nvim_tabpage_list_wins(tabpage)) do
if vim.api.nvim_win_get_config(win).relative == "" then
@ -234,35 +254,35 @@ local function close(tabpage)
end
end
function M.close_this_tab_only()
close(vim.api.nvim_get_current_tabpage())
function ExplorerView:close_this_tab_only()
close(self, vim.api.nvim_get_current_tabpage())
end
function M.close_all_tabs()
for tabpage, _ in pairs(M.View.tabpages) do
close(tabpage)
function ExplorerView:close_all_tabs()
for tabpage, _ in pairs(self.View.tabpages) do
close(self, tabpage)
end
end
function M.close()
if M.View.tab.sync.close then
M.close_all_tabs()
function ExplorerView:close()
if self.View.tab.sync.close then
self:close_all_tabs()
else
M.close_this_tab_only()
self:close_this_tab_only()
end
end
---@param options table|nil
function M.open(options)
if M.is_visible() then
function ExplorerView:open(options)
if self:is_visible() then
return
end
local profile = log.profile_start "view open"
create_buffer()
open_window()
M.resize()
create_buffer(self)
open_window(self)
self:resize()
local opts = options or { focus_tree = true }
if not opts.focus_tree then
@ -273,26 +293,26 @@ function M.open(options)
log.profile_end(profile)
end
local function grow()
local starts_at = M.is_root_folder_visible(require("nvim-tree.core").get_cwd()) and 1 or 0
local lines = vim.api.nvim_buf_get_lines(M.get_bufnr(), starts_at, -1, false)
local function grow(self)
local starts_at = self:is_root_folder_visible(require("nvim-tree.core").get_cwd()) and 1 or 0
local lines = vim.api.nvim_buf_get_lines(self:get_bufnr(), starts_at, -1, false)
-- number of columns of right-padding to indicate end of path
local padding = get_size(M.View.padding)
local padding = get_size(self.View.padding)
-- account for sign/number columns etc.
local wininfo = vim.fn.getwininfo(M.get_winnr())
local wininfo = vim.fn.getwininfo(self:get_winnr())
if type(wininfo) == "table" and type(wininfo[1]) == "table" then
padding = padding + wininfo[1].textoff
end
local resizing_width = M.View.initial_width - padding
local resizing_width = self.View.initial_width - padding
local max_width
-- maybe bound max
if M.View.max_width == -1 then
if self.View.max_width == -1 then
max_width = -1
else
max_width = get_width(M.View.max_width) - padding
max_width = self:get_width(self.View.max_width) - padding
end
for _, l in pairs(lines) do
@ -300,23 +320,23 @@ local function grow()
if resizing_width < count then
resizing_width = count
end
if M.View.adaptive_size and max_width >= 0 and resizing_width >= max_width then
if self.View.adaptive_size and max_width >= 0 and resizing_width >= max_width then
resizing_width = max_width
break
end
end
M.resize(resizing_width + padding)
self:resize(resizing_width + padding)
end
function M.grow_from_content()
if M.View.adaptive_size then
grow()
function ExplorerView:grow_from_content()
if self.View.adaptive_size then
grow(self)
end
end
---@param size string|number|nil
function M.resize(size)
if M.View.float.enable and not M.View.adaptive_size then
function ExplorerView:resize(size)
if self.View.float.enable and not self.View.adaptive_size then
-- if the floating windows's adaptive size is not desired, then the
-- float size should be defined in view.float.open_win_config
return
@ -328,7 +348,7 @@ function M.resize(size)
size = tonumber(size)
if first_char == "+" or first_char == "-" then
size = M.View.width + size
size = self.View.width + size
end
end
@ -337,81 +357,81 @@ function M.resize(size)
end
if size then
M.View.width = size
M.View.height = size
self.View.width = size
self.View.height = size
end
if not M.is_visible() then
if not self:is_visible() then
return
end
local new_size = get_width()
vim.api.nvim_win_set_width(M.get_winnr() or 0, new_size)
local new_size = self:get_width()
vim.api.nvim_win_set_width(self:get_winnr() or 0, new_size)
events._dispatch_on_tree_resize(new_size)
if not M.View.preserve_window_proportions then
if not self.View.preserve_window_proportions then
vim.cmd ":wincmd ="
end
end
function M.reposition_window()
local move_to = move_tbl[M.View.side]
function ExplorerView:reposition_window()
local move_to = move_tbl[self.View.side]
vim.api.nvim_command("wincmd " .. move_to)
M.resize()
self:resize()
end
local function set_current_win()
local function set_current_win(self)
local current_tab = vim.api.nvim_get_current_tabpage()
M.View.tabpages[current_tab].winnr = vim.api.nvim_get_current_win()
self.View.tabpages[current_tab].winnr = vim.api.nvim_get_current_win()
end
---Open the tree in the a window
---@param opts OpenInWinOpts|nil
function M.open_in_win(opts)
function ExplorerView:open_in_win(opts)
opts = opts or { hijack_current_buf = true, resize = true }
if opts.winid and vim.api.nvim_win_is_valid(opts.winid) then
vim.api.nvim_set_current_win(opts.winid)
end
create_buffer(opts.hijack_current_buf and vim.api.nvim_get_current_buf())
setup_tabpage(vim.api.nvim_get_current_tabpage())
set_current_win()
set_window_options_and_buffer()
create_buffer(self, opts.hijack_current_buf and vim.api.nvim_get_current_buf())
setup_tabpage(self, vim.api.nvim_get_current_tabpage())
set_current_win(self)
set_window_options_and_buffer(self)
if opts.resize then
M.reposition_window()
M.resize()
self:reposition_window()
self:resize()
end
end
function M.abandon_current_window()
function ExplorerView:abandon_current_window()
local tab = vim.api.nvim_get_current_tabpage()
BUFNR_PER_TAB[tab] = nil
if M.View.tabpages[tab] then
M.View.tabpages[tab].winnr = nil
if self.View.tabpages[tab] then
self.View.tabpages[tab].winnr = nil
end
end
function M.abandon_all_windows()
function ExplorerView:abandon_all_windows()
for tab, _ in pairs(vim.api.nvim_list_tabpages()) do
BUFNR_PER_TAB[tab] = nil
if M.View.tabpages[tab] then
M.View.tabpages[tab].winnr = nil
if self.View.tabpages[tab] then
self.View.tabpages[tab].winnr = nil
end
end
end
---@param opts table|nil
function M.is_visible(opts)
function ExplorerView:is_visible(opts)
if opts and opts.tabpage then
if M.View.tabpages[opts.tabpage] == nil then
if self.View.tabpages[opts.tabpage] == nil then
return false
end
local winnr = M.View.tabpages[opts.tabpage].winnr
local winnr = self.View.tabpages[opts.tabpage].winnr
return winnr and vim.api.nvim_win_is_valid(winnr)
end
if opts and opts.any_tabpage then
for _, v in pairs(M.View.tabpages) do
for _, v in pairs(self.View.tabpages) do
if v.winnr and vim.api.nvim_win_is_valid(v.winnr) then
return true
end
@ -419,27 +439,27 @@ function M.is_visible(opts)
return false
end
return M.get_winnr() ~= nil and vim.api.nvim_win_is_valid(M.get_winnr() or 0)
return self:get_winnr() ~= nil and vim.api.nvim_win_is_valid(self:get_winnr() or 0)
end
---@param opts table|nil
function M.set_cursor(opts)
if M.is_visible() then
pcall(vim.api.nvim_win_set_cursor, M.get_winnr(), opts)
function ExplorerView:set_cursor(opts)
if self:is_visible() then
pcall(vim.api.nvim_win_set_cursor, self:get_winnr(), opts)
end
end
---@param winnr number|nil
---@param open_if_closed boolean|nil
function M.focus(winnr, open_if_closed)
local wnr = winnr or M.get_winnr()
function ExplorerView:focus(winnr, open_if_closed)
local wnr = winnr or self:get_winnr()
if vim.api.nvim_win_get_tabpage(wnr or 0) ~= vim.api.nvim_win_get_tabpage(0) then
M.close()
M.open()
wnr = M.get_winnr()
elseif open_if_closed and not M.is_visible() then
M.open()
self:close()
self:open()
wnr = self:get_winnr()
elseif open_if_closed and not self:is_visible() then
self:open()
end
if wnr then
@ -450,30 +470,30 @@ end
--- Retrieve the winid of the open tree.
---@param opts ApiTreeWinIdOpts|nil
---@return number|nil winid unlike get_winnr(), this returns nil if the nvim-tree window is not visible
function M.winid(opts)
function ExplorerView:winid(opts)
local tabpage = opts and opts.tabpage
if tabpage == 0 then
tabpage = vim.api.nvim_get_current_tabpage()
end
if M.is_visible { tabpage = tabpage } then
return M.get_winnr(tabpage)
if self:is_visible { tabpage = tabpage } then
return self:get_winnr(tabpage)
else
return nil
end
end
--- Restores the state of a NvimTree window if it was initialized before.
function M.restore_tab_state()
function ExplorerView:restore_tab_state()
local tabpage = vim.api.nvim_get_current_tabpage()
M.set_cursor(M.View.cursors[tabpage])
self:set_cursor(self.View.cursors[tabpage])
end
--- Returns the window number for nvim-tree within the tabpage specified
---@param tabpage number|nil (optional) the number of the chosen tabpage. Defaults to current tabpage.
---@return number|nil
function M.get_winnr(tabpage)
function ExplorerView:get_winnr(tabpage)
tabpage = tabpage or vim.api.nvim_get_current_tabpage()
local tabinfo = M.View.tabpages[tabpage]
local tabinfo = self.View.tabpages[tabpage]
if tabinfo and tabinfo.winnr and vim.api.nvim_win_is_valid(tabinfo.winnr) then
return tabinfo.winnr
end
@ -481,19 +501,19 @@ end
--- Returns the current nvim tree bufnr
---@return number
function M.get_bufnr()
function ExplorerView:get_bufnr()
return BUFNR_PER_TAB[vim.api.nvim_get_current_tabpage()]
end
---@param bufnr number
---@return boolean
function M.is_buf_valid(bufnr)
function ExplorerView:is_buf_valid(bufnr)
return bufnr and vim.api.nvim_buf_is_valid(bufnr) and vim.api.nvim_buf_is_loaded(bufnr)
end
function M._prevent_buffer_override()
local view_winnr = M.get_winnr()
local view_bufnr = M.get_bufnr()
function ExplorerView:_prevent_buffer_override()
local view_winnr = self:get_winnr()
local view_bufnr = self:get_bufnr()
-- need to schedule to let the new buffer populate the window
-- because this event needs to be run on bufWipeout.
@ -505,7 +525,7 @@ function M._prevent_buffer_override()
local bufname = vim.api.nvim_buf_get_name(curbuf)
if not bufname:match "NvimTree" then
for i, tabpage in ipairs(M.View.tabpages) do
for i, tabpage in ipairs(self.View.tabpages) do
if tabpage.winnr == view_winnr then
M.View.tabpages[i] = nil
break
@ -536,65 +556,44 @@ end
---@param cwd string|nil
---@return boolean
function M.is_root_folder_visible(cwd)
return cwd ~= "/" and not M.View.hide_root_folder
function ExplorerView:is_root_folder_visible(cwd)
return cwd ~= "/" and not self.View.hide_root_folder
end
-- used on ColorScheme event
function M.reset_winhl()
local winnr = M.get_winnr()
function ExplorerView:reset_winhl()
local winnr = self:get_winnr()
if winnr and vim.api.nvim_win_is_valid(winnr) then
vim.wo[M.get_winnr()].winhl = M.View.winopts.winhl
vim.wo[self:get_winnr()].winhl = self.View.winopts.winhl
end
end
---Check if width determined or calculated on-fly
---@return boolean
function M.is_width_determined()
return type(M.View.width) ~= "function"
function ExplorerView:is_width_determined()
return type(self.View.width) ~= "function"
end
---Configure width-related config
---@param width string|function|number|table|nil
function M.configure_width(width)
function ExplorerView:configure_width(width)
if type(width) == "table" then
M.View.adaptive_size = true
M.View.width = width.min or DEFAULT_MIN_WIDTH
M.View.max_width = width.max or DEFAULT_MAX_WIDTH
M.View.padding = width.padding or DEFAULT_PADDING
self.View.adaptive_size = true
self.View.width = width.min or DEFAULT_MIN_WIDTH
self.View.max_width = width.max or DEFAULT_MAX_WIDTH
self.View.padding = width.padding or DEFAULT_PADDING
elseif width == nil then
if M.config.width ~= nil then
if self.config.width ~= nil then
-- if we had input config - fallback to it
M.configure_width(M.config.width)
self.configure_width(self.config.width)
else
-- otherwise - restore initial width
M.View.width = M.View.initial_width
self.View.width = self.View.initial_width
end
else
M.View.adaptive_size = false
M.View.width = width
self.View.adaptive_size = false
self.View.width = width
end
end
function M.setup(opts)
local options = opts.view or {}
M.View.centralize_selection = options.centralize_selection
M.View.side = (options.side == "right") and "right" or "left"
M.View.height = options.height
M.View.hide_root_folder = opts.renderer.root_folder_label == false
M.View.tab = opts.tab
M.View.preserve_window_proportions = options.preserve_window_proportions
M.View.winopts.cursorline = options.cursorline
M.View.winopts.number = options.number
M.View.winopts.relativenumber = options.relativenumber
M.View.winopts.signcolumn = options.signcolumn
M.View.float = options.float
M.on_attach = opts.on_attach
M.config = options
M.configure_width(options.width)
M.View.initial_width = get_width()
end
return M
return ExplorerView

View file

@ -1,5 +1,4 @@
local renderer = require "nvim-tree.renderer"
local view = require "nvim-tree.view"
local core = require "nvim-tree.core"
local utils = require "nvim-tree.utils"
local events = require "nvim-tree.events"
@ -17,11 +16,12 @@ local M = {
---@return Node|nil
function M.get_node_at_cursor()
if not core.get_explorer() then
local explorer = core.get_explorer()
if not explorer then
return
end
local winnr = view.get_winnr()
local winnr = explorer.view:get_winnr()
if not winnr then
return
end
@ -29,7 +29,7 @@ function M.get_node_at_cursor()
local cursor = vim.api.nvim_win_get_cursor(winnr)
local line = cursor[1]
if line == 1 and view.is_root_folder_visible(core.get_cwd()) then
if line == 1 and explorer.view:is_root_folder_visible(core.get_cwd()) then
return { name = ".." }
end
@ -189,8 +189,12 @@ local function handle_buf_cwd(cwd)
end
local function open_view_and_draw()
local explorer = core.get_explorer()
if not explorer then
return
end
local cwd = vim.fn.getcwd()
view.open()
explorer.view:open()
handle_buf_cwd(cwd)
renderer.draw()
end
@ -261,20 +265,24 @@ function M.open(opts)
core.init(cwd)
end
end
local explorer = core.get_explorer()
if not explorer then
return
end
if should_hijack_current_buf() then
view.close_this_tab_only()
view.open_in_win()
explorer.view:close_this_tab_only()
explorer.view:open_in_win()
renderer.draw()
elseif opts.winid then
view.open_in_win { hijack_current_buf = false, resize = false, winid = opts.winid }
explorer.view:open_in_win { hijack_current_buf = false, resize = false, winid = opts.winid }
renderer.draw()
elseif opts.current_window then
view.open_in_win { hijack_current_buf = false, resize = false }
explorer.view:open_in_win { hijack_current_buf = false, resize = false }
renderer.draw()
else
open_view_and_draw()
end
view.restore_tab_state()
explorer.view:restore_tab_state()
events._dispatch_on_tree_open()
end

View file

@ -1,7 +1,6 @@
local core = require "nvim-tree.core"
local notify = require "nvim-tree.notify"
local utils = require "nvim-tree.utils"
local view = require "nvim-tree.view"
local DecoratorBookmarks = require "nvim-tree.renderer.decorator.bookmarks"
local DecoratorCopied = require "nvim-tree.renderer.decorator.copied"
@ -408,14 +407,17 @@ end
---@private
function Builder:build_header()
local explorer = core.get_explorer()
if view.is_root_folder_visible(core.get_cwd()) then
if not explorer then
return
end
if explorer.view:is_root_folder_visible(core.get_cwd()) then
local root_name = self:format_root_name(M.opts.renderer.root_folder_label)
table.insert(self.lines, root_name)
self:insert_highlight({ "NvimTreeRootFolder" }, 0, string.len(root_name))
self.index = 1
end
if explorer and explorer.live_filter.filter then
if explorer.live_filter.filter then
local filter_line = string.format("%s/%s/", M.opts.live_filter.prefix, explorer.live_filter.filter)
table.insert(self.lines, filter_line)
local prefix_length = string.len(M.opts.live_filter.prefix)

View file

@ -1,6 +1,5 @@
local core = require "nvim-tree.core"
local log = require "nvim-tree.log"
local view = require "nvim-tree.view"
local events = require "nvim-tree.events"
local _padding = require "nvim-tree.renderer.components.padding"
@ -67,14 +66,18 @@ function M.render_hl(bufnr, hl)
end
function M.draw()
local bufnr = view.get_bufnr()
if not core.get_explorer() or not bufnr or not vim.api.nvim_buf_is_loaded(bufnr) then
local explorer = core.get_explorer()
if not explorer then
return
end
local bufnr = explorer.view:get_bufnr()
if not bufnr or not vim.api.nvim_buf_is_loaded(bufnr) then
return
end
local profile = log.profile_start "draw"
local cursor = vim.api.nvim_win_get_cursor(view.get_winnr() or 0)
local cursor = vim.api.nvim_win_get_cursor(explorer.view:get_winnr() or 0)
icon_component.reset_config()
local builder = Builder:new():build()
@ -82,14 +85,14 @@ function M.draw()
_draw(bufnr, builder.lines, builder.hl_args, builder.signs, builder.extmarks)
if cursor and #builder.lines >= cursor[1] then
vim.api.nvim_win_set_cursor(view.get_winnr() or 0, cursor)
vim.api.nvim_win_set_cursor(explorer.view:get_winnr() or 0, cursor)
end
view.grow_from_content()
explorer.view:grow_from_content()
log.profile_end(profile)
events._dispatch_on_tree_rendered(bufnr, view.get_winnr())
events._dispatch_on_tree_rendered(bufnr, explorer.view:get_winnr())
end
function M.setup(opts)

View file

@ -111,8 +111,8 @@ function M.find_node(nodes, fn)
return node.group_next and { node.group_next } or (node.open and #node.nodes > 0 and node.nodes)
end)
:iterate()
i = require("nvim-tree.view").is_root_folder_visible() and i or i - 1
local explorer = require("nvim-tree.core").get_explorer()
i = explorer and explorer.view:is_root_folder_visible() and i or i - 1
if explorer and explorer.live_filter.filter then
i = i + 1
end
@ -444,10 +444,14 @@ function M.debounce(context, timeout, callback)
end
function M.focus_file(path)
local _, i = M.find_node(require("nvim-tree.core").get_explorer().nodes, function(node)
local explorer = require("nvim-tree.core").get_explorer()
if not explorer then
return
end
local _, i = M.find_node(explorer.nodes, function(node)
return node.absolute_path == path
end)
require("nvim-tree.view").set_cursor { i + 1, 1 }
explorer.view:set_cursor { i + 1, 1 }
end
---Focus node passed as parameter if visible, otherwise focus first visible parent.
@ -467,7 +471,7 @@ function M.focus_node_or_parent(node)
end)
if found_node or node.parent == nil then
require("nvim-tree.view").set_cursor { i + 1, 1 }
explorer.view:set_cursor { i + 1, 1 }
break
end