Skip to content

Commit 2621f53

Browse files
authored
feat: add highlight open files option (#838)
1 parent 85bba0c commit 2621f53

File tree

9 files changed

+70
-30
lines changed

9 files changed

+70
-30
lines changed

lua/neo-tree/defaults.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ local config = {
1919
enable_diagnostics = true,
2020
enable_git_status = true,
2121
enable_modified_markers = true, -- Show markers for files with unsaved changes.
22+
enable_opened_markers = true, -- Enable tracking of opened files. Required for `components.name.highlight_opened_files`
2223
enable_refresh_on_write = true, -- Refresh the tree when a file is written. Only used if `use_libuv_file_watcher` is false.
2324
git_status_async = true,
2425
-- These options are for people with VERY large git repos
@@ -210,6 +211,7 @@ local config = {
210211
},
211212
name = {
212213
trailing_slash = false,
214+
highlight_opened_files = false, -- Requires `enable_opened_markers = true`
213215
use_git_status_colors = true,
214216
highlight = "NeoTreeFileName",
215217
},

lua/neo-tree/setup/init.lua

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,24 @@ local define_events = function()
4747

4848
events.define_autocmd_event(events.VIM_BUFFER_CHANGED, { "BufWritePost", "BufFilePost" }, 200)
4949

50+
local update_opened_buffers = function(args)
51+
args.opened_buffers = utils.get_opened_buffers()
52+
return args
53+
end
5054
events.define_autocmd_event(
5155
events.VIM_BUFFER_MODIFIED_SET,
5256
{ "BufModifiedSet" },
5357
0,
54-
function(args)
55-
args.modified_buffers = utils.get_modified_buffers()
56-
return args
57-
end
58+
update_opened_buffers
5859
)
5960

60-
events.define_autocmd_event(events.VIM_BUFFER_ADDED, { "BufAdd" }, 200)
61-
events.define_autocmd_event(events.VIM_BUFFER_DELETED, { "BufDelete" }, 200)
61+
events.define_autocmd_event(events.VIM_BUFFER_ADDED, { "BufAdd" }, 200, update_opened_buffers)
62+
events.define_autocmd_event(
63+
events.VIM_BUFFER_DELETED,
64+
{ "BufDelete" },
65+
200,
66+
update_opened_buffers
67+
)
6268
events.define_autocmd_event(events.VIM_BUFFER_ENTER, { "BufEnter", "BufWinEnter" }, 0)
6369

6470
events.define_autocmd_event(events.VIM_TERMINAL_ENTER, { "TermEnter" }, 0)
@@ -278,10 +284,10 @@ M.win_enter_event = function()
278284
if state == nil then
279285
return
280286
end
281-
local mod = utils.get_modified_buffers()
287+
local mod = utils.get_opened_buffers()
282288
log.debug("close_if_last_window, modified files found: ", vim.inspect(mod))
283-
for filename, is_modified in pairs(mod) do
284-
if is_modified then
289+
for filename, buf_info in pairs(mod) do
290+
if buf_info.modified then
285291
if vim.startswith(filename, "[No Name]#") then
286292
bufnr = string.sub(filename, 11)
287293
log.trace("close_if_last_window, showing unnamed modified buffer: ", filename)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ local buffers_changed_internal = function()
5959
for _, tabnr in ipairs(vim.api.nvim_list_tabpages()) do
6060
local state = manager.get_state(M.name, tabnr)
6161
if state.path and renderer.window_exists(state) then
62-
items.get_open_buffers(state)
62+
items.get_opened_buffers(state)
6363
if state.follow_current_file then
6464
follow_internal()
6565
end
@@ -93,7 +93,7 @@ M.navigate = function(state, path, path_to_reveal)
9393
renderer.position.set(state, path_to_reveal)
9494
end
9595

96-
items.get_open_buffers(state)
96+
items.get_opened_buffers(state)
9797

9898
if path_changed and state.bind_to_cwd then
9999
vim.api.nvim_command("tcd " .. path)
@@ -168,7 +168,7 @@ M.setup = function(config, global_config)
168168
if global_config.enable_modified_markers then
169169
manager.subscribe(M.name, {
170170
event = events.VIM_BUFFER_MODIFIED_SET,
171-
handler = wrap(manager.modified_buffers_changed),
171+
handler = wrap(manager.opened_buffers_changed),
172172
})
173173
end
174174

lua/neo-tree/sources/buffers/lib/items.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ local M = {}
88

99
---Get a table of all open buffers, along with all parent paths of those buffers.
1010
---The paths are the keys of the table, and all the values are 'true'.
11-
M.get_open_buffers = function(state)
11+
M.get_opened_buffers = function(state)
1212
if state.loading then
1313
return
1414
end

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,10 @@ M.icon = function(config, node, state)
292292
end
293293

294294
M.modified = function(config, node, state)
295-
local modified_buffers = state.modified_buffers or {}
295+
local opened_buffers = state.opened_buffers or {}
296+
local buf_info = opened_buffers[node.path]
296297

297-
if modified_buffers[node.path] then
298+
if buf_info and buf_info.modified then
298299
return {
299300
text = (make_two_char(config.symbol) or "[+] "),
300301
highlight = config.highlight or highlights.MODIFIED,
@@ -327,6 +328,13 @@ M.name = function(config, node, state)
327328
end
328329
end
329330

331+
if config.highlight_opened_files then
332+
local opened_buffers = state.opened_buffers or {}
333+
if opened_buffers[node.path] ~= nil then
334+
highlight = highlights.FILE_NAME_OPENED
335+
end
336+
end
337+
330338
if type(config.right_padding) == "number" then
331339
if config.right_padding > 0 then
332340
text = text .. string.rep(" ", config.right_padding)

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,19 @@ M.setup = function(config, global_config)
365365
if global_config.enable_modified_markers then
366366
manager.subscribe(M.name, {
367367
event = events.VIM_BUFFER_MODIFIED_SET,
368-
handler = wrap(manager.modified_buffers_changed),
368+
handler = wrap(manager.opened_buffers_changed),
369369
})
370370
end
371371

372+
if global_config.enable_opened_markers then
373+
for _, event in ipairs({ events.VIM_BUFFER_ADDED, events.VIM_BUFFER_DELETED }) do
374+
manager.subscribe(M.name, {
375+
event = event,
376+
handler = wrap(manager.opened_buffers_changed),
377+
})
378+
end
379+
end
380+
372381
-- Configure event handler for follow_current_file option
373382
if config.follow_current_file then
374383
manager.subscribe(M.name, {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ M.setup = function(config, global_config)
7878
if global_config.enable_modified_markers then
7979
manager.subscribe(M.name, {
8080
event = events.VIM_BUFFER_MODIFIED_SET,
81-
handler = wrap(manager.modified_buffers_changed),
81+
handler = wrap(manager.opened_buffers_changed),
8282
})
8383
end
8484

lua/neo-tree/sources/manager.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -425,17 +425,17 @@ M.focus = function(source_name, path_to_reveal, callback)
425425
end
426426

427427
---Redraws the tree with updated modified markers without scanning the filesystem again.
428-
M.modified_buffers_changed = function(source_name, args)
428+
M.opened_buffers_changed = function(source_name, args)
429429
if not type(args) == "table" then
430-
error("modified_buffers_changed: args must be a table")
430+
error("opened_buffers_changed: args must be a table")
431431
end
432-
if type(args.modified_buffers) == "table" then
432+
if type(args.opened_buffers) == "table" then
433433
M._for_each_state(source_name, function(state)
434-
if utils.tbl_equals(args.modified_buffers, state.modified_buffers) then
434+
if utils.tbl_equals(args.opened_buffers, state.opened_buffers) then
435435
-- no changes, no need to redraw
436436
return
437437
end
438-
state.modified_buffers = args.modified_buffers
438+
state.opened_buffers = args.opened_buffers
439439
renderer.redraw(state)
440440
end)
441441
end

lua/neo-tree/utils.lua

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ M.debounce = function(id, fn, frequency_in_ms, strategy, action)
144144
end
145145

146146
--- Returns true if the contents of two tables are equal.
147-
M.tbl_equals = function (table1, table2)
147+
M.tbl_equals = function(table1, table2)
148148
-- same object
149149
if table1 == table2 then
150150
return true
@@ -241,18 +241,33 @@ M.get_diagnostic_counts = function()
241241
return lookup
242242
end
243243

244+
--- DEPRECATED: This will be removed in v3. Use `get_opened_buffers` instead.
244245
---Gets a lookup of all open buffers keyed by path with the modifed flag as the value
245-
---@return table
246+
---@return table opened_buffers { [buffer_name] = bool }
246247
M.get_modified_buffers = function()
247-
local modified_buffers = {}
248+
local opened_buffers = M.get_opened_buffers()
249+
for bufname, bufinfo in pairs(opened_buffers) do
250+
opened_buffers[bufname] = bufinfo.modified
251+
end
252+
return opened_buffers
253+
end
254+
255+
---Gets a lookup of all open buffers keyed by path with additional information
256+
---@return table opened_buffers { [buffer_name] = { modified = bool } }
257+
M.get_opened_buffers = function()
258+
local opened_buffers = {}
248259
for _, buffer in ipairs(vim.api.nvim_list_bufs()) do
249-
local buffer_name = vim.api.nvim_buf_get_name(buffer)
250-
if buffer_name == nil or buffer_name == "" then
251-
buffer_name = "[No Name]#" .. buffer
260+
if vim.api.nvim_buf_is_loaded(buffer) and vim.fn.buflisted(buffer) then
261+
local buffer_name = vim.api.nvim_buf_get_name(buffer)
262+
if buffer_name == nil or buffer_name == "" then
263+
buffer_name = "[No Name]#" .. buffer
264+
end
265+
opened_buffers[buffer_name] = {
266+
["modified"] = vim.api.nvim_buf_get_option(buffer, "modified"),
267+
}
252268
end
253-
modified_buffers[buffer_name] = vim.api.nvim_buf_get_option(buffer, "modified")
254269
end
255-
return modified_buffers
270+
return opened_buffers
256271
end
257272

258273
---Resolves some variable to a string. The object can be either a string or a

0 commit comments

Comments
 (0)