Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions lua/refjump/counter.lua
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)
Comment thread
mawkler marked this conversation as resolved.
Outdated
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
1 change: 1 addition & 0 deletions lua/refjump/highlight.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ end
function M.disable()
if not highlight_references then
vim.api.nvim_buf_clear_namespace(0, highlight_namespace, 0, -1)
require('refjump.counter').clear(0)
else
highlight_references = false
end
Expand Down
13 changes: 13 additions & 0 deletions lua/refjump/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ local M = {}
---@field enable? boolean Highlight the LSP references on jump
---@field auto_clear boolean Automatically clear highlights when cursor moves

---@class RefjumpCounterOptions
---@field enable? boolean Show virtual text counter at end of line
---@field hl_group? string Highlight group for counter text

---@class RefjumpIntegrationOptions
---@field demicolon? { enable?: boolean } Make `]r`/`[r` repeatable with `;`/`,` using demicolon.nvim

---@class RefjumpOptions
---@field keymaps? RefjumpKeymapOptions
---@field highlights? RefjumpHighlightOptions
---@field counter? RefjumpCounterOptions
---@field integrations? RefjumpIntegrationOptions
---@field verbose? boolean Print message if no reference is found
local options = {
Expand All @@ -27,6 +32,10 @@ local options = {
enable = true,
auto_clear = true,
},
counter = {
enable = true,
hl_group = 'WarningMsg',
},
integrations = {
demicolon = {
enable = true,
Expand Down Expand Up @@ -55,6 +64,10 @@ function M.setup(opts)
require('refjump.highlight').auto_clear_reference_highlights()
end
end

if options.counter.enable then
require('refjump.counter').create_fallback_hl_group(options.counter.hl_group)
end
end

M.reference_jump = require('refjump.jump').reference_jump
Expand Down
22 changes: 22 additions & 0 deletions lua/refjump/jump.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a very minor nit, but everywhere where you have type | nil you can replace with type? which is slightly cleaner in my opinion

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

Comment thread
mawkler marked this conversation as resolved.
---@param next_reference RefjumpReference
---@param forward boolean
---@param references RefjumpReference[]
Expand All @@ -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
Comment thread
mawkler marked this conversation as resolved.
Outdated
else
vim.notify('refjump.nvim: Could not find the next reference', vim.log.levels.WARN)
end
Expand Down