mirror of
https://github.com/neovim/neovim
synced 2025-07-16 09:11:51 +00:00
[Backport release-0.7] fix(lsp): fix unnecessary buffers being added on empty diagnostics (#18469)
* fix(lsp): fix unnecessary buffers being added on empty diagnostics Some language servers send empty `textDocument/publishDiagnostics` messages after indexing the project, sometimes resulting in creation of a lot of unnecessary buffers. As a workaround, skip empty messages for nonexistent buffers before resolving the filename to a bufnr. (cherry picked from commit26eb6785eb
) * Add test (cherry picked from commitd2e9dab377
) Co-authored-by: ii14 <ii14@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
9e040acfa3
commit
35075dcc22
@ -185,7 +185,12 @@ end
|
||||
function M.on_publish_diagnostics(_, result, ctx, config)
|
||||
local client_id = ctx.client_id
|
||||
local uri = result.uri
|
||||
local bufnr = vim.uri_to_bufnr(uri)
|
||||
local fname = vim.uri_to_fname(uri)
|
||||
local diagnostics = result.diagnostics
|
||||
if #diagnostics == 0 and vim.fn.bufexists(fname) == 0 then
|
||||
return
|
||||
end
|
||||
local bufnr = vim.fn.bufadd(fname)
|
||||
|
||||
if not bufnr then
|
||||
return
|
||||
@ -193,7 +198,6 @@ function M.on_publish_diagnostics(_, result, ctx, config)
|
||||
|
||||
client_id = get_client_id(client_id)
|
||||
local namespace = M.get_namespace(client_id)
|
||||
local diagnostics = result.diagnostics
|
||||
|
||||
if config then
|
||||
for _, opt in pairs(config) do
|
||||
|
@ -3,6 +3,7 @@ local helpers = require('test.functional.helpers')(after_each)
|
||||
local clear = helpers.clear
|
||||
local exec_lua = helpers.exec_lua
|
||||
local eq = helpers.eq
|
||||
local neq = require('test.helpers').neq
|
||||
|
||||
describe('vim.lsp.diagnostic', function()
|
||||
local fake_uri
|
||||
@ -227,5 +228,43 @@ describe('vim.lsp.diagnostic', function()
|
||||
eq(exec_lua([[return vim.str_byteindex(..., 7, true)]], line), result[1].col)
|
||||
eq(exec_lua([[return vim.str_byteindex(..., 8, true)]], line), result[1].end_col)
|
||||
end)
|
||||
|
||||
it('does not create buffer on empty diagnostics', function()
|
||||
local bufnr
|
||||
|
||||
-- No buffer is created without diagnostics
|
||||
bufnr = exec_lua [[
|
||||
vim.lsp.diagnostic.on_publish_diagnostics(nil, {
|
||||
uri = "file:///fake/uri2",
|
||||
diagnostics = {},
|
||||
}, {client_id=client_id})
|
||||
return vim.fn.bufnr(vim.uri_to_fname("file:///fake/uri2"))
|
||||
]]
|
||||
eq(bufnr, -1)
|
||||
|
||||
-- Create buffer on diagnostics
|
||||
bufnr = exec_lua [[
|
||||
vim.lsp.diagnostic.on_publish_diagnostics(nil, {
|
||||
uri = "file:///fake/uri2",
|
||||
diagnostics = {
|
||||
make_error('Diagnostic', 0, 0, 0, 0),
|
||||
},
|
||||
}, {client_id=client_id})
|
||||
return vim.fn.bufnr(vim.uri_to_fname("file:///fake/uri2"))
|
||||
]]
|
||||
neq(bufnr, -1)
|
||||
eq(exec_lua([[return #vim.diagnostic.get(...)]], bufnr), 1)
|
||||
|
||||
-- Clear diagnostics after buffer was created
|
||||
bufnr = exec_lua [[
|
||||
vim.lsp.diagnostic.on_publish_diagnostics(nil, {
|
||||
uri = "file:///fake/uri2",
|
||||
diagnostics = {},
|
||||
}, {client_id=client_id})
|
||||
return vim.fn.bufnr(vim.uri_to_fname("file:///fake/uri2"))
|
||||
]]
|
||||
neq(bufnr, -1)
|
||||
eq(exec_lua([[return #vim.diagnostic.get(...)]], bufnr), 0)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
Reference in New Issue
Block a user