@@ -14,12 +14,15 @@ local SIGN_GROUP = "NvimTreeRendererSigns"
1414
1515local namespace_highlights_id = vim .api .nvim_create_namespace " NvimTreeHighlights"
1616local namespace_extmarks_id = vim .api .nvim_create_namespace " NvimTreeExtmarks"
17+ local namespace_size_extmarks_id = vim .api .nvim_create_namespace " NvimTreeSizeExtmarks"
1718
1819--- @param bufnr number
1920--- @param lines string[]
2021--- @param hl_args AddHighlightArgs[]
2122--- @param signs string[]
22- local function _draw (bufnr , lines , hl_args , signs , extmarks )
23+ --- @param extmarks table<integer , HighlightedString[]>
24+ --- @param size_extmarks table<integer , HighlightedString[]>
25+ local function _draw (bufnr , lines , hl_args , signs , extmarks , size_extmarks )
2326 if vim .fn .has " nvim-0.10" == 1 then
2427 vim .api .nvim_set_option_value (" modifiable" , true , { buf = bufnr })
2528 else
@@ -40,16 +43,8 @@ local function _draw(bufnr, lines, hl_args, signs, extmarks)
4043 vim .fn .sign_place (0 , SIGN_GROUP , sign_name , bufnr , { lnum = i + 1 })
4144 end
4245
43- vim .api .nvim_buf_clear_namespace (bufnr , namespace_extmarks_id , 0 , - 1 )
44- for i , extname in pairs (extmarks ) do
45- for _ , mark in ipairs (extname ) do
46- vim .api .nvim_buf_set_extmark (bufnr , namespace_extmarks_id , i , - 1 , {
47- virt_text = { { mark .str , mark .hl } },
48- virt_text_pos = " right_align" ,
49- hl_mode = " combine" ,
50- })
51- end
52- end
46+ M .render_extmarks (bufnr , namespace_extmarks_id , extmarks )
47+ M .render_extmarks (bufnr , namespace_size_extmarks_id , size_extmarks )
5348end
5449
5550function M .render_hl (bufnr , hl )
@@ -66,6 +61,50 @@ function M.render_hl(bufnr, hl)
6661 end
6762end
6863
64+ function M .render_extmarks (bufnr , ns_id , extmarks )
65+ if not bufnr or not vim .api .nvim_buf_is_loaded (bufnr ) then
66+ return
67+ end
68+ vim .api .nvim_buf_clear_namespace (bufnr , ns_id , 0 , - 1 )
69+ for i , extname in pairs (extmarks ) do
70+ for _ , mark in ipairs (extname ) do
71+ vim .api .nvim_buf_set_extmark (bufnr , ns_id , i , - 1 , {
72+ virt_text = { { mark .str , mark .hl } },
73+ virt_text_pos = " right_align" ,
74+ hl_mode = " combine" ,
75+ })
76+ end
77+ end
78+ end
79+
80+ -- Here we do a partial redraw of only a subsection of extra marks.
81+ -- We could simply call reloaders.reload(), but that would be substantially slower.
82+ -- Calling `nvim_buf_clear_namespace` to hide size information
83+ -- and `render_extmarks` to show again is preferable in place of
84+ -- reloading all decorators, nodes and lines from scratch, since resize does not trigger a reload.
85+ local redraw_size_extmarks = false
86+ function M .on_resize ()
87+ if M .builder == nil then
88+ return
89+ end
90+
91+ local bufnr = view .get_bufnr ()
92+
93+ if view .get_current_width () < M .config .size .width_cutoff then
94+ redraw_size_extmarks = true
95+ vim .api .nvim_buf_clear_namespace (bufnr , namespace_size_extmarks_id , 0 , - 1 )
96+ return
97+ end
98+
99+ -- If we got here, we only need to know if we should redraw
100+ -- We don't have to check if decorator_size is enbaled, because if it's
101+ -- not, then size_extmarks would've been empty
102+ if redraw_size_extmarks then
103+ redraw_size_extmarks = false
104+ M .render_extmarks (bufnr , namespace_size_extmarks_id , M .builder .size_extmarks )
105+ end
106+ end
107+
69108function M .draw ()
70109 local bufnr = view .get_bufnr ()
71110 if not core .get_explorer () or not bufnr or not vim .api .nvim_buf_is_loaded (bufnr ) then
@@ -77,11 +116,11 @@ function M.draw()
77116 local cursor = vim .api .nvim_win_get_cursor (view .get_winnr () or 0 )
78117 icon_component .reset_config ()
79118
80- local builder = Builder : new (): build ()
81-
82- _draw (bufnr , builder .lines , builder .hl_args , builder .signs , builder .extmarks )
119+ redraw_size_extmarks = false
120+ M . builder = Builder : new (): build ()
121+ _draw (bufnr , M . builder .lines , M . builder .hl_args , M . builder .signs , M . builder .extmarks , M . builder . size_extmarks )
83122
84- if cursor and # builder .lines >= cursor [1 ] then
123+ if cursor and # M . builder .lines >= cursor [1 ] then
85124 vim .api .nvim_win_set_cursor (view .get_winnr () or 0 , cursor )
86125 end
87126
94133
95134function M .setup (opts )
96135 M .config = opts .renderer
97-
136+ M . builder = nil
98137 _padding .setup (opts )
99138 full_name .setup (opts )
100139 icon_component .setup (opts )
101-
102140 Builder .setup (opts )
103141end
104142
0 commit comments