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

Only show highlights in the window containing the cursor #171

Closed MariaSolOs closed 11 months ago

MariaSolOs commented 11 months ago

Not sure if this is the intended behavior, so I'm not categorizing it as a bug.

When I have 2 split windows, I noticed that the highlights will remain despite my cursor not being on that word anymore (it is in another window).

Here's a picture of what I mean. My cursor is circled with pink, but notice the highlights on the left.

image
RRethy commented 11 months ago

The highlights are attached to the buffer so it's intended. If you do a split with 2 windows where each window has the same buffer, you'll see the highlights update in both windows.

Clearing the highlights when you move to another window would be relatively easy (just WinLeave autocmd) and could be put behind a config option. I'm open to PRs, not sure if I'll have the time to implement that.

MariaSolOs commented 11 months ago

@RRethy thank you for the reply! I ended adding a pair of autocommands and it works like a charm :) Not creating a PR since the exact pause-resume toggling might be tweaked quite a lot, and the workaround is very straightforward anyway.

Leaving here for future reference:

local illuminate = require 'illuminate'

-- Clear highlights when leaving a buffer.
local illuminate_group = vim.api.nvim_create_augroup('IlluminateBufUpdate', { clear = true })
vim.api.nvim_create_autocmd('BufEnter', {
  group = illuminate_group,
  callback = illuminate.resume_buf,
})
vim.api.nvim_create_autocmd('BufLeave', {
  group = illuminate_group,
  callback = illuminate.pause_buf,
})