-
Notifications
You must be signed in to change notification settings - Fork 4
feat: add virtual text counter display #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
- Add new counter.lua module to display [X/Y] reference count - Show counter as virtual text at end of line when jumping - Counter clears automatically when highlights are cleared - Configurable via counter.enable and counter.hl_group options - Default styling: bold orange text for high visibility
mawkler
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your contribution!
I was also thinking that perhaps you should break out the logic for getting the current index and the total number of references to its own function. That way one could use it if they for example wanted to instead display that information in the statusline instead of using virtual text. That function would return the two values current_reference_index, total_reference_count (or with similar names). I notice however when looking through my code that we're passing around state a lot, including the references and current reference. So perhaps the best way would be to create a Lua "object" for storing that state. What do you think?
lua/refjump/counter.lua
Outdated
| local counter_hl_name = 'RefjumpCounter' | ||
|
|
||
| ---Create fallback highlight group if it doesn't exist | ||
| function M.create_fallback_hl_group(fallback_hl) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer if we always use the RefjumpCounter highlight group, and just link it to WarningMsg if it isn't set (that highlight group should always exist). You can thereby remove the counter.hl_group option.
| ---Find the index of a reference in the references list | ||
| ---@param reference RefjumpReference | ||
| ---@param references RefjumpReference[] | ||
| ---@return integer|nil | ||
| local function find_reference_index(reference, references) | ||
| for i, ref in ipairs(references) do | ||
| if ref.range.start.line == reference.range.start.line | ||
| and ref.range.start.character == reference.range.start.character then | ||
| return i | ||
| end | ||
| end | ||
| return nil | ||
| end | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should be able to refactor this to use vim.iter instead for a less imperative implementation, something like this (I haven't actually tried running it though):
local function find_reference_index(reference, references)
local idx, _ = vim.iter(references):enumerate():find(function(i, ref)
return ref.range.start.line == reference.range.start.line
and ref.range.start.character == reference.range.start.character
end)
return idx
end
lua/refjump/jump.lua
Outdated
| -- Display current index and total count | ||
| local current_index = find_reference_index(next_reference, references) | ||
| if current_index then | ||
| local total_count = #references | ||
| local bufnr = vim.api.nvim_get_current_buf() | ||
| require('refjump.counter').show(current_index, total_count, bufnr) | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer if we did the check for counter.enable here before doing all this logic, instead of in require('refjump.counter').show(). And maybe break out this whole block to its own function, that you can wrap in if not require('refjump').get_options().counter.enable.
- Add lua/refjump/state.lua for per-buffer state storage - Expose get_reference_info() for statusline integration - Remove counter.hl_group option, link RefjumpCounter to WarningMsg - Refactor find_reference_index() to use vim.iter - Move counter.enable check to jump.lua with show_counter() helper - Add test infrastructure with plenary.nvim (12 tests) - Update CI workflow to run tests on push/PR
The state was being cleared immediately on CursorMoved after a jump, preventing lualine/statusline from reading reference info. - Restore highlight_references toggle flag in highlight.lua - Add highlight.is_active() to expose the flag - Update state.is_active() to delegate to highlight.is_active() - Fix disable() to use toggle pattern: first call flips flag, second clears - Update tests to reflect new is_active() behavior
- Document counter.enable option in configuration - Document RefjumpCounter highlight group - Add statusline integration section with lualine example
|
Hey @mawkler, thanks for the feedback! I've addressed all your comments:
|
Summary
[3/12])counteroptionsChanges
lua/refjump/counter.lua- handles virtual text displaylua/refjump/jump.lua- calls counter instead of vim.notifylua/refjump/highlight.lua- clears counter with highlightslua/refjump/init.lua- adds counter configuration optionsConfiguration
Demo
When jumping between references with
]r/[r, the counter appears at the end of the line:[1/5],[2/5], etc.Replaces the previous vim.notify() notification with inline display inspired by nvim-hlslens.