Skip to content

Commit b1654ce

Browse files
committed
add DecoratorOpened
1 parent 5fb81f9 commit b1654ce

File tree

11 files changed

+142
-116
lines changed

11 files changed

+142
-116
lines changed

doc/nvim-tree-lua.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2243,6 +2243,9 @@ Modified: >
22432243
NvimTreeModifiedFileHL NvimTreeModifiedIcon
22442244
NvimTreeModifiedFolderHL NvimTreeModifiedIcon
22452245
<
2246+
Opened: >
2247+
NvimTreeOpenedHL Constant
2248+
<
22462249
Picker: >
22472250
NvimTreeWindowPicker guifg=#ededed guibg=#4493c8 gui=bold ctermfg=White ctermbg=Cyan
22482251
<
@@ -2326,6 +2329,7 @@ Legacy highlight group are still obeyed when they are defined and the current
23262329
highlight group is not, hard linking as follows: >
23272330
23282331
NvimTreeModifiedIcon NvimTreeModifiedFile
2332+
NvimTreeOpenedHL NvimTreeOpenedFile
23292333
23302334
NvimTreeGitDeletedIcon NvimTreeGitDeleted
23312335
NvimTreeGitDirtyIcon NvimTreeGitDirty

lua/nvim-tree.lua

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ local core = require "nvim-tree.core"
1111
local reloaders = require "nvim-tree.actions.reloaders.reloaders"
1212
local git = require "nvim-tree.git"
1313
local filters = require "nvim-tree.explorer.filters"
14-
local modified = require "nvim-tree.modified"
14+
local buffers = require "nvim-tree.buffers"
1515
local find_file = require "nvim-tree.actions.tree.find-file"
1616
local open = require "nvim-tree.actions.tree.open"
1717
local events = require "nvim-tree.events"
@@ -231,7 +231,9 @@ local function setup_autocommands(opts)
231231
-- update opened file buffers
232232
if (filters.config.filter_no_buffer or renderer.config.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == "" then
233233
utils.debounce("Buf:filter_buffer", opts.view.debounce_delay, function()
234-
reloaders.reload_explorer(nil, data.buf)
234+
buffers.set_unloaded_bufnr(data.buf)
235+
reloaders.reload_explorer()
236+
buffers.reset_unloaded_bufnr()
235237
end)
236238
end
237239
end,
@@ -335,7 +337,7 @@ local function setup_autocommands(opts)
335337
create_nvim_tree_autocmd({ "BufModifiedSet", "BufWritePost" }, {
336338
callback = function()
337339
utils.debounce("Buf:modified", opts.view.debounce_delay, function()
338-
modified.reload()
340+
buffers.reload_modified()
339341
reloaders.reload_explorer()
340342
end)
341343
end,
@@ -809,7 +811,7 @@ function M.setup(conf)
809811
require("nvim-tree.renderer").setup(opts)
810812
require("nvim-tree.live-filter").setup(opts)
811813
require("nvim-tree.marks").setup(opts)
812-
require("nvim-tree.modified").setup(opts)
814+
require("nvim-tree.buffers").setup(opts)
813815
require("nvim-tree.help").setup(opts)
814816
require("nvim-tree.watcher").setup(opts)
815817
if M.config.renderer.icons.show.file and pcall(require, "nvim-web-devicons") then

lua/nvim-tree/actions/reloaders/reloaders.lua

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ local Iterator = require "nvim-tree.iterators.node-iterator"
88

99
local M = {}
1010

11-
local function refresh_nodes(node, projects, unloaded_bufnr)
11+
local function refresh_nodes(node, projects)
1212
Iterator.builder({ node })
1313
:applier(function(n)
1414
if n.open and n.nodes then
1515
local toplevel = git.get_toplevel(n.cwd or n.link_to or n.absolute_path)
16-
explorer_module.reload(n, projects[toplevel] or {}, unloaded_bufnr)
16+
explorer_module.reload(n, projects[toplevel] or {})
1717
end
1818
end)
1919
:recursor(function(n)
@@ -34,18 +34,16 @@ function M.reload_node_status(parent_node, projects)
3434
end
3535

3636
local event_running = false
37-
---@param _ table|nil unused node passed by action
38-
---@param unloaded_bufnr number|nil optional bufnr recently unloaded via BufUnload event
39-
function M.reload_explorer(_, unloaded_bufnr)
37+
function M.reload_explorer()
4038
if event_running or not core.get_explorer() or vim.v.exiting ~= vim.NIL then
4139
return
4240
end
4341
event_running = true
4442

4543
local projects = git.reload()
46-
refresh_nodes(core.get_explorer(), projects, unloaded_bufnr)
44+
refresh_nodes(core.get_explorer(), projects)
4745
if view.is_visible() then
48-
renderer.draw(unloaded_bufnr)
46+
renderer.draw()
4947
end
5048
event_running = false
5149
end

lua/nvim-tree/buffers.lua

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
local M = {}
2+
3+
---@type table<string, boolean> record of which file is modified
4+
M._modified = {}
5+
6+
---@type number unloaded_bufnr for the duration of BufUnload
7+
M._unloaded_bufnr = nil
8+
9+
---refresh M._modified
10+
function M.reload_modified()
11+
M._modified = {}
12+
local bufs = vim.fn.getbufinfo { bufmodified = true, buflisted = true }
13+
for _, buf in pairs(bufs) do
14+
local path = buf.name
15+
if path ~= "" then -- not a [No Name] buffer
16+
-- mark all the parent as modified as well
17+
while
18+
M._modified[path] ~= true
19+
-- no need to keep going if already recorded
20+
-- This also prevents an infinite loop
21+
do
22+
M._modified[path] = true
23+
path = vim.fn.fnamemodify(path, ":h")
24+
end
25+
end
26+
end
27+
end
28+
29+
---@param node table
30+
---@return boolean
31+
function M.is_modified(node)
32+
return node
33+
and M.config.modified.enable
34+
and M._modified[node.absolute_path]
35+
and (not node.nodes or M.config.modified.show_on_dirs)
36+
and (not node.open or M.config.modified.show_on_open_dirs)
37+
end
38+
39+
---A buffer exists for the node's absolute path and it's not in the middle of a BufUnload handler.
40+
---@param node table
41+
---@return boolean
42+
function M.is_opened(node)
43+
return node and vim.fn.bufloaded(node.absolute_path) > 0 and vim.fn.bufnr(node.absolute_path) ~= M._unloaded_bufnr
44+
end
45+
46+
---Set the unloaded bufnr - at the start of the BufUnload handler.
47+
---@param bufnr number
48+
function M.set_unloaded_bufnr(bufnr)
49+
M._unloaded_bufnr = bufnr
50+
end
51+
52+
---Reset the unloaded bufnr - at the end of the BufUnload handler.
53+
function M.reset_unloaded_bufnr()
54+
M._unloaded_bufnr = nil
55+
end
56+
57+
---@param opts table
58+
function M.setup(opts)
59+
M.config = {
60+
modified = opts.modified,
61+
}
62+
end
63+
64+
return M

lua/nvim-tree/colors.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ local DEFAULT_LINKS = {
6969
NvimTreeModifiedFileHL = "NvimTreeModifiedIcon",
7070
NvimTreeModifiedFolderHL = "NvimTreeModifiedFileHL",
7171

72+
-- Opened
73+
NvimTreeOpenedHL = "Constant",
74+
7275
-- LiveFilter
7376
NvimTreeLiveFilterPrefix = "PreProc",
7477
NvimTreeLiveFilterValue = "ModeMsg",
@@ -123,6 +126,8 @@ local DEFAULT_LINKS = {
123126
local LEGACY_LINKS = {
124127
NvimTreeModifiedIcon = "NvimTreeModifiedFile",
125128

129+
NvimTreeOpenedHL = "NvimTreeOpenedFile",
130+
126131
NvimTreeGitDeletedIcon = "NvimTreeGitDeleted",
127132
NvimTreeGitDirtyIcon = "NvimTreeGitDirty",
128133
NvimTreeGitIgnoredIcon = "NvimTreeGitIgnored",

lua/nvim-tree/modified.lua

Lines changed: 0 additions & 41 deletions
This file was deleted.

lua/nvim-tree/renderer/builder.lua

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,6 @@ function Builder:configure_filter(filter, prefix)
5353
return self
5454
end
5555

56-
function Builder:configure_opened_file_highlighting(highlight_opened_files)
57-
self.highlight_opened_files = highlight_opened_files
58-
return self
59-
end
60-
6156
function Builder:configure_icon_padding(padding)
6257
self.icon_padding = padding or " "
6358
return self
@@ -242,41 +237,6 @@ function Builder:_get_bookmark_icon(node)
242237
return bookmark_icon
243238
end
244239

245-
---@param node table
246-
---@return string|nil icon_hl
247-
---@return string|nil name_hl
248-
function Builder:_get_highlight_override(node, unloaded_bufnr)
249-
local name_hl, icon_hl
250-
251-
-- opened file
252-
if self.highlight_opened_files and vim.fn.bufloaded(node.absolute_path) > 0 and vim.fn.bufnr(node.absolute_path) ~= unloaded_bufnr then
253-
if self.highlight_opened_files == "all" or self.highlight_opened_files == "name" then
254-
name_hl = "NvimTreeOpenedFile"
255-
end
256-
if self.highlight_opened_files == "all" or self.highlight_opened_files == "icon" then
257-
icon_hl = "NvimTreeOpenedFileIcon"
258-
end
259-
end
260-
return icon_hl, name_hl
261-
end
262-
263-
---Append optional highlighting to icon or name.
264-
---@param node table
265-
---@param get_hl fun(node: table): HL_POSITION, string
266-
---@param icon_hl string[] icons to append to
267-
---@param name_hl string[] names to append to
268-
function Builder:_append_highlight(node, get_hl, icon_hl, name_hl)
269-
local pos, hl = get_hl(node)
270-
if pos ~= HL_POSITION.none and hl then
271-
if pos == HL_POSITION.all or pos == HL_POSITION.icon then
272-
table.insert(icon_hl, hl)
273-
end
274-
if pos == HL_POSITION.all or pos == HL_POSITION.name then
275-
table.insert(name_hl, hl)
276-
end
277-
end
278-
end
279-
280240
---Append optional highlighting to icon or name.
281241
---@param node table
282242
---@param decorator Decorator
@@ -354,7 +314,7 @@ function Builder:_format_line(indent_markers, arrows, icon, name, git_icons, dia
354314
return line
355315
end
356316

357-
function Builder:_build_line(node, idx, num_children, unloaded_bufnr)
317+
function Builder:_build_line(node, idx, num_children)
358318
-- various components
359319
local indent_markers = pad.get_indent_markers(self.depth, idx, num_children, node, self.markers)
360320
local arrows = pad.get_arrows(node)
@@ -377,17 +337,9 @@ function Builder:_build_line(node, idx, num_children, unloaded_bufnr)
377337
icon, name = self:_build_file(node)
378338
end
379339

380-
-- highlight override
381-
local icon_hl_override, name_hl_override = self:_get_highlight_override(node, unloaded_bufnr)
382-
if icon_hl_override then
383-
icon.hl = { icon_hl_override }
384-
end
385-
if name_hl_override then
386-
name.hl = { name_hl_override }
387-
end
388-
389340
-- extra highighting
390341
self:_append_dec_highlight(node, self.decorators.git, icon.hl, name.hl)
342+
self:_append_dec_highlight(node, self.decorators.opened, icon.hl, name.hl)
391343
self:_append_dec_highlight(node, self.decorators.modified, icon.hl, name.hl)
392344
self:_append_dec_highlight(node, self.decorators.bookmarks, icon.hl, name.hl)
393345
self:_append_dec_highlight(node, self.decorators.diagnostics, icon.hl, name.hl)
@@ -403,7 +355,7 @@ function Builder:_build_line(node, idx, num_children, unloaded_bufnr)
403355

404356
if node.open then
405357
self.depth = self.depth + 1
406-
self:build(node, unloaded_bufnr)
358+
self:build(node)
407359
self.depth = self.depth - 1
408360
end
409361
end
@@ -422,12 +374,12 @@ function Builder:_get_nodes_number(nodes)
422374
return i
423375
end
424376

425-
function Builder:build(tree, unloaded_bufnr)
377+
function Builder:build(tree)
426378
local num_children = self:_get_nodes_number(tree.nodes)
427379
local idx = 1
428380
for _, node in ipairs(tree.nodes) do
429381
if not node.hidden then
430-
self:_build_line(node, idx, num_children, unloaded_bufnr)
382+
self:_build_line(node, idx, num_children)
431383
idx = idx + 1
432384
end
433385
end

lua/nvim-tree/renderer/decorator/init.lua

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ end
1616
---@diagnostic disable: unused-local
1717

1818
--- Node icon
19-
--- @param node table
19+
--- @param _ table node
2020
--- @return HighlightedString|nil modified icon
21-
function Decorator:get_icon(node) end
21+
function Decorator:get_icon(_) end
2222

2323
--- Node highlight group
24-
--- @param node table
24+
--- @param _ table node
2525
--- @return string|nil group
26-
function Decorator:get_highlight(node) end
26+
function Decorator:get_highlight(_) end
2727

2828
---@diagnostic enable: unused-local
2929

@@ -32,7 +32,13 @@ function Decorator:get_highlight(node) end
3232
--- @param icon HighlightedString|nil
3333
function Decorator:define_sign(icon)
3434
if icon and #icon.hl > 0 then
35-
vim.fn.sign_define(icon.hl[1], {
35+
local name = icon.hl[1]
36+
37+
if not vim.tbl_isempty(vim.fn.sign_getdefined(name)) then
38+
vim.fn.sign_undefine(name)
39+
end
40+
41+
vim.fn.sign_define(name, {
3642
text = icon.str,
3743
texthl = icon.hl[1],
3844
})

lua/nvim-tree/renderer/decorator/modified.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
local modified = require "nvim-tree.modified"
1+
local buffers = require "nvim-tree.buffers"
22

33
local HL_POSITION = require("nvim-tree.enum").HL_POSITION
44
local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT
@@ -37,14 +37,14 @@ end
3737

3838
--- Modified icon: modified.enable, renderer.icons.show.modified and node is modified
3939
function DecoratorModified:get_icon(node)
40-
if self.enabled and modified.is_modified(node) then
40+
if self.enabled and buffers.is_modified(node) then
4141
return self.icon
4242
end
4343
end
4444

4545
--- Modified highlight: modified.enable, renderer.highlight_modified and node is modified
4646
function DecoratorModified:get_highlight(node)
47-
if not self.enabled or self.hl_pos == HL_POSITION.none or not modified.is_modified(node) then
47+
if not self.enabled or self.hl_pos == HL_POSITION.none or not buffers.is_modified(node) then
4848
return nil
4949
end
5050

0 commit comments

Comments
 (0)