Skip to content
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

feat(#3037): add node API methods for deleting/wiping buffers #3040

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,8 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
remove_file = {
close_window = true,
},
delete_buffer = {},
wipe_buffer = {},
},
trash = {
cmd = "gio trash",
Expand Down
16 changes: 16 additions & 0 deletions lua/nvim-tree/actions/node/delete-buffer.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- Copyright 2019 Yazdani Kiyan under MIT License
local utils = require("nvim-tree.actions.node.utils")

local M = {}

---@param filename string
---@param opts ApiNodeDeleteWipeBufferOpts|nil
---@return nil
function M.fn(filename, opts)
utils.delete_buffer("delete", filename, opts)
end

function M.setup(_)
end

return M
4 changes: 4 additions & 0 deletions lua/nvim-tree/actions/node/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ M.file_popup = require("nvim-tree.actions.node.file-popup")
M.open_file = require("nvim-tree.actions.node.open-file")
M.run_command = require("nvim-tree.actions.node.run-command")
M.system_open = require("nvim-tree.actions.node.system-open")
M.delete_buffer = require("nvim-tree.actions.node.delete-buffer")
M.wipe_buffer = require("nvim-tree.actions.node.wipe-buffer")

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.delete-buffer").setup(opts)
require("nvim-tree.actions.node.wipe-buffer").setup(opts)
end

return M
44 changes: 44 additions & 0 deletions lua/nvim-tree/actions/node/utils.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
-- Copyright 2019 Yazdani Kiyan under MIT License
local notify = require("nvim-tree.notify")

local M = {}

---@alias ApiNodeDeleteWipeBufferMode '"delete"'|'"wipe"'

---@param mode ApiNodeDeleteWipeBufferMode
---@param filename string
---@param opts ApiNodeDeleteWipeBufferOpts|nil
---@return nil
function M.delete_buffer(mode, filename, opts)
if type(mode) ~= "string" then
mode = "delete"
end

local buf_fn = vim.cmd.bdelete
if mode == "wipe" then
buf_fn = vim.cmd.bwipe
end

opts = opts or { force = false }

local notify_node = notify.render_path(filename)

-- check if buffer for file at cursor exists and if it is loaded
local bufnr_at_filename = vim.fn.bufnr(filename)
if bufnr_at_filename == -1 or vim.fn.getbufinfo(bufnr_at_filename)[1].loaded == 0 then
notify.error("No loaded buffer coincides with " .. notify_node)
return
end

local force = opts.force
-- check if buffer is modified
local buf_modified = vim.fn.getbufinfo(bufnr_at_filename)[1].changed
if not force and buf_modified == 1 then
notify.error("Buffer for file " .. notify_node .. " is modified")
return
end

buf_fn({ filename, bang = force })
end

return M
16 changes: 16 additions & 0 deletions lua/nvim-tree/actions/node/wipe-buffer.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- Copyright 2019 Yazdani Kiyan under MIT License
local utils = require("nvim-tree.actions.node.utils")

local M = {}

---@param filename string
---@param opts ApiNodeDeleteWipeBufferOpts|nil
---@return nil
function M.fn(filename, opts)
utils.delete_buffer("wipe", filename, opts)
end

function M.setup(_)
end

return M
13 changes: 12 additions & 1 deletion 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 = {},
buffer = {},
},
events = {},
marks = {
Expand Down Expand Up @@ -131,7 +132,7 @@ end
Api.tree.open = wrap(actions.tree.open.fn)
Api.tree.focus = Api.tree.open

---@class ApiTreeToggleOpts
---@class ApiTreeToggleOptsApiTreeTo
---@field path string|nil
---@field current_window boolean|nil default false
---@field winid number|nil
Expand Down Expand Up @@ -286,6 +287,16 @@ Api.node.navigate.diagnostics.prev_recursive = wrap_node(actions.moves.item.fn({
Api.node.navigate.opened.next = wrap_node(actions.moves.item.fn({ where = "next", what = "opened" }))
Api.node.navigate.opened.prev = wrap_node(actions.moves.item.fn({ where = "prev", what = "opened" }))

---@class ApiNodeDeleteWipeBufferOpts
---@field force boolean|nil default false

Api.node.buffer.delete = wrap_node(function(node, opts)
actions.node.delete_buffer.fn(node.absolute_path, opts)
end)
Api.node.buffer.wipe = wrap_node(function(node, opts)
actions.node.wipe_buffer.fn(node.absolute_path, opts)
end)

Api.git.reload = wrap_explorer("reload_git")

Api.events.subscribe = events.subscribe
Expand Down