-
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
Open
subev
wants to merge
4
commits into
mawkler:main
Choose a base branch
from
subev:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
54df939
feat: add virtual text counter display at end of line
subev 9a2c688
refactor: add state module and address PR review feedback
subev a83a906
fix: restore highlight_references flag to fix state persistence
subev 58e1c96
docs: add counter option and statusline integration example
subev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| local M = {} | ||
|
|
||
| ---Namespace for counter virtual text extmarks | ||
| local counter_namespace = vim.api.nvim_create_namespace('RefjumpCounter') | ||
|
|
||
| ---Name of the highlight group for the counter | ||
| local counter_hl_name = 'RefjumpCounter' | ||
|
|
||
| ---Create fallback highlight group if it doesn't exist | ||
| function M.create_fallback_hl_group(fallback_hl) | ||
| local hl = vim.api.nvim_get_hl(0, { name = counter_hl_name }) | ||
|
|
||
| if vim.tbl_isempty(hl) then | ||
| -- Create a distinctive highlight: yellow/orange text on dark background | ||
| -- with bold for extra visibility | ||
| vim.api.nvim_set_hl(0, counter_hl_name, { | ||
| fg = '#FFA500', -- Orange/yellow color | ||
| bold = true, | ||
| default = true, | ||
| }) | ||
| end | ||
| end | ||
|
|
||
| ---Show virtual text counter at the end of the current line | ||
| ---@param current_index integer Current reference index (1-based) | ||
| ---@param total_count integer Total number of references | ||
| ---@param bufnr integer Buffer number | ||
| function M.show(current_index, total_count, bufnr) | ||
| if not require('refjump').get_options().counter.enable then | ||
| return | ||
| end | ||
|
|
||
| -- Get current cursor position | ||
| local cursor = vim.api.nvim_win_get_cursor(0) | ||
| local line = cursor[1] - 1 -- Convert to 0-indexed | ||
|
|
||
| -- Clear any existing counter in this buffer | ||
| M.clear(bufnr) | ||
|
|
||
| -- Format the counter text | ||
| local text = string.format(' [%d/%d]', current_index, total_count) | ||
|
|
||
| -- Add virtual text at end of line | ||
| vim.api.nvim_buf_set_extmark(bufnr, counter_namespace, line, 0, { | ||
| virt_text = { { text, counter_hl_name } }, | ||
| virt_text_pos = 'eol', | ||
| priority = 100, | ||
| }) | ||
| end | ||
|
|
||
| ---Clear counter virtual text from buffer | ||
| ---@param bufnr integer Buffer number (0 for current buffer) | ||
| function M.clear(bufnr) | ||
| bufnr = bufnr or 0 | ||
| vim.api.nvim_buf_clear_namespace(bufnr, counter_namespace, 0, -1) | ||
| end | ||
|
|
||
| return M | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -63,6 +63,20 @@ local function jump_to(next_reference) | |
| vim.cmd('normal! zv') | ||
| end | ||
|
|
||
| ---Find the index of a reference in the references list | ||
| ---@param reference RefjumpReference | ||
| ---@param references RefjumpReference[] | ||
| ---@return integer|nil | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a very minor nit, but everywhere where you have |
||
| 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 | ||
|
|
||
|
mawkler marked this conversation as resolved.
|
||
| ---@param next_reference RefjumpReference | ||
| ---@param forward boolean | ||
| ---@param references RefjumpReference[] | ||
|
|
@@ -74,6 +88,14 @@ local function jump_to_next_reference(next_reference, forward, references) | |
|
|
||
| if next_reference then | ||
| jump_to(next_reference) | ||
|
|
||
| -- 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 | ||
|
mawkler marked this conversation as resolved.
Outdated
|
||
| else | ||
| vim.notify('refjump.nvim: Could not find the next reference', vim.log.levels.WARN) | ||
| end | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.