From 007f0efbca55b235e84e74b50e805ef87a5310c3 Mon Sep 17 00:00:00 2001 From: Akianonymus Date: Fri, 9 Sep 2022 13:49:10 +0530 Subject: Make sure to use existing options when attaching buffer --- doc/colorizer.txt | 5 ++++- doc/modules/colorizer.html | 7 ++++++- lua/colorizer.lua | 20 +++++++++++++++----- test/expectation.txt | 2 +- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/doc/colorizer.txt b/doc/colorizer.txt index 05fc8da..3661d8f 100644 --- a/doc/colorizer.txt +++ b/doc/colorizer.txt @@ -130,7 +130,7 @@ attach_to_buffer({buf}, {options}, {typ}) *colorizer.attach_to_buffer* Parameters: ~ {buf} - integer: A value of 0 implies the current buffer. - {options} - table: Configuration options as described in `setup` + {options} - table|nil: Configuration options as described in `setup` {typ} - string|nil: "buf" or "file" - The type of buffer option @@ -174,6 +174,9 @@ get_buffer_options({buf}) *colorizer.get_buffer_options* Parameters: ~ {buf} - number|nil: Buffer number + returns:~ + table or nil + reload_all_buffers() *colorizer.reload_all_buffers* diff --git a/doc/modules/colorizer.html b/doc/modules/colorizer.html index 3b04ab9..311bd60 100644 --- a/doc/modules/colorizer.html +++ b/doc/modules/colorizer.html @@ -262,7 +262,7 @@ USE WITH LUA integer: A value of 0 implies the current buffer.
  • options - table: Configuration options as described in setup + table|nil: Configuration options as described in setup
  • typ string|nil: "buf" or "file" - The type of buffer option @@ -335,6 +335,11 @@ Setup an autocmd which enables colorizing for the filetypes and options specifie
  • +

    Returns:

    +
      + + table|nil +
    diff --git a/lua/colorizer.lua b/lua/colorizer.lua index f9010c4..7f89685 100644 --- a/lua/colorizer.lua +++ b/lua/colorizer.lua @@ -68,6 +68,7 @@ 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 +local get_buffer_options ---Highlight the buffer region ---@function highlight_buffer -- @see colorizer.buffer_utils.highlight_buffer @@ -169,6 +170,7 @@ local function is_buffer_attached(buf) buf = current_buf() else if not api.nvim_buf_is_valid(buf) then + BUFFER_LOCAL[buf], BUFFER_OPTIONS[buf] = nil, nil return end end @@ -215,17 +217,24 @@ end ---Attach to a buffer and continuously highlight changes. ---@param buf integer: A value of 0 implies the current buffer. ----@param options table: Configuration options as described in `setup` +---@param options table|nil: Configuration options as described in `setup` ---@param typ string|nil: "buf" or "file" - The type of buffer option 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 + BUFFER_LOCAL[buf], BUFFER_OPTIONS[buf] = nil, nil return end end + -- if the buffer is already attached then grab those options + if not options then + options = get_buffer_options(buf) + end + + -- if not make new options if not options then options = new_buffer_options(buf, typ) end @@ -442,11 +451,12 @@ end --- Return the currently active buffer options. ---@param buf number|nil: Buffer number -local function get_buffer_options(buf) - if buf == 0 or buf == nil then - buf = current_buf() +---@return table|nil +function get_buffer_options(buf) + local buffer = is_buffer_attached(buf) + if buffer then + return BUFFER_OPTIONS[buffer] end - return merge({}, BUFFER_OPTIONS[buf]) end --- Reload all of the currently active highlighted buffers. diff --git a/test/expectation.txt b/test/expectation.txt index 0969608..7ae3484 100644 --- a/test/expectation.txt +++ b/test/expectation.txt @@ -1,6 +1,6 @@ -- vim:ft=lua require("colorizer").detach_from_buffer(0) -require("colorizer").attach_to_buffer(0, { AARRGGBB = true, css = true, mode = "background" }) +require("colorizer").attach_to_buffer(0, { AARRGGBB = true, css = true, mode = "foreground" }) --[[ SUCCESS 0xFf32A14B 0xFf32A14B #32a14b -- cgit v1.2.3-70-g09d2