Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,6 @@ function M.setup(config_user)
end

require("nvim-tree.appearance").setup()
require("nvim-tree.view").setup(config.g)
require("nvim-tree.renderer.components").setup(config.g)

setup_autocommands()
Expand Down
93 changes: 52 additions & 41 deletions lua/nvim-tree/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ local function create_buffer(bufnr)
events._dispatch_tree_attached_post(M.get_bufnr())
end

---@param size (fun():integer)|integer|string
---@param size nvim_tree.config.view.width.spec
---@return integer
local function get_size(size)
if type(size) == "number" then
Expand All @@ -129,6 +129,40 @@ local function get_size(size)
return math.floor(vim.o.columns * percent_as_decimal)
end

---Return the width as per config
---@return integer
local function initial_width()
if type(config.g.view.width) == "table" then
return get_size(config.g.view.width.min or DEFAULT_MIN_WIDTH)
else
return get_size(config.g.view.width --[[@as nvim_tree.config.view.width.spec]])
end
end

---Configure width-related config
---@param width string|function|number|table|nil
function M.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
local lines_excluded = width.lines_excluded or DEFAULT_LINES_EXCLUDED
M.View.root_excluded = vim.tbl_contains(lines_excluded, "root")
M.View.padding = width.padding or DEFAULT_PADDING
elseif width == nil then
if config.g.view.width ~= nil then
-- if we had input config - fallback to it
M.configure_width(config.g.view.width)
else
-- otherwise - restore initial width
M.View.width = initial_width()
end
else
M.View.adaptive_size = false
M.View.width = width
end
end

---@param size (fun():integer)|integer|nil
---@return integer
local function get_width(size)
Expand All @@ -154,6 +188,12 @@ end
local function set_window_options_and_buffer()
pcall(vim.api.nvim_command, "buffer " .. M.get_bufnr())

M.View.winopts.cursorline = config.g.view.cursorline
M.View.winopts.cursorlineopt = config.g.view.cursorlineopt
M.View.winopts.number = config.g.view.number
M.View.winopts.relativenumber = config.g.view.relativenumber
M.View.winopts.signcolumn = config.g.view.signcolumn

if vim.fn.has("nvim-0.10") == 1 then
local eventignore = vim.api.nvim_get_option_value("eventignore", {})
vim.api.nvim_set_option_value("eventignore", "all", {})
Expand Down Expand Up @@ -182,7 +222,7 @@ local function open_win_config()
if type(config.g.view.float.open_win_config) == "function" then
return config.g.view.float.open_win_config()
else
return config.g.view.float.open_win_config --[[ @as vim.api.keyset.win_config ]]
return config.g.view.float.open_win_config --[[@as vim.api.keyset.win_config]]
end
end

Expand Down Expand Up @@ -289,6 +329,10 @@ function M.open(options)
return
end

if not M.View.width then
M.configure_width(config.g.view.width)
end

local profile = log.profile_start("view open")

events._dispatch_on_tree_pre_open()
Expand Down Expand Up @@ -317,7 +361,7 @@ local function grow()
padding = padding + wininfo[1].textoff
end

local final_width = M.View.initial_width
local final_width = initial_width()
local max_width = get_width(M.View.max_width)
if max_width == -1 then
max_width = math.huge
Expand Down Expand Up @@ -404,6 +448,11 @@ end
---@param opts OpenInWinOpts|nil
function M.open_in_win(opts)
opts = opts or { hijack_current_buf = true, resize = true }

if not M.View.width then
M.configure_width(config.g.view.width)
end

events._dispatch_on_tree_pre_open()
if opts.winid and vim.api.nvim_win_is_valid(opts.winid) then
vim.api.nvim_set_current_win(opts.winid)
Expand Down Expand Up @@ -590,42 +639,4 @@ function M.is_width_determined()
return type(M.View.width) ~= "function"
end

---Configure width-related config
---@param width string|function|number|table|nil
function M.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
local lines_excluded = width.lines_excluded or DEFAULT_LINES_EXCLUDED
M.View.root_excluded = vim.tbl_contains(lines_excluded, "root")
M.View.padding = width.padding or DEFAULT_PADDING
elseif width == nil then
if config.g.view.width ~= nil then
-- if we had input config - fallback to it
M.configure_width(config.g.view.width)
else
-- otherwise - restore initial width
M.View.width = M.View.initial_width
end
else
M.View.adaptive_size = false
M.View.width = width
end
end

---@param opts nvim_tree.config
function M.setup(opts)
local options = opts.view or {}
M.View.winopts.cursorline = options.cursorline
M.View.winopts.cursorlineopt = options.cursorlineopt
M.View.winopts.number = options.number
M.View.winopts.relativenumber = options.relativenumber
M.View.winopts.signcolumn = options.signcolumn

M.configure_width(options.width)

M.View.initial_width = get_width()
end

return M
Loading