nvim-mini / nvim-mini/mini.nvim

[mini.cmdline] keymaps that trigger command mode and involve the expression register desync the internal state, breaking autocompletion and peek

Open
#2,553 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug mini.cmdline
Dominant language
Lua
Stars
9.5k
Forks
310
Avg merge
4h 16m
Merged PRs (30d)
1

Description

Contributing guidelines
Module(s)

mini.cmdline

Neovim version

0.12.x

Description

Normal mode keymaps that both

  1. Start command mode without issuing a command
  2. Make use of the expression register

break the internal state of the plugin (H.cache.n_nested) which in turns break autocompletion and peek.

For example,

vim.keymap.set("n", "<F4>", ":<c-u> <c-r>=1<cr><home>")

triggers the bug while

vim.keymap.set("c", "<F4>", " <c-r>=1<cr><home>")

doesn't.

Logging the following information

_G.aux = {}

-- This is at the top of `H.on_cmdline_enter`
table.insert(aux, { 'enter', state = H.cache.state, n_nested = H.cache.n_nested })

-- This is at the top of `H.on_cmdline_leave`
table.insert(aux, { 'leave', state = H.cache.state, n_nested = H.cache.n_nested })

Using the previously mentioned keymap and closing the commandline results in the following

{
  { "leave" },
  { "enter" },
  { "leave" },
  { "enter" },
  {
    "enter",
    state = <1>{
      complpat = " 1",
      compltype = "command",
      line = " 1",
      pos = 1,
    },
  },
  { "leave", n_nested = 1, state = <table 1> },
  { "enter", state = <table 1> },
  { "leave", n_nested = 1, state = <table 1> },
}

Since the first leave event happens before the first enter event, n_nested never goes back to 0 and both autocompletion and peek stop working.

This may happen because on

https://github.com/nvim-mini/mini.nvim/blob/ac5dffcc52b8378fbb15efeb0497a83e57a0b01c/lua/mini/cmdline.lua#L580-L583

the CmdlineEnter callback is scheduled while the CmdlineLeave is not. There is more to it, though as the comment above the code points as this being intentional and the bug not happening outside of this niche case.

Reproduction
  1. Create separate 'nvim-repro' config directory:

    • '~/.config/nvim-repro/' on Unix
    • '~/AppData/Local/nvim-repro/' on Windows
  2. Inside 'nvim-repro' directory create a file named 'init.lua'.
    Populate it with the following content:

-- Clone latest 'mini.nvim' (requires Git CLI installed)
vim.cmd('echo "Installing `mini.nvim`" | redraw')
local mini_path = vim.fn.stdpath("data") .. "/site/pack/deps/start/mini.nvim"
local clone_cmd = { "git", "clone", "--depth=1", "https://github.com/nvim-mini/mini.nvim", mini_path }
vim.fn.system(clone_cmd)
vim.cmd('echo "`mini.nvim` is installed" | redraw')

-- Make sure 'mini.nvim' is available
vim.cmd("packadd mini.nvim")
require("mini.deps").setup()

-- Add extra setup steps needed to reproduce the behavior
-- Use `MiniDeps.add('user/repo')` to install another plugin from GitHub

require("mini.cmdline").setup()

vim.keymap.set("n", "<F4>", ":<c-u> <c-r>=1<cr><home>")
  1. Run NVIM_APPNAME=nvim-repro nvim (i.e. execute nvim with NVIM_APPNAME environment variable set to "nvim-repro").
    Wait for all dependencies to install.

  2. :a to check that autocompletion works as expected

  3. <F4> to trigger the problematic keymap and <esc> to exit command mode

  4. The peek window won't be closed, close it manually with :fclose

  5. :a to check that autocompletion is no longer working

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in lua/mini/cmdline.lua around the CmdlineEnter and CmdlineLeave handling at lines 580-583, then trace H.on_cmdline_enter, H.on_cmdline_leave, and H.cache.n_nested. Reproduce the issue with the F4 normal-mode keymap in the provided nvim-repro init.lua. Done means autocompletion and peek still work after exiting command mode triggered through the expression register.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
cli
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
64/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.