@@ -3,27 +3,55 @@ local utils = require("nvim-tree.utils")
33local log = require (" nvim-tree.log" )
44local notify = require (" nvim-tree.notify" )
55
6+ local Class = require (" nvim-tree.classic" )
7+
68--- @class OpenInWinOpts
79--- @field hijack_current_buf boolean | nil default true
810--- @field resize boolean | nil default true
911--- @field winid number | nil 0 or nil for current
1012
11- local M = {}
12-
13- local DEFAULT_MIN_WIDTH = 30
14- local DEFAULT_MAX_WIDTH = - 1
15- local DEFAULT_PADDING = 1
16-
17- M .View = {
18- adaptive_size = false ,
19- centralize_selection = false ,
20- tabpages = {},
21- cursors = {},
22- hide_root_folder = false ,
23- live_filter = {
13+ -- TODO attempt to type the tables, at least the options ones
14+
15+ --- @class (exact ) View : Class
16+ --- @field live_filter table
17+ --- @field side string
18+ --- @field float table
19+ --- TODO private below here
20+ --- @field explorer Explorer
21+ --- @field adaptive_size boolean
22+ --- @field centralize_selection boolean
23+ --- @field tabpages table
24+ --- @field cursors table
25+ --- @field hide_root_folder boolean
26+ --- @field winopts table
27+ --- @field height integer
28+ --- @field tab table
29+ --- @field preserve_window_proportions boolean
30+ --- @field initial_width integer
31+ --- @field width (fun (): integer )| integer | string
32+ --- @field max_width integer
33+ --- @field padding integer
34+ local View = Class :extend ()
35+
36+ --- @class View
37+ --- @overload fun ( args : ViewArgs ): View
38+
39+ --- @class (exact ) ViewArgs
40+ --- @field explorer Explorer
41+
42+ --- @protected
43+ --- @param args ViewArgs
44+ function View :new (args )
45+ self .explorer = args .explorer
46+ self .adaptive_size = false
47+ self .centralize_selection = false
48+ self .tabpages = {}
49+ self .cursors = {}
50+ self .hide_root_folder = false
51+ self .live_filter = {
2452 prev_focused_node = nil ,
25- },
26- winopts = {
53+ }
54+ self . winopts = {
2755 relativenumber = false ,
2856 number = false ,
2957 list = false ,
@@ -53,8 +81,30 @@ M.View = {
5381 " NormalFloat:NvimTreeNormalFloat" ,
5482 " FloatBorder:NvimTreeNormalFloatBorder" ,
5583 }, " ," ),
56- },
57- }
84+ }
85+
86+ self .centralize_selection = self .explorer .opts .view .centralize_selection
87+ self .side = (self .explorer .opts .view .side == " right" ) and " right" or " left"
88+ self .height = self .explorer .opts .view .height
89+ self .hide_root_folder = self .explorer .opts .renderer .root_folder_label == false
90+ self .tab = self .explorer .opts .tab
91+ self .preserve_window_proportions = self .explorer .opts .view .preserve_window_proportions
92+ self .winopts .cursorline = self .explorer .opts .view .cursorline
93+ self .winopts .number = self .explorer .opts .view .number
94+ self .winopts .relativenumber = self .explorer .opts .view .relativenumber
95+ self .winopts .signcolumn = self .explorer .opts .view .signcolumn
96+ self .float = self .explorer .opts .view .float
97+
98+ self :configure_width (self .explorer .opts .view .width )
99+
100+ self .initial_width = self :get_width ()
101+ end
102+
103+ local M = {}
104+
105+ local DEFAULT_MIN_WIDTH = 30
106+ local DEFAULT_MAX_WIDTH = - 1
107+ local DEFAULT_PADDING = 1
58108
59109-- The initial state of a tab
60110local tabinitial = {
@@ -113,13 +163,14 @@ local function create_buffer(bufnr)
113163 events ._dispatch_tree_attached_post (M .get_bufnr ())
114164end
115165
166+ --- @private
116167--- @param size (fun (): integer )| integer | string
117168--- @return integer
118- local function get_size (size )
169+ function View : get_size (size )
119170 if type (size ) == " number" then
120171 return size
121172 elseif type (size ) == " function" then
122- return get_size (size ())
173+ return self : get_size (size ())
123174 end
124175 local size_as_number = tonumber (size :sub (0 , - 2 ))
125176 local percent_as_decimal = size_as_number / 100
@@ -128,11 +179,11 @@ end
128179
129180--- @param size (fun (): integer )| integer | nil
130181--- @return integer
131- local function get_width (size )
182+ function View : get_width (size )
132183 if size then
133- return get_size (size )
184+ return self : get_size (size )
134185 else
135- return get_size (M . View .width )
186+ return self : get_size (self .width )
136187 end
137188end
138189
@@ -305,7 +356,7 @@ local function grow()
305356 local starts_at = M .is_root_folder_visible (require (" nvim-tree.core" ).get_cwd ()) and 1 or 0
306357 local lines = vim .api .nvim_buf_get_lines (M .get_bufnr (), starts_at , - 1 , false )
307358 -- number of columns of right-padding to indicate end of path
308- local padding = get_size (M .View .padding )
359+ local padding = M . View : get_size (M .View .padding )
309360
310361 -- account for sign/number columns etc.
311362 local wininfo = vim .fn .getwininfo (M .get_winnr ())
@@ -320,7 +371,7 @@ local function grow()
320371 if M .View .max_width == - 1 then
321372 max_width = - 1
322373 else
323- max_width = get_width (M .View .max_width ) - padding
374+ max_width = M . View : get_width (M .View .max_width ) - padding
324375 end
325376
326377 local ns_id = vim .api .nvim_get_namespaces ()[" NvimTreeExtmarks" ]
@@ -386,7 +437,7 @@ function M.resize(size)
386437
387438 local winnr = M .get_winnr () or 0
388439
389- local new_size = get_width ()
440+ local new_size = M . View : get_width ()
390441
391442 if new_size ~= vim .api .nvim_win_get_width (winnr ) then
392443 vim .api .nvim_win_set_width (winnr , new_size )
@@ -600,45 +651,32 @@ end
600651
601652--- Configure width-related config
602653--- @param width string | function | number | table | nil
603- function M . configure_width (width )
654+ function View : configure_width (width )
604655 if type (width ) == " table" then
605- M . View .adaptive_size = true
606- M . View .width = width .min or DEFAULT_MIN_WIDTH
607- M . View .max_width = width .max or DEFAULT_MAX_WIDTH
608- M . View .padding = width .padding or DEFAULT_PADDING
656+ self .adaptive_size = true
657+ self .width = width .min or DEFAULT_MIN_WIDTH
658+ self .max_width = width .max or DEFAULT_MAX_WIDTH
659+ self .padding = width .padding or DEFAULT_PADDING
609660 elseif width == nil then
610- if M . config .width ~= nil then
661+ if self . explorer . opts . view .width ~= nil then
611662 -- if we had input config - fallback to it
612- M . configure_width (M . config .width )
663+ self : configure_width (self . explorer . opts . view .width )
613664 else
614665 -- otherwise - restore initial width
615- M . View . width = M . View .initial_width
666+ self . width = self .initial_width
616667 end
617668 else
618- M . View .adaptive_size = false
619- M . View .width = width
669+ self .adaptive_size = false
670+ self .width = width
620671 end
621672end
622673
623674function M .setup (opts )
624- local options = opts .view or {}
625- M .View .centralize_selection = options .centralize_selection
626- M .View .side = (options .side == " right" ) and " right" or " left"
627- M .View .height = options .height
628- M .View .hide_root_folder = opts .renderer .root_folder_label == false
629- M .View .tab = opts .tab
630- M .View .preserve_window_proportions = options .preserve_window_proportions
631- M .View .winopts .cursorline = options .cursorline
632- M .View .winopts .number = options .number
633- M .View .winopts .relativenumber = options .relativenumber
634- M .View .winopts .signcolumn = options .signcolumn
635- M .View .float = options .float
636- M .on_attach = opts .on_attach
637-
638- M .config = options
639- M .configure_width (options .width )
640-
641- M .View .initial_width = get_width ()
675+ M .View = View ({
676+ explorer = {
677+ opts = opts
678+ }
679+ })
642680end
643681
644682return M
0 commit comments