Skip to content

Commit 00d3739

Browse files
committed
show_on_dirs and show_on_open_dirs
1 parent e501f16 commit 00d3739

File tree

4 files changed

+50
-10
lines changed

4 files changed

+50
-10
lines changed

lua/nvim-tree.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ local copy_paste = require "nvim-tree.actions.fs.copy-paste"
1212
local collapse_all = require "nvim-tree.actions.tree-modifiers.collapse-all"
1313
local git = require "nvim-tree.git"
1414
local filters = require "nvim-tree.explorer.filters"
15+
local modified = require "nvim-tree.modified"
1516

1617
local _config = {}
1718

@@ -481,6 +482,7 @@ local function setup_autocommands(opts)
481482
create_nvim_tree_autocmd("BufModifiedSet", {
482483
callback = function()
483484
utils.debounce("BufModifiedSet:modified_files", opts.modified.debounce_delay, function()
485+
modified.reload()
484486
reloaders.reload_explorer()
485487
end)
486488
end,
@@ -828,6 +830,7 @@ function M.setup(conf)
828830
require("nvim-tree.renderer").setup(opts)
829831
require("nvim-tree.live-filter").setup(opts)
830832
require("nvim-tree.marks").setup(opts)
833+
require("nvim-tree.modified").setup(opts)
831834
if M.config.renderer.icons.show.file and pcall(require, "nvim-web-devicons") then
832835
require("nvim-web-devicons").setup()
833836
end

lua/nvim-tree/modified.lua

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
local M = {}
2+
3+
---@type table<string, boolean> record of which file is modified
4+
M.record = {}
5+
6+
---refresh M.record
7+
function M.reload()
8+
M.record = {}
9+
local bufs = vim.fn.getbufinfo { bufmodified = true, buflisted = true }
10+
for _, buf in pairs(bufs) do
11+
local path = buf.name
12+
M.record[path] = true
13+
while path ~= vim.fn.getcwd() and path ~= "/" do
14+
path = vim.fn.fnamemodify(path, ":h")
15+
M.record[path] = true
16+
end
17+
end
18+
end
19+
20+
---@param node table TODO: we should probably have a Node type
21+
---@return boolean
22+
function M.is_modified(node)
23+
return M.config.enable
24+
and M.record[node.absolute_path]
25+
and (not node.nodes or M.config.show_on_dirs)
26+
and (not node.open or M.config.show_on_open_dirs)
27+
end
28+
29+
---@param opts table
30+
function M.setup(opts)
31+
M.config = opts.modified
32+
end
33+
34+
return M

lua/nvim-tree/renderer/builder.lua

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local utils = require "nvim-tree.utils"
22
local core = require "nvim-tree.core"
3+
local modified = require "nvim-tree.modified"
34

45
local git = require "nvim-tree.renderer.components.git"
56
local pad = require "nvim-tree.renderer.components.padding"
@@ -68,10 +69,11 @@ function Builder:configure_git_icons_placement(where)
6869
return self
6970
end
7071

71-
function Builder:configure_modified(modified_icon, modified_placement, modified)
72-
self.modified = modified
73-
self.modified.icon = modified_icon
74-
self.modified.placement = modified_placement
72+
function Builder:configure_modified(modified_icon, modified_placement)
73+
self.modified = {
74+
icon = modified_icon,
75+
placement = modified_placement,
76+
}
7577
return self
7678
end
7779

@@ -118,6 +120,9 @@ function Builder:_build_folder(node, padding, git_hl, git_icons_tbl)
118120
local offset = string.len(padding)
119121

120122
local name = get_folder_name(node)
123+
if modified.is_modified(node) then
124+
name = name .. self.modified.icon
125+
end
121126
local has_children = #node.nodes ~= 0 or node.has_children
122127
local icon = icons.get_folder_icon(node.open, node.link_to ~= nil, has_children)
123128

@@ -213,7 +218,7 @@ function Builder:_highlight_opened_files(node, offset, icon_length, git_icons_le
213218
self:_insert_highlight("NvimTreeOpenedFile", from, to)
214219
end
215220

216-
function Builder:_build_file(node, padding, git_highlight, git_icons_tbl, unloaded_bufnr, modified)
221+
function Builder:_build_file(node, padding, git_highlight, git_icons_tbl, unloaded_bufnr)
217222
local offset = string.len(padding)
218223

219224
local icon = self:_build_file_icon(node, offset)
@@ -222,7 +227,7 @@ function Builder:_build_file(node, padding, git_highlight, git_icons_tbl, unload
222227
local git_icons = self:_unwrap_git_data(git_icons_tbl, git_icons_starts_at)
223228

224229
local name = node.name
225-
if modified then
230+
if modified.is_modified(node) then
226231
name = name .. self.modified.icon
227232
end
228233
self:_insert_line(self:_format_line(padding .. icon, name, git_icons))
@@ -274,8 +279,6 @@ function Builder:_build_line(node, idx, num_children, unloaded_bufnr)
274279
end
275280
end
276281

277-
local modified = vim.fn.bufloaded(node.absolute_path) > 0 and vim.fn.getbufinfo(node.absolute_path)[1].changed == 1
278-
279282
local is_folder = node.nodes ~= nil
280283
local is_symlink = node.link_to ~= nil
281284

@@ -284,7 +287,7 @@ function Builder:_build_line(node, idx, num_children, unloaded_bufnr)
284287
elseif is_symlink then
285288
self:_build_symlink(node, padding, git_highlight, git_icons_tbl)
286289
else
287-
self:_build_file(node, padding, git_highlight, git_icons_tbl, unloaded_bufnr, modified)
290+
self:_build_file(node, padding, git_highlight, git_icons_tbl, unloaded_bufnr)
288291
end
289292
self.index = self.index + 1
290293

lua/nvim-tree/renderer/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function M.draw(unloaded_bufnr)
7070
:configure_opened_file_highlighting(M.config.highlight_opened_files)
7171
:configure_git_icons_padding(M.config.icons.padding)
7272
:configure_git_icons_placement(M.config.icons.git_placement)
73-
:configure_modified(M.config.icons.glyphs.modified, M.config.icons.modified_placement, M.config.modified)
73+
:configure_modified(M.config.icons.glyphs.modified, M.config.icons.modified_placement)
7474
:configure_symlink_destination(M.config.symlink_destination)
7575
:configure_filter(live_filter.filter, live_filter.prefix)
7676
:build_header(view.is_root_folder_visible(core.get_cwd()))

0 commit comments

Comments
 (0)