Skip to content

Commit 290c84a

Browse files
authored
feat: add window.auto_expand_width option (related to #547) (#580)
1 parent b68ebd1 commit 290c84a

File tree

4 files changed

+53
-7
lines changed

4 files changed

+53
-7
lines changed

lua/neo-tree/defaults.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ local config = {
287287
position = "left", -- left, right, top, bottom, float, current
288288
width = 40, -- applies to left and right positions
289289
height = 15, -- applies to top and bottom positions
290+
auto_expand_width = false, -- expand the window when file exceeds the window width. does not work with position = "float"
290291
popup = { -- settings that apply to float position only
291292
size = {
292293
height = "80%",
@@ -338,6 +339,7 @@ local config = {
338339
["p"] = "paste_from_clipboard",
339340
["c"] = "copy", -- takes text input for destination, also accepts the config.show_path option
340341
["m"] = "move", -- takes text input for destination, also accepts the config.show_path option
342+
["e"] = "toggle_auto_expand_width",
341343
["q"] = "close_window",
342344
["?"] = "show_help",
343345
["<"] = "prev_source",
@@ -483,7 +485,7 @@ local config = {
483485
window = {
484486
mappings = {
485487
["<cr>"] = "toggle_node",
486-
["e"] = "example_command",
488+
["<C-e>"] = "example_command",
487489
["d"] = "show_debug_info",
488490
},
489491
},

lua/neo-tree/sources/common/commands.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,23 @@ M.close_window = function(state)
142142
renderer.close(state)
143143
end
144144

145+
M.toggle_auto_expand_width = function(state)
146+
if state.window.position == "float" then
147+
return
148+
end
149+
state.window.auto_expand_width = state.window.auto_expand_width == false
150+
if not state.window.auto_expand_width then
151+
if (state.window.last_user_width or state.window.width) >= vim.api.nvim_win_get_width(0) then
152+
state.window.last_user_width = state.window.width
153+
end
154+
vim.api.nvim_win_set_width(0, state.window.last_user_width)
155+
state.win_width = state.window.last_user_width
156+
state.longest_width_exact = 0
157+
log.trace(string.format("Collapse auto_expand_width."))
158+
end
159+
renderer.redraw(state)
160+
end
161+
145162
local copy_node_to_clipboard = function(state, node)
146163
state.clipboard = state.clipboard or {}
147164
local existing = state.clipboard[node.id]

lua/neo-tree/sources/common/container.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ local merge_content = function(context)
187187
-- * Repeat until all layers have been merged.
188188
-- * Join the left and right tables together and return.
189189
--
190-
local remaining_width = context.container_width
190+
local huge_number = 2000
191+
local remaining_width = context.auto_expand_width and huge_number or context.container_width
191192
local left, right = {}, {}
192193
local left_width, right_width = 0, 0
193194

@@ -253,6 +254,9 @@ local merge_content = function(context)
253254
end
254255
end
255256

257+
if context.auto_expand_width then
258+
remaining_width = context.container_width + remaining_width - huge_number
259+
end
256260
if remaining_width > 0 and #right > 0 then
257261
table.insert(left, { text = string.rep(" ", remaining_width) })
258262
end
@@ -271,6 +275,7 @@ M.render = function(config, node, state, available_width)
271275
left_padding = config.left_padding,
272276
right_padding = config.right_padding,
273277
enable_character_fade = config.enable_character_fade,
278+
auto_expand_width = state.window.auto_expand_width and state.window.position ~= "float",
274279
}
275280

276281
render_content(config, node, state, context)

lua/neo-tree/ui/renderer.lua

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ local M = { resize_timer_interval = 50 }
1515
local ESC_KEY = vim.api.nvim_replace_termcodes("<ESC>", true, false, true)
1616
local default_popup_size = { width = 60, height = "80%" }
1717
local floating_windows = {}
18-
local draw, create_window, create_tree
18+
local draw, create_window, create_tree, render_tree
1919

2020
local resize_monitor_timer = nil
2121
local 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 = {
562566
M.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
568572
end
@@ -922,6 +926,23 @@ M.window_exists = function(state)
922926
return window_exists
923927
end
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

Comments
 (0)