kevinhwang91 / nvim-hlslens

Hlsearch Lens for Neovim
BSD 3-Clause "New" or "Revised" License
748 stars 10 forks source link

Lens flicker on file write #63

Closed MunifTanjim closed 1 week ago

MunifTanjim commented 1 year ago

Describe the bug A clear and concise description of what the bug is.

To Reproduce using nvim -u mini.lua

Example: cat mini.lua

-- packer
use {'kevinhwang91/nvim-hlslens'}

require('hlslens').setup()

local kopts = {noremap = true, silent = true}

vim.api.nvim_set_keymap('n', 'n',
    [[<Cmd>execute('normal! ' . v:count1 . 'n')<CR><Cmd>lua require('hlslens').start()<CR>]],
    kopts)
vim.api.nvim_set_keymap('n', 'N',
    [[<Cmd>execute('normal! ' . v:count1 . 'N')<CR><Cmd>lua require('hlslens').start()<CR>]],
    kopts)
vim.api.nvim_set_keymap('n', '*', [[*<Cmd>lua require('hlslens').start()<CR>]], kopts)
vim.api.nvim_set_keymap('n', '#', [[#<Cmd>lua require('hlslens').start()<CR>]], kopts)
vim.api.nvim_set_keymap('n', 'g*', [[g*<Cmd>lua require('hlslens').start()<CR>]], kopts)
vim.api.nvim_set_keymap('n', 'g#', [[g#<Cmd>lua require('hlslens').start()<CR>]], kopts)

vim.api.nvim_set_keymap('x', '*', [[*<Cmd>lua require('hlslens').start()<CR>]], kopts)
vim.api.nvim_set_keymap('x', '#', [[#<Cmd>lua require('hlslens').start()<CR>]], kopts)
vim.api.nvim_set_keymap('x', 'g*', [[g*<Cmd>lua require('hlslens').start()<CR>]], kopts)
vim.api.nvim_set_keymap('x', 'g#', [[g#<Cmd>lua require('hlslens').start()<CR>]], kopts)

vim.api.nvim_set_keymap('n', '<Leader>l', ':noh<CR>', kopts)

Steps to reproduce the behavior:

  1. Search for something using * or /
  2. Clear highlight using :nohl
  3. Write the file using :w
  4. Keep repeating step 3, sometimes the highlights and lens will be visible for a short time

Expected behavior

Saving a file shouldn't cause flicker.

Screenshots If applicable, add screenshots to help explain your problem.

Kapture 2023-06-02 at 16 15 39

Additional context Add any other context about the problem here.

https://github.com/kevinhwang91/nvim-hlslens/blob/5e3e9088fba7508cee3612ee50b14dfdd3ab19af/lua/hlslens/cmdline/init.lua#L330-L334

The flicker is triggered from this code.

kevinhwang91 commented 1 year ago

Can't reproduce it, maybe some script change your hlsearch.

MunifTanjim commented 1 year ago

Okay, I reproduced it like this:

  vim.api.nvim_create_autocmd("BufWritePre", {
    callback = function()
      vim.wait(2000)
    end,
  })

Here vim.wait(2000) is artificially delaying the file save. But in my actual config it happens because of lsp code formatting that runs before the buffer is written.

Kapture 2023-06-02 at 22 28 37

kevinhwang91 commented 1 year ago

Sorry, still can't rerpoduce it.

mortymacs commented 4 months ago

I have the same problem. When I save a file, it shows the lens quickly on every line and then disappears. My config:

require("hlslens").setup()

Nvim version:

NVIM v0.10.0
Build type: Release
LuaJIT 2.1.1693350652
Rekwass commented 1 week ago

I am able to reproduce the issue with the following config: minimal.lua

vim.env.LAZY_STDPATH = '.repro'
load(vim.fn.system('curl -s https://raw.githubusercontent.com/folke/lazy.nvim/main/bootstrap.lua'))()

vim.api.nvim_create_autocmd("BufWritePre", {
    callback = function()
        vim.wait(10)
        vim.cmd("m +1")
        vim.cmd("m -2")
    end,
})

require('lazy.minit').repro {
    spec = {
        {
            'kevinhwang91/nvim-hlslens',
            config = function()
                require('hlslens').setup()
            end,
            lazy = false,
        },
    },
}

NVIM v0.10.1 Build type: Release LuaJIT 2.1.1725453128

In order to see the flickering follow these steps:

  1. Create a file and add a couple lines to highlight main.cpp

    int main() {
    int foo = 1;
    int bar = 2;
    
    int foobar = foo + bar;
    
    return foobar;
    }
  2. Open this file with the minimal.lua configuration with nvim --clean -u minimal.lua main.cpp
  3. Highlight a word. int for example
  4. Then disable highlight with :noh
  5. Save the file with :w. Repeat this step as it is really inconsistent

nvim-hlslens highlight flickering

I worked around the issue with this answer on stackoverflow.

kevinhwang91 commented 1 week ago

I am able to reproduce the issue with the following config: minimal.lua

vim.env.LAZY_STDPATH = '.repro'
load(vim.fn.system('curl -s https://raw.githubusercontent.com/folke/lazy.nvim/main/bootstrap.lua'))()

vim.api.nvim_create_autocmd("BufWritePre", {
    callback = function()
        vim.wait(10)
        vim.cmd("m +1")
        vim.cmd("m -2")
    end,
})

require('lazy.minit').repro {
    spec = {
        {
            'kevinhwang91/nvim-hlslens',
            config = function()
                require('hlslens').setup()
            end,
            lazy = false,
        },
    },
}

NVIM v0.10.1 Build type: Release LuaJIT 2.1.1725453128

In order to see the flickering follow these steps:

  1. Create a file and add a couple lines to highlight main.cpp
int main() {
  int foo = 1;
  int bar = 2;

  int foobar = foo + bar;

  return foobar;
}
  1. Open this file with the minimal.lua configuration with nvim --clean -u minimal.lua main.cpp
  2. Highlight a word. int for example
  3. Then disable highlight with :noh
  4. Save the file with :w. Repeat this step as it is really inconsistent

nvim-hlslens highlight flickering nvim-hlslens highlight flickering

I worked around the issue with this answer on stackoverflow.

Fixed.