Skip to content

Conversation

@kanghyki
Copy link
Contributor

@kanghyki kanghyki commented Nov 12, 2025

Problem

The hlslens search handler was not working when hlslens was loaded before
nvim-scrollbar due to Lua's module caching mechanism.

Root Cause

  1. When hlslens loads first, it calls require('hlslens.config') which runs
    the init() function and caches the config with build_position_cb = nil
  2. Later when search.setup() calls hlslens.setup() with the callback,
    hlslens stores it in hlslens._config but the cached config module is never updated
  3. The init() function only runs once when the module is first required,
    so subsequent hlslens.setup() calls don't update the cached config

Reproduction

With this plugin loading order:

require('hlslens').setup()  -- Loads first, caches config with nil callback
require('scrollbar.handlers.search').setup()  -- Config already cached

Result: Search marks don't appear on scrollbar

Solution

Directly modify the cached hlslens.config object instead of calling hlslens.setup().
Since Lua tables are passed by reference, modifying the cached config object updates it everywhere.

Note: Alternatively, users can avoid this issue by removing require('hlslens').setup() from their configuration and letting nvim-scrollbar handle hlslens initialization.

Changes

  • Remove hlslens.setup() call
  • Directly access hlslens.config via require("hlslens.config")
  • Set build_position_cb directly on the config object

Benefits

  • Works regardless of plugin loading order
  • Preserves all existing hlslens configuration

Environment

  • OS: macOS (Darwin 25.0.0)
  • Neovim: NVIM v0.11.4
  • Plugin Manager: vim-plug

#83

The hlslens search handler was not working when hlslens was loaded before
nvim-scrollbar due to Lua's module caching mechanism.

1. When hlslens loads first, it calls `require('hlslens.config')` which runs
   the `init()` function and caches the config with `build_position_cb = nil`
2. Later when `search.setup()` calls `hlslens.setup()` with the callback,
   hlslens stores it in `hlslens._config` but the cached config module is never updated
3. The `init()` function only runs once when the module is first required,
   so subsequent `hlslens.setup()` calls don't update the cached config

With this plugin loading order:
```lua
require('hlslens').setup()  -- Loads first, caches config with nil callback
require('scrollbar.handlers.search').setup()  -- Too late, config already cached
```

Result: Search marks don't appear on scrollbar

Directly modify the cached `hlslens.config` object instead of calling
`hlslens.setup()`. Since Lua tables are passed by reference, modifying
the cached config object updates it everywhere.

- Remove `hlslens.setup()` call
- Directly access `hlslens.config` via `require("hlslens.config")`
- Set `build_position_cb` directly on the config object
- Apply user overrides directly to the config object

- Works regardless of plugin loading order
- No need to clear `package.loaded` cache
- Preserves all existing hlslens configuration
- Still supports user overrides via the `overrides` parameter

- OS: macOS (Darwin 25.0.0)
- Neovim: NVIM v0.11.4
- Plugin Manager: [vim-plug](https://github.com/junegunn/vim-plug)
@petertriho
Copy link
Owner

Thanks @kanghyki

This probably resolves a few issues others have seen with the hlslens integration

@petertriho petertriho merged commit f8e87b9 into petertriho:main Nov 17, 2025
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

Successfully merging this pull request may close these issues.

2 participants