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

bug: if the active buffer is filtered out, restoration opens up a default buffer #66

Open
3 tasks done
jpetrie opened this issue Aug 11, 2024 · 1 comment
Open
3 tasks done
Labels
bug Something isn't working

Comments

@jpetrie
Copy link

jpetrie commented Aug 11, 2024

Did you check the docs and existing issues?

  • I have read the docs
  • I have searched the existing issues

Neovim version (nvim -v)

NVIM v0.10.0

Operating system/version

14.5 (23F79)

Describe the bug

If buf_filter is used to exclude a buffer, and that buffer happens to be the active buffer in a window during a session save, then when that session is restored you'll be in a default empty buffer (rather than the next-oldest buffer that was saved into the session, which is what I'd expect).

I have resession configured to automatically save and restore sessions per directory exactly as specified in the README, except that I'm also using a custom buf_filter to exclude certain types of buffers I don't want restored. For the sake of this report I have reproduced the issue using the following simplified buf_filter when setting up resession:

  resession.setup({
    buf_filter = function(buffer)
      local file_type = vim.bo[buffer].filetype
      if vim.bo[buffer].filetype == "test" then
        return false
      end

      return resession.default_buf_filter(buffer)
    end,
    -- the rest of the setup...
  })

Then I create a.txt and b.txt in a directory, open both, :set ft=test while b.txt is the active buffer (in a single window - no splits or anything) and exit Neovim. When I launch it again, a.txt is restored (I can see it in :ls) but Neovim launches into an empty buffer. I think this might be because no "wins" element gets created in the session JSON in this case (get_win_info is returning false because the buffer in the window isn't passing the filter).

Is this the intended behavior? It does make a certain kind of sense from a standpoint of having resession not do anything "automagically" for you, which I appreciate, but I feel like it would also make sense to have resession fall back to loading one of the buffers that did get saved (probably based on load_order) in this case, and only ending up in the default empty buffer if there was no other option.

This can be worked around with existing functionality - a post_load hook that detects the empty buffer and then does bp or something similar, although I don't know if that's the most elegant option.

What is the severity of this bug?

minor (annoyance)

Steps To Reproduce

Use the minimal script below to reproduce (nvim -u repro.lua).

:e a.txt and :e b.txt and optionally type some stuff in them.

:qa: and nvim -u bug.lua again to restore the session. Note which buffer you are in (the last one you had open, as expected).

:set ft=test and :qa

Then nvim -u bug.lua again - this time you'll be in a [No File] buffer, even though :ls shows that your other buffer was loaded.

Expected Behavior

I'd expect to load into the last buffer that was open, unless there are no buffers in the saved session, at which point I'd expect to be loaded into the empty default buffer.

Directory structure

./a.txt
./b.txt

Repro

local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "runtime", "cache" }) do
	vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
	vim.fn.system({
		"git",
		"clone",
		"--filter=blob:none",
		"--single-branch",
		"https://github.com/folke/lazy.nvim.git",
		lazypath,
	})
end
vim.opt.runtimepath:prepend(lazypath)

vim.g.mapleader = " "

-- install plugins
local plugins = {
	{
    "stevearc/resession.nvim",
    config = function()
      local resession = require("resession")
      resession.setup({
        buf_filter = function(buffer)
          local file_type = vim.bo[buffer].filetype
          if file_type == "test" then
            return false
          end

          return resession.default_buf_filter(buffer)
        end,
      })

      vim.api.nvim_create_autocmd("VimEnter", {
        callback = function()
          if vim.fn.argc(-1) > 0 then
            return
          end

          resession.load(vim.fn.getcwd(), {dir = "dirsession", silence_errors = true})
        end,
        nested = true,
      })

      vim.api.nvim_create_autocmd("VimLeavePre", {
        callback = function()
          resession.save(vim.fn.getcwd(), {dir = "dirsession", notify = false})
        end,
      })
    end,
  },
}
require("lazy").setup(plugins, {
	root = root .. "/plugins",
})

Did you check the bug with a clean config?

  • I have confirmed that the bug reproduces with nvim -u repro.lua using the repro.lua file above.
@jpetrie jpetrie added the bug Something isn't working label Aug 11, 2024
@jpetrie
Copy link
Author

jpetrie commented Aug 11, 2024

Further digging around led me to discover this line in init.lua that suggests this is at least known, if not intentional?

If the behavior I'm expecting is in line with what you'd want resession to natively support, I'm guessing this might be the place to handle it - I've locally added an else clause here and just done vim.cmd("bp") and I get the behavior I'd like. Happy to try and make that more robust and submit a PR if you'd like - otherwise I'll just keep using a post_load hook, just let me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant