auto-session icon indicating copy to clipboard operation
auto-session copied to clipboard

[BUG] restore from path argument incompatible with oil.nvim

Open niderhoff opened this issue 1 year ago • 4 comments

Describe the bug Trying to restore a session from a path argument like this:

nvim /my_folder/

will fail, because it is compatible, with oil.nvim, because the latter is rewriting the argument from /my_folder/ to oil:///my_folder.

To Reproduce Steps to reproduce the behavior:

  1. save a session in /my_folder
  2. Install oil.nvim
  3. run nvim /my_folder/
  4. session is not restored, instead oil file explorer is started

Expected behavior Session is restored.

Screenshots Before oil.nvim:

image

after:

image

Checkhealth image

Baseline (please complete the following information):

  • sessionoptions=blank,buffers,curdir,folds,help,tabpages,winsize,terminal
  • Darwin T000fad6c4 23.5.0 Darwin Kernel Version 23.5.0: Wed May 1 20:12:58 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T6000 arm64
  • NVIM v0.10.1

Additional context

niderhoff avatar Sep 02 '24 13:09 niderhoff

I use oil as well and I think the trick is to make sure it's lazy loaded until after the session is restored. What does your oil config look like?

For mine, i set keys:

return {
  'stevearc/oil.nvim',
  keys = {
    {
      '<Bslash><Bslash>',
      function() require('oil').toggle_float() end,
      desc = 'Oil popup',
    },
  },
...

cameronr avatar Sep 02 '24 15:09 cameronr

@cameronr

return {
    "stevearc/oil.nvim",
    opts = {},
    dependencies = { "nvim-tree/nvim-web-devicons" },
    config = function()
        require("oil").setup({
            default_file_explorer = true,
            delete_to_trash = true,
            skip_confirm_for_simple_edits = false,
            view_options = {
                show_hidden = false,
                natural_order = true,
                is_always_hidden = function(name, _)
                    return name == '..' or name == '.git'
                end,
            },
            win_options = {
                wrap = true,
            }
        })
    end,
}

its probably due to default_file_explorer ?

niderhoff avatar Sep 02 '24 16:09 niderhoff

Shouldn't depend on default_file_explorer (i have that in mine as well). What key do you use to open oil / how do you open it?

Or you can try my config:

return {
  'stevearc/oil.nvim',

  keys = {
    {
      '<Bslash><Bslash>',
      function() require('oil').toggle_float() end,
      desc = 'Oil',
    },
  },
  opts = {
    default_file_explorer = true,
    delete_to_trash = true,
    skip_confirm_for_simple_edits = false,
    view_options = {
      show_hidden = true,
      natural_order = true,
      is_always_hidden = function(name, _) return name == '..' or name == '.git' end,
    },
    float = {
      padding = 2,
      max_width = 90,
      max_height = 0,
    },
    win_options = {
      wrap = true,
      winblend = 0,
    },
    keymaps = {
      ['<C-c>'] = false,
      ['q'] = 'actions.close',
      ['<Esc>'] = 'actions.close',
    },
  },
}

With the keys block, oil isn't loaded until I press \\ so it doesn't interfere when launching nvim with a directory argument.

cameronr avatar Sep 02 '24 16:09 cameronr

@niderhoff are you still running into problems or did this resolve it for you?

cameronr avatar Sep 07 '24 20:09 cameronr

I tried again and now it behaves like this (without errors):

nvim -> restore session nvim . -> start oil nvim some_folder -> start oil

it is what I would expected it to behave, but maybe we should update readme to indicate that if something hooks into default_file_explorer (such as oil, or neotree) it might mean that behaviour 2:

When starting nvim . (or another directory), AutoSession will try to restore the session for that directory.

will instead open the file explorer.

niderhoff avatar Nov 12 '24 15:11 niderhoff

I'll take a look at the docs and see if I can clarify a bit.

If you want nvim . to be processed by auto-session instead of oil, you can lazy load oil:

return {
    "stevearc/oil.nvim",
     -- setting cmd/keys will cause Lazy.nvim to only load Oil when on that cmd or key press
     cmd = 'Oil',
     keys = {
          {
               '<Bslash><Bslash>', -- or whatever key you want to trigger oil
               function() require('oil').toggle_float() end, -- or however you want it to be displayed
               desc = 'Oil',
         },
      },
    -- opts = {}, -- setting opts doesn't do anything if you're using a custom config function so this line can be removed
    dependencies = { "nvim-tree/nvim-web-devicons" },
    config = function()
        require("oil").setup({
            default_file_explorer = true,
            delete_to_trash = true,
            skip_confirm_for_simple_edits = false,
            view_options = {
                show_hidden = false,
                natural_order = true,
                is_always_hidden = function(name, _)
                    return name == '..' or name == '.git'
                end,
            },
            win_options = {
                wrap = true,
            }
        })
    end,
}

If you want oil to load for a directory if there's no existing session, you could also add this to your auto-session config:

return {
  'rmagatti/auto-session',
  lazy = false,
  ...
  ---@module "auto-session"
  ---@type AutoSession.Config
  opts = {
    ...
    no_restore_cmds = {
      function()
        -- load oil in case we're launching with a dir arg and there's no session
        require('oil')
      end,
    },
  },
}

cameronr avatar Nov 12 '24 16:11 cameronr