Skip to content

Commit

Permalink
feat(label): added opts.label.format for formatting rendered labels.
Browse files Browse the repository at this point in the history
…Closes #84
  • Loading branch information
folke committed Jun 28, 2023
1 parent 11d323d commit 2d3e7b9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,17 @@ Install the plugin with your preferred package manager:
-- number between 1 and 9
shade = 5,
},
-- With `format`, you can change how the label is rendered.
-- Should return a list of `[text, highlight]` tuples.
---@class Flash.Format
---@field state Flash.State
---@field match Flash.Match
---@field hl_group string
---@field after boolean
---@type fun(opts:Flash.Format): string[][]
format = function(opts)
return { { opts.match.label, opts.hl_group } }
end,
},
highlight = {
-- show a backdrop with hl FlashBackdrop
Expand Down
11 changes: 11 additions & 0 deletions lua/flash/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,17 @@ local defaults = {
-- number between 1 and 9
shade = 5,
},
-- With `format`, you can change how the label is rendered.
-- Should return a list of `[text, highlight]` tuples.
---@class Flash.Format
---@field state Flash.State
---@field match Flash.Match
---@field hl_group string
---@field after boolean
---@type fun(opts:Flash.Format): string[][]
format = function(opts)
return { { opts.match.label, opts.hl_group } }
end,
},
highlight = {
-- show a backdrop with hl FlashBackdrop
Expand Down
17 changes: 13 additions & 4 deletions lua/flash/highlight.lua
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ function M.update(state)
---@param match Flash.Match
---@param pos number[]
---@param offset number[]
local function label(match, pos, offset)
---@param is_after boolean
local function label(match, pos, offset, is_after)
local buf = vim.api.nvim_win_get_buf(match.win)
local row = pos[1] - 1 + offset[1]
local col = math.max(pos[2] + offset[2], 0)
Expand All @@ -132,7 +133,15 @@ function M.update(state)
-- else highlight the label
local key = buf .. ":" .. row .. ":" .. col
extmarks[key] = extmarks[key] or { buf = buf, row = row, col = col, text = {} }
table.insert(extmarks[key].text, 1, { match.label, hl_group })
local text = state.opts.label.format({
state = state,
match = match,
hl_group = hl_group,
after = is_after,
})
for i = #text, 1, -1 do
table.insert(extmarks[key].text, 1, text[i])
end
end
end

Expand All @@ -158,10 +167,10 @@ function M.update(state)

for _, match in ipairs(state.results) do
if match.label and after then
label(match, match.end_pos, after)
label(match, match.end_pos, after, true)
end
if match.label and before then
label(match, match.pos, before)
label(match, match.pos, before, false)
end
end

Expand Down

0 comments on commit 2d3e7b9

Please sign in to comment.