From 9a1343c396178dc55ccc1166bf1eb434b5c18c43 Mon Sep 17 00:00:00 2001 From: Greg Hurrell Date: Fri, 14 Jul 2023 21:04:01 +0200 Subject: [PATCH] fix: teach buffer finder to see all buffers passed on command-line MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As noted in the comment on the issue, the Ruby version used to just grab buffer names with `:ls`. The Lua version instead uses `nvim_list_bufs()`, which the help claims is more like `:ls!` because it includes unlisted (unloaded/deleted) buffers: Includes unlisted (unloaded/deleted) buffers, like `:ls!`. Use |nvim_buf_is_loaded()| to check if a buffer is loaded. Unfortunately, what "unlisted" means is a bit ambiguous, because the help for `:ls` says this: When the [!] is included the list will show unlisted buffers (the term "unlisted" is a bit confusing then...). So, I think I just added the `nvim_buf_is_loaded()` check (in 0b8c9ed39c1352e3, "feat(lua): continue sketching out UI", July 20, 2022) because the help said I should use that to avoid unloaded buffers. The docs for `nvim_buf_is_loaded()` say: Checks if a buffer is valid and loaded. See |api-buffer| for more info about unloaded buffers. So, it's checking for loaded _and_ "valid", whatever that means. It turns out there is a `nvim_buf_is_valid()` function, so perhaps we should just use that! Checks if a buffer is valid. Note: Even if a buffer is valid it may have been unloaded. See |api-buffer| for more info about unloaded buffers. Parameters: ~ • {buffer} Buffer handle, or 0 for current buffer Return: ~ true if the buffer is valid, false otherwise. The risk here is that we let somebody try switching to an unloaded buffer, which presumably might not work. In my experimenting at least, when I run `:bunload` on a buffer and then try switching to it with Command-T, it seems to work fine. Presumably it is loading it again. Closes: https://github.com/wincent/command-t/issues/418 --- doc/command-t.txt | 3 ++- lua/wincent/commandt/init.lua | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/doc/command-t.txt b/doc/command-t.txt index cfe9a3b0..04408409 100644 --- a/doc/command-t.txt +++ b/doc/command-t.txt @@ -609,7 +609,8 @@ HISTORY *command-t-history* main (not yet released) ~ -- ... +- fix: teach buffer finder to see all buffers passed into Neovim as + command-line arguments (https://github.com/wincent/command-t/issues/418). 6.0.0-b.1 (16 December 2022) ~ diff --git a/lua/wincent/commandt/init.lua b/lua/wincent/commandt/init.lua index 59c23e92..20f8e15a 100644 --- a/lua/wincent/commandt/init.lua +++ b/lua/wincent/commandt/init.lua @@ -161,7 +161,7 @@ local default_options = { local handles = vim.api.nvim_list_bufs() local paths = {} for _, handle in ipairs(handles) do - if vim.api.nvim_buf_is_loaded(handle) then + if vim.api.nvim_buf_is_valid(handle) then local name = vim.api.nvim_buf_get_name(handle) if name ~= '' then local relative = vim.fn.fnamemodify(name, ':~:.')