From 88c4c042e1e59f992e4c7aff3531033047b3aa9c Mon Sep 17 00:00:00 2001 From: glepnir Date: Thu, 2 Jan 2025 15:26:38 +0800 Subject: refactor(clangd): imporve switchsourceheader handler (#3537) --- lua/lspconfig/configs/ccls.lua | 5 +++-- lua/lspconfig/configs/clangd.lua | 30 +++++++++++++++--------------- 2 files changed, 18 insertions(+), 17 deletions(-) (limited to 'lua') diff --git a/lua/lspconfig/configs/ccls.lua b/lua/lspconfig/configs/ccls.lua index 9583fcb0..e64df3c6 100644 --- a/lua/lspconfig/configs/ccls.lua +++ b/lua/lspconfig/configs/ccls.lua @@ -1,13 +1,14 @@ local util = require 'lspconfig.util' local function switch_source_header(bufnr) + local method_name = 'textDocument/switchSourceHeader' bufnr = util.validate_bufnr(bufnr) local client = util.get_active_client_by_name(bufnr, 'ccls') if not client then - vim.notify('method textdocument/switchsourceheader is not supported by any servers active on the current buffer') + return vim.notify(('method %s is not supported by any servers active on the current buffer'):format(method_name)) end local params = vim.lsp.util.make_text_document_params(bufnr) - client.request('textDocument/switchsourceheader', params, function(err, result) + client.request(method_name, params, function(err, result) if err then error(tostring(err)) end diff --git a/lua/lspconfig/configs/clangd.lua b/lua/lspconfig/configs/clangd.lua index 20acc127..f85337fa 100644 --- a/lua/lspconfig/configs/clangd.lua +++ b/lua/lspconfig/configs/clangd.lua @@ -2,23 +2,23 @@ local util = require 'lspconfig.util' -- https://clangd.llvm.org/extensions.html#switch-between-sourceheader local function switch_source_header(bufnr) + local method_name = 'textDocument/switchSourceHeader' bufnr = util.validate_bufnr(bufnr) - local clangd_client = util.get_active_client_by_name(bufnr, 'clangd') - local params = { uri = vim.uri_from_bufnr(bufnr) } - if clangd_client then - clangd_client.request('textDocument/switchSourceHeader', params, 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' + local client = util.get_active_client_by_name(bufnr, 'ccls') + if not client then + return vim.notify(('method %s is not supported by any servers active on the current buffer'):format(method_name)) end + local params = vim.lsp.util.make_text_document_params(bufnr) + client.request(method_name, params, function(err, result) + if err then + error(tostring(err)) + end + if not result then + vim.notify('corresponding file cannot be determined') + return + end + vim.cmd.edit(vim.uri_to_fname(result)) + end, bufnr) end local function symbol_info() -- cgit v1.2.3-70-g09d2