Skip to content

Commit 4cbe795

Browse files
authored
perf(#3257): remove setup for view, extracting view-state (#3306)
* perf(#3257): extract view-state * fix(#3257): move prev_focused_node from View to LiveFilter
1 parent b548cfe commit 4cbe795

File tree

5 files changed

+147
-143
lines changed

5 files changed

+147
-143
lines changed

lua/nvim-tree.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,10 @@ function M.setup(config_user)
289289
end
290290

291291
require("nvim-tree.appearance").setup()
292-
require("nvim-tree.view").setup(config.g)
293292
require("nvim-tree.renderer.components").setup(config.g)
294293

294+
require("nvim-tree.view-state").initialize()
295+
295296
setup_autocommands()
296297

297298
if vim.g.NvimTreeSetup == 1 then

lua/nvim-tree/explorer/live-filter.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ local DirectoryNode = require("nvim-tree.node.directory")
1111
---@field prefix string
1212
---@field always_show_folders boolean
1313
---@field filter string
14+
---@field prev_focused_node? Node
1415
local LiveFilter = Class:extend()
1516

1617
---@class LiveFilter
@@ -26,6 +27,7 @@ function LiveFilter:new(args)
2627
self.prefix = self.explorer.opts.live_filter.prefix
2728
self.always_show_folders = self.explorer.opts.live_filter.always_show_folders
2829
self.filter = nil
30+
self.prev_focused_node = nil
2931
end
3032

3133
---@param node_ Node?
@@ -201,7 +203,7 @@ local function create_overlay(self)
201203
end
202204

203205
function LiveFilter:start_filtering()
204-
view.View.live_filter.prev_focused_node = self.explorer:get_node_at_cursor()
206+
self.prev_focused_node = self.explorer:get_node_at_cursor()
205207
self.filter = self.filter or ""
206208

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

217219
function LiveFilter:clear_filter()
218220
local node = self.explorer:get_node_at_cursor()
219-
local last_node = view.View.live_filter.prev_focused_node
220221

221222
self.filter = nil
222223
reset_filter(self)
223224
self.explorer.renderer:draw()
224225

225226
if node then
226227
self.explorer:focus_node_or_parent(node)
227-
elseif last_node then
228-
self.explorer:focus_node_or_parent(last_node)
228+
elseif self.prev_focused_node then
229+
self.explorer:focus_node_or_parent(self.prev_focused_node)
229230
end
230231
end
231232

lua/nvim-tree/renderer/components/full-name.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
local config = require("nvim-tree.config")
2+
local view_state = require("nvim-tree.view-state")
23

34
local M = {}
45

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

88
local function hide(win)
99
if win then
@@ -74,7 +74,7 @@ local function show()
7474
style = "minimal",
7575
border = "none"
7676
})
77-
vim.wo[M.popup_win].winhl = view.View.winopts.winhl
77+
vim.wo[M.popup_win].winhl = view_state.Active.winopts.winhl
7878

7979
local ns_id = vim.api.nvim_get_namespaces()["NvimTreeHighlights"]
8080
local extmarks = vim.api.nvim_buf_get_extmarks(0, ns_id, { line_nr - 1, 0 }, { line_nr - 1, -1 }, { details = true })

lua/nvim-tree/view-state.lua

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
local config = require("nvim-tree.config")
2+
3+
local M = {}
4+
5+
local DEFAULT_MIN_WIDTH = 30
6+
local DEFAULT_MAX_WIDTH = -1
7+
local DEFAULT_LINES_EXCLUDED = {
8+
"root",
9+
}
10+
local DEFAULT_PADDING = 1
11+
12+
M.Active = {
13+
adaptive_size = false,
14+
tabpages = {},
15+
cursors = {},
16+
winopts = {
17+
relativenumber = false,
18+
number = false,
19+
list = false,
20+
foldenable = false,
21+
winfixwidth = true,
22+
winfixheight = true,
23+
spell = false,
24+
signcolumn = "yes",
25+
foldmethod = "manual",
26+
foldcolumn = "0",
27+
cursorcolumn = false,
28+
cursorline = true,
29+
cursorlineopt = "both",
30+
colorcolumn = "0",
31+
wrap = false,
32+
winhl = table.concat({
33+
"EndOfBuffer:NvimTreeEndOfBuffer",
34+
"CursorLine:NvimTreeCursorLine",
35+
"CursorLineNr:NvimTreeCursorLineNr",
36+
"LineNr:NvimTreeLineNr",
37+
"WinSeparator:NvimTreeWinSeparator",
38+
"StatusLine:NvimTreeStatusLine",
39+
"StatusLineNC:NvimTreeStatuslineNC",
40+
"SignColumn:NvimTreeSignColumn",
41+
"Normal:NvimTreeNormal",
42+
"NormalNC:NvimTreeNormalNC",
43+
"NormalFloat:NvimTreeNormalFloat",
44+
"FloatBorder:NvimTreeNormalFloatBorder",
45+
}, ","),
46+
},
47+
}
48+
49+
---@param size (fun():integer)|integer|string
50+
---@return integer
51+
function M.get_size(size)
52+
if type(size) == "number" then
53+
return size
54+
elseif type(size) == "function" then
55+
return M.get_size(size())
56+
end
57+
local size_as_number = tonumber(size:sub(0, -2))
58+
local percent_as_decimal = size_as_number / 100
59+
return math.floor(vim.o.columns * percent_as_decimal)
60+
end
61+
62+
---@param size (fun():integer)|integer|nil
63+
---@return integer
64+
function M.get_width(size)
65+
if size then
66+
return M.get_size(size)
67+
else
68+
return M.get_size(M.Active.width)
69+
end
70+
end
71+
72+
---Configure width-related config
73+
---@param width string|function|number|table|nil
74+
local function configure_width(width)
75+
if type(width) == "table" then
76+
M.Active.adaptive_size = true
77+
M.Active.width = width.min or DEFAULT_MIN_WIDTH
78+
M.Active.max_width = width.max or DEFAULT_MAX_WIDTH
79+
local lines_excluded = width.lines_excluded or DEFAULT_LINES_EXCLUDED
80+
M.Active.root_excluded = vim.tbl_contains(lines_excluded, "root")
81+
M.Active.padding = width.padding or DEFAULT_PADDING
82+
elseif width == nil then
83+
if config.g.view.width ~= nil then
84+
-- if we had input config - fallback to it
85+
M.configure_width(config.g.view.width)
86+
else
87+
-- otherwise - restore initial width
88+
M.Active.width = M.Active.initial_width
89+
end
90+
else
91+
M.Active.adaptive_size = false
92+
M.Active.width = width
93+
end
94+
end
95+
96+
---Apply global configuration to Active
97+
function M.initialize()
98+
M.Active.winopts.cursorline = config.g.view.cursorline
99+
M.Active.winopts.cursorlineopt = config.g.view.cursorlineopt
100+
M.Active.winopts.number = config.g.view.number
101+
M.Active.winopts.relativenumber = config.g.view.relativenumber
102+
M.Active.winopts.signcolumn = config.g.view.signcolumn
103+
104+
configure_width(config.g.view.width)
105+
106+
M.Active.initial_width = M.get_width()
107+
end
108+
109+
return M

0 commit comments

Comments
 (0)