Skip to content

Commit

Permalink
fix: teach buffer finder to see all buffers passed on command-line
Browse files Browse the repository at this point in the history
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
0b8c9ed, "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: #418
  • Loading branch information
wincent committed Jul 14, 2023
1 parent 23d2860 commit 9a1343c
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
3 changes: 2 additions & 1 deletion doc/command-t.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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) ~

Expand Down
2 changes: 1 addition & 1 deletion lua/wincent/commandt/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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, ':~:.')
Expand Down

0 comments on commit 9a1343c

Please sign in to comment.