refactor: golf core module
This commit is contained in:
4 files changed
+11
-30
No files matched your search
@@ -3,14 +3,14 @@
|
|||||||
A tiny lightbulb for Neovim 0.12+. Shows a sign when LSP code actions are
|
A tiny lightbulb for Neovim 0.12+. Shows a sign when LSP code actions are
|
||||||
available on the current line.
|
available on the current line.
|
||||||
|
|
||||||
Zero config. ~50 lines of Lua.
|
Zero config. ~40 lines of Lua.
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
- Displays a 💡 in the sign column when code actions are available
|
- Displays a 💡 in the sign column when code actions are available
|
||||||
- Clears instantly on cursor move
|
- Clears instantly on cursor move
|
||||||
- Async LSP requests with stale-response handling
|
- 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
|
- Works out of the box — no setup call required
|
||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
@@ -36,7 +36,6 @@ All options are optional. Call `setup()` only if you want to customize:
|
|||||||
require("tiny-lightbulb").setup({
|
require("tiny-lightbulb").setup({
|
||||||
sign = "💡", -- sign column text
|
sign = "💡", -- sign column text
|
||||||
hl = "DiagnosticWarn", -- highlight group
|
hl = "DiagnosticWarn", -- highlight group
|
||||||
priority = 10, -- extmark priority
|
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -36,7 +36,6 @@ That's it. The plugin auto-loads with defaults. To customize, call setup: >lua
|
|||||||
require("tiny-lightbulb").setup({
|
require("tiny-lightbulb").setup({
|
||||||
sign = "💡",
|
sign = "💡",
|
||||||
hl = "DiagnosticWarn",
|
hl = "DiagnosticWarn",
|
||||||
priority = 10,
|
|
||||||
})
|
})
|
||||||
<
|
<
|
||||||
==============================================================================
|
==============================================================================
|
||||||
@@ -53,7 +52,6 @@ setup({opts})
|
|||||||
require("tiny-lightbulb").setup({
|
require("tiny-lightbulb").setup({
|
||||||
sign = "💡", -- sign column text
|
sign = "💡", -- sign column text
|
||||||
hl = "DiagnosticWarn", -- highlight group for the sign
|
hl = "DiagnosticWarn", -- highlight group for the sign
|
||||||
priority = 10, -- extmark priority
|
|
||||||
})
|
})
|
||||||
<
|
<
|
||||||
Parameters: ~
|
Parameters: ~
|
||||||
@@ -62,7 +60,6 @@ setup({opts})
|
|||||||
Fields: ~
|
Fields: ~
|
||||||
{sign} (string) Text shown in the sign column. Default: "💡"
|
{sign} (string) Text shown in the sign column. Default: "💡"
|
||||||
{hl} (string) Highlight group for the sign. Default: "DiagnosticWarn"
|
{hl} (string) Highlight group for the sign. Default: "DiagnosticWarn"
|
||||||
{priority} (number) Extmark priority. Default: 10
|
|
||||||
|
|
||||||
==============================================================================
|
==============================================================================
|
||||||
vim:tw=78:ts=8:ft=help:norl:
|
vim:tw=78:ts=8:ft=help:norl:
|
||||||
@@ -1,13 +1,9 @@
|
|||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
function M.setup(opts)
|
function M.setup(opts)
|
||||||
opts = vim.tbl_extend("force", {
|
opts = opts or {}
|
||||||
sign = "💡",
|
|
||||||
hl = "DiagnosticWarn",
|
|
||||||
priority = 10,
|
|
||||||
}, opts or {})
|
|
||||||
|
|
||||||
local ns = vim.api.nvim_create_namespace("tiny-lightbulb")
|
local ns = vim.api.nvim_create_namespace("tiny-lightbulb")
|
||||||
|
local group = vim.api.nvim_create_augroup("tiny-lightbulb", { clear = true })
|
||||||
local gen = 0
|
local gen = 0
|
||||||
|
|
||||||
local function clear()
|
local function clear()
|
||||||
@@ -16,7 +12,7 @@ function M.setup(opts)
|
|||||||
end
|
end
|
||||||
|
|
||||||
vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI" }, {
|
vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI" }, {
|
||||||
group = vim.api.nvim_create_augroup("tiny-lightbulb", { clear = true }),
|
group = group,
|
||||||
callback = function()
|
callback = function()
|
||||||
local buf, lnum = vim.api.nvim_get_current_buf(), vim.fn.line(".") - 1
|
local buf, lnum = vim.api.nvim_get_current_buf(), vim.fn.line(".") - 1
|
||||||
clear()
|
clear()
|
||||||
@@ -25,21 +21,14 @@ function M.setup(opts)
|
|||||||
end
|
end
|
||||||
local id = gen
|
local id = gen
|
||||||
vim.lsp.buf_request(buf, "textDocument/codeAction", {
|
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 } },
|
range = { start = { line = lnum, character = 0 }, ["end"] = { line = lnum, character = 0 } },
|
||||||
context = { diagnostics = vim.diagnostic.get(buf, { lnum = lnum }) },
|
context = { diagnostics = vim.diagnostic.get(buf, { lnum = lnum }) },
|
||||||
}, function(err, results)
|
}, function(err, results)
|
||||||
if
|
if id == gen and not err and results and results[1] and vim.api.nvim_buf_is_valid(buf) then
|
||||||
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, {
|
vim.api.nvim_buf_set_extmark(buf, ns, lnum, 0, {
|
||||||
sign_text = opts.sign,
|
sign_text = opts.sign or "💡",
|
||||||
sign_hl_group = opts.hl,
|
sign_hl_group = opts.hl or "DiagnosticWarn",
|
||||||
priority = opts.priority,
|
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
@@ -47,7 +36,7 @@ function M.setup(opts)
|
|||||||
})
|
})
|
||||||
|
|
||||||
vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI", "InsertEnter" }, {
|
vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI", "InsertEnter" }, {
|
||||||
group = vim.api.nvim_create_augroup("tiny-lightbulb-clear", { clear = true }),
|
group = group,
|
||||||
callback = clear,
|
callback = clear,
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|||||||
+1
-5
@@ -5,12 +5,9 @@ local eq = MiniTest.expect.equality
|
|||||||
|
|
||||||
T["setup"] = MiniTest.new_set()
|
T["setup"] = MiniTest.new_set()
|
||||||
|
|
||||||
T["setup"]["creates augroups"] = function()
|
T["setup"]["creates augroup"] = function()
|
||||||
-- plugin/ auto-loaded via minimal_init, so augroups should exist
|
|
||||||
local groups = vim.api.nvim_get_autocmds({ group = "tiny-lightbulb" })
|
local groups = vim.api.nvim_get_autocmds({ group = "tiny-lightbulb" })
|
||||||
eq(#groups > 0, true)
|
eq(#groups > 0, true)
|
||||||
local clear_groups = vim.api.nvim_get_autocmds({ group = "tiny-lightbulb-clear" })
|
|
||||||
eq(#clear_groups > 0, true)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
T["setup"]["is idempotent"] = function()
|
T["setup"]["is idempotent"] = function()
|
||||||
@@ -21,7 +18,6 @@ T["setup"]["is idempotent"] = function()
|
|||||||
end
|
end
|
||||||
|
|
||||||
T["setup"]["respects custom sign"] = function()
|
T["setup"]["respects custom sign"] = function()
|
||||||
-- Just verify it doesn't error with custom opts
|
|
||||||
require("tiny-lightbulb").setup({ sign = "CA" })
|
require("tiny-lightbulb").setup({ sign = "CA" })
|
||||||
local groups = vim.api.nvim_get_autocmds({ group = "tiny-lightbulb" })
|
local groups = vim.api.nvim_get_autocmds({ group = "tiny-lightbulb" })
|
||||||
eq(#groups > 0, true)
|
eq(#groups > 0, true)
|
||||||
|
|||||||
Reference in new issue
Block a user