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

Sometimes the statuscolumn disappears #559

Open
abeldekat opened this issue Apr 6, 2024 · 0 comments
Open

Sometimes the statuscolumn disappears #559

abeldekat opened this issue Apr 6, 2024 · 0 comments

Comments

@abeldekat
Copy link

abeldekat commented Apr 6, 2024

statuscolumn_removed.mp4

Using harpoon

grapple_replay.mp4

Replay using grapple.nvim

The problem

Seemingly at random moments, when selecting a harpooned file, the complete statuscolumn disappears.

Reproduce

Required:

  • treesitter and treesitter-context
  • any starter plugin(mini.starter, dashboard-nvim)
  • an autocommand for going to the last location when opening a buffer
  • a lua file with functions that trigger treesitter-context

One could argue if the erroneous behavior is related to Harpoon,or to the other plugins needed to reproduce. The behavior does not occur when using grapple.nvim,or a plain global mark, in the exact same scenario.

Init.lua
local function clone(path_to_site)
  local mini_path = path_to_site .. "pack/deps/start/mini.deps"
  if not vim.loop.fs_stat(mini_path) then
    vim.cmd('echo "Installing `mini.deps`" | redraw')
    local clone_cmd =
      { "git", "clone", "--filter=blob:none", "https://github.com/echasnovski/mini.deps", mini_path }
    vim.fn.system(clone_cmd)
    vim.cmd("packadd mini.deps | helptags ALL")
    vim.cmd('echo "Installed `mini.deps`" | redraw')
  end
end

local path_to_site = vim.fn.stdpath("data") .. "/site/"
clone(path_to_site)
local MiniDeps = require("mini.deps")
MiniDeps.setup({ path = { package = path_to_site } })
local add, now = MiniDeps.add, MiniDeps.now

vim.g.mapleader = " "
vim.opt.number = true -- show the statuscolumn
local function augroup(name)
  return vim.api.nvim_create_augroup("ak_" .. name, { clear = true })
end
vim.api.nvim_create_autocmd("BufReadPost", {
  group = augroup("last_loc"), -- go to last location when opening a buffer
  callback = function(event)
    local buf = event.buf
    if vim.b[buf].ak_last_loc then
      return
    end
    vim.b[buf].ak_last_loc = true

    local mark = vim.api.nvim_buf_get_mark(buf, '"')
    local lcount = vim.api.nvim_buf_line_count(buf)
    if mark[1] > 0 and mark[1] <= lcount then
      pcall(vim.api.nvim_win_set_cursor, 0, mark) -- the source of the bug!
    end
  end,
})

now(function()
  add("folke/tokyonight.nvim")
  vim.cmd.colorscheme("tokyonight")

  add("nvim-treesitter/nvim-treesitter")
  ---@diagnostic disable-next-line: missing-fields
  require("nvim-treesitter.configs").setup({})
  add("nvim-treesitter/nvim-treesitter-context")
  require("treesitter-context").setup({ mode = "cursor", max_lines = 3 })

  add({ source = "ThePrimeagen/harpoon", checkout = "harpoon2", depends = { "nvim-lua/plenary.nvim" } })
  local Harpoon = require("harpoon")
  Harpoon:setup({ settings = { save_on_toggle = true } })

  add("echasnovski/mini.starter") -- or nvimdev/dashboard-nvim
  require("mini.starter").setup()

  vim.keymap.set("n", "<leader>a", function()
    Harpoon:list():add()
  end, { desc = "Harpoon add", silent = true })
  vim.keymap.set("n", "<leader>j", function()
    Harpoon.ui:toggle_quick_menu(Harpoon:list())
  end, { desc = "Harpoon ui", silent = true })
  vim.keymap.set("n", "<c-j>", function()
    Harpoon:list():select(1)
  end, { desc = "Harpoon select 1", silent = true })
end)
Repro setup

Create repro

mkdir ~/.config/repro
cd ~/.config/repro

# add the contents of the provided init.lua
touch init.lua

NVIM_APPNAME=repro nvim init.lua 

Remove repro

rm -rf ~/.local/share/repro ~/.local/state/repro ~/.local/cache/repro
rm -rf ~/.config/repro
Replay using grapple.nvim
local function clone(path_to_site)
  local mini_path = path_to_site .. "pack/deps/start/mini.deps"
  if not vim.loop.fs_stat(mini_path) then
    vim.cmd('echo "Installing `mini.deps`" | redraw')
    local clone_cmd =
      { "git", "clone", "--filter=blob:none", "https://github.com/echasnovski/mini.deps", mini_path }
    vim.fn.system(clone_cmd)
    vim.cmd("packadd mini.deps | helptags ALL")
    vim.cmd('echo "Installed `mini.deps`" | redraw')
  end
end

local path_to_site = vim.fn.stdpath("data") .. "/site/"
clone(path_to_site)
local MiniDeps = require("mini.deps")
MiniDeps.setup({ path = { package = path_to_site } })
local add, now = MiniDeps.add, MiniDeps.now

vim.g.mapleader = " "
vim.opt.number = true
local function augroup(name)
  return vim.api.nvim_create_augroup("ak_" .. name, { clear = true })
end
vim.api.nvim_create_autocmd("BufReadPost", {
  group = augroup("last_loc"),
  callback = function(event)
    local buf = event.buf
    if vim.b[buf].ak_last_loc then
      return
    end
    vim.b[buf].ak_last_loc = true

    local mark = vim.api.nvim_buf_get_mark(buf, '"')
    local lcount = vim.api.nvim_buf_line_count(buf)
    if mark[1] > 0 and mark[1] <= lcount then
      pcall(vim.api.nvim_win_set_cursor, 0, mark)
    end
  end,
})

now(function()
  add("folke/tokyonight.nvim")
  vim.cmd.colorscheme("tokyonight")

  add("nvim-treesitter/nvim-treesitter")
  ---@diagnostic disable-next-line: missing-fields
  require("nvim-treesitter.configs").setup({})
  add("nvim-treesitter/nvim-treesitter-context")
  require("treesitter-context").setup({ mode = "cursor", max_lines = 3 })

  add("cbochs/grapple.nvim")
  local Grapple = require("grapple")
  Grapple.setup({ icons = false })

  add("echasnovski/mini.starter")
  require("mini.starter").setup()

  vim.keymap.set("n", "<leader>a", "<cmd>Grapple toggle<cr>", { desc = "Grappe add", silent = true })
  vim.keymap.set("n", "<leader>j", "<cmd>Grapple toggle_tags<cr>", { desc = "Grapple ui", silent = true })
  vim.keymap.set("n", "<c-j>", "<cmd>Grapple select index=1<cr>", { desc = "Grapple select 1", silent = true })
end)
alias repro="NVIM_APPNAME=repro nvim"
  1. Open init.lua: repro init.lua
  2. Place the cursor on line 52, requiring Harpoon
  3. Harpoon add
  4. Close Neovim
  5. Open Neovim without arguments: repro
  6. Harpoon select 1 (using the keymapping)

NOTE: The behavior does not occur when replacing step 6 with the following:

  1. Open the Harpoon ui
  2. Select init.lua and enter

The solution

Harpoon config.lua

-- More lua....
--
if not vim.api.nvim_buf_is_loaded(bufnr) then
    vim.fn.bufload(bufnr) --> REMOVE this line!
    vim.api.nvim_set_option_value("buflisted", true, {
        buf = bufnr,
    })
end
if options.vsplit then
    vim.cmd("vsplit")
elseif options.split then
    vim.cmd("split")
elseif options.tabedit then
    vim.cmd("tabedit")
end
vim.api.nvim_set_current_buf(bufnr)
--
-- More lua....

Line vim.fn.bufload(bufnr) can be removed, because the buffer is loaded
when calling vim.api.nvim_set_current_buf(bufnr)

I assume that calling vim.fn.bufload(bufnr) creates
a window-of-opportunity for the behavior seen in this issue.

See this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant