-
-
Notifications
You must be signed in to change notification settings - Fork 623
feat(#3037): add API node.buffer.delete, node.buffer.wipe #3040
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
Changes from 12 commits
71451ea
c72de8c
6de8526
33ff62a
e645b5f
404aae7
2719542
6e598f3
c87c193
343b8a0
199c0c0
ede43ed
0703c47
aa6fe10
2bcb5fe
7a1712e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
-- 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 | ||
|
||
function M.setup(_) | ||
end | ||
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. We can remove this NOP; we're moving away from this setup pattern anyway. |
||
|
||
return M |
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. Tested permutations and combination with 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")) 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. Great! Thank you 👍🏼 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. Looks like this file can go - everything is in buffer.lua 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. 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.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 |
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 | ||
|
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.