RRethy / vim-illuminate

illuminate.vim - (Neo)Vim plugin for automatically highlighting other uses of the word under the cursor using either LSP, Tree-sitter, or regex matching.
2.12k stars 44 forks source link

`<a-n>` and `<a-p>` do not working in macros that change the symbol under the cursor #122

Closed PowerUser64 closed 1 year ago

PowerUser64 commented 1 year ago

Desciption When using <a-n> or <a-p> in macros, the cursor doesn't move to the previous/next result. Doing :lua require('illuminate').goto_next_reference() inside the macro produces the same result.

To Reproduce Minimal init.lua:

-- Bootstrap packer if needed
local fn = vim.fn
local install_path = fn.stdpath('data')..'/site/pack/packer/start/packer.nvim'
if fn.empty(fn.glob(install_path)) > 0 then
  packer_bootstrap = fn.system({'git', 'clone', '--depth', '1', 'https://github.com/wbthomason/packer.nvim', install_path})
  vim.cmd [[packadd packer.nvim]]
end

local use = require('packer').use

-- Load plugins with packer
return require('packer').startup({function()
   use ({ 'wbthomason/packer.nvim' })
   use ({
      'RRethy/vim-illuminate',
      config = function()
         require('illuminate').configure({
            providers = {
               'regex',
            }
         })
      end
   })

   -- Bootstrap packer if needed
   if (packer_bootstrap) then
      require('packer').sync()
   end
end
})
  1. Open a file, like lua/illuminate/goto.lua from this repository
  2. Place the cursor on the vim word after a symbol
  3. Record a macro like this:
    • This goes to the previous word, the next reference, and then goes to the next word:
      • press b
      • use illuminate to go to the next reference to the symbol under the cursor (<a-n> or :lua require('illuminate').goto_next_reference())
      • press w
  4. Run the macro on the word after a symbol illuminate can find
  5. Notice how the cursor stays in the same place it started from when the macro is done

Expected behavior At the end of the macro presented here, the cursor should be on the word after the next reference to the symbol used in the macro; as seen when performing the steps above when not recording a macro.

Screenshots Here's a short screen recording of the demonstration I describe above, using the minimal init.lua:

It's a little slow, but you get the picture.

Additional context Creating and running a macro that just does <a-n> works fine, but it seems macros that move the cursor off the starting symbol break it. I only talk about <a-n> here, but this applies to <a-p> as well.

RRethy commented 1 year ago

This is because the macro is moving faster than the delay, so when you move off the reference, the references will be refreshed but they won't be ready for until the duration of delay has passed (minimum delay of 17ms under the hood).

You can get around this by freezing illuminate with require('illuminate').freeze_buf() at the start of the macro and require('illuminate').unfreeze_buf() at the end of the macro. These could be keymapped to something like vim.keymap.set('n', '<a-i>', require('illuminate').toggle_freeze_buf) which you execute at the start and end of the macro where you want to use illuminate.