From d079fbfe354ebfec18c4e1bfd7fee695703e3692 Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Fri, 8 Nov 2024 09:44:45 +0100 Subject: [PATCH] feat(words): configurable mode to show references. Defaults to n, i, c. Closes #18 --- lua/snacks/words.lua | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/lua/snacks/words.lua b/lua/snacks/words.lua index 71917002..1409ce5b 100644 --- a/lua/snacks/words.lua +++ b/lua/snacks/words.lua @@ -11,6 +11,7 @@ local defaults = { debounce = 200, -- time in ms to wait before updating notify_jump = false, -- show a notification when jumping notify_end = true, -- show a notification when reaching the end + modes = { "n", "i", "c" }, -- modes to show references } local config = Snacks.config.get("words", defaults) @@ -21,9 +22,13 @@ local timer = (vim.uv or vim.loop).new_timer() function M.setup() local group = vim.api.nvim_create_augroup("snacks_words", { clear = true }) - vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, { + vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI", "ModeChanged" }, { group = group, callback = function() + if not M.is_enabled() then + M.clear() + return + end if not ({ M.get() })[2] then M.update() end @@ -31,6 +36,10 @@ function M.setup() }) end +function M.clear() + vim.lsp.buf.clear_references() +end + ---@private function M.update() local buf = vim.api.nvim_get_current_buf() @@ -42,7 +51,7 @@ function M.update() return end vim.lsp.buf.document_highlight() - vim.lsp.buf.clear_references() + M.clear() end) end end) @@ -51,7 +60,12 @@ end ---@param buf number? function M.is_enabled(buf) + local mode = vim.api.nvim_get_mode().mode:lower() + mode = mode:gsub("\22", "v"):gsub("\19", "s") + mode = mode:sub(1, 2) == "no" and "o" or mode + mode = mode:sub(1, 1):match("[ncitsvo]") or "n" return config.enabled + and vim.tbl_contains(config.modes, mode) and #vim.lsp.get_clients({ method = vim.lsp.protocol.Methods.textDocument_documentHighlight, bufnr = buf or 0,