From cf4f33f5ea34f9c4e9975299b094f1a2b8b34aa0 Mon Sep 17 00:00:00 2001 From: Akianonymus Date: Thu, 15 Sep 2022 10:00:00 +0530 Subject: Better structure and naming | Misc improvements --- lua/colorizer/tailwind.lua | 53 +++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 24 deletions(-) (limited to 'lua/colorizer/tailwind.lua') diff --git a/lua/colorizer/tailwind.lua b/lua/colorizer/tailwind.lua index 88094f5..839cd0c 100644 --- a/lua/colorizer/tailwind.lua +++ b/lua/colorizer/tailwind.lua @@ -2,6 +2,8 @@ --@module colorizer.tailwind local api = vim.api +local tailwind = {} + -- use a different namespace for tailwind as will be cleared if kept in Default namespace local DEFAULT_NAMESPACE_TAILWIND = api.nvim_create_namespace "colorizer_tailwind" @@ -9,23 +11,27 @@ local TAILWIND = {} --- Cleanup tailwind variables and autocmd ---@param buf number -local function tailwind_cleanup(buf) +function tailwind.cleanup(buf) pcall(api.nvim_del_autocmd, TAILWIND[buf] and TAILWIND[buf].AU_ID[1]) pcall(api.nvim_del_autocmd, TAILWIND[buf] and TAILWIND[buf].AU_ID[2]) api.nvim_buf_clear_namespace(buf, DEFAULT_NAMESPACE_TAILWIND, 0, -1) TAILWIND[buf] = nil end -local function highlight_buffer_tailwind(buf, ns, options, add_highlight) +local function highlight_tailwind(buf, ns, options, add_highlight) -- it can take some time to actually fetch the results -- on top of that, tailwindcss is quite slow in neovim vim.defer_fn(function() + if not TAILWIND[buf] or not TAILWIND[buf].CLIENT or not TAILWIND[buf].CLIENT.request then + return + end + local opts = { textDocument = vim.lsp.util.make_text_document_params() } --@local ---@diagnostic disable-next-line: param-type-mismatch TAILWIND[buf].CLIENT.request("textDocument/documentColor", opts, function(err, results, _, _) if err == nil and results ~= nil then - local datas, line_start, line_end = {}, nil, nil + local data, line_start, line_end = {}, nil, nil for _, color in pairs(results) do local cur_line = color.range.start.line if line_start then @@ -50,22 +56,22 @@ local function highlight_buffer_tailwind(buf, ns, options, add_highlight) local first_col = color.range.start.character local end_col = color.range["end"].character - datas[cur_line] = datas[cur_line] or {} - table.insert(datas[cur_line], { rgb_hex = rgb_hex, range = { first_col, end_col } }) + data[cur_line] = data[cur_line] or {} + table.insert(data[cur_line], { rgb_hex = rgb_hex, range = { first_col, end_col } }) end - add_highlight(options, buf, ns, datas, line_start or 0, line_end and (line_end + 2) or -1) + add_highlight(buf, ns, line_start or 0, line_end and (line_end + 2) or -1, data, options) end end) end, 10) end --- highlight buffer using values returned by tailwindcss --- To see these table information, see |colorizer.buffer_utils| +-- To see these table information, see |colorizer.buffer| ---@param buf number ---@param options table ---@param options_local table ---@param add_highlight function -local function tailwind_setup_lsp_colors(buf, options, options_local, add_highlight) +function tailwind.setup_lsp_colors(buf, options, options_local, add_highlight) TAILWIND[buf] = TAILWIND[buf] or {} TAILWIND[buf].AU_ID = TAILWIND[buf].AU_ID or {} @@ -73,7 +79,7 @@ local function tailwind_setup_lsp_colors(buf, options, options_local, add_highli if vim.version().minor >= 8 then -- create the autocmds so tailwind colours only activate when tailwindcss lsp is active if not TAILWIND[buf].AU_CREATED then - tailwind_cleanup(buf) + tailwind.cleanup(buf) TAILWIND[buf].AU_ID[1] = api.nvim_create_autocmd("LspAttach", { group = options_local.__augroup_id, buffer = buf, @@ -84,7 +90,7 @@ local function tailwind_setup_lsp_colors(buf, options, options_local, add_highli -- wait 100 ms for the first request TAILWIND[buf].CLIENT = client vim.defer_fn(function() - highlight_buffer_tailwind(buf, DEFAULT_NAMESPACE_TAILWIND, options, add_highlight) + highlight_tailwind(buf, DEFAULT_NAMESPACE_TAILWIND, options, add_highlight) end, 100) end end @@ -95,7 +101,7 @@ local function tailwind_setup_lsp_colors(buf, options, options_local, add_highli group = options_local.__augroup_id, buffer = buf, callback = function() - tailwind_cleanup(buf) + tailwind.cleanup(buf) end, }) TAILWIND[buf].AU_CREATED = true @@ -117,16 +123,19 @@ local function tailwind_setup_lsp_colors(buf, options, options_local, add_highli for _, cl in pairs(tailwind_client) do if cl["name"] == "tailwindcss" then tailwind_client = cl - ok = false + ok = true break end end if - vim.tbl_isempty(tailwind_client or {}) - or not tailwind_client - or not tailwind_client.supports_method - or not tailwind_client.supports_method "textDocument/documentColor" + not ok + and ( + vim.tbl_isempty(tailwind_client or {}) + or not tailwind_client + or not tailwind_client.supports_method + or not tailwind_client.supports_method "textDocument/documentColor" + ) then return true end @@ -135,20 +144,16 @@ local function tailwind_setup_lsp_colors(buf, options, options_local, add_highli -- wait 500 ms for the first request vim.defer_fn(function() - highlight_buffer_tailwind(buf, DEFAULT_NAMESPACE_TAILWIND, options, add_highlight) - end, 500) + highlight_tailwind(buf, DEFAULT_NAMESPACE_TAILWIND, options, add_highlight) + end, 1000) return true end -- only try to do tailwindcss highlight if lsp is attached if TAILWIND[buf].CLIENT then - highlight_buffer_tailwind(buf, DEFAULT_NAMESPACE_TAILWIND, options, add_highlight) + highlight_tailwind(buf, DEFAULT_NAMESPACE_TAILWIND, options, add_highlight) end end ----@export -return { - tailwind_cleanup = tailwind_cleanup, - tailwind_setup_lsp_colors = tailwind_setup_lsp_colors, -} +return tailwind -- cgit v1.2.3-70-g09d2