Skip to content

Commit 7d3282a

Browse files
committed
Merge remote-tracking branch 'origin/master' into 2415-highlight-overhaul
2 parents 5289342 + 46e1f77 commit 7d3282a

File tree

10 files changed

+85
-48
lines changed

10 files changed

+85
-48
lines changed

.github/workflows/semantic-pr-subject.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ jobs:
1010
semantic-pr-subject:
1111
runs-on: ubuntu-latest
1212
steps:
13-
- uses: amannn/action-semantic-pull-request@v5.3.0
13+
- uses: amannn/action-semantic-pull-request@v5.4.0
1414
env:
1515
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

doc/nvim-tree-lua.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,9 @@ Appends a trailing slash to folder names.
796796

797797
*nvim-tree.renderer.group_empty*
798798
Compact folders that only contain a single folder into one node.
799-
Type: `boolean`, Default: `false`
799+
Boolean or function that takes one argument (the relative path
800+
of grouped folders) and returns a string to be displayed.
801+
Type: `boolean | function(relative_path):string`, Default: `false`
800802

801803
*nvim-tree.renderer.full_name*
802804
Display node whose name length is wider than the width of nvim-tree window in

lua/nvim-tree.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,8 @@ local function setup_autocommands(opts)
283283
create_nvim_tree_autocmd("BufEnter", {
284284
pattern = "NvimTree_*",
285285
callback = function()
286-
if opts.reload_on_bufenter and not opts.filesystem_watchers.enable then
287-
if utils.is_nvim_tree_buf(0) then
286+
if utils.is_nvim_tree_buf(0) then
287+
if vim.fn.getcwd() ~= core.get_cwd() or (opts.reload_on_bufenter and not opts.filesystem_watchers.enable) then
288288
reloaders.reload_explorer()
289289
end
290290
end
@@ -640,6 +640,7 @@ local ACCEPTED_TYPES = {
640640
},
641641
},
642642
renderer = {
643+
group_empty = { "boolean", "function" },
643644
root_folder_label = { "function", "string", "boolean" },
644645
},
645646
actions = {

lua/nvim-tree/actions/moves/item.lua

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,20 @@ local M = {}
99
function M.fn(where, what)
1010
return function()
1111
local node_cur = lib.get_node_at_cursor()
12-
local nodes_by_line = utils.get_nodes_by_line(core.get_explorer().nodes, core.get_nodes_starting_line())
12+
local first_node_line = core.get_nodes_starting_line()
13+
local nodes_by_line = utils.get_nodes_by_line(core.get_explorer().nodes, first_node_line)
14+
local iter_start, iter_end, iter_step, cur, first, nex
1315

14-
local cur, first, prev, nex = nil, nil, nil, nil
15-
for line, node in pairs(nodes_by_line) do
16+
if where == "next" then
17+
iter_start, iter_end, iter_step = first_node_line, #nodes_by_line, 1
18+
elseif where == "prev" then
19+
iter_start, iter_end, iter_step = #nodes_by_line, first_node_line, -1
20+
end
21+
22+
for line = iter_start, iter_end, iter_step do
23+
local node = nodes_by_line[line]
1624
local valid = false
25+
1726
if what == "git" then
1827
valid = explorer_node.get_git_status(node) ~= nil
1928
elseif what == "diag" then
@@ -28,29 +37,16 @@ function M.fn(where, what)
2837

2938
if node == node_cur then
3039
cur = line
31-
elseif valid then
32-
if not cur then
33-
prev = line
34-
end
35-
if cur and not nex then
36-
nex = line
37-
break
38-
end
40+
elseif valid and cur then
41+
nex = line
42+
break
3943
end
4044
end
4145

42-
if where == "prev" then
43-
if prev then
44-
view.set_cursor { prev, 0 }
45-
end
46-
else
47-
if cur then
48-
if nex then
49-
view.set_cursor { nex, 0 }
50-
end
51-
elseif first then
52-
view.set_cursor { first, 0 }
53-
end
46+
if nex then
47+
view.set_cursor { nex, 0 }
48+
elseif vim.o.wrapscan and first then
49+
view.set_cursor { first, 0 }
5450
end
5551
end
5652
end

lua/nvim-tree/actions/node/open-file.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ end
299299

300300
local function edit_in_current_buf(filename)
301301
require("nvim-tree.view").abandon_current_window()
302-
vim.cmd("keepjumps edit " .. vim.fn.fnameescape(filename))
302+
vim.cmd("keepalt keepjumps edit " .. vim.fn.fnameescape(filename))
303303
end
304304

305305
function M.fn(mode, filename)

lua/nvim-tree/api.lua

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,26 @@ local function wrap(f)
4040
end
4141
end
4242

43-
--- Inject the node as the first argument if absent.
44-
--- f function to invoke
45-
local function wrap_node(f)
43+
---Inject the node as the first argument if absent.
44+
---@param fn function function to invoke
45+
local function wrap_node(fn)
4646
return function(node, ...)
4747
node = node or require("nvim-tree.lib").get_node_at_cursor()
4848
if node then
49-
f(node, ...)
49+
fn(node, ...)
5050
end
5151
end
5252
end
5353

54+
---Inject the node or nil as the first argument if absent.
55+
---@param fn function function to invoke
56+
local function wrap_node_or_nil(fn)
57+
return function(node, ...)
58+
node = node or require("nvim-tree.lib").get_node_at_cursor()
59+
fn(node, ...)
60+
end
61+
end
62+
5463
---@class ApiTreeOpenOpts
5564
---@field path string|nil path
5665
---@field current_window boolean|nil default false
@@ -136,7 +145,7 @@ Api.tree.is_tree_buf = wrap(require("nvim-tree.utils").is_nvim_tree_buf)
136145

137146
Api.tree.is_visible = wrap(require("nvim-tree.view").is_visible)
138147

139-
Api.fs.create = wrap_node(require("nvim-tree.actions.fs.create-file").fn)
148+
Api.fs.create = wrap_node_or_nil(require("nvim-tree.actions.fs.create-file").fn)
140149
Api.fs.remove = wrap_node(require("nvim-tree.actions.fs.remove-file").fn)
141150
Api.fs.trash = wrap_node(require("nvim-tree.actions.fs.trash").fn)
142151
Api.fs.rename_node = wrap_node(require("nvim-tree.actions.fs.rename-file").fn ":t")

lua/nvim-tree/explorer/node.lua

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ local function get_dir_git_status(parent_ignored, status, absolute_path)
1414
return { file = "!!" }
1515
end
1616

17-
return {
18-
file = status.files and status.files[absolute_path],
19-
dir = status.dirs and {
20-
direct = status.dirs.direct[absolute_path],
21-
indirect = status.dirs.indirect[absolute_path],
22-
},
23-
}
17+
if status then
18+
return {
19+
file = status.files and status.files[absolute_path],
20+
dir = status.dirs and {
21+
direct = status.dirs.direct[absolute_path],
22+
indirect = status.dirs.indirect[absolute_path],
23+
},
24+
}
25+
end
2426
end
2527

2628
local function get_git_status(parent_ignored, status, absolute_path)

lua/nvim-tree/live-filter.lua

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ local function remove_overlay()
3737
})
3838
end
3939

40-
vim.api.nvim_win_close(overlay_winnr, { force = true })
40+
vim.api.nvim_win_close(overlay_winnr, true)
41+
vim.api.nvim_buf_delete(overlay_bufnr, { force = true })
4142
overlay_bufnr = nil
4243
overlay_winnr = nil
4344

@@ -104,25 +105,32 @@ local function configure_buffer_overlay()
104105
vim.api.nvim_buf_set_keymap(overlay_bufnr, "i", "<CR>", "<cmd>stopinsert<CR>", {})
105106
end
106107

108+
local function calculate_overlay_win_width()
109+
local wininfo = vim.fn.getwininfo(view.get_winnr())[1]
110+
111+
if wininfo then
112+
return wininfo.width - wininfo.textoff - #M.prefix
113+
end
114+
115+
return 20
116+
end
117+
107118
local function create_overlay()
108-
local min_width = 20
109119
if view.View.float.enable then
110120
-- don't close nvim-tree float when focus is changed to filter window
111121
vim.api.nvim_clear_autocmds {
112122
event = "WinLeave",
113123
pattern = "NvimTree_*",
114124
group = vim.api.nvim_create_augroup("NvimTree", { clear = false }),
115125
}
116-
117-
min_width = min_width - 2
118126
end
119127

120128
configure_buffer_overlay()
121129
overlay_winnr = vim.api.nvim_open_win(overlay_bufnr, true, {
122130
col = 1,
123131
row = 0,
124132
relative = "cursor",
125-
width = math.max(min_width, vim.api.nvim_win_get_width(view.get_winnr()) - #M.prefix - 2),
133+
width = calculate_overlay_win_width(),
126134
height = 1,
127135
border = "none",
128136
style = "minimal",

lua/nvim-tree/renderer/builder.lua

Lines changed: 21 additions & 3 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 notify = require "nvim-tree.notify"
34

45
local pad = require "nvim-tree.renderer.components.padding"
56
local icons = require "nvim-tree.renderer.components.icons"
@@ -63,6 +64,13 @@ function Builder:configure_symlink_destination(show)
6364
return self
6465
end
6566

67+
function Builder:configure_group_name_modifier(group_name_modifier)
68+
if type(group_name_modifier) == "function" then
69+
self.group_name_modifier = group_name_modifier
70+
end
71+
return self
72+
end
73+
6674
function Builder:_insert_highlight(group, start, end_)
6775
table.insert(self.highlights, { group, self.index, start, end_ or -1 })
6876
end
@@ -71,14 +79,24 @@ function Builder:_insert_line(line)
7179
table.insert(self.lines, line)
7280
end
7381

74-
local function get_folder_name(node)
82+
function Builder:_get_folder_name(node)
7583
local name = node.name
7684
local next = node.group_next
7785
while next do
7886
name = name .. "/" .. next.name
7987
next = next.group_next
8088
end
81-
return name
89+
90+
if node.group_next and self.group_name_modifier then
91+
local new_name = self.group_name_modifier(name)
92+
if type(new_name) == "string" then
93+
name = new_name
94+
else
95+
notify.warn(string.format("Invalid return type for field renderer.group_empty. Expected string, got %s", type(new_name)))
96+
end
97+
end
98+
99+
return name .. self.trailing_slash
82100
end
83101

84102
---@class HighlightedString
@@ -110,7 +128,7 @@ end
110128
function Builder:_build_folder(node)
111129
local has_children = #node.nodes ~= 0 or node.has_children
112130
local icon, icon_hl = icons.get_folder_icon(node, has_children)
113-
local foldername = get_folder_name(node) .. self.trailing_slash
131+
local foldername = self:_get_folder_name(node)
114132

115133
if #icon > 0 and icon_hl == nil then
116134
if node.open then

lua/nvim-tree/renderer/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ function M.draw()
7979
:configure_icon_padding(M.config.icons.padding)
8080
:configure_symlink_destination(M.config.symlink_destination)
8181
:configure_filter(live_filter.filter, live_filter.prefix)
82+
:configure_group_name_modifier(M.config.group_empty)
8283
:build_header(view.is_root_folder_visible(core.get_cwd()))
8384
:build(core.get_explorer())
8485
:unwrap()

0 commit comments

Comments
 (0)