Skip to content

Commit 4b755ea

Browse files
authored
fix(components): fix use_git_status_colors config and refactor component gap padding (#864)
1 parent 58e7225 commit 4b755ea

File tree

9 files changed

+301
-129
lines changed

9 files changed

+301
-129
lines changed

lua/neo-tree/sources/buffers/components.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ M.name = function(config, node, state)
3333
else
3434
highlight = highlights.FILE_NAME
3535
end
36-
else
36+
elseif config.use_git_status_colors then
3737
local git_status = state.components.git_status({}, node, state)
3838
if git_status and git_status.highlight then
3939
highlight = git_status.highlight

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

+12-12
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ M.bufnr = function(config, node, state)
3434
return {}
3535
end
3636
return {
37-
text = string.format(" #%s", bufnr),
37+
text = string.format("#%s", bufnr),
3838
highlight = highlight,
3939
}
4040
end
@@ -60,15 +60,15 @@ M.current_filter = function(config, node, state)
6060
end
6161
return {
6262
{
63-
text = "Find ",
63+
text = "Find",
6464
highlight = highlights.DIM_TEXT,
6565
},
6666
{
6767
text = string.format('"%s"', filter),
6868
highlight = config.highlight or highlights.FILTER_TERM,
6969
},
7070
{
71-
text = " in ",
71+
text = "in",
7272
highlight = highlights.DIM_TEXT,
7373
},
7474
}
@@ -107,7 +107,7 @@ M.diagnostics = function(config, node, state)
107107
defined.text = config.symbols[severity_lower]
108108
end
109109
if config.highlights and config.highlights[severity_lower] then
110-
defined = defined or { text = severity:sub(1, 1) .. " " }
110+
defined = defined or { text = severity:sub(1, 1) }
111111
defined.texthl = config.highlights[severity_lower]
112112
end
113113

@@ -118,7 +118,7 @@ M.diagnostics = function(config, node, state)
118118
}
119119
else
120120
return {
121-
text = severity:sub(1, 1) .. " ",
121+
text = severity:sub(1, 1),
122122
highlight = "Diagnostic" .. severity,
123123
}
124124
end
@@ -233,27 +233,27 @@ M.filtered_by = function(config, node, state)
233233
local fby = node.filtered_by
234234
if fby.name then
235235
result = {
236-
text = "(hide by name) ",
236+
text = "(hide by name)",
237237
highlight = highlights.HIDDEN_BY_NAME,
238238
}
239239
elseif fby.pattern then
240240
result = {
241-
text = "(hide by pattern) ",
241+
text = "(hide by pattern)",
242242
highlight = highlights.HIDDEN_BY_NAME,
243243
}
244244
elseif fby.gitignored then
245245
result = {
246-
text = "(gitignored) ",
246+
text = "(gitignored)",
247247
highlight = highlights.GIT_IGNORED,
248248
}
249249
elseif fby.dotfiles then
250250
result = {
251-
text = "(dotfile) ",
251+
text = "(dotfile)",
252252
highlight = highlights.DOTFILE,
253253
}
254254
elseif fby.hidden then
255255
result = {
256-
text = "(hidden) ",
256+
text = "(hidden)",
257257
highlight = highlights.WINDOWS_HIDDEN,
258258
}
259259
end
@@ -297,7 +297,7 @@ M.modified = function(config, node, state)
297297

298298
if buf_info and buf_info.modified then
299299
return {
300-
text = (make_two_char(config.symbol) or "[+] "),
300+
text = (make_two_char(config.symbol) or "[+]"),
301301
highlight = config.highlight or highlights.MODIFIED,
302302
}
303303
else
@@ -344,7 +344,7 @@ M.name = function(config, node, state)
344344
text = text .. string.rep(" ", config.right_padding)
345345
end
346346
else
347-
text = text .. " "
347+
text = text
348348
end
349349

350350
return {

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

+55-23
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,36 @@ local calc_container_width = function(config, node, state, context)
4949
end
5050

5151
local render_content = function(config, node, state, context)
52-
local max_width = 0
52+
local add_padding = function(rendered_item, should_pad)
53+
for _, data in ipairs(rendered_item) do
54+
if data.text then
55+
local padding = (should_pad and #data.text and data.text:sub(1, 1) ~= " ") and " " or ""
56+
data.text = padding .. data.text
57+
should_pad = data.text:sub(#data.text) ~= " "
58+
end
59+
end
60+
return should_pad
61+
end
5362

63+
local max_width = 0
5464
local grouped_by_zindex = utils.group_by(config.content, "zindex")
65+
5566
for zindex, items in pairs(grouped_by_zindex) do
67+
local should_pad = { left = false, right = false }
5668
local zindex_rendered = { left = {}, right = {} }
5769
local rendered_width = 0
70+
5871
for _, item in ipairs(items) do
5972
local rendered_item = renderer.render_component(item, node, state, context.available_width)
6073
if rendered_item then
61-
vim.list_extend(zindex_rendered[item.align or "left"], rendered_item)
74+
local align = item.align or "left"
75+
should_pad[align] = add_padding(rendered_item, should_pad[align])
76+
77+
vim.list_extend(zindex_rendered[align], rendered_item)
6278
rendered_width = rendered_width + calc_rendered_width(rendered_item)
6379
end
6480
end
81+
6582
max_width = math.max(max_width, rendered_width)
6683
grouped_by_zindex[zindex] = zindex_rendered
6784
end
@@ -120,21 +137,24 @@ local truncate_layer_keep_right = function(layer, skip_count, max_length)
120137
local text_length = vim.fn.strchars(item.text)
121138
local remaining_to_skip = skip_count - skipped
122139
if remaining_to_skip > 0 then
123-
if #item.text <= remaining_to_skip then
140+
if text_length <= remaining_to_skip then
124141
skipped = skipped + text_length
125142
item.text = ""
126143
else
127-
item.text = item.text:sub(1, text_length - remaining_to_skip)
128-
if #item.text + taken > max_length then
129-
item.text = item.text:sub(text_length - (max_length - taken))
144+
item.text = vim.fn.strcharpart(item.text, 0, text_length - remaining_to_skip)
145+
text_length = vim.fn.strchars(item.text)
146+
if text_length + taken > max_length then
147+
item.text = vim.fn.strcharpart(item.text, text_length - (max_length - taken))
148+
text_length = vim.fn.strchars(item.text)
130149
end
131150
table.insert(result, item)
132151
taken = taken + text_length
133152
skipped = skipped + remaining_to_skip
134153
end
135154
elseif taken <= max_length then
136-
if #item.text + taken > max_length then
137-
item.text = item.text:sub(text_length - (max_length - taken))
155+
if text_length + taken > max_length then
156+
item.text = vim.fn.strcharpart(item.text, text_length - (max_length - taken))
157+
text_length = vim.fn.strchars(item.text)
138158
end
139159
table.insert(result, item)
140160
taken = taken + text_length
@@ -149,21 +169,22 @@ local fade_content = function(layer, fade_char_count)
149169
return
150170
end
151171
local hl = layer[#layer].highlight or "Normal"
152-
local fade0 = highlights.get_faded_highlight_group(hl, 0.68)
153-
local fade1 = highlights.get_faded_highlight_group(hl, 0.6)
154-
local fade2 = highlights.get_faded_highlight_group(hl, 0.35)
155-
if #text >= 3 and fade_char_count >= 3 then
156-
layer[#layer].text = text:sub(1, #text - 3)
157-
table.insert(layer, { text = text:sub(#text - 2, -3), highlight = fade0 })
158-
table.insert(layer, { text = text:sub(#text - 1, -2), highlight = fade1 })
159-
table.insert(layer, { text = text:sub(#text), highlight = fade2 })
160-
elseif #text >= 2 and fade_char_count >= 2 then
161-
layer[#layer].text = text:sub(1, #text - 2)
162-
table.insert(layer, { text = text:sub(#text - 1, -2), highlight = fade0 })
163-
table.insert(layer, { text = text:sub(#text), highlight = fade1 })
164-
elseif #text >= 1 and fade_char_count >= 1 then
165-
layer[#layer].text = text:sub(1, #text - 1)
166-
table.insert(layer, { text = text:sub(#text), highlight = fade0 })
172+
local fade = {
173+
highlights.get_faded_highlight_group(hl, 0.68),
174+
highlights.get_faded_highlight_group(hl, 0.6),
175+
highlights.get_faded_highlight_group(hl, 0.35),
176+
}
177+
178+
for i = 3, 1, -1 do
179+
if #text >= i and fade_char_count >= i then
180+
layer[#layer].text = text:sub(1, -i - 1)
181+
for j = i, 1, -1 do
182+
-- force no padding for each faded character
183+
local entry = { text = text:sub(-j, -j), highlight = fade[i - j + 1], no_padding = true }
184+
table.insert(layer, entry)
185+
end
186+
break
187+
end
167188
end
168189
end
169190

@@ -268,6 +289,12 @@ local merge_content = function(context)
268289

269290
local result = {}
270291
vim.list_extend(result, left)
292+
293+
-- we do not pad between left and right side
294+
if #right >= 1 then
295+
right[1].no_padding = true
296+
end
297+
271298
vim.list_extend(result, right)
272299
context.merged_content = result
273300
log.trace("wanted width: ", wanted_width, " actual width: ", context.container_width)
@@ -293,6 +320,11 @@ M.render = function(config, node, state, available_width)
293320
if context.has_right_content then
294321
state.has_right_content = true
295322
end
323+
324+
-- we still want padding between this container and the previous component
325+
if #context.merged_content > 0 then
326+
context.merged_content[1].no_padding = false
327+
end
296328
return context.merged_content, context.wanted_width
297329
end
298330

lua/neo-tree/sources/document_symbols/components.lua

+4-22
Original file line numberDiff line numberDiff line change
@@ -16,43 +16,25 @@ local common = require("neo-tree.sources.common.components")
1616
local M = {}
1717

1818
M.icon = function(config, node, state)
19-
local padding = config.padding or " "
20-
local icon = node.extra.kind.icon
21-
22-
icon = config.align == "right" and padding .. icon or icon .. padding
23-
if node:get_depth() == 1 then
24-
icon = ""
25-
end
26-
2719
return {
28-
text = icon,
20+
text = node:get_depth() == 1 and "" or node.extra.kind.icon,
2921
highlight = node.extra.kind.hl,
3022
}
3123
end
3224

3325
M.kind_icon = M.icon
3426

3527
M.kind_name = function(config, node, state)
36-
local padding = config.padding or " "
37-
local name = node.extra.kind.name
38-
39-
name = config.align == "right" and padding .. name or name .. padding
40-
if node:get_depth() == 1 then
41-
name = ""
42-
end
43-
4428
return {
45-
text = name,
29+
text = node:get_depth() == 1 and "" or node.extra.kind.name,
4630
highlight = node.extra.kind.hl,
4731
}
4832
end
4933

5034
M.name = function(config, node, state)
51-
local highlight = node.extra.kind.hl or highlights.FILE_NAME
52-
local text = node.name
5335
return {
54-
text = text,
55-
highlight = highlight,
36+
text = node.name,
37+
highlight = node.extra.kind.hl or highlights.FILE_NAME,
5638
}
5739
end
5840

lua/neo-tree/sources/filesystem/components.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ M.current_filter = function(config, node, state)
2323
end
2424
return {
2525
{
26-
text = "Find ",
26+
text = "Find",
2727
highlight = highlights.DIM_TEXT,
2828
},
2929
{
3030
text = string.format('"%s"', filter),
3131
highlight = config.highlight or highlights.FILTER_TERM,
3232
},
3333
{
34-
text = " in ",
34+
text = "in",
3535
highlight = highlights.DIM_TEXT,
3636
},
3737
}

lua/neo-tree/sources/git_status/components.lua

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
local highlights = require("neo-tree.ui.highlights")
1414
local common = require("neo-tree.sources.common.components")
15-
local utils = require("neo-tree.utils")
1615

1716
local M = {}
1817

@@ -30,7 +29,7 @@ M.name = function(config, node, state)
3029
else
3130
highlight = highlights.DIRECTORY_NAME
3231
end
33-
else
32+
elseif config.use_git_status_colors then
3433
local git_status = state.components.git_status({}, node, state)
3534
if git_status and git_status.highlight then
3635
highlight = git_status.highlight

0 commit comments

Comments
 (0)