From a9aacd33ce50961fcf835a4b713e58f8080189ed Mon Sep 17 00:00:00 2001 From: Akianonymus Date: Fri, 9 Sep 2022 09:51:23 +0530 Subject: Fix reload on au ColorScheme | Check if buffer is valid before attaching --- doc/colorizer.txt | 7 +++++++ doc/modules/colorizer.buffer_utils.html | 18 ++++++++++++++++++ lua/colorizer.lua | 12 +++++++++++- lua/colorizer/buffer_utils.lua | 6 ++++++ 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/doc/colorizer.txt b/doc/colorizer.txt index 3dba4d2..05fc8da 100644 --- a/doc/colorizer.txt +++ b/doc/colorizer.txt @@ -247,6 +247,8 @@ Helper functions to highlight buffer smartly LUA API *colorizer.buffer_utils-lua-api* Functions: ~ + |clear_hl_cache| - Clean the highlight cache + |highlight_buffer| - Highlight the buffer region. |rehighlight_buffer| - Rehighlight the buffer if colorizer is active @@ -260,6 +262,11 @@ Fields: ~ `colorizer.attach_to_buffer`. +clear_hl_cache() *colorizer.buffer_utils.clear_hl_cache* + Clean the highlight cache + + + *colorizer.buffer_utils.highlight_buffer* highlight_buffer({buf}, {ns}, {lines}, {line_start}, {line_end}, {options}, diff --git a/doc/modules/colorizer.buffer_utils.html b/doc/modules/colorizer.buffer_utils.html index 315794d..7260c5c 100644 --- a/doc/modules/colorizer.buffer_utils.html +++ b/doc/modules/colorizer.buffer_utils.html @@ -61,6 +61,10 @@

Functions

+ + + + @@ -92,6 +96,20 @@

Functions

+
+ + clear_hl_cache () +
+
+ Clean the highlight cache + + + + + + + +
highlight_buffer (buf, ns, lines, line_start, line_end, options, options_local) diff --git a/lua/colorizer.lua b/lua/colorizer.lua index 93d21db..f9010c4 100644 --- a/lua/colorizer.lua +++ b/lua/colorizer.lua @@ -65,6 +65,7 @@ local buffer_utils = require "colorizer.buffer_utils" local DEFAULT_NAMESPACE = buffer_utils.DEFAULT_NAMESPACE local HIGHLIGHT_MODE_NAMES = buffer_utils.HIGHLIGHT_MODE_NAMES +local clear_hl_cache = buffer_utils.clear_hl_cache local rehighlight_buffer = buffer_utils.rehighlight_buffer ---Highlight the buffer region @@ -166,7 +167,12 @@ end local function is_buffer_attached(buf) if buf == 0 or buf == nil then buf = current_buf() + else + if not api.nvim_buf_is_valid(buf) then + return + end end + local au = api.nvim_get_autocmds { group = AUGROUP_ID, event = { "WinScrolled", "TextChanged", "TextChangedI", "TextChangedP" }, @@ -214,6 +220,10 @@ end local function attach_to_buffer(buf, options, typ) if buf == 0 or buf == nil then buf = current_buf() + else + if not api.nvim_buf_is_valid(buf) then + return + end end if not options then @@ -448,7 +458,7 @@ end --- Clear the highlight cache and reload all buffers. local function clear_highlight_cache() - HIGHLIGHT_CACHE = {} + clear_hl_cache() vim.schedule(reload_all_buffers) end diff --git a/lua/colorizer/buffer_utils.lua b/lua/colorizer/buffer_utils.lua index 2f31a2b..a3b3da7 100644 --- a/lua/colorizer/buffer_utils.lua +++ b/lua/colorizer/buffer_utils.lua @@ -30,6 +30,11 @@ local HIGHLIGHT_MODE_NAMES = { } local HIGHLIGHT_CACHE = {} +--- Clean the highlight cache +local function clear_hl_cache() + HIGHLIGHT_CACHE = {} +end + --- Make a deterministic name for a highlight given these attributes local function make_highlight_name(rgb, mode) return table.concat({ HIGHLIGHT_NAME_PREFIX, HIGHLIGHT_MODE_NAMES[mode], rgb }, "_") @@ -333,6 +338,7 @@ end return { DEFAULT_NAMESPACE = DEFAULT_NAMESPACE, HIGHLIGHT_MODE_NAMES = HIGHLIGHT_MODE_NAMES, + clear_hl_cache = clear_hl_cache, rehighlight_buffer = rehighlight_buffer, highlight_buffer = highlight_buffer, } -- cgit v1.2.3-70-g09d2
clear_hl_cache ()Clean the highlight cache
highlight_buffer (buf, ns, lines, line_start, line_end, options, options_local) Highlight the buffer region.