Skip to content

Commit 7a6b0d4

Browse files
authored
fix(source_selector): fix bug with tabs_layout, fixes #848 (#852)
1 parent 92624d0 commit 7a6b0d4

File tree

1 file changed

+44
-5
lines changed

1 file changed

+44
-5
lines changed

lua/neo-tree/ui/selector.lua

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -339,19 +339,58 @@ M.get_selector = function(state, width)
339339
.. text_with_hl("", hl_background)
340340
end
341341
else -- config.source_selector.tab_labels == "start", "end", "center"
342-
local tmp = ""
343-
for _, tab in ipairs(tabs) do
344-
tmp = tmp
342+
-- calculate padding based on tabs_layout
343+
local pad_length = width - length_sum
344+
local left_pad, right_pad = 0, 0
345+
if pad_length > 0 then
346+
if tabs_layout == "start" then
347+
left_pad, right_pad = 0, pad_length
348+
elseif tabs_layout == "end" then
349+
left_pad, right_pad = pad_length, 0
350+
elseif tabs_layout == "center" then
351+
left_pad, right_pad = pad_length / 2, math.ceil(pad_length / 2)
352+
end
353+
end
354+
355+
for i, tab in ipairs(tabs) do
356+
if width == 0 then
357+
break
358+
end
359+
360+
-- only render trunc_char if there is no space for the tab
361+
local sep_length = tab.length - tab.text_length
362+
if width <= sep_length + 1 then
363+
return_string = return_string
364+
.. text_with_hl(trunc_char .. add_padding(width - 1), hl_background)
365+
width = 0
366+
break
367+
end
368+
369+
-- tab_length should not exceed width
370+
local tab_length = width < tab.length and width or tab.length
371+
width = width - tab_length
372+
373+
-- add padding for first and last tab
374+
local tab_text = tab.text
375+
if i == 1 then
376+
tab_text = add_padding(left_pad) .. tab_text
377+
tab_length = tab_length + left_pad
378+
end
379+
if i == #tabs then
380+
tab_text = tab_text .. add_padding(right_pad)
381+
tab_length = tab_length + right_pad
382+
end
383+
384+
return_string = return_string
345385
.. render_tab(
346386
tab.left,
347387
tab.right,
348388
tab.sep_hl,
349-
tab.text,
389+
text_layout(tab_text, tabs_layout, tab_length - sep_length, trunc_char),
350390
tab.tab_hl,
351391
calc_click_id_from_source(winid, tab.index)
352392
)
353393
end
354-
return_string = return_string .. text_layout(tmp, tabs_layout, width, trunc_char)
355394
end
356395
return return_string .. "%<%0@v:lua.___neotree_selector_click@"
357396
end

0 commit comments

Comments
 (0)