stevearc / oil.nvim

Neovim file explorer: edit your filesystem like a buffer
MIT License
3.4k stars 92 forks source link

bug: 'A preview window already exists' when enabling auto-open auto-refresh preview #357

Closed yingzhu146 closed 2 months ago

yingzhu146 commented 2 months ago

Did you check the docs and existing issues?

Neovim version (nvim -v)

v0.10.0-dev-2943+gb0f922817 Build type: Release LuaJIT 2.1.1710088188 Run "nvim -V1 -v" for more info

Operating system/version

MacOS 12.4

Describe the bug

I have been having issues with the config proposed by https://github.com/stevearc/oil.nvim/issues/87

When pressing actions.parent twice, I get the following error

Error executing vim.schedule lua callback: .../code/pytorch/.repro/plugins/oil.nvim/lua/oil/init.lua:528: E590: A preview window already exists
stack traceback:
        [C]: in function 'nvim_set_option_value'
        .../code/pytorch/.repro/plugins/oil.nvim/lua/oil/init.lua:528: in function 'callback'
        ...torch/.repro/plugins/oil.nvim/lua/oil/adapters/files.lua:238: in function 'fn'
        vim/_editor.lua:351: in function <vim/_editor.lua:350>

What is the severity of this bug?

blocking (cannot use plugin)

Steps To Reproduce

  1. gh repo clone pytorch/torch
  2. cd torch
  3. nvim torch/cuda/amp/grad_scaler.py -u repro.lua
  4. <s-tab> (actions.parent)
  5. <s-tab> (actions.parent)

Expected Behavior

No error

Directory structure

(see torch repo example - file is arbitrary just needed to have sufficient nested structure to repro in public repo)

Repro

local root = vim.fn.fnamemodify("./.repro", ":p")
for _, name in ipairs({ "config", "data", "state", "runtime", "cache" }) do
    vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end
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)
local plugins = {
    {
        "stevearc/oil.nvim",
        cmd = "Oil",
        keys = {
            {
                "<s-tab>",
                function()
                    require("oil").open()
                end,
            },
        },
        config = function()
            vim.api.nvim_create_autocmd("User", {
                pattern = "OilEnter",
                callback = vim.schedule_wrap(function(args)
                    local oil = require("oil")
                    if vim.api.nvim_get_current_buf() == args.data.buf and oil.get_cursor_entry() then
                        oil.open_preview()
                    end
                end),
            })
            require("oil").setup({
                keymaps = {
                    ["<tab>"] = "actions.select",
                },
                preview = {
                    update_on_cursor_moved = true,
                },
            })
        end,
    },
}
require("lazy").setup(plugins, {
    root = root .. "/plugins",
})

Did you check the bug with a clean config?

yingzhu146 commented 2 months ago
{
  "stevearc/oil.nvim",
  cmd = "Oil",
  keys = {
    {
      "<s-tab>",
      function()
        local util = require("oil.util")
        local oil = require("oil")
        local winid = util.get_preview_win()
        if winid then
          vim.api.nvim_win_close(winid, true)
        end
        oil.open()
      end,
    },
  },
  config = function()
    local util = require("oil.util")
    local oil = require("oil")
    vim.api.nvim_create_autocmd("User", {
      pattern = "OilEnter",
      callback = vim.schedule_wrap(function(args)
        if vim.api.nvim_get_current_buf() == args.data.buf and oil.get_cursor_entry() then
          local winid = util.get_preview_win()
          if winid then
            vim.api.nvim_win_close(winid, true)
            return
          end
          oil.open_preview()
        end
      end),
    })
    oil.setup({
      keymaps = {
        ["<tab>"] = "actions.select",
      },
    })
  end,
}

like this works.

kolja commented 2 weeks ago

It is helpful to know about oil.util and util.get_preview_win() (which I didn't find documented anywhere). ~~However this config still doesn't fix the preview window already exists error for me: Just opening oil with a preview window works just fine. Problems start when I navigate to parent directories.~~ ... updating to the latest 'stable' fixed it.