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 6 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 = {},
Copy link
Member

Choose a reason for hiding this comment

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

Are these options used?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No, I was unsure if it was a convention to have these even if there are no underlying options, so I added these in even though they are empty.

I can remove them 👍🏼

},
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
Copy link
Member

Choose a reason for hiding this comment

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

Tested permutations and combination with :ls! reporting as expected.

  vim.keymap.set("n", "BW", function()
    api.node.buffer.wipe(api.tree.get_node_under_cursor(), { force = false })
  end, opts("Wipe"))

  vim.keymap.set("n", "BD", function()
    api.node.buffer.wipe(api.tree.get_node_under_cursor(), { force = true })
  end, opts("Delete"))

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Great! Thank you 👍🏼

Copy link
Member

Choose a reason for hiding this comment

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

Looks like this file can go - everything is in buffer.lua

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh yeah, we can remove this, I forgot to do so, sorry!

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)
Copy link
Member

Choose a reason for hiding this comment

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

Can we please change this to INFO? Users may wish to ignore this message.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, sure! Should I also change the error statement at line 37 (the one that says "Buffer for file " .. notify_node .. " is modified"), or just this one?

Copy link
Member

Choose a reason for hiding this comment

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

I think just this one. Deleting a modified buffer truly is an error.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ok, done!

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
Copy link
Member

Choose a reason for hiding this comment

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

What's the reason for this change? lua/nvim-tree/actions/tree/toggle.lua is still using this class...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No idea 😅 , this was probably done accidentally. Just reverted it 👍🏼

---@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
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.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
Loading