Skip to content

Commit 9b88ff1

Browse files
authored
fix: make pre_highlight hook modifications work for hl_color and hlgroup (#294)
1 parent 52089c9 commit 9b88ff1

File tree

5 files changed

+73
-8
lines changed

5 files changed

+73
-8
lines changed

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -774,14 +774,12 @@ api.register_hook("pre_highlight", function(data)
774774

775775
-- Modify the highlight color
776776
if data.operation == "undo" then
777-
data.hl_color = { bg = "#FF0000" }
777+
data.hl_color = { bg = "#FF0000" } -- Override the background color
778+
-- data.hlgroup = "TermCursor" -- Use other group
779+
-- Or set the highlight group directly:
780+
-- vim.api.nvim_set_hl(0, "UgUndo", { bg = "#FF0000" })
778781
end
779782
end, 100) -- priority (higher = runs first)
780-
781-
-- Run after highlighting completes
782-
api.register_hook("post_highlight", function(data)
783-
print("Highlighted:", data.operation)
784-
end)
785783
```
786784

787785
**Available Hooks:**
@@ -792,6 +790,12 @@ end)
792790
- `on_error` - Error handling
793791
- `pre_highlight_setup` / `post_highlight_setup` - Highlight group creation
794792

793+
**Hook Data Modifications:**
794+
795+
- `data.hl_color` - Override the highlight color (takes precedence over config)
796+
- `data.hlgroup` - Change the highlight group used
797+
- Other fields like `data.operation` are read-only
798+
795799
### Event System
796800

797801
Subscribe to plugin events:

lua/undo-glow/commands.lua

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ function M.undo(opts)
3131
local api = require("undo-glow.api")
3232
api.emit("command_executed", { command = "undo", opts = opts })
3333

34+
-- Call pre_highlight hook to allow modifications
35+
api.call_hook("pre_highlight", opts)
36+
3437
-- Always trigger hooks by calling highlight_region_enhanced
3538
-- This ensures all hooks fire even if there are no text changes to highlight
3639
require("undo-glow.api").highlight_region_enhanced(
@@ -69,6 +72,9 @@ function M.redo(opts)
6972
local api = require("undo-glow.api")
7073
api.emit("command_executed", { command = "redo", opts = opts })
7174

75+
-- Call pre_highlight hook to allow modifications
76+
api.call_hook("pre_highlight", opts)
77+
7278
-- Always trigger hooks by calling highlight_region_enhanced
7379
-- This ensures all hooks fire even if there are no text changes to highlight
7480
require("undo-glow.api").highlight_region_enhanced(
@@ -106,6 +112,9 @@ function M.yank(opts)
106112
opts = require("undo-glow.utils").merge_command_opts("UgYank", opts)
107113
opts.operation = "yank"
108114

115+
-- Call pre_highlight hook to allow modifications
116+
api.call_hook("pre_highlight", opts)
117+
109118
-- Always trigger hooks by calling highlight_region directly
110119
require("undo-glow.api").highlight_region_enhanced(
111120
vim.tbl_extend("force", opts, {
@@ -149,6 +158,9 @@ function M.paste_below(opts)
149158
local api = require("undo-glow.api")
150159
api.emit("command_executed", { command = "paste_below", opts = opts })
151160

161+
-- Call pre_highlight hook to allow modifications
162+
api.call_hook("pre_highlight", opts)
163+
152164
-- Always trigger hooks by calling highlight_region directly
153165
require("undo-glow.api").highlight_region_enhanced(
154166
vim.tbl_extend("force", opts, {
@@ -186,6 +198,9 @@ function M.paste_above(opts)
186198
local api = require("undo-glow.api")
187199
api.emit("command_executed", { command = "paste_above", opts = opts })
188200

201+
-- Call pre_highlight hook to allow modifications
202+
api.call_hook("pre_highlight", opts)
203+
189204
-- Always trigger hooks by calling highlight_region directly
190205
require("undo-glow.api").highlight_region_enhanced(
191206
vim.tbl_extend("force", opts, {
@@ -229,6 +244,10 @@ function M.search_cmd(opts)
229244
opts = require("undo-glow.utils").merge_command_opts("UgSearch", opts)
230245
opts.operation = "search_cmd"
231246

247+
local api = require("undo-glow.api")
248+
-- Call pre_highlight hook to allow modifications
249+
api.call_hook("pre_highlight", opts)
250+
232251
local region = require("undo-glow.utils").get_current_cursor_row()
233252

234253
require("undo-glow.api").highlight_region_enhanced(
@@ -270,6 +289,9 @@ function M.search_next(opts)
270289
local api = require("undo-glow.api")
271290
api.emit("command_executed", { command = "search_next", opts = opts })
272291

292+
-- Call pre_highlight hook to allow modifications
293+
api.call_hook("pre_highlight", opts)
294+
273295
require("undo-glow.api").highlight_region_enhanced(
274296
vim.tbl_extend("force", opts, {
275297
s_row = region.s_row,
@@ -303,6 +325,9 @@ function M.search_prev(opts)
303325
local api = require("undo-glow.api")
304326
api.emit("command_executed", { command = "search_prev", opts = opts })
305327

328+
-- Call pre_highlight hook to allow modifications
329+
api.call_hook("pre_highlight", opts)
330+
306331
require("undo-glow.api").highlight_region_enhanced(
307332
vim.tbl_extend("force", opts, {
308333
s_row = region.s_row,
@@ -336,6 +361,9 @@ function M.search_star(opts)
336361
local api = require("undo-glow.api")
337362
api.emit("command_executed", { command = "search_star", opts = opts })
338363

364+
-- Call pre_highlight hook to allow modifications
365+
api.call_hook("pre_highlight", opts)
366+
339367
require("undo-glow.api").highlight_region_enhanced(
340368
vim.tbl_extend("force", opts, {
341369
s_row = region.s_row,
@@ -369,6 +397,9 @@ function M.search_hash(opts)
369397
local api = require("undo-glow.api")
370398
api.emit("command_executed", { command = "search_hash", opts = opts })
371399

400+
-- Call pre_highlight hook to allow modifications
401+
api.call_hook("pre_highlight", opts)
402+
372403
require("undo-glow.api").highlight_region_enhanced(
373404
vim.tbl_extend("force", opts, {
374405
s_row = region.s_row,
@@ -449,6 +480,10 @@ function M.cursor_moved(opts, cursor_moved_opts)
449480
opts = require("undo-glow.utils").merge_command_opts("UgCursor", opts)
450481
opts.operation = "cursor_moved"
451482

483+
local api = require("undo-glow.api")
484+
-- Call pre_highlight hook to allow modifications
485+
api.call_hook("pre_highlight", opts)
486+
452487
cursor_moved_opts = vim.tbl_deep_extend("force", {
453488
ignored_ft = {},
454489
steps_to_trigger = 10,

lua/undo-glow/types.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ local M = {}
126126
---State for the undo-glow highlight.
127127
---@class UndoGlow.State
128128
---@field current_hlgroup string The current highlight group in use.
129+
---@field hl_color? UndoGlow.HlColor Override color for the highlight.
129130
---@field should_detach boolean Whether the highlight should detach.
130131
---@field animation? UndoGlow.Config.Animation Animation configuration.
131132
---@field force_edge? boolean Whether to force edge highlighting.
@@ -146,6 +147,7 @@ local M = {}
146147
---Command options for triggering highlights.
147148
---@class UndoGlow.CommandOpts
148149
---@field hlgroup? string Optional highlight group to use.
150+
---@field hl_color? UndoGlow.HlColor Override color for the highlight.
149151
---@field animation? UndoGlow.Config.Animation Optional animation configuration.
150152
---@field force_edge? boolean Optional flag to force edge highlighting.
151153
---@field operation? string Operation type.

lua/undo-glow/utils.lua

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,13 @@ function M.handle_highlight(opts)
121121
opts.state.current_hlgroup
122122
)
123123

124-
local init_color =
125-
require("undo-glow.color").init_colors(current_hlgroup_detail)
124+
local init_color
125+
if opts.state.hl_color then
126+
init_color = vim.deepcopy(opts.state.hl_color)
127+
else
128+
init_color =
129+
require("undo-glow.color").init_colors(current_hlgroup_detail)
130+
end
126131

127132
opts.s_row, opts.s_col, opts.e_row, opts.e_col = M.sanitize_coords(
128133
opts.bufnr,
@@ -136,6 +141,11 @@ function M.handle_highlight(opts)
136141

137142
opts.ns = M.create_namespace(opts.bufnr, opts.state.animation.window_scoped)
138143

144+
-- If hl_color is set, update the highlight group with the custom color
145+
if opts.state.hl_color then
146+
vim.api.nvim_set_hl(0, unique_hlgroup, opts.state.hl_color)
147+
end
148+
139149
--- If disabled animation, set extmark and clear it afterwards
140150
if opts.state.animation.enabled ~= true then
141151
local extmark_opts = M.create_extmark_opts({
@@ -475,6 +485,7 @@ function M.create_state(opts)
475485
return {
476486
should_detach = false,
477487
current_hlgroup = opts.hlgroup or "UgUndo",
488+
hl_color = opts.hl_color,
478489
force_edge = type(opts.force_edge) == "nil" and false
479490
or opts.force_edge,
480491
operation = opts.operation,

tests/enhanced_api_spec.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,9 +476,22 @@ describe("Enhanced API", function()
476476

477477
assert.is_true(color_changed)
478478

479+
-- Verify that hl_color modification is preserved in the hook data
480+
local hook_called_with_color = false
481+
local test_hook_id = api.register_hook("pre_highlight", function(data)
482+
if data.operation == "undo" and data.hl_color and data.hl_color.bg == "#2D1B69" then
483+
hook_called_with_color = true
484+
end
485+
end, 100)
486+
487+
require("undo-glow").undo()
488+
489+
assert.is_true(hook_called_with_color)
490+
479491
-- Cleanup
480492
os.date = original_os_date
481493
api.remove_hook("pre_highlight", hook_id)
494+
api.remove_hook("pre_highlight", test_hook_id)
482495
end)
483496

484497
it("should react to configuration changes", function()

0 commit comments

Comments
 (0)