chore: use client.request instead of buf_request (#1503)

* Use client.request directly instead of vim.lsp.buf_request to call methods
that are outside the official LSP specification, such as clangd's 
"textDocument/switchSourceHeader". 
* This avoids sending an inapplicable request to all servers, as we cannot
ahead of time validate the methods a given server supports.
This commit is contained in:
ranjithshegde 2021-11-29 15:05:53 +01:00 committed by GitHub
parent 760d1c7477
commit 8436a197cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 38 deletions

View file

@ -3,22 +3,27 @@ local util = require 'lspconfig.util'
-- https://clangd.llvm.org/extensions.html#switch-between-sourceheader
local function switch_source_header(bufnr)
bufnr = util.validate_bufnr(bufnr)
local clangd_client = util.get_active_client_by_name(bufnr, 'clangd')
local params = { uri = vim.uri_from_bufnr(bufnr) }
vim.lsp.buf_request(
bufnr,
'textDocument/switchSourceHeader',
params,
util.compat_handler(function(err, result)
if err then
error(tostring(err))
end
if not result then
print 'Corresponding file cannot be determined'
return
end
vim.api.nvim_command('edit ' .. vim.uri_to_fname(result))
end)
)
if clangd_client then
clangd_client.request(
'textDocument/switchSourceHeader',
params,
util.compat_handler(function(err, result)
if err then
error(tostring(err))
end
if not result then
print 'Corresponding file cannot be determined'
return
end
vim.api.nvim_command('edit ' .. vim.uri_to_fname(result))
end),
bufnr
)
else
print 'method textDocument/switchSourceHeader is not supported by any servers active on the current buffer'
end
end
local root_pattern = util.root_pattern('compile_commands.json', 'compile_flags.txt', '.git')

View file

@ -1,5 +1,4 @@
local util = require 'lspconfig.util'
local lsp = vim.lsp
local texlab_build_status = vim.tbl_add_reverse_lookup {
Success = 0,
@ -17,39 +16,49 @@ local texlab_forward_status = vim.tbl_add_reverse_lookup {
local function buf_build(bufnr)
bufnr = util.validate_bufnr(bufnr)
local texlab_client = util.get_active_client_by_name(bufnr, 'texlab')
local params = {
textDocument = { uri = vim.uri_from_bufnr(bufnr) },
}
lsp.buf_request(
bufnr,
'textDocument/build',
params,
util.compat_handler(function(err, result)
if err then
error(tostring(err))
end
print('Build ' .. texlab_build_status[result.status])
end)
)
if texlab_client then
texlab_client.request(
'textDocument/build',
params,
util.compat_handler(function(err, result)
if err then
error(tostring(err))
end
print('Build ' .. texlab_build_status[result.status])
end),
bufnr
)
else
print 'method textDocument/build is not supported by any servers active on the current buffer'
end
end
local function buf_search(bufnr)
bufnr = util.validate_bufnr(bufnr)
local texlab_client = util.get_active_client_by_name(bufnr, 'texlab')
local params = {
textDocument = { uri = vim.uri_from_bufnr(bufnr) },
position = { line = vim.fn.line '.' - 1, character = vim.fn.col '.' },
}
lsp.buf_request(
bufnr,
'textDocument/forwardSearch',
params,
util.compat_handler(function(err, result)
if err then
error(tostring(err))
end
print('Search ' .. texlab_forward_status[result.status])
end)
)
if texlab_client then
texlab_client.request(
'textDocument/forwardSearch',
params,
util.compat_handler(function(err, result)
if err then
error(tostring(err))
end
print('Search ' .. texlab_forward_status[result.status])
end),
bufnr
)
else
print 'method textDocument/forwardSearch is not supported by any servers active on the current buffer'
end
end
-- bufnr isn't actually required here, but we need a valid buffer in order to

View file

@ -414,4 +414,12 @@ function M.get_clients_from_cmd_args(arg)
return vim.tbl_values(result)
end
function M.get_active_client_by_name(bufnr, servername)
for _, client in pairs(vim.lsp.buf_get_clients(bufnr)) do
if client.name == servername then
return client
end
end
end
return M