From b43113ed597b571de9ddedb9f7290001e56b3285 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 25 Mar 2023 16:15:22 +1100 Subject: [PATCH 1/5] feat(commands): add descriptions --- lua/nvim-tree.lua | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index e5f03b2bd4e..90e43d47f9f 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -245,38 +245,53 @@ end local function setup_vim_commands() vim.api.nvim_create_user_command("NvimTreeOpen", function(res) require("nvim-tree.api").tree.open { path = res.args } - end, { nargs = "?", complete = "dir" }) + end, { nargs = "?", complete = "dir", desc = "nvim-tree: open" }) + vim.api.nvim_create_user_command("NvimTreeClose", function() require("nvim-tree.api").tree.close() - end, { bar = true }) + end, { bar = true, desc = "nvim-tree: close" }) + vim.api.nvim_create_user_command("NvimTreeToggle", function(res) require("nvim-tree.api").tree.toggle { find_file = false, focus = true, path = res.args, update_root = false } - end, { nargs = "?", complete = "dir" }) + end, { nargs = "?", complete = "dir", desc = "nvim-tree: toggle" }) + vim.api.nvim_create_user_command("NvimTreeFocus", function() require("nvim-tree.api").tree.focus() - end, { bar = true }) + end, { bar = true, desc = "nvim-tree: focus" }) + vim.api.nvim_create_user_command("NvimTreeRefresh", function() require("nvim-tree.api").tree.reload() - end, { bar = true }) + end, { bar = true, desc = "nvim-tree: refresh" }) + vim.api.nvim_create_user_command("NvimTreeClipboard", function() require("nvim-tree.api").fs.print_clipboard() - end, { bar = true }) + end, { bar = true, desc = "nvim-tree: print clipboard" }) + vim.api.nvim_create_user_command("NvimTreeFindFile", function(res) require("nvim-tree.api").tree.find_file { open = true, update_root = res.bang } - end, { bang = true, bar = true }) + end, { bang = true, bar = true, desc = "nvim-tree: find file" }) + vim.api.nvim_create_user_command("NvimTreeFindFileToggle", function(res) require("nvim-tree.api").tree.toggle { find_file = true, focus = true, path = res.args, update_root = res.bang } - end, { bang = true, nargs = "?", complete = "dir" }) + end, { bang = true, nargs = "?", complete = "dir", desc = "nvim-tree: find file, toggle" }) + vim.api.nvim_create_user_command("NvimTreeResize", function(res) M.resize(res.args) - end, { nargs = 1, bar = true }) + end, { nargs = 1, bar = true, desc = "nvim-tree: resize" }) + vim.api.nvim_create_user_command("NvimTreeCollapse", function() require("nvim-tree.api").tree.collapse_all(false) - end, { bar = true }) + end, { bar = true, desc = "nvim-tree: collapse" }) + vim.api.nvim_create_user_command("NvimTreeCollapseKeepBuffers", function() require("nvim-tree.api").tree.collapse_all(true) - end, { bar = true }) - vim.api.nvim_create_user_command("NvimTreeGenerateOnAttach", keymap_legacy.cmd_generate_on_attach, {}) + end, { bar = true, desc = "nvim-tree: collapse, keep directories open" }) + + vim.api.nvim_create_user_command( + "NvimTreeGenerateOnAttach", + keymap_legacy.cmd_generate_on_attach, + { desc = "nvim-tree: generate on_attach from view.mappings" } + ) end function M.change_dir(name) From b2fcd7cd716657b5439cdfa76e6e9dc229f50c93 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 26 Mar 2023 15:15:06 +1100 Subject: [PATCH 2/5] Update lua/nvim-tree.lua Co-authored-by: gegoune <69750637+gegoune@users.noreply.github.com> --- lua/nvim-tree.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 90e43d47f9f..cb7b19e7d2b 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -290,7 +290,7 @@ local function setup_vim_commands() vim.api.nvim_create_user_command( "NvimTreeGenerateOnAttach", keymap_legacy.cmd_generate_on_attach, - { desc = "nvim-tree: generate on_attach from view.mappings" } + { desc = "nvim-tree: generate on_attach function from deprecated view.mappings" } ) end From ee7757930db72ccc522ad233a4c0e3c3acdcc969 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 26 Mar 2023 16:23:53 +1100 Subject: [PATCH 3/5] feat(commands): add descriptions, extract to commands.lua --- lua/nvim-tree.lua | 57 +------------------ lua/nvim-tree/commands.lua | 113 +++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 55 deletions(-) create mode 100644 lua/nvim-tree/commands.lua diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index bf1bf24287c..84091b8b217 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -3,6 +3,7 @@ local log = require "nvim-tree.log" local colors = require "nvim-tree.colors" local renderer = require "nvim-tree.renderer" local view = require "nvim-tree.view" +local commands = require "nvim-tree.commands" local utils = require "nvim-tree.utils" local change_dir = require "nvim-tree.actions.root.change-dir" local legacy = require "nvim-tree.legacy" @@ -123,8 +124,6 @@ local function find_existing_windows() end, vim.api.nvim_list_wins()) end -M.resize = view.resize - function M.open_on_directory() local should_proceed = M.initialized and (_config.hijack_directories.auto_open or view.is_visible()) if not should_proceed then @@ -247,58 +246,6 @@ local function manage_netrw(disable_netrw, hijack_netrw) end end -local function setup_vim_commands() - vim.api.nvim_create_user_command("NvimTreeOpen", function(res) - require("nvim-tree.api").tree.open { path = res.args } - end, { nargs = "?", complete = "dir", desc = "nvim-tree: open" }) - - vim.api.nvim_create_user_command("NvimTreeClose", function() - require("nvim-tree.api").tree.close() - end, { bar = true, desc = "nvim-tree: close" }) - - vim.api.nvim_create_user_command("NvimTreeToggle", function(res) - require("nvim-tree.api").tree.toggle { find_file = false, focus = true, path = res.args, update_root = false } - end, { nargs = "?", complete = "dir", desc = "nvim-tree: toggle" }) - - vim.api.nvim_create_user_command("NvimTreeFocus", function() - require("nvim-tree.api").tree.focus() - end, { bar = true, desc = "nvim-tree: focus" }) - - vim.api.nvim_create_user_command("NvimTreeRefresh", function() - require("nvim-tree.api").tree.reload() - end, { bar = true, desc = "nvim-tree: refresh" }) - - vim.api.nvim_create_user_command("NvimTreeClipboard", function() - require("nvim-tree.api").fs.print_clipboard() - end, { bar = true, desc = "nvim-tree: print clipboard" }) - - vim.api.nvim_create_user_command("NvimTreeFindFile", function(res) - require("nvim-tree.api").tree.find_file { open = true, focus = true, update_root = res.bang } - end, { bang = true, bar = true, desc = "nvim-tree: find file" }) - - vim.api.nvim_create_user_command("NvimTreeFindFileToggle", function(res) - require("nvim-tree.api").tree.toggle { find_file = true, focus = true, path = res.args, update_root = res.bang } - end, { bang = true, nargs = "?", complete = "dir", desc = "nvim-tree: find file, toggle" }) - - vim.api.nvim_create_user_command("NvimTreeResize", function(res) - M.resize(res.args) - end, { nargs = 1, bar = true, desc = "nvim-tree: resize" }) - - vim.api.nvim_create_user_command("NvimTreeCollapse", function() - require("nvim-tree.api").tree.collapse_all(false) - end, { bar = true, desc = "nvim-tree: collapse" }) - - vim.api.nvim_create_user_command("NvimTreeCollapseKeepBuffers", function() - require("nvim-tree.api").tree.collapse_all(true) - end, { bar = true, desc = "nvim-tree: collapse, keep directories open" }) - - vim.api.nvim_create_user_command( - "NvimTreeGenerateOnAttach", - keymap_legacy.cmd_generate_on_attach, - { desc = "nvim-tree: generate on_attach function from deprecated view.mappings" } - ) -end - function M.change_dir(name) change_dir.fn(name) @@ -826,7 +773,7 @@ function M.setup(conf) if not M.setup_called then -- first call to setup - setup_vim_commands() + commands.setup() else -- subsequent calls to setup require("nvim-tree.watcher").purge_watchers() diff --git a/lua/nvim-tree/commands.lua b/lua/nvim-tree/commands.lua new file mode 100644 index 00000000000..6d3a4a14a8b --- /dev/null +++ b/lua/nvim-tree/commands.lua @@ -0,0 +1,113 @@ +local keymap_legacy = require "nvim-tree.keymap-legacy" +local api = require "nvim-tree.api" +local view = require "nvim-tree.view" + +local M = {} + +local CMDS = { + { + name = "NvimTreeOpen", + desc = "nvim-tree: open", + opts = { nargs = "?", complete = "dir" }, + command = function(c) + api.tree.open { path = c.args } + end, + }, + { + name = "NvimTreeClose", + desc = "nvim-tree: close", + opts = { bar = true }, + command = function() + api.tree.close() + end, + }, + { + name = "NvimTreeToggle", + desc = "nvim-tree: toggle", + opts = { nargs = "?", complete = "dir" }, + command = function(c) + api.tree.toggle { find_file = false, focus = true, path = c.args, update_root = false } + end, + }, + { + name = "NvimTreeFocus", + desc = "nvim-tree: focus", + opts = { bar = true }, + command = function() + api.tree.focus() + end, + }, + { + name = "NvimTreeRefresh", + desc = "nvim-tree: refresh", + opts = { bar = true }, + command = function() + api.tree.reload() + end, + }, + { + name = "NvimTreeClipboard", + desc = "nvim-tree: print clipboard", + opts = { bar = true }, + command = function() + api.fs.print_clipboard() + end, + }, + { + name = "NvimTreeFindFile", + desc = "nvim-tree: find file", + opts = { bang = true, bar = true }, + command = function(c) + api.tree.find_file { open = true, focus = true, update_root = c.bang } + end, + }, + { + name = "NvimTreeFindFileToggle", + desc = "nvim-tree: find file, toggle", + opts = { bang = true, nargs = "?", complete = "dir" }, + command = function(c) + api.tree.toggle { find_file = true, focus = true, path = c.args, update_root = c.bang } + end, + }, + { + name = "NvimTreeResize", + desc = "nvim-tree: resize", + opts = { nargs = 1, bar = true }, + command = function(c) + view.resize(c.args) + end, + }, + { + name = "NvimTreeCollapse", + desc = "nvim-tree: collapse", + opts = { bar = true }, + command = function() + api.tree.collapse_all(false) + end, + }, + { + name = "NvimTreeCollapseKeepBuffers", + desc = "nvim-tree: collapse, keep directories open", + opts = { bar = true }, + command = function() + api.tree.collapse_all(true) + end, + }, + { + name = "NvimTreeGenerateOnAttach", + desc = "nvim-tree: generate on_attach function from deprecated view.mappings", + opts = {}, + command = function() + keymap_legacy.cmd_generate_on_attach() + end, + }, +} + +function M.setup() + for _, cmd in ipairs(CMDS) do + local opts = vim.tbl_extend("force", cmd.opts, { desc = cmd.desc }) + vim.api.nvim_create_user_command(cmd.name, cmd.command, opts) + end +end + +return M From 7d5b07f5178f71ce02c2dedeea201b0d332fcab9 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 26 Mar 2023 17:00:31 +1100 Subject: [PATCH 4/5] feat(commands): add descriptions, add api.get_commands --- doc/nvim-tree-lua.txt | 11 ++++- lua/nvim-tree/api.lua | 4 ++ lua/nvim-tree/commands.lua | 83 ++++++++++++++++++++++++++------------ 3 files changed, 72 insertions(+), 26 deletions(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 4f9f4143193..54345395cc7 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -1503,7 +1503,7 @@ api.config.mappings.get_keymap() Return: ~ (table) as per |nvim_buf_get_keymap()| - *nvim-tree.api.config.mappings.get_keymap_default()* + *nvim-tree.api.config.mappings.get_keymap_default()* api.config.mappings.get_keymap_default() Retrieves the buffer local mappings for nvim-tree that are applied by |nvim-tree.api.config.mappings.default_on_attach()| @@ -1511,6 +1511,15 @@ api.config.mappings.get_keymap_default() Return: ~ (table) as per |nvim_buf_get_keymap()| +api.get_commands() *nvim-tree.api.get_commands()* + Retrieve all commands, see |nvim-tree-commands| + + Return: ~ + (table) array containing |nvim_create_user_command()| parameters: + • {name} (string) + • {command} (function) + • {opts} (table) + ============================================================================== 6. MAPPINGS *nvim-tree-mappings* diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index 78083a9a3a3..2452aed75ac 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -193,4 +193,8 @@ Api.config.mappings.get_keymap_default = function() return require("nvim-tree.keymap").get_keymap_default() end +Api.get_commands = function() + return require("nvim-tree.commands").get_commands() +end + return Api diff --git a/lua/nvim-tree/commands.lua b/lua/nvim-tree/commands.lua index 6d3a4a14a8b..1246a1ed963 100644 --- a/lua/nvim-tree/commands.lua +++ b/lua/nvim-tree/commands.lua @@ -7,105 +7,138 @@ local M = {} local CMDS = { { name = "NvimTreeOpen", - desc = "nvim-tree: open", - opts = { nargs = "?", complete = "dir" }, + opts = { + desc = "nvim-tree: open", + nargs = "?", + complete = "dir", + }, command = function(c) api.tree.open { path = c.args } end, }, { name = "NvimTreeClose", - desc = "nvim-tree: close", - opts = { bar = true }, + opts = { + desc = "nvim-tree: close", + bar = true, + }, command = function() api.tree.close() end, }, { name = "NvimTreeToggle", - desc = "nvim-tree: toggle", - opts = { nargs = "?", complete = "dir" }, + opts = { + desc = "nvim-tree: toggle", + nargs = "?", + complete = "dir", + }, command = function(c) api.tree.toggle { find_file = false, focus = true, path = c.args, update_root = false } end, }, { name = "NvimTreeFocus", - desc = "nvim-tree: focus", - opts = { bar = true }, + opts = { + desc = "nvim-tree: focus", + bar = true, + }, command = function() api.tree.focus() end, }, { name = "NvimTreeRefresh", - desc = "nvim-tree: refresh", - opts = { bar = true }, + opts = { + desc = "nvim-tree: refresh", + bar = true, + }, command = function() api.tree.reload() end, }, { name = "NvimTreeClipboard", - desc = "nvim-tree: print clipboard", - opts = { bar = true }, + opts = { + desc = "nvim-tree: print clipboard", + bar = true, + }, command = function() api.fs.print_clipboard() end, }, { name = "NvimTreeFindFile", - desc = "nvim-tree: find file", - opts = { bang = true, bar = true }, + opts = { + desc = "nvim-tree: find file", + bang = true, + bar = true, + }, command = function(c) api.tree.find_file { open = true, focus = true, update_root = c.bang } end, }, { name = "NvimTreeFindFileToggle", - desc = "nvim-tree: find file, toggle", - opts = { bang = true, nargs = "?", complete = "dir" }, + opts = { + desc = "nvim-tree: find file, toggle", + bang = true, + nargs = "?", + complete = "dir", + }, command = function(c) api.tree.toggle { find_file = true, focus = true, path = c.args, update_root = c.bang } end, }, { name = "NvimTreeResize", - desc = "nvim-tree: resize", - opts = { nargs = 1, bar = true }, + opts = { + desc = "nvim-tree: resize", + nargs = 1, + bar = true, + }, command = function(c) view.resize(c.args) end, }, { name = "NvimTreeCollapse", - desc = "nvim-tree: collapse", - opts = { bar = true }, + opts = { + desc = "nvim-tree: collapse", + bar = true, + }, command = function() api.tree.collapse_all(false) end, }, { name = "NvimTreeCollapseKeepBuffers", - desc = "nvim-tree: collapse, keep directories open", - opts = { bar = true }, + opts = { + desc = "nvim-tree: collapse, keep directories open", + bar = true, + }, command = function() api.tree.collapse_all(true) end, }, { name = "NvimTreeGenerateOnAttach", - desc = "nvim-tree: generate on_attach function from deprecated view.mappings", - opts = {}, + opts = { + desc = "nvim-tree: generate on_attach function from deprecated view.mappings", + }, command = function() keymap_legacy.cmd_generate_on_attach() end, }, } +function M.get_commands() + return vim.deepcopy(CMDS) +end + function M.setup() for _, cmd in ipairs(CMDS) do - local opts = vim.tbl_extend("force", cmd.opts, { desc = cmd.desc }) + local opts = vim.tbl_extend("force", cmd.opts, { force = true }) vim.api.nvim_create_user_command(cmd.name, cmd.command, opts) end end From 177a94727c9055b1fc16f85eb5ca117edc567f48 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Mon, 27 Mar 2023 10:10:56 +1100 Subject: [PATCH 5/5] feat(commands): add descriptions, api.get_commands -> api.commands.get --- doc/nvim-tree-lua.txt | 2 +- lua/nvim-tree/api.lua | 5 +++-- lua/nvim-tree/commands.lua | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 54345395cc7..91632426c2f 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -1511,7 +1511,7 @@ api.config.mappings.get_keymap_default() Return: ~ (table) as per |nvim_buf_get_keymap()| -api.get_commands() *nvim-tree.api.get_commands()* +api.commands.get() *nvim-tree.api.commands.get()* Retrieve all commands, see |nvim-tree-commands| Return: ~ diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index 2452aed75ac..22ef6ee8063 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -9,6 +9,7 @@ local Api = { git = {}, live_filter = {}, config = { mappings = {} }, + commands = {}, } local function inject_node(f) @@ -193,8 +194,8 @@ Api.config.mappings.get_keymap_default = function() return require("nvim-tree.keymap").get_keymap_default() end -Api.get_commands = function() - return require("nvim-tree.commands").get_commands() +Api.commands.get = function() + return require("nvim-tree.commands").get() end return Api diff --git a/lua/nvim-tree/commands.lua b/lua/nvim-tree/commands.lua index 1246a1ed963..d71937aa3ef 100644 --- a/lua/nvim-tree/commands.lua +++ b/lua/nvim-tree/commands.lua @@ -132,7 +132,7 @@ local CMDS = { }, } -function M.get_commands() +function M.get() return vim.deepcopy(CMDS) end