-
-
Notifications
You must be signed in to change notification settings - Fork 610
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
alex-courtis
merged 16 commits into
nvim-tree:master
from
GCrispino:feat/close-file-buffer-2
Jan 25, 2025
+95
−1
Merged
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
71451ea
feat(mappings): add key map to close file buffer
GCrispino c72de8c
feat: implement Api.node.buffer.delete
GCrispino 6de8526
feat: implement Api.node.buffer.wipe
GCrispino 33ff62a
refactor: add util fn for common delete ops on bufs
GCrispino e645b5f
fix: minor fixes
GCrispino 404aae7
refactor: fix lint issues
GCrispino 2719542
fix: undo unintended ApiTreeToggleOpts change
GCrispino 6e598f3
fix: change error message level to info
GCrispino c87c193
fix: remove unused opts
GCrispino 343b8a0
refactor: merge delete-buffer and wipe-buffer into single buffer file
GCrispino 199c0c0
refactor: make wipe and delete fns take a node instead of a file path
GCrispino ede43ed
docs: update help with new API commands
GCrispino 0703c47
remove refactored utils.lua
alex-courtis aa6fe10
remove unused static setup
alex-courtis 2bcb5fe
Merge branch 'master' into feat/close-file-buffer-2
alex-courtis 7a1712e
tweak doc
alex-courtis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ local Api = { | |
}, | ||
run = {}, | ||
open = {}, | ||
buffer = {}, | ||
}, | ||
events = {}, | ||
marks = { | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.