stevearc / resession.nvim

A replacement for mksession with a better API
MIT License
175 stars 13 forks source link

fix(quickfix): wait until it is safe to open the quickfix list #30

Closed Subjective closed 9 months ago

Subjective commented 9 months ago

I'm not sure why this might be the case, but it seems like nvim-bqf completely breaks in the first qf window restored by resession quickfix extension on startup.

Either deferring the callback of :copen or setqflist() resolves this, but the former seems to have the added benefit of properly restoring the quickfix list if the cursor was last in the qf window upon last saving the session (an entirely separate issue btw). Unfortunately, this also means that if the cursor was not previously in the qf window, then it is incorrectly set to the qf window upon restoring the session.

This is a rough proposal, do let me know if you have any better ideas.

stevearc commented 9 months ago

As written this PR won't work, since the copen is deferred to some point in the future and we have no idea what tab we'll be in or what window will be focused. This could put the qf window in a very incorrect location. If you can, I think a better approach would be to create a minimal repro config with resession and bqf so we can dive deeper into the debugging and see if we can find the root cause.

Subjective commented 9 months ago

Here's a minimal repro:

-- save as repro.lua
-- run with nvim -u repro.lua
-- DO NOT change the paths
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 = {
    "folke/tokyonight.nvim",
    {
        "nvim-telescope/telescope.nvim",
        cmd = "Telescope",
        dependencies = {
            "nvim-lua/plenary.nvim",
            {
                "nvim-telescope/telescope-fzf-native.nvim",
                build = "make",
            },
        },
        opts = {},
        config = function(_, opts)
            local telescope = require("telescope")
            telescope.setup(opts)
            telescope.load_extension("fzf")
        end,
        keys = { { "<leader>f", "<cmd>Telescope find_files<cr>" } },
    },
    { "kevinhwang91/nvim-bqf", ft = "qf", opts = {} },
    {
        "stevearc/resession.nvim",
        opts = {},
        keys = {
            {
                "<leader>s",
                function()
                    require("resession").save()
                end,
            },
            {
                "<leader>l",
                function()
                    require("resession").load()
                end,
            },
        },
    },
    -- add any other plugins here
}
require("lazy").setup(plugins, {
    root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")
-- add anything else here

This is a clip of me replicating the issue: https://asciinema.org/a/E2D9AKQdCwwhtaEVfkfTrroPu

It seems that nvim-bqf only has issues with the qf window that resession opens on startup – running :cclose then :copen to reopen the qf window works fine.

stevearc commented 9 months ago

nvim-bqf needed the quickfix to be populated earlier. I've fixed this by adding a new method to the extension API: on_pre_load, which will run before all the buffers/windows/tabs are restored. I've changed the quickfix extension to load the previous qf in pre-load, and that seems to fix the issue with bqf.