@@ -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 ()
2828function 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 )
3737
3838-- Set up autocommands to apply highlighting
3939function 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 )
5677-- Highlight keywords in a buffer
5778function 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 )))
94121end
95122
96- return M
123+ return M
0 commit comments