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] How to run commands on cwd if no session is restored? #398

Open
jack-obrien opened this issue Jan 24, 2025 · 1 comment
Open

[BUG] How to run commands on cwd if no session is restored? #398

jack-obrien opened this issue Jan 24, 2025 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@jack-obrien
Copy link

Describe the bug
I want to run my custom function open_nvim_tree_no_focus on session restore. I also want to run it when I change directory from within nvim to a new project. So I set that function as my post_restore_cmds and my no_restore_cmds. This works fine in almost all cases. However, when I change directory from within neovim to a new project (i.e. a directory with no saved session), this case is not covered by my current config. Looking at the docs, it seems like no_restore_cmds is only run at VimEnter when no session is restored. If I'm not mistaken, VimEnter is only run when opening nvim, not on cwd.

To Reproduce
Steps to reproduce the behavior:

  1. set a function under no_restore_cmds
  2. open nvim
  3. cd to a directory with no session saved
  4. Notice that your function has not been run

Expected behavior
I'm not sure if this behaviour should be changed in no_restore_cmds as that would be a breaking change. I'm just wondering what would be the best way to configure this behaviour? I will probably just put my function call open_nvim_tree_no_focus in my post_cwd_changed_cmds - as that function should be ok to be called twice in some cases.

@jack-obrien jack-obrien added the bug Something isn't working label Jan 24, 2025
@jack-obrien
Copy link
Author

OK here's my solution to all my nvim tree qualms - I'll close this issue now and leave it up in case it's helpful to others.

Another one of my issues was that we have to manually close nvim tree when switching directories to a new session, as mentioned in #178

vim.o.sessionoptions = 'blank,buffers,curdir,folds,help,tabpages,winsize,winpos,terminal,localoptions'
local barbar_tree_offset = require 'custom.plugins.utils.barbar-tree-offset'

return {
  {
    'rmagatti/auto-session',
    lazy = false,
    dependencies = {
      'nvim-telescope/telescope.nvim', -- Only needed if you want to use session lens
    },

    ---enables autocomplete for opts
    ---@module "auto-session"
    ---@type AutoSession.Config
    opts = {
      suppressed_dirs = { '~/', '~/Projects', '~/Downloads', '/' },
      --log_level = 'debug',
      auto_restore_last_session = true,
      cwd_change_handling = true,
      post_restore_cmds = { -- require open nvim-tree on restore session
        barbar_tree_offset.open_nvim_tree_no_focus,
      },
      no_restore_cmds = {
        barbar_tree_offset.open_nvim_tree_no_focus,
      },
      post_cwd_changed_cmds = {
        barbar_tree_offset.open_nvim_tree_no_focus,
      },
      pre_cwd_changed_cmds = {
        function()
          require('nvim-tree.api').tree.close()
        end
      },
    },
  },
}

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

2 participants