local M = {} function M.setup(opts) opts = vim.tbl_extend("force", { sign = "💡", hl = "DiagnosticWarn", priority = 10, }, opts or {}) local ns = vim.api.nvim_create_namespace("tiny-lightbulb") local gen = 0 local function clear() gen = gen + 1 vim.api.nvim_buf_clear_namespace(0, ns, 0, -1) end vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI" }, { group = vim.api.nvim_create_augroup("tiny-lightbulb", { clear = true }), callback = function() local buf, lnum = vim.api.nvim_get_current_buf(), vim.fn.line(".") - 1 clear() if #vim.lsp.get_clients({ bufnr = buf, method = "textDocument/codeAction" }) == 0 then return end local id = gen vim.lsp.buf_request(buf, "textDocument/codeAction", { textDocument = vim.lsp.util.make_text_document_params(buf), range = { start = { line = lnum, character = 0 }, ["end"] = { line = lnum, character = 0 } }, context = { diagnostics = vim.diagnostic.get(buf, { lnum = lnum }) }, }, function(err, results) if id == gen and not err and results and #results > 0 and vim.api.nvim_buf_is_valid(buf) then vim.api.nvim_buf_set_extmark(buf, ns, lnum, 0, { sign_text = opts.sign, sign_hl_group = opts.hl, priority = opts.priority, }) end end) end, }) vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI", "InsertEnter" }, { group = vim.api.nvim_create_augroup("tiny-lightbulb-clear", { clear = true }), callback = clear, }) end return M