diff options
| author | ranjithshegde <ranjithshegde@gmail.com> | 2021-11-29 15:05:53 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-11-29 09:05:53 -0500 |
| commit | 8436a197cc76f33fc2dc26145272c66cf5ee193d (patch) | |
| tree | 199d2043f614fd18026e6d8be8aee37d2eae4edf | |
| parent | docs: update server_configurations.md (diff) | |
| download | nvim-lspconfig-8436a197cc76f33fc2dc26145272c66cf5ee193d.tar nvim-lspconfig-8436a197cc76f33fc2dc26145272c66cf5ee193d.tar.gz nvim-lspconfig-8436a197cc76f33fc2dc26145272c66cf5ee193d.tar.bz2 nvim-lspconfig-8436a197cc76f33fc2dc26145272c66cf5ee193d.tar.lz nvim-lspconfig-8436a197cc76f33fc2dc26145272c66cf5ee193d.tar.xz nvim-lspconfig-8436a197cc76f33fc2dc26145272c66cf5ee193d.tar.zst nvim-lspconfig-8436a197cc76f33fc2dc26145272c66cf5ee193d.zip | |
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.
| -rw-r--r-- | lua/lspconfig/server_configurations/clangd.lua | 35 | ||||
| -rw-r--r-- | lua/lspconfig/server_configurations/texlab.lua | 55 | ||||
| -rw-r--r-- | lua/lspconfig/util.lua | 8 |
3 files changed, 60 insertions, 38 deletions
diff --git a/lua/lspconfig/server_configurations/clangd.lua b/lua/lspconfig/server_configurations/clangd.lua index 08cf307a..cda6ef43 100644 --- a/lua/lspconfig/server_configurations/clangd.lua +++ b/lua/lspconfig/server_configurations/clangd.lua @@ -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') diff --git a/lua/lspconfig/server_configurations/texlab.lua b/lua/lspconfig/server_configurations/texlab.lua index 4bcb6c81..1e43976a 100644 --- a/lua/lspconfig/server_configurations/texlab.lua +++ b/lua/lspconfig/server_configurations/texlab.lua @@ -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 diff --git a/lua/lspconfig/util.lua b/lua/lspconfig/util.lua index b6780aa9..c7f4eb53 100644 --- a/lua/lspconfig/util.lua +++ b/lua/lspconfig/util.lua @@ -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 |
