1- local function switch_source_header_splitcmd (bufnr , splitcmd )
2- bufnr = require (" lspconfig" ).util .validate_bufnr (bufnr )
3- local clangd_client = require (" lspconfig" ).util .get_active_client_by_name (bufnr , " clangd" )
4- local params = { uri = vim .uri_from_bufnr (bufnr ) }
5- if clangd_client then
6- clangd_client .request (" textDocument/switchSourceHeader" , params , function (err , result )
7- if err then
8- error (tostring (err ))
9- end
10- if not result then
11- vim .notify (" Corresponding file can’t be determined" , vim .log .levels .ERROR , { title = " LSP Error!" })
12- return
13- end
14- vim .api .nvim_command (splitcmd .. " " .. vim .uri_to_fname (result ))
15- end , bufnr )
16- else
17- vim .notify (
18- " Method textDocument/switchSourceHeader is not supported by any active server attached to buffer" ,
1+ -- https://github.com/neovim/nvim-lspconfig/blob/master/lsp/clangd.lua
2+
3+ local function switch_source_header_splitcmd (bufnr , splitcmd , client )
4+ local method_name = " textDocument/switchSourceHeader"
5+ --- @diagnostic disable-next-line : param-type-mismatch
6+ if not client or not client :supports_method (method_name ) then
7+ return vim .notify (
8+ (" Method %s is not supported by any active server attached to buffer" ):format (method_name ),
199 vim .log .levels .ERROR ,
2010 { title = " LSP Error!" }
2111 )
2212 end
13+ local params = vim .lsp .util .make_text_document_params (bufnr )
14+ client :request (method_name , params , function (err , result )
15+ if err then
16+ error (tostring (err ))
17+ end
18+ if not result then
19+ vim .notify (" corresponding file cannot be determined" )
20+ return
21+ end
22+ vim .api .nvim_command (splitcmd .. " " .. vim .uri_to_fname (result ))
23+ end , bufnr )
24+ end
25+
26+ local function symbol_info (bufnr , client )
27+ local method_name = " textDocument/symbolInfo"
28+ --- @diagnostic disable-next-line : param-type-mismatch
29+ if not client or not client :supports_method (method_name ) then
30+ return vim .notify (" Clangd client not found" , vim .log .levels .ERROR )
31+ end
32+ local win = vim .api .nvim_get_current_win ()
33+ local params = vim .lsp .util .make_position_params (win , client .offset_encoding )
34+ --- @diagnostic disable-next-line : param-type-mismatch
35+ client :request (method_name , params , function (err , res )
36+ if err or # res == 0 then
37+ -- Clangd always returns an error, there is no reason to parse it
38+ return
39+ end
40+ local container = string.format (" container: %s" , res [1 ].containerName ) --- @type string
41+ local name = string.format (" name: %s" , res [1 ].name ) --- @type string
42+ vim .lsp .util .open_floating_preview ({ name , container }, " " , {
43+ height = 2 ,
44+ width = math.max (string.len (name ), string.len (container )),
45+ focusable = false ,
46+ focus = false ,
47+ title = " Symbol Info" ,
48+ })
49+ end , bufnr )
2350end
2451
2552local function get_binary_path_list (binaries )
3663-- https://github.com/neovim/nvim-lspconfig/blob/master/lua/lspconfig/configs/clangd.lua
3764return function (defaults )
3865 vim .lsp .config (" clangd" , {
39- on_attach = defaults .on_attach ,
4066 capabilities = vim .tbl_deep_extend (" keep" , { offsetEncoding = { " utf-16" , " utf-8" } }, defaults .capabilities ),
4167 single_file_support = true ,
4268 cmd = {
@@ -57,25 +83,22 @@ return function(defaults)
5783 " --limit-results=300" ,
5884 " --pch-storage=memory" ,
5985 },
60- commands = {
61- ClangdSwitchSourceHeader = {
62- function ()
63- switch_source_header_splitcmd (0 , " edit" )
64- end ,
65- description = " Open source/header in current buffer" ,
66- },
67- ClangdSwitchSourceHeaderVSplit = {
68- function ()
69- switch_source_header_splitcmd (0 , " vsplit" )
70- end ,
71- description = " Open source/header in a new vsplit" ,
72- },
73- ClangdSwitchSourceHeaderSplit = {
74- function ()
75- switch_source_header_splitcmd (0 , " split" )
76- end ,
77- description = " Open source/header in a new split" ,
78- },
79- },
86+ on_attach = function (client , bufnr )
87+ vim .api .nvim_buf_create_user_command (bufnr , " LspClangdSwitchSourceHeader" , function ()
88+ switch_source_header_splitcmd (bufnr , " edit" , client )
89+ end , { desc = " Open source/header in a new vsplit" })
90+
91+ vim .api .nvim_buf_create_user_command (bufnr , " LspClangdSwitchSourceHeaderVsplit" , function ()
92+ switch_source_header_splitcmd (bufnr , " vsplit" , client )
93+ end , { desc = " Open source/header in a new vsplit" })
94+
95+ vim .api .nvim_buf_create_user_command (bufnr , " LspClangdSwitchSourceHeaderSplit" , function ()
96+ switch_source_header_splitcmd (bufnr , " split" , client )
97+ end , { desc = " Open source/header in a new split" })
98+
99+ vim .api .nvim_buf_create_user_command (bufnr , " LspClangdShowSymbolInfo" , function ()
100+ symbol_info (bufnr , client )
101+ end , { desc = " Show symbol info" })
102+ end ,
80103 })
81104end
0 commit comments