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 API node.buffer.delete, node.buffer.wipe #3040

Merged
merged 16 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from 15 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
8 changes: 8 additions & 0 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2164,6 +2164,12 @@ node.run.cmd({node}) *nvim-tree-api.node.run.cmd()*
node.run.system({node}) *nvim-tree-api.node.run.system()*
Execute |nvim-tree.system_open|

node.buffer.delete({node}) *nvim-tree-api.node.buffer.delete()*
Deletes node's related buffer.

node.buffer.wipe({node}) *nvim-tree-api.node.buffer.wipe()*
Same as |api.node.buffer.delete()|, but runs :bwipe instead of :bdelete.

==============================================================================
6.4 API GIT *nvim-tree-api.git*

Expand Down Expand Up @@ -3178,6 +3184,8 @@ highlight group is not, hard linking as follows: >
|nvim-tree-api.marks.navigate.prev()|
|nvim-tree-api.marks.navigate.select()|
|nvim-tree-api.marks.toggle()|
|nvim-tree-api.node.buffer.delete()|
|nvim-tree-api.node.buffer.wipe()|
|nvim-tree-api.node.navigate.diagnostics.next()|
|nvim-tree-api.node.navigate.diagnostics.next_recursive()|
|nvim-tree-api.node.navigate.diagnostics.prev()|
Expand Down
58 changes: 58 additions & 0 deletions lua/nvim-tree/actions/node/buffer.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
-- Copyright 2019 Yazdani Kiyan under MIT License
local notify = require("nvim-tree.notify")

local M = {}

---@param node Node
---@param opts ApiNodeDeleteWipeBufferOpts|nil
---@return nil
function M.delete(node, opts)
M.delete_buffer("delete", node.absolute_path, opts)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This handles nil or partial node gracefully, however not a malformed node like { aaaabsolute_path = "foo" }

That's OK - garbage in, garbage out.

end

---@param node Node
---@param opts ApiNodeDeleteWipeBufferOpts|nil
---@return nil
function M.wipe(node, opts)
M.delete_buffer("wipe", node.absolute_path, opts)
end

---@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.info("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
1 change: 1 addition & 0 deletions lua/nvim-tree/actions/node/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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.buffer = require("nvim-tree.actions.node.buffer")

function M.setup(opts)
require("nvim-tree.actions.node.system-open").setup(opts)
Expand Down
11 changes: 11 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 = {},
buffer = {},
},
events = {},
marks = {
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you.


Api.node.buffer.delete = wrap_node(function(node, opts)
actions.node.buffer.delete(node, opts)
end)
Api.node.buffer.wipe = wrap_node(function(node, opts)
actions.node.buffer.wipe(node, opts)
end)

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

Api.events.subscribe = events.subscribe
Expand Down
Loading