Skip to content

Commit 7d18f15

Browse files
author
Moshe Avni
committed
parent 4601444
author Kiyan <yazdani.kiyan@protonmail.com> 1652514867 +0200 committer Moshe Avni <mavni@netapp.com> 1653224067 +0300 feat(renderer): add ability to set git icons in signcolumn (nvim-tree#1242) feat: extension sorter (nvim-tree#1181) (nvim-tree#1264) Revert "nvim-tree#1253 only pad git icons when they are present (nvim-tree#1259)" This reverts commit 90d7b8e. fixes nvim-tree#1267 fix(renderer): padding when git icons are after the name fixes nvim-tree#1253 fix(renderer): empty space at end of line fixes nvim-tree#1253 add fish performance tip to README.md feat(live-filter): add ability to live filter out nodes in the tree (nvim-tree#1056) feat: reload explorer on buf enter (nvim-tree#1265) chore(config): auto resize the tree by default when opening a file. config.open_file.auto_resize is now true by default. Breaking change for default configurations. See nvim-tree#1275 (comment) chore: remove custom set local implementation Seems vim.opt_local has been fixed. see neovim/neovim#14670 refactor: simplify opening file in new tab fixes nvim-tree#1271. Also fixes opening a file in new tab when close_on_open was true. This introduces breaking change since we don't do any extra behavior and let the buffer be opened by the tree. The previous behavior was a bit old and i believe this should've been fixed by now. Reference this commit if unexpected behavior appears while opening files in new tabs from nvim-tree. feat: optional path argument for NvimTreeToggle and NvimTreeFindFileToggle (nvim-tree#1276) Ignore case when removing default mappings issue#362: File Management popup menu UI. First Iteration. Workin on menu refactoring
1 parent a71bb3a commit 7d18f15

File tree

5 files changed

+66
-114
lines changed

5 files changed

+66
-114
lines changed

lua/nvim-tree/actions/init.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ local nvim_tree_callback = require("nvim-tree.config").nvim_tree_callback
88

99
local M = {
1010
mappings = {
11-
{ key = {"m"}, action = "open_popup_menu"},
11+
{ key = {"M"}, action = "open_popup_menu"},
1212
{ key = { "<CR>", "o", "<2-LeftMouse>" }, action = "edit" },
1313
{ key = "<C-e>", action = "edit_in_place" },
1414
{ key = "O", action = "edit_no_picker" },
@@ -54,8 +54,8 @@ local M = {
5454
custom_keypress_funcs = {},
5555
}
5656

57-
local keypress_funcs = {
58-
open_popup_menu = require("nvim-tree.popup-menu").open_menu,
57+
M.keypress_funcs = {
58+
open_popup_menu = require("nvim-tree.popup-menu").open_window,
5959
close = view.close,
6060
close_node = require("nvim-tree.actions.movements").parent_node(true),
6161
collapse_all = require("nvim-tree.actions.collapse-all").fn,
@@ -109,7 +109,7 @@ function M.on_keypress(action)
109109
end
110110

111111
local custom_function = M.custom_keypress_funcs[action]
112-
local default_function = keypress_funcs[action]
112+
local default_function = M.keypress_funcs[action]
113113

114114
if type(custom_function) == "function" then
115115
return custom_function(node)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
local M = {}
2+
3+
M.actions = {
4+
['Copy File Name'] = 'copy_name',
5+
['Copy File Path'] = 'copy_path',
6+
['Open File'] = 'edit_no_picker',
7+
}
8+
9+
return M

lua/nvim-tree/popup-menu/init.lua

Lines changed: 53 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,79 @@
11
local a = vim.api
22

3-
local get_node = require('nvim-tree.lib').get_node_at_cursor
4-
local get_explorer = require('nvim-tree.core').get_explorer
5-
63
local M = {}
74

8-
M.is_open = false
9-
M.win = 0
10-
M.bufnr = 0
11-
12-
local actions = {
13-
['copy'] = require('nvim-tree.actions.copy-paste').copy
14-
}
5+
local function get_node()
6+
local get_node_at_cursor = require'nvim-tree.lib'.get_node_at_cursor
7+
8+
local current_node = get_node_at_cursor()
9+
return current_node
10+
end
1511

16-
M.mappings = {
17-
['<Esc>'] = 'close_menu',
18-
['<CR>'] = 'on_selection',
19-
}
12+
local current_popup = nil
2013

21-
local ns_PopupMenu = a.nvim_create_namespace('ns_NvimTreePopupMenu')
14+
local winnr
15+
local bufnr
2216

23-
local function create_buf()
24-
if vim.fn.bufexists("NvimTreePopupMenuBuf") == 1 then
25-
a.nvim_buf_delete(vim.fn.bufnr('NvimTreePopupMenuBuf'), {} )
26-
end
27-
local buf = a.nvim_create_buf(false, true)
28-
a.nvim_buf_set_name(buf, 'NvimTreePopupMenuBuf')
29-
M.bufnr = buf
17+
function get_current_action(_winnr, _bufnr)
18+
local node = current_popup.current_node
19+
local cur_pos = a.nvim_win_get_cursor(_winnr)[1]
20+
local current_action = a.nvim_buf_get_lines(_bufnr, cur_pos -1, cur_pos, false)[1]
3021

31-
return buf
22+
for _,v in pairs(require'nvim-tree.actions'.mappings) do
23+
if v.action == current_action then
24+
require'nvim-tree.actions'.keypress_funcs[current_action](current_popup.current_node)
25+
end
26+
end
3227
end
3328

34-
function M.open_menu()
35-
if M.is_open == true then
36-
close_menu()
37-
end
29+
local function setup_mappings()
30+
-- TODO: pass mappings as arguments from setup or default
31+
a.nvim_buf_set_keymap(bufnr, 'n', '<CR>', string.format([[:lua get_current_action(%s, %s)<CR>]], winnr, bufnr), { noremap = true })
32+
a.nvim_buf_set_keymap(bufnr, 'n', '<Esc>', [[:lua require("nvim-tree.popup-menu").close_window()<CR>]], { noremap = true } )
33+
end
3834

39-
create_buf()
40-
local win_opts = {
41-
width = 10, -- we should set this as NvimExplorer width
42-
height = 1, -- TODO: Set this as length of actions, as for now I'm not able to do it.
35+
local function setup_window(actions)
36+
local max_width = vim.fn.max(vim.tbl_map(function(n) return #n end, vim.tbl_keys(actions)))
37+
winnr = a.nvim_open_win(0, true, {
38+
col = 0,
4339
row = 1,
44-
col = 0,
45-
style = 'minimal',
46-
relative = 'cursor'
47-
}
40+
relative = 'cursor',
4841

49-
local win_ui = a.nvim_open_win(M.bufnr, true, win_opts)
50-
M.is_open = true
51-
M.win = win_ui
52-
set_actions(actions)
42+
width = max_width + 1,
43+
height = vim.tbl_count(actions),
44+
style = 'minimal'
45+
})
5346

54-
-- TODO: Convert this to nvim lua api.
55-
-- at this time on my machine with nvim 0.7.0-dev+1328-gfb5587d2b
56-
-- keep yelling that "event" must be an array or string
57-
local au_group = a.nvim_create_augroup('NvimSelectionAuGroup', { clear = true })
58-
vim.cmd(string.format([[
59-
augroup NvimSelectionAuGroup
60-
au! CursorMoved <buffer=%s> :lua require('nvim-tree.popup-menu').add_hl_to_selection()
61-
augroup END
62-
]], M.bufnr))
63-
64-
-- mappings on open window
65-
-- should we pass this on separate file?
66-
for k, v in pairs(M.mappings) do
67-
if v ~= nil then
68-
a.nvim_buf_set_keymap(
69-
M.bufnr,
70-
'n', k,
71-
string.format([[:lua %s()<CR>]], v), { noremap = true, silent = true })
72-
end
73-
end
74-
end
47+
current_popup = {
48+
winnr = winnr,
49+
bufnr = bufnr,
50+
current_node = get_node()
51+
}
7552

76-
function close_menu()
77-
a.nvim_win_close(M.win, true)
78-
M.is_open = false
53+
bufnr = a.nvim_create_buf(false, true)
54+
a.nvim_buf_set_lines(bufnr, 0, -1, false, vim.tbl_keys(actions))
55+
a.nvim_win_set_buf(winnr, bufnr)
7956
end
8057

81-
function set_actions(tbl)
82-
a.nvim_buf_set_lines(M.bufnr, 0, 1, true, vim.tbl_keys(tbl))
58+
function M.open_window()
59+
setup_window(M.actions)
60+
setup_mappings()
8361
end
8462

85-
function M.add_hl_to_selection()
86-
if M.is_open == true then
87-
local row = a.nvim_win_get_cursor(M.win)[1]
88-
local sel_item = a.nvim_buf_get_lines(M.bufnr, row -1, row, {})[1] -- should we pass this as module params to let user customize action on current selected item?
63+
function M.close_window()
64+
if current_popup ~= nil then
65+
a.nvim_win_close(current_popup.winnr, { force = true })
66+
a.nvim_buf_delete(bufnr, { force = true })
8967

90-
-- clear prev highlights
91-
a.nvim_buf_clear_namespace(M.bufnr, ns_PopupMenu, 0, -1)
92-
93-
a.nvim_buf_add_highlight(
94-
M.bufnr,
95-
ns_PopupMenu,
96-
'Error',
97-
row -1,
98-
0,
99-
-1
100-
)
68+
current_popup = nil
10169
end
10270
end
10371

104-
function on_selection()
105-
local row = a.nvim_win_get_cursor(M.win)[1]
106-
local sel_item = a.nvim_buf_get_lines(M.bufnr, row -1, row, {})[1] -- should we pass this as module params to let user customize action on current selected item?
107-
108-
actions[sel_item](get_node())
109-
close_menu()
72+
function M.setup(opts)
73+
M.actions = opts.menu.actions
74+
if M.actions == nil then
75+
M.actions = require("default_actions").actions
76+
end
11077
end
11178

11279
return M

lua/nvim-tree/popup-menu/mappings.lua

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

lua/nvim-tree/popup-menu/scratch.lua

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

0 commit comments

Comments
 (0)