refactor: address emmylua diagnostics

This commit is contained in:
2026-04-20 22:11:18 +02:00
parent 516b9ea749
commit c7dd083083
29 changed files with 542 additions and 532 deletions
+10 -23
View File
@@ -7,23 +7,13 @@ local log = require("log")
local GROUP = vim.api.nvim_create_augroup("ow.lsp.codelens", { clear = true })
---@type table<integer, ow.lsp.codelens.Session>
local session_by_buf = {}
local M = {}
---@param buf integer
---@return ow.lsp.codelens.Session
local function get_session(buf)
session_by_buf[buf] = session_by_buf[buf] or Session.new(buf)
return session_by_buf[buf]
end
---@param buf? integer
---@return boolean
function M.is_enabled(buf)
buf = buf or vim.api.nvim_get_current_buf()
local session = session_by_buf[buf]
local session = Session.find(buf)
return session ~= nil and session.enabled
end
@@ -34,13 +24,13 @@ function M.enable(value, buf)
value = true
end
buf = buf or vim.api.nvim_get_current_buf()
get_session(buf):enable(value)
Session.get(buf):enable(value)
end
---@param buf? integer
function M.toggle(buf)
buf = buf or vim.api.nvim_get_current_buf()
get_session(buf):toggle()
Session.get(buf):toggle()
end
---@class ow.lsp.codelens.SetupOpts : ow.lsp.codelens.row.ConfigureOpts
@@ -49,17 +39,18 @@ end
function M.setup(opts)
Row.configure(opts or {})
local method = vim.lsp.protocol.Methods.workspace_codeLens_refresh
vim.lsp.handlers[method] = function(err, _, ctx)
---@param err lsp.ResponseError?
---@param ctx lsp.HandlerContext
vim.lsp.handlers["workspace/codeLens/refresh"] = function(err, _, ctx)
if err then
log.warning(
"client %d: error on %s: %s",
ctx.client_id,
method,
ctx.method,
err.message
)
end
for buf, session in pairs(session_by_buf) do
for buf, session in pairs(Session.all()) do
if
session.enabled
and #vim.lsp.get_clients({ bufnr = buf, id = ctx.client_id })
@@ -74,7 +65,7 @@ function M.setup(opts)
vim.api.nvim_create_autocmd({ "BufEnter", "LspAttach" }, {
group = GROUP,
callback = function(ev)
local session = session_by_buf[ev.buf]
local session = Session.find(ev.buf)
if session and session.enabled then
session:refresh()
end
@@ -84,11 +75,7 @@ function M.setup(opts)
vim.api.nvim_create_autocmd({ "BufDelete", "BufWipeout" }, {
group = GROUP,
callback = function(ev)
local session = session_by_buf[ev.buf]
if session then
session:abort()
end
session_by_buf[ev.buf] = nil
Session.remove(ev.buf)
end,
})
end
+19 -26
View File
@@ -15,7 +15,8 @@ function RefreshTask.new(session)
session = session,
aborted = false,
cancels = {},
}, RefreshTask)
},
RefreshTask)
end
---@param cancel fun()
@@ -35,20 +36,19 @@ end
---@param lens lsp.CodeLens
---@param row ow.lsp.codelens.Row
function RefreshTask:resolve(client, lens, row)
local method = vim.lsp.protocol.Methods.codeLens_resolve
local _, req_id = client:request(
method,
"codeLens/resolve",
lens,
---@param err lsp.ResponseError?
---@param resolved lsp.CodeLens?
function(err, resolved)
---@param ctx lsp.HandlerContext
function(err, resolved, ctx)
if self.aborted then
return
end
if err then
log.warning(
"client %d: %s failed: %s",
client.id,
method,
"client %d: %s failed: %s", client.id, ctx.method,
err.message
)
end
@@ -77,33 +77,27 @@ function RefreshTask:process_lens(rows, client, lens)
row:add(lens)
return
end
if
not client
or not client:supports_method(vim.lsp.protocol.Methods.codeLens_resolve)
then
if not client or not client:supports_method("codeLens/resolve") then
return
end
row:expect()
self:resolve(client, lens, row)
end
---@param responses table<integer, ow.lsp.CodeLensResponse>
function RefreshTask:process_responses(responses)
local method = vim.lsp.protocol.Methods.textDocument_codeLens
---@param results table<integer, ow.lsp.CodeLensResponse>
function RefreshTask:process_results(results)
local session = self.session
local new_rows = {}
for client_id, response in pairs(responses) do
if response.err then
for client_id, result in pairs(results) do
if result.err then
log.warning(
"client %d: %s failed: %s",
client_id,
method,
response.err.message
"client %d: %s failed: %s", client_id, result.context.method,
result.err.message
)
end
if not response.err and type(response.result) == "table" then
if not result.err and type(result.result) == "table" then
local client = vim.lsp.get_client_by_id(client_id)
for _, lens in ipairs(response.result) do
for _, lens in ipairs(result.result) do
self:process_lens(new_rows, client, lens)
end
end
@@ -113,19 +107,18 @@ function RefreshTask:process_responses(responses)
end
function RefreshTask:run()
local method = vim.lsp.protocol.Methods.textDocument_codeLens
local params = {
textDocument = vim.lsp.util.make_text_document_params(self.session.buf),
}
local cancel = vim.lsp.buf_request_all(
self.session.buf,
method,
"textDocument/codeLens",
params,
function(responses)
function(results)
if self.aborted then
return
end
self:process_responses(responses)
self:process_results(results)
end
)
self:track(cancel)
+18 -7
View File
@@ -6,9 +6,12 @@ local NS = vim.api.nvim_create_namespace("ow.lsp.codelens")
local position = "above"
local separator = " | "
---@class ow.lsp.CodeLens : lsp.CodeLens
---@field command lsp.Command
---@class ow.lsp.codelens.Row
---@field row integer
---@field lenses lsp.CodeLens[]
---@field lenses ow.lsp.CodeLens[]
---@field pending integer
local Row = {}
Row.__index = Row
@@ -19,7 +22,7 @@ function Row.new(row)
return setmetatable({ row = row, lenses = {}, pending = 0 }, Row)
end
---@param lens lsp.CodeLens
---@param lens ow.lsp.CodeLens
function Row:add(lens)
table.insert(self.lenses, lens)
end
@@ -31,8 +34,8 @@ end
---@param lens? lsp.CodeLens
function Row:resolve(lens)
self.pending = self.pending - 1
if lens then
table.insert(self.lenses, lens)
if lens and lens.command then
self:add(lens)
end
end
@@ -58,7 +61,9 @@ function Row:render(buf)
end
end
local col, opts
---@type vim.api.keyset.set_extmark
local opts
local col
if position == "above" then
local line = vim.api.nvim_buf_get_lines(
buf,
@@ -79,7 +84,8 @@ function Row:render(buf)
hl_mode = "combine",
}
else
col = position == "inline" and self.lenses[1].range.start.character or 0
local first = assert(self.lenses[1])
col = position == "inline" and first.range.start.character or 0
opts = {
virt_text = parts,
virt_text_pos = position,
@@ -97,6 +103,11 @@ function Row:render(buf)
{ self.row, -1 },
{ details = true }
)
---@type {
--- id: integer,
--- col: integer,
--- details: vim.api.keyset.extmark_details?
---}
local primary
for i, mark in ipairs(marks) do
if i == 1 then
@@ -107,7 +118,7 @@ function Row:render(buf)
end
if primary and primary.col == col and primary.details then
local d = primary.details --[[@as vim.api.keyset.extmark_details]]
local d = primary.details
local same = opts.virt_lines
and vim.deep_equal(d.virt_lines, opts.virt_lines)
and d.virt_lines_above == opts.virt_lines_above
+41 -13
View File
@@ -13,9 +13,13 @@ local REFRESH_DEBOUNCE_MS = 200
local Session = {}
Session.__index = Session
---@param session ow.lsp.codelens.Session
local function do_refresh(session)
if not session.enabled then
---@type table<integer, ow.lsp.codelens.Session>
local by_buf = {}
---@param buf integer
local function do_refresh(buf)
local session = by_buf[buf]
if not session or not session.enabled then
return
end
session:abort()
@@ -24,21 +28,45 @@ local function do_refresh(session)
task:run()
end
local refresher = util.keyed_debounce(do_refresh, REFRESH_DEBOUNCE_MS)
local refresh, refresh_handle =
util.keyed_debounce(do_refresh, REFRESH_DEBOUNCE_MS)
---@param buf integer
---@return ow.lsp.codelens.Session
function Session.new(buf)
return setmetatable({
buf = buf,
enabled = false,
attached = false,
rows = {},
}, Session)
function Session.get(buf)
if not by_buf[buf] then
by_buf[buf] = setmetatable({
buf = buf,
enabled = false,
attached = false,
rows = {},
}, Session)
end
return by_buf[buf]
end
---@param buf integer
---@return ow.lsp.codelens.Session?
function Session.find(buf)
return by_buf[buf]
end
---@return table<integer, ow.lsp.codelens.Session>
function Session.all()
return by_buf
end
---@param buf integer
function Session.remove(buf)
local session = by_buf[buf]
if session then
session:abort()
end
by_buf[buf] = nil
end
function Session:abort()
refresher:cancel(self)
refresh_handle.cancel(self.buf)
if self.current then
self.current:abort()
self.current = nil
@@ -46,7 +74,7 @@ function Session:abort()
end
function Session:refresh()
refresher(self)
refresh(self.buf)
end
function Session:render()
+55 -52
View File
@@ -10,11 +10,7 @@ local popup = Popup.new()
---@param client vim.lsp.Client
---@param buf integer
local function on_attach(client, buf)
if
not client:supports_method(
vim.lsp.protocol.Methods.textDocument_completion
)
then
if not client:supports_method("textDocument/completion") then
return
end
@@ -64,26 +60,23 @@ function M.setup()
end
local ft = vim.bo[ev.buf].filetype
---@type ow.lsp.completion.Pum
local pum = {
row = vim.v.event.row,
col = vim.v.event.col,
width = vim.v.event.width,
height = vim.v.event.height,
scrollbar = vim.v.event.scrollbar,
row = vim.v.event.row --[[@as integer]],
col = vim.v.event.col --[[@as integer]],
width = vim.v.event.width --[[@as integer]],
height = vim.v.event.height --[[@as integer]],
scrollbar = vim.v.event.scrollbar --[[@as boolean]],
}
if
client:supports_method(
vim.lsp.protocol.Methods.completionItem_resolve
)
then
if client:supports_method("completionItem/resolve") then
popup:dispatch_resolve(
client,
item,
ft,
pum,
completed.word,
completed.word or "",
ev.buf
)
else
@@ -125,22 +118,27 @@ function M.setup()
)
end
if raw.command then
local method =
vim.lsp.protocol.Methods.workspace_executeCommand
client:request(method, {
command = raw.command.command,
arguments = raw.command.arguments,
}, function(err)
if err then
log.warning(
"client %d: %s failed for %s: %s",
client.id,
method,
raw.command.title,
err.message
)
end
end, ev.buf)
client:request(
"workspace/executeCommand",
{
command = raw.command.command,
arguments = raw.command.arguments,
},
---@param err lsp.ResponseError?
---@param ctx lsp.HandlerContext
function(err, _, ctx)
if err then
log.warning(
"client %d: %s failed for %s: %s",
client.id,
ctx.method,
raw.command.title,
err.message
)
end
end,
ev.buf
)
end
if target.snippet then
local word = completed.word or ""
@@ -161,32 +159,37 @@ function M.setup()
-- Prefer the resolved item when we have it. If we haven't cached
-- one yet and the original is missing edits, resolve on the fly.
local cached = popup:resolved_for(completed.word)
local cached = popup:resolved_for(completed.word or "")
if cached then
apply(cached)
elseif
client:supports_method(
vim.lsp.protocol.Methods.completionItem_resolve
)
client:supports_method("completionItem/resolve")
and not item.raw.additionalTextEdits
and not item.raw.command
then
local method = vim.lsp.protocol.Methods.completionItem_resolve
client:request(method, item.raw, function(err, resolved)
if err then
log.warning(
"client %d: %s failed: %s",
client.id,
method,
err.message
)
end
if err or not resolved then
return
end
item:apply_resolved(resolved)
apply(item)
end, ev.buf)
client:request(
"completionItem/resolve",
item.raw,
---@param err lsp.ResponseError?
---@param resolved lsp.CompletionItem?
---@param ctx lsp.HandlerContext
function(err, resolved, ctx)
if err then
log.warning(
"client %d: %s failed: %s",
client.id,
ctx.method,
err.message
)
end
if err or not resolved then
return
end
item:apply_resolved(resolved)
apply(item)
end,
ev.buf
)
else
apply(item)
end
+77 -53
View File
@@ -81,15 +81,24 @@ function Popup:resolved_for(word)
return nil
end
---@return integer?
function Popup:active_win()
if self.winid and vim.api.nvim_win_is_valid(self.winid) then
return self.winid
end
return nil
end
---@return boolean
function Popup:is_visible()
return self.winid ~= nil and vim.api.nvim_win_is_valid(self.winid)
return self:active_win() ~= nil
end
function Popup:close()
self:cancel_pending()
if self:is_visible() then
vim.api.nvim_win_close(self.winid, true)
local winid = self:active_win()
if winid then
vim.api.nvim_win_close(winid, true)
end
self.winid = nil
end
@@ -115,28 +124,35 @@ end
function Popup:dispatch_resolve(client, item, ft, pum, word, buf)
self:cancel_pending()
self.resolved = nil
local method = vim.lsp.protocol.Methods.completionItem_resolve
local _, request_id = client:request(method, item.raw, function(err, result)
if err then
log.warning(
"client %d: %s failed: %s",
client.id,
method,
err.message
)
end
self.pending = nil
if err or not result then
return
end
local cur = vim.fn.complete_info({ "completed" })
if (vim.tbl_get(cur, "completed", "word") or "") ~= word then
return
end
item:apply_resolved(result)
self.resolved = { word = word, item = item }
self:show(item, ft, pum)
end, buf)
local _, request_id = client:request(
"completionItem/resolve",
item.raw,
---@param err lsp.ResponseError?
---@param result lsp.CompletionItem?
---@param ctx lsp.HandlerContext
function(err, result, ctx)
if err then
log.warning(
"client %d: %s failed: %s",
client.id,
ctx.method,
err.message
)
end
self.pending = nil
if err or not result then
return
end
local cur = vim.fn.complete_info({ "completed" })
if (vim.tbl_get(cur, "completed", "word") or "") ~= word then
return
end
item:apply_resolved(result)
self.resolved = { word = word, item = item }
self:show(item, ft, pum)
end,
buf
)
if request_id then
self.pending = { client = client, id = request_id }
end
@@ -148,14 +164,14 @@ Popup.HALF_PAGE = HALF_HEIGHT
---@param pum ow.lsp.completion.Pum
---@param width integer
function Popup:render(content, pum, width)
self:ensure_buffer()
local bufnr = self:ensure_buffer()
local lines = vim.split(content, "\n", { plain = true })
vim.api.nvim_buf_set_lines(self.bufnr, 0, -1, false, lines)
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
vim.api.nvim_buf_clear_namespace(self.bufnr, NS, 0, -1)
vim.api.nvim_buf_clear_namespace(bufnr, NS, 0, -1)
for i, line in ipairs(lines) do
if line ~= "" and line:gsub("", "") == "" then
vim.api.nvim_buf_set_extmark(self.bufnr, NS, i - 1, 0, {
vim.api.nvim_buf_set_extmark(bufnr, NS, i - 1, 0, {
end_row = i - 1,
end_col = #line,
hl_group = "FloatBorder",
@@ -187,22 +203,24 @@ function Popup:render(content, pum, width)
style = "minimal",
}
if self:is_visible() then
vim.api.nvim_win_set_config(self.winid, cfg)
local winid = self:active_win()
if winid then
vim.api.nvim_win_set_config(winid, cfg)
else
cfg.noautocmd = true
self.winid = vim.api.nvim_open_win(self.bufnr, false, cfg)
vim.wo[self.winid].wrap = true
vim.wo[self.winid].linebreak = true
vim.wo[self.winid].conceallevel = 2
winid = vim.api.nvim_open_win(bufnr, false, cfg)
self.winid = winid
vim.wo[winid].wrap = true
vim.wo[winid].linebreak = true
vim.wo[winid].conceallevel = 2
end
vim.api.nvim_win_set_cursor(self.winid, { 1, 0 })
vim.api.nvim_win_set_cursor(winid, { 1, 0 })
local actual = vim.api.nvim_win_text_height(self.winid, {
local actual = vim.api.nvim_win_text_height(winid, {
max_height = MAX_HEIGHT,
}).all
if actual ~= height then
vim.api.nvim_win_set_height(self.winid, actual)
vim.api.nvim_win_set_height(winid, actual)
end
self:update_indicators()
@@ -211,12 +229,13 @@ end
---@param direction "up" | "down"
---@param count integer
function Popup:scroll(direction, count)
if not self:is_visible() then
local winid = self:active_win()
if not winid then
return
end
local key = direction == "down" and vim.keycode("<C-e>")
or vim.keycode("<C-y>")
vim.api.nvim_win_call(self.winid, function()
vim.api.nvim_win_call(winid, function()
vim.cmd.normal({ args = { count .. key }, bang = true })
end)
self:update_indicators()
@@ -229,46 +248,51 @@ function Popup:cancel_pending()
end
end
---@return integer
function Popup:ensure_buffer()
if self.bufnr and vim.api.nvim_buf_is_valid(self.bufnr) then
return
return self.bufnr
end
self.bufnr = vim.api.nvim_create_buf(false, true)
vim.bo[self.bufnr].buftype = "nofile"
vim.bo[self.bufnr].bufhidden = "hide"
vim.bo[self.bufnr].swapfile = false
local bufnr = vim.api.nvim_create_buf(false, true)
vim.bo[bufnr].buftype = "nofile"
vim.bo[bufnr].bufhidden = "hide"
vim.bo[bufnr].swapfile = false
-- Markdown parser may not be installed; fall back to no highlighting.
pcall(vim.treesitter.start, self.bufnr, "markdown")
pcall(vim.treesitter.start, bufnr, "markdown")
self.bufnr = bufnr
return bufnr
end
function Popup:update_indicators()
if not self:is_visible() then
local winid = self:active_win()
if not winid then
return
end
local visible = vim.api.nvim_win_get_height(self.winid)
local topline, botline = unpack(vim.api.nvim_win_call(self.winid, function()
local visible = vim.api.nvim_win_get_height(winid)
---@type integer, integer
local topline, botline = unpack(vim.api.nvim_win_call(winid, function()
return { vim.fn.line("w0"), vim.fn.line("w$") }
end))
local display_topline = 1
if topline > 1 then
display_topline = vim.api.nvim_win_text_height(self.winid, {
display_topline = vim.api.nvim_win_text_height(winid, {
end_row = topline - 2,
}).all + 1
end
local display_bot = display_topline + visible - 1
local total = vim.api.nvim_win_text_height(self.winid, {
local total = vim.api.nvim_win_text_height(winid, {
max_height = display_bot + 1,
}).all
local has_above = display_topline > 1
local has_below = display_bot < total
and botline
< vim.api.nvim_buf_line_count(vim.api.nvim_win_get_buf(self.winid))
< vim.api.nvim_buf_line_count(vim.api.nvim_win_get_buf(winid))
vim.api.nvim_win_set_config(self.winid, {
vim.api.nvim_win_set_config(winid, {
title = has_above and { { "", "FloatBorder" } } or "",
title_pos = has_above and "right" or nil,
footer = has_below and { { "", "FloatBorder" } } or "",
+54 -48
View File
@@ -113,6 +113,7 @@ local function build_items(responses, base)
items = vim.fn.matchfuzzy(items, base, { key = "filter" })
else
items = vim.tbl_filter(function(item)
---@diagnostic disable-next-line: undefined-field
return vim.startswith(item.filter, base)
end, items)
end
@@ -123,7 +124,7 @@ end
local function buffer_has_completion_client()
return #vim.lsp.get_clients({
bufnr = 0,
method = vim.lsp.protocol.Methods.textDocument_completion,
method = "textDocument/completion",
}) > 0
end
@@ -140,7 +141,7 @@ end
local function is_trigger_char(char)
local clients = vim.lsp.get_clients({
bufnr = 0,
method = vim.lsp.protocol.Methods.textDocument_completion,
method = "textDocument/completion",
})
for _, client in ipairs(clients) do
local chars = vim.tbl_get(
@@ -199,61 +200,66 @@ function Session:dispatch(trigger_kind, trigger_char, manual)
triggerKind = trigger_kind,
triggerCharacter = trigger_char,
}
local method = vim.lsp.protocol.Methods.textDocument_completion
self.cancel = vim.lsp.buf_request_all(buf, method, params, function(result)
if self.generation ~= gen then
return
end
vim.schedule(function()
-- Re-check: another dispatch may have fired between response
-- and the scheduled callback running.
self.cancel = vim.lsp.buf_request_all(
buf,
"textDocument/completion",
params,
function(result, ctx)
if self.generation ~= gen then
return
end
if vim.fn.mode() ~= "i" then
return
end
local word_start, cursor = word_bounds()
if
not self.manual
and not self.trigger_char
and word_start == cursor
then
return
end
self.is_incomplete = false
for client_id, response in pairs(result) do
if response.err then
log.warning(
"client %d: %s failed: %s",
client_id,
method,
response.err.message
)
vim.schedule(function()
-- Re-check: another dispatch may have fired between response
-- and the scheduled callback running.
if self.generation ~= gen then
return
end
local r = response.result
if type(r) == "table" and r.isIncomplete then
self.is_incomplete = true
break
if vim.fn.mode() ~= "i" then
return
end
end
local start = word_start
for _, response in pairs(result) do
local pos = edit_start(response)
if pos then
start = pos.character
break
local word_start, cursor = word_bounds()
if
not self.manual
and not self.trigger_char
and word_start == cursor
then
return
end
end
local base = vim.api.nvim_get_current_line():sub(start + 1, cursor)
vim.fn.complete(start + 1, build_items(result, base))
end)
end)
self.is_incomplete = false
for client_id, response in pairs(result) do
if response.err then
log.warning(
"client %d: %s failed: %s",
client_id,
ctx.method,
response.err.message
)
end
local r = response.result
if type(r) == "table" and r.isIncomplete then
self.is_incomplete = true
break
end
end
local start = word_start
for _, response in pairs(result) do
local pos = edit_start(response)
if pos then
start = pos.character
break
end
end
local base =
vim.api.nvim_get_current_line():sub(start + 1, cursor)
vim.fn.complete(start + 1, build_items(result, base))
end)
end
)
end
local session = Session.new()
local dispatcher = util.debounce(function(trigger_kind, char)
local dispatch = util.debounce(function(trigger_kind, char)
if vim.fn.mode() ~= "i" then
return
end
@@ -288,7 +294,7 @@ function M.on_insert_char_pre()
local kind_num = is_trigger
and vim.lsp.protocol.CompletionTriggerKind.TriggerCharacter
or vim.lsp.protocol.CompletionTriggerKind.Invoked
dispatcher(kind_num, is_trigger and char or nil)
dispatch(kind_num, is_trigger and char or nil)
end
return M
+9
View File
@@ -33,6 +33,15 @@ local function with_file(path, settings)
return
end
if type(resp) ~= "table" then
log.warning(
"Invalid json in file %s: expected table, got: %s",
path,
type(resp)
)
return
end
return vim.tbl_deep_extend("force", settings or {}, resp)
end