Skip to content

Commit

Permalink
[Backport release-0.7] fix(lsp): fix unnecessary buffers being added …
Browse files Browse the repository at this point in the history
…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 commit 26eb678)

* Add test

(cherry picked from commit d2e9dab)

Co-authored-by: ii14 <ii14@users.noreply.github.com>
  • Loading branch information
github-actions[bot] and ii14 committed May 8, 2022
1 parent 9e040ac commit 35075dc
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
8 changes: 6 additions & 2 deletions runtime/lua/vim/lsp/diagnostic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,19 @@ 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
end

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
Expand Down
39 changes: 39 additions & 0 deletions test/functional/plugin/lsp/diagnostic_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

0 comments on commit 35075dc

Please sign in to comment.