From 45400cd7e02027937cd5e49845545e606ecf5a1f Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Tue, 28 Mar 2023 10:52:48 +1100 Subject: [PATCH] feat(api): add api.commands.get (#2083) * feat(commands): add descriptions * Update lua/nvim-tree.lua Co-authored-by: gegoune <69750637+gegoune@users.noreply.github.com> * 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 <69750637+gegoune@users.noreply.github.com> --- doc/nvim-tree-lua.txt | 11 ++- lua/nvim-tree.lua | 42 +---------- lua/nvim-tree/api.lua | 5 ++ lua/nvim-tree/commands.lua | 146 +++++++++++++++++++++++++++++++++++++ 4 files changed, 163 insertions(+), 41 deletions(-) create mode 100644 lua/nvim-tree/commands.lua diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 4f9f4143..91632426 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.commands.get() *nvim-tree.api.commands.get()* + 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.lua b/lua/nvim-tree.lua index 3fd4d697..84091b8b 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,43 +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" }) - vim.api.nvim_create_user_command("NvimTreeClose", function() - require("nvim-tree.api").tree.close() - end, { bar = true }) - 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" }) - vim.api.nvim_create_user_command("NvimTreeFocus", function() - require("nvim-tree.api").tree.focus() - end, { bar = true }) - vim.api.nvim_create_user_command("NvimTreeRefresh", function() - require("nvim-tree.api").tree.reload() - end, { bar = true }) - vim.api.nvim_create_user_command("NvimTreeClipboard", function() - require("nvim-tree.api").fs.print_clipboard() - end, { bar = true }) - 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 }) - 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" }) - vim.api.nvim_create_user_command("NvimTreeResize", function(res) - M.resize(res.args) - end, { nargs = 1, bar = true }) - vim.api.nvim_create_user_command("NvimTreeCollapse", function() - require("nvim-tree.api").tree.collapse_all(false) - end, { bar = true }) - 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 - function M.change_dir(name) change_dir.fn(name) @@ -811,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/api.lua b/lua/nvim-tree/api.lua index 72a828a8..cba415dd 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,4 +194,8 @@ Api.config.mappings.get_keymap_default = function() return require("nvim-tree.keymap").get_keymap_default() end +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 new file mode 100644 index 00000000..d71937aa --- /dev/null +++ b/lua/nvim-tree/commands.lua @@ -0,0 +1,146 @@ +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", + opts = { + desc = "nvim-tree: open", + nargs = "?", + complete = "dir", + }, + command = function(c) + api.tree.open { path = c.args } + end, + }, + { + name = "NvimTreeClose", + opts = { + desc = "nvim-tree: close", + bar = true, + }, + command = function() + api.tree.close() + end, + }, + { + name = "NvimTreeToggle", + 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", + opts = { + desc = "nvim-tree: focus", + bar = true, + }, + command = function() + api.tree.focus() + end, + }, + { + name = "NvimTreeRefresh", + opts = { + desc = "nvim-tree: refresh", + bar = true, + }, + command = function() + api.tree.reload() + end, + }, + { + name = "NvimTreeClipboard", + opts = { + desc = "nvim-tree: print clipboard", + bar = true, + }, + command = function() + api.fs.print_clipboard() + end, + }, + { + name = "NvimTreeFindFile", + 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", + 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", + opts = { + desc = "nvim-tree: resize", + nargs = 1, + bar = true, + }, + command = function(c) + view.resize(c.args) + end, + }, + { + name = "NvimTreeCollapse", + opts = { + desc = "nvim-tree: collapse", + bar = true, + }, + command = function() + api.tree.collapse_all(false) + end, + }, + { + name = "NvimTreeCollapseKeepBuffers", + opts = { + desc = "nvim-tree: collapse, keep directories open", + bar = true, + }, + command = function() + api.tree.collapse_all(true) + end, + }, + { + name = "NvimTreeGenerateOnAttach", + opts = { + desc = "nvim-tree: generate on_attach function from deprecated view.mappings", + }, + command = function() + keymap_legacy.cmd_generate_on_attach() + end, + }, +} + +function M.get() + return vim.deepcopy(CMDS) +end + +function M.setup() + for _, cmd in ipairs(CMDS) do + local opts = vim.tbl_extend("force", cmd.opts, { force = true }) + vim.api.nvim_create_user_command(cmd.name, cmd.command, opts) + end +end + +return M