Skip to content

Commit a8c3d50

Browse files
authored
lsp: add vim.lsp.diagnostic.set_qflist() function (neovim#14831)
* Add vim.lsp.diagnostic.set_qflist() function * replaces opts.open_loclist with unified opts.open
1 parent ea35584 commit a8c3d50

File tree

2 files changed

+71
-10
lines changed

2 files changed

+71
-10
lines changed

runtime/doc/lsp.txt

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1470,12 +1470,36 @@ save({diagnostics}, {bufnr}, {client_id}) *vim.lsp.diagnostic.save()*
14701470
save_extmarks({bufnr}, {client_id})
14711471
TODO: Documentation
14721472

1473+
set_qflist({opts}) *vim.lsp.diagnostic.set_qflist()*
1474+
Sets the quickfix list
1475+
1476+
Parameters: ~
1477+
{opts} table|nil Configuration table. Keys:
1478+
{open}: (boolean, default true)
1479+
• Open quickfix list after set
1480+
1481+
• {client_id}: (number)
1482+
• If nil, will consider all clients attached to
1483+
buffer.
1484+
1485+
{severity}: (DiagnosticSeverity)
1486+
• Exclusive severity to consider. Overrides
1487+
{severity_limit}
1488+
1489+
• {severity_limit}: (DiagnosticSeverity)
1490+
• Limit severity of diagnostics found. E.g.
1491+
"Warning" means { "Error", "Warning" } will be
1492+
valid.
1493+
1494+
{workspace}: (boolean, default true)
1495+
• Set the list with workspace diagnostics
1496+
14731497
set_loclist({opts}) *vim.lsp.diagnostic.set_loclist()*
14741498
Sets the location list
14751499

14761500
Parameters: ~
14771501
{opts} table|nil Configuration table. Keys:
1478-
• {open_loclist}: (boolean, default true)
1502+
{open}: (boolean, default true)
14791503
• Open loclist after set
14801504

14811505
• {client_id}: (number)

runtime/lua/vim/lsp/diagnostic.lua

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,10 +1249,10 @@ function M.reset(client_id, buffer_client_map)
12491249
end)
12501250
end
12511251

1252-
--- Sets the location list
1252+
--- Gets diagnostics, converts them to quickfix/location list items, and applies the item_handler callback to the items.
1253+
---@param item_handler function Callback to apply to the diagnostic items
1254+
---@param command string|nil Command to execute after applying the item_handler
12531255
---@param opts table|nil Configuration table. Keys:
1254-
--- - {open_loclist}: (boolean, default true)
1255-
--- - Open loclist after set
12561256
--- - {client_id}: (number)
12571257
--- - If nil, will consider all clients attached to buffer.
12581258
--- - {severity}: (DiagnosticSeverity)
@@ -1261,9 +1261,8 @@ end
12611261
--- - Limit severity of diagnostics found. E.g. "Warning" means { "Error", "Warning" } will be valid.
12621262
--- - {workspace}: (boolean, default false)
12631263
--- - Set the list with workspace diagnostics
1264-
function M.set_loclist(opts)
1264+
local function apply_to_diagnostic_items(item_handler, command, opts)
12651265
opts = opts or {}
1266-
local open_loclist = if_nil(opts.open_loclist, true)
12671266
local current_bufnr = api.nvim_get_current_buf()
12681267
local diags = opts.workspace and M.get_all(opts.client_id) or {
12691268
[current_bufnr] = M.get(current_bufnr, opts.client_id)
@@ -1280,13 +1279,51 @@ function M.set_loclist(opts)
12801279
return true
12811280
end
12821281
local items = util.diagnostics_to_items(diags, predicate)
1283-
local win_id = vim.api.nvim_get_current_win()
1284-
util.set_loclist(items, win_id)
1285-
if open_loclist then
1286-
vim.cmd [[lopen]]
1282+
item_handler(items)
1283+
if command then
1284+
vim.cmd(command)
12871285
end
12881286
end
12891287

1288+
--- Sets the quickfix list
1289+
---@param opts table|nil Configuration table. Keys:
1290+
--- - {open}: (boolean, default true)
1291+
--- - Open quickfix list after set
1292+
--- - {client_id}: (number)
1293+
--- - If nil, will consider all clients attached to buffer.
1294+
--- - {severity}: (DiagnosticSeverity)
1295+
--- - Exclusive severity to consider. Overrides {severity_limit}
1296+
--- - {severity_limit}: (DiagnosticSeverity)
1297+
--- - Limit severity of diagnostics found. E.g. "Warning" means { "Error", "Warning" } will be valid.
1298+
--- - {workspace}: (boolean, default true)
1299+
--- - Set the list with workspace diagnostics
1300+
function M.set_qflist(opts)
1301+
opts = opts or {}
1302+
opts.workspace = if_nil(opts.workspace, true)
1303+
local open_qflist = if_nil(opts.open, true)
1304+
local command = open_qflist and [[copen]] or nil
1305+
apply_to_diagnostic_items(util.set_qflist, command, opts)
1306+
end
1307+
1308+
--- Sets the location list
1309+
---@param opts table|nil Configuration table. Keys:
1310+
--- - {open}: (boolean, default true)
1311+
--- - Open loclist after set
1312+
--- - {client_id}: (number)
1313+
--- - If nil, will consider all clients attached to buffer.
1314+
--- - {severity}: (DiagnosticSeverity)
1315+
--- - Exclusive severity to consider. Overrides {severity_limit}
1316+
--- - {severity_limit}: (DiagnosticSeverity)
1317+
--- - Limit severity of diagnostics found. E.g. "Warning" means { "Error", "Warning" } will be valid.
1318+
--- - {workspace}: (boolean, default false)
1319+
--- - Set the list with workspace diagnostics
1320+
function M.set_loclist(opts)
1321+
opts = opts or {}
1322+
local open_loclist = if_nil(opts.open, true)
1323+
local command = open_loclist and [[lopen]] or nil
1324+
apply_to_diagnostic_items(util.set_loclist, command, opts)
1325+
end
1326+
12901327
--- Disable diagnostics for the given buffer and client
12911328
--- @param bufnr (optional, number): Buffer handle, defaults to current
12921329
--- @param client_id (optional, number): Disable diagnostics for the given

0 commit comments

Comments
 (0)