Skip to content

Commit ecd7cb1

Browse files
committed
🐛 fix: highlighting not working properly
1 parent fa0aabd commit ecd7cb1

File tree

2 files changed

+73
-22
lines changed

2 files changed

+73
-22
lines changed

lua/notice-me/init.lua

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function M.setup(opts)
1515
-- Merge user config with defaults
1616
opts = opts or {}
1717
M.config = vim.tbl_deep_extend("force", M.config, opts)
18-
18+
1919
-- Initialize your plugin here
2020
M.setup_highlights()
2121
M.setup_autocmds()
@@ -28,7 +28,7 @@ end
2828
function M.setup_highlights()
2929
local ns_id = vim.api.nvim_create_namespace("notice_me")
3030
M.namespace = ns_id
31-
31+
3232
-- Create highlight groups
3333
for keyword, color in pairs(M.config.keywords) do
3434
vim.api.nvim_set_hl(0, "NoticeMe" .. keyword, color)
@@ -37,17 +37,38 @@ end
3737

3838
-- Set up autocommands to apply highlighting
3939
function M.setup_autocmds()
40-
vim.api.nvim_create_autocmd({"BufEnter", "BufWritePost"}, {
40+
-- Apply on buffer events
41+
vim.api.nvim_create_autocmd({ "BufEnter", "BufWritePost", "TextChanged", "TextChangedI" }, {
4142
callback = function(args)
4243
if not M.config.enable then return end
43-
M.highlight_keywords(args.buf)
44+
vim.schedule(function()
45+
M.highlight_keywords(args.buf)
46+
end)
4447
end,
45-
desc = "Notice-me highlight keywords on buffer enter/write",
48+
desc = "Notice-me highlight keywords on buffer events",
4649
})
47-
50+
51+
-- Apply on colorscheme change to ensure highlights are preserved
52+
vim.api.nvim_create_autocmd("ColorScheme", {
53+
callback = function()
54+
M.setup_highlights()
55+
-- Re-highlight all buffers
56+
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
57+
if vim.api.nvim_buf_is_valid(buf) and vim.bo[buf].modifiable then
58+
vim.schedule(function()
59+
if M.config.enable then
60+
M.highlight_keywords(buf)
61+
end
62+
end)
63+
end
64+
end
65+
end,
66+
desc = "Notice-me re-apply highlights after colorscheme change",
67+
})
68+
4869
-- Also highlight current buffer immediately
4970
vim.schedule(function()
50-
if vim.api.nvim_get_current_buf() then
71+
if M.config.enable and vim.api.nvim_get_current_buf() then
5172
M.highlight_keywords(vim.api.nvim_get_current_buf())
5273
end
5374
end)
@@ -56,31 +77,37 @@ end
5677
-- Highlight keywords in a buffer
5778
function M.highlight_keywords(bufnr)
5879
if not bufnr or not vim.api.nvim_buf_is_valid(bufnr) then return end
59-
80+
81+
-- Skip if the buffer is not modifiable or in a special mode
82+
if vim.bo[bufnr].buftype ~= "" then return end
83+
6084
-- Clear previous highlights
6185
vim.api.nvim_buf_clear_namespace(bufnr, M.namespace, 0, -1)
62-
86+
6387
-- Get buffer content
6488
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
65-
89+
6690
-- Highlight keywords
6791
for i, line in ipairs(lines) do
6892
for keyword, _ in pairs(M.config.keywords) do
93+
-- Find exact keywords, not partial matches
6994
local start_idx = 1
7095
while true do
71-
local match_start, match_end = line:find(keyword, start_idx)
96+
-- Find using pattern to ensure we match whole words
97+
local pattern = "%f[%w_]" .. keyword .. "%f[^%w_]"
98+
local match_start, match_end = line:find(pattern, start_idx)
7299
if not match_start then break end
73-
100+
74101
-- Apply highlight
75-
vim.api.nvim_buf_add_highlight(
102+
pcall(vim.api.nvim_buf_add_highlight,
76103
bufnr,
77104
M.namespace,
78105
"NoticeMe" .. keyword,
79106
i - 1,
80107
match_start - 1,
81108
match_end
82109
)
83-
110+
84111
start_idx = match_end + 1
85112
end
86113
end
@@ -93,4 +120,4 @@ function M.example_function()
93120
print("Keywords being highlighted:", vim.inspect(vim.tbl_keys(M.config.keywords)))
94121
end
95122

96-
return M
123+
return M

plugin/notice-me.lua

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,39 @@ end, { desc = "Example notice-me command" })
1414
vim.api.nvim_create_user_command("NoticeMeToggle", function()
1515
local notice_me = require("notice-me")
1616
notice_me.config.enable = not notice_me.config.enable
17-
17+
1818
if notice_me.config.enable then
19-
-- Reapply highlights on current buffer
20-
notice_me.highlight_keywords(vim.api.nvim_get_current_buf())
19+
-- Reapply highlights on all buffers
20+
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
21+
if vim.api.nvim_buf_is_valid(buf) and vim.bo[buf].modifiable then
22+
vim.schedule(function()
23+
notice_me.highlight_keywords(buf)
24+
end)
25+
end
26+
end
2127
vim.notify("Keyword highlighting enabled")
2228
else
23-
-- Clear highlights on current buffer
24-
local bufnr = vim.api.nvim_get_current_buf()
25-
vim.api.nvim_buf_clear_namespace(bufnr, notice_me.namespace, 0, -1)
29+
-- Clear highlights on all buffers
30+
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
31+
if vim.api.nvim_buf_is_valid(buf) then
32+
vim.api.nvim_buf_clear_namespace(buf, notice_me.namespace, 0, -1)
33+
end
34+
end
2635
vim.notify("Keyword highlighting disabled")
2736
end
28-
end, { desc = "Toggle notice-me keyword highlighting" })
37+
end, { desc = "Toggle notice-me keyword highlighting" })
38+
39+
-- Add command to refresh highlights
40+
vim.api.nvim_create_user_command("NoticeMeRefresh", function()
41+
local notice_me = require("notice-me")
42+
if not notice_me.config.enable then
43+
vim.notify("Notice-me is disabled, enable it first with :NoticeMeToggle")
44+
return
45+
end
46+
47+
-- Force refresh highlights on current buffer
48+
local bufnr = vim.api.nvim_get_current_buf()
49+
notice_me.highlight_keywords(bufnr)
50+
vim.notify("Refreshed keyword highlights")
51+
end, { desc = "Refresh notice-me keyword highlighting" })
52+

0 commit comments

Comments
 (0)