From 4412c971fd5e7145ce14d5deca3d5e79c7f3aa20 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Thu, 9 Apr 2026 14:06:02 -0600 Subject: [PATCH] refactor: golf core module --- README.md | 5 ++--- doc/tiny-lightbulb.txt | 7 ++----- lua/tiny-lightbulb/init.lua | 27 ++++++++------------------- tests/test_init.lua | 6 +----- 4 files changed, 13 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 4160468..91ef959 100644 --- a/README.md +++ b/README.md @@ -3,14 +3,14 @@ A tiny lightbulb for Neovim 0.12+. Shows a sign when LSP code actions are available on the current line. -Zero config. ~50 lines of Lua. +Zero config. ~40 lines of Lua. ## Features - Displays a 💡 in the sign column when code actions are available - Clears instantly on cursor move - Async LSP requests with stale-response handling -- Configurable sign text, highlight, and priority +- Configurable sign text and highlight group - Works out of the box — no setup call required ## Requirements @@ -36,7 +36,6 @@ All options are optional. Call `setup()` only if you want to customize: require("tiny-lightbulb").setup({ sign = "💡", -- sign column text hl = "DiagnosticWarn", -- highlight group - priority = 10, -- extmark priority }) ``` diff --git a/doc/tiny-lightbulb.txt b/doc/tiny-lightbulb.txt index c065b23..713a39d 100644 --- a/doc/tiny-lightbulb.txt +++ b/doc/tiny-lightbulb.txt @@ -36,7 +36,6 @@ That's it. The plugin auto-loads with defaults. To customize, call setup: >lua require("tiny-lightbulb").setup({ sign = "💡", hl = "DiagnosticWarn", - priority = 10, }) < ============================================================================== @@ -53,16 +52,14 @@ setup({opts}) require("tiny-lightbulb").setup({ sign = "💡", -- sign column text hl = "DiagnosticWarn", -- highlight group for the sign - priority = 10, -- extmark priority }) < Parameters: ~ {opts} (table|nil) Configuration table Fields: ~ - {sign} (string) Text shown in the sign column. Default: "💡" - {hl} (string) Highlight group for the sign. Default: "DiagnosticWarn" - {priority} (number) Extmark priority. Default: 10 + {sign} (string) Text shown in the sign column. Default: "💡" + {hl} (string) Highlight group for the sign. Default: "DiagnosticWarn" ============================================================================== vim:tw=78:ts=8:ft=help:norl: diff --git a/lua/tiny-lightbulb/init.lua b/lua/tiny-lightbulb/init.lua index 5bfb84f..4562986 100644 --- a/lua/tiny-lightbulb/init.lua +++ b/lua/tiny-lightbulb/init.lua @@ -1,13 +1,9 @@ local M = {} function M.setup(opts) - opts = vim.tbl_extend("force", { - sign = "💡", - hl = "DiagnosticWarn", - priority = 10, - }, opts or {}) - + opts = opts or {} local ns = vim.api.nvim_create_namespace("tiny-lightbulb") + local group = vim.api.nvim_create_augroup("tiny-lightbulb", { clear = true }) local gen = 0 local function clear() @@ -16,7 +12,7 @@ function M.setup(opts) end vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI" }, { - group = vim.api.nvim_create_augroup("tiny-lightbulb", { clear = true }), + group = group, callback = function() local buf, lnum = vim.api.nvim_get_current_buf(), vim.fn.line(".") - 1 clear() @@ -25,21 +21,14 @@ function M.setup(opts) end local id = gen vim.lsp.buf_request(buf, "textDocument/codeAction", { - textDocument = vim.lsp.util.make_text_document_params(buf), + textDocument = { uri = vim.uri_from_bufnr(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 + if id == gen and not err and results and results[1] 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, + sign_text = opts.sign or "💡", + sign_hl_group = opts.hl or "DiagnosticWarn", }) end end) @@ -47,7 +36,7 @@ function M.setup(opts) }) vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI", "InsertEnter" }, { - group = vim.api.nvim_create_augroup("tiny-lightbulb-clear", { clear = true }), + group = group, callback = clear, }) end diff --git a/tests/test_init.lua b/tests/test_init.lua index d2e1ccb..4afb2ab 100644 --- a/tests/test_init.lua +++ b/tests/test_init.lua @@ -5,12 +5,9 @@ local eq = MiniTest.expect.equality T["setup"] = MiniTest.new_set() -T["setup"]["creates augroups"] = function() - -- plugin/ auto-loaded via minimal_init, so augroups should exist +T["setup"]["creates augroup"] = function() local groups = vim.api.nvim_get_autocmds({ group = "tiny-lightbulb" }) eq(#groups > 0, true) - local clear_groups = vim.api.nvim_get_autocmds({ group = "tiny-lightbulb-clear" }) - eq(#clear_groups > 0, true) end T["setup"]["is idempotent"] = function() @@ -21,7 +18,6 @@ T["setup"]["is idempotent"] = function() end T["setup"]["respects custom sign"] = function() - -- Just verify it doesn't error with custom opts require("tiny-lightbulb").setup({ sign = "CA" }) local groups = vim.api.nvim_get_autocmds({ group = "tiny-lightbulb" }) eq(#groups > 0, true)