Skip to content

Commit 44310cf

Browse files
authored
fix: reset faded hl cache on colorscheme change, allow disable char fading, fixes #311, fixes #291 (#312)
1 parent af23225 commit 44310cf

File tree

5 files changed

+87
-51
lines changed

5 files changed

+87
-51
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ use {
121121
enable_git_status = true,
122122
enable_diagnostics = true,
123123
default_component_configs = {
124+
container = {
125+
enable_character_fade = true
126+
},
124127
indent = {
125128
indent_size = 2,
126129
padding = 1, -- extra padding on left hand side

lua/neo-tree/defaults.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ local config = {
6868
-- }
6969
--},
7070
default_component_configs = {
71+
container = {
72+
enable_character_fade = true
73+
},
7174
indent = {
7275
indent_size = 2,
7376
padding = 1,

lua/neo-tree/setup/init.lua

Lines changed: 64 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,6 @@ local function merge_global_components_config(components, config)
227227
if name == "indent" then
228228
indent_exists = true
229229
end
230-
if name == "container" then
231-
for i, child in ipairs(component.content) do
232-
component.content[i] = do_merge(child)
233-
end
234-
return component
235-
end
236230
local merged = { name }
237231
local global_config = config.default_component_configs[name]
238232
if global_config then
@@ -243,6 +237,11 @@ local function merge_global_components_config(components, config)
243237
for k, v in pairs(component) do
244238
merged[k] = v
245239
end
240+
if name == "container" then
241+
for i, child in ipairs(component.content) do
242+
merged.content[i] = do_merge(child)
243+
end
244+
end
246245
return merged
247246
else
248247
log.error("component name is the wrong type", component)
@@ -267,11 +266,51 @@ local function merge_global_components_config(components, config)
267266
return merged_components
268267
end
269268

270-
M.merge_config = function(config, is_auto_config)
269+
local merge_renderers = function (default_config, source_default_config, user_config)
270+
-- This can't be a deep copy/merge. If a renderer is specified in the target it completely
271+
-- replaces the base renderer.
272+
273+
if source_default_config == nil then
274+
-- first override the default config global renderer with the user's global renderers
275+
for name, renderer in pairs(user_config.renderers or {}) do
276+
log.debug("overriding global renderer for " .. name)
277+
default_config.renderers[name] = renderer
278+
end
279+
else
280+
-- then override the global renderers with the source specific renderers
281+
source_default_config.renderers = source_default_config.renderers or {}
282+
for name, renderer in pairs(default_config.renderers or {}) do
283+
if source_default_config.renderers[name] == nil then
284+
log.debug("overriding source renderer for " .. name)
285+
local r = {}
286+
-- Only copy components that exist in the target source.
287+
-- This alllows us to specify global renderers that include components from all sources,
288+
-- even if some of those components are not universal
289+
for _, value in ipairs(renderer) do
290+
if value[1] and source_default_config.components[value[1]] ~= nil then
291+
table.insert(r, value)
292+
end
293+
end
294+
source_default_config.renderers[name] = r
295+
end
296+
end
297+
298+
-- if user sets renderers, completely wipe the default ones
299+
local source_name = source_default_config.name
300+
for name, _ in pairs(source_default_config.renderers) do
301+
local user = utils.get_value(user_config, source_name .. ".renderers." .. name)
302+
if user then
303+
source_default_config.renderers[name] = nil
304+
end
305+
end
306+
end
307+
end
308+
309+
M.merge_config = function(user_config, is_auto_config)
271310
local default_config = vim.deepcopy(defaults)
272-
config = vim.deepcopy(config or {})
311+
user_config = vim.deepcopy(user_config or {})
273312

274-
local migrations = require("neo-tree.setup.deprecations").migrate(config)
313+
local migrations = require("neo-tree.setup.deprecations").migrate(user_config)
275314
if #migrations > 0 then
276315
-- defer to make sure it is the last message printed
277316
vim.defer_fn(function()
@@ -281,10 +320,10 @@ M.merge_config = function(config, is_auto_config)
281320
end, 50)
282321
end
283322

284-
if config.log_level ~= nil then
285-
M.set_log_level(config.log_level)
323+
if user_config.log_level ~= nil then
324+
M.set_log_level(user_config.log_level)
286325
end
287-
log.use_file(config.log_to_file, true)
326+
log.use_file(user_config.log_to_file, true)
288327
log.debug("setup")
289328

290329
events.clear_all_events()
@@ -296,8 +335,8 @@ M.merge_config = function(config, is_auto_config)
296335
handler = M.buffer_enter_event,
297336
})
298337

299-
if config.event_handlers ~= nil then
300-
for _, handler in ipairs(config.event_handlers) do
338+
if user_config.event_handlers ~= nil then
339+
for _, handler in ipairs(user_config.event_handlers) do
301340
events.subscribe(handler)
302341
end
303342
end
@@ -306,6 +345,7 @@ M.merge_config = function(config, is_auto_config)
306345

307346
-- setup the default values for all sources
308347
normalize_mappings(default_config)
348+
merge_renderers(default_config, nil, user_config)
309349
for _, source_name in ipairs(sources) do
310350
local source_default_config = default_config[source_name]
311351
local mod_root = "neo-tree.sources." .. source_name
@@ -315,47 +355,29 @@ M.merge_config = function(config, is_auto_config)
315355

316356
-- Make sure all the mappings are normalized so they will merge properly.
317357
normalize_mappings(source_default_config)
318-
normalize_mappings(config[source_name])
358+
normalize_mappings(user_config[source_name])
319359

320360
local use_default_mappings = default_config.use_default_mappings
321-
if type(config.use_default_mappings) ~= "nil" then
322-
use_default_mappings = config.use_default_mappings
361+
if type(user_config.use_default_mappings) ~= "nil" then
362+
use_default_mappings = user_config.use_default_mappings
323363
end
324364
if use_default_mappings then
325365
-- merge the global config with the source specific config
326366
source_default_config.window = vim.tbl_deep_extend(
327367
"force",
328368
default_config.window or {},
329369
source_default_config.window or {},
330-
config.window or {}
370+
user_config.window or {}
331371
)
332372
else
333-
source_default_config.window = config.window
334-
end
335-
source_default_config.renderers = source_default_config.renderers or {}
336-
-- if source does not specify a renderer, use the global default
337-
for name, renderer in pairs(default_config.renderers or {}) do
338-
if source_default_config.renderers[name] == nil then
339-
local r = {}
340-
for _, value in ipairs(renderer) do
341-
if value[1] and source_default_config.components[value[1]] ~= nil then
342-
table.insert(r, value)
343-
end
344-
end
345-
source_default_config.renderers[name] = r
346-
end
347-
end
348-
-- if user sets renderers, completely wipe the default ones
349-
for name, _ in pairs(source_default_config.renderers) do
350-
local user = utils.get_value(config, source_name .. ".renderers." .. name)
351-
if user then
352-
source_default_config.renderers[name] = nil
353-
end
373+
source_default_config.window = user_config.window
354374
end
355375

376+
merge_renderers(default_config, source_default_config, user_config)
377+
356378
--validate the window.position
357379
local pos_key = source_name .. ".window.position"
358-
local position = utils.get_value(config, pos_key, "left", true)
380+
local position = utils.get_value(user_config, pos_key, "left", true)
359381
local valid_positions = {
360382
left = true,
361383
right = true,
@@ -366,13 +388,13 @@ M.merge_config = function(config, is_auto_config)
366388
}
367389
if not valid_positions[position] then
368390
log.error("Invalid value for ", pos_key, ": ", position)
369-
config[source_name].window.position = "left"
391+
user_config[source_name].window.position = "left"
370392
end
371393
end
372394
--print(vim.inspect(default_config.filesystem))
373395

374396
-- apply the users config
375-
M.config = vim.tbl_deep_extend("force", default_config, config)
397+
M.config = vim.tbl_deep_extend("force", default_config, user_config)
376398
if not M.config.enable_git_status then
377399
M.config.git_status_async = false
378400
end

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local log = require("neo-tree.log")
55

66
local M = {}
77

8-
local calc_rendered_width = function (rendered_item)
8+
local calc_rendered_width = function(rendered_item)
99
local width = 0
1010

1111
for _, item in ipairs(rendered_item) do
@@ -48,7 +48,7 @@ local calc_container_width = function(config, node, state, context)
4848
return container_width
4949
end
5050

51-
local render_content = function (config, node, state, context)
51+
local render_content = function(config, node, state, context)
5252
local max_width = 0
5353

5454
local grouped_by_zindex = utils.group_by(config.content, "zindex")
@@ -75,7 +75,7 @@ end
7575
---@param layer table The list of rendered components.
7676
---@param skip_count number The number of characters to skip from the begining/left.
7777
---@param max_length number The maximum number of characters to return.
78-
local truncate_layer_keep_left = function (layer, skip_count, max_length)
78+
local truncate_layer_keep_left = function(layer, skip_count, max_length)
7979
local result = {}
8080
local taken = 0
8181
local skipped = 0
@@ -109,7 +109,7 @@ end
109109
---@param layer table The list of rendered components.
110110
---@param skip_count number The number of characters to skip from the end/right.
111111
---@param max_length number The maximum number of characters to return.
112-
local truncate_layer_keep_right = function (layer, skip_count, max_length)
112+
local truncate_layer_keep_right = function(layer, skip_count, max_length)
113113
local result = {}
114114
local taken = 0
115115
local skipped = 0
@@ -229,14 +229,18 @@ local merge_content = function(context)
229229
local width = calc_rendered_width(layer.left)
230230
if width > remaining_width then
231231
local truncated = truncate_layer_keep_left(layer.left, left_width, remaining_width)
232-
try_fade_content(truncated, 3)
232+
if context.enable_character_fade then
233+
try_fade_content(truncated, 3)
234+
end
233235
vim.list_extend(left, truncated)
234236
remaining_width = 0
235237
else
236238
remaining_width = remaining_width - width
237-
local fade_chars = 3 - remaining_width
238-
if fade_chars > 0 then
239-
try_fade_content(layer.left, fade_chars)
239+
if context.enable_character_fade then
240+
local fade_chars = 3 - remaining_width
241+
if fade_chars > 0 then
242+
try_fade_content(layer.left, fade_chars)
243+
end
240244
end
241245
vim.list_extend(left, layer.left)
242246
left_width = left_width + width
@@ -259,13 +263,14 @@ local merge_content = function(context)
259263
context.merged_content = result
260264
end
261265

262-
M.render = function (config, node, state, available_width)
266+
M.render = function(config, node, state, available_width)
263267
local context = {
264268
max_width = 0,
265269
grouped_by_zindex = {},
266270
available_width = available_width,
267271
left_padding = config.left_padding,
268272
right_padding = config.right_padding,
273+
enable_character_fade = config.enable_character_fade,
269274
}
270275

271276
render_content(config, node, state, context)

lua/neo-tree/ui/highlights.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ M.get_faded_highlight_group = function(hl_group_name, fade_percentage)
192192
end
193193

194194
M.setup = function()
195+
-- Reset this here in case of color scheme change
196+
faded_highlight_group_cache = {}
197+
195198
local normal_hl = create_highlight_group(M.NORMAL, { "Normal" })
196199
local normalnc_hl = create_highlight_group(M.NORMALNC, { "NormalNC", M.NORMAL })
197200

0 commit comments

Comments
 (0)