Skip to content
Merged
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
3 changes: 2 additions & 1 deletion lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,10 @@ 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)

require("nvim-tree.view-state").initialize()

setup_autocommands()

if vim.g.NvimTreeSetup == 1 then
Expand Down
9 changes: 5 additions & 4 deletions lua/nvim-tree/explorer/live-filter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ local DirectoryNode = require("nvim-tree.node.directory")
---@field prefix string
---@field always_show_folders boolean
---@field filter string
---@field prev_focused_node? Node
local LiveFilter = Class:extend()

---@class LiveFilter
Expand All @@ -26,6 +27,7 @@ function LiveFilter:new(args)
self.prefix = self.explorer.opts.live_filter.prefix
self.always_show_folders = self.explorer.opts.live_filter.always_show_folders
self.filter = nil
self.prev_focused_node = nil
end

---@param node_ Node?
Expand Down Expand Up @@ -201,7 +203,7 @@ local function create_overlay(self)
end

function LiveFilter:start_filtering()
view.View.live_filter.prev_focused_node = self.explorer:get_node_at_cursor()
self.prev_focused_node = self.explorer:get_node_at_cursor()
self.filter = self.filter or ""

self.explorer.renderer:draw()
Expand All @@ -216,16 +218,15 @@ end

function LiveFilter:clear_filter()
local node = self.explorer:get_node_at_cursor()
local last_node = view.View.live_filter.prev_focused_node

self.filter = nil
reset_filter(self)
self.explorer.renderer:draw()

if node then
self.explorer:focus_node_or_parent(node)
elseif last_node then
self.explorer:focus_node_or_parent(last_node)
elseif self.prev_focused_node then
self.explorer:focus_node_or_parent(self.prev_focused_node)
end
end

Expand Down
4 changes: 2 additions & 2 deletions lua/nvim-tree/renderer/components/full-name.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
local config = require("nvim-tree.config")
local view_state = require("nvim-tree.view-state")

local M = {}

local utils = require("nvim-tree.utils")
local view = require("nvim-tree.view")

local function hide(win)
if win then
Expand Down Expand Up @@ -74,7 +74,7 @@ local function show()
style = "minimal",
border = "none"
})
vim.wo[M.popup_win].winhl = view.View.winopts.winhl
vim.wo[M.popup_win].winhl = view_state.Active.winopts.winhl

local ns_id = vim.api.nvim_get_namespaces()["NvimTreeHighlights"]
local extmarks = vim.api.nvim_buf_get_extmarks(0, ns_id, { line_nr - 1, 0 }, { line_nr - 1, -1 }, { details = true })
Expand Down
109 changes: 109 additions & 0 deletions lua/nvim-tree/view-state.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
local config = require("nvim-tree.config")

local M = {}

local DEFAULT_MIN_WIDTH = 30
local DEFAULT_MAX_WIDTH = -1
local DEFAULT_LINES_EXCLUDED = {
"root",
}
local DEFAULT_PADDING = 1

M.Active = {
adaptive_size = false,
tabpages = {},
cursors = {},
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",
"FloatBorder:NvimTreeNormalFloatBorder",
}, ","),
},
}

---@param size (fun():integer)|integer|string
---@return integer
function M.get_size(size)
if type(size) == "number" then
return size
elseif type(size) == "function" then
return M.get_size(size())
end
local size_as_number = tonumber(size:sub(0, -2))
local percent_as_decimal = size_as_number / 100
return math.floor(vim.o.columns * percent_as_decimal)
end

---@param size (fun():integer)|integer|nil
---@return integer
function M.get_width(size)
if size then
return M.get_size(size)
else
return M.get_size(M.Active.width)
end
end

---Configure width-related config
---@param width string|function|number|table|nil
local function configure_width(width)
if type(width) == "table" then
M.Active.adaptive_size = true
M.Active.width = width.min or DEFAULT_MIN_WIDTH
M.Active.max_width = width.max or DEFAULT_MAX_WIDTH
local lines_excluded = width.lines_excluded or DEFAULT_LINES_EXCLUDED
M.Active.root_excluded = vim.tbl_contains(lines_excluded, "root")
M.Active.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.Active.width = M.Active.initial_width
end
else
M.Active.adaptive_size = false
M.Active.width = width
end
end

---Apply global configuration to Active
function M.initialize()
M.Active.winopts.cursorline = config.g.view.cursorline
M.Active.winopts.cursorlineopt = config.g.view.cursorlineopt
M.Active.winopts.number = config.g.view.number
M.Active.winopts.relativenumber = config.g.view.relativenumber
M.Active.winopts.signcolumn = config.g.view.signcolumn

configure_width(config.g.view.width)

M.Active.initial_width = M.get_width()
end

return M
Loading
Loading