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

[FEATURE] Close buffers for files deleted in ranger #15

Closed
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ do not invoke `ranger_nvim.setup()` `ranger.nvim` will use the default values.
| `enable_cmds` | `boolean` | `false` | Set vim commands, see [commands](#commands). |
| `keybinds` | `Keybind = table<string, OPEN_MODE>` | See [ranger keybindings](#ranger-keybindings). | Key bindings set in `ranger` to control how files are opened in neovim. See [ranger keybindings](#ranger-keybindings). |
| `replace_netrw` | `boolean` | `false` | Replace `netrw` with `ranger` when neovim is launched with a directory argument. |
| `close_deleted_files` | `boolean` | `false` | Attempt to close buffers for files that were deleted with `ranger`. |
| `ui` | `UI` | See [UI Configuration](#configuration---ui). | Settings for ranger window. |

See below code snippet for example configuring `ranger.nvim` with the default
Expand All @@ -52,6 +53,7 @@ local ranger_nvim = require("ranger-nvim")
ranger_nvim.setup({
enable_cmds = false,
replace_netrw = false,
close_deleted_files = false,
keybinds = {
["ov"] = ranger_nvim.OPEN_MODE.vsplit,
["oh"] = ranger_nvim.OPEN_MODE.split,
Expand Down
41 changes: 41 additions & 0 deletions lua/ranger-nvim.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ M.OPEN_MODE = {
---@class Options
---@field enable_cmds boolean set commands
---@field replace_netrw boolean
---@field close_deleted_files boolean
---@field keybinds Keybinds
---@field ui UI

Expand All @@ -31,6 +32,7 @@ M.OPEN_MODE = {
local opts = {
enable_cmds = false,
replace_netrw = false,
close_deleted_files = false,
keybinds = {
["ov"] = M.OPEN_MODE.vsplit,
["oh"] = M.OPEN_MODE.split,
Expand Down Expand Up @@ -135,6 +137,32 @@ local function open_win()
vim.api.nvim_set_hl(0, "NormalFloat", { bg = "" })
end

---Returns a table with the names of all the currently listed buffers, which point to existing filenames.
local function get_buffers_for_existing_files()
local buffers = {}

for buffer_nr = 1, vim.fn.bufnr("$") do
if vim.fn.buflisted(buffer_nr) == 1 then
local bufname = vim.fn.bufname(buffer_nr)
if vim.fn.filereadable(bufname) == 1 then
table.insert(buffers, bufname)
end
end
end

return buffers
end

---Closes any buffers from the given table of buffer names which point to files that don't exist
---@param buffers table<string>
local function close_empty_buffers(buffers)
for _, buffer in ipairs(buffers) do
if vim.fn.filereadable(buffer) ~= 1 then
vim.cmd.bdelete(buffer)
end
end
end

---Clean up temporary files used to communicate between ranger and the plugin.
local function clean_up()
vim.fn.delete(SELECTED_FILEPATH)
Expand Down Expand Up @@ -195,6 +223,12 @@ function M.open(select_current_file)

clean_up()

-- Store buffers that are open (and not empty) prior to running ranger
local buffers_for_existing_files
if opts.close_deleted_files then
buffers_for_existing_files = get_buffers_for_existing_files()
end

local cmd = build_ranger_cmd(select_current_file)
local last_win = vim.api.nvim_get_current_win()
open_win()
Expand All @@ -206,6 +240,13 @@ function M.open(select_current_file)
open_files(SELECTED_FILEPATH, get_open_func())
end
clean_up()

-- Close any buffers that were previously pointing to existing files, but don't
-- after running ranger. This should close any buffers for files which were
-- deleted using ranger.
if opts.close_deleted_files then
close_empty_buffers(buffers_for_existing_files)
end
end,
})
vim.cmd.startinsert()
Expand Down