Skip to content

Commit

Permalink
fix(lsp): remove call to deprecated vim.lsp.diagnostic (fixes #31)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Dec 11, 2021
1 parent 2482556 commit e02059b
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 53 deletions.
56 changes: 52 additions & 4 deletions lua/aerial/backends/lsp/callbacks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,33 @@ M.handle_symbols = function(result, bufnr)
backends.set_symbols(bufnr, process_symbols(result, bufnr))
end

local function get_error_count(bufnr, client_id)
-- Neovim 0.6+
if vim.diagnostic then
return #vim.diagnostic.get(bufnr, {
severity = vim.lsp.protocol.DiagnosticSeverity.Error,
namespace = vim.lsp.diagnostic.get_namespace(client_id),
})
else
-- Neovim < 0.6
return vim.lsp.diagnostic.get_count(bufnr, "Error")
end
end

local results = {}
M.symbol_callback = function(_err, result, context, _config)
local client_id = context.client_id
if not result or vim.tbl_isempty(result) then
return
end
local bufnr = context.bufnr
-- Don't update if there are diagnostics errors (or override by setting)
local error_count = vim.lsp.diagnostic.get_count(bufnr, "Error")
local has_symbols = data:has_symbols(bufnr)
if not config.lsp.update_when_errors and error_count > 0 and has_symbols then
-- Don't update if there are diagnostics errors, unless config option is set
-- or we have no symbols for this buffer
if
not config.lsp.update_when_errors
and data:has_symbols(bufnr)
and get_error_count(bufnr, client_id) > 0
then
return
end

Expand All @@ -96,4 +113,35 @@ M.symbol_callback = function(_err, result, context, _config)
results[bufnr] = result
end

M.on_publish_diagnostics = function(_err, result, ctx, _config)
local client_id = ctx.client_id
local client = vim.lsp.get_client_by_id(client_id)
local uri = result.uri
local bufnr = vim.uri_to_bufnr(uri)
if
not bufnr
or not backends.is_backend_attached(bufnr, "lsp")
or not config.lsp.diagnostics_trigger_update
or not client.resolved_capabilities.document_symbol
then
return
end

-- Don't update if there are diagnostics errors, unless config option is set
-- or we have no symbols for this buffer
if not config.lsp.update_when_errors and data:has_symbols(bufnr) then
for _, diagnostic in ipairs(result.diagnostics) do
local severity = diagnostic.severity
if type(severity) == "string" then
severity = vim.lsp.protocol.DiagnosticSeverity[diagnostic.severity]
end
if severity == vim.lsp.protocol.DiagnosticSeverity.Error then
return
end
end
end

backends.get(bufnr).fetch_symbols()
end

return M
72 changes: 23 additions & 49 deletions lua/aerial/backends/lsp/init.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
local backends = require("aerial.backends")
local callbacks = require("aerial.backends.lsp.callbacks")
local config = require("aerial.config")
local data = require("aerial.data")
local M = {}

-- callback args changed in Neovim 0.6. See:
Expand All @@ -24,21 +23,30 @@ local function mk_handler(fn)
end
end

local has_hook = false
local function add_handler(preserve_callback)
if has_hook then
return
end
has_hook = true
local old_callback = vim.lsp.handlers["textDocument/documentSymbol"]
local function replace_handler(name, callback, preserve_callback)
local old_callback = vim.lsp.handlers[name]
local new_callback
new_callback = function(...)
mk_handler(callbacks.symbol_callback)(...)
mk_handler(callback)(...)
if preserve_callback then
old_callback(...)
end
end
vim.lsp.handlers["textDocument/documentSymbol"] = new_callback
vim.lsp.handlers[name] = new_callback
end

local has_hook = false
local function hook_handlers(preserve_symbol_callback)
if has_hook then
return
end
has_hook = true
replace_handler(
"textDocument/documentSymbol",
callbacks.symbol_callback,
preserve_symbol_callback
)
replace_handler("textDocument/publishDiagnostics", callbacks.on_publish_diagnostics, true)
end

M.fetch_symbols = function()
Expand Down Expand Up @@ -84,38 +92,15 @@ M.on_attach = function(client, bufnr, opts)
bufnr = 0
end
opts = opts or {}
if not client.resolved_capabilities.document_symbol then
return
if client.resolved_capabilities.document_symbol then
hook_handlers(opts.preserve_callback)
mark_lsp_attached(bufnr)
backends.attach(bufnr, true)
end
add_handler(opts.preserve_callback)
mark_lsp_attached(bufnr)
backends.attach(bufnr, true)
end

M.attach = function(bufnr)
if config.lsp.diagnostics_trigger_update then
local cmd = [[lua require'aerial.backends.lsp'._on_diagnostics_changed()]]
if vim.fn.exists("##DiagnosticChanged") == 1 then
vim.cmd(string.format(
[[augroup AerialDiagnostics
au! * <buffer>
au DiagnosticChanged <buffer> %s
augroup END
]],
cmd
))
else
vim.cmd(string.format(
[[augroup AerialDiagnostics
au!
au User LspDiagnosticsChanged %s
augroup END
]],
cmd
))
end
end
if config.open_automatic() and not config.lsp.diagnostics_trigger_update then
if config.open_automatic(bufnr) and not config.lsp.diagnostics_trigger_update then
M.fetch_symbols()
end
end
Expand All @@ -124,15 +109,4 @@ M.detach = function(bufnr)
-- pass
end

M._on_diagnostics_changed = function()
if not backends.is_backend_attached(0, "lsp") then
return
end
local errors = vim.lsp.diagnostic.get_count(0, "Error")
-- if no errors, refresh symbols
if config.lsp.update_when_errors or errors == 0 or not data:has_symbols() then
M.fetch_symbols()
end
end

return M

0 comments on commit e02059b

Please sign in to comment.