stevearc / resession.nvim

A replacement for mksession with a better API
MIT License
220 stars 15 forks source link

bug: `tab_buf_filter` example from README doesn't work with partial matches #48

Closed serranomorante closed 8 months ago

serranomorante commented 9 months ago

Did you check the docs and existing issues?

Neovim version (nvim -v)

NVIM v0.10.0-dev-2039+g583c1de17

Operating system/version

arch linux

Describe the bug

tab_buf_filter example from README doesn't work with partial matches

What is the severity of this bug?

breaking (some functionality is broken)

Steps To Reproduce

From the README

This will save only the current tabpage layout, but will save all of the open buffers. You can provide a filter to exclude buffers. For example, if you are using :tcd to have tabs open for different directories, this will only save buffers in the current tabpage directory:

require("resession").setup({
  tab_buf_filter = function(tabpage, bufnr)
    local dir = vim.fn.getcwd(-1, vim.api.nvim_tabpage_get_number(tabpage))
    return vim.startswith(vim.api.nvim_buf_get_name(bufnr), dir)
  end,
})

That code snippet doesn't work with partial matches in folder names. If tab:1 has :tcd app-1 and tab:2 has :tcd app-12, saving session on tab:1 will also save files from tab:2 because any filename from tab:2 will match both app-12 folder and app-1 folder.

Sorry if I'm not providing a repro, but this can be reproduced easily from the nvim commands.

lua vim.print(vim.startswith("/folder/app-1/file1.md", "/folder/app-1")) -- true
lua vim.print(vim.startswith("/folder/app-12/file1.md", "/folder/app-1")) -- also true

I'm using this on my config to fix this:

    tab_buf_filter = function(tabpage, bufnr)
      local dir = vim.fn.getcwd(-1, vim.api.nvim_tabpage_get_number(tabpage))
      if dir:sub(-1) ~= "/" then dir = dir .. "/" end
      return vim.api.nvim_buf_get_name(bufnr):sub(1, #dir) == dir
    end,
...

Thanks for this beautiful plugin πŸ’–

Expected Behavior

only save buffers for current tab with the snipped provided in the README.

Directory structure

β”œβ”€β”€ app-1 β”œβ”€β”€ app-12 └── repro.lua

Repro

lua vim.print(vim.startswith("/folder/app-1/file1.md", "/folder/app-1")) -- true
lua vim.print(vim.startswith("/folder/app-12/file1.md", "/folder/app-1")) -- also true

Did you check the bug with a clean config?

stevearc commented 8 months ago

Updated the example, thanks!