olimorris / persisted.nvim

💾 Simple session management for Neovim with git branching, autoloading and Telescope support
MIT License
437 stars 26 forks source link

[Bug]: no-neck-pain.nvim `disable` doesn't work in `SavePre` hook #129

Closed okkdev closed 3 months ago

okkdev commented 4 months ago

Your minimal.lua config

local root = "/tmp/persisted"

-- Set stdpaths to use root dir
for _, name in ipairs({ "config", "data", "state", "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.opt.sessionoptions = "buffers,curdir,folds,globals,tabpages,winpos,winsize" -- Session options to store in the session

-- Install plugins
local plugins = {
  {
    "olimorris/persisted.nvim",
    opts = {
      autoload = true,
    },
  },
  {
    "shortcuts/no-neck-pain.nvim",
    opts = {
      buffers = {
        scratchPad = {
          enabled = true,
          location = "/tmp/",
        },
        bo = {
          filetype = "md",
        },
      },
    },
  },
  -- Put any other plugins here
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

local persisted_hooks = vim.api.nvim_create_augroup("PersistedHooks", {})

vim.api.nvim_create_autocmd({ "User" }, {
  pattern = "PersistedSavePre",
  group = persisted_hooks,
  callback = function()
    require("no-neck-pain").disable()
  end,
})

Error messages

No response

Describe the bug

What I expect

After :quitting a file while using :NoNeckPain to center the code, I expect to get back into the file at the same position where I left off when loading the Session.

What happens instead

Persisted.nvim opens the side buffer of NoNeckPain instead of my file. Even though I disable NoNeckPain using the PersistedSavePre hook.

Reproduce the bug

Steps to reproduce:

  1. nvim
  2. :e some-file.txt
  3. :NoNeckPain
  4. :q
  5. nvim

Final checks

olimorris commented 4 months ago

With your setup, :q seems to close the buffer you're working in, leaving the no-neck-pain tabs open:

2024-05-16 16_22_41 - WezTerm

okkdev commented 4 months ago

I see. But if I disable them pre save, how are they getting persisted? Is that because the working buffer closes first?

olimorris commented 4 months ago

So doing :q triggers the VimLeavePre autocmd. I've built Persisted to listen for that autocmd and run the persisted.save method which in turn calls this method:

---Write the session to disk
---@param session string
---@return nil
local function write(session)
  vim.api.nvim_exec_autocmds("User", { pattern = "PersistedSavePre" })
  vim.cmd("mks! " .. e(session))
  vim.g.persisting = true
  vim.api.nvim_exec_autocmds("User", { pattern = "PersistedSavePost" })
end

I suspect that VimLeavePre causes a conflict with NoNeckPain which doesn't allow it to close down its buffers. Might be worth asking the creator how this works in their plugin.

A quick search on GitHub reveals that there are a lot of users utilising the PersistedSavePre hook with other plugins.

okkdev commented 4 months ago

I'm taking @shortcuts up on their offer to ping them.

@shortcuts Any ideas/insight, why this doesn't work?

shortcuts commented 4 months ago

Hey there, thanks for the investigation! I've tried with your reproduction and the only weird thing I've seen is that the reopened file is one of the nnp side buffer.

My guess is that there is a race condition between the two plugin since we both have hooks on the quit events. I noticed the prompt before quitting on your recording @olimorris and I do believe this might cause an issue, from my investigation it seems to be on the nnp side so I'll try to see if I can improve that!

olimorris commented 4 months ago

@shortcuts sounds great. Totally up for rework on my side too to get our plugins to be friends 😉

okkdev commented 4 months ago

I found someone close the NoNeckPain buffers "manually": https://github.com/yuma140902/dotfiles-public/blob/8dd33b742a45e74416b785d1a529ef4e35120752/neovim/nvim/lua/rc/plugins.lua#L568

They are using the blank buffers, I'm not sure how I can identify the buffers when I have scratch pads enabled. I tried some things, but couldn't get it to work. It's probably a skill issue though.

shortcuts commented 4 months ago

I found someone close the NoNeckPain buffers "manually": https://github.com/yuma140902/dotfiles-public/blob/8dd33b742a45e74416b785d1a529ef4e35120752/neovim/nvim/lua/rc/plugins.lua#L568

They are using the blank buffers, I'm not sure how I can identify the buffers when I have scratch pads enabled. I tried some things, but couldn't get it to work. It's probably a skill issue though.

You can maybe try to compare the path of the file opened in the buffer? You can get access to it via _G.NoNeckPain.config and from there you have the path of the opened file in buffers.[left|right].scratchPad.pathToFile


I find a way to remove the prompt when quitting nvim with persisted enabled but it doesn't yet solve the issue of the side buffers :/ i'll keep digging

olimorris commented 3 months ago

Closing this for now and please let me know if we do find a solution