From 4455b656106759376bacd5e71ee3b264dbe1527d Mon Sep 17 00:00:00 2001 From: Yegappan Lakshmanan Date: Tue, 8 Mar 2022 14:22:09 -0800 Subject: [PATCH] Update the switch source header support --- autoload/lsp/handlers.vim | 4 ++++ autoload/lsp/lsp.vim | 1 + autoload/lsp/lspserver.vim | 1 + doc/lsp.txt | 4 +++- plugin/lsp.vim | 9 +++++++++ 5 files changed, 18 insertions(+), 1 deletion(-) diff --git a/autoload/lsp/handlers.vim b/autoload/lsp/handlers.vim index b6fe263..89b8ddf 100644 --- a/autoload/lsp/handlers.vim +++ b/autoload/lsp/handlers.vim @@ -147,8 +147,12 @@ def ProcessDefDeclReply(lspserver: dict, req: dict, reply: dict): enddef # process the 'textDocument/switchSourceHeader' reply from the LSP server +# Clangd specific extension # Result: URI | null def ProcessSwitchHeaderReply(lspserver: dict, req: dict, reply: dict): void + if reply.result->empty() + return + endif var fname = util.LspUriToFile(reply.result) if (&modified && !&hidden) || &buftype != '' # if the current buffer has unsaved changes and 'hidden' is not set, diff --git a/autoload/lsp/lsp.vim b/autoload/lsp/lsp.vim index 19abc90..e4f4c8b 100644 --- a/autoload/lsp/lsp.vim +++ b/autoload/lsp/lsp.vim @@ -216,6 +216,7 @@ export def GotoDefinition(peek: bool) enddef # Switch source header using "textDocument/switchSourceHeader" LSP request +# (Clangd specifc extension) export def SwitchSourceHeader() var lspserver: dict = CurbufGetServerChecked() if lspserver->empty() diff --git a/autoload/lsp/lspserver.vim b/autoload/lsp/lspserver.vim index e99d941..92ab3b1 100644 --- a/autoload/lsp/lspserver.vim +++ b/autoload/lsp/lspserver.vim @@ -479,6 +479,7 @@ enddef # Request: "textDocument/switchSourceHeader" # Param: TextDocumentIdentifier +# Clangd specific extension def SwitchSourceHeader(lspserver: dict) var req = lspserver.createRequest('textDocument/switchSourceHeader') req.params->extend({uri: util.LspFileToUri(@%)}) diff --git a/doc/lsp.txt b/doc/lsp.txt index 7f631ce..0ad70a0 100644 --- a/doc/lsp.txt +++ b/doc/lsp.txt @@ -319,7 +319,9 @@ diagnostic messages, you can add the following line to your .vimrc file: Default is false. *:LspSwitchSourceHeader* -:LspSwitchSourceHeader Switch between source and header files. +:LspSwitchSourceHeader Switch between source and header files. This is a + Clangd specific extension and only works with C/C++ + source files. *:LspDiagShow* :LspDiagShow Creates a new location list with the diagnostics diff --git a/plugin/lsp.vim b/plugin/lsp.vim index d305778..dd3f79e 100644 --- a/plugin/lsp.vim +++ b/plugin/lsp.vim @@ -6,6 +6,10 @@ if !has('patch-8.2.2342') finish endif +# The following is needed to support both Vim 8.2.3741 (shipped with Ubuntu +# 21.10) and the latest Vim. The Vim9 script syntax for import changed between +# these two versions. Once offical Vim9 is out, the following can be +# simplified. var opt = {} var lspf = {} if has('patch-8.2.4257') @@ -85,6 +89,7 @@ elseif has('patch-8.2.4019') lspf.hover = lsp_import.Hover lspf.selectionExpand = lsp_import.SelectionExpand lspf.selectionShrink = lsp_import.SelectionShrink + lspf.switchSourceHeader = lsp_import.SwitchSourceHeader lspf.foldDocument = lsp_import.FoldDocument lspf.listWorkspaceFolders = lsp_import.ListWorkspaceFolders lspf.addWorkspaceFolder = lsp_import.AddWorkspaceFolder @@ -121,6 +126,7 @@ else Hover, SelectionExpand, SelectionShrink, + SwitchSourceHeader, FoldDocument, ListWorkspaceFolders, AddWorkspaceFolder, @@ -163,6 +169,7 @@ else lspf.hover = Hover lspf.selectionExpand = SelectionExpand lspf.selectionShrink = SelectionShrink + lspf.switchSourceHeader = SwitchSourceHeader lspf.foldDocument = FoldDocument lspf.listWorkspaceFolders = ListWorkspaceFolders lspf.addWorkspaceFolder = AddWorkspaceFolder @@ -256,6 +263,8 @@ command! -nargs=0 -bar LspDiagHighlightEnable call TdiagHighlightEnable() command! -nargs=0 -bar LspDiagHighlightDisable call TdiagHighlightDisable() command! -nargs=0 -bar LspShowReferences call TshowReferences(v:false) command! -nargs=0 -bar LspPeekReferences call TshowReferences(v:true) +# Clangd specifc extension to switch from one C/C++ source file to a +# corresponding header file command! -nargs=0 -bar LspSwitchSourceHeader call TswitchSourceHeader() command! -nargs=0 -bar LspHighlight call LspDocHighlight() command! -nargs=0 -bar LspHighlightClear call LspDocHighlightClear() -- 2.48.1