import './textedit.vim'
import './options.vim' as opt
+var CommandHandlers: dict<func>
+
+export def RegisterCmdHandler(cmd: string, Handler: func)
+ CommandHandlers[cmd] = Handler
+enddef
+
+def DoCommand(lspserver: dict<any>, cmd: dict<any>)
+ if CommandHandlers->has_key(cmd.command)
+ var CmdHandler: func = CommandHandlers[cmd.command]
+ call CmdHandler(cmd)
+ else
+ lspserver.executeCommand(cmd)
+ endif
+enddef
+
export def HandleCodeAction(lspserver: dict<any>, selAction: dict<any>)
# textDocument/codeAction can return either Command[] or CodeAction[].
# If it is a CodeAction, it can have either an edit, a command or both.
textedit.ApplyWorkspaceEdit(selAction.edit)
endif
if selAction->has_key('command')
- lspserver.executeCommand(selAction.command)
+ DoCommand(lspserver, selAction.command)
endif
else
# selAction is a Command instance, apply it directly
- lspserver.executeCommand(selAction)
+ DoCommand(lspserver, selAction)
endif
enddef
import './symbol.vim'
import './outline.vim'
import './signature.vim'
+import './codeaction.vim'
# LSP server information
var lspServers: list<dict<any>> = []
return lspserver.tagFunc(pat, flags, info)
enddef
+export def RegisterCmdHandler(cmd: string, Handler: func)
+ codeaction.RegisterCmdHandler(cmd, Handler)
+enddef
+
# vim: tabstop=8 shiftwidth=2 softtabstop=2
var save_cursor: list<number> = getcurpos()
for [uri, changes] in workspaceEdit.changes->items()
- var fname: string = util.LspUriToFile(uri)
- var bnr: number = fname->bufnr()
- if bnr == -1
- # file is already removed
+ var bnr: number = util.LspUriToBufnr(uri)
+ if bnr == 0
+ # file is not present
continue
endif
return uri_decoded
enddef
+# Convert a LSP file URI (file://<absolute_path>) to a Vim buffer number.
+# If the file is not in a Vim buffer, then adds the buffer.
+# Returns 0 on error.
+export def LspUriToBufnr(uri: string): number
+ return LspUriToFile(uri)->bufadd()
+enddef
+
# Returns if the URI refers to a remote file (e.g. ssh://)
# Credit: vim-lsp plugin
export def LspUriRemote(uri: string): bool
:LspServerTrace { off | messages | verbose }
<
+==============================================================================
+11. Custom Command Handlers *lsp-custom-commands*
+
+When applying a code action, the language server may issue a non-standard
+command. For example, the Java language server uses non-standard commands
+(e.g. java.apply.workspaceEdit). To handle these commands, you can register a
+callback function for each command using the LspRegisterCmdHandler() function.
+For example: >
+
+ vim9script
+ import "../../lsp/autoload/lsp/textedit.vim"
+
+ def WorkspaceEdit(cmd: dict<any>)
+ for editAct in cmd.arguments
+ textedit.ApplyWorkspaceEdit(editAct)
+ endfor
+ enddef
+ g:LspRegisterCmdHandler('java.apply.worksspaceEdit', WorkspaceEdit)
+<
+Place the above code in a file named lsp_java/plugin/lsp_java.vim and load
+this plugin.
+
+The callback function should accept a Dict argument. The Dict argument
+contains the LSP Command interface fields. Refer to the LSP specification for
+more information about the "Command" interface.
+
vim:tw=78:ts=8:noet:ft=help:norl:
lsp.AddServer(serverList)
enddef
+def g:LspRegisterCmdHandler(cmd: string, Handler: func)
+ lsp.RegisterCmdHandler(cmd, Handler)
+enddef
+
# Returns true if the language server for the current buffer is initialized
# and ready to accept requests.
def g:LspServerReady(): bool