From 1f59884e0d6b3f84e966393196da51abc4a97fa2 Mon Sep 17 00:00:00 2001 From: akianonymus Date: Sat, 25 Feb 2023 23:26:18 +0530 Subject: fix: Highlight buffer even if not focused | #28 like cmp comletion menu --- lua/colorizer.lua | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'lua/colorizer.lua') diff --git a/lua/colorizer.lua b/lua/colorizer.lua index 8da9ce0..f7c189f 100644 --- a/lua/colorizer.lua +++ b/lua/colorizer.lua @@ -90,6 +90,8 @@ local AUGROUP_NAME = "ColorizerSetup" local BUFFER_OPTIONS = {} -- buffer local options created after setup local BUFFER_LOCAL = {} +-- the current buffer id, used in buf_attach calls +local CURRENT_BUF = 0 ---defaults options. --In `user_default_options`, there are 2 types of options @@ -358,10 +360,38 @@ function colorizer.attach_to_buffer(buf, options, typ) table.insert(text_changed_au, "InsertLeave") end + if CURRENT_BUF == 0 then + CURRENT_BUF = buf + end + + -- attach using lua api so buffer gets updated even when not the current buffer + -- completely moving to buf_attach is not possible because it doesn't handle all the text change events + vim.api.nvim_buf_attach(buf, false, { + on_lines = function(_, buffer) + -- only reload if the buffer is not the current one + if not (CURRENT_BUF == buffer) then + -- only reload if it was not disabled using detach_from_buffer + if BUFFER_OPTIONS[buf] then + rehighlight_buffer(buf, options, BUFFER_LOCAL[buf]) + end + end + end, + on_reload = function(_, buffer) + -- only reload if the buffer is not the current one + if not (CURRENT_BUF == buffer) then + -- only reload if it was not disabled using detach_from_buffer + if BUFFER_OPTIONS[buf] then + rehighlight_buffer(buf, options, BUFFER_LOCAL[buf]) + end + end + end, + }) + autocmds[#autocmds + 1] = autocmd(text_changed_au, { group = au_group_id, buffer = buf, callback = function(args) + CURRENT_BUF = buf -- only reload if it was not disabled using detach_from_buffer if BUFFER_OPTIONS[buf] then BUFFER_LOCAL[buf].__event = args.event -- cgit v1.2.3-70-g09d2