Skip to content

Commit 87d49ec

Browse files
committed
Merge branch '2415-highlight-overhaul' into 2415-highlight-open-modified-additive
2 parents cc65530 + 506c195 commit 87d49ec

File tree

11 files changed

+157
-45
lines changed

11 files changed

+157
-45
lines changed

.github/ISSUE_TEMPLATE/nvt-min.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,13 @@ _G.setup = function()
3535
require("nvim-tree").setup {}
3636
end
3737

38+
-- UNCOMMENT this block for diagnostics issues, substituting pattern and cmd as appropriate.
39+
-- Requires diagnostics.enable = true in setup.
40+
--[[
41+
vim.api.nvim_create_autocmd("FileType", {
42+
pattern = "lua",
43+
callback = function()
44+
vim.lsp.start { cmd = { "lua-language-server" } }
45+
end,
46+
})
47+
]]

doc/nvim-tree-lua.txt

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ CONTENTS *nvim-tree*
2929
5.14 Opts: Trash |nvim-tree-opts-trash|
3030
5.15 Opts: Tab |nvim-tree-opts-tab|
3131
5.16 Opts: Notify |nvim-tree-opts-notify|
32-
5.17 Opts: UI |nvim-tree-opts-ui|
33-
5.18 Opts: Experimental |nvim-tree-opts-experimental|
34-
5.19 Opts: Log |nvim-tree-opts-log|
32+
5.17 Opts: Help |nvim-tree-opts-help|
33+
5.18 Opts: UI |nvim-tree-opts-ui|
34+
5.19 Opts: Experimental |nvim-tree-opts-experimental|
35+
5.20 Opts: Log |nvim-tree-opts-log|
3536
6. API |nvim-tree-api|
3637
6.1 API Tree |nvim-tree-api.tree|
3738
6.2 API File System |nvim-tree-api.fs|
@@ -566,10 +567,14 @@ Following is the default configuration. See |nvim-tree-opts| for details.
566567
threshold = vim.log.levels.INFO,
567568
absolute_path = true,
568569
},
570+
help = {
571+
sort_by = "key",
572+
},
569573
ui = {
570574
confirm = {
571575
remove = true,
572576
trash = true,
577+
default_yes = false,
573578
},
574579
},
575580
experimental = {},
@@ -1436,7 +1441,16 @@ Whether to use absolute paths or item names in fs action notifications.
14361441
Type: `boolean`, Default: `true`
14371442

14381443
==============================================================================
1439-
5.17 OPTS: UI *nvim-tree-opts-ui*
1444+
5.17 OPTS: HELP *nvim-tree-opts-help*
1445+
1446+
*nvim-tree.help.sort_by*
1447+
Defines how mappings are sorted in the help window.
1448+
Can be `"key"` (sort alphabetically by keymap)
1449+
or `"desc"` (sort alphabetically by description).
1450+
Type: `string`, Default: `"key"`
1451+
1452+
==============================================================================
1453+
5.18 OPTS: UI *nvim-tree-opts-ui*
14401454

14411455
*nvim-tree.ui.confirm*
14421456
Confirmation prompts.
@@ -1449,15 +1463,19 @@ Confirmation prompts.
14491463
Prompt before trashing.
14501464
Type: `boolean`, Default: `true`
14511465

1466+
*nvim-tree.ui.confirm.default_yes*
1467+
If `true` the prompt will be `"Y/n"`, otherwise `"y/N"`.
1468+
Type: `boolean`, Default: `false`
1469+
14521470
==============================================================================
1453-
5.18 OPTS: EXPERIMENTAL *nvim-tree-opts-experimental*
1471+
5.19 OPTS: EXPERIMENTAL *nvim-tree-opts-experimental*
14541472

14551473
*nvim-tree.experimental*
14561474
Experimental features that may become default or optional functionality.
14571475
In the event of a problem please disable the experiment and raise an issue.
14581476

14591477
==============================================================================
1460-
5.19 OPTS: LOG *nvim-tree-opts-log*
1478+
5.20 OPTS: LOG *nvim-tree-opts-log*
14611479

14621480
Configuration for diagnostic logging.
14631481

lua/nvim-tree.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,10 +588,14 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
588588
threshold = vim.log.levels.INFO,
589589
absolute_path = true,
590590
},
591+
help = {
592+
sort_by = "key",
593+
},
591594
ui = {
592595
confirm = {
593596
remove = true,
594597
trash = true,
598+
default_yes = false,
595599
},
596600
},
597601
experimental = {},
@@ -668,6 +672,9 @@ local ACCEPTED_STRINGS = {
668672
bookmarks_placement = { "before", "after", "signcolumn" },
669673
},
670674
},
675+
help = {
676+
sort_by = { "key", "desc" },
677+
},
671678
}
672679

673680
local function validate_options(conf)

lua/nvim-tree/actions/fs/copy-paste.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,15 +252,19 @@ function M.print_clipboard()
252252
end
253253

254254
local function copy_to_clipboard(content)
255+
local clipboard_name
255256
if M.config.actions.use_system_clipboard == true then
256257
vim.fn.setreg("+", content)
257258
vim.fn.setreg('"', content)
258-
return notify.info(string.format("Copied %s to system clipboard!", content))
259+
clipboard_name = "system"
259260
else
260261
vim.fn.setreg('"', content)
261262
vim.fn.setreg("1", content)
262-
return notify.info(string.format("Copied %s to neovim clipboard!", content))
263+
clipboard_name = "neovim"
263264
end
265+
266+
vim.api.nvim_exec_autocmds("TextYankPost", {})
267+
return notify.info(string.format("Copied %s to %s clipboard!", content, clipboard_name))
264268
end
265269

266270
function M.copy_filename(node)

lua/nvim-tree/actions/fs/remove-file.lua

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,22 @@ function M.fn(node)
106106
end
107107

108108
if M.config.ui.confirm.remove then
109-
local prompt_select = "Remove " .. node.name .. " ?"
110-
local prompt_input = prompt_select .. " y/N: "
111-
lib.prompt(prompt_input, prompt_select, { "", "y" }, { "No", "Yes" }, function(item_short)
109+
local prompt_select = "Remove " .. node.name .. "?"
110+
local prompt_input, items_short, items_long
111+
112+
if M.config.ui.confirm.default_yes then
113+
prompt_input = prompt_select .. " Y/n: "
114+
items_short = { "", "n" }
115+
items_long = { "Yes", "No" }
116+
else
117+
prompt_input = prompt_select .. " y/N: "
118+
items_short = { "", "y" }
119+
items_long = { "No", "Yes" }
120+
end
121+
122+
lib.prompt(prompt_input, prompt_select, items_short, items_long, function(item_short)
112123
utils.clear_prompt()
113-
if item_short == "y" then
124+
if item_short == "y" or item_short == (M.config.ui.confirm.default_yes and "") then
114125
do_remove()
115126
end
116127
end)

lua/nvim-tree/actions/fs/trash.lua

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,22 @@ function M.fn(node)
8686
end
8787

8888
if M.config.ui.confirm.trash then
89-
local prompt_select = "Trash " .. node.name .. " ?"
90-
local prompt_input = prompt_select .. " y/N: "
91-
lib.prompt(prompt_input, prompt_select, { "", "y" }, { "No", "Yes" }, function(item_short)
89+
local prompt_select = "Trash " .. node.name .. "?"
90+
local prompt_input, items_short, items_long
91+
92+
if M.config.ui.confirm.default_yes then
93+
prompt_input = prompt_select .. " Y/n: "
94+
items_short = { "", "n" }
95+
items_long = { "Yes", "No" }
96+
else
97+
prompt_input = prompt_select .. " y/N: "
98+
items_short = { "", "y" }
99+
items_long = { "No", "Yes" }
100+
end
101+
102+
lib.prompt(prompt_input, prompt_select, items_short, items_long, function(item_short)
92103
utils.clear_prompt()
93-
if item_short == "y" then
104+
if item_short == "y" or item_short == (M.config.ui.confirm.default_yes and "") then
94105
do_trash()
95106
end
96107
end)

lua/nvim-tree/api.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ end
4545
local function wrap_node(f)
4646
return function(node, ...)
4747
node = node or require("nvim-tree.lib").get_node_at_cursor()
48-
f(node, ...)
48+
if node then
49+
f(node, ...)
50+
end
4951
end
5052
end
5153

lua/nvim-tree/git/utils.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,20 @@ function M.get_toplevel(cwd)
3636
-- git always returns path with forward slashes
3737
if vim.fn.has "win32" == 1 then
3838
-- msys2 git support
39+
-- cygpath calls must in array format to avoid shell compatibility issues
3940
if M.use_cygpath then
40-
toplevel = vim.fn.system("cygpath -w " .. vim.fn.shellescape(toplevel))
41+
toplevel = vim.fn.system { "cygpath", "-w", toplevel }
4142
if vim.v.shell_error ~= 0 then
4243
return nil, nil
4344
end
44-
git_dir = vim.fn.system("cygpath -w " .. vim.fn.shellescape(git_dir))
45+
-- remove trailing newline(\n) character added by vim.fn.system
46+
toplevel = toplevel:gsub("\n", "")
47+
git_dir = vim.fn.system { "cygpath", "-w", git_dir }
4548
if vim.v.shell_error ~= 0 then
4649
return nil, nil
4750
end
51+
-- remove trailing newline(\n) character added by vim.fn.system
52+
git_dir = git_dir:gsub("\n", "")
4853
end
4954
toplevel = toplevel:gsub("/", "\\")
5055
git_dir = git_dir:gsub("/", "\\")

lua/nvim-tree/help.lua

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,29 @@ end
8484
--- @return number maximum length of text
8585
local function compute()
8686
local head_lhs = "nvim-tree mappings"
87-
local head_rhs = "exit: q"
87+
local head_rhs1 = "exit: q"
88+
local head_rhs2 = string.format("sort by %s: s", M.config.sort_by == "key" and "description" or "keymap")
8889

8990
-- formatted lhs and desc from active keymap
9091
local mappings = vim.tbl_map(function(map)
9192
return { lhs = tidy_lhs(map.lhs), desc = tidy_desc(map.desc) }
9293
end, keymap.get_keymap())
9394

94-
-- sort roughly by lhs
95-
table.sort(mappings, function(a, b)
96-
return sort_lhs(a.lhs, b.lhs)
97-
end)
95+
-- sorter function for mappings
96+
local sort_fn
97+
98+
if M.config.sort_by == "desc" then
99+
sort_fn = function(a, b)
100+
return a.desc:lower() < b.desc:lower()
101+
end
102+
else
103+
-- by default sort roughly by lhs
104+
sort_fn = function(a, b)
105+
return sort_lhs(a.lhs, b.lhs)
106+
end
107+
end
108+
109+
table.sort(mappings, sort_fn)
98110

99111
-- longest lhs and description
100112
local max_lhs = 0
@@ -105,11 +117,14 @@ local function compute()
105117
end
106118

107119
-- increase desc if lines are shorter than the header
108-
max_desc = math.max(max_desc, #head_lhs + #head_rhs - max_lhs)
120+
max_desc = math.max(max_desc, #head_lhs + #head_rhs1 - max_lhs)
109121

110122
-- header, not padded
111123
local hl = { { "NvimTreeRootFolder", 0, 0, #head_lhs } }
112-
local lines = { ("%s%s%s"):format(head_lhs, string.rep(" ", max_desc + max_lhs - #head_lhs - #head_rhs + 2), head_rhs) }
124+
local lines = {
125+
head_lhs .. string.rep(" ", max_desc + max_lhs - #head_lhs - #head_rhs1 + 2) .. head_rhs1,
126+
string.rep(" ", max_desc + max_lhs - #head_rhs2 + 2) .. head_rhs2,
127+
}
113128
local width = #lines[1]
114129

115130
-- mappings, left padded 1
@@ -121,7 +136,7 @@ local function compute()
121136
width = math.max(#line, width)
122137

123138
-- highlight lhs
124-
table.insert(hl, { "NvimTreeFolderName", i, 1, #l.lhs + 1 })
139+
table.insert(hl, { "NvimTreeFolderName", i + 1, 1, #l.lhs + 1 })
125140
end
126141

127142
return lines, hl, width
@@ -175,14 +190,25 @@ local function open()
175190
vim.wo[M.winnr].winhl = WIN_HL
176191
vim.wo[M.winnr].cursorline = M.config.cursorline
177192

178-
-- quit binding
179-
vim.keymap.set("n", "q", close, {
180-
desc = "nvim-tree: exit help",
181-
buffer = M.bufnr,
182-
noremap = true,
183-
silent = true,
184-
nowait = true,
185-
})
193+
local function toggle_sort()
194+
M.config.sort_by = (M.config.sort_by == "desc") and "key" or "desc"
195+
open()
196+
end
197+
198+
local keymaps = {
199+
q = { fn = close, desc = "nvim-tree: exit help" },
200+
s = { fn = toggle_sort, desc = "nvim-tree: toggle sorting method" },
201+
}
202+
203+
for k, v in pairs(keymaps) do
204+
vim.keymap.set("n", k, v.fn, {
205+
desc = v.desc,
206+
buffer = M.bufnr,
207+
noremap = true,
208+
silent = true,
209+
nowait = true,
210+
})
211+
end
186212

187213
-- close window and delete buffer on leave
188214
vim.api.nvim_create_autocmd({ "BufLeave", "WinLeave" }, {
@@ -202,6 +228,7 @@ end
202228

203229
function M.setup(opts)
204230
M.config.cursorline = opts.view.cursorline
231+
M.config.sort_by = opts.help.sort_by
205232
end
206233

207234
return M

lua/nvim-tree/renderer/components/full-name.lua

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ end
3434

3535
local function show()
3636
local line_nr = vim.api.nvim_win_get_cursor(0)[1]
37-
if line_nr == 1 and require("nvim-tree.view").is_root_folder_visible() then
38-
return
39-
end
4037
if vim.wo.wrap then
4138
return
4239
end
@@ -60,8 +57,9 @@ local function show()
6057
end
6158

6259
M.popup_win = vim.api.nvim_open_win(vim.api.nvim_create_buf(false, false), false, {
63-
relative = "win",
64-
bufpos = { vim.fn.line "." - 2, 0 },
60+
relative = "cursor",
61+
row = 0,
62+
col = 1 - vim.fn.getcursorcharpos()[3],
6563
width = math.min(text_width, vim.o.columns - 2),
6664
height = 1,
6765
noautocmd = true,
@@ -71,7 +69,7 @@ local function show()
7169
local ns_id = vim.api.nvim_get_namespaces()["NvimTreeHighlights"]
7270
local extmarks = vim.api.nvim_buf_get_extmarks(0, ns_id, { line_nr - 1, 0 }, { line_nr - 1, -1 }, { details = 1 })
7371
vim.api.nvim_win_call(M.popup_win, function()
74-
vim.fn.setbufline("%", 1, line)
72+
vim.api.nvim_buf_set_lines(0, 0, -1, true, { line })
7573
for _, extmark in ipairs(extmarks) do
7674
local hl = extmark[4]
7775
vim.api.nvim_buf_add_highlight(0, ns_id, hl.hl_group, 0, extmark[3], hl.end_col)

lua/nvim-tree/utils.lua

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,19 +280,38 @@ function M.move_missing_val(src, src_path, src_pos, dst, dst_path, dst_pos, remo
280280
end
281281
end
282282

283+
local function round(value)
284+
-- Amount of digits to round to after floating point.
285+
local digits = 2
286+
local round_number = 10 ^ digits
287+
return math.floor((value * round_number) + 0.5) / round_number
288+
end
289+
283290
function M.format_bytes(bytes)
284-
local units = { "B", "K", "M", "G", "T" }
291+
local units = { "B", "K", "M", "G", "T", "P", "E", "Z", "Y" }
292+
local i = "i" -- bInary
285293

286294
bytes = math.max(bytes, 0)
287295
local pow = math.floor((bytes and math.log(bytes) or 0) / math.log(1024))
288296
pow = math.min(pow, #units)
289297

290-
local value = bytes / (1024 ^ pow)
291-
value = math.floor((value * 10) + 0.5) / 10
298+
local value = round(bytes / (1024 ^ pow))
292299

293300
pow = pow + 1
294301

295-
return (units[pow] == nil) and (bytes .. "B") or (value .. units[pow])
302+
-- units[pow] == nil when size == 0 B or size >= 1024 YiB
303+
if units[pow] == nil or pow == 1 then
304+
if bytes < 1024 then
305+
return bytes .. " " .. units[1]
306+
else
307+
-- Use the biggest adopted multiple of 2 instead of bytes.
308+
value = round(bytes / (1024 ^ (#units - 1)))
309+
-- For big numbers decimal part is not useful.
310+
return string.format("%.0f %s%s%s", value, units[#units], i, units[1])
311+
end
312+
else
313+
return value .. " " .. units[pow] .. i .. units[1]
314+
end
296315
end
297316

298317
function M.key_by(tbl, key)

0 commit comments

Comments
 (0)