Skip to content

Commit 45400cd

Browse files
feat(api): add api.commands.get (#2083)
* feat(commands): add descriptions * Update lua/nvim-tree.lua Co-authored-by: gegoune <[email protected]> * feat(commands): add descriptions, extract to commands.lua * feat(commands): add descriptions, add api.get_commands * feat(commands): add descriptions, api.get_commands -> api.commands.get --------- Co-authored-by: gegoune <[email protected]>
1 parent a38f9a5 commit 45400cd

File tree

4 files changed

+163
-41
lines changed

4 files changed

+163
-41
lines changed

doc/nvim-tree-lua.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1503,14 +1503,23 @@ api.config.mappings.get_keymap()
15031503
Return: ~
15041504
(table) as per |nvim_buf_get_keymap()|
15051505

1506-
*nvim-tree.api.config.mappings.get_keymap_default()*
1506+
*nvim-tree.api.config.mappings.get_keymap_default()*
15071507
api.config.mappings.get_keymap_default()
15081508
Retrieves the buffer local mappings for nvim-tree that are applied by
15091509
|nvim-tree.api.config.mappings.default_on_attach()|
15101510

15111511
Return: ~
15121512
(table) as per |nvim_buf_get_keymap()|
15131513

1514+
api.commands.get() *nvim-tree.api.commands.get()*
1515+
Retrieve all commands, see |nvim-tree-commands|
1516+
1517+
Return: ~
1518+
(table) array containing |nvim_create_user_command()| parameters:
1519+
{name} (string)
1520+
{command} (function)
1521+
{opts} (table)
1522+
15141523
==============================================================================
15151524
6. MAPPINGS *nvim-tree-mappings*
15161525

lua/nvim-tree.lua

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ local log = require "nvim-tree.log"
33
local colors = require "nvim-tree.colors"
44
local renderer = require "nvim-tree.renderer"
55
local view = require "nvim-tree.view"
6+
local commands = require "nvim-tree.commands"
67
local utils = require "nvim-tree.utils"
78
local change_dir = require "nvim-tree.actions.root.change-dir"
89
local legacy = require "nvim-tree.legacy"
@@ -123,8 +124,6 @@ local function find_existing_windows()
123124
end, vim.api.nvim_list_wins())
124125
end
125126

126-
M.resize = view.resize
127-
128127
function M.open_on_directory()
129128
local should_proceed = M.initialized and (_config.hijack_directories.auto_open or view.is_visible())
130129
if not should_proceed then
@@ -247,43 +246,6 @@ local function manage_netrw(disable_netrw, hijack_netrw)
247246
end
248247
end
249248

250-
local function setup_vim_commands()
251-
vim.api.nvim_create_user_command("NvimTreeOpen", function(res)
252-
require("nvim-tree.api").tree.open { path = res.args }
253-
end, { nargs = "?", complete = "dir" })
254-
vim.api.nvim_create_user_command("NvimTreeClose", function()
255-
require("nvim-tree.api").tree.close()
256-
end, { bar = true })
257-
vim.api.nvim_create_user_command("NvimTreeToggle", function(res)
258-
require("nvim-tree.api").tree.toggle { find_file = false, focus = true, path = res.args, update_root = false }
259-
end, { nargs = "?", complete = "dir" })
260-
vim.api.nvim_create_user_command("NvimTreeFocus", function()
261-
require("nvim-tree.api").tree.focus()
262-
end, { bar = true })
263-
vim.api.nvim_create_user_command("NvimTreeRefresh", function()
264-
require("nvim-tree.api").tree.reload()
265-
end, { bar = true })
266-
vim.api.nvim_create_user_command("NvimTreeClipboard", function()
267-
require("nvim-tree.api").fs.print_clipboard()
268-
end, { bar = true })
269-
vim.api.nvim_create_user_command("NvimTreeFindFile", function(res)
270-
require("nvim-tree.api").tree.find_file { open = true, focus = true, update_root = res.bang }
271-
end, { bang = true, bar = true })
272-
vim.api.nvim_create_user_command("NvimTreeFindFileToggle", function(res)
273-
require("nvim-tree.api").tree.toggle { find_file = true, focus = true, path = res.args, update_root = res.bang }
274-
end, { bang = true, nargs = "?", complete = "dir" })
275-
vim.api.nvim_create_user_command("NvimTreeResize", function(res)
276-
M.resize(res.args)
277-
end, { nargs = 1, bar = true })
278-
vim.api.nvim_create_user_command("NvimTreeCollapse", function()
279-
require("nvim-tree.api").tree.collapse_all(false)
280-
end, { bar = true })
281-
vim.api.nvim_create_user_command("NvimTreeCollapseKeepBuffers", function()
282-
require("nvim-tree.api").tree.collapse_all(true)
283-
end, { bar = true })
284-
vim.api.nvim_create_user_command("NvimTreeGenerateOnAttach", keymap_legacy.cmd_generate_on_attach, {})
285-
end
286-
287249
function M.change_dir(name)
288250
change_dir.fn(name)
289251

@@ -811,7 +773,7 @@ function M.setup(conf)
811773

812774
if not M.setup_called then
813775
-- first call to setup
814-
setup_vim_commands()
776+
commands.setup()
815777
else
816778
-- subsequent calls to setup
817779
require("nvim-tree.watcher").purge_watchers()

lua/nvim-tree/api.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ local Api = {
99
git = {},
1010
live_filter = {},
1111
config = { mappings = {} },
12+
commands = {},
1213
}
1314

1415
local function inject_node(f)
@@ -193,4 +194,8 @@ Api.config.mappings.get_keymap_default = function()
193194
return require("nvim-tree.keymap").get_keymap_default()
194195
end
195196

197+
Api.commands.get = function()
198+
return require("nvim-tree.commands").get()
199+
end
200+
196201
return Api

lua/nvim-tree/commands.lua

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
local keymap_legacy = require "nvim-tree.keymap-legacy"
2+
local api = require "nvim-tree.api"
3+
local view = require "nvim-tree.view"
4+
5+
local M = {}
6+
7+
local CMDS = {
8+
{
9+
name = "NvimTreeOpen",
10+
opts = {
11+
desc = "nvim-tree: open",
12+
nargs = "?",
13+
complete = "dir",
14+
},
15+
command = function(c)
16+
api.tree.open { path = c.args }
17+
end,
18+
},
19+
{
20+
name = "NvimTreeClose",
21+
opts = {
22+
desc = "nvim-tree: close",
23+
bar = true,
24+
},
25+
command = function()
26+
api.tree.close()
27+
end,
28+
},
29+
{
30+
name = "NvimTreeToggle",
31+
opts = {
32+
desc = "nvim-tree: toggle",
33+
nargs = "?",
34+
complete = "dir",
35+
},
36+
command = function(c)
37+
api.tree.toggle { find_file = false, focus = true, path = c.args, update_root = false }
38+
end,
39+
},
40+
{
41+
name = "NvimTreeFocus",
42+
opts = {
43+
desc = "nvim-tree: focus",
44+
bar = true,
45+
},
46+
command = function()
47+
api.tree.focus()
48+
end,
49+
},
50+
{
51+
name = "NvimTreeRefresh",
52+
opts = {
53+
desc = "nvim-tree: refresh",
54+
bar = true,
55+
},
56+
command = function()
57+
api.tree.reload()
58+
end,
59+
},
60+
{
61+
name = "NvimTreeClipboard",
62+
opts = {
63+
desc = "nvim-tree: print clipboard",
64+
bar = true,
65+
},
66+
command = function()
67+
api.fs.print_clipboard()
68+
end,
69+
},
70+
{
71+
name = "NvimTreeFindFile",
72+
opts = {
73+
desc = "nvim-tree: find file",
74+
bang = true,
75+
bar = true,
76+
},
77+
command = function(c)
78+
api.tree.find_file { open = true, focus = true, update_root = c.bang }
79+
end,
80+
},
81+
{
82+
name = "NvimTreeFindFileToggle",
83+
opts = {
84+
desc = "nvim-tree: find file, toggle",
85+
bang = true,
86+
nargs = "?",
87+
complete = "dir",
88+
},
89+
command = function(c)
90+
api.tree.toggle { find_file = true, focus = true, path = c.args, update_root = c.bang }
91+
end,
92+
},
93+
{
94+
name = "NvimTreeResize",
95+
opts = {
96+
desc = "nvim-tree: resize",
97+
nargs = 1,
98+
bar = true,
99+
},
100+
command = function(c)
101+
view.resize(c.args)
102+
end,
103+
},
104+
{
105+
name = "NvimTreeCollapse",
106+
opts = {
107+
desc = "nvim-tree: collapse",
108+
bar = true,
109+
},
110+
command = function()
111+
api.tree.collapse_all(false)
112+
end,
113+
},
114+
{
115+
name = "NvimTreeCollapseKeepBuffers",
116+
opts = {
117+
desc = "nvim-tree: collapse, keep directories open",
118+
bar = true,
119+
},
120+
command = function()
121+
api.tree.collapse_all(true)
122+
end,
123+
},
124+
{
125+
name = "NvimTreeGenerateOnAttach",
126+
opts = {
127+
desc = "nvim-tree: generate on_attach function from deprecated view.mappings",
128+
},
129+
command = function()
130+
keymap_legacy.cmd_generate_on_attach()
131+
end,
132+
},
133+
}
134+
135+
function M.get()
136+
return vim.deepcopy(CMDS)
137+
end
138+
139+
function M.setup()
140+
for _, cmd in ipairs(CMDS) do
141+
local opts = vim.tbl_extend("force", cmd.opts, { force = true })
142+
vim.api.nvim_create_user_command(cmd.name, cmd.command, opts)
143+
end
144+
end
145+
146+
return M

0 commit comments

Comments
 (0)