Skip to content

feat(commands): add descriptions #2083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1503,14 +1503,23 @@ 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()|

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*

Expand Down
42 changes: 2 additions & 40 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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()
Expand Down
5 changes: 5 additions & 0 deletions lua/nvim-tree/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ local Api = {
git = {},
live_filter = {},
config = { mappings = {} },
commands = {},
}

local function inject_node(f)
Expand Down Expand Up @@ -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
146 changes: 146 additions & 0 deletions lua/nvim-tree/commands.lua
Original file line number Diff line number Diff line change
@@ -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