@@ -15,7 +15,7 @@ local M = { resize_timer_interval = 50 }
1515local ESC_KEY = vim .api .nvim_replace_termcodes (" <ESC>" , true , false , true )
1616local default_popup_size = { width = 60 , height = " 80%" }
1717local floating_windows = {}
18- local draw , create_window , create_tree
18+ local draw , create_window , create_tree , render_tree
1919
2020local resize_monitor_timer = nil
2121local start_resize_monitor = function ()
@@ -42,7 +42,7 @@ local start_resize_monitor = function()
4242 if current_size ~= state .win_width then
4343 log .trace (" Window size changed, redrawing tree" )
4444 state .win_width = current_size
45- state . tree : render ( )
45+ render_tree ( state )
4646 speed_up_loops = 21 -- move to fast timer for the next 1000 ms
4747 end
4848 end
@@ -343,6 +343,10 @@ local prepare_node = function(item, state)
343343 end
344344 end
345345 end
346+ state .longest_width_exact = math.max (
347+ state .longest_width_exact ,
348+ vim .api .nvim_strwidth (line :content ()) + 1
349+ )
346350 end
347351
348352 return line
@@ -562,7 +566,7 @@ M.position = {
562566M .redraw = function (state )
563567 if state .tree and M .window_exists (state ) then
564568 log .trace (" Redrawing tree" , state .name , state .id )
565- state . tree : render ( )
569+ render_tree ( state )
566570 log .trace (" Redrawing tree done" , state .name , state .id )
567571 end
568572end
@@ -922,6 +926,23 @@ M.window_exists = function(state)
922926 return window_exists
923927end
924928
929+ --- Renders the given tree and expands window width if needed
930+ -- @param state table The state containing tree to render. Almost same as state.tree:render()
931+ render_tree = function (state )
932+ state .tree :render ()
933+ if state .window .auto_expand_width and state .window .position ~= " float" then
934+ state .window .last_user_width = vim .api .nvim_win_get_width (0 )
935+ if state .longest_width_exact > state .window .last_user_width then
936+ log .trace (
937+ string.format (" `auto_expand_width: on. Expanding width to %s." , state .longest_width_exact )
938+ )
939+ vim .api .nvim_win_set_width (0 , state .longest_width_exact )
940+ state .win_width = state .longest_width_exact
941+ render_tree (state )
942+ end
943+ end
944+ end
945+
925946--- Draws the given nodes on the screen.
926947-- @param nodes table The nodes to draw.
927948-- @param state table The current state of the source.
@@ -968,7 +989,7 @@ draw = function(nodes, state, parent_id)
968989 state .win_width = utils .get_inner_win_width (state .winid )
969990 start_resize_monitor ()
970991
971- state . tree : render ( )
992+ render_tree ( state )
972993
973994 -- draw winbar / statusbar
974995 require (" neo-tree.ui.selector" ).set_source_selector (state )
@@ -1007,6 +1028,7 @@ M.show_nodes = function(sourceItems, state, parentId, callback)
10071028 -- local id = string.format("show_nodes %s:%s [%s]", state.name, state.force_float, state.tabnr)
10081029 -- utils.debounce(id, function()
10091030 events .fire_event (events .BEFORE_RENDER , state )
1031+ state .longest_width_exact = 0
10101032 local parent
10111033 local level = 0
10121034 if parentId ~= nil then
@@ -1086,7 +1108,7 @@ M.show_nodes = function(sourceItems, state, parentId, callback)
10861108 else
10871109 -- this was a force grouping of a lazy loaded folder
10881110 state .win_width = utils .get_inner_win_width (state .winid )
1089- state . tree : render ( )
1111+ render_tree ( state )
10901112 end
10911113
10921114 vim .schedule (function ()
0 commit comments