echasnovski / mini.nvim

Library of 40+ independent Lua modules improving overall Neovim (version 0.8 and higher) experience with minimal effort
MIT License
4.45k stars 171 forks source link

`mini.diff` affects the `foldmethod` option of the window opened by `vim-flog`. #969

Closed gh-liu closed 2 weeks ago

gh-liu commented 2 weeks ago

Contributing guidelines

Module(s)

mini.diff

Description

I do not know if the mini.diff plugin causes this bug, or if it's the vim-flog plugin. When mini.diff is enabled, it affects the foldmethod option of the window opened by vim-flog.

Neovim version

v0.11.0-dev-236+g5def8714a

Steps to reproduce

  1. git clone https://github.com/echasnovski/mini.diff.git
  2. cd mini.diff
  3. nvim -nu minimal.lua
  4. run :Flogsplit
  5. press <CR> to select one commit, the right window is foldable, foldmethod of the window is syntax
  6. press <CR> to select other commit, the right window is foldable, foldmethod of the window is syntax
  7. press <CR> to select the commit in step 5, the right window is not foldable, foldmethod of the window is manual
  8. disable mini.diff in minimal.lua, the right window in step 7 is foldable
-- minimal.lua
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not (vim.uv or vim.loop).fs_stat(lazypath) then
    vim.fn.system({
        "git",
        "clone",
        "--filter=blob:none",
        "https://github.com/folke/lazy.nvim.git",
        "--branch=stable", -- latest stable release
        lazypath,
    })

    local name = [[lazy]]
    if vim.v.shell_error == 0 then
        vim.cmd([[redraw]])
        print(name .. ": finished installing")
    else
        print(name .. ": fail to install")
    end
end
vim.opt.rtp:prepend(lazypath)

require("lazy").setup({
    {
        "tpope/vim-fugitive",
        config = function()
            local autocmd = vim.api.nvim_create_autocmd
            autocmd("FileType", {
                pattern = "git",
                callback = function(ev)
                    vim.wo.foldmethod = "syntax"
                end,
            })
        end,
    },
    {
        "rbong/vim-flog",
    },
    {
        "echasnovski/mini.diff",
        -- enabled = false,
        opts = {},
    },
})

Expected behavior

The right window in step 7 is foldable.

Actual behavior

step 5:

1

step 6:

2

step 7:

3
echasnovski commented 2 weeks ago

Thanks for the issue!

'mini.diff' was indeed a source of the issue, but it surfaced mostly due to (what seems to me like) an unreasonable behavior from some other plugin. However, this particular case should be fixed on latest main.


The culprit was that 'mini.diff' tried to auto enable buffer from step 5 during step 6. This happened because that target buffer was not completely removed but hidden and unloaded (which made it perfectly valid for auto enabling). During enable(), there is a conditional step for loading buffer (as potential source needs buffer lines to be up to date). That loading in turn also updated window-local foldmethod option taking it from the current one (which is 'manual' from the "main" log window). At step 7, that exact buffer is shown in the split and has different 'foldmethod' value.

I partially consider this behavior more due to the plugins which manage those buffers. Not only they trigger a BufEnter event for unlisted and unloaded buffer, but they still keep that buffer around when completely removing it is a cleaner approach.

However, not auto enabling unlisted buffers looks (at the moment) like a better approach overall. And this fixes this particular issue.