Skip to content

WIP: add keymap to close file buffer #1

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

Closed
wants to merge 7 commits into from
Closed
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: 11 additions & 0 deletions lua/nvim-tree/actions/node/close-file-buffer.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
local M = {}

---@param filename string
---@return nil
function M.fn(filename)
vim.cmd({ cmd = "bdelete", args = { filename } })
end

function M.setup(opts) end

return M
2 changes: 2 additions & 0 deletions lua/nvim-tree/actions/node/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ local M = {}

M.file_popup = require("nvim-tree.actions.node.file-popup")
M.open_file = require("nvim-tree.actions.node.open-file")
M.close_file_buffer = require("nvim-tree.actions.node.close-file-buffer")
M.run_command = require("nvim-tree.actions.node.run-command")
M.system_open = require("nvim-tree.actions.node.system-open")

function M.setup(opts)
require("nvim-tree.actions.node.system-open").setup(opts)
require("nvim-tree.actions.node.file-popup").setup(opts)
require("nvim-tree.actions.node.open-file").setup(opts)
require("nvim-tree.actions.node.close-file-buffer").setup(opts)
end

return M
10 changes: 10 additions & 0 deletions lua/nvim-tree/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ local Api = {
},
run = {},
open = {},
close = {},
},
events = {},
marks = {
Expand Down Expand Up @@ -230,6 +231,13 @@ local function edit(mode, node)
actions.node.open_file.fn(mode, path)
end

---@param node Node
local function close_file_buffer(node)
local file_link = node:as(FileLinkNode)
local path = file_link and file_link.link_to or node.absolute_path
actions.node.close_file_buffer.fn(path)
end

---@param mode string
---@param toggle_group boolean?
---@return fun(node: Node)
Expand All @@ -249,6 +257,8 @@ local function open_or_expand_or_dir_up(mode, toggle_group)
end
end

Api.node.close.close_file_buffer = wrap_node(close_file_buffer)

Api.node.open.edit = wrap_node(open_or_expand_or_dir_up("edit"))
Api.node.open.drop = wrap_node(open_or_expand_or_dir_up("drop"))
Api.node.open.tab_drop = wrap_node(open_or_expand_or_dir_up("tab_drop"))
Expand Down
1 change: 1 addition & 0 deletions lua/nvim-tree/keymap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function M.default_on_attach(bufnr)
vim.keymap.set("n", "<C-r>", api.fs.rename_sub, opts("Rename: Omit Filename"))
vim.keymap.set("n", "<C-t>", api.node.open.tab, opts("Open: New Tab"))
vim.keymap.set("n", "<C-v>", api.node.open.vertical, opts("Open: Vertical Split"))
vim.keymap.set("n", "<C-c>", api.node.close.close_file_buffer, opts("Close file buffer (if any exist)"))
vim.keymap.set("n", "<C-x>", api.node.open.horizontal, opts("Open: Horizontal Split"))
vim.keymap.set("n", "<BS>", api.node.navigate.parent_close, opts("Close Directory"))
vim.keymap.set("n", "<CR>", api.node.open.edit, opts("Open"))
Expand Down